EECS 395 Programming Languages: Homework 5

Due: Saturday, February 19th, 2011, noon

Part 0 – Deferred Substition

You must start with the interpreter that uses deferred substition for this assignment (see the lecture notes).

Part 1 – Shorthands

Add with to the parser for the FAE language, according to this rule:

{with {x FAE1} FAE2} =>
{{fun {x} FAE2} FAE1}

Add multiple argument functions to the parser for the FAE language, according to these rules:

{FAE1 FAE2 FAE3 FAE4 ...} =>
{{FAE1 FAE2} FAE3 FAE4 ....}

{fun {ID1 ID2 ID3 ...} FAE} =>
{fun {ID1} {fun {ID2 ID3 ...} FAE}}

The ellipses following a non-terminal above means that the rules apply with an arbitrary number of expressions in place of the non-terminal that appears just before the ellipsis. These are example uses of above rules:

{f x y z} => {{f x} y z} => {{{f x} y} z}

{fun {a b} {+ a b}} => {fun {a} {fun {b} {+ a b}}}

Nullary functions and application expressions with no arguments should both be syntax errors. Also note that these rules are intended to be schematic, not precise descriptions of how to implement your parser.

Part 2 – Conditionals, again

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.

Part 3 – Errors

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

Part 4 – Numbers as functions

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}}}}},
etc. In general the number N is represented as a function that accepts a function and returns a new function that iterates the original N times. Each of these functions has the type (X -> X) -> (X -> X). That is, if you pass in a function that consumes and produces objects of the same shape, then it returns to you a function that calls the input N times, to represent the number N.

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

Part 5 – Addition and multiplication

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}}}}}}}
 => 5
Simlarly, 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

Part N – Handin instructions

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, 2011
robby@eecs.northwestern.edu