Docsity

Midterm Examination CS 565 - March 8, 2006 - Prof. Suresh Jagannathan, Exams of Programming Languages

Partial preview of the text

Download Midterm Examination CS 565 - March 8, 2006 - Prof. Suresh Jagannathan and more Exams Programming Languages in PDF only on Docsity!

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:

  1. (5 points) Write down the nameless representation (i.e., the representation via deBrujin indices) of the untyped λ term, λx.λy.x(λz.x(yz)). λ.λ.1(λ.2(1 0))
  2. (5 points) Let t 1 = λ.λ.((2 0)(1 0)) and let t 2 = λ(2 1). What is the result of substituting t 2 for t 1 ’s only free variable? λ.λ.((λ.(4 3) 0) (1 0))

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.)

  1. Using this result, determine the fixpoint for the following terms, and validate your answer:

(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)

  1. (10 points) A fixed-point combinator is any term M such that ∀F.M F →∗^ F (M F ). Consider the term Θ ≡ A A where A ≡ λxy.y(x x y). Is Θ a fixed-point combinator? If yes, prove it; if not, present a counter-example.

Θ 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:

  1. while false ⇓ σ = σ and σ(x) is even by the induction hypothesis.
  2. while true. In evaluating the antecedents of the true case, we have 〈x := x + 2, σ〉 ⇓ σ′′^ and D 1 = 〈while b do, ..., σ′′^ ⇓ σ′〉. We know that σ′′^ = σ[x 7 → σ(x) + 2] and thus, σ′′(x) is even. Apply the induction hypothesis to the derivation rooted at D 1 to complete the proof.

Some call-by-value languages provide explicit constructs to support lazy evaluation. One common approach is to define operations called delay and force:

  • The delay procedure is used together with the procedure force to implement lazy evaluation or call by need. The expression: delay(expression) returns an object called a promise which at some point in the future may be asked (by the force procedure) to evaluate expression, and deliver the resulting value.
  • The expression force(expression) evaluates expression to a promise, and returns the value of the promise. If no value has been computed for the promise, then a value is computed and returned. Significantly, the value of the promise forced is memoized or cached, so that if it is forced a second time, the previously computed value is returned.

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.

  1. (5 points) Show the typing rules for delay and force. You may assume the existence of a new type constructor prom such that τ prom denotes the type of a promise holding a value of type τ.

Γ e : τ Γ delay(e) : τ prom

Γ e : τ prom Γ force(e) : τ

Γ e : τ Γ promise(e) : τ prom

  1. (10 points) Using a small-step operational semantics, formalize the informal description given above for the simply typed λ-calculus. (Do these evaluation rules adequately capture the memoization property of a promise?)

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

  1. (10 points) The type preservation theorem can be stated as:

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 τ.