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 →

Sequence:

While: — requires loop invariant

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

Factorial Example

{n ≥ 0}  f := 1; i := 1;
while i ≤ n do ⟨f = fact(i-1) ∧ 1 ≤ i ≤ n+1⟩
  { f := f·i; i := i+1 }
{f = fact(n)}

Key Definitions

  • Valid: : if and then
  • Auxiliary vars: in / but not in (e.g., for initial value)
  • Loop unfolding: while do if then while do else skip

Proof Strategy

  1. Work bottom-up from conclusion
  2. Assignments: substitute backwards into postcondition
  3. While: identify invariant , show , conclude while
  4. Use consequence rule to bridge logical gaps
  5. Verify side conditions (, )

Common Invariant Patterns

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