Docsity

Type Checking Lecture 2: Understanding Types and Type Checking in Lambda Calculus, Study notes of Computer Graphics

Toggle sidebar
bg1
Type-Checking
Lecture 2
8 Jan 2004 290G - Lecture 2 1 / 19
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Type Checking Lecture 2: Understanding Types and Type Checking in Lambda Calculus and more Study notes Computer Graphics in PDF only on Docsity!

  • Type-Checking Lecture
    • 8 Jan
    • 290G - Lecture

Outline^ 8 Jan 2004

290G - Lecture 2

-^ A language^ –^ Lambda calculus •^ Types^ –^ Type checking

Why Lambda Calculus?^ 8 Jan 2004

290G - Lecture 2

  • History^ – Introduced by Church in the 1930’s^ – Small, paradigmatic language • Computation via

beta reduction

  • Function call^ • substitution of argument for formal parameter – Roughly:

(λx:τ.e) e’

→^ e[x^ β^

←^ e’]

Simple Examples^ 8 Jan 2004

290G - Lecture 2

(λx:τ.x) y

→^ yβ^

(λx:int.x+1) 3

→^ 3+1β^

→^4

Function Types^ 8 Jan 2004

290G - Lecture 2

-^ Intuitively, a type

τ→^ τ^1

stands for the set of

functions that map arguments of type

τto results of^1

type^ τ.^2 • Placeholder for any other structured datatype^ –^ Lists^ –^ Trees^ –^ Arrays

Types are Trees^ 8 Jan 2004

290G - Lecture 2

  • Types are terms • Any term can be represented by a tree^ – The parse tree of the term^ – Tree representation is important in algorithms

(α^ →^ int)

→^ α^ →

int

→ →

→ α int

int

α

Type Environments^ 8 Jan 2004

290G - Lecture 2

-^ To determine whether the types in an expression are consistentwe perform

type checking.

-^ But we need types for free variables, too!•^ Atype environment is a function from variables to types.

The

syntax of environments is:• The meaning is:

|^ ,^ : A^

A x^ τ= ∅

if

(^ ,^ :^

)(^ )^

(^ )^ if

x^ y

A x^

y^ A y

x

y τ τ^

=^

Type Checking Rules^ 8 Jan 2004

290G - Lecture 2

-^ Type checking is done by structural induction.– One inference rule for each form– Assumptions contain types of free variables– A term is

well-typed if

∅^ d^ e:^ τ

1 2 1 2 1 1

2 2

3 1 2

(^ )^
,^ :^ : : :^.^ :^

: : int : int^

: int^

i : int^

: int^

if^

A^ e A^ e

A x

A x^ e

A^ x^

A^ x^ e

A^ e^ e A^ e

A^ e

A^ e

A^ e

A^ e

A

A^ e^ e

A^ e^ e

τ^ τ τ^ e

τ^ τ

τ^ λ^

τ^ τ^ τ

′^

d dd → +

d^

d^

d d d^

d d^

d

d^

d^

d

Type Checking Algorithm^ 8 Jan 2004

290G - Lecture 2

  • There is a simple algorithm for type checking • Observe that there is only one possible“shape” of the type derivation^ – only one inference rule applies to each form.

?^ :?? :^ x.^ : ?y x :.^ :^.^ :? x^ λ^ β y^ x λ^ α λ

β ∅

d d d

Algorithm (Cont.)^ 8 Jan 2004

290G - Lecture 2

-^ Walk the proof tree from the root to the leaves,generating the correct environments. •^ Assumptions are simply gathered from lambdaabstractions.

:^ ,^ :

:? :^

:^.^ :? :. :^.^

:?

x^ y

x x

y^ x x y^

x α^

β α^ λ

β λ^ α λ

β ∅

d d d

A Bigger Example^ 8 Jan 2004

290G - Lecture 2

16 / 19

:^ ,^

:^ : :^

:^.^ :^

:^ :

:^.^

:^.^ : (^

)^

:^.^ :

(^ :^

.^ :^.^

)^ :^.^

: (^ )

x

y^ x x

y^ x

z^ z

x

y^ x

z^ z

x

y^ x^

z^ z α^ α^

β^ α^

α α^ α^

λ^ β^

β^ α^

α

α^ α

λ^ α^

α λ^ β^

α^ α^

β^ α^

α^ λ^

α^ α^

α

λ^ α^

α λ^ β^

λ^ α^

α^ α^

β^ α^

α

→^

→ →^

→^ →

∅^

→^

→^ →^

→^ →^

∅^

→

∅^

→^

→^ →^

→^ →

d d^

d

d^

d

d

What Do Types Mean?^ 8 Jan 2004

290G - Lecture 2

-^ Thm.

If A^ d

e:τ^ and

∗e → d, then Aβ^

d^ d:τ

  • Evaluation preserves types. • This is the basis of a claim that there can beno runtime type errors – functions applied to data of the wrong type^ • Adding to a function^ • Using an integer as a function

Next Time ...^ 8 Jan 2004

290G - Lecture 2

  • LCLint^ – a debugging tool^ – based on dataflow analysis^ – compensates for C’s weak type system • Preparation^ – read 2 LCLint papers^ – also finish “Type Systems”