Example. Verification conditions in while loops [004n]
Example. Verification conditions in while loops [004n]
Let's consider the following while loop:
@pre x <= 0
while [x <= 6] (x <= 5) do
x := x + 1
@post x = 6
if we denote the loop and its body as \(W\), then it corresponds to the following hoare triple:
\[ \{x \leq 0\}\ W\ \{x = 6\} \]With the addition of the invariant \(I \triangleq x \leq 6\), now the verification condition for the loop is expressed as:
\[ \begin {align} \texttt {vc}(W [I], x = 6) &= \forall \sigma .\ I(\sigma ) \land (b \Downarrow ^t \sigma ) \to \texttt {wp}(W [I], s, I(\sigma )) \\ &\land \forall \sigma .\ I(\sigma ) \land (b \Downarrow ^f \sigma ) \to Q(\sigma ) \\ &\land \texttt {vc}(s, I) \end {align} \]starting with the first conjunct with have:
\[ \forall \sigma , x \leq 6 \to (x \leq 5 \Downarrow ^t \sigma ) \to \texttt {wp}(x := x + 1, (x \leq 6)) \]After we unfold the \(\texttt {wp}\) our goal reduces to:
\[ x \mapsto x + 1 \leq 6 \equiv x \leq 5 \]And we clearly know this is true just by inversion of the evaluation of the loop guard. Moving on to the second conjunct:
\[ \forall \sigma .\ x \leq 6 \land (x \leq 5 \Downarrow ^f \sigma ) \to x = 6 \]This represents our termination condition, here we know by inversion of the loop guard again that the premise of our implication becomes:
\[ x \leq 6 \land x > 5 \to x = 6 \]Our final conjunct is the verification condition on the assignment, so:
\[ \texttt {vc}(x := x + 1, x \leq 6) \]Unfolding the call again we know this just resolves to \(\top \) as there are no verification conditions for assignment.
Finally we also have to demonstrate that:
\[ (x \leq 0 \land x = x_0) \to \texttt {wp}(W [I], Q) \]unfolding the \(\texttt {wp}\) again gives
\[ (x \leq 0 \land x = x_0) \to x \leq 6 \]Clearly we can see this trivially holds, i.e. our precondition is stronger than the invariant, hence by precondition strengthening, it is valid.