Docsity

CS 565 Midterm: Fixed-Point Combinators, Function Composition, and Logic Questions - Prof., Exams of Programming Languages

Partial preview of the text

Download CS 565 Midterm: Fixed-Point Combinators, Function Composition, and Logic Questions - Prof. and more Exams Programming Languages in PDF only on Docsity!

Midterm Examination CS 565 March 11, 2009

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: 100 Attained score:

Let A = λaλbλcλdλeλf λgλhλiλjλkλlλmλnλoλpλqλsλtλuλvλwλxλyλzλr.r(isthisaf ixedpointcombinator) and let B = AAAAAAAAAAAAAAAAAAAAAAAAAA (26 applications of A).

Is B a fixed-point combinator? Justify your answer.

Yes. If B is a fixed-point combinator, then B A = A(B A). B A = A[26 applications of A]A = A (B A).

(c) (5 points) Show that all members of the sequence Y 1 ,... are fixed point combinators.

By induction over n. The base case is trivial. Now, GY n+1^ = G(Y nG) = Y nG since Y n^ is a fixed-point combinator by IH. ≡ Y n+

Using the axiom of assignment and the rule of composition prove the following sequence of statements exchanges the values of variables x and y.

x := x − y; y := x + y; x := y − x

Make sure to clearly state appropriate pre- and post-conditions in your proof.

The precondition is {x = A ∧ y = B}. The postcondition is {x = B ∧ y = A}. Working backwards, we have:

{P 1 }x := y − x{x = B ∧ y = A}

Using the axiom of assignment, we get:

{y − x = B ∧ y = A}x := y − x{x = B ∧ y = A}

We apply a similar process using the newly derived precondition as the last the postcondition for the second- last statement:

{x + y − x = B ∧ x + y = A}y := x + y{y − x = B ∧ y = A}

We simplify the precondition to {y = B ∧ x + y = A}. Applying the rule of sequencing and repeating our use of the axiom of assignment, we get:

{y = B ∧ x − y + y = A}x := x − y{y = B ∧ x + y = A}

The precondtion can be simplified to {y = B ∧ x = A}. We apply the rule of composition once more to prove the validity of the entire program.

(a) (10 points) For each of the following lambda terms, decide whether it is typable in the simply-typed lambda calculus. If a term is typable, give a type for it. i. λx. λ y.x(x y)

(σ 1 → σ 2 ) → σ 1 → σ 2

ii. λx. λ y.(y x)y

Not typable

iii. λx. λ y.y(x y)

((σ 1 → σ 2 ) → σ 1 ) → (σ 1 → σ 2 ) → σ 2

(b) (10 points) For each of the following type formulas, provide a closed lambda term (i.e., a term with no free variables) in the simply typed lambda calculus that has that type, if one exists. i. σ → σ

λ x : σ.x

ii. (σ 1 → σ 2 ) → σ 3 → (σ 1 → σ 2 )

λ x : σ 1 → σ 2 .λ y : σ 3 .x

iii. (σ 1 → σ 2 ) → (σ 2 → σ 3 ) → (σ 1 → σ 3 )

λ f.σ 1 → σ 2 .λ g : σ 2 → σ 3 .λ x : σ 1 .g(f x)

The SKI-calculus is comprised of three combinators:

S = λ x.λ y.λ z.x z (y z) K = λ x.λ y.x I = λ x.x

  1. (5 points) What is the λ-calculus term that corresponds to S I I (S I I)? (λ x.(x x))(λ x.(x x))
  2. (5 points) What is the value of S (K S) K when reduced to normal form? Can you think of a good name for this term? This combinator reduces to λ x. λ y.λz.x (y z). It could be regarded as a function composition operation.