Learning path

Full curriculum

Full curriculum

Unit content

Parametric polymorphism

Parametric polymorphism lets one definition work uniformly for many types without inspecting which type was chosen.

A polymorphic identity function has type

$$\forall A.\ A\to A.$$

It can be instantiated at Int, Bool, a record type or any other admissible type:

$$\text{id}[\text{Int}] : \text{Int}\to\text{Int},$$ $$\text{id}[\text{Bool}] : \text{Bool}\to\text{Bool}.$$

A polymorphic list function might have type

$$\forall A.\ \text{List}(A)\to\text{Int}$$

for computing length. Because the implementation cannot assume operations specific to $A$, it can count elements independently of their contents.

This is different from ad hoc polymorphism, where the same surface operation may use different implementations for different types, such as integer addition and floating-point addition.

A formal type abstraction can bind a type variable much as a function abstraction binds a value variable, while type application chooses an instantiation. Practical languages may expose generic parameters explicitly or infer them.

Parametric polymorphism increases reuse while retaining static type information. More importantly, the requirement of uniform behavior constrains what polymorphic functions can do, enabling strong reasoning principles such as parametricity.