EconCSLib.Foundation.CostM.Cells #
The "tropical-shaped" cost monoid for peak-style resource analysis.
A Cells value carries two integers, peak and delta, with invariants
0 ≤ peak and delta ≤ peak:
peak— maximum occupancy reached during a computation, relative to the occupancy at the start of the computation.delta— net change in occupancy from start to end. Negative if the computation ends with fewer live cells than it started (freeexceedsalloc).
Composition
(p₁, d₁) ⋆ (p₂, d₂) = (max p₁ (d₁ + p₂), d₁ + d₂)
encodes sequential semantics: the new peak is whichever was higher — the first computation's peak, or the second computation's peak shifted up by the first computation's net offset.
This is not an additive monoid in the elementwise sense — + does
not just add componentwise; the peak component composes "tropically"
via max. But the resulting structure still satisfies the AddMonoid
laws, which is all CostM requires.
Primitives #
alloc n := ⟨n, n, _, _⟩— claimncells; both peak and delta rise byn.free n := ⟨0, -n, _, _⟩— releasencells; peak unchanged, delta drops.
Bound shape #
For an algorithm
do alloc K; ⟨body with zero ticks⟩; free K; pure result
the resulting cost has .peak = K. When K is independent of input size
this gives an O(1) (constant-space) bound. The Boyer-Moore example in
Examples/CostM/BoyerMoore.lean demonstrates this shape with K = 2.
Equations
- Cells.instZero = { zero := { peak := 0, delta := 0, zero_le_peak := Cells.instZero._proof_1, delta_le_peak := Cells.instZero._proof_1 } }
Equations
- One or more equations did not get rendered due to their size.
Release n cells: peak unchanged, delta drops by n.
Equations
- Cells.free n = { peak := 0, delta := -↑n, zero_le_peak := Cells.instZero._proof_1, delta_le_peak := ⋯ }