Docsity

Lecture Slides on Type Inference | CMPS 290, Study notes of Computer Graphics

Partial preview of the text

Download Lecture Slides on Type Inference | CMPS 290 and more Study notes Computer Graphics in PDF only on Docsity!

Type Inference^ Lecture 3a

13 Jan 2004^

290G - Lecture 3a

Review of last class ...^ 13 Jan 2004

290G - Lecture 3a

-^ Programming language •^ Semantics

(λx:

τ.e) e’^ →^ β

e[x^ ←^ e’]

-^ Type language •^ Some programs have types^ –^ (according to type rules) •^ Soundness Theorem:

Evaluation preserves types

-^ If A^ d^ e:

τ^ ande^ →^

∗d, then Aβ^

d^ d:τ

-^ Untypable program: 3 + (

|^ :^.^ |^ λx:τ.e)

|^ |^

|if

e^ x^

x^ e^

e^ e^ i^

e^ e^

e e e

λ^ τ=

|^

|int

τ^ α^

τ^ τ= →^ A^ d^

e:τ

Type Inference^ 13 Jan 2004

290G - Lecture 3a

  • Thetype erasure of

e ise with all type

information removed^ – the untyped term • Is an untyped term the erasure of somesimply typed term?^ – And what are the types? • This is a

type inference problem.

  • We must infer, rather than check, the types.

Outline^ 13 Jan 2004

290G - Lecture 3a

  • We develop the inference algorithm in steps:^ – recast the type rules in an equivalent form^ – show typing in the new rules reduces to aconstraint satisfaction problem^ – show the constraint problem is solvable^ • In this case, via term unification. • We will use this outline again.

New Rules^ 13 Jan 2004

290G - Lecture 3a

1 1 2 2 1 2 1 2 1 1 1 1

2 2 2 2

3 3 1 2

1 2

3 1 2

: : 1 2 3 2

(^ )^

,^ :^ : :.^ :^

: : :^

: :^

: int^

int i : int^

: int^

if^ : x

x x

A^ x e A e

A x

A xe A^ x

A^ x e

A^ e^ e A^ e A^ e

A^ e A^ e

A^ e AA

e^ e

A^ e^ e^ τ^ τ τ τ β e α

α^ τ αλ^

α^ τ

β τ τ

τ τ

τ τ^ τ

ττ^ =^ → τ^ τ =

→ = = =^

=

d d d d^

d^

d d d^

d d^

d d^ d

d

-^ Sidestep the problems by introducing explicit unknownsand constraints

New Rules^ 13 Jan 2004

290G - Lecture 3a

1 1 2 2 1 2 1 2 1 1 1 1

2 2 2 2

3 3 1 2

1 2

3 1 2

: : 1 2 3 2

(^ )^

,^ :^ : :.^ :^

: : :^

: :^

: int^

int i : int^

: int^

if^ : x

x x

A^ x e A e

A x

A xe A^ x

A^ x e

A^ e^ e A^ e A^ e

A^ e A^ e

A^ e AA

e^ e

A^ e^ e^ τ^ τ τ τ β e α

α^ τ αλ^

α^ τ

β τ τ

τ τ

τ τ^ τ

ττ^ =^ → τ^ τ =

→ = = =^

=

d d d d^

d^

d d d^

d d^

d d^ d

d

-^ Type assumption for variable x is a fresh variable

αx

New Rules^ 13 Jan 2004

290G - Lecture 3a

1 1 2 2 1 2 1 2 1 1 1 1

2 2 2 2

3 3 1 2

1 2

3 1 2

: : 1 2 3 2

(^ )^

,^ :^ : :.^ :^

: : :^

: :^

: int^

int i : int^

: int^

if^ : x

x x

A^ x e A e

A x

A xe A^ x

A^ x e

A^ e^ e A^ e A^ e

A^ e A^ e

A^ e AA

e^ e

A^ e^ e^ τ^ τ τ τ β e α

α^ τ αλ^

α^ τ

β τ τ

τ τ

τ τ^ τ

ττ^ =^ → τ^ τ =

→ = = =^

=

d d d d^

d^

d d d^

d d^

d d^ d

d

-^ Hypotheses are all arbitrary^ –^ Can always complete a derivation, pending constraint resolution

Notes^ 13 Jan 2004

290G - Lecture 3a

  • The introduction of unknowns and constraintsworks only because the shape of the proof isalready known.^ – This tells us where to put the constraints andunknowns. • The revised rules are trivial to implement,except for handling the constraints.

Example^ 13 Jan 2004

290G - Lecture 3a α^ β^ γ^ =^ → α^ γ^ β=^ →^ intβ=

-^ A solution is

int^ int,

int,

int

α

β

γ =^ →

=^

=

Solving Type Equations^ 13 Jan 2004

290G - Lecture 3a

  • Term equations are a unification problem.^ – Solvable in near-linear time using a union-findbased algorithm. • No solutions

α^ = T[α

] are permitted

  • Theoccurs check. – The check is omitted if we allow infinite types.

Syntax^ 13 Jan 2004

290G - Lecture 3a

16 / 28

-^ We distinguish

solved equations

α^ {^ τ

-^ Each rule manipulates only unsolved equations.

{^ }^

{^ }

{^

}^

{^

}

{^ 1 2 3 }

1 3 2 4

4

{^ }

[^ /^ ]

,

S int^ int

S

S

S

S

S

S

S α^ α α^ τ

τ^ α^

α^ τ

τ^ τ^

τ^ τ

τ^ τ^ τ^

τ

∪^ =^

⇒ ∪^ =^

⇒^

∪^ ≅

∪^ →^

=^ →^

⇒^ ∪^

=^ =

∪^ =^

⇒

Rules 1 and 4^ 13 Jan 2004

290G - Lecture 3a

17 / 28

-^ Rules 1 and 4 eliminate trivial constraints. •^ Rule 1 is applied in preference to rule 2^ –^ the only such possible conflict

{^ }^

{^ }

{^

}^

{^

}

{^ 1 2 3 }

1 3 2 4

4

{^ }

[^ /^ ]

,

S int^ int

S

S

S

S

S

S

S α^ α α^ τ

τ^ α^

α^ τ

τ^ τ^

τ^ τ

τ^ τ^ τ^

τ

∪^ =^

⇒ ∪^ =^

⇒^

∪^ ≅

∪^ →^

=^ →^

⇒^ ∪^

=^ =

∪^ =^

⇒

Rule 3^ 13 Jan 2004

290G - Lecture 3a

19 / 28

-^ Rule 3 applies structural equality to non-trivial terms.

{^ }^

{^ }

{^

}^

{^

}

{^ 1 2 3 }

1 3 2 4

4

{^ }

[^ /^ ]

,

S int^ int

S

S

S

S

S

S

S α^ α α^ τ

τ^ α^

α^ τ

τ^ τ^

τ^ τ

τ^ τ^ τ^

τ

∪^ =^

⇒ ∪^ =^

⇒^

∪^ ≅

∪^ →^

=^ →^

⇒^ ∪^

=^ =

∪^ =^

⇒

Correctness^ 13 Jan 2004

290G - Lecture 3a

20 / 28

-^ Each rule preserves the set of solutions.^ –^ Rules 1 and 4 eliminate trivial constraints.^ –^ Rule 2 substitutes equals for equals.^ –^ Rule 3 is the definition of equality on function types.

{^ }^

{^ }

{^

}^

{^

}

{^ 1 2 3 }

1 3 2 4

4

{^ }

[^ /^ ]

,

S int^ int

S

S

S

S

S

S

S α^ α α^ τ

τ^ α^

α^ τ

τ^ τ^

τ^ τ

τ^ τ^ τ^

τ

∪^ =^

⇒ ∪^ =^

⇒^

∪^ ≅

∪^ →^

=^ →^

⇒^ ∪^

=^ =

∪^ =^

⇒