13. Specify behaviour before proving it
A type-correct program may still implement the wrong requirement. Verification begins by choosing a statement precise enough to express the intended behaviour. The proof comes afterwards.
A successful withdrawal accounts for the original balance. Every affordable withdrawal succeeds, and every excessive withdrawal fails. Proving only the first property would permit a broken implementation that always returns failure, because it would have no successful cases to contradict the theorem.
Define the operation and state its properties
def withdraw (balance amount : Nat) : Option Nat :=
if amount ≤ balance then some (balance - amount) else none
theorem withdrawSuccess (balance amount : Nat) (h : amount ≤ balance) :
withdraw balance amount = some (balance - amount) := by
simp [withdraw, h]
theorem withdrawFailure (balance amount : Nat) (h : balance < amount) :
withdraw balance amount = none := by
have hn : ¬ amount ≤ balance := by omega
simp [withdraw, hn]
theorem withdrawAccounts (balance amount result : Nat)
(h : withdraw balance amount = some result) :
result + amount = balance := by
unfold withdraw at h
split at h
· rename_i affordable
have same : balance - amount = result := Option.some.inj h
omega
· contradiction
The conservation proof uses the same case split as the implementation. In the successful branch, injectivity of some identifies the result, and arithmetic establishes conservation. In the failing branch, the hypothesis contradicts the fact that none and some result are distinct.
The theorem covers the pure function on natural-number units. Check separately that the parser, database, or external payment system uses those units and calls the function correctly.
Strengthen the success specification
theorem withdrawExact (balance amount result : Nat) :
withdraw balance amount = some result ↔
amount ≤ balance ∧ result = balance - amount := by
by_cases h : amount ≤ balance
· simp [withdraw, h, eq_comm]
· simp [withdraw, h]
by_cases h : amount ≤ balance creates one branch with evidence of affordability and one with evidence of its negation. Supplying h to simp resolves the corresponding conditional in each branch. eq_comm aligns the direction of the result equality.
The forward direction shows that a successful withdrawal was affordable and returned the correct balance. The reverse direction shows that an affordable withdrawal returns that balance. Callers can use the equivalence to reason about the result without unfolding the definition.
Choose theorem names that reveal which operation and property they describe. A collection of small interface lemmas often survives implementation changes better than proofs scattered throughout callers that unfold every detail.
Counterexamples help improve contracts
Suppose you propose that depositing and withdrawing the same amount always returns the original balance. Specify the order: a deposit makes the subsequent withdrawal affordable, while withdrawing first requires enough funds in the original balance.
Before attempting a proof, test a balance of 0, an amount of 0, exact depletion, and excessive withdrawal.
A failed attempt can reveal that the statement is false, the available assumptions are too weak, or the proof approach is inappropriate. Separate these possibilities by evaluating concrete examples, then examining the symbolic goal.
Exercises
- Prove
withdraw b 0 = some b. - Prove
withdraw b b = some 0. - Prove
withdraw (b + a) a = some b. - Explain why conservation alone accepts an implementation that always fails.
These exercises check whether the specification covers the expected success and failure cases.