Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't the Haskell standard libraries make more use of polymorphism?

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:

  1. Are there technical or design reasons that the Prelude is like this, or is it just for historical reasons?

  2. 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?

like image 943
shosti Avatar asked Jan 24 '11 05:01

shosti


People also ask

What makes Haskell different from the languages that came before it?

Immutability and purity. Most programming languages out there default to mutability: a variable or field in a data structure can be changed at any time. Haskell is different in two ways: Values are immutable by default, and mutability must be explicitly indicated with a variable type.

Is Haskell object oriented?

Haskell isn't an object-oriented language. All of the functionality built here from scratch already exists in a much more powerful form, using Haskell's type system.

Why is Haskell better?

Although it's not as popular as Python/Java/C++, Haskell has many benefits compared to them: Concise, high-level, practical and also very fast. An advanced system, which provides a lot of extra safety and flexibility. Concurrency is easy compared to many other languages.

Why is Haskell important?

Haskell is the main technology that helps us deliver high quality software. There are various criteria to judge software quality, but the most important ones are correctness, performance, and maintainability. Haskell facilitates writing code that scores high on all of these accounts: Correctness.


2 Answers

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.

like image 135
stephen tetley Avatar answered Oct 04 '22 00:10

stephen tetley


The Haskell base library used to be more polymorphic - list comprehensions used to work for any monad, map and ++ weren't limited to List, and perhaps other things.

But folks at the time thought that it led to confusing error messages for beginners and that folks who aren't beginners can use the specifically polymorphic versions.

like image 30
Antoine Latter Avatar answered Oct 03 '22 23:10

Antoine Latter