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 polymorphic functions and data types are sometimes called generic functions and generic datatypes, respectively, and they form the basis of generic programming. Parametric polymorphism may be contrasted with ad hoc polymorphism. Parametrically polymorphic definitions are uniform: they behave identically regardless of the type they are instantiated at. In contrast, ad hoc polymorphic definitions are given a distinct definition for each type. 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. The usual theoretical device for studying parametric polymorphism is system F, which extends simply typed lambda calculus with quantification over types.
Basic definition It is possible to write functions that do not depend on the types of their arguments. For example, the identity function 𝗂𝖽(x)=x simply returns its argument unmodified. This naturally gives rise to a family of potential types, such as 𝖨𝗇𝗍→𝖨𝗇𝗍, 𝖡𝗈𝗈𝗅→𝖡𝗈𝗈𝗅, 𝖲𝗍𝗋𝗂𝗇𝗀→𝖲𝗍𝗋𝗂𝗇𝗀, and so on. Parametric polymorphism allows 𝗂𝖽 to be given a single, most general type by introducing a universally quantified type variable:
𝗂𝖽:∀α.α→α The polymorphic definition can then be instantiated by substituting any concrete type for α, yielding the full family of potential types. The identity function is a particularly extreme example, but many other functions also benefit from parametric polymorphism. For example, an 𝖺𝗉𝗉𝖾𝗇𝖽 function that concatenates two lists does not inspect the elements of the list, only the list structure itself. Therefore, 𝖺𝗉𝗉𝖾𝗇𝖽 can be given a similar family of types, such as [𝖨𝗇𝗍]×[𝖨𝗇𝗍]→[𝖨𝗇𝗍], [𝖡𝗈𝗈𝗅]×[𝖡𝗈𝗈𝗅]→[𝖡𝗈𝗈𝗅], and so on, where [T] denotes a list of elements of type T. The most general type is therefore
𝖺𝗉𝗉𝖾𝗇𝖽:∀α.[α]×[α]→[α] which can be instantiated to any type in the family. Parametrically polymorphic functions like 𝗂𝖽 and 𝖺𝗉𝗉𝖾𝗇𝖽 are said to be parameterized over an arbitrary type α. Both 𝗂𝖽 and 𝖺𝗉𝗉𝖾𝗇𝖽 are parameterized over a single type, but functions may be parameterized over arbitrarily many types. For example, the 𝖿𝗌𝗍 and 𝗌𝗇𝖽 functions that return the first and second elements of a pair, respectively, can be given the following types:
𝖿𝗌𝗍:∀α.∀β.α×β→α𝗌𝗇𝖽:∀α.∀β.α×β→β In the expression 𝖿𝗌𝗍((3,𝗍𝗋𝗎𝖾)), α is instantiated to 𝖨𝗇𝗍 and β is instantiated to 𝖡𝗈𝗈𝗅 in the call to 𝖿𝗌𝗍, so the type of the overall expression is 𝖨𝗇𝗍. The syntax used to introduce parametric polymorphism varies significantly between programming languages. For example, in some programming languages, such as Haskell, the ∀α quantifier is implicit and may be omitted. Other languages require types to be instantiated explicitly at a parametrically polymorphic function's call sites, either at all sites or alternatively at least sufficient to allow type inference to determine the rest.
History Parametric polymorphism was first introduced to programming languages in ML in 1975. Today it exists in Standard ML, OCaml, F#, Ada, Haskell, Mercury, Visual Prolog, Scala, Julia, Python, TypeScript, C++ and others. Java, C#, Visual Basic .NET and Delphi have each 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. In the 1980s, Leivant introduced a stratified (i.e. predicative) version of Girard and Reynold's system F. Leivant's approach is based on a notion of rank of the quantifiers, which measures their nesting depth inside function constructors. The ML approach is limited to rank-1 polymorphism in this perspective. Haskell has adopted higher-rank parametric polymorphism in the 1990s. For example, rank-2 parametric polymorphism is used in Haskell to define the runST monad, which effectively simulates a type and effect system, with "isolated regions of imperative programming". At type level, state isolation essentially stems from the deeper, rank-2 quantification over state in runST. (This is not enough to formally describe the runtime semantics of runST. For the latter, one needs some additional ingredients like separation logic.)
Predicativity, impredicativity, and higher-rank polymorphism A type is said to be of rank k (for some fixed integer k) if no path from its root to a ∀ quantifier passes to the left of k or more arrows, when the type is drawn as a tree. A type system is said to support rank-k polymorphism if it admits types with rank less than or equal to k. For example, a type system that supports rank-2 polymorphism would allow (∀α.α→α)→T but not ((∀α.α→α)→T)→T. A type system that admits types of arbitrary rank is said to be "rank-n polymorphic". (This notion of rank is a different from how quantifier rank is defined in classical logic because here it measures nesting depth relative to a non-quantifier connective, whereas in classical logic non-quantifier connectives do not increase the rank of quantifiers nested under them, but other quantifiers do.)
Rank-1 (predicative) polymorphism
In a predicative type system (also known as a prenex polymorphic system), type variables may not be instantiated with polymorphic types. Predicative type theories include Martin-Löf type theory and Nuprl. 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 of predicativity is that all types can be written in a form that places all quantifiers at the outermost (prenex) position. For example, consider the 𝖺𝗉𝗉𝖾𝗇𝖽 function described above, which has the following type:
… excerpt ends here. Continue reading the full article.