Algorithm W Created 2026-10-14 Updated 2026-11-05
Algorithm W infers data types for a pure lambda calculus with let polymorphism. Write , where is a type substitution and the inferred data type. Composition means applying first. The following presentation uses type schemes explicitly.[ref]
For a program identifier, take a fresh instance of its type scheme in the typing context. For , introduce fresh , infer , and return .
For an application , compute and . With fresh , let be the most general unifier of and . Return . An incompatible base data type or failed occurs check rejects the expression.
For let x = e1 in e2, first infer . Generalize exactly those type variables of not free in , producing a type scheme . Infer for under and return .
For example, let id = \x -> x in let a = id 3 in id True has a Boolean data type result. The identity function receives type scheme , then separate uses instantiate it at integer and Boolean data types. A parameter to a lambda abstraction instead has one shared monomorphic type; treating that parameter like a let-bound type scheme would accept programs outside this type system.
Robin Milner 2026-11-05
Robin Milner presented a polymorphic type discipline and its inference algorithm in 1978. Algorithm W derives types from program structure while using unification to satisfy type constraints; suitable let-bound expressions receive reusable type schemes. This work is a foundation of Hindley-Milner type inference.[ref]
For example, an identity function has type . Generalizing a let-bound identity permits separate uses at integer and Boolean types, whereas one lambda-bound parameter has one shared type within its scope. The discipline therefore gives precise rules for reuse rather than treating every occurrence as independently polymorphic. Its type-soundness claim concerns specified runtime type errors, not every possible program failure.