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.[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
where and are assertions and is a command.[note 1] is named the precondition and 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.[3] Thus the intuitive reading of a Hoare triple is: Whenever holds of the state before the execution of , then will hold afterwards, or does not terminate. In the latter case, there is no "after", so can be any statement at all. Indeed, one can choose to be false to express that 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:[4]
Rules
Empty statement axiom schema
The empty statement rule asserts that the Template:Mono statement does not change the state of the program, thus whatever holds true before Template:Mono also holds true afterwards.[note 2]
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 Template:Mvar be an assertion in which the variable Template:Mvar is free. Then:
where denotes the assertion Template:Mvar in which each free occurrence of Template:Mvar has been replaced by the expression Template:Mvar.
The assignment axiom scheme means that the truth of is equivalent to the after-assignment truth of Template:Mvar. Thus were true prior to the assignment, by the assignment axiom, then Template:Mvar would be true subsequent to which. Conversely, were false (i.e. true) prior to the assignment statement, Template:Mvar must then be false afterwards.
Examples of valid triples include:
All preconditions that are not modified by the expression can be carried over to the postcondition. In the first example, assigning does not change the fact that , so both statements may appear in the postcondition. Formally, this result is obtained by applying the axiom schema with Template:Mvar being ( and ), which yields being ( and ), which can in turn be simplified to the given precondition .
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: ; this rule leads to nonsensical examples like:
Another incorrect rule looking tempting at first glance is ; it leads to nonsensical examples like:
While a given postcondition Template:Mvar uniquely determines the precondition , 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 Template:Mvar and Template:Mvar refer to the same variable (aliasing), although it is a proper instance of the assignment axiom scheme (with both and being ).
Rule of composition
| Verifying swap-code without auxiliary variables | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||
Hoare's rule of composition applies to sequentially executed programs Template:Mvar and Template:Mvar, where Template:Mvar executes prior to Template:Mvar and is written (Template:Mvar is called the midcondition):[5]
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 Template:Mvar common to Template:Mono and Template:Mono part is also a postcondition of the whole Template:Mono statement.[6] In the Template:Mono and the Template:Mono part, the unnegated and negated condition Template:Mvar can be added to the precondition Template:Mvar, respectively. The condition, Template:Mvar, 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
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 Template:Mono loop, Template:Mono loop, Template:Mono, Template:Mono, Template:Mono can be reduced by program transformation to the rules from Hoare's original paper.
Consequence rule
This rule allows to strengthen the precondition and/or to weaken the postcondition . It is used e.g. to achieve literally identical postconditions for the Template:Mono and the Template:Mono part.
For example, a proof of
needs to apply the conditional rule, which in turn requires to prove
- , or simplified
for the Template:Mono part, and
- , or simplified
for the Template:Mono part.
However, the assignment rule for the Template:Mono part requires to choose Template:Mvar as ; rule application hence yields
- , which is logically equivalent to
- .
The consequence rule is needed to strengthen the precondition obtained from the assignment rule to required for the conditional rule.
Similarly, for the Template:Mono part, the assignment rule yields
- , or equivalently
- ,
hence the consequence rule has to be applied with and being and , respectively, to strengthen again the precondition. Informally, the effect of the consequence rule is to "forget" that is known at the entry of the Template:Mono part, since the assignment rule used for the Template:Mono part doesn't need that information.
While rule
Here Template:Mvar is the loop invariant, which is to be preserved by the loop body Template:Mvar. After the loop is finished, this invariant Template:Mvar still holds, and moreover must have caused the loop to end. As in the conditional rule, Template:Mvar must not have side effects.
For example, a proof of
by the while rule requires to prove
- , or simplified
- ,
which is easily obtained by the assignment rule. Finally, the postcondition can be simplified to .
For another example, the while rule can be used to formally verify the following strange program to compute the exact square root Template:Mvar of an arbitrary number Template:Mvar—even if Template:Mvar is an integer variable and Template:Mvar is not a square number:
After applying the while rule with Template:Mvar being Template:Mono, it remains to prove
- ,
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 Template:Mvar must have contained (by chance) the value of Template:Mvar'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 Template:Mvar, called the loop variant, whose value strictly decreases with respect to a well-founded relation Template:Mvar on some domain set Template:Mvar during each iteration. Since Template:Mvar is well-founded, a strictly decreasing chain of members of Template:Mvar can have only finite length, so Template:Mvar cannot keep decreasing forever. (For example, the usual order Template:Mvar is well-founded on positive integers , but neither on the 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 Template:Mvar, the condition Template:Mvar must imply that Template:Mvar is not a minimal element of Template:Mvar, for otherwise the body Template:Mvar could not decrease Template:Mvar any further, i.e. the premise of the rule would be false. (This is one of various notations for total correctness.) [note 3]
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. Template:Mvar being the non-negative integers with the usual order, and the expression Template:Mvar being , which then in turn requires to prove
Informally speaking, we have to prove that the distance 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
- ,
which can be proven as follows:
- is obtained by the assignment rule, and
- can be strengthened to by the consequence rule.
For the second example of the previous section, of course no expression Template:Mvar can be found that is decreased by the empty loop body, hence termination cannot be proved.
See also
Notes
References
Further reading
- Robert D. Tennent. Specifying Software (a textbook that includes an introduction to Hoare logic, written in 2002) Template:ISBN
External links
- KeY-Hoare is a semi-automatic verification system built on top of the KeY theorem prover. It features a Hoare calculus for a simple while language.
- j-Algo Hoare Calculus module (j-Algo on GitHub, j-Algo on SourceForge) – A visualisation of the Hoare calculus in the algorithm visualisation program j-Algo.
Template:Program analysis Template:Authority control
- ↑ 1.0 1.1 Template:Cite journal
- ↑ R. W. Floyd. "Assigning meanings to programs." Proceedings of the American Mathematical Society Symposia on Applied Mathematics. Vol. 19, pp. 19–31. 1967.
- ↑ 3.0 3.1 Template:Cite book) Here: Sect. 3.4, p. 64.
- ↑ Hoare (1969), p.578-579
- ↑ Template:Cite book
- ↑ Template:Cite journal
Cite error: <ref> tags exist for a group named "note", but no corresponding <references group="note"/> tag was found