HOME

TheInfoList



OR:

In
programming language theory Programming language theory (PLT) is a branch of computer science that deals with the design, implementation, analysis, characterization, and classification of formal languages known as programming languages. Programming language theory is clos ...
, parametricity is an abstract uniformity property enjoyed by parametrically polymorphic functions, which captures the intuition that all instances of a polymorphic function act the same way.


Idea

Consider this example, based on a set ''X'' and the type ''T''(''X'') = 'X'' → ''X''of functions from ''X'' to itself. The higher-order function ''twice''''X'' : ''T''(''X'') → ''T''(''X'') given by ''twice''''X''(''f'') = ''f'' ∘ ''f'', is intuitively independent of the set ''X''. The family of all such functions ''twice''''X'', parametrized by sets ''X'', is called a " parametrically polymorphic function". We simply write twice for the entire family of these functions and write its type as \forall''X''. ''T''(''X'') → ''T''(''X''). The individual functions ''twice''''X'' are called the ''components'' or ''instances'' of the polymorphic function. Notice that all the component functions ''twice''''X'' act "the same way" because they are given by the same rule. Other families of functions obtained by picking one arbitrary function from each ''T''(''X'') → ''T''(''X'') would not have such uniformity. They are called "''ad hoc'' polymorphic functions". ''Parametricity'' is the abstract property enjoyed by the uniformly acting families such as twice, which distinguishes them from ''ad hoc'' families. With an adequate formalization of parametricity, it is possible to prove that the parametrically polymorphic functions of type \forall''X''. ''T''(''X'') → ''T''(''X'') are one-to-one with natural numbers. The function corresponding to the natural number ''n'' is given by the rule ''f'' \mapsto ''f''''n'', i.e., the polymorphic
Church numeral In mathematics, Church encoding is a means of representing data and operators in the lambda calculus. The Church numerals are a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded ...
for ''n''. In contrast, the collection of all ''ad hoc'' families would be too large to be a set.


History

The ''parametricity theorem'' was originally stated by
John C. Reynolds John Charles Reynolds (June 1, 1935 – April 28, 2013) was an American computer scientist. Education and affiliations John Reynolds studied at Purdue University and then earned a Doctor of Philosophy (Ph.D.) in theoretical physics from Harvard U ...
, who called it the ''abstraction theorem''. In his paper "Theorems for free!",
Philip Wadler Philip Lee Wadler (born April 8, 1956) is an American computer scientist known for his contributions to programming language design and type theory. He is the chair of Theoretical Computer Science at the Laboratory for Foundations of Computer ...
described an application of parametricity to derive theorems about parametrically polymorphic functions based on their types.


Programming language implementation

Parametricity is the basis for many
program transformation A program transformation is any operation that takes a computer program and generates another program. In many cases the transformed program is required to be semantically equivalent to the original, relative to a particular formal semantics and ...
s implemented in compilers for the
Haskell programming language Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming la ...
. These transformations were traditionally thought to be correct in Haskell because of Haskell's non-strict semantics. Despite being a lazy programming language, Haskell does support certain primitive operations—such as the operator seq—that enable so-called "selective strictness", allowing the programmer to force the evaluation of certain expressions. In their paper "Free theorems in the presence of ''seq''", Patricia Johann and Janis Voigtlaender showed that because of the presence of these operations, the general parametricity theorem does not hold for Haskell programs; thus, these transformations are unsound in general.


Dependent types


See also

*
Parametric polymorphism In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
*
Non-strict programming language A strict programming language is a programming language which employs a strict programming paradigm, allowing only strict functions (functions whose parameters must be evaluated completely before they may be called) to be defined by the user. A no ...


References

{{reflist, 30em


External links


Wadler: Parametricity
Programming language topics Type theory Polymorphism (computer science)