EconCSLib.Foundation.CostM.Visited #
The "memoization-shaped" cost monoid for CostM: an AddMonoid whose
elements are finite sets of sub-problem indices, with + := (· ∪ ·) and
0 := ∅.
The defining feature is idempotence (s + s = s): repeated visits to
the same sub-problem do not enlarge the recorded cost. This is the
algebraic shadow of memoization — when the cost type is Visited A, the
recorded cost is the distinct set of sub-problems touched, regardless of
whether the algorithm actually memoizes.
Type synonym, not direct instance #
Visited A is a type synonym for Finset A. We do not give the
union-monoid as a direct instance on Finset A because Mathlib's
Mathlib.Algebra.Group.Pointwise.Finset.Basic already provides a scoped
Zero (Finset A) := ⟨{0}⟩ (the singleton of the underlying zero, not
∅). The two interpretations are incompatible, so we firewall instance
resolution behind a fresh type name. Cf. Mathlib's Additive /
Multiplicative / OrderDual.
Use site #
import EconCSLib.Foundation.CostM
import EconCSLib.Foundation.CostM.Visited
def alg : ℕ → CostM (Visited ℕ) Result := …
Tick Visited.singleton i whenever sub-problem i is touched; the
recorded cost will be exactly the set of indices reached. See
Examples/CostM/MemoFib.lean for a worked example.
Equations
- Visited.instZero = { zero := ∅ }
Equations
- Visited.instAdd = { add := fun (a b : Visited A) => Visited.ofFinset (a.toFinset ∪ b.toFinset) }
Equations
- Visited.instAddMonoid = { toAdd := Visited.instAdd, add_assoc := ⋯, toZero := Visited.instZero, zero_add := ⋯, add_zero := ⋯, nsmul := nsmulRec, nsmul_zero := ⋯, nsmul_succ := ⋯ }