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]
A Theory of Type Polymorphism in Programming by Robin Milner. sections 4.1–4.2, Algorithm W and its soundness
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. Boolean data type 2026-10-24
A Boolean data type has two truth values. A binary probabilistic query returns such a value; rejection or failure must be represented separately if false outcomes are valid observations of the query.