Princeton University
Computer Science Dept.

Computer Science 441
Programming Languages
Fall 1998

Assignment 6
Due 11/10/98


Please do problem 19 on page 243 of the text, but do not turn it in. The answer is in the back of the text.

  1. Please do problem 15 on page 241 of the text.

  2. Please do problem 20, parts a and b only, on page 243 of the text.

  3. As hinted in class, the THUNK's used to support recursion can also provide direct support for call-by-name parameter passing. Please modify the environment-based interpreter to use THUNK's to support call-by-name rather than call-by-value parameter passing. If you don't like your own, you can use mine, which can be found in ~kb/COS441/ML/interps/Envinterp.sml)

    When you evaluate a function call (with a user-defined function), you should not evaluate the parameter, instead package the (unevaluated) actual parameter and the current environment into a NameThunk and insert it into the environment in the same way that you used to insert the value of the actual parameter into the environment. Make sure that the THUNK is handled properly when its value is needed. (Hint: Depending on how you handled THUNK's before you may not need to make any changes to that part of your code.)

    To test your interpreter, execute a program that will blow up or not terminate with call by value, but give an answer with call-by-name. Be sure to turn in the program that you used to test it (though you can turn in an informal description if you'd like).

  4. Please read Sections 8.1 - 8.3 and 8.5 of Ullman. ML's structures provide encapsulation, signatures take the place of types for structures and can support information hiding, while functors are parameterized structures. The following abstype definition of a generic stack was given in class.
    	abstype 'a stack = mkstack of ('a list)
    	  with exception stackUnderflow
    	    val emptyStk : 'a stack = mkstack []
     	   fun push (e:'a) (mkstack(s):'a stack) = mkstack(e::s)
     	   fun pop (mkstack([]):'a stack) = raise stackUnderflow
     	     | pop (mkstack(e::s):'a stack) = mkstack(s):'a stack
     	   fun top (mkstack([]):'a stack) = raise stackUnderflow
     	     | top (mkstack(e::s):'a stack) = e
     	   fun IsEmpty (mkstack([]):'a stack) = true
     	     | IsEmpty (mkstack(e::s):'a stack) = false
     	  end;
    1. Please write an equivalent structure definition and supply an appropriate signature for it. Please make sure that the implementation of stack is hidden to the user, but be sure that the exception is visible. (You should test the construct with your "balance" program from assn 2.)

    2. Compare the ADT mechanisms of Ada and ML (for ML consider structures, signatures, and functors). How do they differ? Does either have any advantages over the other?