ArticleslgStudy

biology

Parametric polymorphism

Parametric polymorphism is a biology topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Parametric polymorphism rather than just read about it. In short: 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.

Key takeaways

  • Parametric polymorphism belongs to biology; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Parametric polymorphism to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Parametric polymorphism from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Parametric polymorphism

Start with the simplest possible case. Write down what Parametric polymorphism claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In biology, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Parametric polymorphism before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Parametric polymorphism ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Parametric polymorphism

In research
Parametric polymorphism appears in biology research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Parametric polymorphism in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Parametric polymorphism is common in secondary-school and first-year university syllabi. It links to neighbouring topics Generic programming, Polymorphism (computer science), Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Parametric polymorphism outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Parametric polymorphism” →

How to study Parametric polymorphism in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Parametric polymorphism means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Parametric polymorphism out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Parametric polymorphism in simple terms?

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 gen…

Why does Parametric polymorphism matter?

Because it connects several biology ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Parametric polymorphism?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Parametric polymorphism.

Tags

  • Generic programming
  • Polymorphism (computer science)
  • Type theory

Keep exploring