Lambda Calculus Homework Solutions, Assignments of Programming Languages

Solutions for homework problems related to lambda calculus, including drawing binding and free variables, reducing lambda expressions, and translating algol-like code to lambda calculus.

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-4dj
koofers-user-4dj 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 452 Homework #2
Due: Tuesday September 16
1. In the following lambda expressions, draw an arrow showing the lambda binding for
each occurrence of a bound variable; additionally, draw a box around each occurrence
of a free variable.
(a) λz.(xz) 0
(b) λx. λy. λz. (x z ) + ( y z )λx.(x+z)
(c) λp. λq. hλp.(p[p q ] ) λr.(p+r)i(p+ 4 ) 2
2. Reduce each of the lambda expressions in problem 1 until no further β-reductions are
possible. To receive full credit for this problem, you must show each step—that is, you
need to show the result after each β-reduction.
3. Below, the Algol-like program fragment on the left can be written as the “sugared”
lambda expression (see slide 46, Tuesday of week #2) on the right:
w = 2;
function f(x)
return x + w
end;
function g(z)
return z * z
end;
g(f(1)+1);
let w= 2
in
let f=λx.(x+w)
in
let g=λz.(zz)
in
g( (f1) + 1 )
end
end
end
1
pf2

Partial preview of the text

Download Lambda Calculus Homework Solutions and more Assignments Programming Languages in PDF only on Docsity!

CSE 452 Homework # Due: Tuesday September 16

  1. In the following lambda expressions, draw an arrow showing the lambda binding for each occurrence of a bound variable; additionally, draw a box around each occurrence of a free variable.

(a) λz.( x ∗ z ) 0

(b) λx. λy. λz.

( ( x z ) + ( y z )

) λx.( x + z )

(c) λp.

( λq.

[ λp.( p [ p q ] ) λr.( p + r )

] ( p + 4 )

) 2

  1. Reduce each of the lambda expressions in problem 1 until no further β-reductions are possible. To receive full credit for this problem, you must show each step—that is, you need to show the result after each β-reduction.
  2. Below, the Algol-like program fragment on the left can be written as the “sugared” lambda expression (see slide 46, Tuesday of week #2) on the right:

w = 2; function f(x) return x + w end; function g(z) return z * z end; g(f(1)+1);

let w = 2 in let f = λx.(x + w) in let g = λz.(z ∗ z) in g ( (f 1) + 1 ) end end end

The latter, in turn, can be “desugared” by replacing each let-expression with its defi- nition to produce:

λw.

( λf.

[ λg.

( g ( (f 1) + 1 )

) λz.( z ∗ z )

] λx.( x + w )

) 2

(a) Reduce this lambda expression by choosing, at each step, the reduction that eliminates a λ as far to the left as possible, until no further reductions are possible. Show each step of the reduction. (b) Reduce this same lambda expression by choosing, at each step, the reduction that eliminates a λ as far to the right as possible, until no further reductions are possible. Show each step of the reduction.

  1. Consider the following Algol-like program.

function g(y) return y * y end; function h(k) return k(k(3)) end; h(g);

(a) Write it as a “sugared” lambda expression, as illustrated in the statement of the previous problem. (b) “Desugar” the let-expressions in your answer to part (a) of this problem, as illus- trated in the statement of the previous problem. (c) Reduce the desugared lambda expression that you obtained for part (b) of this problem to find the result of this program. Show the steps in the reduction.

  1. Consider the following C-like function definition:

int poly(x, n) { if (n == 0) return 0; else return 1 + x * poly(x, n-1); }

Using mathematical induction, prove that poly(x, n) returns

∑^ n i=

xi, for any real num- ber x and non-negative integer n, where, as you may recall from your discrete math course, ∑n i=

xi^ =

{ 1 + x + x^2 + · · · + xn^ for n > 0 0 for n = 0

  1. Problem 4.11 on page 87 of Mitchell.