Hoare logic 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.
Article
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.
Contents
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
}
{\displaystyle \{P\}C\{Q\}}
where
P
{\displaystyle P}
and
Q
{\displaystyle Q}
are assertions and
C
{\displaystyle C}
is a command.
P
{\displaystyle P}
is named the precondition and
Q
{\displaystyle 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. Thus, the intuitive reading of a Hoare triple is: Whenever
P
{\displaystyle P}
holds of the state before the execution of
C
{\displaystyle C}
, then
Q
{\displaystyle Q}
will hold afterwards, or
C
{\displaystyle C}
does not terminate. In the latter case, there is no "after", so
Q
{\displaystyle Q}
can be any statement at all. Indeed, one can choose
Q
{\displaystyle Q}
to be false to express that
C
{\displaystyle 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:
Rules
Empty statement axiom schema
The empty statement rule asserts that the skip statement does not change the state of the program, thus whatever holds true before skip also holds true afterwards.
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 P be an assertion in which the variable x is free. Then:
{
P
[
E
/
x
]
}
x
:=
E
{
P
}
{\displaystyle {\dfrac {}{\{P[E/x]\}x:=E\{P\}}}}
where
P
[
E
/
x
]
{\displaystyle P[E/x]}
denotes the assertion P in which each free occurrence of x has been replaced by the expression E.
The assignment axiom scheme means that the truth of
P
[
E
/
Rule of composition
Hoare's rule of composition applies to sequentially executed programs S and T, where S executes prior to T and is written
The conditional rule states that a postcondition Q common to then and else part is also a postcondition of the whole if...endif statement. In the then and the else part, the unnegated and negated condition B can be added to the precondition P, respectively.
Here P is the loop invariant, which is to be preserved by the loop body S.
After the loop is finished, this invariant P still holds, and moreover
¬
B
{\displaystyle \neg B}
must have caused the loop to end.
As in the conditional rule, B must not have side effects.
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.
<
is a well-founded ordering on the set
D
,
[
P
∧
B
∧
t
∈
D
∧
t
=
z
]
S
[
P
∧
t
∈
D
∧
t
<
z
]
[
P
∧
Article from Wikipedia (CC BY-SA 4.0), where it is maintained by volunteer editors.
Another deficiency in the axioms and rules quoted above is that they give no basis for a proof that a program successfully terminates. Failure to terminate may be due to an infinite loop; or it may be due to violation of an implementation-defined limit, for example, the range of numeric operands, the size of storage, or an operating system time limit. Thus the notation “
P
{
Q
}
R
{\displaystyle P\{Q\}R}
” should be interpreted “provided that the program successfully terminates, the properties of its results are described by
R
{\displaystyle R}
.” It is fairly easy to adapt the axioms so that they cannot be used to predict the “results” of nonterminating programs; but the actual use of the axioms would now depend on knowledge of many implementation-dependent features, for example, the size and speed of the computer, the range of numbers, and the choice of overflow technique. Apart from proofs of the avoidance of infinite loops, it is probably better to prove the “conditional” correctness of a program and rely on an implementation to give a warning if it has had to abandon execution of the program as a result of violation of an implementation limit.
x
]
{\displaystyle P[E/x]}
is equivalent to the after-assignment truth of P. Thus, were
P
[
E
/
x
]
{\displaystyle P[E/x]}
true before the assignment, by the assignment axiom, then P would be true afterwards. Conversely, were
P
[
E
/
x
]
{\displaystyle P[E/x]}
false (i.e.
¬
P
[
E
/
x
]
{\displaystyle \neg P[E/x]}
true) prior to the assignment statement, P must then be false afterwards.
Examples of valid triples include:
{
x
+
1
=
43
}
y
:=
x
+
1
{
y
=
43
}
{\displaystyle \{x+1=43\}y:=x+1\{y=43\}}
{
x
+
1
≤
N
}
x
:=
x
+
1
{
x
≤
N
}
{\displaystyle \{x+1\leq N\}x:=x+1\{x\leq N\}}
All preconditions that are not modified by the expression can be carried over to the postcondition. In the first example, assigning
y
:=
x
+
1
{\displaystyle y:=x+1}
does not change the fact that
x
+
1
=
43
{\displaystyle x+1=43}
, so both statements may appear in the postcondition. Formally, this result is obtained by applying the axiom schema with P being (
y
=
43
{\displaystyle y=43}
and
x
+
1
=
43
{\displaystyle x+1=43}
), which yields
P
[
(
x
+
1
)
/
y
]
{\displaystyle P[(x+1)/y]}
being (
x
+
1
=
43
{\displaystyle x+1=43}
and
x
+
1
=
43
{\displaystyle x+1=43}
), which can in turn be simplified to the given precondition
x
+
1
=
43
{\displaystyle 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
]
}
{\displaystyle \{P\}x:=E\{P[E/x]\}}
;
this rule leads to nonsensical examples like:
{
x
=
5
}
x
:=
3
{
3
=
5
}
{\displaystyle \{x=5\}x:=3\{3=5\}}
Another incorrect rule looking tempting at first glance is
{
P
}
x
:=
E
{
P
∧
x
=
E
}
{\displaystyle \{P\}x:=E\{P\wedge x=E\}}
; it leads to nonsensical examples like:
{
x
=
5
}
x
:=
x
+
1
{
x
=
5
∧
x
=
x
+
1
}
{\displaystyle \{x=5\}x:=x+1\{x=5\wedge x=x+1\}}
While a given postcondition P uniquely determines the precondition
the conditional rule can be derived from the other Hoare rules.
In a similar way, rules for other derived program constructs, like for loop, do...until loop, switch, break, continue can be reduced by program transformation to the rules from Hoare's original paper.
and/or to weaken the postcondition
Q
2
{\displaystyle Q_{2}}
.
It is used, e.g., to achieve literally identical postconditions for the then and the else part.
For another example, the while rule can be used to formally verify the following strange program to compute the exact square root x of an arbitrary number a—even if x is an integer variable and a is not a square number:
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 x must have contained (by chance) the value of a's square root.
In all other cases, it will not terminate; therefore, it is not totally correct.
In this rule, in addition to maintaining the loop invariant, one also proves termination by way of an expression t, called the loop variant, whose value strictly decreases with respect to a well-founded relation < on some domain set D during each iteration. Since < is well-founded, a strictly decreasing chain of members of D can have only finite length, so t cannot keep decreasing forever. (For example, the usual order < is well-founded on positive integers
N
{\displaystyle \mathbb {N} }
, but neither on the integers
Z
{\displaystyle \mathbb {Z} }
nor on positive real numbers
R
+
{\displaystyle \mathbb {R} ^{+}}
; all these sets are meant in the mathematical, not in the computing sense, they are all infinite in particular.)
Given the loop invariant P, the condition B must imply that t is not a minimal element of D, for otherwise the body S could not decrease t 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
For the second example of the previous section, of course, no expression t can be found that is decreased by the empty loop body, hence termination cannot be proved.