# 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

${\displaystyle \{P\}C\{Q\}}$

where ${\displaystyle P}$ and ${\displaystyle Q}$ are assertions and ${\displaystyle C}$ is a command.[note 1] ${\displaystyle P}$ is named the precondition and ${\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, while termination needs to be proved separately. Thus the intuitive reading of a Hoare triple is: Whenever ${\displaystyle P}$ holds of the state before the execution of ${\displaystyle C}$, then ${\displaystyle Q}$ will hold afterwards, or ${\displaystyle C}$ does not terminate. In the latter case, there is no "after", so ${\displaystyle Q}$ can be any statement at all. Indeed, one can choose ${\displaystyle Q}$ to be false to express that ${\displaystyle C}$ does not terminate.

Total correctness can also be proven with an extended version of the While rule.[citation needed]

In his 1969 paper, Hoare used a narrower notion of termination which also entailed absence of any run-time errors: "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."[3]

## Rules

### Empty statement axiom schema

The empty statement rule asserts that the ${\displaystyle {\textbf {skip}}}$ statement does not change the state of the program, thus whatever holds true before ${\displaystyle {\textbf {skip}}}$ also holds true afterwards.[note 2]

${\displaystyle {\dfrac {}{\{P\}{\textbf {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 ${\displaystyle P}$ be an assertion in which the variable ${\displaystyle x}$ is free. Then:

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

where ${\displaystyle P[E/x]}$ denotes the assertion ${\displaystyle P}$ in which each free occurrence of ${\displaystyle x}$ has been replaced by the expression ${\displaystyle E}$.

The assignment axiom scheme means that the truth of ${\displaystyle P[E/x]}$ is equivalent to the after-assignment truth of ${\displaystyle P}$. Thus were ${\displaystyle P[E/x]}$ true prior to the assignment, by the assignment axiom, then ${\displaystyle P}$ would be true subsequent to which. Conversely, were ${\displaystyle P[E/x]}$ false (i.e. ${\displaystyle \neg P[E/x]}$ true) prior to the assignment statement, ${\displaystyle P}$ must then be false afterwards.

Examples of valid triples include:

• ${\displaystyle \{x+1=43\}y:=x+1\{y=43\}}$
• ${\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 ${\displaystyle y:=x+1}$ does not change the fact that ${\displaystyle x+1=43}$, so both statements may appear in the postcondition. Formally, this result is obtained by applying the axiom schema with ${\displaystyle P}$ being (${\displaystyle y=43}$ and ${\displaystyle x+1=43}$), which yields ${\displaystyle P[(x+1)/y]}$ being (${\displaystyle x+1=43}$ and ${\displaystyle x+1=43}$), which can in turn be simplified to the given precondition ${\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: ${\displaystyle \{P\}x:=E\{P[E/x]\}}$; this rule leads to nonsensical examples like:

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

Another incorrect rule looking tempting at first glance is ${\displaystyle \{P\}x:=E\{P\wedge x=E\}}$; it leads to nonsensical examples like:

${\displaystyle \{x=5\}x:=x+1\{x=5\wedge x=x+1\}}$

While a given postcondition ${\displaystyle P}$ uniquely determines the precondition ${\displaystyle P[E/x]}$, the converse is not true. For example:

• ${\displaystyle \{0\leq y\cdot y\wedge y\cdot y\leq 9\}x:=y\cdot y\{0\leq x\wedge x\leq 9\}}$,
• ${\displaystyle \{0\leq y\cdot y\wedge y\cdot y\leq 9\}x:=y\cdot y\{0\leq x\wedge y\cdot y\leq 9\}}$ ,
• ${\displaystyle \{0\leq y\cdot y\wedge y\cdot y\leq 9\}x:=y\cdot y\{0\leq y\cdot y\wedge x\leq 9\}}$ , and
• ${\displaystyle \{0\leq y\cdot y\wedge y\cdot y\leq 9\}x:=y\cdot y\{0\leq y\cdot y\wedge y\cdot y\leq 9\}}$

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,

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

is wrong if ${\displaystyle x}$ and ${\displaystyle y}$ refer to the same variable (aliasing), although it is a proper instance of the assignment axiom scheme (with both ${\displaystyle \{P\}}$ and ${\displaystyle \{P[2/x]\}}$ being ${\displaystyle \{y=3\}}$).

### Rule of composition

Hoare's rule of composition applies to sequentially executed programs ${\displaystyle S}$ and ${\displaystyle T}$, where ${\displaystyle S}$ executes prior to ${\displaystyle T}$ and is written ${\displaystyle S;T}$ (${\displaystyle Q}$ is called the midcondition):[4]

${\displaystyle {\dfrac {\{P\}S\{Q\}\quad ,\quad \{Q\}T\{R\}}{\{P\}S;T\{R\}}}}$

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

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

and

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

By the sequencing rule, one concludes:

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

Another example is shown in the right box.

### Conditional rule

${\displaystyle {\dfrac {\{B\wedge P\}S\{Q\}\quad ,\quad \{\neg B\wedge P\}T\{Q\}}{\{P\}{\textbf {if}}\ B\ {\textbf {then}}\ S\ {\textbf {else}}\ T\ {\textbf {endif}}\{Q\}}}}$

The conditional rule states that a postcondition ${\displaystyle Q}$ common to ${\displaystyle {\textbf {then}}}$ and ${\displaystyle {\textbf {else}}}$ part is also a postcondition of the whole ${\displaystyle {\textbf {if}}\cdots {\textbf {endif}}}$ statement. In the ${\displaystyle {\textbf {then}}}$ and the ${\displaystyle {\textbf {else}}}$ part, the unnegated and negated condition ${\displaystyle B}$ can be added to the precondition ${\displaystyle P}$, respectively. The condition, ${\displaystyle B}$, 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

${\displaystyle {\textbf {if}}\ B\ {\textbf {then}}\ S\ {\textbf {else}}\ T\ {\textbf {endif}}}$

has the same effect as a one-time loop construct

${\displaystyle {\textbf {bool}}\ b:={\textbf {true}};{\textbf {while}}\ B\wedge b\ {\textbf {do}}\ S;b:={\textbf {false}}\ {\textbf {done}};b:={\textbf {true}};{\textbf {while}}\ \neg B\wedge b\ {\textbf {do}}\ T;b:={\textbf {false}}\ {\textbf {done}}}$

the conditional rule can be derived from the other Hoare rules. In a similar way, rules for other derived program constructs, like ${\displaystyle {\textbf {for}}}$ loop, ${\displaystyle {\textbf {do}}\cdots {\textbf {until}}}$ loop, ${\displaystyle {\textbf {switch}}}$, ${\displaystyle {\textbf {break}}}$, ${\displaystyle {\textbf {continue}}}$ can be reduced by program transformation to the rules from Hoare's original paper.

### Consequence rule

${\displaystyle {\dfrac {P_{1}\rightarrow P_{2}\quad ,\quad \{P_{2}\}S\{Q_{2}\}\quad ,\quad Q_{2}\rightarrow Q_{1}}{\{P_{1}\}S\{Q_{1}\}}}}$

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 ${\displaystyle {\textbf {then}}}$ and the ${\displaystyle {\textbf {else}}}$ part.

For example, a proof of

${\displaystyle \{0\leq x\leq 15\}{\textbf {if}}\ x<15\ {\textbf {then}}\ x:=x+1\ {\textbf {else}}\ x:=0\ {\textbf {endif}}\{0\leq x\leq 15\}}$

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

${\displaystyle \{0\leq x\leq 15\wedge x<15\}x:=x+1\{0\leq x\leq 15\}}$,   or simplified
${\displaystyle \{0\leq x<15\}x:=x+1\{0\leq x\leq 15\}}$

for the ${\displaystyle {\textbf {then}}}$ part, and

${\displaystyle \{0\leq x\leq 15\wedge x\geq 15\}x:=0\{0\leq x\leq 15\}}$,   or simplified
${\displaystyle \{x=15\}x:=0\{0\leq x\leq 15\}}$

for the ${\displaystyle {\textbf {else}}}$ part.

However, the assignment rule for the ${\displaystyle {\textbf {then}}}$ part requires to choose ${\displaystyle P}$ as ${\displaystyle 0\leq x\leq 15}$; rule application hence yields

${\displaystyle \{0\leq x+1\leq 15\}x:=x+1\{0\leq x\leq 15\}}$,   which is logically equivalent to
${\displaystyle \{-1\leq x<15\}x:=x+1\{0\leq x\leq 15\}}$.

The consequence rule is needed to strengthen the precondition ${\displaystyle \{-1\leq x<15\}}$ obtained from the assignment rule to ${\displaystyle \{0\leq x<15\}}$ required for the conditional rule.

Similarly, for the ${\displaystyle {\textbf {else}}}$ part, the assignment rule yields

${\displaystyle \{0\leq 0\leq 15\}x:=0\{0\leq x\leq 15\}}$,   or equivalently
${\displaystyle \{{\textbf {true}}\}x:=0\{0\leq x\leq 15\}}$,

hence the consequence rule has to be applied with ${\displaystyle P_{1}}$ and ${\displaystyle P_{2}}$ being ${\displaystyle \{x=15\}}$ and ${\displaystyle \{{\textbf {true}}\}}$, respectively, to strengthen again the precondition. Informally, the effect of the consequence rule is to "forget" that ${\displaystyle \{x=15\}}$ is known at the entry of the ${\displaystyle {\textbf {else}}}$ part, since the assignment rule used for the ${\displaystyle {\textbf {else}}}$ part doesn't need that information.

### While rule

${\displaystyle {\dfrac {\{P\wedge B\}S\{P\}}{\{P\}{\textbf {while}}\ B\ {\textbf {do}}\ S\ {\textbf {done}}\{\neg B\wedge P\}}}}$

Here ${\displaystyle P}$ is the loop invariant, which is to be preserved by the loop body ${\displaystyle S}$. After the loop is finished, this invariant ${\displaystyle P}$ still holds, and moreover ${\displaystyle \neg B}$ must have caused the loop to end. As in the conditional rule, ${\displaystyle B}$ must not have side effects.

For example, a proof of

${\displaystyle \{x\leq 10\}{\textbf {while}}\ x<10\ {\textbf {do}}\ x:=x+1\ {\textbf {done}}\{\neg x<10\wedge x\leq 10\}}$

by the while rule requires to prove

${\displaystyle \{x\leq 10\wedge x<10\}x:=x+1\{x\leq 10\}}$,   or simplified
${\displaystyle \{x<10\}x:=x+1\{x\leq 10\}}$,

which is easily obtained by the assignment rule. Finally, the postcondition ${\displaystyle \{\neg x<10\wedge x\leq 10\}}$ can be simplified to ${\displaystyle \{x=10\}}$.

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

${\displaystyle \{{\textbf {true}}\}{\textbf {while}}\ x\cdot x\neq a\ {\textbf {do}}\ {\textbf {skip}}\ {\textbf {done}}\{x\cdot x=a\wedge {\textbf {true}}\}}$

After applying the while rule with ${\displaystyle P}$ being ${\displaystyle {\textbf {true}}}$, it remains to prove

${\displaystyle \{{\textbf {true}}\wedge x\cdot x\neq a\}{\textbf {skip}}\{{\textbf {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 ${\displaystyle x}$ must have contained (by chance) the value of ${\displaystyle a}$'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[note 3] as well as partial correctness. Commonly, square brackets are used here instead of curly braces to indicate the different notion of program correctness.

${\displaystyle {\dfrac {<\ {\text{is a well-founded ordering on the set}}\ D\quad ,\quad [P\wedge B\wedge t\in D\wedge t=z]S[P\wedge t\in D\wedge t

In this rule, in addition to maintaining the loop invariant, one also proves termination by way of an expression ${\displaystyle t}$, called the loop variant, whose value strictly decreases with respect to a well-founded relation ${\displaystyle <}$ on some domain set ${\displaystyle D}$ during each iteration. Since ${\displaystyle <}$ is well-founded, a strictly decreasing chain of members of ${\displaystyle D}$ can have only finite length, so ${\displaystyle t}$ cannot keep decreasing forever. (For example, the usual order ${\displaystyle <}$ is well-founded on positive integers ${\displaystyle \mathbb {N} }$, but neither on the integers ${\displaystyle \mathbb {Z} }$ nor on positive real numbers ${\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 ${\displaystyle P}$, the condition ${\displaystyle B}$ must imply that ${\displaystyle t}$ is not a minimal element of ${\displaystyle D}$, for otherwise the body ${\displaystyle S}$ could not decrease ${\displaystyle t}$ any further, i.e. the premise of the rule would be false. (This is one of various notations for total correctness.) [note 4]

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

${\displaystyle [x\leq 10]{\textbf {while}}\ x<10\ {\textbf {do}}\ x:=x+1\ {\textbf {done}}[\neg x<10\wedge x\leq 10]}$

the while rule for total correctness can be applied with e.g. ${\displaystyle D}$ being the non-negative integers with the usual order, and the expression ${\displaystyle t}$ being ${\displaystyle 10-x}$, which then in turn requires to prove

${\displaystyle [x\leq 10\wedge x<10\wedge 10-x\geq 0\wedge 10-x=z]x:=x+1[x\leq 10\wedge 10-x\geq 0\wedge 10-x

Informally speaking, we have to prove that the distance ${\displaystyle 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

${\displaystyle [x<10\wedge 10-x=z]x:=x+1[x\leq 10\wedge 10-x,

which can be proven as follows:

${\displaystyle [x+1\leq 10\wedge 10-x-1 is obtained by the assignment rule, and
${\displaystyle [x+1\leq 10\wedge 10-x-1 can be strengthened to ${\displaystyle [x<10\wedge 10-x=z]}$ by the consequence rule.

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

## Notes

1. ^ Hoare originally wrote "${\displaystyle P\{C\}Q}$" rather than "${\displaystyle \{P\}C\{Q\}}$".
2. ^ This article uses a natural deduction style notation for rules. For example, ${\displaystyle {\dfrac {\alpha ,\beta }{\phi }}}$ informally means "If both ${\displaystyle \alpha }$ and ${\displaystyle \beta }$ hold, then also ${\displaystyle \phi }$ holds"; ${\displaystyle \alpha }$ and ${\displaystyle \beta }$ are called antecedents of the rule, ${\displaystyle \phi }$ is called its succedent. A rule without antecedents is called an axiom, and written as ${\displaystyle {\dfrac {}{\quad \phi \quad }}}$.
3. ^ "Termination" here is meant in the broader sense that computation will eventually be finished; it does not imply that no limit violation (e.g. zero divide) can stop the program prematurely.
4. ^ Hoare's 1969 paper didn't provide a total correctness rule; cf. his discussion on p.579 (top left). For example Reynolds' textbook (John C. Reynolds (2009). Theory of Programming Languages. Cambridge University Press.), Sect.3.4, p.64 gives the following version of a total correctness rule: ${\displaystyle {\dfrac {P\wedge B\rightarrow 0\leq t\quad ,\quad [P\wedge B\wedge t=z]S[P\wedge t when ${\displaystyle z}$ is an integer variable that doesn't occur free in ${\displaystyle P}$, ${\displaystyle B}$, ${\displaystyle S}$, or ${\displaystyle t}$, and ${\displaystyle t}$ is an integer expression (Reynolds' variables renamed to fit with this article's settings).

## References

1. ^ a b Hoare, C. A. R. (October 1969). "An axiomatic basis for computer programming" (PDF). Communications of the ACM. 12 (10): 576–580. doi:10.1145/363235.363259.
2. ^ R. W. Floyd. "Assigning meanings to programs." Proceedings of the American Mathematical Society Symposia on Applied Mathematics. Vol. 19, pp. 19–31. 1967.
3. ^ p.579 upper left
4. ^ Huth, Michael; Ryan, Mark (2004-08-26). Logic in Computer Science (second ed.). CUP. p. 276. ISBN 978-0521543101.