Traces into a monoid #
Control.Trace ω X is the type X → ω for any monoid ω.
It is the universal carrier of "stateless effectful trace" data:
each input contributes one element of a fixed monoid ω, and pointwise
multiplication (via Pi.monoid) gives Trace ω X itself the structure of a
monoid. Nothing in this file requires a monad.
Why a monoid, not a monad #
Many bookkeeping / observation patterns share this exact shape:
QueryLog spec—ω = List ((t : spec.Domain) × spec.Range t), i.e. the free monoid on indices,QueryCount ι—ω = ι → ℕ, a commutative monoid under pointwise addition,QueryCache spec—ω = (t : spec.Domain) → Option (spec.Range t)with at-most-once write semigroup,BoundaryAction.emit—ω = TraceList Δ.Out(the polynomial-trace specialisation inPFunctor/Trace.lean),- abstract cost functions —
ωany commutative monoid (e.g.ℝ≥0).
The monoid axioms are exactly what is needed to turn "one contribution per input" into a meaningful aggregate over a sequence of inputs.
The companion file ToMathlib/PFunctor/Trace.lean specialises this at
ω = FreeMonoid (Idx P) for a polynomial functor P, recovering the
universal "log of P-events" trace, together with a universal property
Trace P X → Control.Trace ω X (toMonoid) coming from the free-monoid
universal property.
Conventions #
Trace is @[reducible] so that Pi.monoid and Pi.commMonoid apply
directly, and so that downstream definitions like QueryLog and QueryCount
can be exhibited as Trace-instances by rfl-clean abbreviations.
References #
- Bonchi, Di Lavore, Romàn — Effectful Mealy machines and Kleisli categories.
- Hancock, Setzer — Interactive programs and weakly final coalgebras in dependent type theory.
- Spivak, Niu — Polynomial Functors: A Mathematical Theory of Interaction, arXiv:2202.00534.
Trace ω X is the type X → ω of stateless valuations from inputs X into
a monoid ω. It inherits a monoid structure from Pi.monoid, with 1
the constant trace and * the pointwise product.
See the module docstring for motivation and the catalogue of instances
(QueryLog, QueryCount, ...) it is intended to subsume.
Instances For
Post-compose a trace with a monoid homomorphism.
Instances For
Bundle map φ as a monoid homomorphism on the trace monoid.
This is the manifest algebraic content of map: post-composing a trace with
a monoid hom is itself a monoid hom on the pointwise-monoid Trace ω X.