For application, inference creates a fresh type variable and solves the equality constraint by a most general unifier:
The occurs check rejects equations such as
because they would require an infinite type.
Principal Type Property
Principal type property
If an expression is typable in HM under , then there exists a monotype such that:
;
for every with , there is a substitution with .
Thus is the most general type scheme for .
The principal type property is what makes annotation-free inference practical: the system does not have to guess among unrelated valid types.[^damas-milner]
Worked Example
Consider:
First infer the bound expression:
Since , generalise:
In the body, each occurrence is instantiated separately:
Application requires
Unification gives
Therefore:
The whole expression has principal type scheme:
Boundary
HM is deliberately small. In its classical form it excludes:
higher-rank polymorphism, where appears inside function arguments;
ad-hoc overloading, unless added by a separate mechanism such as type classes;
general subtyping;
unrestricted polymorphic recursion;
dependent types.
These features can be added to programming languages, but usually at the cost of a simpler principal-type theorem, complete inference, or both.