I am not sure that
{ true } x := y { x = y }
is a valid Hoare triple.
The triple should be read as follows:
"Regardless of starting state, after executing x:=y
x equals y."
and it does hold. The formal argument for why it holds is that
- the weakest precondition of
x := y
given postcondition { x = y }
is { y = y }
, and
{ true }
implies { y = y }
.
However, I completely understand why you feel uneasy about this triple, and you're worried for a good reason!
The triple is badly formulated because the pre- and post condition do not provide a useful specification. Why? Because (as you've discovered) x := 0; y := 0
also satisfies the spec, since x = y
holds after execution.
Clearly, x := 0; y := 0
is not a very useful implementation and the reason why it still satisfies the specification, is (according to me) due to a specification bug.
How to fix this:
The "correct" way of expressing the specification is to make sure the specification is self contained by using some meta variables that the program can't possible access (x₀
and y₀
in this case):
{ x=x₀ ∧ y=y₀ } x := y { x=y₀ ∧ y=y₀ }
Here x := 0; y := 0
no longer satisfies the post condition.