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
| Rule | Formula | How to say it |
|---|---|---|
| skip | Does nothing → requirement unchanged | |
| assignment | You take the goal and undo the assignment in your head | |
| sequence | it is about chaining two statements together by finding an intermediate condition. | |
| if-else | Both branches must reach the goal | |
| while | needs invariant | Can’t compute automatically |
Assignment example: Goal , statement x := x+1
→ Replace with : →
3. Worked Example
Verify: x := x+1; y := x*2
| Step | Action | Result |
|---|---|---|
| Start | Postcondition | |
| 1 | Undo y := x*2: replace with | |
| 2 | Undo 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:
| Condition | Formula | Meaning |
|---|---|---|
| Initiation | Invariant holds at start | |
| Preservation | Loop body maintains it | |
| Exit | Exiting gives postcondition |
5. Summary
| What | = weakest condition before to guarantee after |
| How | Substitute backwards through each statement |
| Theorem | valid iff |
| Loops | Need invariant; verify 3 conditions |
| Benefit | Automates 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”