Definitioncore
Hindley-Milner type system
Also called Damas-Milner type system, HM type system.
The type system of ML, with type schemes that quantify type variables at let bindings, so that each typable term has a principal type.
The Hindley-Milner (HM) type system is the type system of the core of ML, OCaml, F# and Haskell 98. It extends the Curry-style simply typed lambda calculus by one feature: a name bound by let can get a type scheme such as , and each use of the name can choose its own instance of the scheme. A name bound by fun keeps one type. This is let-polymorphism. The system is small enough to have a principal type for each typable term, and algorithm W finds it with no annotations in the program (Damas and Milner 1982). It sits between the simply typed calculus, which has no polymorphism, and System F, where full type inference is undecidable.
Definition
Terms. Let range over term variables and over constants. The surface syntax is
The constants are the integers, true and false. The pages on algorithms also use if, the operators + - * < == and a recursive function form fix f x -> e. These need one rule each and change nothing below.
Types and schemes. Let range over an infinite set of type variables.
A type has no quantifier. A type scheme has all its quantifiers at the outside, and are its bound variables. So the quantifier never appears inside an arrow, and a function never takes a polymorphic argument. A context maps finitely many term variables to schemes. We write for the free type variables of a scheme, and for the union over the schemes in .
Instances. A type is an instance of , written , if for some types . Only bound variables are replaced. For example , but , because is free. A scheme is a generic instance of , written , if each instance of is an instance of .
Generalization. , where lists the variables of . It quantifies exactly the variables that the context does not mention.
Rules. The judgment is the smallest relation closed under these rules. They are the rules of Damas and Milner (1982), with the names of Pierce's book. T-Gen and T-Inst can end a derivation of any term, so this form is not syntax directed.
x : sigma in Gamma Gamma, x : tau1 |- e : tau2 Gamma |- e1 : tau1 -> tau2 Gamma |- e2 : tau1
------------------ T-Var ---------------------------------- T-Abs ---------------------------------------------- T-App
Gamma |- x : sigma Gamma |- fun x -> e : tau1 -> tau2 Gamma |- e1 e2 : tau2
Gamma |- e1 : sigma Gamma, x : sigma |- e2 : tau
--------------------------------------------------- T-Let
Gamma |- let x = e1 in e2 : tau
Gamma |- e : sigma alpha not in ftv(Gamma) Gamma |- e : sigma sigma below sigma'
--------------------------------------------- T-Gen ---------------------------------------- T-Inst
Gamma |- e : forall alpha. sigma Gamma |- e : sigma'
In T-Inst, "sigma below sigma'" is . T-Abs puts a type, not a scheme, into the context. This one restriction is what keeps inference decidable.
The syntax-directed form. Move each T-Inst to the variables and each T-Gen to the let that needs it. This gives an equivalent system with one rule per form of term:
x : sigma in Gamma sigma below tau Gamma |- e1 : tau1 Gamma, x : Gen(Gamma, tau1) |- e2 : tau2
------------------------------------- T-Var' ---------------------------------------------------------- T-Let'
Gamma |- x : tau Gamma |- let x = e1 in e2 : tau2
with T-Abs and T-App as before. A judgment holds in the first system exactly when it holds in the second, and holds in the first exactly when holds in the second for a with . The proof is an induction on derivations: a T-Gen step can always move down to the next let or to the end, and a T-Inst step can always move up to a variable. Algorithms work on the syntax-directed form.
Recursion. The rule for fix f x -> e types the body with and , both as plain types. So a recursive function is monomorphic inside its own body. Mycroft's extension, which gives a scheme there, makes inference undecidable.
Soundness. With the call-by-value semantics of the simply typed lambda calculus and the step , a closed well-typed term never gets stuck. The proof is progress and preservation (Wright and Felleisen 1994). Preservation needs two substitution lemmas. The term substitution lemma: if and , then . The type substitution lemma: if , then for each substitution of types for type variables. The side condition of T-Gen is what makes the type substitution lemma true. Milner (1978) proved the first form of the theorem, for a denotational semantics.
Examples and non-examples
Two uses at two types. In let id = fun x -> x in if id true then id 1 else 0, the name id gets the scheme . The first use takes , the second , and the term has type . The syntax-directed derivation, with , is:
x : a |- x : a Gamma |- id : bool -> bool Gamma |- true : bool Gamma |- id 1 : int Gamma |- 0 : int
------------------------------ T-Abs ------------------------------------------------ T-App
|- fun x -> x : a -> a Gamma |- id true : bool
----------------------------------------------------------------------------------- T-If
Gamma |- if id true then id 1 else 0 : int
-------------------------------------------------------------------------------------------------------------------------- T-Let'
|- let id = fun x -> x in if id true then id 1 else 0 : int
The empty context has no free variables, so .
The same body with a fun. The term fun f -> if f true then f 1 else 0 has no type. The variable f gets one type from T-Abs. The two uses need and , and . System F types this term with , a rank-2 type that HM cannot write.
A let that does not generalize. In fun x -> let y = x in y, the variable x has type in the context when let is typed. So with no quantifier, and the term has type . The term fun x -> let y = x in if y true then y 1 else 0 has no type for the same reason. If were quantified, y would get , the term would get the type , and the application of it to 5 would get stuck at 5 true. This is why T-Gen asks that is not free in .
Self-application. fun x -> x x has no type. T-App would need , which fails the occurs check. With recursive types the equation has a solution.
Not an HM type. is a type of System F, but not an HM type or scheme, because a quantifier sits to the left of an arrow.
Principal types. fun f -> fun g -> fun x -> f (g x) has the principal type . Each other type of the term, such as , is an instance of it. The principal type theorem says that such a type exists for each typable term.
Related definitions
- Simply typed lambda calculus. HM without schemes. A term
let x = e1 in e2has an HM type exactly when has a type and the copy has an HM type. So repeated substitution turns HM typing into simple typing, at the cost of an exponential growth of the term. - System F. HM is the fragment of System F in which quantifiers appear only at the outside of the types of
let-bound names. Inference for HM is decidable, and for System F it is not. - Algorithm W and algorithm J. The two classic algorithms that compute principal types. Constraint-based inference splits the same work into constraint generation and solving.
- Principal type theorem. Every typable term has a principal type scheme, and W computes it.
- DEXPTIME-completeness of ML typability. Deciding whether a term has an HM type is complete for EXPTIME, because nested lets make types exponentially large.
- Value restriction. With mutable references, T-Let' is unsound. ML generalizes only when is a syntactic value.
- Polymorphic recursion. HM with schemes for recursive functions inside their own bodies. Typability for it is equivalent to semi-unification, so it is undecidable.
- Extensions. Type classes add constraints such as
Eq ato schemes. Row polymorphism adds row variables for records. Subtyping is hard to combine with principal types.
References
- Luis Damas and Robin Milner, Principal type-schemes for functional programs, Ninth ACM Symposium on Principles of Programming Languages (POPL 1982), pages 207 to 212, the type schemes, the generic instance relation, the six inference rules (TAUT, INST, GEN, COMB, ABS, LET), and the statement of soundness and completeness of algorithm W
- Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, pages 348 to 375, Section 3 (the semantic soundness theorem, "well-typed expressions do not go wrong") and Section 4 (algorithm W, with polymorphism at let)
- J. Roger Hindley, The principal type-scheme of an object in combinatory logic, Transactions of the American Mathematical Society 146, 1969, pages 29 to 60, the principal type-scheme theorem for combinatory logic, found by unification
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 22 (Section 22.7, let-polymorphism, with the rule that types let by substitution and the discussion of the cost of nested lets)
- Benjamin C. Pierce (ed.), Advanced Topics in Types and Programming Languages, MIT Press, 2005, Chapter 10 by Francois Pottier and Didier Remy, The essence of ML type inference (Section 10.1 the ML calculus and the Damas-Milner rules, Section 10.2 constraints)
- Andrew K. Wright and Matthias Felleisen, A syntactic approach to type soundness, Information and Computation 115(1), 1994, pages 38 to 94, the proof of soundness for a functional core of ML with let-polymorphism by progress and subject reduction, and the type substitution lemma for schemes
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 83V6MD Edit this page History Report an error Markdown