Apex › Systems & OS

How types are inferred with none written (Hindley-Milner)

How ML, Haskell, Rust, and TypeScript know every type without annotations. Give each unknown a fresh type variable; each application f x forces f to be (type of x)→result, a constraint; solve them by unification, which finds the most general (principal) type. An occurs check forbids infinite types (so λx. x x doesn’t type), and let-bound values are generalized to be used at several types. Verified in node: Algorithm W infers the known principal types of five classic λ-terms exactly (λx.x : a→a, compose : (a→b)→(c→a)→c→b, …) and rejects self-application. Pick a term and read its inferred type.

More in Systems & OS