4

In Graham Hutton's book "Programming in Haskell", he defines a function to be polymorphic, if its type "contains one or more type variables". He then defines a function to be overloaded, if its type "contains one or more class constraints".

From these definitions, my question is, "can a function be polymorphic and overloaded?".

For example, the function elem in the standard prelude, essentially has a type of Eq a => [a] -> Bool. Can we say that this function is both polymorphic and overloaded?

EDIT:

Graham Hutton said "Given that class constraints can only be used to type variables, my answer would be yes, a function can be called both polymorphic and overloaded. In particular, any overloaded function is by these definitions also polymorphic. However, I can understand why someone may wish to keep a distinction between these two terms. For the purposes of the book I wanted to have clear and simple definitions."

Haskell Wiki page, states that elem is an example of "ad-hoc polymorphism", whilst something like id :: a -> a, is an example of "parametric polymorphism".

Ryan Smith
  • 151
  • 5

2 Answers2

4

An "overloaded" function has to be polymorphic, because it's impossible to write a class constraint unless the function signature already contains a type variable. Based on this, I wouldn't say that overloaded is a very useful concept in Haskell.

Ryan Reich
  • 264
  • 1
  • 2
  • 4
0

Yes. Following the definition the answer is trivial. If A => B and C => D, then A⋀C => B⋀D

sara
  • 2,549
  • 15
  • 23