<div dir="ltr">I'm not sure if this is related but the package <a href="http://hackage.haskell.org/package/map-classes-0.1.0.0/docs/Control-Class-Impl-Map.html">Map-Classes</a> provides about 50 functions on around a dozen key/value like datatypes e.g. Arrays, Maps, Sets (value is ()) etc. Even ByteStrings are included (Int -> Word8 mapping). <div><br></div><div>You should be able to fairly easily add new types and even new functions to the instances if you give them default implementations.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, May 31, 2019 at 9:23 AM 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">Thanks again Iavor,<br>
<br>
Despite the type inference issue, and the fact that this requires a separate type class, this is the best solution I've seen so far.<br>
<br>
Cheers,<br>
Andrey<br>
<br>
-----Original Message-----<br>
From: Iavor Diatchki [mailto:<a href="mailto:iavor.diatchki@gmail.com" target="_blank">iavor.diatchki@gmail.com</a>] <br>
Sent: 30 May 2019 23:16<br>
To: Andrey Mokhov <<a href="mailto:andrey.mokhov@newcastle.ac.uk" target="_blank">andrey.mokhov@newcastle.ac.uk</a>><br>
Cc: Brandon Allbery <<a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>>; Andreas Klebinger <<a href="mailto:klebinger.andreas@gmx.at" target="_blank">klebinger.andreas@gmx.at</a>>; <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
Subject: Re: Container type classes<br>
<br>
Yeah, there is really no relation between the two parameters of `Fun`,<br>
so you'd have to specify the intermediate type manually. For example:<br>
<br>
add3 :: forall s. (Fun s s, Elem s ~ Int) => s -> s<br>
add3 = colMap @s (+1) . colMap (+2)<br>
<br>
I wouldn't say that it's a particularly convenient interface to work<br>
with, unless you are working in a setting where most of the containers<br>
have known types.<br>
<br>
<br>
On Thu, May 30, 2019 at 2:58 PM Andrey Mokhov<br>
<<a href="mailto:andrey.mokhov@newcastle.ac.uk" target="_blank">andrey.mokhov@newcastle.ac.uk</a>> wrote:<br>
><br>
> Many thanks Iavor,<br>
><br>
> This looks very promising! I played with your encoding a little, but quickly came across type inference issues. The following doesn't compile:<br>
><br>
> add3 :: (Fun s s, Elem s ~ Int) => s -> s<br>
> add3 = colMap (+1) . colMap (+2)<br>
><br>
> I'm getting:<br>
><br>
> * Could not deduce: Elem a0 ~ Int<br>
> from the context: (Fun s s, Elem s ~ Int)<br>
> bound by the type signature for:<br>
> add3 :: forall s. (Fun s s, Elem s ~ Int) => s -> s<br>
> Expected type: Elem a0 -> Elem s<br>
> Actual type: Int -> Int<br>
> The type variable `a0' is ambiguous<br>
><br>
> Fun s s is supposed to say that the intermediate type is `s` too, but I guess this is not how type class resolution works.<br>
><br>
> Cheers,<br>
> Andrey<br>
><br>
> -----Original Message-----<br>
> From: Iavor Diatchki [mailto:<a href="mailto:iavor.diatchki@gmail.com" target="_blank">iavor.diatchki@gmail.com</a>]<br>
> Sent: 30 May 2019 22:38<br>
> To: Brandon Allbery <<a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>><br>
> Cc: Andrey Mokhov <<a href="mailto:andrey.mokhov@newcastle.ac.uk" target="_blank">andrey.mokhov@newcastle.ac.uk</a>>; Andreas Klebinger <<a href="mailto:klebinger.andreas@gmx.at" target="_blank">klebinger.andreas@gmx.at</a>>; <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
> Subject: Re: Container type classes<br>
><br>
> This is how you could define `map`. This is just for fun, and to<br>
> discuss Haskell idioms---I am not suggesting we should do it. Of<br>
> course, it might be a bit more general than what you'd like---for<br>
> example it allows defining instances like `Fun IntSet (Set Int)` that,<br>
> perhaps?, you'd like to disallow:<br>
><br>
> {-# LANGUAGE MultiParamTypeClasses, TypeFamilies #-}<br>
><br>
> import Data.Set (Set)<br>
> import qualified Data.Set as Set<br>
> import Data.IntSet (IntSet)<br>
> import qualified Data.IntSet as ISet<br>
><br>
> class Col t where<br>
> type Elem t<br>
> -- ... As in Andreas's example<br>
><br>
> class (Col a, Col b) => Fun a b where<br>
> colMap :: (Elem a -> Elem b) -> a -> b<br>
><br>
> instance Col (Set a) where<br>
> type Elem (Set a) = a<br>
><br>
> instance Col IntSet where<br>
> type Elem IntSet = Int<br>
><br>
> instance Fun IntSet IntSet where<br>
> colMap = ISet.map<br>
><br>
> instance Ord b => Fun (Set a) (Set b) where<br>
> colMap = Set.map<br>
><br>
> On Thu, May 30, 2019 at 2:32 PM Brandon Allbery <<a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>> wrote:<br>
> ><br>
> > They can, with more work. You want indexed monads, so you can describe types that have e.g. an ordering constraint as well as the Monad constraint.<br>
> ><br>
> > On Thu, May 30, 2019 at 5:26 PM Andrey Mokhov <<a href="mailto:andrey.mokhov@newcastle.ac.uk" target="_blank">andrey.mokhov@newcastle.ac.uk</a>> wrote:<br>
> >><br>
> >> Hi Artem,<br>
> >><br>
> >><br>
> >><br>
> >> Thanks for the pointer, but this doesn’t seem to be a solution to my challenge: they simply give up on overloading `map` for both Set and IntSet. As a result, we can’t write polymorphic functions over Set and IntSet if they involve any mapping.<br>
> >><br>
> >><br>
> >><br>
> >> I looked at the prototype by Andreas Klebinger, and it doesn’t include the method `setMap` either.<br>
> >><br>
> >><br>
> >><br>
> >> Perhaps, Haskell’s type classes just can’t cope with this problem.<br>
> >><br>
> >><br>
> >><br>
> >> *ducks for cover*<br>
> >><br>
> >><br>
> >><br>
> >> Cheers,<br>
> >><br>
> >> Andrey<br>
> >><br>
> >><br>
> >><br>
> >> From: Artem Pelenitsyn [mailto:<a href="mailto:a.pelenitsyn@gmail.com" target="_blank">a.pelenitsyn@gmail.com</a>]<br>
> >> Sent: 30 May 2019 20:56<br>
> >> To: Andrey Mokhov <<a href="mailto:andrey.mokhov@newcastle.ac.uk" target="_blank">andrey.mokhov@newcastle.ac.uk</a>><br>
> >> Cc: <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>; Andreas Klebinger <<a href="mailto:klebinger.andreas@gmx.at" target="_blank">klebinger.andreas@gmx.at</a>><br>
> >> Subject: Re: Container type classes<br>
> >><br>
> >><br>
> >><br>
> >> Hi Andrey,<br>
> >><br>
> >><br>
> >><br>
> >> FWIW, mono-traversable (<a href="http://hackage.haskell.org/package/mono-traversable" rel="noreferrer" target="_blank">http://hackage.haskell.org/package/mono-traversable</a>) suggests decoupling IsSet and Funtor-like.<br>
> >><br>
> >><br>
> >><br>
> >> 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):<br>
> >><br>
> >><br>
> >><br>
> >> class MonoFunctor mono where<br>
> >> omap :: (Element mono -> Element mono) -> mono -> mono<br>
> >><br>
> >><br>
> >><br>
> >> And gazillion of instances for both polymorphic containers with a fixed type parameter and monomorphic ones.<br>
> >><br>
> >><br>
> >><br>
> >> --<br>
> >><br>
> >> Best wishes,<br>
> >><br>
> >> Artem<br>
> >><br>
> >><br>
> >><br>
> >> On Thu, 30 May 2019 at 20:11, Andrey Mokhov <<a href="mailto:andrey.mokhov@newcastle.ac.uk" target="_blank">andrey.mokhov@newcastle.ac.uk</a>> wrote:<br>
> >><br>
> >> 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>
> >><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>
> ><br>
> ><br>
> ><br>
> > --<br>
> > brandon s allbery kf8nh<br>
> > <a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a><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>
_______________________________________________<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>