Algorithm Search⌘K

Definitioncore

Strongest postcondition

Also called strongest post-condition.

The most precise condition that holds after a command runs from a state satisfying a given precondition, the forward dual of wp.

The strongest postcondition sp(S,P)\mathrm{sp}(S, P) of a command SS and a precondition PP is the set of all states in which a run of SS can stop when it starts in a state of PP. Every true postcondition of SS from PP contains it, so it is the most precise one. It is computed forwards through the code, and it is the dual of the weakest liberal precondition: sp(S,P)⊆Q\mathrm{sp}(S, P) \subseteq Q holds exactly when P⊆wlp(S,Q)P \subseteq \mathrm{wlp}(S, Q). Symbolic execution, forward verification condition generation and abstract interpretation all compute it or approximate it.

Definition

Use the notation of the weakest precondition page. A state σ\sigma gives an integer to each variable. A predicate is a set of states, written as a formula; P∧QP \wedge Q is the intersection, P∨QP \vee Q the union, and P  ⟹  QP \implies Q means P⊆QP \subseteq Q. The commands are

S::=skip∣x:=e∣havoc x∣assume b∣S1;S2∣if b then S1 else S2∣while b do S.S ::= \mathbf{skip} \mid x := e \mid \mathbf{havoc}\ x \mid \mathbf{assume}\ b \mid S_1; S_2 \mid \mathbf{if}\ b\ \mathbf{then}\ S_1\ \mathbf{else}\ S_2 \mid \mathbf{while}\ b\ \mathbf{do}\ S.

havoc x\mathbf{havoc}\ x gives xx any value, and assume b\mathbf{assume}\ b stops the run with no final state when bb is false. The meaning [ ⁣[S] ⁣][\![S]\!] of SS is the set of pairs (σ,σ′)(\sigma, \sigma') such that some run of SS from σ\sigma stops in σ′\sigma'.

Definition. For a command SS and a predicate PP,

sp(S,P)={σ′∣there is σ∈P with (σ,σ′)∈[ ⁣[S] ⁣]}.\mathrm{sp}(S, P) = \{\sigma' \mid \text{there is } \sigma \in P \text{ with } (\sigma, \sigma') \in [\![S]\!]\}.

So sp(S,P)\mathrm{sp}(S, P) is the image of PP under the relation [ ⁣[S] ⁣][\![S]\!]. The partial correctness triple {P} S {Q}\{P\}\ S\ \{Q\} holds exactly when sp(S,P)  ⟹  Q\mathrm{sp}(S, P) \implies Q. It follows that sp(S,P)\mathrm{sp}(S, P) is the strongest QQ for which the triple holds, which gives the name. Runs that do not stop add nothing, so sp, like wlp, is about partial correctness only.

Duality with wlp. For all SS, PP and QQ,

sp(S,P)  ⟹  Qif and only ifP  ⟹  wlp(S,Q).\mathrm{sp}(S, P) \implies Q \quad\text{if and only if}\quad P \implies \mathrm{wlp}(S, Q).

Proof. Both sides say the same thing: for every σ∈P\sigma \in P and every σ′\sigma' with (σ,σ′)∈[ ⁣[S] ⁣](\sigma, \sigma') \in [\![S]\!], σ′∈Q\sigma' \in Q. The left side says it because sp(S,P)\mathrm{sp}(S, P) is the set of such σ′\sigma'. The right side says it because wlp(S,Q)\mathrm{wlp}(S, Q) is the set of σ\sigma all of whose final states are in QQ. □\square

In the words of order theory, sp(S,⋅)\mathrm{sp}(S, \cdot) and wlp(S,⋅)\mathrm{wlp}(S, \cdot) form a Galois connection on the lattice of predicates, with sp as the lower adjoint. Three facts follow at once. First, sp is monotone. Second, sp preserves every union: sp(S,⋁iPi)=⋁isp(S,Pi)\mathrm{sp}(S, \bigvee_i P_i) = \bigvee_i \mathrm{sp}(S, P_i), and sp(S,false)=false\mathrm{sp}(S, \mathit{false}) = \mathit{false}. Third, sp(S,wlp(S,Q))  ⟹  Q\mathrm{sp}(S, \mathrm{wlp}(S, Q)) \implies Q and P  ⟹  wlp(S,sp(S,P))P \implies \mathrm{wlp}(S, \mathrm{sp}(S, P)).

Assignment. Let P[x0/x]P[x_0/x] be PP with every free xx replaced by a fresh variable x0x_0. Then

sp(x:=e,P)=∃x0. P[x0/x]∧x=e[x0/x].\mathrm{sp}(x := e, P) = \exists x_0 .\ P[x_0/x] \wedge x = e[x_0/x].

Proof. The command maps σ\sigma to σ′=σ[x↦e(σ)]\sigma' = \sigma[x \mapsto e(\sigma)]. Suppose σ′=σ[x↦e(σ)]\sigma' = \sigma[x \mapsto e(\sigma)] for some σ∈P\sigma \in P. Take x0=σ(x)x_0 = \sigma(x). The other variables agree in σ\sigma and σ′\sigma', so σ′\sigma' with x0x_0 set to σ(x)\sigma(x) satisfies P[x0/x]P[x_0/x], because that formula reads the old value of xx from x0x_0. For the same reason e[x0/x]e[x_0/x] has the value e(σ)e(\sigma), which is σ′(x)\sigma'(x). So σ′\sigma' satisfies the right side. Conversely, suppose σ′\sigma' and a value vv for x0x_0 satisfy P[x0/x]∧x=e[x0/x]P[x_0/x] \wedge x = e[x_0/x]. Let σ=σ′[x↦v]\sigma = \sigma'[x \mapsto v]. Then σ∈P\sigma \in P, and e(σ)e(\sigma) equals the value of e[x0/x]e[x_0/x], which is σ′(x)\sigma'(x). So σ′=σ[x↦e(σ)]\sigma' = \sigma[x \mapsto e(\sigma)] is a final state from PP. □\square

The duality makes this the mirror of the rule wlp(x:=e,Q)=Q[e/x]\mathrm{wlp}(x := e, Q) = Q[e/x]. The forward rule needs the quantifier, because the old value of xx is lost. When the assignment can be undone, the quantifier goes away: for x:=x+cx := x + c, the only possible old value is x−cx - c, so sp(x:=x+c,P)=P[x−c/x]\mathrm{sp}(x := x + c, P) = P[x - c/x].

The other commands. Each rule follows from the definition of [ ⁣[S] ⁣][\![S]\!].

  • sp(skip,P)=P\mathrm{sp}(\mathbf{skip}, P) = P and sp(assume b,P)=P∧b\mathrm{sp}(\mathbf{assume}\ b, P) = P \wedge b.
  • sp(havoc x,P)=∃x. P\mathrm{sp}(\mathbf{havoc}\ x, P) = \exists x .\ P.
  • sp(S1;S2,P)=sp(S2,sp(S1,P))\mathrm{sp}(S_1; S_2, P) = \mathrm{sp}(S_2, \mathrm{sp}(S_1, P)), since the meaning of a sequence is the composition of relations.
  • sp(if b then S1 else S2,P)=sp(S1,P∧b)∨sp(S2,P∧¬b)\mathrm{sp}(\mathbf{if}\ b\ \mathbf{then}\ S_1\ \mathbf{else}\ S_2, P) = \mathrm{sp}(S_1, P \wedge b) \vee \mathrm{sp}(S_2, P \wedge \neg b). With this rule the formula of a program with nn branches in sequence can have 2n2^n disjuncts, one for each path.
  • sp(while b do S,P)=¬b∧H\mathrm{sp}(\mathbf{while}\ b\ \mathbf{do}\ S, P) = \neg b \wedge H, where HH is the least fixed point of F(X)=P∨sp(S,X∧b)F(X) = P \vee \mathrm{sp}(S, X \wedge b).

Proof of the loop rule. Let HkH_k be the set of states at the loop head after exactly kk iterations of a run from PP. Then H0=PH_0 = P and Hk+1=sp(S,Hk∧b)H_{k+1} = \mathrm{sp}(S, H_k \wedge b). The loop stops in σ′\sigma' exactly when σ′\sigma' is in some HkH_k and fails bb, so the result is ¬b∧⋁kHk\neg b \wedge \bigvee_k H_k. Because sp preserves unions, FF preserves the union of every increasing chain, and by the Kleene fixed-point theorem its least fixed point is ⋁kFk(false)\bigvee_k F^k(\mathit{false}). By induction, Fk+1(false)=H0∨⋯∨HkF^{k+1}(\mathit{false}) = H_0 \vee \dots \vee H_k, so the least fixed point is ⋁kHk\bigvee_k H_k. □\square

The set HH is the strongest loop invariant at the head: every inductive invariant II with P  ⟹  IP \implies I and sp(S,I∧b)  ⟹  I\mathrm{sp}(S, I \wedge b) \implies I is a prefixed point of FF, so H  ⟹  IH \implies I. This is why loop invariants are needed. The fixed point is a set of states, but in general no formula of the assertion language describes it.

What is decidable. For a loop-free command over linear integer arithmetic, sp(S,P)\mathrm{sp}(S, P) is a formula of Presburger arithmetic, with one existential quantifier for each assignment and havoc. Presburger arithmetic is decidable, so the triple {P} S {Q}\{P\}\ S\ \{Q\} is decidable for such commands; real tools give sp(S,P)∧¬Q\mathrm{sp}(S, P) \wedge \neg Q to an SMT solver, which treats the quantified variables as fresh constants. With products of variables, validity is undecidable by Hilbert's tenth problem. With loops, the triple is undecidable even over linear arithmetic, by Rice's theorem, since it can express that a program never reaches a given point.

Examples and non-examples

  • An assignment that can be undone. From PP: x=2x = 2, the command x:=x+1x := x + 1 gives ∃x0. x0=2∧x=x0+1\exists x_0 .\ x_0 = 2 \wedge x = x_0 + 1, which is x=3x = 3. The short rule P[x−1/x]P[x - 1/x] gives x−1=2x - 1 = 2, the same set.
  • An assignment that forgets. From x>0∧y=xx > 0 \wedge y = x, the command x:=5x := 5 gives ∃x0. x0>0∧y=x0∧x=5\exists x_0 .\ x_0 > 0 \wedge y = x_0 \wedge x = 5, which is y>0∧x=5y > 0 \wedge x = 5. The fact about the old xx survives through yy.
  • A quantifier that does not go away. From true\mathit{true}, the command x:=2xx := 2x gives ∃x0. x=2x0\exists x_0 .\ x = 2x_0: "xx is even". Presburger arithmetic eliminates the quantifier only with a divisibility predicate, 2∣x2 \mid x; no formula built from linear inequalities alone describes the even numbers.
  • A loop. From n≥0n \ge 0, the program x:=0; while x<n do x:=x+1x := 0;\ \mathbf{while}\ x < n\ \mathbf{do}\ x := x + 1 has the head states H=0≤x≤nH = 0 \le x \le n. This HH satisfies F(H)=HF(H) = H: from 0≤x≤n∧x<n0 \le x \le n \wedge x < n, the step x:=x+1x := x + 1 gives 1≤x≤n1 \le x \le n, and the initial states add x=0x = 0. The strongest postcondition is x≥n∧0≤x≤nx \ge n \wedge 0 \le x \le n, which is x=n∧n≥0x = n \wedge n \ge 0.
  • A run that does not stop. sp(while true do skip,P)=false\mathrm{sp}(\mathbf{while}\ \mathit{true}\ \mathbf{do}\ \mathbf{skip}, P) = \mathit{false} for every PP. So sp(S,P)  ⟹  Q\mathrm{sp}(S, P) \implies Q never proves termination.
  • Non-example: conjoining without renaming. The formula P∧x=eP \wedge x = e is not the strongest postcondition. From x=2x = 2, the command x:=x+1x := x + 1 would give x=2∧x=x+1x = 2 \wedge x = x + 1, which is false, and the triple with postcondition false\mathit{false} does not hold.
  • Non-example: wp run forwards. The backward rule used forwards, Q=P[e/x]Q = P[e/x], is wrong too. From x=2x = 2, the command x:=x+1x := x + 1 would give x+1=2x + 1 = 2, that is x=1x = 1.
  • Non-example: sp does not preserve intersections. For S=(x:=0)S = (x := 0), P1=(x=1)P_1 = (x = 1) and P2=(x=2)P_2 = (x = 2): sp(S,P1∧P2)=sp(S,false)=false\mathrm{sp}(S, P_1 \wedge P_2) = \mathrm{sp}(S, \mathit{false}) = \mathit{false}, but sp(S,P1)∧sp(S,P2)=(x=0)\mathrm{sp}(S, P_1) \wedge \mathrm{sp}(S, P_2) = (x = 0). Only unions are preserved, as for every lower adjoint.
  • Non-example: going forwards and back does not return. For S=(x:=0)S = (x := 0) and Q=(x≥0)Q = (x \ge 0), wlp(S,Q)=true\mathrm{wlp}(S, Q) = \mathit{true}, but sp(S,true)=(x=0)\mathrm{sp}(S, \mathit{true}) = (x = 0), which is strictly stronger than QQ.
  • Weakest precondition. The weakest precondition works backwards from the postcondition, and its liberal form wlp is the upper adjoint of sp. A backward VC generator uses wp, since its assignment rule is a substitution with no quantifier. A forward one uses sp.
  • Symbolic execution. Symbolic execution computes sp one path at a time. The path condition of a path, together with its symbolic store, is the sp of the path with the old values named by input symbols in place of ∃\exists.
  • Collecting semantics and abstract interpretation. The collecting semantics at a program point is the sp of the code that leads there. Abstract interpretation computes an over-approximation of it in an abstract domain, and predicate abstraction computes the best one over a fixed set of predicates.
  • Inductive assertions. Floyd's method of inductive assertions used the forward assignment rule with the existential quantifier. A loop invariant is a predicate that contains the head states HH of the loop rule, and loop invariant synthesis searches for one that a solver can handle.
  • Expressiveness. Cook called an assertion language expressive when it can describe sp(S,P)\mathrm{sp}(S, P) for every SS and PP, and proved Hoare logic complete relative to such a language.

References

  1. Robert W. Floyd, Assigning meanings to programs, in Mathematical Aspects of Computer Science, Proceedings of Symposia in Applied Mathematics 19, American Mathematical Society, 1967, pp. 19 to 32, the verification condition for an assignment operator, which introduces the existential quantifier over the old value
  2. Edsger W. Dijkstra and Carel S. Scholten, The strongest postcondition, chapter of Predicate Calculus and Program Semantics, Springer, 1990, pp. 209 to 215, and the chapter on converse predicate transformers before it, pp. 201 to 208
  3. Stephen A. Cook, Soundness and completeness of an axiom system for program verification, SIAM Journal on Computing 7(1), 1978, pp. 70 to 90, the definition of expressiveness by the strongest postcondition
  4. Cormac Flanagan and James B. Saxe, Avoiding exponential explosion, generating compact verification conditions, Proceedings of POPL 2001, pp. 193 to 205, Section 1, which notes that strongest postconditions and symbolic execution can also blow up

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/HFHFEB/text.yaml