arvau

Wikipedia / Hoare logic

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. The original ideas were seeded by the work of Robert W. Floyd, who had published a similar system 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

are assertions is a command. is named the precondition 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, proceduress, jumpss, and pointerss.

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. 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:

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.

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:

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

is equivalent to the after-assignment truth of true before the assignment, by the assignment axiom, then true) prior to the assignment statement, must then be false afterwards.

Examples of valid triples include:

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

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; this rule leads to nonsensical examples like:

Another incorrect; it leads to nonsensical examples like:

While a given postcondition , the converse is not true. For example:

  • ,

  • ,

  • , and

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,

is wrong if and refer to the same variable (aliasing).

Rule of composition

Verifying swap-codewithout auxiliary variables

Nr

Code

Assertions

1:

2:

3:

4:

5:

6:

7:

The three statements below (line 2, 4, 6) exchange the values of the variables and , without needing an auxiliary variable. In the verification proof, the initial value of and is denoted by the constant and , respectively. The proof is best read backwards, starting from line 7; for example, line 5 is obtained from line 7 by replacing (line 3→1).

Hoare's rule of composition applies to sequentially executed programs and , where executes prior to ( is called the midcondition):

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

and

By the sequencing rule, one concludes:

Another example is shown in the right box.

Conditional rule

The conditional rule states that a postcondition common to and part is also a postcondition of the whole statement. 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. However, since a statement

has the same effect as a one-time loop construct

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

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

For example, a proof of

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

  1. , or simplified

for the part, and

  1. , or simplified

for the part.

However, the assignment rule for the part requires to choose ; rule application hence yields

  1. , which is logically equivalent to

  2. .

required for the conditional rule.

Similarly, for the part, the assignment rule yields

  1. , or equivalently

  2. ,

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

While rule

Here is the loop invariant, which is to be preserved by the loop body . After the loop is finished, this invariant must have caused the loop to end. As in the conditional rule, must not have side effects.

For example, a proof of

by the while rule requires to prove

  1. , or simplified

  2. ,

.

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:

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

  1. ,

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.

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 nor on positive real numbers; 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

the while rule for total correctness can be applied with e.g. being the non-negative integers with the usual order, and the expression , which then in turn requires to prove

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

  1. ,

which can be proven as follows:

  1. is obtained by the assignment rule, and

  2. 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.

See also

Notes

References

Bibliography