Personal tools

Polymorphism

From HaskellWiki

Revision as of 18:01, 30 April 2012 by Benmachine (Talk | contribs)

Jump to: navigation, search

A value is polymorphic if, depending on the context where it's used, it can take on more than one type.

There are different kinds of polymorphism.

  1. Parametric polymorphism; mostly found in functional languages
  2. Ad-hoc polymorphism or overloading
  3. Inclusion polymorphism; mostly found in object oriented languages

1 Examples

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr
is a parametrically polymorphic function. When actually used, it may take on any of a variety of types, for example:
:: (Char -> Int -> Int) -> Int -> String -> Int -- a = Char, b = Int
:: (String -> String -> String) -> String -> [String] -> String -- a = b = String

Numeric literals are overloaded (i.e. subject to ad-hoc polymorphism):

1 :: (Num t) => t -- notice the =>, indicating a type class is involved

2 References