Hindley–Milner Inference by Constraint Solving

Infer a function’s most general type by introducing unknowns and unifying the constraints generated by each application.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Assign a fresh variable to an unknown type
  2. Solve constraints in the environment they came from

Assign a fresh variable to an unknown type

When inferring \(\lambda f.\lambda x.f\,x\), assign fresh variables \(F,X,R\) for the type of \(f\), the type of \(x\), and the result. Application generates the equation \(F=X\to R\). Solving it yields

\[ (X\to R)\to X\to R . \]

Every concrete instance chooses types for \(X\) and \(R\). The algorithm need not choose one particular instance during inference.

Solve constraints in the environment they came from

An expression can constrain a type variable that also occurs in its environment. Generalizing such a shared variable would incorrectly promise independent choices later. Let-polymorphism generalizes only eligible variables outside the environment. Unification solves equalities between type constructors and rejects infinite types in this calculus. The principal type property means other valid types are instances of the inferred scheme for the stated language fragment.[1][2]

References

  1. Luís Damas and Robin Milner, Principal Type-Schemes for Functional Programs ↩
  2. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩