type reconstruction & parametric polymorphism

51
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism Introduction Unification and type reconstruction Let polymorphism Explicit parametric polymorphism

Upload: olin

Post on 16-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

Type Reconstruction & Parametric Polymorphism. Introduction Unification and type reconstruction Let polymorphism Explicit parametric polymorphism. Introduction. Type reconstruction (inference) : Given e w/o type declarations, find: e’ with type declarations - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 1

Type Reconstruction & Parametric Polymorphism

Introduction Unification and type reconstructionLet polymorphismExplicit parametric polymorphism

Page 2: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 2

Introduction

Type reconstruction (inference):

Given e w/o type declarations, find: • e’ with type declarations • H for freevars(e) (= freevars(e’)) • a type s.t. Erase (e’) = e and

We then say that

(in practice, no need to compute e’)

| :H e

| :H e

Page 3: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 3

In a monomorphic system – reconstruction is problematic:

Polymorphism: allow many types for a value/variable/expression

Reconstruction makes sense in a polymorphic system

Can ask for (a finite representation of) all

?

| . 2 : int int (ok) ,but

| . : (which monomorphic type to choose?)

x x

x x

, τ such that | : τ H H e-

Page 4: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 4

Parametric polymorphism:

based on the use of type variables

A ground type: no type variables

( , , ... type variables in the language)

1

, t

:= int | bool

Type

| ..

Ty

.|

pe-

t

variabl

| τ | *...*

e

n

: . : , with intuition:

. is of every gr

F

o

or examp

und insta

l

n of ce

e x x

x x

: means (for now): for any assignment ,

from the type variables in to types, e:

e

Page 5: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 5

A type with type variables: a finite representation of a set of mono-types

parametric polymorphic type system

we can hope to find for expression e a finite representation of all the pairs s.t., τH

| : τH e-

:

the single polymorphic type

For e

xample

for . x x

Page 6: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 6

Unification and type reconstruction

we study type reconstruction using : FL with let

Type rules for type reconstruction in a poly system,

1st try: the “same” rule as the monomorphic system

Differences: • no type annotations for declared variables• Types may contain type variables

Page 7: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 7

(const) | typeO (c): fH c (var) , : | :H x x

1 1

1 1

| : ,..., | :(tuple)

| ( ,..., ) : ...n n

n n

H e H e

H e e

1 2

1 2

| : | :(applic)

| :

H e H e

H e e

1 2 3

1 2 3

| : bool | : | :(if)

| if then else :

H e H e H e

H e e e

{ : } | :( ) ( type declaration!)

| . :no

H x e

H x e

| : { : } | :(let)

| let in :

H e H x e

H x e e

Page 8: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 8

Note: we guess types for declared variables – this is how type assignments get into type environments

Q: Where in a type derivation do we guess?

A: in the root-to-leaves phase, in a lambda node

:

{ : int} | : int { : } | :

. : int int . :

Some type derivations

x x x x

x x x x

Page 9: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 9

Q: Which guess is right? best?

A: The approach of type reconstruction:

We guess the most general types, then we specialize as needed to satisfy the constraints (from the type rules), implied by the given expression

The hope: to obtain the most general typing – that represents all the typings of the expression

Q: Which guess is the most general?

: For λ . , guess : , where news A ix b x a a

Page 10: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 10

Example:

What is the type of

We guess:

then specialize to satisfy the constraint in the (applic) rule:

:x ( . ) 3?x x

{ : int} | : int| 3 : int| . : int int

| ( . ) 3 : int

x x

x x

x x

: (λ .Example ) (λ . ) x x x x

{ : } | :| 3 :| . :

| ( . ) 3 :

x x

x x

x x

Page 11: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 11

Substitutions, type constraints, unification

type substitution: a function from type variables to types

Notation:

Application of a substitution:

{ int, int}

( ) replace each by ( )

( ) - an i on fstance

: ( ) if not written explicitlyDefault otherwise

Page 12: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 12

A substitution can be applied also to

type environments, sets of types, typed expressions

inst( ) an is cean of

Page 13: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 13

Composition of substitutions:

Example:

1 2 1 2( )( ) ( ( ))

1 2

1 2 2 1

{ int, int}, { , float}

? ?

1 2 2 1

1 2 3 2 3 1

more general insta is than (or is an of )

denoted if

nc

)

e

(

3

: { int } { int int}

(What is ?)

Example

Page 14: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 14

1 2 1 2 1: iDefine f

is a , rule fails:

{

sypre-o mmetrrd

}

yr

}

e

{ } {

The equivalence classes: same substitution modulo variable renaming (actual variables used are irrelevant)

partial order is a on the equi-classes

Page 15: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 15

Type reconstruction :• Given un-typed e, we guess distinct type variables

for all free & lambda-bound variables in e, yielding:

H for freevars(e) and typed e’

The restriction of the initial guess is needed because of equality constraints (type equations) in the rules

Note: all uses of a program variable have the same type

using a substitution to instantiate the initial guess.

(or fail, if none exists)then, we find , s.t. ( ) | : ( ) H e

Page 16: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 16

Some type rules impose equality constraints(type equations) on the types used in them

Examples:

Are there constraints in other rules?

1 1 2 2 3 3

1

1 2 3

2 3 2

| : | : | : ,(if)

| if then else :

bH e H e H e ool

H e e e

1 2

1 2

| : | :(applic)

| : ττH e H e

H e e

1 2: when types for and are derived,

if is an arrow type, τ needs to be guessed.

The most general guess for

not

newτ is a type va

Note

riable

e e

Page 17: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 17

How do we solve a set of type equations?

Solving equations of expressions with variables, constants and constructors is unification

Solving one equation or several --- equivalent problems (introduce a dummy root)

Most general unifier, mgu --- more/as general as any other.

Does one exist? Is it unique?

A substitution a set C of type equations,

is a for C, if = in C, ( )= ( )

solves

unifier

Page 18: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 18

:

{ int, int} is a for ( , int)

so is { int int, int, int}

The first is an mgu (can you pr

Example

ove i

un i r

t )

f

?

i e

Page 19: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 19

The unification algorithm:

Input: a set C of type equations

Output: a substitution - a unifier (an mgu), or fail

Intuition: In each step:

One equation is resolved, resulting in:• An incremental change to substitution • Possibly addition of some equations

: base types, Notati typevan ro b t

Page 20: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 20

(empty substitution)

( ) / un / a functional version

case of : (first basis, then induction)

{}

{(

ify

unify, )} ' if then (C') els faie 1 2 1 2

{( , )} '

case:

Algorithm

l

C

C

b b C b b

t C

(the only case that contributes to subsution.)

( ')

occurs in (occur check)

_ let { }

in ( (C'

uni

fai

)

l

fy

un y

f )i

t C

t

t

Page 21: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 21

(prev. case)

( ) // cont'd

{( , )} ' ( Tvar) ( ' {( , )}

{ , } ' ({( , ),( , )} ')1 1 2 2 1 2 2 2

unify

u

{ , } ' ({( , ),( , )} ')1 1 2 2 1 2 2 2

_

nify

un

f

Algorit

ai

h

l

m

ify

unify

C

t C C t

C C

C C

Page 22: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 22

Examples: (An equation is represented as a pair of types)

(combine to one equation)

1

(I) (1) ( , int ), (2) (int , )

From (1): { int }

Applying to (2): (in ft ) a, ilint

int

int

int

1

Page 23: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 23

1

1

2

2 1

(II) (1) ( , int ), (2) (int , )

From (1): { int }

Applying to to (2): (int int , )

{ int int }

solution: { int int , int }

int

int

int

1 2

Page 24: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 24

Comments:

(iii) The algorithm assumes a freely generated domain

Where? (iv) The algorithm runs in poly time (can be made linear)

(i) Assume given {( , )} , let ={ }.

Then ( ) does not contain

if solves ( ), then

= = { }.

C

C

C

(ii) Can we compute unify(C) first, then apply ?

Page 25: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 25

Properties of the algorithm:Termination: is guaranteed (why?)

Soundness: if the algorithm succeeds, it returns a unifier (that is, if there is no unifier, it fails)

Corollary: if C has a unifier, then

(i) An mgu exists

(ii) The algorithm returns an mgu

(Which of the above may fail for a domain not freely generated?)

(induction on # of equation elimination steps)

: if C has a unifier , then the algorithm

returns s.t. (a solution at least as general as

Complete e

)

n

ss

Page 26: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 26

Now, back to a type reconstruction algorithm:

The standard type checking algorithm is modified:• Top-down: Guess fresh type variables for each

lambda-bound variable• Bottom-up:

– Guess fresh type variable for each free use

– Climb up the expression tree from the leaves, solve constraints using unification, whenever sub-trees are merged

(collecting equations and solving at the end is cheaper, but this

formulation is easier to understand – is it?)

Page 27: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 27

when ( )

otherwise

1

a versiotemporar ny !

)(( , ) returns H', s.t. ' | : or //

case of

( , ( ))

( { : }, ) ( is n

fait

ew),

( , ( ))

R ls

e

(

con

x dom H

H e H e

e

c H typeOf c

x H x

H H x

e

(these have disjoint sets of type variables for variables)

i,

1

ree

1

f

,..., )

let ( , ) ( , )

in let =unify({ ) |1 , ,

( : , : )}

in ( ( ), ( ,...

tRec

)

o

, )

nn

i i i

j

i i j j

ni n

e

H e H

i j n

x x H x H

H

Page 28: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 28

in the above, fr is

1 2

1 2

esh

tR. ' let ( { : }, ' ) = ( ', )

in ( '\ { : }, ) where : '

let ( , ) ( ' , ) 1,2

in let = ({ , ) | ( : ' ,

econ

tRecon

unify 1,2)}

{

i i i

i i

x e H x e H

H x x H

e e H e H i

x x H i

1 2

1 2

( , ), new}

in ( ( ' ' ), ( ))H H

•The application case involves guessing a range type•The other cases (if, let) are left to you•How would the algorithm look like if equations were collected to the end?

Page 29: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 29

The guesses at the top down phase are redundant:

Since we unify at nodes on the way up in the bottom up phase, no need to make a guess for lambda when going down; can make the guesses at the leaves

Here is the modified case for lambda:

The else case – when x does not occur in the body

. ' let ( , ' ) = ( ', )

in : ' ( '\ { : }, )

if then

el ( ', where isse ne

tRecon

w τ)

x e H e H

x H H x

H

Page 30: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 30

The triple | : is a for if

- | : has a proof tree

It is a if

- ( ) is freevars( ) (i.e. domai

typing

principal typing

n)

- if | : is also a typing of ,(same dom

untyped

minimal

H e e

H e

dom H e

H e e

(A prinicpal typing is as general as can be. It is , up to variable renamuniq ing)ue

ain for ',

then s.t. , ( , )

H

H H

When e is closed H is empty, we have a principal type

Page 31: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 31

Proposition:

The algorithm tRecon• Always terminates• Fails iff there is no typing• If it returns a typing, then it is principal• Works in poly time (can be implemented in linear

time)

All is well that ends well

But, is it really the end?

Page 32: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 32

Two ideas are used in type reconstruction:• Type constraints are local: each originates in the

merging of sub-trees at a proof tree node• The solution of each constraint applies globally:

all occurrences of a type variable are substituted

We examine the necessity and consequences of these via the concept of polymorphic function

These are the main reason we are in this business !

Page 33: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 33

Polymorphic functions:

The input type represents the intersection of the sets of (poly)types that are compatible with the operations on the parameter in the body

(I.e. the set of types that are compatible with all these operations)

If contains type vars – every instantiation is ok for the body

if a function f has type , then is derived from

constraints in type-chekimg the bo

construct use

dy of f:

every applied to a of the parameter in the

body must be ok f r

o

Page 34: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 34

:

. has t

Exam

ype

ple

s

x x

. applies to , has type list x hd x hd x

. ( ) 3 has type int list intx hd x

.if ( true) then ( 3) else 5

applies to and to , so must have types

bool and int

this expression is type-able

true

t

3

no

f f f

f f

Page 35: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 35

A consequence:

We have polymorphic types, how nice!

BUT

We cannot use polymorphic values polymorphically

I.e., use the same value in the same program/expression, in different places, that require different types

Thus, we cannot use the function polymorphicallyThe argument applies to all scenarios using polymorphic functions

Consider

( . ( true) ( 3) 5) .

or . ( true) ( 3) 5

both expressions cannot be typed!

if then else

let in if then else

f f f x x

f x x f f

.x x

Page 36: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 36

The point:

In the bottom-up phase, when sub-trees are merged, their environments are unioned, and types for same variables forced to be equal

a function value cannot have conflicting types such as

differently typed incompatible uses of a function parameter lead to a failure! (and same for let)

What shall we do now?

int and bool

Page 37: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 37

when the function is applied, f may be replaced by any instance of its inferred type

If we decide it is then both

Each creates a type error in the bodyf’s param type represents a set of mono types ;

each must be compatible with all uses of param

:

( .if ( true) t

Consider again

hen ( 3) else 5)f f f

. 5 and . are legal argumentsx x x x

Let polymorphism (due to Robin Milner):

Page 38: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 38

f in the let is associated now with a value that is known to be a poly function that can be applied to either a bool or an int

If we type it by no type error will occur in the evaluation of the body

We would like the type for f to mean that its actual value is indeed a poly value of this type, not of an instance type -- hence not to instantiate it by all the constraints in the body together

:

let . in if

But, compare

( true) then ( 3) else 5

to

f x x f f

Page 39: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 39

Now, we need to use type variables in two different meanings, as

• “Mono-types” -- all uses are same type• Poly-types different uses are unrelated

If we have notation for that, we can use let for real polymorphism

Page 40: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 40

Notation:

Note that U2 contains U1

A free type var – a mono type var

A bound type var – a poly type var

(type var)

type scheme poly-type

(mono) types (universe U1)

: int | bool | ... | t

, also called (universe

| .

U2)

: t

. -- is of type ( ) = [ ' /t] for eac ={t '} h t

Page 41: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 41

New type rules (poly types in red)

First part: (if and tuple are like applic –omitted)

A problem: How can we generate/use poly-types?

(var) , : : | H x x

1 1 2 2 1

1 2 2

| : | :(applic)

| :H e H e

H e e

1 2

1 2

{ : } | :( )

| . :H x e

H x e

| : { : } | :(let)

| let in :H e H x e

H x e e

Page 42: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 42

Second part:

| :(gen) ( n does occur free in )

| :t

.o

H et H

H e t

| : .(inst)

| : [ / ]

H e t

H e t

First rule allows to generate a polytype for a variable defined in a letSecond rule allows to use it in the body

In algorithm, 2nd rule used thus: replace t by a fresh type var

Page 43: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 43

Reconsider:

let . in if ( true) then ( 3) else 5f x x f f

: reconstruct the typing | for .

: generalize to .

: reconstruct a type for the body,

g

1st s

iven { : . }

instantiate typ

tep

2nd step

3rd step

e of 1

x x

f

st to

instantiate type of 2nd to

infer type int for the full expression

f

f

Page 44: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 44

. .( , ) if #2 ( ) then l #et 2 (3) elsn ei 5x f y x y f x f

: reconstruct { : } | .( , ) :Step 1

Step

.

reconstruct body under2: { : . }

x y x y

f

Type reconstruction succeeds: bool int

: consider ( ) : { : , : }

: consider #2 f as a test in = =bool

(local for bu

Step 3

Step 4 if

S

t global for )

: consider #2 (3) { : }

tep 5

f x x f

f f

=int (local)

Page 45: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 45

What would happen in previous example, the second branch was #1 f(x) (rather than 5)?

An issue: how to ensure that the type of x is the same across the expression?

: consider #1 ( ) : { : , : }

Now, since the 2st branch gave int, =int

and type-checkimg succeeds with bool int

But, it should

Step

fa

6

!il

f x x f

Page 46: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 46

Solution:• When the type for f in

is reconstructed, return for f both type and type environment

This, in particular, tell us that is mono and keeps it as the type of x (when H is empty, this reduces to previous case)

• Replace input H in tRecon by A --- a set of such bindings; change the algorithm to deal with such A’s

.(let , ) f y x y

{ :{( : }, )}f x in

Page 47: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 47

(if c is poly, convert its type to mono)

(here we use A!)

)(( , ) returns H', s.t. ' | : or case of

( , ( ))

if A(x) = ( , τ)

then ( , τ

tReco fails

)

n e H eA

e

c typeOf c

x H

H

when ( )

(see applicatio

with variables in τ, not in H renamed to fresh

else ({ : }, ) ( new),

tuple , if ... treated essentially as before

x dom Ax

n)

1 2

1 2

1 2

1 2

let (A, ) ( ' , ) 1, 2

in let = ({ , ) | ( : ' , 1, 2)}

{(

tR

, ), new}

in

ec

( ( ' ' ), ( )

on

n

)

u ify

i i i

i i

e e e H i

x x H i

H H

Page 48: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 48

. ' let (A, ' ) = ( ', )

in : ' ( '\ { : }, )

if then

else ( ', where is new

tRecon

τ)

x e e H

x H H x

H

( ,let ' in let* (A, ') τ)

A' = A { : ( , τ)

tRecon

tRecon (A', )=( ', )

in ( ', )

Hx e e e

x H

e H

H

m

m

== Þ

U

Page 49: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 49

. .( , ) if #2 ( ) then #2 (3)

let in

else #1 ( )

x f y x y f x f

f x

Step 1

Step

: reconstruct { : } | .( , ) :

reconstruct body under 2: { : ({ : ), )}

x y x y

f x

Type reconstruction fails

: consider ( ) :

for we obtain ({x: }, )

for we obtain ({x: }, )

for the application we obtain

Step 3

and so on

f x

f

x

Page 50: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 50

Explicit parametric polymorphism

what is the explicit parametric type system of ML?(if programmer did declare type for variables, how would these look

like?)

There is more than one answer!

All contain:• functions with type arguments:• Application to types

λ . eaτe

Page 51: Type Reconstruction & Parametric Polymorphism

Catriel Beeri Pls/Winter 2004/5 type reconstruction 51

Example:

let double = λ .λ : .λ : . ( );;

(double int) (λ : int . 1) 3 * 5

(double bool) (λ : bool. ) true * true

f x f f x

x x

x x

a a a a®

+ ®

Ø ®

end