CS 611 Advanced Programming Languages Andrew Myers Cornell

  • Slides: 15
Download presentation
CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2

CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05 Cornell University CS 611 Fall'04 - Andrew Myers

Type inference (reconstruction) Simple typed language: e : : = x | b |

Type inference (reconstruction) Simple typed language: e : : = x | b | lx: t. e | e 1 e 2 | e 1 + e 2 | if e 0 then e 1 else e 2 | let x=e 1 in e 2 | rec y: t 1 t 2. (lx. e) t : : = unit | bool | int | t 1 t 2 • Question: Do we really need to write type declarations? e : : = … | lx. e | …| rec y. (lx. e) Cornell University CS 611 Fall'04 -- Andrew Myers 2

Typing rules e : : = x | b | lx. e | e

Typing rules e : : = x | b | lx. e | e 1 e 2 | e 1 e 2 | if e 0 then e 1 else e 2 | let x=e 1 in e 2| rec y. lx. e G, x: t e : t G lx. e : t t Problem: how does type checker construct proof? G, y: t t , x: t e : t G rec y. lx. e : t t Guess t, t ? Cornell University CS 611 Fall'04 -- Andrew Myers 3

Example let square = lz. z*z in (lf. lx. ly. if (f x y)

Example let square = lz. z*z in (lf. lx. ly. if (f x y) then (f (square x) y) else (f x y))) What is the type of this program? Cornell University CS 611 Fall'04 -- Andrew Myers 4

Manual type inference let square = lz. z*z in (lf. lx. ly. if (f

Manual type inference let square = lz. z*z in (lf. lx. ly. if (f x y) then (f (square x) y) else (f x y))) z : int s, square : int f : tx ty bool y : ty =bool x : tx = int Answer: (int bool) int bool Cornell University CS 611 Fall'04 -- Andrew Myers 5

Type inference • Goal: reconstruct types even after erasure • Idea: run ordinary type-checking

Type inference • Goal: reconstruct types even after erasure • Idea: run ordinary type-checking algorithm, generate type equations on type variables f: T 2, x: T 5 f : int T 6 f: T 2, x: T 5 1 : int f: T 2, x: T 5 f 1 : T 6 f: T 2 lx. f 1 : T 1 (=T 5 T 6) lf. lx. f 1 : T 2 T 1 (T 3=T 4) y: T 3 y : T 4 (ly. y) : T 2 (T 2=T 3 T 4) (lf. lx. (f 1)) (ly. y) : T 1 T 2 = T 3 T 4, T 3 = T 4, T 1 =T 5 T 6, T 2 = int T 6 Cornell University CS 611 Fall'04 -- Andrew Myers 6

Typing rules G n : int x dom(G) G x : G(x) G, x:

Typing rules G n : int x dom(G) G x : G(x) G, x: Tx e : t G lx. e : Tx t G e 0 : t 0 G e 1 : t 1 G e 2 : t 2 t 1=t 2, t 0 = bool G if e 0 then e 1 else e 2: t 1 G e 1 : t 1 G, x : t 1 e 2 : t 2 G let x =e 1 in e 2 : t 2 G, x: T 1 , y: T 1 T 2 e : t t=T 2 G rec y. lx. e : T 1 T 2 G e 0 : t 0 G e 1 : t 1 t 0 = t 1 T 2 Only type metavariables G e 0 e 1 : T 2 on RHS of premises Cornell University CS 611 Fall'04 -- Andrew Myers 7

Unification • How to solve equations? • Idea: given equation t 1 = t

Unification • How to solve equations? • Idea: given equation t 1 = t 2, unify type expressions to solve for variables in both • Example: T 1 int = (bool T 2) T 3 • Result: substitution T 1 bool T 2, T 3 int T 1 int = bool T 3 T 2 Cornell University CS 611 Fall'04 -- Andrew Myers 8

Robinson’s algorithm (1965) • Unification produces weakest substitution that equates two trees – T

Robinson’s algorithm (1965) • Unification produces weakest substitution that equates two trees – T 1 int = (bool T 2) T 3 equated by any T 1 bool T 2, T 3 int, T 2 t – Defn. S 1 is weaker than S 2 if S 2 = S 3 S 1 for S 3 a non-trivial substitution • Unify(E) where E is set of equations gives weakest equating substitution: define recursively Unify(T = t, E) = Unify(E{t/T}) [T t] (if T FTV(t)) Cornell University CS 611 Fall'04 -- Andrew Myers 9

Rest of algorithm Unify(T = t, E) = Unify(E{t/T}) [T t] (if T FTV

Rest of algorithm Unify(T = t, E) = Unify(E{t/T}) [T t] (if T FTV t ) Unify( ) = Unify(B = B, E) = Unify(E) Unify(B 1 = B 2, E) = ? Unify(T = T, E) = Unify(E) Unify(t 1 t 2=t 3 t 4, E) = Unify(t 1=t 3, t 2=t 4, E) Well-founded? Degree = (#vars, size(eqns)) Cornell University CS 611 Fall'04 -- Andrew Myers 10

Type inference algorithm • R(e, G, S) = t, S means “Reconstructing the type

Type inference algorithm • R(e, G, S) = t, S means “Reconstructing the type of e in typing context G with respect to substitution S yields type t, identical or stronger substitution S or “S is weakest substitution no weaker than S such that S (G) e : S (t)” Define: Unify(E, S) = Unify(SE) S – solve substituted equations E and fold in new substitutions Cornell University CS 611 Fall'04 -- Andrew Myers 11

Inductive defn of inference • R(e, G, S) = t, S “S is weakest

Inductive defn of inference • R(e, G, S) = t, S “S is weakest substitution stronger than (or same as) S such that S (G) e : S (t)” • Unify(E, S) = Unify(SE) S R(n, G, S) = int, S R(true, G, S) = bool, S R(x, G, S) = G(x), S R(e 1 e 2, G, S) = let T 1, S 1 = R(e 1, G, S) in let T 2, S 2 = R(e 2, G, S 1) in Tf, Unify(T 1=T 2 Tf, S 2) R(lx. e, G, S) = let T 1, S 1 = R(e, G[x Tf], S) in S 1 Cornell University CS 611 T Fall'04 f -- T 1, Andrew Myers 12 where T is “fresh” (not mentioned anywhere in e, G, S)

Example R((lx. x) 1, , ) = let T 1, S 1 = R(lx.

Example R((lx. x) 1, , ) = let T 1, S 1 = R(lx. x, , ) in let T 2, S 2 = R(1, , S 1) in T 3, Unify(T 1 T 3 = T 2, S 2) R(lx. x, , ) = let T 1, S 1 = R(x, G[x T 4], ) in T 4 T 1, S 1 = T 4, = let T 2, S 2 = R(1, , ) in T 3, Unify(T 2 T 3 = T 4 T 4, ) = T 3, Unify(int=T 4, T 3 = T 4, ) = T 3, Unify(T 3 = int, [T 4 int]) = T 3, [T 3 int, T 4 int]) Cornell University CS 611 Fall'04 -- Andrew Myers 13

Implementation • Can implement with imperative update: datatype = Int | Bool | Arrow

Implementation • Can implement with imperative update: datatype = Int | Bool | Arrow of type * type | Type. Var of type option ref fun fresh. Type. Var() = Type. Var(ref NONE) fun resolve(t: type) = case t of Type. Var(ref (SOME t’)) => t’ | _ => t fun unify(t 1: type, t 2: type) : unit = case (resolve t 1, resolve t 2) of (Type. Var(r as ref NONE), t 2) => r : = t 2 | (t 1, Type. Var(r as ref NONE)) => r : = t 1 | (Arrow(t 1, t 2), Arrow(t 3, t 4)) => unify(t 1, t 3); unify(t 2, t 4) | (Int, Int) => () | (Bool, Bool) => () | _ => raise Fail “Can’t unify types” Cornell University CS 611 Fall'04 -- Andrew Myers 14

Polymorphism R(lx. x, , ) = let T 1, S 1 = R(x, G[x

Polymorphism R(lx. x, , ) = let T 1, S 1 = R(x, G[x T 4], ) in T 4 T 1, S 1 = T 4, • Reconstruction algorithm doesn’t solve type fully… opportunity! • lx. x can have type T 4 for any T 4 – polymorphic (= “many shape”) term – Could reuse same expression multiple places in program, with different types: let id = (lx. x) in … (f id) … (g x id) … id Cornell University CS 611 Fall'04 -- Andrew Myers 15