Homework 4 (Due Monday 9/25/2023 at 11:59pm)
To submit this assignment, simply upload the files in your local GitHub repository to your GitHub Classroom repository for this assignment (as you have done for earlier assignments). In contrast to Assignments 1 and 2 where you could put the programs for all problems in a single file (HW01.rkt for Assignment 1 and HW02.rkt for Assignment 2), you must store the solution to each problem in a separate .rkt file, namely HW04-1.rkt, HW04-2.rkt, HW04-3.rkt, and HW04-4.rkt (if you do the extra credit problem) for problems 1, 2, 3, and 4 (extra credit). Unfortunately, none of the HTDP languages supported by DrRacket allow these files to be combined.
Embed any answer text that is not executable in a DrRacket block comment or block commenting brackets (#| and |#).
Use the Intermediate Student with lambda language.
Given the Racket structure definitions:
(define-struct sum (left right)) (define-struct prod (left right)) (define-struct diff (left right)) (define-struct quot (left right))
an ArithExpr is either:
- a number
n, - a sum
(make-sum ae1 ae2), - a product
(make-prod ae1 ae2), - a difference
(make-diff ae1 ae2), or - a quotient
(make-quot ae1 ae2)
where n is a number (as defined in Racket), and ae1 and ae2 are ArithExprs.
The following 4 exercises involve the data type ArithExpr. If you are asked to write a function(s), follow the design recipe: contract, purpose, examples/tests, (function) template instantiation, code, testing (which happens automatically when the examples are presented using (check-expect ...) ). Follow the same recipe for any help function that you introduce. You may use any of the library functions shown in class lectures including append.
- (40 pts.) Write an evaluator for arithmetic expressions as follows:
- Write the (function) template for
ArithExpr Write a function
to-listthat maps anArithExprto the corresponding "list" representation in Racket. Numbers are unchanged. Some other examples include:(to-list 21) => 21 (to-list (make-sum (make-prod 4 7) 25)) => '(+ (* 4 7) 25) (to-list (make-quot (make-diff 4 7) 25)) => '(/ (- 4 7) 25)
Notes:
The notation
'(+ (* 4 7) 25)abbreviates(list '+ (list '* 4 7) 25).You need to define the output type (named
RacketExpr) for this function, but you can omit the template because this assignment does not include any functions that process this type. There are several mathematically distinct definitions that are correct. Some are more restrictive (narrower) than others but all are correct. In general, narrow definitions are better because they imply stronger claims about the elements of the type.The notation
'(+ (* 4 7) 25)abbreviates(list '+ (list '* 4 7) 25).
- Write a function
eval: ArithExpr -> numberthat evaluates anArithExpr. Your evaluator should produce exactly the same result for anArithExprEthat Racket evaluation would produce for the list representation(to-list E).
- Write the (function) template for
- (40 pts.) Extend the definition of
ArithExpras follows:- Add a clause for variables represented as Racket symbols.
- Write the (function) template for this extended definition; it should be similar to your template for
ArithExprfrom Problem 1. - Modify your definition of
to-listto support the expanded definition ofArithExpr. Given the Racket structure definition:
(define-structure binding (var val))
a
bindingis(make-binding s n)wheresis a symbol andnis a number. Anenvironmentis a(list-of binding). Write a (function) template instantiation for processing anenvironment.- Define a top-level variable
empty-envthat is bound to the emptyenvironmentcontaining no bindings (i.e., theemptylist). Note thatempty-envis really a constant since variables cannot be re-bound in our functional subset of Racket. Write a function
extendthat takes environmentenv, a symbols, and a numbern, and returns an extended environment identical toenvexcept that it adds the additional binding ofston(as the most local [inner] binding).
The definition ofextendis trivial; it requires no recursion. As a result,extendsatisfies the invariant(check-expect (extend empty-env s n) (list (make-binding s n)))
where
sis any symbol andnis any number. Hence,(extend empty-env 'a 4) => (list (make-binding 'a 4))
In the remainder of the problem, use
empty-envandextendto define example environments for test cases.- Write a function
lookupthat takes a symbolsand an environmentenvand returns the firstbindinginenvwith avarcomponent that equalss. If no match is found,lookupreturnsempty. Note that the return type oflookupis not simplybindingbecause it can returnempty. Define the new union type calledoption-bindingfor the return type. You do not need to write a template for this type since it is a trivial union type. Write a new
evalfunction for the expanded definition ofArithExpr. The newevaltakes two arguments: anArithExprEto evaluate and anenvironment envspecifying the values of free variables inE. For example,(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
If an
ArithExprEcontains 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 since the behavioral contract forevalsays nothing about detecting errors.
- (20 pts.) An
environmentis 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 Racket (and other programming languages). Even the identity function is not finite. For the purpose of this exercise, we redefine the typeenvironmentas(symbol -> option-binding)which includes infinite objects (sincesymbolis infinite).- Rewrite
evalto useenvironmentdefined as the type(symbol -> option-binding)instead of as a(list-of option-binding). If you cleanly coded your definition ofevalin 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 theenvironmenttype; and revise your tests forextend. Note thatextendcannot be directly tested (since the result is a function!) but we can write indirect tests by usinglookupto examine it. (Note: if you wrote a correct solution to problem 2, you can do this problem in less than 15 minutes!)
Hint: you can uselambda-notation in Racket to define a constant function forempty-env, andextendcan be defined as a functional that takes a function (representing an environment) and adds a new pair to the function--using anifembedded inside alambda-abstraction.
- Rewrite
- Extra Credit (50 pts.) Add support for
lambda-abstractions in your evaluator from Problem 2 as follows:- Extend the definition of
ArithExprby adding a clause for unarylambda-abstractions and a clause for unary applications of anArithExpr(which can be a function!) to anArithExpr. Use the namelamfor the structure representing alambda-abstraction and the namesvarandbodyfor the accessors of this structure. Use the nameappfor the structure representing a unary application and the namesratorandrandfor the argument of this structure. Note that theratorof anappis anArithExprnot alam(which is a proper subtype ofArithExpr). - Write a template for this additional expansion of the definition of
ArithExpr. - Extend the definition of
to-listto support this expansion of the definition ofArithExpr. You print the corresponding "concrete" syntax that would be given as input to a compiler. Hence,
(to-list (make-lam 'x (make-plus 'x 'y))) => (list 'lambda (list 'x) '(+ x y)) = '(lambda (x) (+ x y)) - Extend the definition of
evalto support this expansion ofArithExpr. Note thatevalcan now return functions as well as numbers. Your biggest challenge is determining a good representation for function values. What doesevalreturn as the value of alaminput? That input may contain free variables. In principle, you could represent the value of thelaminput 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 originallamand a description of what substitution (of values for free variables) would have been made, deferring the actual substitution just asevaldefers substitutions by maintaining an environment. For this problem, you can use brute force to describe the substitution, which makes constructing and applying closures nearly trivial to code. In practice, there are much more computationally efficient approaches.
- Extend the definition of