Skip to content

Floyd-Hoare Logic

Logic to reason about program behavior, ultimately with the aim of proving program correctness.

Hoare triples

\(\{P\}\ S\ \{Q\}\) says that if \(P\) holds before \(S\) runs, and \(S\) terminates, then \(Q\) holds after. \(P\) is the precondition, and \(Q\) is the postcondition.

This is partial correctness. For total correctness, you also need program termination.

Rules

\[ \dfrac{}{\{P\}\ \text{skip}\ \{P\}}\ \text{(Skip)} \]
\[ \dfrac{}{\{Q[e/x]\}\ x := e\ \{Q\}}\ \text{(Assign)} \]
\[ \dfrac{\{P\}\ S_1\ \{R\} \qquad \{R\}\ S_2\ \{Q\}}{\{P\}\ S_1;\ S_2\ \{Q\}}\ \text{(Seq)} \]
\[ \dfrac{\{P \land G\}\ S_1\ \{Q\} \qquad \{P \land \lnot G\}\ S_2\ \{Q\}} {\{P\}\ \text{if}\ G\ \text{then}\ S_1\ \text{else}\ S_2\ \{Q\}}\ \text{(If)} \]
\[ \dfrac{\{I \land G\}\ S\ \{I\}}{\{I\}\ \text{while}\ G\ \text{do}\ S\ \{I \land \lnot G\}}\ \text{(While)} \]
\[ \dfrac{P \implies P' \qquad \{P'\}\ S\ \{Q'\} \qquad Q' \implies Q}{\{P\}\ S\ \{Q\}}\ \text{(Conseq)} \]
Why is the substitution in the precondition of (Assign)?

\(Q[e/x]\) is \(Q\) with every \(x\) replaced by \(e\). (Assign) goes backwards, from what you want after to what you need before. For example, to get \(\{x > 5\}\) after x := x + 1, you need \(\{x + 1 > 5\}\) before. That's why the substitution is in the precondition, since after the assignment, \(x\) holds the value of \(e\) from before.

If you put the substitution in the postcondition instead,

\[ \dfrac{}{\{P\}\ x := e\ \{P[e/x]\}}\ \text{(Wrong)} \]

you can prove false things. Take \(P \equiv x = 0\) and x := x + 1.

\[ \{x = 0\}\ x := x + 1\ \{x + 1 = 0\} \]

After the assignment \(x = 1\), so \(x + 1 = 2\), not \(0\). \(P\) talks about the old \(x\), but in the postcondition \(x\) already means the new one.

Composition

The main sell of Hoare logic is that proofs compose. Every rule builds a triple for a bigger program out of triples for its parts. (Seq) glues \(S_1\) and \(S_2\) through \(R\), (If) glues the two branches, and (While) glues one iteration into the whole loop.

So the proof has the same shape as the program, and you never have to reason about the whole program at once. It also means you can prove a function once, against its precondition and postcondition, and reuse that triple wherever it's called without looking inside it again.

Verifying with weakest preconditions

The rules tell you whether a proof is valid, but not how to find one. Weakest preconditions make it mechanical.

\(\text{wp}(S, Q)\) is the weakest \(P\) such that \(\{P\}\ S\ \{Q\}\) holds. To verify \(\{P\}\ S\ \{Q\}\), compute \(\text{wp}(S, Q)\) and check

\[ P \implies \text{wp}(S, Q) \]

Each rule turns into a function that walks the program backwards.

\[ \begin{aligned} \text{wp}(\text{skip}, Q) &= Q \\ \text{wp}(x := e, Q) &= Q[e/x] \\ \text{wp}(S_1;\ S_2, Q) &= \text{wp}(S_1, \text{wp}(S_2, Q)) \\ \text{wp}(\text{if}\ G\ \text{then}\ S_1\ \text{else}\ S_2, Q) &= (G \implies \text{wp}(S_1, Q)) \land (\lnot G \implies \text{wp}(S_2, Q)) \end{aligned} \]

For example, to verify

\[ \{x > 4\}\ x := x + 1;\ y := 2x\ \{y > 10\} \]

walk backwards from the postcondition.

\[ \begin{aligned} \text{wp}(y := 2x, y > 10) &= 2x > 10 \\ \text{wp}(x := x + 1, 2x > 10) &= 2(x + 1) > 10 \equiv x > 4 \end{aligned} \]

Then \(x > 4 \implies x > 4\), so the triple holds.

Loops are the exception, since \(\text{wp}\) of a loop can't be computed in general. You supply the invariant \(I\), and the loop becomes three checks instead.

\[ \begin{aligned} &P \implies I \\ &I \land G \implies \text{wp}(S, I) \\ &I \land \lnot G \implies Q \end{aligned} \]

These are the initialisation, maintenance and termination steps from inductive invariants. Tools like Dafny work roughly this way. They generate these checks and hand them to an SMT solver.

Strongest postcondition

You can also go forwards. \(\text{sp}(S, P)\) is the strongest \(Q\) such that \(\{P\}\ S\ \{Q\}\) holds, and you verify by checking \(\text{sp}(S, P) \implies Q\). It's less common, because (Assign) needs an existential for the old value of \(x\).

\[ \text{sp}(x := e, P) = \exists x_0.\ P[x_0/x] \land x = e[x_0/x] \]