![Page 1: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/1.jpg)
L07-1 September 29, 2011
Type Inference and the Hindley-Milner Type System
Armando Solar-Lezama Computer Science and Artificial Intelligence Laboratory
M.I.T.With slides from Arvind. Used with permission.
September 29, 2011
![Page 2: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/2.jpg)
L06-2
Type Inference
โข Consider the following expression โ (๐f:int int. f 5) (๐x:int. x + 1)
โข Is it well typed in F1?
![Page 3: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/3.jpg)
L06-3
Type Inference
โข There wasnโt a single point in the derivation where we had to look at the type labels in order to know what rule to apply! โ we could have written the derivation without the labels
โข The labels helped us determine the actual types for all the ๐s in the typing rules. โ we could have figured these out even without the labels
โ this is the key idea behind type inference!
![Page 4: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/4.jpg)
L06-4
Type Inference Strategy 1
โข 1. Use the typing rules to define constraints on the possible types of expressions
โข 2. Solve the resulting constraint system
![Page 5: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/5.jpg)
L06-5
Deducing Types
1. Assign types to every subexpression x :: t0 f :: t1
f x :: t2 f (f x) :: t3
twice :: ?
twice f x = f (f x)
What is the most "general type" for twice?
2. Set up the constraints t1 = because of (f x) t1 = because of f (f x)
3. Resolve the constraints t0 -> t2 = t2 -> t3
t0 = t2 and t2 = t3 t0 = t2 = t3
twice :: (t0 -> t0) -> t0 -> t0
t1 -> t0 -> t3
t0 -> t2
t2 -> t3
September 8, 2011
![Page 6: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/6.jpg)
L06-6
The language of Equality Constraints
โข Consider the following Language of Constraints
โข Constraints in this language have a lot of good properties โ Nice and compositional
โ Linear time solution algorithm
![Page 7: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/7.jpg)
L06-7
Building Constraints from Typing Rules
โข Notation
โ The constraints on the right ensure that the
judgment on the left holds
โ This mapping is defined recursively.
โข Base cases
โข Inductive Cases
ฮ โข ๐1 + ๐2: ๐ = ฮ โข ๐1: ๐๐๐ก โง ฮ โข ๐2: ๐๐๐ก โง ๐ = ๐๐๐ก
![Page 8: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/8.jpg)
L06-8
Back to our example
o (๐f. f 5) (๐x. x + 1)
![Page 9: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/9.jpg)
L06-9
Equality and Unification
โข What does it mean for two types a and b to be equal? โ Structural Equality Suppose a = 1 -> 2 b = 3 -> 4 Is a = b ?
iff 1 = 3 and 2 = 4
โข Can two types be made equal by choosing appropriate substitutions for their type variables? โ Robinsonโs unification algorithm Suppose a = t1 -> Bool b = Int -> t2 Are a and b unifiable ? if t1= Int and t2= Bool
Suppose a = t1-> Bool b = Int -> Int
Are a and b unifiable ? No September 27, 2011
![Page 10: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/10.jpg)
L06-10
Simple Type Substitutions needed to define type unification
A substitution is a map S : Type Variables Types S = [1 / t1,..., n / tn] โ = S โ is a Substitution Instance of Example: S = [(t -> Bool) / t1] S ( t1 -> t1) = ? Substitutions can be composed, i.e., S2 S1
Example:
S1 = [(t -> Bool) / t1] ; S2 = [Int / t]
S2 S1 ( t1 -> t1) ?
Types ::= base types (Int, Bool ..)
| t type variables | -> Function types
( t -> Bool) -> ( t -> Bool)
= S2 (( t -> Bool) -> ( t -> Bool)) = ( Int -> Bool) -> ( Int -> Bool)
September 27, 2011
![Page 11: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/11.jpg)
L06-11
Unification An essential subroutine for type inference
def Unify(1, 2) = case (1, 2) of
(1, t2) = [1 / t2] provided t2 FV(1) (t1, 2) = [2 / t1] provided t1 FV(2) (1, 2) = if (eq? 1 2) then [ ]
else fail (11->12, 21 ->22) =
Unify(1, 2) tries to unify 1 and 2 and returns a substitution if successful
Does the order matter?
let S1=Unify(11, 21) S2=Unify(S1(12), S1(22)) in S2 S1
otherwise = fail
No
September 27, 2011
![Page 12: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/12.jpg)
L06-12
Type inference strategy 2
โข Like strategy 1, but we solve the constraints as we see them โ Build the substitution map incrementally
September 29, 2011
![Page 13: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/13.jpg)
L06-13
Simple Inference Algorithm
W(TE, e) returns (S,) such that S (TE) โ e :
The type environment TE records the most general type of each identifier while the substitution S records the changes in the type variables
Def W(TE, e) = Case e of x = ... n = ... x.e = ... (e1 e2) = ... ...
September 27, 2011
This is just ฮ (itโs hard to write ฮ in code)
![Page 14: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/14.jpg)
L06-14
Simple Inference Algorithm (cont-1)
Def W(TE, e) = Case e of
N = x = x.e = (e1 e2) =
let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
September 27, 2011
let (S1, 2)= W(TE + { x : u }, e) in (S1, S1(u) -> 2)
if (x Dom(TE)) then Fail else let = TE(x); in ({}, )
({}, Typeof(N))
uโs represent new type variables
![Page 15: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/15.jpg)
L06-15
Simple Inference Algorithm (cont-1)
Def W(TE, e) = Case e of
c = x = x.e = (e1 e2) =
let x = e1 in e2 =
uโs represent new type variables
let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
let (S1, 1) = W(TE + {x : u}, e1); S2 = Unify(S1(u), 1); (S3, 2) = W(S2 S1(TE) + {x : }, e2); in (S3 S2 S1, 2)
September 27, 2011
let (S1, 1) = W(TE + { x : u }, e) in (S1, S1(u) -> 1)
if (x Dom(TE)) then Fail else let = TE(x); in ({}, )
({}, Typeof(c))
![Page 16: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/16.jpg)
Example Def W(TE, e) =
Case e of
โฆ
x.e = let (S1, 1) = W(TE + { x : u }, e) in (S1, S1(u) -> 1)
(e1 e2) = let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
๐(โ , ๐๐. ๐ 5 ๐๐ฅ. ๐ฅ )
๐ โ , ๐๐. ๐ 5 =
๐ ๐: ๐ข0 , ๐ 5 =
๐ ๐:๐ข0 , ๐ = (โ , ๐ข0) ๐ ๐:๐ข0 , 5 = (โ , ๐ผ๐๐ก)
๐๐๐๐๐ฆ ๐ข0, ๐ผ๐๐ก โ ๐ข1 =
![Page 17: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/17.jpg)
Example
๐(โ , ๐๐. ๐ 5 ๐๐ฅ. ๐ฅ )
๐ โ , ๐๐. ๐ 5 =
๐ ๐: ๐ข0 , ๐ 5 =
๐ ๐:๐ข0 , ๐ = (โ , ๐ข0) ๐ ๐:๐ข0 , 5 = (โ , ๐ผ๐๐ก)
๐๐๐๐๐ฆ ๐ข0, ๐ผ๐๐ก โ ๐ข1 =
def Unify(1, 2) = case (1, 2) of
(1, t2) = [1 / t2] provided t2 FV(1)
(t1, 2) = [2 / t1] provided t1 FV(2)
(1, 2) = if (eq? 1 2) then [ ] else fail
(11->12, 21 ->22) = let S1=Unify(11, 21) S2=Unify(S1(12), S1(22))
in S2 S1
[(๐ผ๐๐ก โ ๐ข1) ๐ข0 ]
![Page 18: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/18.jpg)
Example
๐(โ , ๐๐. ๐ 5 ๐๐ฅ. ๐ฅ )
๐ โ , ๐๐. ๐ 5 =
๐ ๐: ๐ข0 , ๐ 5 =
๐ ๐:๐ข0 , ๐ = (โ , ๐ข0) ๐ ๐:๐ข0 , 5 = (โ , ๐ผ๐๐ก)
๐๐๐๐๐ฆ ๐ข0, ๐ผ๐๐ก โ ๐ข1 = [(๐ผ๐๐ก โ ๐ข1) ๐ข0 ]
Def W(TE, e) =
Case e of
โฆ
x.e = let (S1, 1) = W(TE + { x : u }, e) in (S1, S1(u) -> 1)
(e1 e2) = let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
([๐ผ๐๐ก โ ๐ข1 ๐ข0 ], ๐ข1)
( (๐ผ๐๐ก โ ๐ข1) ๐ข0 , ๐ผ๐๐ก โ ๐ข1 โ ๐ข1)
![Page 19: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/19.jpg)
Example
๐(โ , ๐๐. ๐ 5 ๐๐ฅ. ๐ฅ )
๐ โ , ๐๐. ๐ 5 =
Def W(TE, e) =
Case e of
โฆ
x.e = let (S1, 1) = W(TE + { x : u }, e) in (S1, S1(u) -> 1)
(e1 e2) = let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
( (๐ผ๐๐ก โ ๐ข1) ๐ข0 , ๐ผ๐๐ก โ ๐ข1 โ ๐ข1)
๐ โ , ๐๐ฅ. ๐ฅ = (โ , ๐ข3 โ ๐ข3)
๐๐๐๐๐ฆ ๐ผ๐๐ก โ ๐ข1 โ ๐ข1, ๐ข3 โ ๐ข3 โ ๐ข4 =
![Page 20: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/20.jpg)
Example
๐(โ , ๐๐. ๐ 5 ๐๐ฅ. ๐ฅ )
๐๐๐๐๐ฆ ๐ผ๐๐ก โ ๐ข1 โ ๐ข1, ๐ข3 โ ๐ข3 โ ๐ข4 =
def Unify(1, 2) = case (1, 2) of
(1, t2) = [1 / t2] provided t2 FV(1)
(t1, 2) = [2 / t1] provided t1 FV(2)
(1, 2) = if (eq? 1 2) then [ ] else fail
(11->12, 21 ->22) = let S1=Unify(11, 21) S2=Unify(S1(12), S1(22))
in S2 S1
๐๐๐๐๐ฆ ๐ผ๐๐ก, ๐ข4 = [๐ผ๐๐ก ๐ข4 ]
๐๐๐๐๐ฆ ๐ผ๐๐ก โ ๐ข1 , ๐ข3 โ ๐ข3 = ๐ผ๐๐ก ๐ข3 ; ๐ผ๐๐ก ๐ข1
๐ผ๐๐ก ๐ข3 ; ๐ผ๐๐ก ๐ข1 ; ๐ผ๐๐ก/๐ข4
![Page 21: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/21.jpg)
Example
๐ โ , ๐๐. ๐ 5 ๐๐ฅ. ๐ฅ =
๐ โ , ๐๐. ๐ 5 =
Def W(TE, e) =
Case e of
โฆ
x.e = let (S1, 1) = W(TE + { x : u }, e) in (S1, S1(u) -> 1)
(e1 e2) = let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
( (๐ผ๐๐ก โ ๐ข1) ๐ข0 , ๐ผ๐๐ก โ ๐ข1 โ ๐ข1)
๐ โ , ๐๐ฅ. ๐ฅ = (โ , ๐ข3 โ ๐ข3)
๐๐๐๐๐ฆ ๐ผ๐๐ก โ ๐ข1 โ ๐ข1, ๐ข3 โ ๐ข3 โ ๐ข4 = ๐ผ๐๐ก ๐ข3 ; ๐ผ๐๐ก ๐ข1 ; ๐ผ๐๐ก/๐ข4
( (๐ผ๐๐ก โ ๐ข1) ๐ข0 ; ๐ผ๐๐ก ๐ข3 ; ๐ผ๐๐ก ๐ข1 ; ๐ผ๐๐ก/๐ข4 , ๐ผ๐๐ก)
![Page 22: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/22.jpg)
L06-22
What about Let?
โข ๐๐๐ก ๐ฅ = ๐1 ๐๐ ๐2
โข Typing rule
โฮ;๐ฅ:๐โฒโข๐1:๐
โฒ ฮ;๐ฅ:๐โฒโข๐2:๐
ฮ โข๐๐๐ก ๐ฅ=๐1๐๐ ๐2โถ๐
โข Constraints โ ฮ โข ๐๐๐ก ๐ฅ = ๐1๐๐ ๐2 โถ ๐ =
โ๐โฒ, ฮ; ๐ฅ: ๐โฒ โข ๐1: ๐โฒ โง ฮ; ๐ฅ: ๐โฒ โข ๐2: ๐
โข Algorithm
September 29, 2011
Case Exp = let x = e1 in e2 => let (S1, 1) = W(TE + {x : u}, e1); S2 = Unify(S1(u), 1); (S3, 2) = W(S2 S1(TE) + {x : }, e2); in (S3 S2 S1, 2)
This is Hindley Milner
without polymorphism
![Page 23: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/23.jpg)
L06-23
Polymorphism
September 27, 2011
![Page 24: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/24.jpg)
L06-24
Some observations
โข A type system restricts the class of programs that are considered โlegalโ
โข It is possible a term in the untyped โcalculus may be reducible to a value but may not be typeable in a particular type system
let id = x. x in
... (id True) ... (id 1) ...
This term is not typeable in the simple type system we have discussed so far. However, it is typeable in the Hindley-Milner system
September 29, 2011
![Page 25: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/25.jpg)
L06-25
Explicit polymorphism
โข Youโve seen this before
โข How do we formalize this?
โข Example
๐๐ = ฮ๐. ๐๐ฅ โถ ๐. ๐ฅ
๐๐ ๐๐๐ก 5 September 27, 2011
public interface List<E>{
void add(E x);
E get();
}
List<String> ls = ...
ls.add("Hello");
String hello = ls.get(0);
ฮ โข ๐ โถ ๐
ฮ โข ฮ๐ก. ๐ โถ โ๐ก. ๐
ฮ โข ๐ โถ โ๐ก. ๐โฒ
ฮ โข ๐[๐] โถ ๐โฒ[๐ / ๐ก]
![Page 26: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/26.jpg)
L06-26
Different Styles of Polymorphism
โข Impredicative Polymorphism
๐ ::= ๐ ๐1 โ ๐2 ๐ | โ๐. ๐
e ::= ๐ฅ ๐๐ฅ: ๐. ๐ ๐1๐2 ฮT. e e[๐]
โข Very powerful โ Although you still canโt express recursion
โข Type inference is undecidable !
![Page 27: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/27.jpg)
L06-27
Different Styles of Polymorphism
โข Predicative Polymorphism
๐ ::= ๐ ๐1 โ ๐2 ๐
๐ โท= ๐ โ๐. ๐ ๐1 โ ๐2
e ::= ๐ฅ ๐๐ฅ: ๐. ๐ ๐1๐2 ฮT. e e[๐]
โข Still very powerful โ But you can no longer instantiate with a polymorphic type
โข Type inference is still undecidable !
![Page 28: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/28.jpg)
L06-28
Different Styles of Polymorphism
โข Prenex Predicative Polymorphism
๐ ::= ๐ ๐1 โ ๐2 ๐
๐ โท= ๐ |โ๐. ๐
e ::= ๐ฅ ๐๐ฅ: ๐. ๐ ๐1๐2 ฮT. e e[๐]
โข Now we have decidable type inference
โข But polymorphism is now very limited โ We canโt pass polymorphic functions as arguments!!
โ (๐๐ : โ๐ . ๐ โฆ ๐ ๐๐๐ก ๐ฅ โฆ ๐ ๐๐๐๐ ๐ฅ)(ฮ๐. ๐๐๐๐ ๐๐๐ ๐ ๐๐๐ก)
![Page 29: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/29.jpg)
L06-29
Let polymorphism
โข Introduce let x = e1 in e2 โ Just like saying ๐๐ฅ. ๐2 ๐1
โ Except x can be polymorphic
โข Good engineering compromise โ Enhance expressiveness
โ Preserve decidability
โข This is the Hindley Milner type system
September 29, 2011
![Page 30: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/30.jpg)
L06-30
Type inference with polymorphism
September 27, 2011
![Page 31: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/31.jpg)
L06-31
Polymorphic Types
Constraints:
let id = x. x in
... (id True) ... (id 1) ...
id :: t1 --> t1 id :: Int --> t2 id :: Bool --> t3
Does not unify!!
id :: t1. t1 --> t1
Different uses of a generalized type variable may be instantiated differently
id2 : Bool --> Bool id1 : Int --> Int
Solution: Generalize the type variable
When can we
generalize?
September 29, 2011
![Page 32: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/32.jpg)
L06-32
A mini Language to study Hindley-Milner Types
โข There are no types in the syntax of the language! โข The type of each subexpression is derived by the
Hindley-Milner type inference algorithm.
Expressions E ::= c constant
| x variable | x. E abstraction | (E1 E2) application | let x = E1 in E2 let-block
September 29, 2011
![Page 33: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/33.jpg)
L06-33
A Formal Type System
Note, all the โs occur in the beginning of a type scheme, i.e., a type cannot contain a type scheme
Types ::= base types
| t type variables | 1 -> 2 Function types
Type Schemes ::= | t.
Type Environments TE ::= Identifiers Type Schemes
September 29, 2011
![Page 34: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/34.jpg)
L06-34
Instantiations
โข Type scheme can be instantiated into a type โ by substituting types for the bound variables of , i.e., โ = S for some S s.t. Dom(S) BV()
- โ is said to be an instance of ( > โ)
- โ is said to be a generic instance of when S
maps variables to new variables.
= t1...tn.
Example: =t1. t1 -> t2
t3 -> t2 is a generic instance of Int -> t2 is a non generic instance of
September 29, 2011
![Page 35: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/35.jpg)
L06-35
Generalization aka Closing
โข Generalization introduces polymorphism
โข Quantify type variables that are free in but not free in the type environment (TE)
โข Captures the notion of new type variables of
Gen(TE,) = t1...tn. where { t1...tn } = FV() - FV(TE)
September 29, 2011
![Page 36: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/36.jpg)
L06-36
HM Type Inference Rules
(App) ฮโข๐1:๐โ๐โฒ ฮโข๐2:๐
ฮโข ๐1๐2 :๐โฒ
(Abs) ฮ ; ๐ฅ:๐ โข๐โถ๐โฒ
ฮโข๐๐ฅ.๐โถ๐โ๐โฒ
(Var) ๐ฅ:๐ โฮ ๐โฅ๐
ฮโข๐ฅ:๐
(Const) ๐ก๐ฆ๐๐๐๐(๐)โฅ๐
ฮโข๐:๐
(Let) ฮ; ๐ฅ:๐ โข๐1:๐ ฮ; ๐ฅ:๐บ๐๐(ฮ,๐) โข๐2:๐โฒ
ฮโข(๐๐๐ก ๐ฅ=๐1 ๐๐ ๐2):๐โฒ
September 29, 2011
๐ฅ can be considered of type ๐ as long as its type as specified in the environment can be specialized to ๐ (i.e. ๐ is an instance of ๐)
Note: x has a different type in ๐1 than in ๐2. In ๐1, x is not a polymorphic type, but in ๐2 it gets generalized into one.
Remember, ๐ stands for a monotype, ๐ for a polymorphic type
![Page 37: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/37.jpg)
L06-37
HM Inference Algorithm
Def W(TE, e) = Case e of c = ({}, Typeof(c)) x = x.e = (e1 e2) =
let x = e1 in e2 =
uโs represent new type variables
let (S1, 1) = W(TE, e1); (S2, 2) = W(S1(TE), e2); S3 = Unify(S2(1), 2 -> u); in (S3 S2 S1, S3(u))
if (x Dom(TE)) then Fail else let t1...tn. = TE(x); in ( { }, [ui / ti] )
let (S1, 1) = W(TE + { x : u }, e); in (S1, S1(u) -> 1)
let (S1, 1) = W(TE + {x : u}, e1); S2 = Unify(S1(u), 1); = Gen(S2 S1(TE), S2(1) ); (S3, 2) = W(S2 S1(TE) + {x : }, e2); in (S3 S2 S1, 2)
September 29, 2011
![Page 38: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/38.jpg)
L06-38
Hindley-Milner: Example
x. let f = y.x
in (f 1, f True)
W(, A) =
W({x : u1}, B) =
( [ ] , u1 )
( [ ] , u3 -> u1 )
u3.u3 -> u1
TE = {x : u1, f : u3.u3 -> u1}
( [ ] , u4 -> u1 )
W(TE, 1) = ( [ ] , Int ) [ Int / u4 , u1 / u5 ]
( [ ] , u1 )
( [ ] , (u1,u1) )
Unify(u4 -> u1 , Int -> u5) =
W({x : u1, f : u2, y : u3}, x) =
W({x : u1, f : u2}, y.x) =
Gen({x : u1}, u3 -> u1) =
W(TE, (f 1) ) =
( [ ] , u1 -> (u1,u1) )
W(TE, f) =
Unify(u2 , u3 -> u1) =
A B
[ (u3 -> u1) / u2 ]
... September 29, 2011
![Page 39: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/39.jpg)
L06-39
Important Observations
โข Do not generalize over type variables used elsewhere
โข Let is the only way of defining polymorphic constructs
โข Generalize the types of let-bound identifiers only after processing their definitions
September 29, 2011
![Page 40: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/40.jpg)
L06-40
Properties of HM Type Inference
โข It is sound with respect to the type system. An inferred type is verifiable.
โข It generates most general types of expressions. Any verifiable type is inferred.
โข Complexity
PSPACE-Hard Nested let blocks
September 29, 2011
![Page 41: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/41.jpg)
L06-41
Extensions
โข Type Declarations Sanity check; can relax restrictions
โข Incremental Type checking The whole program is not given at the same time, sound inferencing when types of some functions are not known
โข Typing references to mutable objects Hindley-Milner system is unsound for a language with refs (mutable locations) โข Overloading Resolution
September 29, 2011
![Page 42: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/42.jpg)
L06-42
HM Limitations: -bound vs Let-bound Variables
Only let-bound identifiers can be instantiated differently.
let twice f x = f (f x) in twice twice succ 4
versus let twice f x = f (f x) foo g = (g g succ) 4 in foo twice
foo is not type correct !
Generic vs. Non-generic type variables
September 29, 2011
![Page 43: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/43.jpg)
L06-43
Puzzle: Another set of Inference rules
(Gen) TE โ e : t FV(TE)
TE โ e : t. (Spec) TE โ e : t.
TE โ e : [u/t]
(Var) (x : ) TE
TE โ x :
(Let) TE+{x:} โ e1: TE+{x:} โ e2:โ
TE โ (let x = e1 in e2) : โ
(App) and (Abs) rules remain unchanged.
Sound but no inference algorithm !
September 29, 2011
![Page 44: Type Inference and the Hindley-Milner Type Systemย ยท โขIntroduce let x = e1 in e2 โ Just like saying ๐ . 2 1 โ Except x can be polymorphic โขGood engineering compromise โ](https://reader035.vdocuments.us/reader035/viewer/2022070812/5f0afe577e708231d42e5542/html5/thumbnails/44.jpg)
MIT OpenCourseWare
http://ocw.mit.edu
6.820 Fundamentals of Program AnalysisFall 2015
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.