The parameters and result of functions in Haskell can be one “concerate” type, e.g., Bool
, Int
, etc; or polymorphic type, i.e., not a specified type, represented by a type variable, such as a
, b
, and so on.
Take length
function as an example:
length :: [a] -> Int
The return value must be Int
type, but the input parmater is a list which can contain any type. But if the polymorphic type is constrained as a set of types, e.g. (+)
:
(+) :: Num a => a -> a -> a
Num
is called “type class”, it means not all types can be use in (+)
except the ones which belong to Num
. This “constrianed” polynomical type (Num a
in (+)
) is referred as “Ad hoc polymorphism”, while “no constrianed” one (a
in length
) is referred as “Parametric polymorphism”.