next up previous
Next: 4. Multiple Superclasses: To Up: No Title Previous: 2. The TOOL Interpreter

3. Programming assignment

When you load the code for this problem set, the entire TOOL interpreter code (attached) will be loaded into Scheme. However, in order to do the programming assignment, you will need to modify only a tiny bit of the interpreter. This code has been separated out in the file ps10-mod.scm, so you can edit it conveniently. (You may also need to define some auxiliary procedures.)

To start the TOOL interpreter, type (initialize-tool). This initializes the global environment and starts the read-eval-print loop. To evaluate a TOOL expression, type the expression after the prompt, followed by CTRL-x CTRL-e.

In order to keep the TOOL interpreter simple, we have not provided any mechanism for handling errors. Any error (such as an unbound variable) will bounce you back into Scheme's error handler. To get back to TOOL, quit out of the error and restart the driver loop by typing (driver-loop). If you make an error that requires reinitializing the environment, you can rerun initialize-tool, but this will make you lose any new classes, generic functions, or methods you have defined.

Computer exercise 1:

Show how to implement two-dimensional vector arithmetic in TOOL by extending the generic functions + and *, which are already predefined to work on numbers. Define a class <vector> with slots xcor and ycor. Arithmetic should be defined so that adding two vectors produces the vector sum, and multiplying two vectors produces the dot product
displaymath311
Multiplying a number times a vector, or a vector times a number, should scale the vector by the number. Adding a vector plus a number is not defined. Also define a generic function length, such that the length of a vector is its length and the length of a number is its absolute value. Also, add a print method for vectors so that TOOL will print vectors showing their xcor and ycor. Turn in your definitions and a brief interaction showing that they work. Here are some useful definitions (not included in the problem set code) that illustrate one way to get started:

(define-method - ((x <number>))
  (- 0 x))

(define-generic-function square)
(define-method square ((x <object>))
  (* x x))
Check the 6.001 discussion forum for computer-ex-01
Look here for information about the forum.

Computer exercise 2:

(This is really an exercise in reading code, not programming.) Towards the end of section 1 of this problem set, there was an example showing the result of evaluating the expression (say socks 37). Explain how apply-generic-function finds the correct say method when we ask the cat socks to say a number. In particular, what are all the applicable methods? In what order will they appear after they are sorted according to method-more-specific?
Check the 6.001 discussion forum for computer-ex-02
Look here for information about the forum.

Computer exercise 3:

One annoying thing about TOOL is that if you define a method before you've defined a generic function for that method, you will get an error. For example, in the first example in section 1, we had to explicitly evaluate

(define-generic-function 4-legged?)
before we could evaluate
(define-method 4-legged? ((thing <object>))
  'Who-knows?)
If we hadn't done this, the second expression would have given the error that 4-legged? is undefined. Modify the TOOL interpreter so that, if the user attempts to define a method for a generic function that does not yet exist, TOOL will first automatically define the generic function. One thing to consider: In which environment should the name of the generic function be bound: the global environment, the environment of the evaluation? some other environment? There is no ``right answer'' to this question-- you are the language designer. But whatever choice you make, write a brief paragraph justifying your choice. In particular, include an example of a program for which the choice of environment matters, i.e., where the program would have a different behavior (or perhaps give an error) if the choice were different. (Hint: The only procedure you should need to modify for this exercise is eval-define-method.) Turn in, along with your design justification, your modified code together with a brief interaction showing that the modified interpreter works as intended.)
Check the 6.001 discussion forum for computer-ex-03
Look here for information about the forum.

Computer exercise 4:

Another inconvenience in TOOL is that we need to use get-slot in order to obtain slot values. It would be more convenient to have TOOL automatically define selectors for slots. For example, it would be nice to be able to get the x and y coordinates of a vector by typing (xcor v) and (ycor v) rather than (get-slot v 'xcor) and (get-slot v 'ycor). Modify the interpreter to do this. Namely, whenever a class is defined, TOOL should automatically define a generic function for each of its slot names, together with a method that returns the corresponding slot value for arguments of that class. Turn in a listing of your code and an example showing that it works. (Hint: The only part of interpreter you need to modify for this exercise is eval-define-class.)
Check the 6.001 discussion forum for computer-ex-04
Look here for information about the forum.

Lab exercise 5:

Give some simple example of defining some objects and methods (besides cats and vectors) that involve subclasses, superclasses, and methods, and which illustrate the modifications you made in exercises 3 and 4.
Check the 6.001 discussion forum for computer-ex-05
Look here for information about the forum.


next up previous
Next: 4. Multiple Superclasses: To Up: No Title Previous: 2. The TOOL Interpreter

Hal Abelson
Sat Apr 11 16:28:40 EDT 1998