I'm in the process of learning Haskell, and type classes seem like a powerful way to make type-safe polymorphic functions. But a lot of the Haskell Prelude functions don't use them. More specifically:
Most of the list functions don't work with other data structures (for instance, foldr
and length
are only implemented for lists and can't be used on arrays).
Modules like Data.ByteString
are unusable unless you use import qualified
since they include functions that have the same names as Prelude functions.
It seems like both of these problems would go away if the standard library used generic functions with type classes (please let me know if I'm totally off base with this).
I have two questions:
Are there technical or design reasons that the Prelude is like this, or is it just for historical reasons?
Looking around, it looks like there are a
couple of libraries (like
Data.Foldable
and, if I'm not
mistaken, Scrap Your Boilerplate)
that replace the standard Prelude functions
with generic alternatives. Are
there any plans to incorporate these ideas into future versions of Haskell?
There is a very good pragmatic reason that "standard" Haskell (Prelude + base + maybe some more) doesn't use more polymorphism:
Designing general-use type classes is hard. Good designs for classes that abstract over container types like lists, arrays and "bytestrings" (personally I don't really consider Bytestring a container) aren't floating round waiting to be included in Haskell 2012. There are some designs e.g Listlike and the Edison classes, and a number of people have chipped away at the problem but excepting Foldable and Traversable no-one has produced any compelling designs.