Difference between revisions of "Parametric polymorphism"
Karl Jones (Talk | contribs) (Created page with "In programming languages and type theory, '''parametric polymorphism''' is a way to make a language more expressive, while still maintaining full...") |
(No difference)
|
Latest revision as of 11:03, 20 September 2016
In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type safety.
Description
Using parametric 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 forall a. [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.
See also
External links
- Parametric polymorphism @ Wikipedia