Hoare logic: formal system to prove program correctness instead of testing.

Hoare Triple: — “If precondition holds and terminates, then postcondition holds”

2. Validity (45 sec)

TripleValid?Why?
i+=1 True always holds
i+=1 Precondition never satisfied → trivially valid
i+=1 then
i+=1 Counterexample:

3. Core Rules (1.5 min)

Assignment (read backwards): x := e

  • Ex: x:=x+1 → substitute →
  • You take the goal and undo the assignment in your head

Sequence:

  • it is about chaining two statements together by finding an intermediate condition.

While: — requires loop invariant

“For while loops, we need a loop invariant — a condition that is true before the loop starts, stays true after every iteration, and when the loop ends, combined with the loop being finished, gives us our postcondition.”

4. Loop Invariants (1 min)

Property that holds before and after every iteration:

Ex: while (i<n) {i+=1} — Invariant ? Check: i+=1

Finding good invariants is the hard part — must be strong enough for postcondition!

5. Properties (15 sec)

  • Sound: derivable → valid ✓
  • Not complete: some valid triples not derivable (undecidable)
  • Relatively complete (Cook ‘74): complete if invariants expressible & side conditions provable

All Rules (System H)

RuleForm
Skip skip
Assign x := e
Seq
Cond
While
Conseq if and

Common Invariant Patterns

  • Counter bounds:
  • Accumulator: where is partial computation
  • Relationship: (for multiplication by addition)