Why Tests Miss Truncation—and Equations Catch It
Ask an AI coding assistant for a token-bucket rate limiter and a test suite, and both files will look clean. The tests pass limit = 100, used = 40 and limit = 100, used = 100. Because the same model wrote both implementation and tests, neither asks what happens after an administrative quota reduction lowers limit below used.
In Lean 4, every function must return a valid value of its return type for every possible input—a design guarantee we call a total function (earned here because there are no exceptions, null pointers, or undefined traps to hide behind). Making Nat - Nat : Nat total forces 0 - 1 = 0, known as saturating subtraction.
Instead of hoping callers remember used ≤ limit, we have two honest engineering options: use signed integers (Int), or return Option Nat (some remaining vs none) so failure is explicit in the return type and prove conservation across all valid withdrawals.
Your First Two Proof Tools: `rfl` and `omega`
When both sides of an equation compute to the exact same value—such as #guard (10 : Nat) - 12 = 0 or withdraw 10 4 = some 6—Lean closes the proof by computation (rfl, short for reflexivity).
When your claim universally quantifies over every limit used : Nat—infinitely many inputs—computation on concrete constants is no longer enough. Lean's linear integer/natural solver omega checks whether the arithmetic equation holds across all non-negative integers, and immediately flags missing bounds like used ≤ limit.