Lably
Skip to content

Parametric polymorphism

Basis of generic programming

  • 6 min read
  • 4 chapters

Related on Lably

Through Type theory, Function (computer programming) and Data type

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 id(x)=x simply returns its argument unmodified. This naturally gives rise to a family of potential types, such as Int→Int, Bool→Bool, String→String, and so on. Parametric polymorphism allows id to be given a single, most general type by introducing a universally quantified type variable:

id:∀α.α→α

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 append function that concatenates two lists does not inspect the elements of the list, only the list structure itself. Therefore, append can be given a similar family of types, such as [Int]×[Int]→[Int], [Bool]×[Bool]→[Bool], and so on, where [T] denotes a list of elements of type T. The most general type is therefore

append:∀α.[α]×[α]→[α]

which can be instantiated to any type in the family.

Parametrically polymorphic functions like id and append are said to be parameterized over an arbitrary type α. Both id and append are parameterized over a single type, but functions may be parameterized over arbitrarily many types. For example, the fst and snd functions that return the first and second elements of a pair, respectively, can be given the following types:

fst:∀α.∀β.α×β→αsnd:∀α.∀β.α×β→β

In the expression fst((3,true)), α is instantiated to Int and β is instantiated to Bool in the call to fst, so the type of the overall expression is Int.

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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication