Example Worked Example: Constant-Space Boyer–Moore Majority
example formalized

Worked Example: Constant-Space Boyer–Moore Majority

The Boyer–Moore majority vote in CostM Cells (Peak-Memory Cost via the Cells Monoid), proving a constant peak-memory bound: regardless of input length, the peak working set is two cells — one candidate and one counter, $$ (\operatorname{majority}\,xs).\mathrm{cost}.\mathrm{peak} \;\le\; 2, $$ which is majority_peak_le. The mechanism is that the single-pass loop allocates nothing — loop_cost : (majority.loop \ldots).cost = 0 — so the only contributions to the peak come from one alloc 2 / free 2 pair bracketing the pass. Because peak (not sum) is the resource, the tropical Cells monoid is essential here; an additive cost cannot state this bound.

This example deliberately does not prove functional correctness (that majority returns the true majority when one exists). That omission is the point: in CostM the complexity bound lives on .cost and is provable in complete isolation from the .ret value, demonstrating the correctness/complexity decoupling described in The CostM Complexity Monad.

Lean declarations

  • BoyerMoore.majority : List α → CostM Cells (Option α) with its single-pass where loop carrying the candidate and counter.
  • BoyerMoore.loop_cost — the loop contributes zero cost.
  • BoyerMoore.majority_peak_le — the constant peak ≤ 2 bound.

References

Also in