Download
programming languages and compilers cs 421 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Programming Languages and Compilers (CS 421) PowerPoint Presentation
Download Presentation
Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

91 Vues Download Presentation
Télécharger la présentation

Programming Languages and Compilers (CS 421)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://www.cs.uiuc.edu/class/sp07/cs421/ Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

  2. Type Inference - The Problem • Given an expression e, and a typing environment , does there exist a type  such that the judgment  |- e :  is valid - ie., follows from the typing rules?

  3. Type Inference - Outline • 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 gather additional constraints to guarantee a solution for components • Solve system of constraints to generate a substitution • Apply substitution to orig. type var. to get answer

  4. Type Inference - Example • What type can we give to fun x -> fun f -> f x? • Start with a type variable and then look at the way the term is constructed

  5. Type Inference - Example • First approximate: [ ] |- (fun x -> fun f -> f x) :  • Second approximate: use fun rule [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ()

  6. Type Inference - Example • Third approximate: use fun rule [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  )

  7. Type Inference - Example • Fourth approximate: use app rule [f : ; x : ] |- f :   [f : ; x : ] |- x :  [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  )

  8. Type Inference - Example • Fifth approximate: use var rule [f :  ; x : ] |- f :   [f :  ; x : ] |- x :  [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  );   ( ) .

  9. Type Inference - Example • Sixth approximate: use var rule [f :  ; x : ] |- f :   [f :  ; x : ] |- x :  [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  );   ( );   

  10. Type Inference - Example • Done building proof tree; now solve! [f :  ; x : ] |- f :   [f :  ; x : ] |- x :  [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  );   ( );   

  11. Type Inference - Example • Type unification; solve like linear equations; [f :  ; x : ] |- f :   [f :  ; x : ] |- x :  [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  );   ( );   

  12. Type Inference - Example • Eliminate : [f :  ; x : ] |- f :   [f :  ; x : ] |- x :  [f :  ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (  );   ( );   

  13. Type Inference - Example • Next eliminate  : [f : ; x : ] |- f :   [f :  ; x : ] |- x :  [f : ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) :  [ ] |- (fun x -> fun f -> f x) :  •   ();   (()  );   ( ) ;

  14. Type Inference - Example • Next eliminate  : [f : ; x : ] |- f :   [f :  ; x : ] |- x :  [f : ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) : (() ) [ ] |- (fun x -> fun f -> f x) :  •   ((() ));   (() );

  15. Type Inference - Example • Next eliminate  : [f : ; x : ] |- f :   [f :  ; x : ] |- x :  [f : ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) : (() ) [ ] |- (fun x -> fun f -> f x) : ( (() )) •   ((() ));

  16. Type Inference - Example • No more equations to solve; we are done [f : ; x : ] |- f :   [f :  ; x : ] |- x :  [f : ; x : ] |- (f x) :  [x : ] |- (fun f -> f x) : (() ) [ ] |- (fun x -> fun f -> f x) : ( (() )) • Any instance of ( (() ))is a valid type

  17. Type Inference - The Problem • Given an expression e, and a typing environment , does there exist a type  such that the judgment  |- e :  is valid - ie., follows from the typing rules?

  18. Type Inference Algorithm Let has_type (, e, ) = S •  is a typing environment • e is an expression •  is a (generalized) type, • S is a set of equations between generalized types

  19. Type Inference Algorithm • Let has_type (, e, ) = S •  is a typing environment, • e is an expression, •  is a (generalized) type, • S is a set of equations between generalized types. Idea: S is the constraints on type variables necessary for  |- e :  • Let Unif(S) be a substitution of generalized types for type variables solving S • Solution: Unif(S)() |- e : Unif(S)()

  20. Type Inference Algorithm has_type (, exp, ) = • Case exp of • Var v --> return {  (v)} • Const c --> return {  } where  |- c :  by the constant rules • fun x -> e --> • Let ,  be fresh variables • Let S = has_type ([x: ]  , e, ) • Return {    }  S

  21. Type Inference Algorithm (cont) • Case exp of • App (e1e2) --> • Let  be a fresh variable • Let S1 = has_type(, e1,   ) • Let S2 = has_type(, e2, ) • Return S1  S2

  22. Type Inference Algorithm (cont) • Case exp of • let x = e1 in e2 --> • Let  be a fresh variable • Let S1 = has_type(, e1, ) • Let S2 = has_type([x: ]  , e2, ) • Return S1  S2

  23. Type Inference Algorithm (cont) • Case exp of • let rec x = e1 in e2 --> • Let  be a fresh variable • Let S1 = has_type([x: ]  , e1, ) • Let S2 = has_type([x: ]  , e2, ) • Return S1  S2

  24. Type Inference Algorithm (cont) • To infer a type, introduce type_of • Let  be a fresh variable • type_of (, e) = • Let S = has_type (, e, ) • Return Unif(S)() • Need an algorithm for Unif