Polymorphic type in Haskell

The parameters and result of functions in Haskell can be one “concerate” type, e.g., BoolInt, etc; or polymorphic type, i.e., not a specified type, represented by a type variable, such as ab, 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”.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.