Skip to content

Let-Polymorphism

Type-scheme generalization at let bindings with fresh instantiation at each use in Hindley–Milner systems.

Version
v2 · 2026-11-14 · History
Domain-specific #
10375
Domain group
Applied Sciences & Engineering
Origin domain
Computer Science & Software Engineering
Subdomains
Type Theory, Programming Languages → Computer Science & Software Engineering
Aliases
Let Polymorphism, ML Style Let Polymorphism

Core Idea

Let polymorphism in Hindley–Milner typing generalizes eligible type variables at a let binding into a type scheme and freshly instantiates that scheme at each use, enabling one definition to serve multiple types.

let id = λx.x allows id 3 and id true through separate instantiations. A mutable reference is withheld from unrestricted generalization under a value restriction.

How would you explain it like I'm…

The Fits-Anything Helper

Imagine a magic box that hands back whatever you put in. You give it a name once, and then you can use it with a ball one time and a book another time. Each time you use it, it gets ready for that kind of thing all over again.

One Name, Many Types

In programming, every value has a type, like "number" or "true/false." Let-polymorphism is a rule some languages use when you give a piece of code a name with "let." If the code works the same for any type, the language remembers it as a general pattern. Each time you use it, the language makes a fresh copy of that pattern for the type you need. So a function that just hands back its input can be used once with 3 and again with true. Some tricky things, like boxes whose contents can change, are not allowed to be made general this way.

Generalize at Let, Instantiate at Use

Let-polymorphism is part of the Hindley–Milner type system used by languages like ML. When you write let id = λx.x, the type checker infers id has type a → a, where a is a type variable. At the let binding, it generalizes that into a type scheme meaning "for all a, a → a." Each time id is used, the scheme is instantiated with fresh variables. So id 3 gets int → int and id true gets bool → bool, and both type-check. Generalization happens only at let bindings and only for eligible variables. Mutable references are kept from unrestricted generalization by the value restriction, which prevents unsound programs.

 

Let-Polymorphism is the mechanism in Hindley–Milner typing that gives let-bound definitions polymorphic types. When checking let x = e1 in e2, the inferencer computes a type for e1 and generalizes the type variables that are not free in the surrounding environment, producing a type scheme such as ∀a. a → a. At each occurrence of x in e2, the scheme is instantiated with fresh type variables, which can then unify independently with different concrete types. Thus let id = λx.x in ... permits both id 3 and id true, since each use gets a separate instantiation. By contrast, a λ-bound variable stays monomorphic inside its body, which keeps inference decidable. In languages with mutable state, generalizing a reference type would allow writing a value of one type and reading it as another, so the value restriction withholds unrestricted generalization from such expressions.

Scope of Application

  • Functional languages. Types reusable local definitions.
  • Type inference. Computes principal schemes.
  • Language design. Adds effect restrictions.
  • Programming-language theory. Studies parametricity and soundness.

Clarity

Include HM-style generalization at let and fresh scheme instantiation at occurrences under declared restrictions. Exclude ad hoc overloading, subtype polymorphism, explicit higher-rank quantifiers, and lambda-bound parameters generalized as if let-bound. Inclusion test: Include HM-style generalization at let and fresh scheme instantiation at occurrences under declared restrictions. Exclusion test: Exclude ad hoc overloading, subtype polymorphism, explicit higher-rank quantifiers, and lambda-bound parameters generalized as if let-bound. Nearest boundary: Parametric polymorphism is broader; let polymorphism names this binding-specific introduction and use discipline. Exit condition: The mechanism exits when a binding remains monomorphic or uses share one unrefreshed type variable. Common misclassifications: It is not ad hoc overloading. It is not subtype polymorphism. It is not arbitrary higher-rank polymorphism. It does not generalize every expression safely. Nearest named distinctions: Parametric polymorphism: The broader uniform-type idea. Monomorphism: Uses one fixed type. Overloading: Selects type-specific behavior. Higher-rank polymorphism: Allows nested explicit quantification.

Manages Complexity

Generalization enables reuse while effects can couple supposedly independent instances. Inference finds a most general scheme within HM limits.

Abstract Reasoning

  1. Bound expression — Supplies the inferred monotype. No binding means no generalization point.
  2. Environment — Marks type variables already fixed by context. Generalizing them is unsound.
  3. Generalization — Quantifies eligible free variables. Keeping a monotype loses polymorphism.
  4. Type scheme — Stores the quantified type. Universal variables must remain scoped.
  5. Instantiation — Replaces quantified variables freshly per use. Sharing one instance couples unrelated calls.
  6. Value/effect restriction — Limits generalization in languages with effects. Mutable references make unrestricted rules unsound.

Knowledge Transfer

Generalize-at-let and instantiate-at-use transfers across HM-family languages under their value, effect, and recursion restrictions; unrestricted generalization is unsound with mutable state.

Relationships to Other Abstractions

Local relationship map for Let-PolymorphismParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Let-PolymorphismDOMAINDomain-specific abstraction: Type System — presupposesType SystemDOMAIN

Current abstraction Let-Polymorphism Domain-specific

Parents (1) — outgoing typed relations

  • Let-Polymorphism presupposes Type System Domain-specific

    Let-Polymorphism presupposes Type System because generalization and fresh instantiation operate inside a Hindley–Milner type system.

Hierarchy paths (2) — routes to 2 parentless roots

Neighborhood in Abstraction Space

Let-Polymorphism sits in a crowded region of the domain-specific corpus (38th percentile for distinctiveness): several abstractions share nearly its structure, so a description that fits it tends to fit its neighbors too.

Family — Matrices, Measures & Numeric Structures (30 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14