Algorithm Search⌘K

Theoremcore

Principal type theorem

Also called principal type property, Damas-Milner completeness theorem.

Every typable ML term has a principal type scheme of which all its other types are instances, and algorithm W computes it.

The principal type theorem says that a typable term of the Hindley-Milner type system has one most general type scheme, and that every other type of the term is an instance of it. Algorithm W computes this scheme and fails on untypable terms. So an ML compiler can find the types of a whole program without one annotation, and the type it finds never rules out a correct use. Hindley proved the first form in 1969, for combinatory logic. Damas and Milner proved the form with let-polymorphism in 1982. It is the reason that ML-style languages can make type annotations optional.

Statement

Use the syntax, types, schemes and rules of the Hindley-Milner page. A substitution SS maps type variables to types. For schemes, σ⊑σ′\sigma \sqsubseteq \sigma' means that σ′\sigma' is a generic instance of σ\sigma: each type that is an instance of σ′\sigma' is an instance of σ\sigma.

Definition. A principal type scheme of a term ee in a context Γ\Gamma is a scheme σp\sigma_p with Γ⊢e:σp\Gamma \vdash e : \sigma_p such that σp⊑σ\sigma_p \sqsubseteq \sigma for each σ\sigma with Γ⊢e:σ\Gamma \vdash e : \sigma.

Theorem (Damas and Milner 1982).

  1. Closed terms. If a closed term ee has a type, then it has a principal type scheme σp=∀α⃗. τp\sigma_p = \forall \vec\alpha.\, \tau_p. W computes τp\tau_p, and α⃗\vec\alpha lists all the variables of τp\tau_p. The types of ee are exactly the instances of τp\tau_p.
  2. Open terms. For each context Γ\Gamma and term ee: if RΓ⊢e:σR\Gamma \vdash e : \sigma for some substitution RR and scheme σ\sigma, then W(Γ,e)W(\Gamma, e) returns (S,τ)(S, \tau), and there is a substitution R′R' with R′SΓ=RΓR'S\Gamma = R\Gamma and R′(Gen(SΓ,τ))⊑σR'(\mathrm{Gen}(S\Gamma, \tau)) \sqsubseteq \sigma. If there are no such RR and σ\sigma, W fails.
  3. Soundness. If W(Γ,e)=(S,τ)W(\Gamma, e) = (S, \tau), then SΓ⊢e:τS\Gamma \vdash e : \tau, and so SΓ⊢e:Gen(SΓ,τ)S\Gamma \vdash e : \mathrm{Gen}(S\Gamma, \tau).

The principal scheme is unique up to renaming of its bound variables. The term "principal type property" names part 1.

In plain words

Many types fit a function like "take two functions and compose them". You could say that it works on numbers, or on text, or on lists of truth values. The theorem says that among all these fitting types there is always one best type. It uses placeholders where the program does not care what goes in. Every other fitting type comes out of the best one by filling in the placeholders.

So the compiler never has to guess. If it finds the best type, it has lost nothing, because any use that some correct type allows is also allowed by the best type. If no type fits, the compiler can say so for certain.

The theorem also says how to find the best type: the step-by-step method called algorithm W always finds it.

Proof

The theorem follows from two theorems about W. The proof file of algorithm W proves both in full, by induction on the term, for the syntax-directed rules T-Var', T-Abs, T-App and T-Let'. The syntax-directed rules derive the same types as the declarative rules with T-Gen and T-Inst. That equivalence is proved by moving each T-Inst up to a variable and each T-Gen down to a let (Damas 1985). Here is the structure of the proof.

Two lemmas on derivations.

  • Type substitution. If Γ⊢e:τ\Gamma \vdash e : \tau, then SΓ⊢e:SτS\Gamma \vdash e : S\tau for each substitution SS. At T-Let', rename the generalized variables of e1e_1 apart from SS first. This step is correct only because generalization skips the variables that are free in Γ\Gamma.
  • A more general context. If Γ,x:σ⊢e:τ\Gamma, x : \sigma \vdash e : \tau and σ′⊒σ\sigma' \sqsupseteq \sigma (each instance of σ\sigma is an instance of σ′\sigma'), then Γ,x:σ′⊢e:τ\Gamma, x : \sigma' \vdash e : \tau.

Soundness of W (part 3). If W(Γ,e)=(S,τ)W(\Gamma, e) = (S, \tau), then SΓ⊢e:τS\Gamma \vdash e : \tau. The proof is an induction on ee. At an application, the two premises are moved to the final context by type substitution, and the unifier of line L8 makes the function type match. At a let, the scheme that W stores is at least as general as the scheme that T-Let' builds in the final context, and the lemma on a more general context finishes the case.

Completeness of W (part 2). If RΓ⊢e:τ′R\Gamma \vdash e : \tau', then W succeeds with (S,τ)(S, \tau), and some R′R' has R′τ=τ′R'\tau = \tau' and R′S=RR'S = R on the variables that W did not create. The proof is an induction on ee.

  • At a variable, R′R' maps the fresh copies of the bound variables to the instance that the derivation chose.
  • At an application, the substitution built from the two induction hypotheses, extended by the result type, unifies the two types of line L8. So MGU succeeds, and by the most general unifier property this substitution factors through the unifier. This gives R′R'.
  • At a let, the key claim is R1(Gen(S1Γ,τ1))⊒Gen(RΓ,R1τ1)R_1(\mathrm{Gen}(S_1\Gamma, \tau_1)) \sqsupseteq \mathrm{Gen}(R\Gamma, R_1\tau_1): applying R1R_1 to the scheme that W computes gives a scheme at least as general as the one in the derivation. The proof checks the claim one variable at a time. A variable of τ1\tau_1 that W quantified can be sent to anything. A variable that W did not quantify is free in S1ΓS_1\Gamma, so its image under R1R_1 only has variables that are free in RΓR\Gamma, which the derivation could not quantify either.

From the two theorems to principal types. Take a typable closed term ee and let W(∅,e)=(S,τp)W(\emptyset, e) = (S, \tau_p). By soundness, ⊢e:τp\vdash e : \tau_p, and by type substitution each instance of τp\tau_p is a type of ee. By completeness with Γ=∅\Gamma = \emptyset, each type τ′\tau' of ee is R′τpR'\tau_p for some R′R'. So the types of ee are exactly the instances of τp\tau_p, and ∀ftv(τp). τp\forall \mathrm{ftv}(\tau_p).\, \tau_p is principal. If ee has no type, W cannot succeed, by soundness. For an open term, the same argument with the substitution RR gives part 2. Two principal schemes are generic instances of each other, so they are equal up to renaming. □\square

Consequences

  • No annotations are needed. An ML program can be checked with no type written in it. The compiler can still print the principal type of each definition, which is often the best documentation of the definition.
  • Definitions are typed once. Because the scheme of a let-bound definition is principal, a compiler can type each definition once and store the scheme. The uses do not change it. This allows the separate typing of the definitions of a module.
  • Signatures become matching. A written signature τs\tau_s for a definition is correct exactly when τs\tau_s is an instance of the principal type. This test is one-sided unification, called matching, and takes linear time.
  • Typability is decidable. A term is typable exactly when W succeeds. The cost is not small: the principal type can be exponentially large, and the problem is complete for EXPTIME.
  • Errors are certain. If W fails, no type exists. The place where W fails is not always the place of the mistake, but the failure itself is never a false alarm.

Tightness

  • First-class polymorphism. The theorem fails if function arguments may have polymorphic types. In System F, fun f -> (fun u -> f 1) (f true) has the type (∀α. α→α)→int(\forall \alpha.\, \alpha \to \alpha) \to \mathsf{int} and the type (∀α. α→bool)→bool(\forall \alpha.\, \alpha \to \mathsf{bool}) \to \mathsf{bool}. No type has both as substitution instances: to get them, a free type variable would have to become α\alpha under the binder ∀α\forall \alpha, and substitution never captures a bound variable. Type inference for System F is even undecidable. HM has no such term, because a fun-bound variable has one plain type.
  • Polymorphic recursion. If recursive functions get schemes in their own bodies, typability is equivalent to semi-unification and becomes undecidable. Then W can no longer find the type, even where one exists.
  • Principal typings. The theorem is about the best type for a given context. A stronger property asks for the best pair of a context and a type, for terms with free variables. Wells (2002) showed that the HM system does not have this property, while the simply typed calculus does.
  • Subtyping and overloading. With subtyping or overloading, a term may have several incomparable types. The theorem comes back if the types carry constraints, as in type classes and HM(X).
  • References. With mutable references, the rules must not generalize every let, or soundness fails. The value restriction keeps both soundness and principal types, since it only changes which variables T-Let' generalizes.
  • Size. Principal does not mean small. The principal type of the nested term yky_k of the let-polymorphism page has 8⋅2k−58 \cdot 2^k - 5 nodes for a term with 7k+47k + 4 nodes.

History

Curry and Feys (1958) set up type assignment for combinatory logic. Hindley (1969) proved that each typable object of combinatory logic has a principal type scheme, found by Robinson's unification of 1965. Milner, who did not know Hindley's paper, found the same idea for the ML language of the LCF prover, added polymorphic let, and proved W sound in 1978. Completeness of W, and so the principal type theorem for the system with let, was proved by Damas and Milner in 1982, with the details in Damas's thesis of 1985. The name "Hindley-Milner" comes from these two independent discoveries. Lee and Yi (1998) published complete proofs for W and for the top-down algorithm M.

References

  1. 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 definitions of generic instance and principal type scheme, Theorem 1 (soundness of W), Theorem 2 (completeness of W) and the corollary that each typable expression has a principal type scheme
  2. J. Roger Hindley, The principal type-scheme of an object in combinatory logic, Transactions of the American Mathematical Society 146, 1969, pages 29 to 60, the principal type-scheme theorem for combinatory logic, with the scheme found by Robinson unification
  3. Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, pages 348 to 375, Section 4 (algorithm W and its soundness theorem)
  4. Luis Damas, Type assignment in programming languages, PhD thesis, University of Edinburgh, 1985, the full proofs of soundness and completeness of W, and of the equivalence of the declarative and syntax-directed rules
  5. Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Section 22.5 (principal types for the simply typed calculus by constraint typing and unification) and Section 22.7 (let-polymorphism)
  6. Oukseh Lee and Kwangkeun Yi, Proofs about a folklore let-polymorphic type inference algorithm, ACM Transactions on Programming Languages and Systems 20(4), 1998, pages 707 to 723, complete proofs of soundness and completeness of W and of the top-down algorithm M

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/MV580F/text.yaml