Math 2.0

Computer science Definition

Weakest precondition

Builds on Hoare logicOperational semanticsSubstitution

Definition

Let CC be a command of an imperative language with a set of states Σ\Sigma and an operational semantics. Let QQ be a predicate on Σ\Sigma, identified with the set of states that satisfy it. The weakest precondition wp(C,Q)\mathrm{wp}(C, Q) is the set of states s∈Σs \in \Sigma with this property. Every run of CC from ss terminates, and it ends in a state satisfying QQ. Equivalently, wp(C,Q)\mathrm{wp}(C, Q) is the predicate PP such that:

  1. every run of CC from a state satisfying PP terminates in a state satisfying QQ;
  2. every predicate P′P' with property 1 implies PP.

Remarks

Example: for the assignment x:=Ex := E, one has wp(x:=E,Q)=Q[E/x]\mathrm{wp}(x := E, Q) = Q[E/x], the substitution of EE for xx in QQ, provided EE is always defined. So wp(x:=x−3, x>0)\mathrm{wp}(x := x - 3,\ x > 0) is x−3>0x - 3 > 0, that is, x>3x > 3. Property 1 holds since from x>3x > 3 the new value is positive, and property 2 holds since any state with x≤3x \le 3 ends with x≤0x \le 0.

Non-example: the predicate x>5x > 5 satisfies property 1 for x:=x−3x := x - 3 and x>0x > 0, but not property 2, since x=4x = 4 also works. So x>5x > 5 is a precondition but not the weakest one.

Other standard rules are wp(C1;C2,Q)=wp(C1,wp(C2,Q))\mathrm{wp}(C_1; C_2, Q) = \mathrm{wp}(C_1, \mathrm{wp}(C_2, Q)) and wp(𝗂𝖿 B 𝗍𝗁𝖾𝗇 C1 𝖾𝗅𝗌𝖾 C2,Q)=(B∧wp(C1,Q))∨(¬B∧wp(C2,Q))\mathrm{wp}(\mathsf{if}\ B\ \mathsf{then}\ C_1\ \mathsf{else}\ C_2, Q) = (B \land \mathrm{wp}(C_1, Q)) \lor (\lnot B \land \mathrm{wp}(C_2, Q)). For a loop, wp\mathrm{wp} is a least fixed point and in practice is approximated with an invariant. The weakest liberal precondition wlp(C,Q)\mathrm{wlp}(C, Q) drops the termination requirement and matches partial correctness: the Hoare triple {P} C {Q}\set{P}\, C\, \set{Q} is valid exactly when PP implies wlp(C,Q)\mathrm{wlp}(C, Q).

Reading path

To understand this page from the ground up, read these 14 pages in order. The foundations come first.

Show the path as a graph
Each page uses the pages above it. The colour of the bar on a box shows how strong the evidence is, and the box also gives it as text. The list has the same pages.
  • no evidence level
  1. Binary relation concept
  2. Proposition concept
  3. Substitution concept
  4. Operational semantics concept
  5. Conjunction definition
  6. Disjunction definition
  7. Implication definition
  8. Negation definition
  9. Predicate definition
  10. Biconditional definition
  11. Propositional logic concept
  12. Predicate logic concept
  13. Hoare logic concept
  14. Weakest precondition definition

Further reading

  1. Web https://en.wikipedia.org/wiki/Predicate_transformer_semantics
About this page
Tag4H635X7K1E
URIm2://4H635X7K1E
Kinddefinition
WritingW0 stub
MSC68Q60, 03B70
DefinesWeakest precondition
UsesHoare logic, Operational semantics, Substitution
Changed3 November 2026

Markdown · JSON · Edit on GitHub

What this page needs for W1

  • an inventory row: the row of the inventory that the node accounts for (0 of 1)
  • a source with a locator (0 of 1)

The writing levels say how complete the text of a page is. They do not measure its truth.

What links here

No page links to this page yet.