Programming Languages and Compilers (CS 421) - PowerPoint PPT Presentation

About This Presentation
Title:

Programming Languages and Compilers (CS 421)

Description:

Munawar Hafiz 2219 SC, UIUC http://www.cs.illinois.edu/class/cs421/ Based in part on s by Mattox Beckman, as updated by Vikram Adve and Gul Agha – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 24
Provided by: Me
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages and Compilers (CS 421)


1
Programming Languages and Compilers (CS 421)
  • Munawar Hafiz
  • 2219 SC, UIUC
  • http//www.cs.illinois.edu/class/cs421/

Based in part on slides by Mattox Beckman, as
updated by Vikram Adve and Gul Agha
2
Two Problems
  • Type checking
  • Question Does exp. e have type ? in env ??
  • Answer Yes / No
  • Method Type derivation
  • Typability
  • Question Does exp. e have some type in env. ?? If
    so, what is it?
  • Answer Type ? / error
  • Method Type inference

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 -gt fun f -gt 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 -gt fun f -gt f x) ?
  • Second approximate use fun rule
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?)

6
Type Inference - Example
  • Third approximate use fun rule
  • f ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?) ? ? (? ? ?)

7
Type Inference - Example
  • Fourth approximate use app rule
  • f ? x ? - f ? ? ? f ? x ? - x
    ?
  • f ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?) ? ? (? ? ?)

8
Type Inference - Example
  • Fifth approximate use var rule
  • f ? x ? - f ? ? ? f ? x ? -
    x ?
  • f ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?) ? ? (? ? ?) ? ? (? ? ?) .

9
Type Inference - Example
  • Sixth approximate use var rule
  • f ? x ? - f ? ? ? f ? x ? -
    x ?
  • f ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt 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 -gt f x) ?
  • - (fun x -gt fun f -gt 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 -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?) ? ? (? ? ?) ? ? (? ? ?) ? ? ?

12
Type Inference - Example
  • Eliminate ?
  • f ? x ? - f ? ? ? f ? x ? -
    x ?
  • f ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?) ? ? (? ? ?) ? ? (? ? ?) ? ? ?

13
Type Inference - Example
  • Next eliminate ?
  • f ? ? ? x ? - f ? ? ? f ? ? ? x
    ? - x ?
  • f ? ? ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ?
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ?) ? ? ((? ? ?) ? ?) ? ? (? ? ?)

14
Type Inference - Example
  • Next eliminate ?
  • f? ? ? x? - f? ? ? f? ? ? x? - x?
  • f ? ? ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ((? ? ?) ? ?)
  • - (fun x -gt fun f -gt f x) ?
  • ? ? (? ? ((? ? ?) ? ?)) ? ? ((? ? ?) ? ?)

15
Type Inference - Example
  • Next eliminate ?
  • f ? ? ? x? - f ? ? ? f? ? ? x? -
    x?
  • f ? ? ? x ? - (f x) ?
  • x ? - (fun f -gt f x) ((? ? ?) ? ?)
  • - (fun x -gt fun f -gt 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 -gt f x) ((? ? ?) ? ?)
  • - (fun x -gt fun f -gt f x) (? ? ((? ? ?) ?
    ?))
  • Any instance of (? ? ((? ? ?) ? ?)) is a valid
    type

17
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)(?)

18
Type Inference Algorithm
  • has_type (?, exp, ?)
  • Case exp of
  • Var v --gt return ? ? ?(v)
  • Const c --gt return ? ? ? where ? - c ? by
    the constant rules
  • fun x -gt e --gt
  • Let ?, ? be fresh variables
  • Let S has_type (x ? ?, e, ?)
  • Return ? ? ? ? ? ? S

19
Type Inference Algorithm (cont)
  • Case exp of
  • App (e1 e2) --gt
  • Let ? be a fresh variable
  • Let S1 has_type(?, e1, ? ? ?)
  • Let S2 has_type(?, e2, ?)
  • Return S1 ? S2

20
Type Inference Algorithm (cont)
  • Case exp of
  • If e1 then e2 else e3 --gt
  • Let S1 has_type(?, e1, bool)
  • Let S2 has_type(?, e2, ?)
  • Let S2 has_type(?, e2, ?)
  • Return S1 ? S2? S3

21
Type Inference Algorithm (cont)
  • Case exp of
  • let x e1 in e2 --gt
  • Let ? be a fresh variable
  • Let S1 has_type(?, e1, ?)
  • Let S2
  • has_type(x ? ?, e2, ?)
  • Return S1 ? S2

22
Type Inference Algorithm (cont)
  • Case exp of
  • let rec x e1 in e2 --gt
  • Let ? be a fresh variable
  • Let S1 has_type(x ? ?, e1, ?)
  • Let S2 has_type(x ? ?, e2, ?)
  • Return S1 ? S2

23
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
Write a Comment
User Comments (0)
About PowerShow.com