SE 504 Formal Methods and Models
Spring 2009
HW #9: Tail Recursion
Due: Thursday, April 30

1. Let B be a (mathematical) function mapping natural numbers to integers. Devise a pseudo-tail recursive definition for the function Max having the property that, for n >= 0,

Max.n = (MAX i | 0 ≤ i < n : B.i)

For example, Max.4 = B.0 max B.1 max B.2 max B.3.

From that definition, derive a (fully) tail recursive definition for the function Max' having the property that, for any n >= 0,

Max'.n.0 = Max.n

Using either the tail recursive definition of Max' or the pseudo-tail recursive definition of Max, derive a program that computes Max. In your program, use N as the input variable (constant) and y as the output variable. Thus, its postcondition should be y = Max.N (which is equivalent to y = Max'.N.0). Of course, your program may refer to B (e.g., as though it were an array). That is, for example, if k is an integer variable with value at least zero, the expression B.k (or B(k) if you prefer) yields the intended value.


2. Using the notation [v] as shorthand for v ⊕ empty, here is a pseudo-tail recursive definition of the list reversal function:

rev.x = { empty                    if x = empty
        { rev.(tail.x) | [head.x]  otherwise
Note that, in this definition, the recursive application of the function is within the left operand of the main operator (|), not the right operand.

From this derive a tail recursive definition of rev' such that rev'.x.empty = rev.x. Then show the corresponding program, whose input variable (constant) is X and whose postcondition is rev.X = y.