next up previous
Next: About this document Up: No Title Previous: 3. Programming assignment

4. Multiple Superclasses: To do AFTER you are done programming

This final question asks you to consider a tricky issue in language design. We are not requiring you to actually implement your design. Nevertheless, we do expect you to think carefully about the issues involved and to give a careful description of the solution you come up with. Don't think that this is a straightforward exercise--designers of object-oriented languages are still arguing about it.

The major way in which TOOL is simpler than other object-oriented languages is that each class has only one immediate superclass. As illustrated with message-passing systems (lecture on March 19), there are cases where it is convenient to have a class inherit behavior from more than one kind of class.gif

This will involve some changes to TOOL. As a start, the syntax for define-class must be modified to accept a list of superclasses rather than a single superclass. Let's assume that define-class now takes a list of superclasses. For instance, going back to our original example about cats, we might have:

(define-class <fancy-house-cat> (<house-cat> <show-cat>))

This new class should inherit from both <house-cat> and <show-cat>. In general, when the new class is constructed, it should inherit methods and slots from all its superclasses (and their ancestors).

However, it's not obvious what inheritance should mean. For example, suppose we have a generic function eat and we define methods as follows:

(define-method eat ((c <house-cat>))
   (print '(yum: I'm hungry)))

(define-method eat ((c <show-cat>))
   (print '(I eat only caviar)))

What should happen when we ask a fancy-house-cat (which is both a show-cat and a house-cat) to eat? More generally, what is the ``most specific method'' that should be used when a generic function is applied to its arguments, given that some of the arguments may have multiple superclasses? What are the new kinds of choices that arise? How should the language give the user the ability to control these choices? (Or maybe it shouldn't give the user this level of control.)

Post-computer exercise 6:

You are now a language designer. Your task is to design an extension to TOOL so that it handles classes with multiple superclasses. Three of the issues you have to deal with are: (a) What should be the syntax for defining classes? (b) What slots does a class get when it is defined? (c) How is a method chosen when a generic function is applied to its arguments? Prepare a design writeup that has three parts:
Check the 6.001 discussion forum for computer-ex-06
Look here for information about the forum.

  1. Write a clear 2-3 page description of your language extension. This description should be geared toward the user of the language. It should include a simple, but realistic and non-trivial example of a program that involves multiple superclasses. The example should illustrate how your language handles each of the three issues (a), (b), and (c). You should also explain how the language deals with each of these issues in general.
  2. For each of design choices you illustrated in part 1, give an alternative choice you could have made, and explain briefly why you think your choice is better. If you can't think of any other choice you might have made, then say so.
  3. As carefully as you can (but without actually writing any code) specify the procedure that the evaluator should follow in choosing which method to select when applying a generic function to a given set of arguments. Your description should be clear enough so that someone could implement this procedure based upon your specification.

Optional extra credit

Implement your design for multiple superclasses in TOOL and demonstrate that it works. The TOOL interpreter was designed to make this not too difficult, but it will involve a considerable number of small changes to the code and is likely to be time-consuming.


next up previous
Next: About this document Up: No Title Previous: 3. Programming assignment

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