CS 334
|
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. )
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.