Example of "True Polymorphism"? (Preferably, using Haskell)

c++, haskell, parametric-polymorphism, polymorphism

Solution

The term you are looking for is "parametric polymorphism", which is different from "ad-hoc polymorphism".

An example of parametric polymorphism is in the type signature for `Nothing`:

Nothing :: Maybe a

The `a` in the type could be any conceivable type, since `Nothing` inhabits all `Maybe`s. We say that `a` is parametrically polymorphic because it can be any type.

Now consider this type:

Just 1 :: (Num b) => Maybe b

This time the `b` cannot be any type: it can only be a type that is an instance of `Num`. We say that `b` is ad-hoc polymorphic because it can be any member of a set of types, given by the instances of the `Num` class.

So, to recap:

Parametric polymorphism: Can be any type

Ad-hoc polymorphism: Constrained by a type-class

Problem

I've seen lots of partial definitions of "True Polymorphism", for example here and here but nowhere have I been able to find a clear example of the difference with two concrete examples. I understand that overloading the `+` operator is some form of polymorphism and that it is implemented differently in Haskell and C++. Can someone show precisely what the difference is with examples in both languages?

Original source