24
Lesson 10 Type Reconstruction 2/26 Chapter 22

Lesson 10 Type Reconstruction

Embed Size (px)

DESCRIPTION

Lesson 10 Type Reconstruction. 2/26 Chapter 22. Type Reconstruction. substitutions typing with constraint sets (type equations) unification: solving constraint sets principlal types let polymorphism. Type substitutions. Language:   [Bool, Nat] with type variables - PowerPoint PPT Presentation

Citation preview

Lesson 10Type Reconstruction

2/26Chapter 22

Lesson 10: Type Reconstruction 2

Type Reconstruction

• substitutions• typing with constraint sets (type equations)• unification: solving constraint sets• principlal types• let polymorphism

Lesson 10: Type Reconstruction 3

Type substitutions

Language: [Bool, Nat] with type variables

A type substitution is a finite mapping from type variablesto types.

= [X => Nat -> Nat, Y => Bool, Z => X -> Nat]

Type substitutions can be applied to types: T

(X -> Z) = (Nat -> Nat) -> (X -> Nat)

This extends pointwise to contexts:

Lesson 10: Type Reconstruction 4

Type substitutions

Language: [Bool, Nat] with type variables

A type substitution is a finite mapping from type variablesto types.

= [X => Nat -> Nat, Y => Bool, Z => X -> Nat]

Type substitutions can be applied to types: T

(X -> Z) = (Nat -> Nat) -> (X -> Nat)

This extends pointwise to contexts:

Composition of substitutions o (X) = ( X) if X dom = X otherwise

Lesson 10: Type Reconstruction 5

Substitutions and typing

Thm: If |- t: T, then |- t: T for any type subst. .

Prf: induction on type derivation for |- t: T.

Lesson 10: Type Reconstruction 6

"Solving" typing problems

Given and t, we can ask:

1. For every , does there exist a T s.t. |- t: T?

2. Does there exist a and a T s.t. |- t: T?

Question 1 leads to polymorphism, where T = T' and |- t: T'. The type variables are "quantified".

Question 2 is the basis for type reconstuction: we thinkof the type variables as unknowns to be solved for.

Defn: A solution for (,t) is a pair (, T) s.t. |- t: T.

Lesson 10: Type Reconstruction 7

Example: solutions of a typing problem

(, x:X. y:Y. z:Z . (x z) (y z)) has solutions

[X => Nat -> Bool -> Nat, Y => Nat -> Bool, Z => Nat]

[X => X1 -> X2 -> X3, Y => X1 -> X2, Z => X1]

Lesson 10: Type Reconstruction 8

Constraints

A constraint set C is a set of equations between types.C = {Si = Ti | i 1,..,n}.

A substitution unifies (or satisfies) a constraint set Cif Si = Ti for every equation Si = Ti in C.

A constraint typing relation |- t: T | C where is aset of "fresh" type variables used in the constraint set C.This relation (or judgement) is defined by a set of inferencerules.

Lesson 10: Type Reconstruction 9

Constraint inference rules

|- t1: T1 | C1 1 |- t2: T2 | C2 2

1 2 = 1 FV(T2) = 2 FV(T1) = X 1, 2, t1, t2, T1, T2, C1, C2, C = C1 C2 {T1 = T2 -> X}

= 1 2 {X}

|- t1 t2: X | C

Inference rule for application

Lesson 10: Type Reconstruction 10

Constraint solutions

Defn: Suppose |- t: S | C . A solution for (, t, S, C)is a pair (, T) s.t. satisfies C and T = S.

Thm: [Soundness of Constraint Typing]Suppose |- t: S | C X. If (, T) is a solution for (, t, S, C)then it is also a solution for (, t), i.e. |- t: T.

Thm: [Completeness of Constraint Typing]Suppose |- t: S | C . If (, T) is a solution for (, t) thenthere is a solution (', T) for (, t, S, C) s.t. '\ = .

Cor: Suppose |- t: S | C . There is a soln for (, t) iffthere is a solution for (, t, S, C).

Lesson 10: Type Reconstruction 11

Unification

Defn: < ' if ' = o for some .

Defn: A principle unifier (most general unifier) for a constraintset C is a substitution that satisfies C s.t. < ' for any other' that satifies C.

Lesson 10: Type Reconstruction 12

Unification algorithm

unify C =if C = then [ ]else let {S = T} C' = C in

if S = T then unify(C')else if S = X and X FV(T) then unify([X => T]C') o [X => T]else if T = X and X FV(S) then unify([X => S]C') o [X => S]else if S = S1 -> S2 and T = T1 -> T2

then unify(C' {S1 = T1, S2 = T2})else fail

Thm: unify always terminates, and either fails or returns the principal unifier if a unifier exists.

Lesson 10: Type Reconstruction 13

Principal Types

Defn: A principal solution for (, t, S, C) is a solution (, T)s.t. for any other solution (', T') we gave < '.

Thm: [Principal Types]If (, t, S, C) has a solution, then it has a principal one. Theunify algorithm can be used to determine whether (, t, S, C)has a solution, and if so it calculates a principal one.

Lesson 10: Type Reconstruction 14

Implicit Annotations

We can extend the syntax to allow lambda abstractionswithout type annotations: x.t.

The corresponding type constraint rule supplies a fresh typevariable as an implicit annotation.

X , x: X |- t1: T | C

, |- x: X. t1: X -> T | C ( {X})(CT-AbsInf)

Lesson 10: Type Reconstruction 15

Let Polymorphism

let double = f: Nat -> Nat. x: Nat. f(f x)in double (x: Nat. succ x) 2

let double = f: Bool -> Bool. x: Bool. f(f x)in double (x: not x) false

An attempt at a generic double:

let double = f: X -> X. x: X. f(f x)in let a = double (x: Nat. succ x) 2in let b = double (x: not x) false

==> X -> X = Nat -> Nat = Bool -> Bool

Lesson 10: Type Reconstruction 16

Macro-like let rule

let double = f: X -> X. x: X. f(f x)in let a = double (x: Nat. succ x) 2in let b = double (x: not x) false

could be typed as:

let a = (f: X -> X. x: X. f(f x)) (x: Nat. succ x) 2in let b = (f: X' -> X'. x: X'. f(f x)) (x: not x) false

or, using implicit type annotations:

let a = (f. x. f(f x)) (x: Nat. succ x) 2in let b = (f. x. f(f x)) (x: not x) false

Lesson 10: Type Reconstruction 17

Macro-like let rule

|- [x => t1]t2: T2 |- t1: T1

|- let x = t1 in t2: T2

(T-LetPoly)

The substitution can create multiple independent copiesof t1, each of which can be typed independently (assumingimplicit annotations, which introduce separate type variablesfor each copy).

Lesson 10: Type Reconstruction 18

Type schemes

Add quantified type schemes:

T ::= X | Bool | Nat | T -> T P ::= T | X . P

Contexts become finite mappings from term variables to typeschemes:

::= | , x : P

Examples of type schemes:

Nat, X -> Nat, X. X -> Nat, X.Y. X -> Y -> X

Lesson 10: Type Reconstruction 19

let-polymorphism rules

, x : .T1 |- t2: T2 |- t1: T1

|- let x = t1 in t2: T2

(T-LetPoly)

, x : .T |- x: [ => ']T

where ' is a set of fresh type variables

where ' are the type variables free in T1

but not free in

(T-PolyInst)

Lesson 10: Type Reconstruction 20

let-polymorphism example

let double = f. x. f(f x)in let a = double (x: Nat. succ x) 2in let b = double (x: not x) falsein (a,b)

double : X. (X -> X) -> X -> X

(Y -> Y) -> Y -> Y

(Z -> Z) -> Z -> Z

Then unification yields [Y => Nat, Z => Bool].

Lesson 10: Type Reconstruction 21

let-polymorphism and references

Let ref, !, and := be polymorphic functions with types

ref : X. X -> Ref(X)! : X. Ref(X) -> X:= : X. Ref(X) *X -> Unit

let r = ref(x. x)in let a = r := (x: Nat. succ x)in let b = !r falsein ()

r : X. Ref(X -> X)

Ref(Bool -> Bool)

Ref(Nat -> Nat)

We've managed to apply (x: Nat. succ x) to false!

Lesson 10: Type Reconstruction 22

The value restriction

We correct this unsoundness by only allowing polymorphicgeneralization at let declarations if the expression is avalue. This is called the value restriction.

let r = ref(x. x)in let a = r := (x: Nat. succ x)in let b = !r falsein ()

Ref(Nat -> Nat)

Ref(Nat -> Nat) [X => Nat]

Now we get a type error in " !r false ".

r : Ref(X -> X)

Lesson 10: Type Reconstruction 23

Let polymorphism with recursive values

Another problem comes when we add recursive valuedefinitions.

let rec f = x. t in ...

is typed as though it were written

let f = fix(f. x. t) in ...

where fix : X. (X -> X) -> Xexcept that the type of the outer f can be generalized.

Note that the inner f is -bound, not let bound, so it cannotbe polymorphic within the body t.

Lesson 10: Type Reconstruction 24

Polymorphic Recursion

What can we do about recursive function definitions wherethe function is polymorphic and is used polymorphically inthe body of it's definition? (This is called polymorphicrecursion.)

let rec f = x. (f true; f 3; x)

Have to use a fancier form of type reconstruction: theiterative Mycroft-Milner algorithm.