Definition
A Hoare triple, written , is a statement about a piece of program : if starts in a state where the condition holds and finishes, then the condition holds when it finishes. is the precondition and the postcondition, both logical formulas over the program’s variables. This reading, called partial correctness, says nothing about runs that never finish; total correctness also requires to finish from every state that satisfies . Tony Hoare introduced these statements, and rules for proving them, in 1969.
The idea
A function’s comment says: given a whole number , it returns . That comment is a contract, a requirement on the caller and a promise about the result. A Hoare triple writes the contract in logic, so that it can be proved rather than hoped for. The proof goes piece by piece: a small set of rules, called Hoare logic, derives the triple for a whole program from triples for its statements. Robert Floyd had given a similar method for flowcharts in 1967 [268], so the rules are also called Floyd–Hoare logic.
The partial-correctness reading separates two questions. “If it finishes, is the answer right?” is answered with invariants. “Does it finish?” is answered with a separate termination argument. The separation has a trap. A program that never finishes satisfies every partial-correctness triple, because it has no final state that could violate . The triple while true do skip , whose loop body skip does nothing, holds although no state satisfies . A builder’s contract is a fair picture. “If you provide a level plot, and if the house is ever finished, it will have three bedrooms” is partial correctness; adding “and it will be finished” gives total correctness. The picture stops at the wording: the conditions of a triple are exact logical formulas, which can be checked mechanically.
How it works
A state gives a value to every variable. An assertion, such as , is true or false in each state. Hoare logic proves triples with one rule for each kind of statement, plus one rule for adjusting conditions. Below, means and, means not and means implies.
Assignment. , where is with every replaced by the expression . To make true after , the condition must hold before it.
Sequence. From and , conclude .
Conditional. From and , conclude
ifthenelse.Consequence. From , and , conclude : a precondition may be strengthened and a postcondition weakened.
Loop, partial correctness. The loop
whiledorepeats as long as the condition holds. From , concludewhiledo. The condition is a loop invariant §3.24: true before the loop and kept true by every run of the body.Loop, total correctness. In addition, give an integer expression , a ranking function §3.25 (also called a variant), such that , and such that every run of the body that starts with and finishes with , where stands for the value of before the iteration. A whole number that never goes below 0 cannot decrease forever, so the loop stops.
Total-correctness triples are often written with square brackets, . Edsger Dijkstra’s weakest precondition is the weakest condition on the starting state that guarantees that finishes in a state satisfying ; holds exactly when is true in every state. Program verifiers compute such conditions automatically for code without loops and use the invariants and ranking functions that the programmer writes for loops. They give the resulting implications, called verification conditions, to an SMT solver §3.28 (satisfiability modulo theories solver, a program that decides such formulas).
Worked example
Partial correctness with an invariant. Call the following program :
i := 0; s := 0;
while i < n do (i := i + 1; s := s + i)
The claim is . Take the invariant : and .
Before the loop, and , and ; with , also . So holds.
Each iteration starts with and . The new is , which is at most because and both are whole numbers. The new is , which is the formula of for the new . So holds again.
At exit, holds and is false, so and ; hence and .
By the loop rule and the sequence rule, the partial-correctness triple holds.
The termination argument. Take . Inside the loop , so , which is at least 0, and each iteration adds 1 to , so falls by exactly 1. With the values of are , , and ; then is false and the loop stops with . The invariant and the ranking function together give the total-correctness triple .
Partial but not total. The program i := 0; while i != n do i := i + 2, where != means not equal, satisfies the partial-correctness triple with precondition and postcondition . The loop exits only when , so the invariant and the exit condition give . The program is not totally correct. For , takes the values 0, 2, 4, 6 and so on, and never equals 3. The candidate ranking function takes the values 3, 1, , so the requirement inside the loop fails. With the stronger precondition “ and even”, the invariant “ is even and ” and succeed. Inside the loop , and two even numbers with and satisfy . So after the step , and falls by 2 while staying at least 0.
Machine arithmetic. With 8-bit unsigned integers, which hold 0 to 255 and wrap around, the triple is not valid: for , , where gives the remainder after division. With the precondition the triple is valid. In C, adding 1 to the largest value of the type int is undefined behavior §4.12, so a proof about C code must include such a bound in its precondition.
Stated precisely
Let a state map each variable to a value, and write when assertion is true in . The partial-correctness triple is valid when, for every state with and every state in which some run of started in finishes normally, . The total-correctness triple is valid when, for every state with , every run of started in finishes normally and its final state satisfies . Write for the assertion that holds in every state and for “if and only if”. Then and says only that finishes normally from every state that satisfies .
The termination part needs, for each loop, an invariant and a function from states to a well-founded set: a set with an order in which every strictly decreasing sequence is finite, such as the whole numbers . Whenever and the loop condition hold, must be defined, and one run of the body must make strictly smaller. For recursion, a measure on the arguments plays the same role: it must decrease at every recursive call. Every called procedure must itself finish under its precondition.
Logics differ on runs that stop with a fault, such as a division by zero or an array §14.5 access out of bounds. In the classical reading a fault is not normal termination, so a partial-correctness triple says nothing about a faulting run, and a total-correctness triple excludes it. Many modern program logics, separation logic among them, make every triple also promise that no run that starts in a state satisfying faults. Which reading applies is part of the definition of the logic, not of the notation.
The rules of Hoare logic are sound: every triple they prove is valid. Stephen Cook showed in 1978 that they are also relatively complete. Every valid triple can be proved, provided that every true assertion may be used as a fact and that the assertion language can express the intermediate conditions the proof needs. The proviso cannot be dropped: for the whole numbers with addition and multiplication, no sound proof system whose proofs an algorithm §14.1 can check proves every true assertion.
Limits and common mistakes
A triple is only as strong as its postcondition. The triple is met by a program that sets every element to 0. The postcondition must also say that is a rearrangement of its original contents. That needs a name for the original value, such as a variable that the precondition sets equal to and that the program never changes.
A triple says what holds at the end, not what the program left alone. A proof about says nothing about unless the logic adds a frame condition, a statement of which variables or memory the program may modify; separation logic builds this in.
The assignment rule assumes that different names denote different storage. For arrays and pointers, also changes when , so the rule must treat the whole array as one value, or the logic must track which names can refer to the same storage.
In a concurrent program, another thread can change a shared variable between two steps and break an intermediate assertion. Concurrent programs need extensions such as the Owicki–Gries method or rely-guarantee reasoning, which state what other threads may do.
Assertions about mathematical integers do not carry over to fixed-width machine integers without conditions that rule out overflow, as the 8-bit example shows.
A triple says nothing about intermediate states, running time or memory use, or about what happens when is interrupted halfway.
Tests cannot supply the missing termination argument: no finite set of runs that finished shows that every run finishes.
Questions answered
What extra argument turns partial correctness into total correctness?
The extra argument is a termination proof: every run of the program that starts in a state satisfying the precondition finishes normally. Partial correctness says only that if the program finishes, the postcondition holds; total correctness is exactly partial correctness plus this termination property. For each loop, the usual argument is a ranking function, an integer (or other well-founded) quantity that is at least 0 whenever the loop invariant and the loop condition hold and that strictly decreases on every iteration, so the loop cannot run forever. For the loop while i < n do i := i + 1, the quantity works. For recursion, a measure on the arguments must decrease at every recursive call, and every called procedure must itself be proved to finish under its precondition. If the logic counts a fault, such as a division by zero or an out-of-bounds access, as a way of not finishing normally, the argument must also show that no fault occurs.
Related entries: Loop invariant (§3.24), Ranking function (§3.25), Helper contract (§2.21), Signed integer overflow (§4.15), Termination versus time bound (§2.33). Further reading: [224, 225, 268].