Session 11
Polymorphism
parametric polymorphism, the Hindley–Milner system and type inference by algorithm W
Parametric polymorphism gives one expression several types: the identity function is typed once and used at every type. The session extends the type system of session 10 with type variables and quantified types, and shows how OCaml infers the most general type of a program that carries no type annotation.
Block 1, Polymorphism and type inference. We will start from the mini-ML of session 10, where a function such as the identity must be given a single type. System F removes that limit with types quantified over type variables, at a price: for programs without annotations, type inference is no longer decidable. The Hindley–Milner system, used in OCaml, SML and Haskell, restricts the quantification so that inference becomes decidable again, and algorithm W computes the most general type of an expression by unification. The lecture ends with references, where generalization has to be restricted to keep the system safe.