Due: Saturday, February 19th, 2011, noon
You must start with the interpreter that uses deferred substition for this assignment (see the lecture notes).
Add with to the parser for the FAE language, according to this rule:
Add multiple argument functions to the parser for the FAE language, according to these rules:
Add if0 to your interpreter with the same syntax as homework 4 but, unlike homework 4, you must be careful that the test position can be any value at all, not just a number. If the test position is a procedure, it is treated the same as any other non-0 value.
There are three different kinds of errors that can occur (at runtime) in this language and for each error in the input program, your interpreter must signal an error that includes one of the following phrases:
free identifier application expected procedure numeric operation expected number
It is possible to represent numbers using functions. One way to do this is to represent
0 as | {fun {f} {fun {x} x}} |
1 as | {fun {f} {fun {x} {f x}}} |
2 as | {fun {f} {fun {x} {f {f x}}}} |
3 as | {fun {f} {fun {x} {f {f {f x}}}}}, |
Write an FAE function that converts a natural number in the above represention into a number in the normal represention called n-to-f and write a WAE program that converts back, called f-to-n. The n-to-f function does not have to be well-defined if it gets a negative number (or a rational or complex, etc); that is, you can assume it is called only with natural numbers.
For example,
{with {n-to-f ...} {with {f-to-n ...} {f-to-n {n-to-f 4}}}} => 4
Write the FAE functions plus and timesthat accept two numbers in the above representation and add/multiply them together. Neither of these FAE function can (indirectly or directly) use FAE's + or - operation (i.e., you cannot use n-to-f from part 4 and then add the numbers and call f-to-n).
For example,
{with {n-to-f ...} {with {f-to-n ...} {with {plus ...} {with {times ...} {f-to-n {plus {n-to-f 2} {n-to-f 3}}}}}}} => 5Simlarly, with the appropriate with wrappers, you should get this:
{f-to-n {plus {n-to-f 0} {n-to-f 0}}} => 0 {f-to-n {plus {n-to-f 2} {n-to-f 0}}} => 2 {f-to-n {plus {n-to-f 0} {n-to-f 2}}} => 2 {f-to-n {times {n-to-f 1} {n-to-f 0}}} => 0 {f-to-n {times {n-to-f 0} {n-to-f 1}}} => 0 {f-to-n {times {n-to-f 3} {n-to-f 4}}} => 12
The final program you handin should use this precise define-type definition for FAE.
(define-type FAE [num (n number?)] [add (lhs FAE?) (rhs FAE?)] [sub (lhs FAE?) (rhs FAE?)] [id (name symbol?)] [if0 (test FAE?) (then FAE?) (else FAE?)] [fun (param symbol?) (body FAE?)] [app (fun FAE?) (arg FAE?)])
Provide a definition of interp-expr : FAE -> number or 'procedure, as above.
Provide a definition of parse : sexpression -> FAE, as above.
Bind your definitions of n-to-f, f-to-n, plus to PLAI-level definitions of the same name, e.g.,
(define n-to-f `{fun {n} ...}) (define f-to-n `{fun {f} ...}) (define plus `{fun {f1 f2} ...}) (define times `{times {f1 f2} ...})
Last update: Sunday, February 13th, 2011robby@eecs.northwestern.edu |