



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Advanced Topics in Computer Graphics; Subject: Computer Science; University: University of California-Santa Cruz; Term: Unknown 2005;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Types and Programming Languages, Spring 2005, SoE UCSC
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).
We discussed a couple of definitions.
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,
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. ⊳