CTM: Exercise 3-6
Posted by Urban Hafner
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, soYsis of nonzero length. From the semantics,Y|Rsadds one element toRsand the case statement removes this element fromYs. 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.

