Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is {true} x := y { x = y } a valid Hoare triple?

I am not sure that

{ true } x := y { x = y }

is a valid Hoare triple.

I am not sure one is allowed to reference a variable (in this case, y), without explicitly defining it first either in the triple program body or in the pre-condition.

{ y=1 } x := y { x = y } //valid

{true} y := 1; x := y { x = y } //valid

How is it?

like image 591
devoured elysium Avatar asked Oct 02 '11 20:10

devoured elysium


People also ask

What is a valid Hoare triple?

A Hoare triple has three parts, a precondition P, a program statement or series of statements S, and a postcondition Q. It's usually written in the form. {P} S {Q} The meaning is "if P is true before S is executed, and if the execution of S terminates, then Q is true afterwards".

How do you write Triple Hoare?

{{X = 0}} X ::= X + 1 {{X = 1}} is a valid Hoare triple, stating that command X ::= X + 1 would transform a state in which X = 0 to a state in which X = 1.

What is the strongest postcondition?

The strongest postcondition possible is x = 10; this is the most useful postcondition. Formally, if {P} S {Q} and for all Q such that {P} S {Q}, Q ⇒ Q, then Q is the strongest postcondition of S with respect to P.

What happens if precondition is false?

If precondition P is false, then the Hoare-triple guarantees nothing about execution, so execution of S can do anything! It can get into an infinite loop; it can stop immediately, with no guarantees about what state it is in; it can abort in some fashion, perhaps by dividing by 0.


2 Answers

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

  1. the weakest precondition of x := y given postcondition { x = y } is { y = y }, and
  2. { 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.

like image 81
aioobe Avatar answered Oct 12 '22 04:10

aioobe


{ true } x := y { x = y } is a valid Hoare triple. The reason is as follows:

x := y is an assignment, therefore, replace that in the precondition.
The precondition stands as {y=y}, which implies {true}.

In other words, {y=y} => {true}.

like image 33
Swarnendu Biswas Avatar answered Oct 12 '22 03:10

Swarnendu Biswas