CS 334: HW 2

Instructions

This homework has two types of problems:

  • Self Check: You are strongly encouraged to think about and work through these questions, but you will not submit answers to them.

  • Problems: You will turn in answers to these questions.

Reading

Self Check

Parse Tree

Mitchell, Problem 4.1

Lambda Calculus Reduction

Mitchell, Problem 4.3

Problems

1. Reference Counting (15 pts)

Mitchell, Problem 3.6

2. Parsing and Precedence (10 pts)

Mitchell, Problem 4.2

3. Symbolic Evaluation (15 pts)

Lambda Calculus Checker

I just wrote Lambdasaurus, a Lambda Calculus Reduction Checker, to help you work through the reductions on your homework. You can find it on the CS334 website here.

The Lisp program fragment

(defun f (x) (+ x 4))
(defun g (y) (- 3 y))
(f (g 1))

can be written as the following lambda expression:

\left(\ \underbrace{(\lambda f.\lambda g. f\ ( g\ 1 ))}_\mathrm{main} \ \underbrace{( \lambda x. x + 4 )}_f\ \right) \ \underbrace{( \lambda y. 3 - y )}_g

Reduce the expression to a normal form in two different ways, as described below.

  1. Reduce the expression by choosing, at each step, the reduction that eliminates a \lambda as far to the left as possible.

  2. Reduce the expression by choosing, at each step, the reduction that eliminates a \lambda as far to the right as possible.

  3. In pure \lambda-calculus, the order of evaluation of subexpressions does not effect the value of an expression. The same is true for Pure Lisp: if a Pure Lisp expression has a value under the ordinary Lisp interpreter, then changing the order of evaluation of subterms cannot produce a different value. However, that is not the case for a language with side effects. To give a concrete example, consider the following "Java"-like code fragment:

    int f(int a, int b) {
        ...
    }
    
    {
        int x = 0;
    
        System.out.println(f(e1,e2));
    }
    

    Write a function f and expressions e1 and e2 for which evaluating arguments left-to-right and right-to-left produces a different result. Your expressions may refer to x. Try it out in your favorite imperative language --- C, C++, Java, etc. Which evaluation order is used?

4. Lambda Reduction with Sugar (10 pts)

Here is a "sugared" lambda-expression using let declarations:

\begin{array}{l} {\tt let}\ \mathit{compose} = \lambda f.\,\lambda g.\, \lambda x.\, f (g\,x)\ {\tt in} \\ \ \ \ \ \ {\tt let}\ h = \lambda x.\,x+x\ {\tt in} \\ \ \ \ \ \ \ \ \ \ \ ((\mathit{compose}\,h)\,h)\,3 \end{array}

The "de-sugared" lambda-expression, obtained by replacing each {\tt let}\ z = U ~{\tt in}~ V by \\(\lambda z.\,V)\,U is

\begin{array}{l} (\lambda \mathit{compose}.\, \\ \ \ \ \ \ (\lambda h.\; ((\mathit{compose}\; h)\; h)\; 3) \ \ (\lambda x.\, x+x)) \\ \ \ \ \ \ (\lambda f.\, \lambda g.\, \lambda x.\, f(g\ x)) \end{array}

This is written using the same variable names as the let-form in order to make it easier to read the expression.

Simplify the desugared lambda expression using reduction. Write one or two sentences explaining why the simplified expression is the answer you expected.

5. Garbage Collection Techniques (20 pts)

Read the Wilson Garbage Collection paper. This paper discusses many foundational ideas behind modern garbage collection. Please answer the following questions with one or two sentences each. The most credit will be given for clear, concise answers --- you should not need to write much.

a. What are the limitations of mark-and-sweep and reference-counting collectors?

b. What problem does copying-collection solve?

c. What is the main insight behind incremental collection?

d. What about generational collectors? When will they work well? When will they work poorly?

Most modern collectors use a combination of several techniques to best handle current systems with built-in concurrency and much larger heaps. If you're curious, have a look at the additional GC papers on the Readings web page, including papers on the HotSpot Java Virtual Machine implements garbarge collection, the Immix collector, and others.

Submitting Your Work

Submit your answers to the GradeScope assignment named, for example, "HW 0". It should:

  • be clearly written or typed,
  • include your name and HW number at the top,
  • list any students with whom you discussed the problems, and
  • be a single PDF file, with one problem per page.

You will be asked to resubmit homework not satisfying these requirements. Please select the pages for each question when you submit.