SE 504 Spring 2019
HW #5: Strengthening the Invariant
Due: 7pm, Monday, April 1

1. (Polynomial Evaluation)
Develop a program that, given as input a real number X and an array A containing the coefficients of a polynomial, evaluates that polynomial at X, placing the result into y. That is, the program should establish

y   =   a0   +   a1 × X   +   a2 × X2   +   ... +   aN × XN

where N = #A-1 and, for all i, ai = A.i.

Some of the development has already been done. In particular, the loop's invariant and guard were obtained using the replace a constant by a fresh variable heuristic (and putting the obvious bounds upon the fresh variable). (Specifically, #A was replaced by fresh variable n.)

What remains is to determine the right-hand sides of a few assignment commands. You are to assume, in doing so, that the programming language has no exponentiation operator. With this restriction, you should find that applying the strengthen the loop invariant heuristic is useful in attempting to calculate E. Show this. Be smart about the new conjunct that you introduce into the loop invariant. It should be simple to preserve/re-establish during each loop iteration.

|[ con X : real;
   con A : array of real;
   var y : real;
   var n : int;

   y,n := F, G;

   {loop invariant I: y = (+j | 0≤j<n : A.j × Xj) ∧ 0≤n≤#A}
   do n ≠ #A ⟶
      {I ∧ n≠#A}
      y := E;
      {I(n:=n+1)}
      n := n+1;
      {I}
   od
   {Q: y = (+j | 0≤j<#A : A.j × Xj)}
]|


2. (Recursive Function Evaluation)
Define function h : ℕ → ℤ as follows: h.0 = 0, h.1 = 1, and h.(n+2) = h.n - h.(n+1) (for n≥0).

You should find the sequence ⟨h.0, h.1, h.2, ...⟩ to be an interesting variation of a familiar sequence.

Derive a solution (and provide an accompanying narrative) to the following programming problem. Not surprisingly, your program should include a loop, the invariant of which you should derive by employing the usual heuristics. During development, you should find it useful to strengthen the loop invariant by introducing a fresh variable.

Notice that the precondition says N>0. Try to weaken that to N≥0 without having to make your program more complicated. Explain how you did it.

|[ con N : int;  { N>0 }
   var y : int;

   y := ?;

   {Q: y = h.N }
]|


3. Derive a solution (and provide an accompanying narrative) to the following programming problem:

|[ con B : array of int;  { #B > 0 }
   var z : int;

   z := ? 

   {Q: z = (MAX p,q | 0≤p≤q<#B : B.q - B.p) }
]|

The methods used in deriving the program for the Maximum Segment Sum problem (see relevant web page) will be useful here. In particular, notice the step in the derivation of E in which either (8.16) or (8.18) (two versions of range split) is applied, followed by the (combined) step in which (8.20), (8.14), and (8.20) (again) are applied.

Also note that subtraction distributes over min in a particular way, producing a max. Specifically,

u - (v1 min v2 min ... min vk) = (u - v1) max (u - v2) max ... max (u - vk)

In terms of quantification, this can be stated as

Theorem (Subtraction Distributes over min to produce max):

Provided that x does not occur free in U, U - (MIN x | R : V) = (MAX x | R : U - V)