CS 334: HW 6

Instructions

This homework has two types of problems:

  • Self Check: You are strongly encouraged to think about and work through these questions, but you will not submit answers to them.

  • Problems: You will turn in answers to these questions.

Reading

  • (Required) Read Mitchell, Chapter 8.1---8.2.

  • (Required) Read Mitchell, Chapter 9.1---9.2.

  • (Required) Mitchell, Chapter 10.

Problems

1. Exceptions (10 points)

  1. Consider the following functions, written in ML:

    exception Excpt of int;
    fun twice(f,x) = f(f(x)) handle Excpt(x) => x;
    fun pred(x) = if x = 0 then raise Excpt(x) else x-1;
    fun dumb(x) = raise Excpt(x);
    fun smart(x) = (1 + pred(x)) handle Excpt(x) => 1;
    
  2. What is the result of evaluating each of the following expressions?

    1. twice(pred,1);

    2. twice(dumb,1);

    3. twice(smart,0);

    In each case, be sure to describe which exception gets raised and where.

2. Expression Objects (15 points)

We now look at an object-oriented way of representing arithmetic expressions given by the grammar

\begin{array}{lll} e & ::= & \mathit{num} \;|\; e + e \\ \end{array}

We begin with an "abstract class" called SimpleExpr. While this class has no instances, it lists the operations common to all instances of this class or subclasses. In this case, it is just a single method to return the value of the expression.

abstract class SimpleExpr {
    abstract int eval();
}

Since the grammar gives two cases, we have two subclasses of SimpleExpr, one for numbers and one for sums.

class Number extends SimpleExpr {
    int n;
    public Number(int n) { this.n = n; }
    int eval() { return n; }
}

class Sum extends SimpleExpr {
    SimplExpr left, right;
    public Sum(SimpleExpr left, SimpleExpr right) { 
        this.left = left; 
        this.right = right;
    }
    int eval() { return left.eval() + right.eval(); }
}
Questions
  1. Product Class: Extend this class hierarchy by writing a Times class to represent product expressions of the form

    \begin{array}{lll} e & ::= & \ldots \;|\; e * e \end{array}
  2. Method Calls: Suppose we construct a compound expression by

    SimpleExpr a = new Number(3);
    SimpleExpr b = new Number(5);
    SimpleExpr c = new Number(7);
    SimpleExpr d = new Sum(a,b);
    SimpleExpr e = new Times(d,c);
    

    and send the message eval to e. Explain the sequence of calls that are used to compute the value of this expression: e.eval(). What value is returned?

  3. Comparison to "Type Case" constructs: Let's compare this programming technique to the expression representation used in ML, in which we declared a datatype and defined functions on that datatype by pattern matching. The following eval function is one form of a "type case" operation, in which the program inspects the actual tag (or type) of a value being manipulated and executes different code for the different cases:

    datatype MLExpr = 
        Number   of int
      | Sum      of MLExpr * MLExpr;
    
    fun eval (Number(x)) = x
      | eval (Sum(e1,e2)) = eval(e1) + eval(e2);
    

    This idiom also comes up in class hierarchies or collections of structures where the programmer has included a Tag field in each definition that encodes the actual type of an object.

    1. Discuss, from the point of view of someone maintaining and modifying code, the differences between adding the Times class to the object-oriented version and adding a Times constructor to the MLExpr datatype. In particular, what do you need to add/change in each of the programs. Generalize your observation to programs containing several operations over the arithmetic expressions, and not just eval.

    2. Discuss the differences between adding a new operation, such as toString, to each way of representing expressions. Assume you have already added the product representation so that there is more than one class with a nontrivial eval method.

Submitting Your Work

Submit your answers to the GradeScope assignment named, for example, "HW 0". It should:

  • be clearly written or typed,
  • include your name and HW number at the top,
  • list any students with whom you discussed the problems, and
  • be a single PDF file, with one problem per page.

You will be asked to resubmit homework not satisfying these requirements. Please select the pages for each question when you submit.