Jump to content

Parametric polymorphism: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
mNo edit summary
Line 30: Line 30:


== Bounded parametric polymorphism ==
== Bounded parametric polymorphism ==
[[Luca Cardelli]] and Wegner recognized in 1985 the advantages of allowing ''bounds'' on the type parameters.<ref name="luca">Cardielli, Luca. ''On Understanding Types, Data Abstraction, and Polymorphism'' ACM Computing Surveys.</ref> Many operations require some knowledge of the data types but can otherwise work parametrically. For example, to check whether an item is included in a list, we need to compare the items for equality. In [[Standard ML]], type parameters of the form ''’’a'' are restricted so that the equality operation is available, thus the function would have the type ''’’a'' × ''’’a'' list → bool and ''’’a'' can only be a type with defined equality. In [[Haskell (programming language)|Haskell]], bounding is achieved by requiring types to belong to a [[type class]]; thus the same function has the type <math>{\scriptstyle Eq \, \alpha \, \Rightarrow \alpha \, \rightarrow \left[\alpha \right] \rightarrow Bool}</math> in Haskell. In most object-oriented programming languages that support parametric polymorphism, parameters can be constrained to be [[subtype]]s of a given type (see [[Subtyping polymorphism]] and the article on [[Generic programming]]).
[[Luca Cardelli]] and Wegner recognized in 1985 the advantages of allowing ''bounds'' on the type parameters.<ref name="luca">Cardielli, Luca. ''On Understanding Types, Data Abstraction, and Polymorphism'' ACM Computing Surveys.</ref> Many operations require some knowledge of the data types but can otherwise work parametrically. For example, to check whether an item is included in a list, we need to compare the items for equality. In [[Standard ML]], type parameters of the form ''’’a'' are restricted so that the equality operation is available, thus the function would have the type ''’’a'' × ''’’a'' list → bool and ''’’a'' can only be a type with defined equality. In [[Haskell (programming language)|Haskell]], bounding is achieved by requiring types to belong to a [[type class]]; thus the same function has the type <math>{\scriptstyle Eq \, \alpha \, \Rightarrow \alpha \, \rightarrow \left[\alpha \right] \rightarrow Bool}</math> in Haskell. In most object-oriented programming languages that support parametric polymorphism, parameters can be constrained to be [[subtype]]s of a given type (see [[Subtype polymorphism]] and the article on [[Generic programming]]).


== References ==
== References ==

Revision as of 13:01, 30 March 2011

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 parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type.[1] 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, also called function overloading, 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.

History

Parametric polymorphism was first introduced to programming languages in ML in 1976. Today it exists in Standard ML, OCaml, Ada, Haskell, Visual Prolog and others. Java, C#, Visual Basic .NET and Delphi (CodeGear) have each recently introduced "generics" for parametric polymorphism. Some implementations of type polymorphism are superficially similar to parametric polymorphism while also introducing ad-hoc aspects. One example is C++ template specialization.

The most general form of polymorphism is "higher-rank impredicative polymorphism". Two popular restrictions of this form are restricted rank polymorphism (for example, rank-1 or prenex polymorphism) and predicative polymorphism. Together, these restrictions give "predicative prenex polymorphism", which is essentially the form of polymorphism found in ML and early versions of Haskell.

Higher-ranked polymorphism

Rank-1 (prenex) polymorphism
In a prenex polymorphic system, type variables may not be instantiated with polymorphic types. This is very similar to what is called "ML-style" or "Let-polymorphism" (technically ML's Let-polymorphism has a few other syntactic restrictions).
This restriction makes the distinction between polymorphic and non-polymorphic types very important; thus in predicative systems polymorphic types are sometimes referred to as type schemas to distinguish them from ordinary (monomorphic) types, which are sometimes called monotypes. A consequence is that all types can be written in a form which places all quantifiers at the outermost (prenex) position.
For example, consider the append function described above, which has type [a] × [a] → [a]; in order to apply this function to a pair of lists, a type must be substituted for the variable a in the type of the function such that the type of the arguments matches up with the resulting function type. In an impredicative system, the type being substituted may be any type whatsoever, including a type that is itself polymorphic; thus append can be applied to pairs of lists with elements of any type—even to lists of polymorphic functions such as append itself.
Polymorphism in the language ML and its close relatives is predicative. This is because predicativity, together with other restrictions, makes the type system simple enough that type inference is possible. In languages where explicit type annotations are necessary when applying a polymorphic function, the predicativity restriction is less important; thus these languages are generally impredicative. Haskell manages to achieve type inference without predicativity but with a few complications.
Rank-k polymorphism
For some fixed value k, rank-k polymorphism is a system in which a quantifier may not appear to the left of more than k arrows (when the type is drawn as a tree)[1].
Type inference for rank-2 polymorphism is decidable, but reconstruction for rank-3 and above is not.[citation needed]
Rank-n ("higher-rank") polymorphism
Rank-n polymorphism is polymorphism in which quantifiers may appear to the left of arbitrarily many arrows.

Predicativity and impredicativity

Predicative polymorphism
In a predicative parametric polymorphic system, a type containing a type variable may not be used in such a way that is instantiated to a polymorphic type.
Impredicative polymorphism ("first class" polymorphism)
Also called first-class polymorphism. Impredicative polymorphism allows the instantiation of a variable in a type with any type, including polymorphic types, such as itself.
In type theory, the most frequently studied impredicative typed λ-calculi are based on those of the lambda cube, especially System F. Predicative type theories include Martin-Löf Type Theory and NuPRL.

Bounded parametric polymorphism

Luca Cardelli and Wegner recognized in 1985 the advantages of allowing bounds on the type parameters.[2] Many operations require some knowledge of the data types but can otherwise work parametrically. For example, to check whether an item is included in a list, we need to compare the items for equality. In Standard ML, type parameters of the form ’’a are restricted so that the equality operation is available, thus the function would have the type ’’a × ’’a list → bool and ’’a can only be a type with defined equality. In Haskell, bounding is achieved by requiring types to belong to a type class; thus the same function has the type in Haskell. In most object-oriented programming languages that support parametric polymorphism, parameters can be constrained to be subtypes of a given type (see Subtype polymorphism and the article on Generic programming).

References

  1. ^ a b Pierce, B. C. 2002 Types and Programming Languages. MIT Press.
  2. ^ Cardielli, Luca. On Understanding Types, Data Abstraction, and Polymorphism ACM Computing Surveys.