Docsity

Lambda Calculus Homework 2 Solutions, Assignments of Programming Languages

Toggle sidebar
bg1
CMSC 330
Homework 2
Due: November 5, 2008 in discussion section
Lambda Calculus Problems
0. Here is the ambiguous grammar for lambda calculus given in class:
E → x | x . E | E Eλ
where we use x to stand for any variable. The two rules for parsing with this (ambiguous)
grammar are:
1. Application is left associative. So “e1 e2 e3” is parsed as “(e1 e2) e3”
2. Lambda extends as far to the right as possible. So “ x . e1 e2” is parsed as “ x . (e1λ λ
e2)”
Bearing that in mind, draw parse trees for the following lambda terms:
(a) x .λ x z y . x yλ
Answer:
pf3
pf4

Partial preview of the text

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

CMSC 330

Homework 2 Due: November 5, 2008 in discussion section Lambda Calculus Problems

  1. Here is the ambiguous grammar for lambda calculus given in class: E → x | λx. E | E E where we use x to stand for any variable. The two rules for parsing with this (ambiguous) grammar are:
  2. Application is left associative. So “e1 e2 e3” is parsed as “(e1 e2) e3”
  3. Lambda extends as far to the right as possible. So “ λx. e1 e2” is parsed as “ λx. (e e2)” Bearing that in mind, draw parse trees for the following lambda terms: (a) λx. x z λy. x y Answer:

(b) ( λx. x z) λy. w λw. w y z x Answer: (c) λx. x y λx. y x Answer: If you have any doubt about how to parse these terms, please come to office hours, since misunderstanding this will likely cause you to make mistakes on the rest of the homework.

  1. Find the set of free variables in the following expressions : (a) λx. x z λy. x y Answer: {z} (b) ( λx. x z) λy. w λw. w y z x

(b) Show the result of applying beta reduction to the expression (S (K S) K) x y z until no more beta reductions can be applied. Answer: (S (K S) K) x y z = ( λx. λy. λz. (x z (y z)) ) (KS) K) x y z > ( λ y. λz. ((KS) z (y z)) ) K) x y z > ( λz. ((KS) z (K z)) ) x y z > ( λz. (S (K z)) ) x y z > (S (K x)) y z = ( λ x. λy. λz. (x z (y z)) (K x) ) y z > ( λ y. λz. ( (K x) z (y z)) ) y z > ( λ y. λz. (x (y z)) ) y z > ( λ z. (x (y z)) )z > (x (y z)) > x y z

  1. Using the definitions of true, false, and or given in class (see slides), show that or behaves correctly as a logical “or” function. Answer: To answer this question, we must show that the lambda expression can be reduced to the correct boolean result for each possible combination of true and false. In the following reductions, true and false are used as symbols representing the Church booleans. Or (true, true): λx. λy. ((x true) y) true true > λ y. ((true true) y) true > ((true true) true) = ((( λx. λy. x) true) true) > (( λy. true) true) > true Or (true, false): λx. λy. ((x true) y) true false > λ y. ((true true) y) false > ((true true) false) = ((( λ x. λy. x) true) false) > (( λy. true) false) > true Or (false, true): λx. λy. ((x true) y) false true > λ y. ((false true) y) true > ((false true) true) = ((( λ x. λy. y) true) true) > (( λy. y) true) > true Or (false, false): λx. λy. ((x true) y) false false > λ y. ((false true) y) false > ((false true) false) = ((( λx. λy. y) true) false) > (( λy. y) false) > false