I'm unsure about the value of x
in this Hoare triple: { a = 0 } while (x > a) do (x := x − 1) { x = 0 }
.
I have 2 potential ideas for how to prove whether this Hoare triple is valid or not:
- Assuming
x
is 0, the Hoare triple is valid, or - Assuming
x
is any arbitrary value, we break it down into cases and conclude that the Hoare triple is not valid for all values ofx
Are either of the above approaches valid, or is there another approach I should take?
So you have
Let's try the loop invariant
x ≥ a & a = 0
and let's abbreviate it withI
. When we annotate the program, we get:Now we need to apply the weakest precondition to
x := x - 1
:We end up with the following proof obligations:
(a = 0) ⇒ (x ≥ a & a = 0)
holds, sincex ∈ ℕ
(x ≥ a & a = 0) ⇒ (x - 1 > a & a = 0)
, holds. Proof trivial.(x ≥ a & a = 0 & x ≤ a) ⇒ (x = 0)
holds. Proof trivial.So the original Hoare triple holds.