CTM: Exercise 3-6

Posted by Urban Hafner Wed, 14 Sep 2005 19:25:26 GMT

The problem

State invariants. Write down a state invariant for the IterReverse function.

My solution

Define the state invariant P as follows:

P((Rs,Ys)) = (reverse(Xs) = reverse(Ys)|Rs)

  • The proof for P_(_S0~) follows from S0~ = (nil,Xs).
  • Assuming P_(_Si~) and S_i~ is not the final state, prove _P(S_i+1). This follows from the semantics of the case statement and the function call. Write _Si~ = (Rs,Ys). We are not in the final state, so Ys is of nonzero length. From the semantics, Y|Rs adds one element to Rs and the case statement removes this element from Ys. Therefore P_(_Si+1~) holds.

As you might have noticed I’ve used the proof for IterLength and just changed the bits concerning the state invariant. This is OK because the two functions are nearly the same.

Tags , , , , , ,

Comments are disabled