In programming language theory, 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.
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 twiceX : T(X) → T(X) given by twiceX(f) = f ∘ f, is intuitively independent of the set X. The family of all such functions twiceX, 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
\forall
\mapsto
The parametricity theorem was originally stated by John C. Reynolds, who called it the abstraction theorem.[1] In his paper "Theorems for free!",[2] Philip Wadler described an application of parametricity to derive theorems about parametrically polymorphic functions based on their types.
Parametricity is the basis for many program transformations implemented in compilers for the Haskell programming language. 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",[3] 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.