SE 504 Spring 2022
HW #5: Tail Recursion
Due: 6:30pm, Thursday, March 21

1. Consider this tail recursive definition of the (two-argument) function H : ℕ × ℕ → ℕ (where ℕ denotes the set of nonnegative integers):

H.m.n = { n   if m = 0
H.(m−1).(n+1)    otherwise (i.e., m ≠ 0)

Using this definition, follow the standard procedure (as covered in lecture) by which to derive a program that satisfies the specification below (in which M and N are the inputs). (Make sure that your program is appropriately annotated to include a loop invariant and bound function.) That M and N are declared to be of type nat means that M≥0 ∧ N≥0 is an implicit precondition.

If you don't recognize the function H upon first glance at its definition, you should recognize it sometime thereafter.

|[ con M, N : nat; 
   var k : nat;

   k := ?

   {Q: k = H.M.N }
]|


2. Consider this pseudo-tail recursive function definition, which describes a function having signature ℕ ⟶ ℕ (where ℕ denotes the set of nonnegative integers).

G.n = { 0    if n = 0
n + G.(n−1)    otherwise (i.e., n ≠ 0)

From this definition, use the procedure covered in class by which to derive a program that computes the function. That is, complete the program specified below, in which N is to be interpreted as being the input. (Make sure that your program is appropriately annotated to include a loop invariant and bound function.)

As an intermediate step, you could derive from the pseudo-tail recursive definition of G a (fully) tail recursive definition for function G' such that (for every positive integer n) G'.n.e = G.n, where e is the appropriate identity element.

If you don't recognize the function G upon first glance at its definition, you should recognize it sometime thereafter.

|[ con N : nat; 
   var k : nat;

   k := ?

   {Q: k = G.N }
]|


3. Consider this pseudo-tail recursive function definition of Rev : list<T> → list<T>, which uses the list notation introduced in a previous homework. That notation is enhanced here with the use of the function [] : T ⟶ List<T>, defined by

[b] = b ⊕ empty

In effect, [b] serves as a convenient abbreviation for describing a one-element list whose only element is b:T.

Note: Do not confuse the ⊕ operator as used immediately above (and in the previous homework) with the "generic" ⊕ operator introduced in the discussion of pseudo-tail recursive function definitions. End of note.

Rev.x = { empty   if x = empty
Rev.(tail.x)  |  [head.x]    otherwise

Using this definition, follow the standard procedure (as covered in lecture) by which to derive a program that satisfies the specification below (in which T stands for an arbitary data type). (Make sure that your program is appropriately annotated to include a loop invariant and bound function.)

As an intermediate step, from the given definition of function Rev you can derive a (fully) tail recursive definition for function Rev' such that (for all lists x) Rev'.x.empty = Rev.x.

Notice that in the recursive case of the definition, the application of Rev is the left operand of the concatenate (or append, if you prefer) operator, not the right operand. This is the opposite of the template that we covered in lecture, which means that the roles being played by the two operands here are reversed, which you must take account of.

|[ con X : list<T>;
   var z : list<T>;

   z := ?

   {Q': z = Rev'.X.empty }
   {Q: z = Rev.X }
]|


4. Consider the function valueOf : List<Digit> ⟶ ℕ that maps a list of digits (i.e., natural numbers in the range [0..10)) to the value it represents when interpreted as a decimal numeral. For example,

valueOf.([3] | [7] | [4] | [2]) = 3742

Hint: As an example to illustrate a broader generality, notice that

3742
=10·374 + 2
=10·(10·37 + 4) + 2
=10·(10·(10·3 + 7) + 4) + 2
=10·(10·(10·(10·0 + 3) + 7) + 4) + 2
3742
=2 + 10·374
=2 + 10·(4 + 10·37)
=2 + 10·(4 + 10·(7 + 10·3))
=2 + 10·(4 + 10·(7 + 10·(3 + 10·0)))

The two columns show exactly the same thing; both are shown because one or the other may provide more insight, depending upon the reader.
End of Hint.

Devise a tail recursive definition for the function valueOf' : List<Digit> × ℕ ⟶ ℕ, a two-argument generalization of valueOf such that valueOf.z = valueOf'.z.0. The second argument plays the role of the accumulating parameter.

The function definition can refer to the standard list primitives, including head and tail, as well as arithmetic operators, of course.