Out: Friday, 15 February 2008
Due:
5 PM Friday, 22 February 2008
10 AM Sunday, 24 February 2008
Submission: Individual
This assignment is to be done individually, not in pairs.
One purpose of this assignment is to get further experience extending and modifying interpreters. Another purpose is to explore the issues that arise when programming with state. By the end of this assignment, you should be comfortable making large changes to the LETREC language, and you should understand how state is specified and implemented in the EXPLICIT-REFS language.
let
and letrec
in
the Scheme language.
Consider the following (very complicated, completely artificial) Scheme expression:
(let ((f (lambda (h) ; line 01 (letrec ((w (lambda (j) "P01")) ; line 02 (g (lambda (k) ; line 03 (let ((g ((lambda (y) (y g)) ; line 04 (lambda (y) "P02")))) ; line 05 "P03")))) ; line 06 (letrec ((k (lambda (h) "P04")) ; line 07 (f (lambda (w) "P05"))) ; line 08 (let ((k "P06") ; line 09 (f "P07")) ; line 10 "P08")))))) ; line 11 (letrec ((k (lambda (s) ; line 12 (lambda (j) ; line 13 (letrec ((z (lambda (j) "P09")) ; line 14 (v (lambda (f) "P10")) ; line 15 (f (lambda (y) ; line 16 (let ((z "P11")) ; line 17 "P12")))) ; line 18 "P13")))) ; line 19 (g (lambda (y) ; line 20 (let ((w "P14")) ; line 21 "P15")))) ; line 22 (let ((j (lambda (f) "P16"))) ; line 23 "P17"))) ; line 24Note that this expression is closed; it has no free variables.
"P17"
,
with the line number of each variable's binding."P04"
,
with the line number of each variable's binding."P06"
,
with the line number of each variable's binding."P09"
,
with the line number of each variable's binding.Hint: if you are not sure about your answers at any point during this exercise, we strongly encourage you to construct experimental expressions in Scheme to test your answer. For example, to test the answer aboutThe bound variables at the expression labelled"P17"
are: f (line 01), k (line 12), g (line 20), and j (line 23).
"P17"
above, one might replace
the string "P17"
with various other expressions to try
to learn which variables are in scope and which bindings they refer to.
One might also construct smaller expressions using
let
and letrec
,
binding simpler expressions to the variables, and see
how they behave.
Hint: The expression in task 1 illustrates the complexities of variable scoping. A good way to start this task would be to write programs in the proposed extension of the LETREC language that test whether you get the scoping rules right. Remember that the goal of testing is to find errors, and there are ample opportunities for error here.
> (equal? (run "newref(3)") (run "newref(4)")) #t
newref(3)
is equivalent to the EXPLICIT-REFS expression newref(4)
?
If not, explain how the two expressions are different, and
explain, in terms of the implementation
of the interpreter, why the Scheme equal?
expression above
is returning #t
.
-(exp1,
exp2)
(value-of
exp2ρ
σ
)
= (val2, σ1)
(expval->num
val2)
= n2
(value-of
exp1ρ
σ1
)
= (val1, σ2)
(expval->num
val1)
= n1
n1 - n2 = n
----------------------------------------------------
(value-of (diff-exp
exp1exp2
)
ρσ
)
= ((num-val
n)
, σ2)
Every EXPLICIT-REFS program is also a program in the REFS-EXPLETIVE (and vice versa), but the languages are not the same.
Demonstrate this by making a program that produces a number m in EXPLICIT-REFS and a number n in REFS-EXPLETIVE, where m does not equal n.
You are given the following interpreters:
The easiest way to copy these interpreters into one of your own
directories is to use the cp
command on a CCIS Solaris
machine:
cp -r /course/csg111/.www/interps/letrec-lang . cp -r /course/csg111/.www/interps/explicit-refs .
mp5.txt
that contains
your answers for tasks 1 and 3, as well as the demonstration
program for task 4.
Last modified: 16 February 2008