type inference ii

30
Type Inference II David Walker COS 441

Upload: heather-mendoza

Post on 02-Jan-2016

34 views

Category:

Documents


3 download

DESCRIPTION

Type Inference II. David Walker COS 441. Type Inference. Goal: Given unannotated program, find its type or report it does not type check Overview: generate type constraints (equations) from unannotated programs solve equations. Constraint Generation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Type Inference II

Type Inference II

David Walker

COS 441

Page 2: Type Inference II

Type Inference

Goal: Given unannotated program, find its type or

report it does not type check

Overview:generate type constraints (equations) from

unannotated programssolve equations

Page 3: Type Inference II

Constraint Generation

Typing rules for annotated programs:G |-- e : t

Note: given G and e, at most one type t

Typing rules for unannotated programs:G |-- u => e : t, q

Note: given G and u, there may be many possible types for u; the many possibilities represented using type

schemesu has a type t if q has a solution

Remember: fun f (x) = f x

Page 4: Type Inference II

Rule comparison

G |-- u1 ==> e1 : t1, q1G |-- u2 ==> e2 : t2, q2 G |-- u3 ==> e3 : t3, q3-------------------------------------------------------------------G |-- if u1 then u2 else u3 ==> if e1 then e2 else e3

: a, q1 U q2 U q3 U {t1 = bool, a = t2, a = t3}

G |-- e1 : bool G |-- e2 : t G |-- e3 : t----------------------------------------------------------------G |-- if e1 then e2 else e3 : t

implicit equalityeasy to implementbecause t can’tcontain variables

equality harder to implementbecause t2, t3 cancontain variablesthat may be furtherconstrained elsewhere

Page 5: Type Inference II

Non-local Constraints

fun f (x) = fun g (y) = (if true then x else y, x + y)

fun f (x) = fun g (y) = (if true then x else y, x + (if y then 3 else 4))

Does this type check?

fun f (x) = fun g (y) = (if true then x else y, ...)

It depends:

Page 6: Type Inference II

Non-local Constraints

fun f (x:int) = fun g (y:int) = (if true then x else y, x + y)

fun f (x:int) = fun g (y:bool) = (if true then x else y, x + (if y then 3 else 4))

But remember, it was easy to checkwhen types were declared in advance:

Page 7: Type Inference II

Solving Constraints

A solution to a system of type constraints is a substitution S

S |= q iff applying S makes left- & right-hand sides

of each equation equal

A solution S is better than T if it is “more general” intuition: S makes fewer or less-defined substitutions (leaves more

variables alone) T <= S if and only if T = U o S for some U

Page 8: Type Inference II

Most General Solutions

S is the principal (most general) solution of a constraint q ifS |= q (it is a solution) if T |= q then T <= S (it is the most general one)

Lemma: If q has a solution, then it has a most general one

We care about principal solutions since they will give us the most general types for terms

Page 9: Type Inference II

principal solutions

principal solutions give rise to most general reconstruction of typing information for a term: fun f(x:a):a = x

is a most general reconstruction

fun f(x:int):int = x is not

Page 10: Type Inference II

Unification

Unification: An algorithm that provides the principal solution to a set of constraints (if one exists) If one exists, it will be principal

Page 11: Type Inference II

Unification

Unification: Unification systematically simplifies a set of constraints, yielding a substitutionduring simplification, we maintain (S,q)

S is the solution so farq are the constraints left to simplifyStarting state of unification process: (I,q)Final state of unification process: (S, { })

identitysubstitutionis mostgeneral

Page 12: Type Inference II

Unification Machine

We can specify unification as a transition system: (S,q) -> (S’,q’)

Base types & simple variables:

--------------------------------(S,{int=int} U q) -> (S, q)

------------------------------------(S,{bool=bool} U q) -> (S, q)

-----------------------------(S,{a=a} U q) -> (S, q)

Page 13: Type Inference II

Unification Machine

Functions:

Variable definitions

----------------------------------------------(S,{s11 -> s12= s21 -> s22} U q) -> (S, {s11 = s21, s12 = s22} U q)

--------------------------------------------- (a not in FV(s))(S,{a=s} U q) -> ([s/a] o S, [s/a]q)

-------------------------------------------- (a not in FV(s))(S,{s=a} U q) -> ([s/a] o S, [s/a]q)

Page 14: Type Inference II

Occurs Check

What is the solution to {a = a -> a}?

Page 15: Type Inference II

Occurs Check

What is the solution to {a = a -> a}?There is none! The “occurs check” detects this situation

-------------------------------------------- (a not in FV(s))(S,{s=a} U q) -> ([a=s] o S, [s/a]q)

occurs check

Page 16: Type Inference II

Irreducible States

Recall: final states have the form (S, { })

Stuck states (S,q) are such that every equation in q has the form: int = bools1 -> s2 = s (s not function type)a = s (s contains a)or is symmetric to one of the above

Stuck states arise when constraints are unsolvable

Page 17: Type Inference II

Termination

We want unification to terminate (to give us a type reconstruction algorithm)

In other words, we want to show that there is no infinite sequence of states (S1,q1) -> (S2,q2) -> ...

Page 18: Type Inference II

TerminationWe associate an ordering with constraintsq < q’ if and only if

q contains fewer variables than q’q contains the same number of variables as q’ but

fewer type constructors (ie: fewer occurrences of int, bool, or “->”)

This is a lexicographic orderingThere is no infinite decreasing sequence of

constraintsTo prove termination, we must demonstrate that

every step of the algorithm reduces the size of q according to this ordering

Page 19: Type Inference II

Termination

Lemma: Every step reduces the size of qProof: By cases (ie: induction) on the definition

of the reduction relation.

--------------------------------(S,{int=int} U q) -> (S, q)

------------------------------------(S,{bool=bool} U q) -> (S, q)

-----------------------------(S,{a=a} U q) -> (S, q)

----------------------------------------------(S,{s11 -> s12= s21 -> s22} U q) -> (S, {s11 = s21, s12 = s22} U q)

------------------------ (a not in FV(s))(S,{a=s} U q) -> ([s/a] o S, [s/a]q)

Page 20: Type Inference II

Correctnesswe know the algorithm terminates

we want to prove that a series of steps:

(I,q1) -> (S2,q2) -> (S3,q3) -> ... -> (S,{})

solves the initial constraints q1

we’ll do that by induction on the length of the sequence, but we’ll need to define the invariants that are preserved from step to step

Page 21: Type Inference II

Defining the invariants

A complete solution for (S,q) is a substitution T such that1. T <= S

2. T |= q

Intuition: T extends S and solves q

A principal solution T for (S,q) is complete for (S,q) and3. for all T’ such that 1. and 2. hold, T’ <= T

Intuition: T is the most general solution (it’s the least restrictive)

Page 22: Type Inference II

Properties of Solutions

Lemma 1:Every final state (S, { }) has a complete

solution. It is S since:

S <= SS |= { }

every substitution is a solutionto the empty set of constraints

Page 23: Type Inference II

Properties of Solutions

Lemma 2No stuck state has a complete solution (or

any solution at all) it is impossible for a substitution to make the

necessary equations equal int bool int t1 -> t2 ...

Page 24: Type Inference II

Properties of Solutions

If (S,q) -> (S’,q’) then 1. T is complete for (S,q) iff T is complete for (S’,q’)

2. T is principal for (S,q) iff T is principal for (S’,q’)

Proof: By induction (cases) on the definition of (S,q) -> (S’, q’)

in the forward direction, this is the preservation theorem for the unification machine!

Page 25: Type Inference II

Proof cases

If T is complete for (S,{int=int} U q) then (by definition):(1) T <= S and (2) T |= {int=int} U q

To prove T is complete for (S,q), we show:T <= S (by (1))T |= q (by (2) and definition of |= )

--------------------------------(S,{int=int} U q) -> (S, q)

Page 26: Type Inference II

Proof cases

If T is complete for (S, {a=s} U q) then (by definition):(1) T <= S (2) T |= {a=s} U q

To prove T is complete for ([a=s] o S, q[s/a]), we show:T <= [s/a] o S T |= q (by (2) and definition of |= )

How? Maybe for homework!

---------------------------------------------- (a not in FV(s))(S,{a=s} U q) -> ([s/a] o S, [s/a]q)

Page 27: Type Inference II

Summary: Unification

By termination, (I,q) ->* (S,q’) where (S,q’) is irreducible. Moreover: If q’ = { } then

S is principal for (S,{}) (by lemma 1) S is principal for (I,q) (by lemma 3) Since S is principal for (I,q), S is more general than

any other solution T for q such that T <= I. Since all T <= I, T <= S for all solutions to q. Hence, S is a principal solution for q.

Page 28: Type Inference II

Summary: Unification (cont.)

... Moreover: If q’ is not { } (and (I,q) ->* (S,q’) where

(S,q’) is irreducible) then (S,q) is stuck. Consequently, (S,q) has no

complete solution. By lemma 3, even (I,q) has no complete solution and therefore q has no solution at all.

Page 29: Type Inference II

Summary: Type Inference

Type inference algorithm.Given a context G, and untyped term u:

Find e, t, q such that G |- u ==> e : t, qFind principal solution S of q via unification

if no solution exists, there is no reconstruction

Apply S to e, ie our solution is S(e) S(e) contains schematic type variables a,b,c, etc that

may be instantiated with any type

Since S is principal, S(e) characterizes all reconstructions.

Page 30: Type Inference II

End