snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
152 stars 63 forks source link

Wrapped sets and maps #171

Open treeowl opened 5 years ago

treeowl commented 5 years ago

A map from elements of type k to () can be viewed as a set. Indeed, this is how Data.HashSet works. Less usefully, I imagine, a set of elements of type k can also be viewed as a map from elements of type k to (). Should we offer newtype wrappers implementing this correspondence?

snoyberg commented 5 years ago

What would these look like? What would be the advantage for the user?

treeowl commented 5 years ago

I'm not sure what you mean about what they'd look like. For the wrapped map case, the purpose would be to let libraries depending on this one magically turn their map implementations into set implementations. Applications the other way are definitely not as obvious.

snoyberg commented 5 years ago

I mean what are you proposing as the newtype wrapper here? I'm not sure what practically benefit you're going for here. Is the idea to get to elide definitions of data types like HashSet completely, and just rely on a newtype wrapper?

treeowl commented 5 years ago

Sounds about right.

newtype WrappedMap m = WrappedMap {unwrappedMap :: m}
newtype HashSet a = HashSet (WrappedMap (HashMap a ()))
  deriving ({- all the instances -})
snoyberg commented 5 years ago

What's the purpose of the newtype WrappedMap here?

treeowl commented 5 years ago

It's just a target for GND. The relationship between a set of a and a map from a to () has to be implemented somewhere... There's no (inherent) need to implement it separately and by hand for each map type.

treeowl commented 5 years ago

Obviously unordered-containers will not be incurring a mono-traversable dependency, but I feel like this wrapper should exist as a matter of principle.

snoyberg commented 5 years ago

Arguments on principle aren't really too interesting to me, I'm sorry. I'm much more interested in code being useful in some kind of real-world case.