




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Midterm Examination CS 565 March 8, 2006
Name:
Instructions: Answer all questions in the space provided. Extra blank pages are provided in the back. Partial credit will be given where appropriate.
Maximum score: 120 Attained score:
The Fixed-Point Theorem for the untyped λ-calculus states that for any well-formed λ term F , there also exists a well-formed term X such that F X = X.
The proof of this theorem is as follows:
Let W ≡ λ x.F (x x) and X ≡ W W. Then, X ≡ W W ≡ ((λ x.F (x x)) W ) ≡ F (W W ) ≡ F X
(In the following, consider λ xy.e as shorthand for λ x.λ y.e.)
(a) (5 points) λ x.x. (λ x.(x x))(λ x.(x x))
(b) (10 points) λ xy.(x y) (λ xy.(x x) y)(λ xy.(x x) y)
Θ F ≡ (λ xy.y (x x y)) A F → (λ y.y(A A y))F → F (A A F ) ≡ F (Θ F )
The IMP language is given by the following grammar:
e ∈ AExp ::= n | x | e 1 + e 2 | e 1 − e 2 | e 1 ∗ e 2 b ∈ BExp ::= true | false | e 1 = e 2 | e 1 ≤ e 2 | ¬b | b 1 ∧ b 2 | b 1 ∨ b 2 c ∈ Com ::= skip | x := e | c 1 ; c 2 | if b then c 1 else c 2 | while b do c
Prove the following statement by structural induction: For any boolean command b and any initial state σ, such that σ(x) is even, if
while b do x := x + 2, σ ⇓ σ′
than σ′(x) is even.
Reference: The natural semantics for while loops is given by the following two commands:
(b, σ) ⇓ false
(while b do c, σ) ⇓ σ
(b, σ) ⇓ true, (c; while b do c), σ ⇓ σ′
(while b do c, σ) ⇓ σ′
Consider a derivation D of this expression. There are two possible rules used at the top of D:
Some call-by-value languages provide explicit constructs to support lazy evaluation. One common approach is to define operations called delay and force:
For example:
val count = ref 0; val p = delay( count := (!count) + 1; if ((!count) > x) then count else force(p)); val x = 5; (* x = 5 ) p; ( a promise ) force(p); ( 6 ) p; ( a promise ) (x:=10; force(p)); ( 6 *)
Consider the incorporation of delay and force into the simply-typed λ calculus.
Γ e : τ Γ delay(e) : τ prom
Γ e : τ prom Γ force(e) : τ
Γ e : τ Γ promise(e) : τ prom
delay(e) → promise(e)
e → e′, e 6 = promise(e’) force(e) → force(e′)
e → e′ force(promise(e)) → force(promise(e′)) force(promise(v)) → v
If e : τ and e → e′then e′^ : τ
One way to prove this theorem is by induction on the derivation of e → e′. Show the cases for the new constructs (delay and force) in this proof. The case for delay(e) is straightforward. A delay expression yields an object of type τ prom as does the evaluation of the promise that the delay yields. To prove the theorem for force(e) we examine the remaining three rules. When e is not a promise, the theorem holds trivially by the induction hypothesis. When e is a promise, but the promise does not hold a value, the theorem is satisfied again by the induction hypothesis applied to the rule’s antecedent. In the case where it is a value, we know by the typing rules for force and prom that promise(v) must be of type τ prom, and thus v must be of type τ.