Prerequisites
Sequential program semantics, mathematical induction, logic and reasoning with unbounded integers.
Use mathematical integers with no overflow and input n >= 0. Set i = 0 and total = 0. WHILE i < n: SET i TO i + 1; SET total TO total + i; END. At each loop head, invariant: 0 <= i <= n and total = i(i + 1)/2.
Worked example
For n = 3, loop-head states (i, total) are (0,0), (1,1), (2,3), (3,6). The invariant holds initially, survives each iteration and, with the false guard, gives total = n(n + 1)/2.
Further inquiry
Prove initialization and preservation algebraically, then derive the postcondition from the invariant and false guard. Prove termination with the nonnegative decreasing integer n - i. Compare this model with fixed-width arithmetic and explain the extra preconditions or checks needed to avoid overflow.
Review criteria
- State the execution model and the domain of every input.
- Justify each implication, update order and boundary case rather than only checking examples.
- Distinguish correctness on termination from an argument that termination occurs.