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 -expressions
using de Bruijn notation:
Write a function convert: exp -> dexp that converts closed
-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 that calculates the predecessor of a Church numeral (where , and are as defined in class).
k.(k(p.u.u( (p ))(p )) (u.u )) .
(a) Draw the -tree (parse tree) of this function.
(b) Explain, in English, how it works. (Hint: Function names can be
misleading.)