Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05.

Similar presentations


Presentation on theme: "CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05."— Presentation transcript:

1 CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05

2 Cornell University CS 611 Fall'04 -- Andrew Myers2 Type inference (reconstruction) Simple typed language: e ::= x | b | x: . e | e 1 e 2 | e 1 + e 2 | if e 0 then e 1 else e 2 | let x=e 1 in e 2 | rec y:  1  2.( x.e)  ::= unit | bool | int |  1  2 Question: Do we really need to write type declarations? e ::= … | x.e | …| rec y.( x.e)

3 Cornell University CS 611 Fall'04 -- Andrew Myers3 Typing rules e ::= x | b | x.e | e 1 e 2 | e 1  e 2 | if e 0 then e 1 else e 2 | let x=e 1 in e 2 | rec y. x.e  x:   e :     x. e :    y:    x:   e :     rec y. x. e :   Problem: how does type checker construct proof? Guess   

4 Cornell University CS 611 Fall'04 -- Andrew Myers4 Example let square = z.z*z in ( f. x. y. if (f x y) then (f (square x) y) else (f x (f x y))) What is the type of this program?

5 Cornell University CS 611 Fall'04 -- Andrew Myers5 Manual type inference let square = z.z*z in ( f. x. y. if (f x y) then (f (square x) y) else (f x (f x y))) z : int s, square : int  int f :  x  y  bool y :  y = bool x :  x = int Answer: (int  bool  bool)  int  bool  bool

6 Cornell University CS 611 Fall'04 -- Andrew Myers6 Type inference Goal: reconstruct types even after erasure Idea: run ordinary type-checking algorithm, generate type equations on type variables f:T2, x:T5  f : int  T6f:T2, x:T5  1 : int f:T2, x:T5  f 1 : T6 f:T2  x. f 1 : T1 (=T5  T6) y:T3  y : T4  f. x. f 1 : T2  T1  ( y.y) : T2 (T2=T3  T4)  ( f. x. (f 1)) ( y.y) : T1 T2 = T3  T4, T3 = T4, T1 =T5  T6, T2 = int  T6 (T3=T4)

7 Cornell University CS 611 Fall'04 -- Andrew Myers7   e 0 :  0   e 1 :  1  0 =  1     e 0 e 1 : T 2 Typing rules  x:T x   e :    x. e : T x    n : int   e 0 :  0   e 1 :  1   e 2 :  2  1 =  2,  0 = bool   if e 0 then e 1 else e 2 :  1   e 1 :  1 , x :  1  e 2 :  2   let x =e 1 in e 2 :  2  x:T 1, y:T 1  T 2  e :  T 2   rec y. x.e : T 1  T 2 Only type metavariables on RHS of premises x  dom(    x :  (x)

8 Cornell University CS 611 Fall'04 -- Andrew Myers8 Unification How to solve equations? Idea: given equation   =  , unify type expressions to solve for variables in both Example:    int = (bool   )   Result: substitution    bool  ,    int   int =   bool  

9 Cornell University CS 611 Fall'04 -- Andrew Myers9 Robinson’s algorithm (1965) Unification produces weakest substitution that equates two trees –    int = (bool   )   equated by any    bool  ,    int,     –Defn. S 1 is weaker than S 2 if S 2 = S 3  S 1 for S 3 a non-trivial substitution Unify(E) where E is set of equations gives weakest equating substitution: define recursively Unify(T =  E) = Unify(E{  /T})  [T   ] (if T  FTV(  ) 

10 Cornell University CS 611 Fall'04 -- Andrew Myers10 Rest of algorithm Unify(T =  E) = Unify(E{  /T})  [T   ] (if T  FTV     Unify(  ) =  Unify(B = B, E) = Unify(E) Unify(B 1 = B 2, E) = ? Unify(T = T, E) = Unify(E) Unify(        , E) = Unify(    ,    , E) Well-founded? Degree = (#vars, size(eqns))

11 Cornell University CS 611 Fall'04 -- Andrew Myers11 Type inference algorithm R (e, , S) =  , S  means “Reconstructing the type of e in typing context  with respect to substitution S yields type , identical or stronger substitution S  or “S  is weakest substitution no weaker than than S such that S  (  )    e  S  (  )” Define: Unify(E, S) = Unify(SE)  S –solve substituted equations E and fold in new substitutions

12 Cornell University CS 611 Fall'04 -- Andrew Myers12 Inductive defn of inference R (e, , S) =  , S   “S  is weakest substitution stronger than (or same as) S such that S  (  )    e  S  (  )” Unify(E, S) = Unify(SE)  S R (n, , S) =  int, S  R ( true, , S) =  bool, S  R (x, , S) =   (x), S  R (e 1 e 2, , S) = let  T1, S1  = R (e 1, , S) in let  T2, S2  = R (e 2, , S1) in  T f, Unify(T1=T2  T f, S2)  R ( x.e, , S) = let  T1, S1  = R (e,  x  T f , S) in  T f  T1, S1  where T f is “fresh” (not mentioned anywhere in e, , S)

13 Cornell University CS 611 Fall'04 -- Andrew Myers13 Example R (( x.x) 1, ,  ) = let  T1, S1  = R ( x.x, ,  ) in let  T2, S2  = R (1, , S1) in  T3, Unify(T1  T3 = T2, S2)  R ( x.x, ,  ) = let  T1, S1  = R (x,  x  T4 ,  ) in  T4  T1, S1  =  T4  T4,   = let  T2, S2  = R (1, ,  ) in  T3, Unify(T2  T3 = T4  T4,  )  =  T3, Unify(int  T3 = T4  T4,  )  =  T3, Unify(int=T4, T3 = T4,  )  =  T3, Unify(T3 = int,  T4  int])  =  T3, [T3  int, T4  int]) 

14 Cornell University CS 611 Fall'04 -- Andrew Myers14 Implementation Can implement with imperative update: datatype type = Int | Bool | Arrow of type * type | TypeVar of type option ref fun freshTypeVar() = TypeVar(ref NONE) fun resolve(t: type) = case t of TypeVar(ref (SOME t’)) => t’ | _ => t fun unify(t1: type, t2: type) : unit = case (resolve t1, resolve t2) of (TypeVar(r as ref NONE), t2) => r := t2 | (t1, TypeVar(r as ref NONE)) => r := t1 | (Arrow(t1,t2), Arrow(t3,t4)) => unify(t1,t3); unify(t2,t4) | (Int, Int) => () | (Bool,Bool) => () | _ => raise Fail “Can’t unify types”

15 Cornell University CS 611 Fall'04 -- Andrew Myers15 Polymorphism R ( x.x, ,  ) = let  T1, S1  = R (x,  x  T4 ,  ) in  T4  T1, S1  =  T4  T4,   Reconstruction algorithm doesn’t solve type fully… opportunity! x.x can have type T4  T4 for any T4 –polymorphic (= “many shape”) term –Could reuse same expression multiple places in program, with different types: let id = ( x.x) in … (f id) … (g x id) … id


Download ppt "CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05."

Similar presentations


Ads by Google