[Haskell-cafe] Polymorphic functions over string libraries

John Lato jwlato at gmail.com
Tue Oct 28 00:16:12 UTC 2014


ListLike and mono-traversable both provide their own versions of Functor
and Foldable (I don't think ListLike has Traversable, exactly, but it
could).  The problem is that data  structures like ByteString and Text
simply don't admit instances of these classes.  In fact, that's exactly
what mono-traversable is: a monomorphic variant of Traversable (and hence
the others as well).

It's entirely possible to write something like

> someFunction :: (ListLike (f el) el, Foldable f) => f el -> a

and it may even be sensible, but it means you can't use e.g. Text as an
argument to someFunction.

I don't believe mono-traversable provides

> instance Functor f => MonoFunctor (f a)

that instance is problematic because it overlaps with many other instances
one could wish to write.  However, there are instances for pretty much
every type in base that makes sense, so you can pass any particular Functor
to code that expects a MonoFunctor and it would work.

If you're concerned about getting stuck with a particular library,
classy-prelude seems like exactly the wrong choice, as it makes a lot of
this implicit rather than explicit, which means it would be harder to
migrate away from if you choose to do so later.  Besides, it uses
mono-traversable under the hood anyway.

John L.

On Tue, Oct 28, 2014 at 7:05 AM, gonzaw <gonzaw308 at gmail.com> wrote:

> Yes, I don't explicitly want to make any assumptions about encoding. I want
> to make it general enough, so that if I encounter a situation where I DO
> have to make assumptions about the encoding, I can easily use Text (instead
> of whatever I was using before). If I have to start making assumptions
> about
> other things, then I could easily change it to other string libraries.
>
> Thanks for the responses, I think switching to classy-prelude might be the
> easiest one (it also forces good practices).
>
> How do those other libraries compose with Prelude, and other modules from
> base? For instance, does ListLike compose well with Data.Foldable?
> Same with mono-traversable. Does MonoFunctor compose well with Functor? Is
> there an easy "glue" code to, for instance, pass any Functor to a function
> that expects a MonoFunctor and make it work? Idem with other typeclasses.
>
> Basically, I think all of these seem great, but I'm concerned about getting
> "stuck" with them alone, and not being able to use all the other countless
> great libraries with them because of compatibility issues.
>
>
>
> --
> View this message in context:
> http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libraries-tp5758630p5758747.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141028/8aa292e3/attachment.html>


More information about the Haskell-Cafe mailing list