Parametric Polymorphic Function

From GM-RKB
(Redirected from parametric polymorphism)
Jump to navigation Jump to search

A Parametric Polymorphic Function is a polymorphic function (or polymorphic data type) that is a parametric function (or parametric data type) - can handle values identically without depending on their type.



References

2014

  • (Wikipedia, 2014) ⇒ http://en.wikipedia.org/wiki/parametric_polymorphism Retrieved:2014-8-8.
    • In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. Using polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type. Such functions and data types are called generic functions and generic datatypes respectively and form the basis of generic programming.

      For example, a function append that joins two lists can be constructed so that it does not care about the type of elements: it can append lists of integers, lists of real numbers, lists of strings, and so on. Let the type variable a denote the type of elements in the lists. Then append can be typed [a] × [a] -> [a], where [a] denotes the type of lists with elements of type a. We say that the type of append is parameterized by a for all values of a. (Note that since there is only one type variable, the function cannot be applied to just any pair of lists: the pair, as well as the result list, must consist of the same type of elements.) For each place where append is applied, a value is decided for a.

      Following Christopher Strachey,parametric polymorphism may be contrasted with ad hoc polymorphism, in which a single polymorphic function can have a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. Thus, ad hoc polymorphism can generally only support a limited number of such distinct types, since a separate implementation has to be provided for each type.