Homework 6: Lambda Calculus

COMP 105, Fall 2019

Due Thu, Oct 24 @ 11:59pm [submissions not accepted after this time]

This assignment will give you practice working with lambda calculus. Recall there is no textbook chapter on the lambda calculus. Instead, see the readings linked on the schedule on the class web page. (Note the second Ramsey handout includes a little bit of ML; you can ignore that and read the rest of the handout safely without understand it.) You can also look on Wikipedia.

There are no reading comprehension problems for this homework. Through this homework, we write \ for λ

Put your solutions to these problems in a file written.pdf.

  1. For each of the following lambda terms, add parentheses to show how the term is parsed. For example, given \x.x x x, write \x.((x x) x). For this problem only, you should check your answers with the TAs, who will tell you whether they are correct or not.
    1. \x.x y
    2. \x.x \y.y
    3. \x.\y.x y y
    4. x \x.x y \y.y
  2. For each of the following lambda terms, add an index to each variable so that it's obvious which variables correspond to which lambda bindings. Leave free variables without indices. For example, given x \x.x, write x \x1.x1. Each binding should have a unique index.
    1. \x.x y
    2. \x.x \x.x
    3. \x.\y.x y y
    4. \f.(\x.f (x x)) (\x.f (x x))
  3. Write the result of each of the following substitutions on lambda terms. For example, given (\x.x y)[y ↦ z], write \x.x z. If necessary, alpha-convert to avoid capture.
    1. (y \x.x)[y ↦ z]
    2. (x \x.x)[x ↦ z]
    3. (x \x.y)[y ↦ z]
    4. (\x.x x)[x ↦ z]
    5. (\x.x y)[y ↦ x]
    6. (\x.\y.x y (x z))[z ↦ x]
  4. Recall the rules for taking a single step in the lambda calculus (these rules exclude Eta-reduction and alpha conversion):
    
       ----------------------------- [Beta]
            (\x.M) N  →  M[x↦N]
    
                    M → M'
       ----------------------------- [Nu]
                  M N → M' N
    
                    N → N'
       ----------------------------- [Mu]
                  M N → M N'
    
                    M → M'
       ----------------------------- [Xi]
                 \x.M → \x.M'
    
    Draw a derivation showing that each of the following reductions is possible following the rules above. For applications of [Beta], write the substitution above the line. For example, given \x.(\y.y) z → \x.z, you would write
    
             y[y↦z] = z
      ------------------- [Beta]
           (\y.y) z → z
     ------------------------- [Xi]
      \x.(\y.y) z → \x.z
    
    1. (\x.x) y → y
    2. (\x.y) z → y
    3. (\x.\y.x) w z →(\y.w) z
  5. Using the reduction rules listed for the previous problem, reduce each of the following terms to normal form. Do not write the full derivation of each step of reduction. For example, given (\x.(\y.y) z) w, write (\x.(\y.y) z) w → (\x.z) w → z or write (\x.(\y.y) z) w → (\y.y) z → z.
    1. (\x.\y.x) w z
    2. (\n.(n \z.w) y) (\f.\x.f x)
    3. (\x.\y.\z.(x z) (y z)) (\a.a) (\b.b) c

In class, we saw how Church numerals can be written in µScheme with the following definitions:

(val true (lambda (x) (lambda (y) x)))
(val false (lambda (x) (lambda (y) y)))
(val zero (lambda (f) (lambda (x) x)))
(val one (lambda (f) (lambda (x) (f x))))
(val two (lambda (f) (lambda (x) (f (f x)))))
(val three (lambda (f) (lambda (x) (f (f (f x))))))
(val succ (lambda (n) (lambda (f) (lambda (x) (f ((n f) x))))))
(val plus (lambda (n) (lambda (m) ((n succ) m))))

Implement the following µScheme functions involving Church numerals. You do not need to submit algebraic laws for these functions, but we highly encourage you to think through them and follow the usual design proces. You must, however, write test cases. Write your answers to this part and the next part in a file lambda.scm.

Next, you will implement call-by-value beta-reduction on lambda terms. You will write your code for this part of the project using μScheme. You do not need to write algebraic laws, but you should write test cases. We will represent lambda terms using records constructed with the following definitions:

(record l-var [name])   ; variable
(record l-lam  [name body]) ; lambda
(record l-app [left right]) ; application

For example, here are some lambda terms and their representations:

    (make-l-lam 'x (make-l-var 'x))              ; \x.x

    (make-l-app (make-l-var 'x) (make-l-var 'y)) ; x y

    (make-l-lam 'x (make-l-lam 'y                ; \x.\y.x y
                               (make-l-app (make-l-var 'x)
                                           (make-l-var 'y))))


Implement the following functions over lambda terms.