Problem: Given program and desired result , what must be true before we run it?

Answer: Work backwards—“undo” each statement from the postcondition.

The function: = weakest precondition guaranteeing after

Core theorem:


2. The Five Rules

RuleFormulaHow to say it
skipDoes nothing → requirement unchanged
assignmentYou take the goal and undo the assignment in your head
sequenceit is about chaining two statements together by finding an intermediate condition.
if-elseBoth branches must reach the goal
whileneeds invariant Can’t compute automatically

Assignment example: Goal , statement x := x+1
→ Replace with :


3. Worked Example

Verify: x := x+1; y := x*2

StepActionResult
StartPostcondition
1Undo y := x*2: replace with
2Undo x := x+1: replace with

Check: ? ✓ Valid.


4. Loops (the tricky part) Do not make an example

Loops need a human-supplied invariant (undecidable in general).

Three conditions to verify:

ConditionFormulaMeaning
InitiationInvariant holds at start
PreservationLoop body maintains it
ExitExiting gives postcondition

5. Summary

What = weakest condition before to guarantee after
HowSubstitute backwards through each statement
Theorem valid iff
LoopsNeed invariant; verify 3 conditions
BenefitAutomates Hoare logic for loop-free code

Quick answers:

  • “Weakest” = accepts the most starting states; any stronger precondition also works
  • “w.r.t.” = “with respect to”