In languages like Haskell, OCaml, Rust and TypeScript, you can write a function without ever saying what type it takes or returns — and the compiler will still tell you, with total confidence, that you applied it to the wrong thing. How can it know a type you never wrote down?
The answer is type inference: the compiler treats every unknown type as a placeholder, watches how you use each value, and from that usage deduces the only types that could make the program consistent. Pass a value to + and it must be a number; return it from an if, and both branches must agree.
The classic engine for this is Hindley-Milner (Roger Hindley, 1969; rediscovered by Robin Milner in 1978). It is famous for a near-magical property: with no annotations at all, it finds the most general type of any expression — or proves no type exists. The trick has a name, and it is wonderfully simple: unification.
Comments
Loading comments...