Programming Languages and Compilers CS 421 Dennis Griffith




![Type Inference - Example n First approximate: [ ] |- (fun x -> fun Type Inference - Example n First approximate: [ ] |- (fun x -> fun](https://slidetodoc.com/presentation_image_h/00f3e2b760ed214851731b97888d1675/image-5.jpg)

![Type Inference - Example Fourth approximate: use app rule [f: ; x: ]|- f Type Inference - Example Fourth approximate: use app rule [f: ; x: ]|- f](https://slidetodoc.com/presentation_image_h/00f3e2b760ed214851731b97888d1675/image-7.jpg)


![Type Inference - Example n Current subst: { } [f: ; x: ]|- f: Type Inference - Example n Current subst: { } [f: ; x: ]|- f:](https://slidetodoc.com/presentation_image_h/00f3e2b760ed214851731b97888d1675/image-10.jpg)


















- Slides: 28
Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC http: //www. cs. illinois. edu/class/cs 421/ Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter 2/19/2021 1
Two Problems n Type checking n n Question: Does exp. e have type Answer: Yes / No Method: Type derivation in env ? Typability n n n Question Does exp. e have some type in env ? If so, what is it? Answer: Type / error Method: Type inference 2/19/2021 2
Type Inference - Outline n n n Begin by assigning a type variable as the type of the whole expression Decompose the expression into component expressions Use typing rules to generate constraints on components and whole Recursively find substitution that solves typing judgment of first subcomponent Apply substitution to next subcomponent and find substitution solving it; compose with first, ext Apply comp of all substitution to orig. type var. to get answer 2/19/2021 3
Type Inference - Example n n What type can we give to (fun x -> fun f -> f (f x)) Start with a type variable and then look at the way the term is constructed 2/19/2021 4
Type Inference - Example n First approximate: [ ] |- (fun x -> fun f -> f (f x)) : Second approximate: use fun rule [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n Remember constraint ( ) n 2/19/2021 5
Type Inference - Example Third approximate: use fun rule [f : ; x : ] |- f (f x) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f(f x)) : n ( ); ( ) n 2/19/2021 6
Type Inference - Example Fourth approximate: use app rule [f: ; x: ]|- f : [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 7
Type Inference - Example Fifth approximate: use var rule, get constraint , Solve with same n Apply to next sub-proof [f: ; x: ]|- f : [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 8
Type Inference - Example n n Current subst: { } … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : ( ); ( ) 2/19/2021 9
Type Inference - Example n Current subst: { } [f: ; x: ]|- f: z [f: ; x: ]|- x: z … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) 2/19/2021 10
Type Inference - Example Current subst: { } n Var rule: Solve z Unification [f: ; x: ]|- f: z [f: ; x: ]|- x: z … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 11
Type Inference - Example Current subst: {z , } o { } n Var rule: Solve z Unification [f: ; x: ]|- f: z [f: ; x: ]|- x: z … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 12
Type Inference - Example Current subst: {z , , } n Apply to next sub-proof … [f: ; x: ]|- x: … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 13
Type Inference - Example Current subst: {z , , } n Var rule: … [f: ; x: ]|- x: … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 14
Type Inference - Example Current subst: { }o{z , , } n Solves subproof; return one layer … [f: ; x: ]|- x: … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 2/19/2021 15
Type Inference - Example Current subst: { , z , , } n Solves this subproof; return one layer n n … [f: ; x: ]|- f x : [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : ( ); ( ) 2/19/2021 16
Type Inference - Example Current subst: { , z , , } n Need to satisfy constraint ( ), given subst: (( ) ) n n [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : ( ); ( ) 2/19/2021 17
Type Inference - Example Current subst: { (( ) ), , z , , } n Solves subproof; return one layer n n [f : ; x : ] |- (f (f x)) : [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : ( ); ( ) 2/19/2021 18
Type Inference - Example Current subst: { (( ) ), , z , , } n Need to satisfy constraint ( ) given subst: ( (( ) )) n n [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : ( ); 2/19/2021 19
Type Inference - Example Current subst: { ( (( ) )), (( ) ), , z , , } n Solves subproof; return one layer n [x : ] |- (fun f -> f (f x)) : [ ] |- (fun x -> fun f -> f (f x)) : 2/19/2021 20
Type Inference - Example Current subst: { ( (( ) )), (( ) ), , z , , } n Done: ( (( ) )) n [ ] |- (fun x -> fun f -> f (f x)) : 2/19/2021 21
Type Inference Algorithm Let infer ( , e, ) = s n is a typing environment (giving polymorphic types to expression variables) n e is an expression n is a monomorphic type (with type variables), n s is a substitution of types for type variables n Idea: s is the constraints on type variables necessary for |- e : n Should have s( ) |- e : s( ) 2/19/2021 22
Type Inference Algorithm n n n fresh. Instance instantiates quantifier with new vars infer( , exp, ) = Case exp of n Var v --> return Unify{ fresh. Instance( (v))} n n n 2/19/2021 Replace all quantified type vars by fresh ones Const c --> return Unify{ fresh. Instance } where |- c : by the constant rules fun x -> e --> n Let , be fresh variables n Let s = infer ([x: ] + , e, ) n Return Unify({s( ) s( )}) o s 23
Type Inference Algorithm (cont) n Case exp of n App (e 1 e 2) --> n Let be a fresh variable n Let s 1 = infer( , e 1, ) n Let s 2 = infer(s 1( ), e 2, s 1( )) n Return s 2 o s 1 2/19/2021 24
Type Inference Algorithm (cont) n Case exp of n If e 1 then e 2 else e 3 --> n Let s 1 = infer( , e 1, bool) n Let s 2 = infer(s 1 , e 2, s 1( )) n Let s 3 = infer(s 2 o s 1( ), e 3, s 2 o s 1 ( )) n Return s 3 o s 2 o s 1 2/19/2021 25
Type Inference Algorithm (cont) n Case exp of n let x = e 1 in e 2 --> Let be a fresh variable n Let s 1 = infer( , e 1, ) n Let s 2 = infer([x: GEN(s 1( ), s 1( ))] + s 1( ), e 2, s 1( )) n Return s 2 o s 1 n 2/19/2021 26
Type Inference Algorithm (cont) n Case exp of n let rec x = e 1 in e 2 --> n Let be a fresh variable n Let s 1 = infer([x: ] + , e 1, ) n Let s 2 = Infer([x: GEN(s 1( ), s 1( ))] + s 1( ), e 2, s 1( )) n Return s 2 o s 1 2/19/2021 27
Type Inference Algorithm (cont) n To infer a type, introduce type_of Let be a fresh variable n type_of ( , e) = n Let s = infer ( , e, ) n n n Return s ( ) Need an algorithm for Unif (next time) 2/19/2021 28