Hoare logic explained

Hoare logic (also known as Floyd - Hoare logic or Hoare rules) is a formal system with a set of logical rules for reasoning rigorously about the correctness of computer programs. It was proposed in 1969 by the British computer scientist and logician Tony Hoare, and subsequently refined by Hoare and other researchers.[1] The original ideas were seeded by the work of Robert W. Floyd, who had published a similar system[2] for flowcharts.

Hoare triple

The central feature of Hoare logic is the Hoare triple. A triple describes how the execution of a piece of code changes the state of the computation. A Hoare triple is of the form

\{P\}C\{Q\}

where

P

and

Q

are assertions and

C

is a command.[3]

P

is named the precondition and

Q

the postcondition: when the precondition is met, executing the command establishes the postcondition. Assertions are formulae in predicate logic.

Hoare logic provides axioms and inference rules for all the constructs of a simple imperative programming language. In addition to the rules for the simple language in Hoare's original paper, rules for other language constructs have been developed since then by Hoare and many other researchers. There are rules for concurrency, procedures, jumps, and pointers.

Partial and total correctness

Using standard Hoare logic, only partial correctness can be proven. Total correctness additionally requires termination, which can be proven separately or with an extended version of the While rule.[4] Thus the intuitive reading of a Hoare triple is: Whenever

P

holds of the state before the execution of

C

, then

Q

will hold afterwards, or

C

does not terminate. In the latter case, there is no "after", so

Q

can be any statement at all. Indeed, one can choose

Q

to be false to express that

C

does not terminate.

"Termination" here and in the rest of this article is meant in the broader sense that computation will eventually be finished, that is it implies the absence of infinite loops; it does not imply the absence of implementation limit violations (e.g. division by zero) stopping the program prematurely. In his 1969 paper, Hoare used a narrower notion of termination which also entailed the absence of implementation limit violations, and expressed his preference for the broader notion of termination as it keeps assertions implementation-independent:[5]

Rules

Empty statement axiom schema

The empty statement rule asserts that the statement does not change the state of the program, thus whatever holds true before also holds true afterwards.[6]

\dfrac{}{\{P\}tt{skip}\{P\}}

Assignment axiom schema

The assignment axiom states that, after the assignment, any predicate that was previously true for the right-hand side of the assignment now holds for the variable. Formally, let be an assertion in which the variable is free. Then:

\dfrac{}{\{P[E/x]\}x:=E\{P\}}

where

P[E/x]

denotes the assertion in which each free occurrence of has been replaced by the expression .

The assignment axiom scheme means that the truth of

P[E/x]

is equivalent to the after-assignment truth of . Thus were

P[E/x]

true prior to the assignment, by the assignment axiom, then would be true subsequent to which. Conversely, were

P[E/x]

false (i.e.

\negP[E/x]

true) prior to the assignment statement, must then be false afterwards.

Examples of valid triples include:

\{x+1=43\}y:=x+1\{y=43\}

\{x+1\leqN\}x:=x+1\{x\leqN\}

All preconditions that are not modified by the expression can be carried over to the postcondition. In the first example, assigning

y:=x+1

does not change the fact that

x+1=43

, so both statements may appear in the postcondition. Formally, this result is obtained by applying the axiom schema with being (

y=43

and

x+1=43

), which yields

P[(x+1)/y]

being (

x+1=43

and

x+1=43

), which can in turn be simplified to the given precondition

x+1=43

.

The assignment axiom scheme is equivalent to saying that to find the precondition, first take the post-condition and replace all occurrences of the left-hand side of the assignment with the right-hand side of the assignment. Be careful not to try to do this backwards by following this incorrect way of thinking:

\{P\}x:=E\{P[E/x]\}

;this rule leads to nonsensical examples like:

\{x=5\}x:=3\{3=5\}

Another incorrect rule looking tempting at first glance is

\{P\}x:=E\{P\wedgex=E\}

; it leads to nonsensical examples like:

\{x=5\}x:=x+1\{x=5\wedgex=x+1\}

While a given postcondition uniquely determines the precondition

P[E/x]

, the converse is not true. For example:

\{0\leqyy\wedgeyy\leq9\}x:=yy\{0\leqx\wedgex\leq9\}

,

\{0\leqyy\wedgeyy\leq9\}x:=yy\{0\leqx\wedgeyy\leq9\}

,

\{0\leqyy\wedgeyy\leq9\}x:=yy\{0\leqyy\wedgex\leq9\}

, and

\{0\leqyy\wedgeyy\leq9\}x:=yy\{0\leqyy\wedgeyy\leq9\}

are valid instances of the assignment axiom scheme.

The assignment axiom proposed by Hoare does not apply when more than one name may refer to the same stored value. For example,

\{y=3\}x:=2\{y=3\}

is wrong if and refer to the same variable (aliasing), although it is a proper instance of the assignment axiom scheme (with both

\{P\}

and

\{P[2/x]\}

being

\{y=3\}

).

Rule of composition

Hoare's rule of composition applies to sequentially executed programs and, where executes prior to and is written

S;T

(is called the midcondition):[7]

\dfrac{\{P\}S\{Q\},\{Q\}T\{R\}}{\{P\}S;T\{R\}}

For example, consider the following two instances of the assignment axiom:

\{x+1=43\}y:=x+1\{y=43\}

and

\{y=43\}z:=y\{z=43\}

By the sequencing rule, one concludes:

\{x+1=43\}y:=x+1;z:=y\{z=43\}

Another example is shown in the right box.

Conditional rule

\dfrac{\{B\wedgeP\}S\{Q\},\{\negB\wedgeP\}T\{Q\}}{\{P\}tt{if} B tt{then} S tt{else} T tt{endif}\{Q\}}

The conditional rule states that a postcondition common to and part is also a postcondition of the whole statement.[8] In the and the part, the unnegated and negated condition can be added to the precondition, respectively.The condition,, must not have side effects.An example is given in the next section.

This rule was not contained in Hoare's original publication.[1] However, since a statement

tt{if} B tt{then} S tt{else} T tt{endif}

has the same effect as a one-time loop construct

tt{bool} b:=tt{true};tt{while} B\wedgebtt{do} S;b:=tt{false} tt{done};b:=tt{true};tt{while} \negB\wedgebtt{do} T;b:=tt{false} tt{done}

the conditional rule can be derived from the other Hoare rules.In a similar way, rules for other derived program constructs, like loop, loop,,, can be reduced by program transformation to the rules from Hoare's original paper.

Consequence rule

\dfrac{P1P2   ,\{P2\}S\{Q2\},Q2Q1}{\{P1\}S\{Q1\}}

This rule allows to strengthen the precondition

P2

and/or to weaken the postcondition

Q2

.It is used e.g. to achieve literally identical postconditions for the and the part.

For example, a proof of

\{0\leqx\leq15\}tt{if} x<15 tt{then} x:=x+1 tt{else} x:=0 tt{endif}\{0\leqx\leq15\}

needs to apply the conditional rule, which in turn requires to prove

\{0\leqx\leq15\wedgex<15\}x:=x+1\{0\leqx\leq15\}

,   or simplified

\{0\leqx<15\}x:=x+1\{0\leqx\leq15\}

for the part, and

\{0\leqx\leq15\wedgex\geq15\}x:=0\{0\leqx\leq15\}

,   or simplified

\{x=15\}x:=0\{0\leqx\leq15\}

for the part.

However, the assignment rule for the part requires to choose as

0\leqx\leq15

; rule application hence yields

\{0\leqx+1\leq15\}x:=x+1\{0\leqx\leq15\}

,   which is logically equivalent to

\{-1\leqx<15\}x:=x+1\{0\leqx\leq15\}

.The consequence rule is needed to strengthen the precondition

\{-1\leqx<15\}

obtained from the assignment rule to

\{0\leqx<15\}

required for the conditional rule.

Similarly, for the part, the assignment rule yields

\{0\leq0\leq15\}x:=0\{0\leqx\leq15\}

,   or equivalently

\{tt{true}\}x:=0\{0\leqx\leq15\}

,hence the consequence rule has to be applied with

P1

and

P2

being

\{x=15\}

and

\{tt{true}\}

, respectively, to strengthen again the precondition. Informally, the effect of the consequence rule is to "forget" that

\{x=15\}

is known at the entry of the part, since the assignment rule used for the part doesn't need that information.

While rule

\dfrac{\{P\wedgeB\}S\{P\}}{\{P\}tt{while} B tt{do} S tt{done}\{\negB\wedgeP\}}

Here is the loop invariant, which is to be preserved by the loop body .After the loop is finished, this invariant still holds, and moreover

\negB

must have caused the loop to end.As in the conditional rule, must not have side effects.

For example, a proof of

\{x\leq10\}tt{while} x<10 tt{do} x:=x+1 tt{done}\{\negx<10\wedgex\leq10\}

by the while rule requires to prove

\{x\leq10\wedgex<10\}x:=x+1\{x\leq10\}

,   or simplified

\{x<10\}x:=x+1\{x\leq10\}

,which is easily obtained by the assignment rule.Finally, the postcondition

\{\negx<10\wedgex\leq10\}

can be simplified to

\{x=10\}

.

For another example, the while rule can be used to formally verify the following strange program to compute the exact square root of an arbitrary number —even if is an integer variable and is not a square number:

\{tt{true}\}tt{while} x ⋅ xatt{do} tt{skip} tt{done}\{xx=a\wedgett{true}\}

After applying the while rule with being, it remains to prove

\{tt{true}\wedgexxa\}tt{skip}\{tt{true}\}

,which follows from the skip rule and the consequence rule.

In fact, the strange program is partially correct: if it happened to terminate, it is certain that must have contained (by chance) the value of 's square root.In all other cases, it will not terminate; therefore it is not totally correct.

While rule for total correctness

If the above ordinary while rule is replaced by the following one, the Hoare calculus can also be used to prove total correctness, i.e. termination as well as partial correctness. Commonly, square brackets are used here instead of curly braces to indicate the different notion of program correctness.

\dfrac{<isawell-foundedorderingonthesetD,[P\wedgeB\wedget\inD\wedget=z]S[P\wedget\inD\wedget<z]}{[P\wedget\inD]tt{while} B tt{do} S tt{done}[\negB\wedgeP\wedget\inD]}

In this rule, in addition to maintaining the loop invariant, one also proves termination by way of an expression, called the loop variant, whose value strictly decreases with respect to a well-founded relation on some domain set during each iteration. Since is well-founded, a strictly decreasing chain of members of can have only finite length, so cannot keep decreasing forever. (For example, the usual order is well-founded on positive integers

N

, but neither on the integers

Z

nor on positive real numbers

R+

; all these sets are meant in the mathematical, not in the computing sense, they are all infinite in particular.)

Given the loop invariant, the condition must imply that is not a minimal element of, for otherwise the body could not decrease any further, i.e. the premise of the rule would be false. (This is one of various notations for total correctness.)

Resuming the first example of the previous section, for a total-correctness proof of

[x\leq10]tt{while} x<10 tt{do} x:=x+1 tt{done}[\negx<10\wedgex\leq10]

the while rule for total correctness can be applied with e.g. being the non-negative integers with the usual order, and the expression being

10-x

, which then in turn requires to prove

[x\leq10\wedgex<10\wedge10-x\geq0\wedge10-x=z]x:=x+1[x\leq10\wedge10-x\geq0\wedge10-x<z]

Informally speaking, we have to prove that the distance

10-x

decreases in every loop cycle, while it always remains non-negative; this process can go on only for a finite number of cycles.

The previous proof goal can be simplified to

[x<10\wedge10-x=z]x:=x+1[x\leq10\wedge10-x<z]

,which can be proven as follows:

[x+1\leq10\wedge10-x-1<z]x:=x+1[x\leq10\wedge10-x<z]

is obtained by the assignment rule, and

[x+1\leq10\wedge10-x-1<z]

can be strengthened to

[x<10\wedge10-x=z]

by the consequence rule.

For the second example of the previous section, of course no expression can be found that is decreased by the empty loop body, hence termination cannot be proved.

Further reading

External links

Notes and References

  1. Hoare . C. A. R. . C.A.R. Hoare . An axiomatic basis for computer programming . 10.1145/363235.363259 . . 12 . 10 . 576 - 580 . October 1969 . 207726175 . free .
  2. [Robert W. Floyd|R. W. Floyd]
  3. Hoare originally wrote "

    P\{C\}Q

    " rather than "

    \{P\}C\{Q\}

    ".
  4. Book: Theories of Programming Languages . John C. Reynolds . John C. Reynolds . Cambridge University Press . 2009.) Here: Sect. 3.4, p. 64.
  5. Hoare (1969), p.578-579
  6. This article uses a natural deduction style notation for rules. For example,

    \dfrac{\alpha,\beta}{\phi}

    informally means "If both and hold, then also holds"; and are called antecedents of the rule, is called its succedent. A rule without antecedents is called an axiom, and written as

    \dfrac{}{\phi}

    .
  7. Book: Michael. Huth. Mark. Ryan. CUP. Logic in Computer Science. 978-0521543101. second . 276. 2004-08-26.
  8. Apt . Krzysztof R. . Olderog . Ernst-Rüdiger . Fifty years of Hoare's logic . Formal Aspects of Computing . December 2019 . 31 . 6 . 759 . 10.1007/s00165-019-00501-3. 102351597 .