Documentation

EconCSLib.Foundation.CostM.Cells

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 0peak and deltapeak:

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 #

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.

structure Cells :

Tropical-style cost record: peak occupancy and net delta.

  • peak :

    Peak occupancy reached during the computation (≥ 0).

  • delta :

    Net change in occupancy from start to end (≤ peak).

  • zero_le_peak : 0 self.peak

    Peak is non-negative.

  • delta_le_peak : self.delta self.peak

    Delta never exceeds peak.

Instances For
    theorem Cells.ext_iff {x y : Cells} :
    x = y x.peak = y.peak x.delta = y.delta
    theorem Cells.ext {x y : Cells} (peak : x.peak = y.peak) (delta : x.delta = y.delta) :
    x = y
    @[implicit_reducible]
    Equations
    @[simp]
    theorem Cells.peak_zero :
    peak 0 = 0
    @[simp]
    theorem Cells.delta_zero :
    delta 0 = 0
    @[implicit_reducible]
    Equations
    @[simp]
    theorem Cells.peak_add (a b : Cells) :
    (a + b).peak = max a.peak (a.delta + b.peak)
    @[simp]
    theorem Cells.delta_add (a b : Cells) :
    (a + b).delta = a.delta + b.delta
    @[implicit_reducible]
    Equations
    • One or more equations did not get rendered due to their size.
    def Cells.alloc (n : ) :

    Allocate n cells: peak and delta both rise by n.

    Equations
    • Cells.alloc n = { peak := n, delta := n, zero_le_peak := , delta_le_peak := }
    Instances For
      def Cells.free (n : ) :

      Release n cells: peak unchanged, delta drops by n.

      Equations
      Instances For
        @[simp]
        theorem Cells.peak_alloc (n : ) :
        (alloc n).peak = n
        @[simp]
        theorem Cells.delta_alloc (n : ) :
        (alloc n).delta = n
        @[simp]
        theorem Cells.peak_free (n : ) :
        (free n).peak = 0
        @[simp]
        theorem Cells.delta_free (n : ) :
        (free n).delta = -n