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 of a command and a precondition is the set of all states in which a run of can stop when it starts in a state of . Every true postcondition of from 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: holds exactly when . 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 gives an integer to each variable. A predicate is a set of states, written as a formula; is the intersection, the union, and means . The commands are
gives any value, and stops the run with no final state when is false. The meaning of is the set of pairs such that some run of from stops in .
Definition. For a command and a predicate ,
So is the image of under the relation . The partial correctness triple holds exactly when . It follows that is the strongest 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 , and ,
Proof. Both sides say the same thing: for every and every with , . The left side says it because is the set of such . The right side says it because is the set of all of whose final states are in .
In the words of order theory, and 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: , and . Third, and .
Assignment. Let be with every free replaced by a fresh variable . Then
Proof. The command maps to . Suppose for some . Take . The other variables agree in and , so with set to satisfies , because that formula reads the old value of from . For the same reason has the value , which is . So satisfies the right side. Conversely, suppose and a value for satisfy . Let . Then , and equals the value of , which is . So is a final state from .
The duality makes this the mirror of the rule . The forward rule needs the quantifier, because the old value of is lost. When the assignment can be undone, the quantifier goes away: for , the only possible old value is , so .
The other commands. Each rule follows from the definition of .
- and .
- .
- , since the meaning of a sequence is the composition of relations.
- . With this rule the formula of a program with branches in sequence can have disjuncts, one for each path.
- , where is the least fixed point of .
Proof of the loop rule. Let be the set of states at the loop head after exactly iterations of a run from . Then and . The loop stops in exactly when is in some and fails , so the result is . Because sp preserves unions, preserves the union of every increasing chain, and by the Kleene fixed-point theorem its least fixed point is . By induction, , so the least fixed point is .
The set is the strongest loop invariant at the head: every inductive invariant with and is a prefixed point of , so . 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, is a formula of Presburger arithmetic, with one existential quantifier for each assignment and havoc. Presburger arithmetic is decidable, so the triple is decidable for such commands; real tools give 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 : , the command gives , which is . The short rule gives , the same set.
- An assignment that forgets. From , the command gives , which is . The fact about the old survives through .
- A quantifier that does not go away. From , the command gives : " is even". Presburger arithmetic eliminates the quantifier only with a divisibility predicate, ; no formula built from linear inequalities alone describes the even numbers.
- A loop. From , the program has the head states . This satisfies : from , the step gives , and the initial states add . The strongest postcondition is , which is .
- A run that does not stop. for every . So never proves termination.
- Non-example: conjoining without renaming. The formula is not the strongest postcondition. From , the command would give , which is false, and the triple with postcondition does not hold.
- Non-example: wp run forwards. The backward rule used forwards, , is wrong too. From , the command would give , that is .
- Non-example: sp does not preserve intersections. For , and : , but . Only unions are preserved, as for every lower adjoint.
- Non-example: going forwards and back does not return. For and , , but , which is strictly stronger than .
Related definitions
- 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 .
- 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 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 for every and , and proved Hoare logic complete relative to such a language.
References
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag HFHFEB Edit this page History Report an error Markdown