Hoare Logic & Verified Algorithms
Preconditions, postconditions, weakest preconditions and machine-checked proofs.
Java sorts objects with TimSort, and the code had been tested for years. In 2015 a team that set out to prove it correct with a verification tool found that it could crash: an invariant it relied on did not always hold, and an input of 67,108,864 elements broke it. This lesson builds the logic behind such proofs: Hoare triples, weakest preconditions, loop invariants as proof rules, and the tools that check proofs by machine.
What you’ll learn
- Read and write Hoare triples, and apply the assignment axiom and the composition, conditional, while and consequence rules.
- Prove a small program totally correct in Hoare logic, with an invariant and a variant.
- Compute weakest preconditions, and explain soundness, relative completeness and the limits that undecidability imposes.
- Describe separation logic, and how verification tools are used on real algorithms.
Hoare triples
Hoare (1969), building on Floyd (1967), gave one rule for each construct of a small imperative language, so that a proof follows the shape of the program:
| Rule | Statement |
|---|---|
| assignment | , where is with substituted for |
| composition | from and , infer |
| conditional | from and , infer if then else |
| while | from , infer while do |
| consequence | from , and , infer |
The assignment axiom runs backward, which surprises everyone once. To guarantee after x := x + 1, substitute: the precondition is , that is . The tempting forward version, "", is wrong whenever mentions : after x := x + 1 the claim is false.
What is the weakest precondition that makes valid?
Show the answer
C.
Work backward. Through , the postcondition becomes . Through , that becomes , which is . Running the program from every start with and agrees.
A complete proof
Hoare's 1969 paper proved exactly this program: integer division of by by repeated subtraction.
{ x >= 0 and y > 0 }
r := x; q := 0;
while y <= r do
r := r - y; q := q + 1
{ x = q*y + r and 0 <= r < y }Choose the invariant: what the loop has achieved, plus the bound that will make meaningful at the end.
Initialization. Push backward through
q := 0, thenr := x, with the assignment axiom; the precondition implies the result, so consequence applies:Maintenance. Push backward through the body, and check that together with the loop condition implies it:
Exit. The while rule gives , which is the postcondition:
Termination. The variant is a natural number by , and each iteration lowers it by . The precondition is needed only here: with the partial-correctness proof still goes through, and the loop runs forever.
Weakest preconditions, and the limits
Dijkstra (1975) turned the rules into a calculus. The weakest precondition is the weakest assertion that guarantees that terminates in a state satisfying , so that holds exactly when . For loop-free code it is computed mechanically, backward:
This is how modern verifiers work. The programmer writes the specification and the loop invariants; the tool computes weakest preconditions backward through each loop-free stretch and emits verification conditions, formulas such as ; an automatic theorem prover, usually an SMT solver, discharges them or reports which one fails.
How good is the logic itself? It is sound: every derivable triple is true. Cook (1978) proved it relatively complete: every true triple is derivable, provided the assertion language can express the invariants needed and every true statement of the underlying arithmetic may be used as an axiom. The proviso cannot be dropped, since no proof system derives all truths of arithmetic (Gödel), and some richer language features admit no sound and relatively complete Hoare logic at all (Clarke, 1979).
Verified algorithms in practice
Pointers. Hoare logic assumes that different variables name different storage, which fails with pointers: after p.next = q, every other name for the same node sees the change. Separation logic (O'Hearn, Reynolds & Yang, 2001; Reynolds, 2002) adds a separating conjunction , meaning " and hold of disjoint parts of the heap", and the frame rule: from infer when does not modify the variables of . A proof about one list then stays valid next to any other data, and the idea underlies industrial analyzers such as Meta's Infer.
Tools. Auto-active verifiers such as Dafny and Why3 take code with annotations and discharge the verification conditions automatically. Interactive proof assistants, Coq (renamed Rocq in 2025), Isabelle/HOL and Lean, check every proof step against a small trusted kernel. Landmarks include the CompCert optimizing C compiler (Leroy, 2009) and the seL4 operating-system kernel (Klein et al., 2009), both with machine-checked proofs that the implementation meets its specification.
Key takeaways
- A Hoare triple promises after whenever starts in and terminates; the total-correctness triple also promises termination.
- The assignment axiom substitutes backward, ; the while rule is the loop-invariant method; consequence glues the steps together.
- Weakest preconditions compute proof obligations backward through loop-free code, and verifiers turn annotated programs into verification conditions for SMT solvers.
- Hoare logic is sound and relatively complete (Cook, 1978), but correctness is undecidable in general (Rice, Turing), so people supply invariants and machines check them.
- Separation logic extends the method to pointers, and proof tools have verified compilers, kernels and library algorithms, and found a real bug in TimSort.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
What is the weakest precondition that makes valid, for an integer ?
Show the answer
B.
Substitute for in the postcondition: , that is , or over the integers.
Which of these triples is valid?
Show the answer
A.
The loop never terminates, and a partial-correctness triple promises nothing about runs that do not end, so even the postcondition "false" is vacuously guaranteed. The total-correctness version is invalid, because it promises termination.
To apply the while rule to while B do C with invariant , what must you prove about the body ?
- , for every starting state
Show the answer
B.
The body runs only when holds, so you may assume , and you must re-establish . The rule then delivers after the loop.
Why do verifiers such as Dafny ask the programmer for loop invariants instead of always finding them automatically?
- Because invariants are only documentation
- Because SMT solvers cannot handle arithmetic
- Because correctness is undecidable in general (Rice's theorem), so no tool can always find the invariants
- Because loops have no weakest preconditions
Show the answer
C. Because correctness is undecidable in general (Rice's theorem), so no tool can always find the invariants
If invariants could always be computed, correctness and termination would be decidable, contradicting Rice's theorem and the halting problem. Tools infer many simple invariants and check the rest, but the general problem has no algorithm.
Further reading
- C. A. R. Hoare, "An axiomatic basis for computer programming," Communications of the ACM 12(10) (1969).
- E. W. Dijkstra, "Guarded commands, nondeterminacy and formal derivation of programs," Communications of the ACM 18(8) (1975).
- S. A. Cook, "Soundness and completeness of an axiom system for program verification," SIAM Journal on Computing 7(1) (1978).
- J. C. Reynolds, "Separation logic: A logic for shared mutable data structures," Proceedings of LICS (2002).
- S. de Gouw, J. Rot, F. S. de Boer, R. Bubel & R. Hähnle, "OpenJDK's java.utils.Collection.sort() is broken: The good, the bad and the worst case," Proceedings of CAV (2015).