CS 410/510 Compiling Advanced Languages Homework 1 - due Friday, April 7, 2000

Pencil and Paper

1. Do Field & Harrison Exercises 6.1, 6.2(ii), 6.5(a,c), 6.7a, 6.8. Draw trees where appropriate!

2. Define an addition function for the Church numerals, as described in class.

Programming

These problems refer to the file lambda.ml in the course directory. To load and compile the declarations in this file, start ocaml and execute ``#use "lambda.ml";;''. Make sure you can execute these functions before you continue!

3. (a) Write a function cbv that attempts to evaluate an exp to normal form using call-by-value.

(b) Write a similar function cbn that attempts to evaluate an exp to normal form using call-by-name.

Hints:

5. Extend lambda.ml to support built-in integer constants and simple arithmetic operations on them. Do this by adding new exp constructors Const of int and Plus, and extending all the functions to operate correctly on these constructors.

6. The following datatype is suitable for describing pure $\lambda$-expressions using de Bruijn notation:
\begin{code}type dexp = Dabs of dexp \vert Dapp of dexp * dexp \vert Dvar of int\end{code}
Write a function convert: exp -> dexp that converts closed $\lambda$-expressions from exp format to dexp format by computing the correct de Bruijn indices. For example, the output of (convert one) should equal Dabs(Dabs(Dapp(Dvar 1,Dvar 0))). Remember to handle rebinding of names in inner scopes correctly. Do something sensible if your function is handed a non-closed argument. Hint: Your function can use a list to maintain an environment of bound variable names as it traverses the exp; the position of a variable in this list is closely connected with its de Bruijn index.

Extra Credit (for fun)

Here is a function $\overline{\tt pred}$ that calculates the predecessor of a Church numeral (where $\overline{\tt succ}$, $\overline{\tt true}$ and $\overline{\tt false}$ are as defined in class).

$\lambda$k.(k($\lambda$p.$\lambda$u.u( $\overline{\tt succ}$(p $\overline{\tt true}$))(p $\overline{\tt true}$)) ($\lambda$u.u $\overline{\tt0}$ $\overline{\tt0}$)) $\overline{\tt false}$.

(a) Draw the $\lambda$-tree (parse tree) of this function.

(b) Explain, in English, how it works. (Hint: Function names can be misleading.)


Andrew P. Tolmach
2000-03-31