Example Worked Example: Euclidean GCD Step Count
example formalized

Worked Example: Euclidean GCD Step Count

The simplest CostM instance: gcd in CostM ℕ, charging one unit per modulus operation. Two bounds are proved:

  • gcd_cost_le(gcd a b).cost ≤ b (a trivial linear bound), and
  • gcd_cost_log_le(gcd a b).cost ≤ 2 · \log_2 b + 1, the textbook logarithmic bound following from the fact that two consecutive remainders at least halve the modulus.

This is the canonical "C = ℕ, additive, counts one resource" use of the monad (The CostM Complexity Monad): the cost field accumulates the number of mod steps, and the bound lives purely on .cost while the returned gcd value lives on .ret.

Lean declarations

  • GCD.gcd : ℕ → ℕ → CostM ℕ ℕ — the instrumented algorithm.
  • GCD.gcd_cost_le, GCD.gcd_cost_log_le — the linear and logarithmic step bounds (the latter via a private mod_halves halving lemma).

References

Also in