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.
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.
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 Char
s, ByteString
can contain only Word8
s, []
can contain any type, and Set
can contain only instances of Ord
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With