Assignment 4, CSC430, Winter 2015
1 Goal
2 Guidelines
2.1 Contracts & Test Cases
2.2 Handling errors
2.3 Language Level & File Format
3 The Assignment
3.1 New Forms
3.2 Old Forms
3.3 Syntax of GUCI4
4 Suggested Implementation Strategy
5 Suggested Awesome Test Case
6 Interface
6.1.1.8

Assignment 4, CSC430, Winter 2015

1 Goal

Extend the interpreter to handle mutable arrays.

2 Guidelines

2.1 Contracts & Test Cases

For this and all remaining assignments, every function you develop must come with the following things:

2.2 Handling errors

Your parser and interpreter must detect errors and explicitly signal them by calling (error ...). We will consider an error raised internally by Scheme to be a bug in your code.

For example, Scheme signals a "divide by zero" error if you attempt to evaluate (/ 1 0). Since we use Scheme’s division function to implement division in GUCI4, you may be tempted to leave it to Scheme to signal division by zero errors for you. However, you must signal the error yourself by explicitly testing for division by zero before calling Scheme’s division procedure.

2.3 Language Level & File Format

For this assignment, you must develop your solutions using the
#lang plai-typed
language level. Your test cases must use the (test ...) or the (test/exn ...) forms.

Your solution should take the form of a single file. Solve each problem separately, and make sure that each solution appears in a separate part of the file, with comments separating each problem’s solution.

Hand in your solution using the handin server. For help with the handin server, please see the course web page.

3 The Assignment

For this assignment, you must implement mutable arrays, and mutable bindings.

As in previous assignments, the with form should just expand into an application.

3.1 New Forms

In order to allow mutable bindings and arrays, you’ll need to add a store, and rewrite your interpreter in store-passing style, as we did in class.

Note that arrays are not typed; it’s fine to have an array that mixes numbers and booleans.

3.2 Old Forms

In a language with mutation, programmers can observe the order of evaluation of binop arguments, and function call arguments. For this language, all of these must perform left-to-right evaluation.

Also, the eq? function must now accept arrays. It should return true for arrays only when its two arguments evaluate to the same array; that is, two arrays pointing to the same region of memory.

The serialize function must handle arrays. It should simply return the string "#<array>" for an array.

3.3 Syntax of GUCI4

The concrete syntax of the GUCI4 language with these additional features can be captured with the following EBNF:

  GUCI4 = num
  | true
  | false
  | id
  | {new-array GUCI4 GUCI4}
  | {ref GUCI4[GUCI4]}
  | {GUCI4[GUCI4] <- GUCI4}
  | {id <- GUCI4}
  | {begin GUCI4 GUCI4 ...}
  | {if GUCI4 GUCI4 GUCI4}
  | {with {id = GUCI4} ... GUCI4}
  | {fn {id ...} GUCI4}
  | {operator GUCI4 GUCI4}
  | {GUCI4 GUCI4 ...}

  operator = +
  | -
  | *
  | /
  | eq?
  | <=

... where an id is not true, false, with, if, fn, new-array, =, <-, begin or one of the operator names.

4 Suggested Implementation Strategy

Store-passing style is a bear. Here’s how I’d get started.

First thing, I’d strip down the language from assignment 3. Start in the parser, and comment out everything but binops and numbers. Comment out all of the test cases except those for binops. If you don’t have any tests that just use binops, write some, and make sure they work.

Next, I’d comment out the evaluation rules for everything but numbers and binops in the interpreter. Add an else clause that catches everything else and signals the error "unimplemented". At this point, your binop test cases should still work fine.

Time to add the store! Formulate the define-types and type aliases that you’ll need for stores (just like the book’s). Change the interp function so that it accepts a store, and returns a v*s. Update your test cases so that they pass a store in, and expect the v*s including the answer and the store. Rewrite the interp rules for numbers and binops so that they thread the store through the computation as they should.

With luck and some effort, you should be able to get those binop programs working again.

If this takes you a lot of time and effort, don’t worry: this might be the hardest part of the assignment. Once you get the hang of transforming code into store-passing style, it will get easier. Check to make sure that each store is used exactly once (with the exception of the mutation operations you’ll add later).

Next, I would add the mutation operations. Note that I’m advising you to add these before adding your other language forms (ids, funs, applications, if’s, and booleans) back in. First off, I think I’d add an allocate helper function that accepts a store, a number of locations to be allocated, and a value to place in all of them, and returns two things; the base location, and a new store.

Design your store so that the "next allocated" location is derived directly from the store. It could be a separate counter that’s part of a define-type, or it could be a function that just scans the store to find a new address. Don’t use the new-loc defined by the book; it makes testing quite painful.

Next, I would add a new arrayV value to the set of values. This will require a bunch of extra clauses in various places (serialize, for instance). Note that the representation of arrays is up to you, but it had probably better include a location and a length.

Following this, I would add the new-array operation to the set of expressions, to the interpreter, and to the parser. At this point, you should be able to create arrays, and the result of interpretation should include a store that contains lots of new allocated locations.

At this point, I would go back and add test cases that create arrays as subexpressions of the eq? binop; check that the allocations happen in the right order. As you go forward, you’ll want to use this technique to check order of evaluation for all of your forms.

At this point, it starts to make less difference what order you add language forms in. I think I would probably wait on applications, just because there will be lots of opportunities for mistakes.

5 Suggested Awesome Test Case

This is not a good early test case, but you might want to see if you can write an in-place quicksort routine. You can model recursion as we did in class, by creating a box and then setting the box to contain the closure. If you’re feeling ambitious, you might want to develop a "while" function that accepts a guard procedure and a body procedure and keeps running the body until the guard returns false.

Good luck!

6 Interface

Make sure that you include the following functions, and that they match this interface:

procedure

(parse s)  ExprC

  s : s-expression
Parses an expression.

procedure

(interp e env sto)  V*S

  e : ExprC
  env : Environment
  sto : Store
Interprets an expression, with a given environment.

procedure

(top-eval s)  string

  s : s-expression
Combines parsing and evaluation. Here’s the code you should probably use for this function:

(define (top-eval [s : s-expression]) : string
  (serialize (v*s-v (interp (parse s) empty-env empty-store))))

Your body can be different if you really want, but the types must match these.