Assignment 5
Due 4/8/97

  1. In early FORTRANs (i.e., I to 77) there was usually no checking of code for recursive calls. Instead at execution time the recursive call was made, but the program later fell into an infinite loop. Based on your knowledge of the implementation of subroutine calls in FORTRAN, explain why this happens. Design a mechanism which would allow the program to detect such recursive calls and abort. Your mechanism should also detect mutually recursive or indirectly recursive calls. (It is sufficient to have a run-time rather than compile-time mechanism.)

  2. So problem 23 on page 244 of Louden.

  3. A block is like a procedure body which occurs in-line in a program. That is, it is composed of a series of declarations followed by some executable code. However it may not be called like a procedure, it is only executed when the program counter enters the first statement of the block. Please explain why the static and dynamic links have the same values for blocks.

  4. Please do problem 26 on page 246 of the text.

  5. In class we discussed the use of a run-time stack of activation records for procedure calls in block-structured languages. In the interpreter you wrote last week there was no explicit use of a run-time stack. Your (recursive) interpreter for PCF provides similar support for activation records (though PCF is simpler than block-structured languages because there are no local variables and only one parameter for each function call). Explain how your interpreter provides a stack-like support for activation records by explaining how the environment changes during the evaluation of the following expression:

    	((if is_zero(((fn y => pred y)1)) then succ else pred)
    		((fn y => (pred(pred y)))8))
  6. The function string_to_num defined below uses two auxiliary functions to convert a string of digits into a non-negative integer.

    	fun char_to_num c = ord c - ord "0";
    	fun calc_list ([],n) = n
    	  | calc_list ((fst::rest),n) = 
    	                     calc_list(rest,10 * n + char_to_num fst);
    	fun string_to_num s = calc_list(explode s, 0);
    For instance, string_to_num "3405" returns the integer, 3405. Unfortunately, calc_list returns a spurious result if the string contains any non-digits. For instance, string_to_num "3a05" returns 7905, while string_to_num " 405" returns ~15595. This occurs because char_to_num will convert any character, not just digits. We can attempt to fix this by having char_to_num raise an exception if it is applied to a non-digit.

    a. Please revise the definition of char_to_num to raise this exception, and then modify the function string_to_num so that it handles the exception, returning ~1 if there is a non-digit in the string. You should make no changes to calc_list.

    b. Please rewrite these ML functions to provide the same behavior (including returning ~1 if the string includes a non-digit) as in (a), but without using exceptions. While you may change any function, try to preserve as much of the structure of the original program as possible.