Sec Week04 Soln
Sec Week04 Soln
Lambda calculus basics; Lambda calculus encodings and Recursion (Lectures 7–8)
Section and Practice Problems
Section Week 4
• λa. z λz. a y
Answer:
(λ a . z (λ z . a y ))
Answer:
(λ z . z) (λ b . b (λ a . a a))
• λb. b λa. a b
Answer:
(λ b . b (λ a . a b))
• λz. z λz. z z
Answer:
(λ z . z (λ z . z z))
Answer:
(λ a . (λ b . (λ a . a) (λ b . a)))
Answer:
x (λ x . (λ x . x (λ x . x)))
• y (λy. y)(λy. z)
Answer:
y (λ y . y) (λ y . z )
(b) Alpha equivalence: Which of these three lambda-calculus expressions are alpha equivalent?
i. λx. y λa. a x
ii. λx. z λb. b x
iii. λa. y λb. b a
(Hint: to figure out whether two expressions are alpha equivalent, you need to know which variables
are free and which variables are bound.)
Answer: Expressions i. and iii. are alpha-equivalent. Note that alpha equivalence applies only to bound variables;
free variables cannot be renamed.
(c) Evaluation For each of the following terms, do the following: (a) write the result of one step of the
call-by-value reduction of the term; (b) write the result of one step of the call-by-name reduction of the
term; and (c) write all possible results of one step under full β-reduction. If the term cannot take a step,
please note that instead.
Answer: The semantics for CBV and CBN are the same, we have:
There are no other possible reductions since only one application exists.
Page 2 of 7
Lambda calculus basics; Lambda calculus encodings and Recursion (Lectures 7–8)
Section and Practice Problems
Answer: There are no possible steps under CBV and CBN because neither allows reductions inside a
lambda term. For full β-reduction, we have the following:
(λx. x x x x) (λx. λy. x y) −→ (λx. λy. x y) (λx. λy. x y) (λx. λy. x y) (λx. λy. x y)
(λx. λy. x y) ((λw. λz. w z) (λx. x)) −→ (λx. λy. x y) (λz. (λx. x) z)
(λx. λy. x y) ((λw. λz. w z) (λx. x)) −→ λy. ((λw. λz. w z) (λx. x)) y
There are no additional steps that we can take under full β-reduction.
(λa. (λb. b a) a) (λz. z) (λw. w) −→ ((λb. b (λz. z)) (λz. z)) (λw. w).
In addition, under full β-reduction we can also reduce the subexpression (λb. b a) a, giving us
(d) Suppose we have an applied lambda calculus with integers and addition. Write the sequence of
expressions that the following lambda calculus term evaluates to under call-by-value semantics. Then
do the same under call-by-name semantics.
Page 3 of 7
Lambda calculus basics; Lambda calculus encodings and Recursion (Lectures 7–8)
Section and Practice Problems
AND FALSE TRUE ≜ (λb1 . λb2 . b1 b2 FALSE ) (λx. λy. y) (λx. λy. x)
−→(λb2 . (λx. λy. y) b2 FALSE ) (λx. λy. x)
−→(λx. λy. y) (λx. λy. x) FALSE
−→(λy. y) FALSE
−→FALSE
(b) Evaluate IF FALSE Ω λx. x under CBN semantics. What happens when you evaluated it under CBV
semantics?
Page 4 of 7
Lambda calculus basics; Lambda calculus encodings and Recursion (Lectures 7–8)
Section and Practice Problems
IF FALSE Ω (λx. x) ≜ (λb. λt. λf. b t f ) (λx. λy. y) ((λx. x x) (λx. x x)) (λx. x)
−→(λt. λf. (λx. λy. y) t f ) ((λx. x x) (λx. x x)) (λx. x)
−→(λf. (λx. λy. y) ((λx. x x) (λx. x x)) f ) (λx. x)
−→(λx. λy. y) ((λx. x x) (λx. x x)) (λx. x)
−→(λy. y) (λx. x)
−→(λx. x)
If we evaluate this expression under CBV semantics, we need to evaluate the Omega expression before we can
apply the lambda abstraction. Since the Omega expression evaluates indefinitely, the overall expression never
terminates.
(c) Evaluate ADD 2 1 under CBV semantics. (Make sure you know what the Church encoding of 1 and 2
are, and check that the answer is equal to the Church encoding of 3.)
1 ≜ λf. λx. f x
2 ≜ λf. λx. f (f x)
PLUS ≜ λn1 . λn2 . n1 SUCC n2
SUCC ≜ λn. λf. λx. f (n f x)
(d) In class we made use of a combinator ISZERO, which takes a Church encoding of a natural number
n, and evaluates to TRUE if n is zero, and FALSE if n is not zero. (We don’t care what ISZERO does
if it is applied to a lambda term that is not a Church encoding of a natural number.)
Define ISZERO.
Page 5 of 7
Lambda calculus basics; Lambda calculus encodings and Recursion (Lectures 7–8)
Section and Practice Problems
3 Recursion
Assume we have an applied lambda calculus with integers, booleans, conditionals, etc. Consider the fol-
lowing higher-order function H.
H ≜ λf. λn. if n = 1 then true else if n = 0 then false else not (f (n − 1))
(b) Compute Y H under CBN semantics. What has happened to the function call f (n − 1)?
Here, note that ((λx. H (x x)) (λx. H (x x))) was the result of beta-reducing Y H and it has replaced f in the
evaluation. Thus, f becomes our recursive call ISODD.
(λn. if n = 1 then true else if n = 0 then false else not (ISODD (n − 1))) 2
−→if 2 = 1 then true else if 2 = 0 then false else not (ISODD (2 − 1))
−→if false then true else if 2 = 0 then false else not (ISODD (2 − 1))
−→if 2 = 0 then false else not (ISODD (2 − 1))
−→if false then false else not (ISODD (2 − 1))
−→not (ISODD (2 − 1))
−→not (ISODD 1)
−→∗ not true
−→false
(d) Use the “recursion removal trick” to write another function that behaves the same as the fixed point
of H.
Page 6 of 7
Lambda calculus basics; Lambda calculus encodings and Recursion (Lectures 7–8)
Section and Practice Problems
H ′ ≜λf. λn. if n = 1 then true else if n = 0 then false else not (f f (n − 1))
g = H′ H′
Page 7 of 7