Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no typeclass for container-types?

Tags:

haskell

I found out that some containers have a very similar function set. List, Set, Sequence, Text and Bytestrings for example. I wonder why they don't use one or more common typeclasses.

like image 235
Vektorweg Avatar asked Jul 17 '13 16:07

Vektorweg


3 Answers

What common function set do they have? AFAICS, only the ability to combine multiple containers into a single one, and to generate empty containers. And behold, indeed they're all Monoid, which offers exactly that interface!

You can't really do anything else with all of these since some are polymorphic, some monomorphic.

like image 132
leftaroundabout Avatar answered Oct 06 '22 04:10

leftaroundabout


Actually there are, see question Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations), which is almost a duplicate of yours.

The main reason for being in a separate package is probably that it needs a language extension - either functional dependencies or type families. We have to somehow say that Text can contain only Chars, ByteString can contain only Word8s, [] can contain any type, and Set can contain only instances of Ord.

like image 27
Petr Avatar answered Oct 06 '22 04:10

Petr


Polymorphic containers like list, Sequence, Maybe and Map are instances of Functor, Foldable and Traversable typeclasses.

For Set you have only Foldable, as Ord instance makes it impossible to define others (you can define fmap (Data.Set.map) which would obey functor laws, but it's signature would require Ord instances on arguments).

As mentioned before, Text and Bytestring can be instances of similar typeclasses defined, but you will need either functional dependencies or type families.

like image 45
phadej Avatar answered Oct 06 '22 05:10

phadej