<div dir="ltr"><div>Hi Andrey,</div><div><br></div><div>FWIW, mono-traversable (<a href="http://hackage.haskell.org/package/mono-traversable">http://hackage.haskell.org/package/mono-traversable</a>) suggests decoupling IsSet and Funtor-like. <br></div><div><br></div><div>In a nutshell, they define the IsSet class (in Data.Containers) with typical set operations like member and singleton, union and intersection. And then they tackle a (seemingly) independent problem of mapping monomorphic containers (like IntSet, ByteString, etc.) with a separate class MonoFunctor (in Data.MonoTraversable):</div><div><br></div><div>class MonoFunctor mono where<br>    omap :: (Element mono -> Element mono) -> mono -> mono</div><div><br></div><div>And gazillion of instances for both polymorphic containers with a fixed type parameter and monomorphic ones.</div><div><br></div><div>--</div><div>Best wishes,</div><div>Artem<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 30 May 2019 at 20:11, Andrey Mokhov <<a href="mailto:andrey.mokhov@newcastle.ac.uk">andrey.mokhov@newcastle.ac.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
I tried to use type classes for unifying APIs of several similar data structures and it didn't work well. (In my case I was working with graphs, instead of sets or maps.)<br>
<br>
First, you rarely want to be polymorphic over the set representation, because you care about performance. You really want to use that Very.Special.Set.insert because it has the right performance characteristics for your task at hand. I found only *one* use-case for writing polymorphic functions operating on something like IsSet: the testsuite. Of course, it is very nice to write a single property test like<br>
<br>
memberInsertProperty x set = (member x (insert x set) == True)<br>
<br>
and then use it for testing all set data structures that implement `member` and `insert`. Here you don't care about performance, only about correctness!<br>
<br>
However, this approach leads to problems with type inference, confusing error messages, and complexity. I found that it is much nicer to use explicit dictionary passing and write something like this instead:<br>
<br>
memberInsertProperty SetAPI{..} x set = (member x (insert x set) == True)<br>
<br>
where `member` and `insert` come from the SetAPI record via RecordWildCards. <br>
<br>
Finally, I'm not even sure how to create a type class covering Set and IntSet with the following two methods:<br>
<br>
singleton :: a -> Set a<br>
map :: Ord b => (a -> b) -> Set a -> Set b<br>
<br>
singleton :: Int -> IntSet<br>
map :: (Int -> Int) -> IntSet -> IntSet<br>
<br>
Could anyone please enlighten me about the right way to abstract over this using type classes?<br>
<br>
I tried a few approaches, for example:<br>
<br>
class IsSet s where<br>
    type Elem s<br>
    singleton :: Elem s -> s<br>
    map :: Ord (Elem t) => (Elem s -> Elem t) -> s -> t<br>
<br>
Looks nice, but I can't define the IntSet instance:<br>
<br>
instance IsSet IntSet where<br>
    type Elem IntSet = Int <br>
    singleton = IntSet.singleton<br>
    map = IntSet.map<br>
<br>
This fails with: Couldn't match type `t' with `IntSet' -- and indeed, how do I tell the compiler that in the IntSet case s ~ t in the map signature? Shall I add more associated types, or "associated constraints" using ConstraintKinds? I tried and failed, at various stages, repeatedly.<br>
<br>
...And then you discover that there is Set.cartesianProduct :: Set a -> Set b -> Set (a, b), but no equivalent in IntSet and things get even more grim.<br>
<br>
Cheers,<br>
Andrey<br>
<br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div>