DOC PREVIEW
U of I CS 421 - Programming Languages and Compilers

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming Languages and Compilers (CS 421)Type Inference - The ProblemType Inference - OutlineType Inference - ExampleSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Type Inference AlgorithmSlide 19Slide 20Type Inference Algorithm (cont)Slide 22Slide 23Slide 24Programming Languages and Compilers (CS 421)Elsa L Gunter2112 SC, UIUChttp://www.cs.uiuc.edu/class/sp07/cs421/Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul AghaElsa L. Gunter 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?Elsa L. Gunter 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 answerElsa L. Gunter Type Inference - Example•What type can we give tofun x -> fun f -> f x?•Start with a type variable and then look at the way the term is constructedElsa L. Gunter 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) :   (  )Elsa L. Gunter Type Inference - Example•Third approximate: use fun rule[f :  ; x :  ] |- (f x) : [x : ] |- (fun f -> f x) : [ ] |- (fun x -> fun f -> f x) :   (  );   (  )Elsa L. Gunter 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) :   (  );   (  )Elsa L. Gunter 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) :   (  );   (   );   (  ) .Elsa L. Gunter 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) :   (  );   (   );   (  );   Elsa L. Gunter 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) :   (  );   (   );   (  );   Elsa L. Gunter 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) :   (  );   (   );   (  );   Elsa L. Gunter 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) :   (  );   (   );   (  );   Elsa L. Gunter 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) :   (  );   ((   )  );   (  ) ;Elsa L. Gunter 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) :   (  ((   )  ));   ((  )  );Elsa L. Gunter 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) : (  ((   )  ))   (  ((   )  ));Elsa L. Gunter 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 typeElsa L. Gunter 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?Elsa L. Gunter Type Inference AlgorithmLet has_type (, e, ) = …


View Full Document

U of I CS 421 - Programming Languages and Compilers

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download Programming Languages and Compilers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Languages and Compilers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Languages and Compilers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?