The Typed Lambda Calculus - Lecture Slides | COM S 541, Papers of Programming Languages

Material Type: Paper; Class: PROGRAMNG LANGUAGES; Subject: COMPUTER SCIENCE; University: Iowa State University; Term: Unknown 1997;

Typology: Papers

Pre 2010

Uploaded on 09/02/2009

koofers-user-ivr
koofers-user-ivr 🇺🇸

9 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Com S 541
The Typed Lambda Calculus
!There are many variants of the lambda calculus. The typed lambda
calculus decorates terms with type annotations:
Syntax:
e ::= xτ| e1σ"τe2σ| (λxσ. eτ)σ"τ
Operational Semantics:
α-conversion: λxσ. eτ ↔ λyσ. [yσ/xσ]eτ where yσis not free in eτ
β-reduction: (λxσ. e1τ) e2σ "[e2σ/xσ]e1τ
η-reduction: λxσ. (e1τxσ) "e1τif xσis not free in e1τ
Example:
True (λxσ. (λyτ. xσ)τ"σ)σ"(τ"σ)
False (λxσ. (λyτ. yτ)τ"τ)σ"(τ"τ)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download The Typed Lambda Calculus - Lecture Slides | COM S 541 and more Papers Programming Languages in PDF only on Docsity!

Com S 541

The Typed Lambda Calculus

!

There are many variants of the lambda calculus. The typed lambdacalculus decorates terms with type annotations:

Syntax:

e ::= x

τ

| e

1

σ

"

τ

e

2

σ

x

σ

. e

τ

σ

"

τ

Operational Semantics:

-conversion:

x

σ

. e

τ

y

σ

. [y

σ

/x

σ

]e

τ

where y

σ

is not free in e

τ

-reduction:

x

σ

. e

1

τ

) e

2

σ

[e

2

σ

/x

σ

]e

1

τ

-reduction:

x

σ

. (e

1

τ

x

σ

e

1

τ

if x

σ

is not free in e

1

τ

Example:

True

x

σ

y

τ

. x

σ

τ

"

σ

σ

"

(

τ

"

σ)

False

x

σ

y

τ

. y

τ

τ "

τ

σ

"

( τ

"

τ)

Com S 541

The Polymorphic Lambda Calculus

!

Polymorphic functions like ìmapî cannot be typed in the typed lambdacalculus!

!

We need type variables to capture polymorphism: β

-reduction (ii):

x

Α

. e

1

τ

) e

2

σ

[

/A]([e

2

σ

/x

Α

]e

1

τ

Example:

True

x

Α

y

Β

. x

Α

Β

"

Α

Α

"

(

Β

"

Α)

True

Α

"

(

Β

"

Α)

τ

b

σ

y

Β

. a

τ

Β

"

τ

b

σ

a

τ

Com S 541

Pol

ym or

phi

sm and Sel

fAppl

i

cat

i

on

!

Even the polymorphic lambda calculus is not powerful enoughto express certain lambda terms.

!

Recall that both

Ω

and the Y combinator, which make use of

ìself applicationî:

Ω ≡

(

λ

x. x x) (

λ

x. x x)

Y

≡ λ

f. (

λ

x. f (x x)) (

λ

x. f (x x))

!

What type annotation would you assign to the expression? Aretheses terms typable at all?

Com S 541

Type Inference

Overview:

!

The type inference problem

!

Typed lambda terms, type assertions, and typing rules

!

Wandís algorithm

!

Unification of type equations

References:

!

Mitchell Wand, ìA Simple Algorithm

and Proof for Type Inferenceî,

Fundamenta Informaticae, 10:115-122, 1987

!

J. Roger Hindley, ìBasic Simple Type Theoryî, Cambridge UniversityPress, 1997

!

John C. Mitchell, ìFoundations for Programming Languagesî, MITPress, 1996

Com S 541

Typed Lambda-Terms

!

The set

e

of untyped lambda-terms are defined as follows:

e:

x

x.e)

e

1

e

2

!

The set

T

of types is defined as follows:

t:

K

  • basic types

t

1

"

t

2

  • function types

!

The set

e

T

of typed lambda-terms is obtained by modifying the second

clause in the definition of the untyped lambda-terms:

x:t .e)

Com S 541

A Type System

!

A

type assumption is a partial function

:V
T

with a finite domain

V

the set of

variables.

!

A type assertion or type judgement is a triple

,e,t)

, where

is a type

assumption,

e

is a lambda-term (either typed or untyped, depending on the

context), and

t

is a type. The domain of

is exactly the set of free variables of

e

f

v(

e)

!

A type system for the lambda-calculus:

2

2

1

1

2

2

1

1

2

1

1

2

1

t : )

e

(e

t : e - t t : e -

t

t :

e)

. t : x ( -

t : e - t : x ;

t : x - t : x ;

|

|

| |

|

|

Com S 541

Wandís Algorithm - Skeleton

!

Input:A lambda-term

e

0

!

Initialization:Set

E =

and

G = {(

0

,e

0

0

, where

0

is a type variable and

0

maps the

free variables of

e

0

to other distinct type variables.

!

Loop Step:If

G =

, then halt and return

E

. Otherwise, choose a subgoal

,e,t

from

G

, delete it from

G

, and add to

E

and

G

new verification conditions and

subgoals, as specified in an action table.End of Skeleton

Com S 541

The Action Table

!

Case

(

Γ

,x,t

)

:

Generate the equation

t=

Γ

(

x)

.

!

Case

(

Γ

,(

λ

x.e)

,t

)

:

Let

τ

1

and

τ

2

be fresh type variables. Generate the equation

t=

τ

1

"

τ

2

and the subgoal

(

Γ

;x:

τ

1

,e,

τ

2

)

.

!

Case

(

Γ

,(

e

1

e

2

)

,t

)

:

Let

τ

1

be a fresh type variable. Then generate the subgoals

(

Γ

,e

1

,

τ

1

"

t

)

and

(

Γ

,e

2

,

τ

1

)

.

Com S 541

The Equation Set

!

The generated equations are:

0

1

2

2

3

4

4

5

6

8

7

6

1

8

5

9

7

3

9

5

!

Solving these equations by the unification algorithm gives the solution:

0

5

7

6

5

7

5

6

which is the so-called

principal type of the term

x.

y.

z. x z (y z).

Com S 541

The Algorithm

unify

unify(

unify(E

{K

1

= K

2

if K

1

K

2

then

fail

else unify(E)

unify(E

= t}) =

if

t then unify(E)

else if

occurs in t then

fail

else unify([t/

]E)

°

[t/

]

unify(E

{t =

}) = unify(E

= t})

unify(E

{t

1

t

2

= t

3

t

4

}) = unify(E

{t

1

= t

3

{t

2

= t

4