...
- (40 pts.) Write an evaluator for arithmetic expressions as follows:
- Write the (function) template for
arith-expr
- Write a function
to-list
that maps anarith-expr
to the corresponding "list" representation in Scheme. Numbers are unchanged. Some other examples include:
Note: you need to define the output type (namedCode Block (to-list (make-sum (make-prod 4 7) 25)) => '(+ (* 4 7) 25) (to-list (make-quot (make-diff 4 7) 25)) => '(/ (- 4 7) 25)
scheme-expr
) for this function, but you can omit the template because this assignment does not include any functions that process this type. - Write a function
eval: arith-expr -> number
that evaluates anarith-expr
. Your evaluator should produce exactly the same result for anarith-expr E
that Scheme evaluation would produce for the list representation(to-list E)
.
- Write the (function) template for
- (40 pts.) Extend the definition of
<arith-expr>
} as follows:- Add a clause for variables represented as Scheme symbols.
- Write the (function) template for this definition.
- Modify your definition of
to-list
to support the new definition of arith-expr. - Given the Scheme structure defintion:
aCode Block (define-structure binding (var val))
binding
is(make-binding s n)
wheres
is a symbol andn
is a number and anenvironment
is a(list-of binding)
. Write a (function) template for processing anenvironment
. - Define a top-level variable (constant)
empty-env
that is bound to the empty environment. - Write a function
extend
that takes environmentenv
, a symbols
, and a numbern
,
and returns an extended environment identical toenv
except that it adds the additional binding ofs
ton
.
The definition ofextend
is trivial; it requires no recursion. As a result,extend
satisfies the invariant
In the remainder of the problem, useCode Block (check-expect (extend empty-env s n) (list (make-binding s n)))
empty-env
andextend
to define example environments for test cases. - Write a function
lookup
that takes a symbols
and an environmentenv
and returns the first binding inenv
with avar
component that equalss
. If no match is found,lookup
returns empty. Note that the return type oflookup
is not simplybinding
because it can returnempty
. Define the a new union type calledoption-binding
for the the return type. - Write a new
eval
function for the new definition ofarith-expr
. The neweval
takes two arguments: anarith-expr E
to evaluate and anenvironment env
specifying the values of free variables inE
. For example,
If anCode Block (eval 'x (extend empty-env 'x 17)) => 17 (eval (make-prod 4 7) (extend empty-env 'x 17)) = 28 (eval 'y (extend empty-env 'x 17)) => some form of run-time error
arith-expr E
contains a free variable that is not bound in theenvironment env
, then(eval E env)
will naturally produce some form of run-time error if you have correctly codedeval
. Do not explicitly test for this form of error.
- (20 pts.) An
environment
is really a finite function (a finite set of ordered pairs). It is finite in the sense that it can be completely defined by a finite table, which is not true of nearly all the primitive and library functions in Scheme (and other programming languages). Even the identity function is not finite. For the purpose of this exercise, we redefine the typeenvironment
as(symbol -> option-binding)
.- Rewrite
eval
to useenvironment
defined as a finite function in(symbol -> option-binding)
instead of as a(list-of option-binding)
. If you cleanly coded your definition ofeval
in the preceding problem usinglookup
,make-binding
, andextend
, all that you have to do to your solution to the previous problem is redefine the bindings oflookup
,empty-env
, andextend
, and revise your test cases forextend
. You can literally copy the entire text of your solution to problem 2; change the definitions oflookup
,empty-env
, andextend
; update your documentation (annotations) concerning theenvironment
type; and revise your tests forextend
. Note thatextend
cannot be tested (since the result is a function!) without usinglookup
to examine it. (If you wrote a correct solution to problem 2, you can do this problem is less than 15 minutes!)
Hint: you can uselambda
-notation to define a constant function forempty-env
, andextend
can be defined as a functional that takes a function (representing an environment) and adds a new pair to the function--using aif
embedded inside alambda
-expression.
- Rewrite
- Extra Credit (50 pts.) Add support for
lambda
-expressions in your evaluator as follows:- Extend the definition of
<arith-expr>
by adding a clause for unarylambda
-expressions and a clause for unary applications of anarith-expr
to anarith-expr
. Use the namelam
for the structure representing alambda
-expression and the namesvar
andbody
for the accessors of this structure. Use the nameapp
for the structure representing an application and the nameshead
andarg
for the accessors of this structure. Note that the head of anapp
is anarith-expr
not alam
. - Write a (function) template for the newest definition of
arith-expr
. - Extend the definition of
to-list
to support the newest definition ofarith-expr
. - Extend the definition of
eval
to support the newest definition ofarith-expr
. Note thateval
can now return functions as well as numbers. Your biggest challenge is determining a good representation for function values. What doeseval
return for alam
input? That input may contain free variables. In principle, you could represent the value of thelam
input by a revisedlam
(with no free variables) obtained by substituting the values for free variables from the environment input (just like we do in hand-evaluation). But this approach is tedious and computationally expensive. A better strategy is to define a structure type (called a closure) to represent a function value. The structure type must contain the originallam
and a description of what substitution would have been made, deferring the actual substitution just aseval
defers substitutions by maintaining an environment.
- Extend the definition of