Assignment 8 Due 5/6/97

1. Eiffel allows you to change the type of an instance variable in a subclass to a type which is a subtype of that declared in the supertype. In particular, you probably did this in the answer to 1e with treeRoot. Thus if class A has instance variable x: C, then it is legal in Eiffel for subclass B to redeclare x: D, where D is a subclass of C. Give an example of a class A and subclass B such that when the type of instance variable x is changed as above from a class C to a subclass D, a type error results.
Hint: Let m be a method of A which assigns a value of type C to x. Show what can go wrong in B when m is inherited and the type of x changes.

2. Please add to my parser program the capacity to recognize terms involving the unary minus sign "~" (as in ML). Thus ~ 5 + 7 = (~ 5) + 7 = 2. Revise the production for expressions to read:

        <expression> ::= <term> { ("+" | "-") <term> }
        <term> ::= <factor> { ("*" | "/") <factor> }
        <factor> ::= <oldfactor> | "~" <oldfactor>
        <oldfactor> ::= <Constant> | "(" <expression> ")"
This differs from the grammar actually implemented by the replacement of the production for <factor> and by renaming the former <factor> production to <oldfactor>. Thus ~(5 * 7) + ~ 3 results in the following parse tree:

Please update the parser handed out in class (on-line in ~kim/cs334stuff/Eiffel/parser) to handle this case. You will need to add a new subclass of ExpNode to represent unary minus nodes. You will also have to add a new token for UnaryMinus to TOKENS, update SCANNER so that it recognizes "~" and registers the proper token, and you must update the old factor class and add a new one to handle the unary minus. Feel free to modify TOKENS, SCANNER, and FACTOR directly rather than creating subclasses of those in my program.

3. Variables can be said to have both L-values and R-values, where the L-value of a variable is the location it denotes and the R-value is the value stored in that location. The environment keeps track of the locations corresponding to variables, while the store (or state) keeps track of the values stored in locations. Recall from an earlier problem set that the semantics of the assignment statement for most programming languages is usually given as follows: When V := E is executed, the L-value of V is first obtained from the environment (call it l), then the expression E is evaluated, and that resulting value is stored in the location l. While side-effects can create great confusion in terms of what the result of an assignment statement will be; in this problem, side-effects are not the problem!

a) Describe in detail the effect of executing the Pascal assignment

		a[a[i]] := a[i] + 1
b) Is it always the case (in Pascal) that the value of L after executing assignment L := E is equal to the value of E before executing the command? I.e. if you execute the following code:
		write(E); L := E; writeln(L);
will the two values printed always be the same? For the purposes of this problem assume that the evaluation of E has no side effects. Hint : consider the assignment in part (a) with i = 2, a[2] = 2, a[3] = 0.

c) What does this tell you about the validity of the axiomatic rule for assignment,{P [E / L]} L := E {P}, (e.g., if L = a[a[i]], E = a[i] + 1, and P is a[a[i]] = 3)? Suggest a restriction which makes it valid.

4. Use the formal rules for axiomatic semantics given in the lecture notes to prove the correctness of the following program:

{Precondition: n > 0}

	i <- n
	fact <- 1
	while i > 0 do
		{assert:  ...}
		fact <- fact * i
		i <- i - 1
	end while
	{Postcondition:	fact = 1*2*...*n}
Hint: You need to figure out the loop invariant before you can complete the proof. Your proof should take the same form as the one in the lecture notes - hand-waving is NOT acceptable.

Note: If you prefer to use the weakest precondition rules in the text, be my guest, but I suspect you will find it easier to use those given in class instead

5. You have your choice of several great object-oriented programs here. Do whichever sounds the most interesting to you. The first one is likely the easiest. Turn in paper and electronic copies of all parts of your program (just the Ace and program files, not the compiled code!). Warning: All of these are major projects and will be worth 50 points rather than the usual 10 points. They cannot be written in over the course of three or four nights, especially since they will be your first substantial programs in Eiffel. As a result I will give you two weeks to work on this rather than one. Be aware however, that there will be more problems added next week which will be due at the same time as this one.

The options are to write the animals game, a PCF interpreter, or the core of an adventure game in Eiffel. Since the adventure game is likely the biggest project, you may work on it in teams. Here are the descriptions:

a. Write a program to play the game of animals. In this game the user thinks of an animal and the computer attempts to guess the answer. The user should decide whether to start a new game or to continue an old game. If it is a new game then the computer may only know one question and two possible answers, while if it is an old game the program should read in a file of questions and answers. The computer keeps its database of questions and answers as a binary tree (e.g., with left branches as what to do if the answer is "yes" and right for "no" answers). Each time the computer asks the user a question, the user should answer with either yes or no. Depending on the answer the computer may ask another question or guess the animal. If the computer guesses incorrectly, it should "learn" the correct answer, by asking the user for a question which distinguishes the guessed answer and the correct answer, and then adding the question to the tree. When the user quite the computer should ask if the game should be saved. Please use separate classes for questions and answers. You will find the class for strings to be quite helpful. (Its description can be found either on-line in the Eiffel class library or in the Library manual in the Sun lab.)

b. Rewrite your environment-based (call-by-value) PCF interpreter in Eiffel. You will need classes for trees and values and will need a way to hold environments (lists or binary search trees might be a good idea). Use the "parser" program handed out as a guide for how to do the parsing and evaluation of expressions. The evaluation of terms will follow the same rules as your ML interpreter. Test your program by providing a main program that will evaluate a sample PCF term.

c. You are to write a Williams Adventure game. The idea is that a character will wander around campus performing various actions until the user types "quit". The idea is that there are three basic classes of objects: Persons, Places, and Things. A person wanders about through places, picking up and dropping things. A brief description of the classes is as follows.

All classes of objects should have a name, and be able to report their name on request. When created, their name should be assigned (presumably it won't be changed once created).

Things may have a possessor, which is a person. It should be possible to report and change these values (e.g., when an object is either picked up or dropped).

Persons have a list of possessions (things), a location, and should be able to report their list of possessions, report and reset their locations, move in a particular direction (i.e., north, east, south, or west), pick up or drop a thing which is in the same location, and to look around (which should elicit a brief description of where you are, including all objects in the location).

Places are the most complex. They should keep a list of objects currently in the location, a list of neighbors (which is probably most easily held as an array, but a "linked list" is fine), and a list of people in the location. It must be possible to add and remove items from the objects and people lists.

As you can tell from the above description, you will need to create a generic list module that can add and delete items from a given list. You may create your own or use the one given in the Eiffel structures library.

Have fun with this! Be sure to provide the user with instructions as it begins. It is sufficient to write a program that just has a character wander through a few places on campus, picking up and dropping objects and depositing them some place to win. However, I urge you to use your creativity and make this the core of a wonderful adventure. Feel free to have marauding Deans attack partying students, have homework chewed up by crashing computers, cars hitting students in crosswalks, etc. All it should take is adding extra methods to each of these classes, or making up and adding new classes.

A last piece of advice. Get something very basic going first, then add bells and whistles to make it more fun. The best way to start is to do an initial design of the main program, figure out what you need each object to be able to do, and then start implementing the objects. A good way to start is just to initialize the collection of locations and let a character walk from room to room until the user types quit.

Turn in paper and electronic copies of all parts of your program.