CS 334
Programming Languages
Spring 2000

Assignment 7
Due Friday, 4/21/2000


  1. Suppose class B extends class A in Java by adding at least one new method m. Suppose we include the following code in a method of some other class C:
       public void errormethod(){
          A a1 = new A();
          A a2 = new A();
          B b = new B();
          a1 = b;
          b = a2;
       }
    
    Which of these assignments is always guaranteed to be type-safe? Give an example of code which would break if the other assignment were allowed. (Hint: Consider sending messages. )

  2. Suppose Java allowed call-by-reference as a parameter passing mode. Suppose we have classes A and B as above in which B extends A. Give an example in which it could break the type system if an actual parameter that is a variable declared to have class B, were passed in for a formal call-by-reference parameter of class A.

  3. In Java, if B is a subtype of A then Array of B is considered to be a subtype of Array of A. Please explain why this is not type-safe. (Ignore the subscripts, they don't cause problems.) Note that Java makes up for this flaw in the static type rules by putting in a dynamic check to avoid errors. Of course, that makes the program run slower.

  4. I know you would feel cheated if went a week without discussing interpreters for PCF, so we'll start writing an interpreter for PCF in Java this week. Next week you'll actually finish it up. To make life simple I'll have you emulate what we did in ML as closely as possible. The main difference is that the datatype definition (e.g., of term) will turn into an interface, while each of the cases of the datatype definition will turn into a class which implements that interface.

    I would like you to write a series of classes to represent the terms of PCF. These will be represented very much like the type term in your environment interpreter for PCF written in ML. Thus each term will represent an abstract syntax tree in Java. Each tag in the term datatype definition in ML will be represented by a distinct class in Java. If the tag comes with components (e.g. AST_IF has three term components), then it will have that number of instance variables with the corresponding types (e.g., class IfExp will have 3 instance variables, and its constructor will take three parameters, each of which represents a PCF expression).

    The datatype term will be represented by the following interface:

       public interface PCFExpression{
          String getStringRep();
          /** Return a printable representation of the term represented */
       }
    
    Each of the cases of the definition of the datatype term will be represented by a class which implements PDFExpression. Thus there will be classes named IDExp, NumExp,..., FunExp, and AppExp. When a getStringRep() message is sent to one of these expressions, it should return a string representing the original expression.

    For example, suppose we wish to build an object corresponding to the PCF term: if (isZero 3) then 1 else 2. A program to build this might look like:

       PCFExpression three = new NumExp(3);
       PCFExpression isZ = new IsZeroExp();
       PCFExpression cond = new AppExp(isZ,three);
       PCFExpression trueExp = new NumExp(1);
       PCFExpression falseExp = new NumExp(2);
       PCFExpression theTerm = new IFExp(cond,trueExp,falseExp);
       System.out.println(theTerm.getStringRep());
    
    This program would build a representation of the "if" expression. The string printed at the end would look pretty much like the original expression (though it might have extra parentheses).

    In next week's assignment, you will complete this by adding a method called evaluate to the interface and to each of the classes. (To do this you will also need to write classes corresponding to each of the cases of the datatype value of the ML interpreter.) For this week just think about representing the expressions and printing them out.

    You may write this program on either the Macs or the Suns.

    As you can see from the program fragment above, entering a PCF expression is very painful. For extra credit, write a parser for PCF expressions that builds your abstract syntax trees. To see how to do this, look at the parser.sml file that was used with your ML-based interpreters.