Algorithm Search⌘K

Conceptcore

Let-polymorphism

Also called ML-style polymorphism, prenex polymorphism.

Polymorphism where only let-bound names get type schemes, and lambda-bound names stay monomorphic, which keeps inference decidable.

Let-polymorphism is the form of polymorphism in ML and its relatives. A name bound by let gets a type scheme such as ∀α. α→α\forall \alpha.\, \alpha \to \alpha, and each use of the name may pick a different instance of it. A name bound by fun gets one plain type, the same at all its uses. The quantifiers of a scheme sit only at the outside, so the style is also called prenex or rank-1 polymorphism. Milner (1978) chose this design because it gives reusable generic functions and still lets the compiler infer all types, with no annotations. The formal system is the Hindley-Milner type system.

In plain words

When you write a helper function and give it a name with let, the compiler looks at its definition first. If the definition works for any kind of value, the compiler remembers it as a template. Each later use of the name can then fill the template in its own way: once with numbers, once with truth values.

A parameter of a function is different. When the compiler checks the body of the function, it cannot see what the caller will pass. So it gives the parameter one fixed type, which all uses inside the body must share.

There is one more rule. A template may only leave open the parts that do not depend on the surrounding parameters. If a part comes from an outer parameter, that part is fixed for the whole body, and the template must keep it fixed too.

The idea

Why let and not fun. In let x = e1 in e2, the definition e1e_1 is right there. The type checker can find the most general type τ1\tau_1 of e1e_1 first and then decide which of its type variables are free to vary. In fun x -> e, the argument is unknown when the body is checked. To let each use of x have its own type, the type of the function would have to be something like (∀α. α→α)→int(\forall \alpha.\, \alpha \to \alpha) \to \mathsf{int}, with a quantifier inside an arrow. That is System F, where inference is undecidable. So the line between let and fun is the line between decidable and undecidable inference.

Typing by copying. A good model of the rule: let x = e1 in e2 has a type exactly when e1e_1 has a type and the term [x:=e1] e2[x := e_1]\,e_2, with a fresh copy of e1e_1 at each use of xx, has a simple type (Pierce, Section 22.7). Each copy can be typed on its own, so each use gets its own type. A type scheme is a compact summary of all the types that the copies could get. Algorithms such as algorithm W never make the copies. They type e1e_1 once and store the scheme.

Generalization and the context. After e1e_1 has the type τ1\tau_1, the scheme quantifies only the type variables of τ1\tau_1 that are not free in the context. A variable that is free in the context belongs to an enclosing fun parameter. Its value is fixed by the caller, and all uses must agree on it. Generalizing it anyway is unsound: it would type fun x -> let y = x in y 1 with any argument type, so (fun x -> let y = x in y 1) true would type check, and evaluation would get stuck at true 1.

Effects. If e1e_1 can allocate a mutable reference, the copying model is wrong: the copies would create different cells, but the program creates one cell. Polymorphism on that cell is unsound. ML therefore generalizes only when e1e_1 is a syntactic value. This is the value restriction.

Recursion. A recursive function is monomorphic inside its own body: the recursive calls must all use the type that is being inferred. Lifting this limit gives polymorphic recursion, where inference is undecidable. Haskell allows it only for functions with a written signature.

Cost. Copying can double the size of a type at each level of let. So inference for let-polymorphism is complete for exponential time, although programs in practice do not nest this way and inference is fast.

Examples

One function, two types. In

let id = fun x -> x in if id true then id 1 else 0

id gets ∀α. α→α\forall \alpha.\, \alpha \to \alpha, the first use is at bool→bool\mathsf{bool} \to \mathsf{bool}, and the second at int→int\mathsf{int} \to \mathsf{int}. The same body with fun id -> ... in place of the let has no type.

A context variable stays fixed. In fun x -> let y = x in y, the type of x is α\alpha and α\alpha is free in the context, so y gets α\alpha and not ∀α. α\forall \alpha.\, \alpha. The term has type α→α\alpha \to \alpha.

Partly generic. In fun x -> let k = fun y -> x in k, the definition has type β→α\beta \to \alpha, where α\alpha is the type of x. The scheme is ∀β. β→α\forall \beta.\, \beta \to \alpha: the argument type of k is free, and the result type is fixed.

Types that double. Let

let y0 = fun x -> x in
let y1 = fun z -> z y0 y0 in
let y2 = fun z -> z y1 y1 in
...
let yk = fun z -> z y(k-1) y(k-1) in yk

Each use of yi−1y_{i-1} is a fresh instance, so the type of yiy_i holds two renamed copies of the type of yi−1y_{i-1}. The type of y1y_1 is ((a→a)→(b→b)→c)→c((a \to a) \to (b \to b) \to c) \to c. The term has 7k+47k + 4 nodes, and the principal type has 8⋅2k−58 \cdot 2^k - 5 nodes as a tree and 2k+1−12^{k+1} - 1 type variables. So algorithm W and every other inference algorithm need exponential time on this family, just to write the answer.

Where it is used

  • Standard ML, OCaml and F# use let-polymorphism with the value restriction. OCaml uses a relaxed form, which also generalizes type variables that occur only in covariant positions.
  • Haskell uses it with type classes in the schemes. The monomorphism restriction stops generalization of some class-constrained bindings without arguments. The MonoLocalBinds option of GHC stops generalization of local let bindings that mention variables of the enclosing scope.
  • Elm and PureScript use it for top-level and local definitions. PureScript adds row polymorphism.
  • Compilers turn polymorphic functions into code by monomorphization, as in MLton, by one uniform boxed representation, as in OCaml, or by dictionary passing for type classes.

References

  1. Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, pages 348 to 375, Section 1 (the motivation for polymorphism at let, with the map function as the example) and Section 4 (algorithm W)
  2. Luis Damas and Robin Milner, Principal type-schemes for functional programs, Ninth ACM Symposium on Principles of Programming Languages (POPL 1982), pages 207 to 212, the rules GEN and LET, and the type schemes with the quantifiers at the outside
  3. Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Section 22.7 (let-polymorphism, typing let by substitution, the generalization step and its side condition, and the exponential cost of nested lets)
  4. Andrew K. Wright, Simple imperative polymorphism, LISP and Symbolic Computation 8(4), 1995, pages 343 to 355, the value restriction, which limits generalization at let to syntactic values

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/E10QMV/text.yaml