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 andCompilers (CS 421)Elsa L Gunter2112 SC, UIUChttp://www.cs.uiuc.edu/class/sp07/cs421/Based in part on slides by Mattox Beckman, as updatedby Vikram Adve and Gul AghaElsa L. GunterType Inference - The Problem• Given an expression e, and a typingenvironment Γ, does there exist atype τ such that the judgment Γ |- e : τ is valid - ie., follows from the typingrules?Elsa L. GunterType Inference - Outline• Begin by assigning a type variable as the type ofthe whole expression• Decompose the expression into componentexpressions• Use typing rules to generate constraints oncomponents and whole• Recursively gather additional constraints toguarantee a solution for components• Solve system of constraints to generate asubstitution• Apply substitution to orig. type var. to get answerElsa L. GunterType Inference - Example• What type can we give tofun x -> fun f -> f x?• Start with a type variable and then lookat the way the term is constructedElsa L. GunterType 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. GunterType Inference - Example• Third approximate: use fun rule[f : δ ; x : β] |- (f x) : ε[x : β] |- (fun f -> f x) : γ[ ] |- (fun x -> fun f -> f x) : α• α ≡ (β → γ); γ ≡ (δ → ε)Elsa L. GunterType 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. GunterType 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. GunterType 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. GunterType 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. GunterType Inference - Example• Type unification; solve like linearequations;[f : δ ; x : β] |- f : ϕ → ε [f : δ ; x : β] |- x : ϕ[f : δ ; x : β] |- (f x) : ε[x : β] |- (fun f -> f x) : γ[ ] |- (fun x -> fun f -> f x) : α• α ≡ (β → γ); γ ≡ (δ → ε); δ ≡ (ϕ → ε); ϕ ≡ βElsa L. GunterType 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. GunterType 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. GunterType 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. GunterType 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. GunterType 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 validtypeElsa L. GunterType Inference - The Problem• Given an expression e, and a typingenvironment Γ, does there exist atype τ such that the judgment Γ |- e : τ is valid - ie., follows from the typingrules?Elsa L. GunterType Inference AlgorithmLet has_type (Γ, e, τ) = S• Γ is a typing environment• e is an expression• τ is a (generalized) type,• S is a set of equations betweengeneralized typesElsa L. GunterType 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 variablesnecessary for Γ …


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?