Simply Typed Lambda Calculus - Lecture Notes | CMPS 290, Study notes of Computer Graphics

Material Type: Notes; Class: Advanced Topics in Computer Graphics; Subject: Computer Science; University: University of California-Santa Cruz; Term: Unknown 2005;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-une-1
koofers-user-une-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Simply-Typed Lambda Calculus
Types and Programming Languages, Spring 2005, SoE UCSC
presenter Jessica Gronski
scribe Avik Chaudhuri
April 5, 2005
1 Why types?
Recall that the untyped lambda calculus has the following syntax and semantics.
Terms e::= x(variable)
|λx.e (abstraction)
|e e (application)
Values v::= λx.e
Reduction e1 e
1
(app-l)e1e2 e
1e2
e2 e
2
(app-r)v1e2 v1e
2
(β)λx.e1v2 e1[x7→ v2]
A normal form is any term that cannot be reduced. We agreed that one of the goals of typing would be
to eliminate the possibility of normal forms that are not values. Such a property would be eventually
named progress.
A second goal would be to guarantee termination by eliminating terms like λx.(xx)λx.(xx).
2 Notions of safety
We discussed a couple of definitions.
1. e. e. e esuch that eis a value in normal form.
This means that there must exist some evaluation path that terminates in a value. If there are
several possible evaluation paths, some paths may not terminate, or may terminate in terms that
are not values. This did not seem quite appropriate for our goals.
2. e. e.(eeand eis in normal form) implies eis a value.
Observe that this does not require that all evaluation paths terminate; it does require that if an
evaluation terminates, it does so at a value.
1
pf3
pf4
pf5

Partial preview of the text

Download Simply Typed Lambda Calculus - Lecture Notes | CMPS 290 and more Study notes Computer Graphics in PDF only on Docsity!

Simply-Typed Lambda Calculus

Types and Programming Languages, Spring 2005, SoE UCSC

presenter Jessica Gronski

scribe Avik Chaudhuri

April 5, 2005

1 Why types?

Recall that the untyped lambda calculus has the following syntax and semantics. Terms e ::= x (variable) | λx.e (abstraction) | e e (application)

Values v ::= λx.e Reduction e 1 −→ e′ 1 (app-l) e 1 e 2 −→ e′ 1 e 2

e 2 −→ e′ 2 (app-r) v 1 e 2 −→ v 1 e′ 2

(β) λx.e 1 v 2 −→ e 1 [x 7 → v 2 ]

A normal form is any term that cannot be reduced. We agreed that one of the goals of typing would be to eliminate the possibility of normal forms that are not values. Such a property would be eventually named progress. A second goal would be to guarantee termination by eliminating terms like λx.(xx) λx.(xx).

2 Notions of safety

We discussed a couple of definitions.

  1. ∀e. ∃e′. e −→⋆^ e′^ such that e′^ is a value in normal form. This means that there must exist some evaluation path that terminates in a value. If there are several possible evaluation paths, some paths may not terminate, or may terminate in terms that are not values. This did not seem quite appropriate for our goals.
  2. ∀e. ∀e′. (e −→⋆^ e′^ and e′^ is in normal form) implies e′^ is a value. Observe that this does not require that all evaluation paths terminate; it does require that if an evaluation terminates, it does so at a value.

We adopted (2) as our definition of safety.

3 Simply-typing an applied λ calculus

We wrote down a simply-typed applied calculus with booleans and conditional branches. Simple types T ::= T → T (function type) | B (boolean type)

Terms e ::= x (variable) | λx : T.e (abstraction) | e e (application) | true (true) | false (false) | if e then e else e (conditional branch)

Values v ::= λx.e | true | false Reduction e 1 −→ e′ 1 (T-app-l) e 1 e 2 −→ e′ 1 e 2

e 2 −→ e′ 2 (T-app-r) v 1 e 2 −→ v 1 e′ 2

(T-β) λx : T.e 1 v 2 −→ e 1 [x 7 → v 2 ]

e 1 −→ e′ 1 (T-cond-b) if e 1 then e 2 else e 3 −→ if e′ 1 then e 2 else e 3

(T-cond-l) if true then e 1 else e 2 −→ e 1

(T-cond-r) if false then e 1 else e 2 −→ e 2

We then wrote down typing rules for the calculus.

Type environments Γ ::= · (empty) | Γ; x : T (augment)

Type judgements ::= Γ ⊢ e : T

The notation x : T ∈ Γ means that the rightmost binding of x in Γ is x : T. We write ⊢ e : T to mean · ⊢ e : T.

(ax-var) x^ :^ T^ ∈^ Γ Γ ⊢ x : T

Γ, x : T ⊢ e : T ′ (→i) λx : T.e : T → T ′

(→e) Γ^ ⊢^ e^1 :^ T^ →^ T^ ′^ Γ^ ⊢^ e^2 :^ T Γ ⊢ e 1 e 2 : T ′ (ax-true) Γ ⊢ true : B

(ax-false) Γ ⊢ false : B

4 Proving safety

We discussed two lemmas that are sufficient to prove safety in the simply-typed λ calculus.

Lemma 4.1 (Progress). If e is well-typed ( i.e., ⊢ e : T for some T ) then either e is a value or ∃e′^ such that e −→ e′.

proof. By induction on the structure of the derivation ⊢ e : T. ⊳

Lemma 4.2 (Preservation). If ⊢ e : T for some T and ∃e′^ such that e −→ e′, then ⊢ e′^ : T.

proof. By induction on the structure of the derivation ⊢ e : T and case analysis on e −→ e′. ⊳

The following theorem asserts the safety property discussed in §2 for well-typed terms.

Theorem 4.3 (Safety). If e is well typed and e −→⋆^ e′^ such that e′^ is in normal form, then e′^ is a value.

proof. By induction on the length of e −→⋆^ e′, using Lemmas 4.1 and 4.2. ⊳

In fact, a weak version of Lemma 4.2 is sufficient (together with Lemma 4.1) to prove Theorem 4.3.

Lemma 4.4 (Weak preservation). If e is well-typed and ∃e′^ such that e −→ e′, then e′^ is well-typed.

5 Venn diagrams for interesting subsets of terms

Figure 1: Subsets of terms in the λ calculus

Figure 1 shows containment relationships between the sets V (values), N (terms in normal form), YV (terms that yield values), YN (terms that yield normal forms) and W (well-typed terms).

6 Erasing type annotations

Finally, we agreed that typing does not affect the operational semantics of the language. The function erase erases type annotations on all abstracted variables, giving terms in the untyped language. Thus

erase(x) = x erase(λx : T.e) = λx. erase(e) erase(e 1 e 2 ) = erase(e 1 ) erase(e 2 ) erase(true) = true erase(false) = false erase(if e 1 then e 2 else e 3 ) = if erase(e 1 ) then erase(e 2 ) else erase(e 3 )

Lemma 6.1 (Correspondence of −→). For any term e in the typed language,

  1. if e −→ e′^ then erase(e) −→ erase(e′)
  2. if erase(e) −→ e′′^ then ∃e′. e −→ e′^ and e′′^ = erase(e′)

proof. (1) By induction on the structure of the derivation e → e′^ and (2) by case analysis on e and induction on the structure of erase(e) −→ e′′. ⊳

In other words, the following diagram commutes. e −→ e′^ −→... (typed world) ⇓ ⇓ (erase type annotations) erase(e) −→ erase(e) −→... (untyped world)

Theorem 6.2 (Correspondence of safety). If e is well-typed and erase(e) −→⋆^ e′′^ such that e′′^ is in normal form, then e′′^ is a value.

proof. By induction on the length of erase(e) −→⋆^ e′′^ with Lemma 6.1, and using Theorem 4.3. ⊳