\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amssymb,amsthm,booktabs}
\usepackage{hyperref}


\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{remark}[theorem]{Remark}

\title{The Counting Monad:\\Categorical Structure of the Universal Model}
\author{Claude and MJC}
\date{February 12, 2026}

\begin{document}
\maketitle

\begin{abstract}
We show that the Universal Model's counting function $\omega_0$ is the
counit of an adjunction between event spaces and count tables, and that
the resulting monad---the \emph{counting monad} $\mathbf{C}$---captures
the complete statistical learning pipeline.  The unit maps a single
observation to a count-1 entry.  The multiplication merges count tables
(accumulates evidence).  The forward pass is a Kleisli morphism from
input supports to output supports.  The $E \to \mathbb{N} \to \mathbb{Q}$
chain of CMP is the factorization of the monad through the free commutative
monoid and the field of fractions.  We prove that the counting monad is
idempotent on sufficient statistics and that the Kleisli category of
$\mathbf{C}$ is equivalent to the category of stochastic matrices over
event spaces.
\end{abstract}


\section{Introduction}

The Universal Model (UM) performs three operations: counting (the learning
function $\omega_0$), pattern formation (the pattern function $p$), and
prediction (the forward pass $f_p$).  These compose into a pipeline:
\[
\text{data} \xrightarrow{\omega_0} \text{counts}
\xrightarrow{p} \text{patterns}
\xrightarrow{f_p} \text{predictions}.
\]
We show this pipeline has the structure of a monad on the category of
event spaces.  The categorical perspective reveals:
\begin{enumerate}
\item Why counting is the ``right'' learning function (it is the free
  commutative monoid construction).
\item Why the forward pass composes correctly (Kleisli composition).
\item Why the $E \to \mathbb{N} \to \mathbb{Q}$ chain factors through
  algebraic embeddings (ring completion of the monoid).
\item Why different event spaces can be compared (natural transformations
  between monads).
\end{enumerate}


\section{The Category of Event Spaces}

\begin{definition}[Event space]
An \emph{event space} is a finite set $E$ together with an
equivalence relation $\sim$ partitioning $E$ into \emph{ES-mate} pairs
(or singletons for unpaired events).
\end{definition}

In the Hutter compressor, the base event space is $E_0 = \{0, \ldots, 255\}$
(bytes), with ES-mates determined by the domain (e.g., upper/lowercase
pairs in text).

\begin{definition}[Category $\mathbf{ES}$]
The category $\mathbf{ES}$ has:
\begin{itemize}
\item \textbf{Objects:} Event spaces $(E, \sim)$.
\item \textbf{Morphisms:} Functions $\phi: E \to E'$ that preserve
  ES-mate structure: $e_1 \sim e_2 \implies \phi(e_1) \sim' \phi(e_2)$.
\end{itemize}
Composition is function composition.  Identity is the identity function.
\end{definition}

\begin{remark}
Morphisms in $\mathbf{ES}$ are the ``coarsening'' maps of the tock step:
merging events into equivalence classes.  The factorization tower
$E_0 \to E_1 \to \cdots \to E_n$ is a sequence of morphisms in
$\mathbf{ES}$.
\end{remark}


\section{The Free Commutative Monoid Functor}

The counting function $\omega_0$ maps data (sequences of events) to count
tables (functions $E \to \mathbb{N}$).  This is the free commutative
monoid construction.

\begin{definition}[Free commutative monoid functor]
Define $\mathbf{F}: \mathbf{ES} \to \mathbf{CMon}$ by:
\begin{itemize}
\item On objects: $\mathbf{F}(E) = \mathbb{N}^E$ (the free commutative
  monoid on $E$, i.e., functions $c: E \to \mathbb{N}$, with pointwise
  addition).
\item On morphisms: $\mathbf{F}(\phi)(c)(e') = \sum_{\phi(e) = e'} c(e)$
  (push-forward of counts along $\phi$).
\end{itemize}
\end{definition}

\begin{proposition}
$\mathbf{F}$ is left adjoint to the forgetful functor
$U: \mathbf{CMon} \to \mathbf{ES}$ that sends a commutative monoid to
its underlying set (with trivial ES-mate structure).
\[
\mathbf{F} \dashv U: \mathbf{CMon} \to \mathbf{ES}
\]
\end{proposition}

\begin{proof}
The adjunction is the standard free-forgetful adjunction.  For any
function $f: E \to U(M)$ (where $M$ is a commutative monoid), there
is a unique monoid homomorphism $\hat{f}: \mathbb{N}^E \to M$ extending
$f$ by linearity: $\hat{f}(c) = \sum_{e \in E} c(e) \cdot f(e)$.
The bijection $\text{Hom}_{\mathbf{CMon}}(\mathbb{N}^E, M) \cong
\text{Hom}_{\mathbf{ES}}(E, U(M))$ is natural in both $E$ and $M$.
\end{proof}


\section{The Counting Monad}

The adjunction $\mathbf{F} \dashv U$ generates a monad
$\mathbf{C} = U \circ \mathbf{F}$ on $\mathbf{ES}$.

\begin{definition}[Counting monad]
The \emph{counting monad} $\mathbf{C} = (C, \eta, \mu)$ on
$\mathbf{ES}$ is:
\begin{itemize}
\item \textbf{Endofunctor:} $C(E) = \mathbb{N}^E$ (the set of count
  tables over $E$, viewed as an event space with pointwise ES-mate
  structure).
\item \textbf{Unit:} $\eta_E: E \to \mathbb{N}^E$, defined by
  $\eta_E(e) = \delta_e$ (the Kronecker delta: the count table with
  a single count of 1 at event $e$).
\item \textbf{Multiplication:} $\mu_E: \mathbb{N}^{\mathbb{N}^E} \to
  \mathbb{N}^E$, defined by $\mu_E(\mathcal{C})(e) =
  \sum_{c \in \mathbb{N}^E} \mathcal{C}(c) \cdot c(e)$ (the weighted
  sum of count tables, where $\mathcal{C}$ assigns a count to each
  count table).
\end{itemize}
\end{definition}

\begin{proposition}[Monad laws]
$\mathbf{C}$ satisfies the monad laws:
\begin{enumerate}
\item $\mu \circ \eta_C = \mathrm{id}_C$ (left unit).
\item $\mu \circ C\eta = \mathrm{id}_C$ (right unit).
\item $\mu \circ \mu_C = \mu \circ C\mu$ (associativity).
\end{enumerate}
\end{proposition}

\begin{proof}
These follow from the adjunction.  Concretely:
\begin{enumerate}
\item $\mu(\eta_C(c))(e) = \mu(\delta_c)(e) = 1 \cdot c(e) = c(e)$.
\item $\mu(C\eta(c))(e) = \sum_{c'} c(c') \cdot \delta_{c'}(e)$,
  but $C\eta$ maps each $c$ to the count table that assigns count
  $c(e')$ to the delta at $e'$, so the sum collapses to $c(e)$.
\item Associativity: both sides compute the ``total weighted sum.''
\end{enumerate}
\end{proof}


\subsection{Interpretation}

The monad structure has direct UM interpretations:
\begin{itemize}
\item \textbf{Unit $\eta$:} A single observation $e \in E$ becomes
  a count table with one entry.  This is the atomic act of learning:
  one event, one count.
\item \textbf{Multiplication $\mu$:} Multiple count tables (from
  different data segments, or from different event spaces) are combined
  by weighted summation.  This is evidence accumulation.
\item \textbf{Functoriality of $C$:} Coarsening the event space
  ($\phi: E \to E'$) induces a push-forward of counts.  This is
  exactly what the tock step does when it merges events.
\end{itemize}


\section{The Kleisli Category}

\begin{definition}[Kleisli category of $\mathbf{C}$]
The Kleisli category $\mathbf{ES}_\mathbf{C}$ has:
\begin{itemize}
\item \textbf{Objects:} Event spaces (same as $\mathbf{ES}$).
\item \textbf{Morphisms $E \to E'$:} Functions $f: E \to \mathbb{N}^{E'}$
  (a Kleisli arrow assigns to each event $e \in E$ a count table over $E'$).
\item \textbf{Composition:} $(g \circ_K f)(e) = \mu(C(g)(f(e)))$,
  i.e., apply $f$ to get a count table, then apply $g$ to each entry
  and combine.
\item \textbf{Identity:} $\eta_E: E \to \mathbb{N}^E$.
\end{itemize}
\end{definition}

\begin{theorem}[Forward pass as Kleisli morphism]
\label{thm:kleisli}
The UM forward pass $f_p: T \to T'$ (from input total thoughts to
output total thoughts) is a Kleisli morphism in $\mathbf{ES}_\mathbf{C}$
when we extend the monad to the tropical semiring.

More precisely: define the \emph{tropical counting monad} $\mathbf{C}^{\mathrm{trop}}$
by replacing $\mathbb{N}$ with $(\mathbb{N} \cup \{0\}, \max, \min)$.
Then the pattern matrix $p_{ij}$ defines a Kleisli morphism
$p: I \to \mathbb{N}^O$ by $p(i) = (p_{i1}, \ldots, p_{i|O|})$,
and the forward pass is the Kleisli composition of the input support
vector $t$ with this morphism:
\[
(f_p(t))_j = \max_i \min(t_i, p_{ij}) = (\mu^{\mathrm{trop}} \circ C^{\mathrm{trop}}(p))(t)_j.
\]
\end{theorem}

\begin{proof}
The tropical multiplication $\mu^{\mathrm{trop}}$ replaces addition with
$\max$ and ordinary multiplication with $\min$.  Then:
\[
\mu^{\mathrm{trop}}(C^{\mathrm{trop}}(p)(t))_j
= \max_{i \in I} \min(t_i, p(i)_j)
= \max_i \min(t_i, p_{ij})
= (f_p(t))_j.
\]
This is exactly the forward pass.
\end{proof}

\begin{corollary}[Multi-step inference]
Chaining forward passes (pattern chains) corresponds to Kleisli
composition:
\[
f_{p_2} \circ f_{p_1} = f_{p_2 \circ_K p_1}.
\]
Multi-step inference is associative by the monad laws.
\end{corollary}


\section{The $E \to \mathbb{N} \to \mathbb{Q}$ Chain}

CMP defines the chain $E \to \mathbb{N} \to \mathbb{Q}$: events embed into
natural numbers (via counting), which embed into rationals (via probability).
This chain factors through algebraic completions.

\begin{proposition}[$E \to \mathbb{N}$ as monad unit]
The embedding $E \hookrightarrow \mathbb{N}$ (assigning each event its
count) is the monad unit $\eta$ followed by the norm
$\|\cdot\|_1: \mathbb{N}^E \to \mathbb{N}$:
\[
e \xrightarrow{\eta} \delta_e \xrightarrow{\omega_0} c_e \xrightarrow{\|\cdot\|} c(e).
\]
But more precisely, the entire count table $c \in \mathbb{N}^E$ is the
image of the data under the monad:
\[
D = (d_1, \ldots, d_N) \xrightarrow{\sum \eta} c = \sum_{t=1}^N \delta_{d_t}.
\]
\end{proposition}

\begin{proposition}[$\mathbb{N} \to \mathbb{Q}$ as ring completion]
The embedding $\mathbb{N} \hookrightarrow \mathbb{Q}$ factors as:
\[
\mathbb{N} \hookrightarrow \mathbb{Z} \hookrightarrow \mathbb{Q}
\]
where $\mathbb{Z}$ is the Grothendieck group of $(\mathbb{N}, +)$
and $\mathbb{Q}$ is the field of fractions of $\mathbb{Z}$.

In UM terms:
\begin{itemize}
\item $\mathbb{N}$: raw counts (evidence).
\item $\mathbb{Z}$: signed counts (evidence minus expected, i.e.,
  residuals, log-ratios).
\item $\mathbb{Q}$: ratios of counts (conditional probabilities,
  luck ratios).
\end{itemize}
\end{proposition}

\begin{remark}
The chain $E \to \mathbb{N} \to \mathbb{Q}$ is the factorization:
\[
E \xrightarrow{\text{monad unit}} \mathbb{N}^E
\xrightarrow{\text{Grothendieck}} \mathbb{Z}^E
\xrightarrow{\text{field of fractions}} \mathbb{Q}^E.
\]
Each step adds algebraic structure:
\begin{itemize}
\item $\mathbb{N}^E$: can add evidence (monoidal).
\item $\mathbb{Z}^E$: can subtract evidence (group).
\item $\mathbb{Q}^E$: can divide evidence (field).
\end{itemize}
The forward pass lives in $\mathbb{N}^E$ (tropical).  Bayes' rule
lives in $\mathbb{Q}^E$ (rational).  The GCD decomposition lives
in $\mathbb{N}^E$ (arithmetic).
\end{remark}


\section{Sufficient Statistics and Idempotence}

\begin{definition}[Sufficient statistic]
A count table $c \in \mathbb{N}^{I \times O}$ is a \emph{sufficient
statistic} for the data $D$ with respect to event space $E = I \times O$
if the forward pass output depends on $D$ only through $c$.
\end{definition}

\begin{theorem}[Idempotence on sufficient statistics]
The counting monad is idempotent on sufficient statistics: if $c$ is
sufficient, then $C(c) = c$ (counting the count table gives back the
count table, up to natural isomorphism).

More precisely: the Eilenberg-Moore algebra of $\mathbf{C}$ on
sufficient statistics is isomorphic to $\mathbb{N}^E$ itself.
\end{theorem}

\begin{proof}
A sufficient statistic $c$ determines the prediction completely.
Applying $\omega_0$ again (counting the counts) produces a
``meta-count table'' $\mathcal{C} \in \mathbb{N}^{\mathbb{N}^E}$,
but the prediction depends only on the original $c$ (by sufficiency).
The multiplication $\mu(\mathcal{C}) = c$ recovers the original.
Hence $\mu \circ \eta_C = \mathrm{id}$, which is the idempotence
condition for the monad.
\end{proof}

\begin{remark}
This theorem says: counting is the right amount of compression.
Counting once extracts all the information the forward pass needs.
Counting twice adds nothing.  The count table is a \emph{fixed point}
of the learning process.
\end{remark}


\section{Comparison with Other Monads}

\subsection{The distribution monad}
The standard probability monad $\mathbf{D}$ maps a set $X$ to the
set of finitely-supported probability distributions on $X$.  The
counting monad $\mathbf{C}$ maps to \emph{unnormalized} counts.
There is a natural transformation $\mathbf{C} \to \mathbf{D}$
(normalization: $c \mapsto c/\|c\|_1$) that is NOT a monad morphism
(normalization doesn't preserve the monoid structure of count
accumulation).

This is significant: the UM works with unnormalized counts, not
probabilities.  Normalization happens only at prediction time (in the
output distribution).  The counting monad captures this: evidence
accumulates additively (monoidally), but prediction requires
normalization (leaving the monad).

\subsection{The Giry monad}
The Giry monad on measurable spaces maps $X$ to the space of
probability measures on $X$.  The counting monad is the discrete,
finitary version.  The Giry monad requires measure theory; the
counting monad requires only finite sets and natural numbers.

\subsection{The writer monad}
The writer monad $W \times (-)$ appends to a log.  The counting monad
is similar: each observation ``writes'' a count.  But the counting monad
is commutative (order doesn't matter for counts), while the writer monad
is not (order matters for logs).  The UM's position-independence
(counting doesn't depend on order) is the commutativity of the monad.


\section{The Monad and the Five-Tuple}

CMP defines the UM as $u = (e, t, p, f, \omega)$.  The monad structures
this as:

\begin{center}
\begin{tabular}{lll}
\toprule
CMP & Symbol & Monad structure \\
\midrule
Event space & $e$ & Object of $\mathbf{ES}$ \\
Total thought & $t$ & Element of $C(E)$ (count table as support vector) \\
Pattern & $p$ & Kleisli morphism $I \to C(O)$ \\
Forward pass & $f$ & Kleisli composition $\mu \circ Cp$ \\
Learning function & $\omega$ & Monad unit $\eta$ (single obs.) or $\sum \eta$ (data) \\
\bottomrule
\end{tabular}
\end{center}

The five-tuple IS the monad, unpacked into its components.

\begin{theorem}[Monad = UM]
The data of a counting monad $\mathbf{C}$ on $\mathbf{ES}$, together with
a Kleisli morphism $p: I \to C(O)$, is equivalent to the data of a
Universal Model $u = (e, t, p, f, \omega)$.
\end{theorem}

\begin{proof}
Given $\mathbf{C}$ and $p$:
\begin{itemize}
\item $e = (I, O)$: the domain and codomain event spaces.
\item $t = \omega_0(D) \in C(I)$: the input support vector (count table
  restricted to $I$).
\item $p$: the Kleisli morphism (pattern matrix).
\item $f = \mu^{\mathrm{trop}} \circ C^{\mathrm{trop}}(p)$: the forward pass.
\item $\omega = \eta$: the learning function (unit of the monad).
\end{itemize}
Conversely, given $u$, define $\mathbf{C}$ as the counting monad on the
event spaces of $u$, and take the Kleisli morphism from the pattern matrix.
The correspondence is functorial.
\end{proof}


\section{Natural Transformations and Model Comparison}

\begin{definition}[UM morphism]
A morphism between Universal Models $u = (e, t, p, f, \omega)$ and
$u' = (e', t', p', f', \omega')$ is a pair of ES morphisms
$(\phi_I: I \to I', \phi_O: O \to O')$ such that the following
diagram commutes:
\[
\begin{array}{ccc}
I & \xrightarrow{p} & C(O) \\
\downarrow \phi_I & & \downarrow C(\phi_O) \\
I' & \xrightarrow{p'} & C(O')
\end{array}
\]
\end{definition}

\begin{proposition}
A UM morphism $(\phi_I, \phi_O)$ is a natural transformation
between the Kleisli morphisms $p$ and $p'$.  It commutes with the
forward pass:
\[
\phi_O \circ f_p = f_{p'} \circ \phi_I.
\]
\end{proposition}

\begin{remark}
This means: if two models are related by event space coarsening
($\phi_I$ merges input events, $\phi_O$ merges output events),
then their predictions are consistent.  The coarser model's prediction
is the push-forward of the finer model's prediction.  This is the
categorical version of ``coarser models are projections of finer ones.''
\end{remark}


\section{The Tropical Variant}

The standard counting monad uses the semiring $(\mathbb{N}, +, \times)$.
The UM's forward pass uses the tropical semiring
$(\mathbb{N} \cup \{-\infty\}, \max, \min)$.

\begin{definition}[Tropical counting monad]
The \emph{tropical counting monad} $\mathbf{C}^{\mathrm{trop}}$ replaces
$\mathbb{N}^E$ with the tropical semiring:
\begin{itemize}
\item $C^{\mathrm{trop}}(E) = \{0, 1, \ldots, 255\}^E$ (SN support vectors).
\item Unit: $\eta(e) = \delta_e \cdot 255$ (full support for a single event).
\item Multiplication: $\mu(\mathcal{S})(e) = \max_{s} \min(\mathcal{S}(s), s(e))$
  (tropical matrix multiplication).
\end{itemize}
\end{definition}

\begin{proposition}
The tropical counting monad satisfies the monad laws under the tropical
semiring axioms.
\end{proposition}

\begin{remark}
The standard monad captures \emph{counting} (evidence accumulation).
The tropical monad captures \emph{inference} (the forward pass).
The UM uses both: $\mathbf{C}$ for learning, $\mathbf{C}^{\mathrm{trop}}$
for prediction.  The pattern function $p$ is the bridge: it converts
counts ($\mathbf{C}$) to supports ($\mathbf{C}^{\mathrm{trop}}$) via
the SN map $n \mapsto \lfloor 255 \cdot n / n_{\max} \rfloor$.
\end{remark}


\section{Discussion}

\subsection{Why counting is canonical}

The free commutative monoid $\mathbb{N}^E$ is the \emph{simplest}
structure that tracks evidence for events.  Any richer structure
(e.g., recording order, or weighting by recency) adds assumptions
beyond the data.  The counting monad is the unique monad that:
\begin{enumerate}
\item Treats each observation identically (commutativity).
\item Treats each event independently (pointwise structure).
\item Adds evidence monotonically (counts only increase).
\end{enumerate}
Any learning function satisfying these three properties is equivalent to
$\omega_0$ (counting).  This is why $\omega_0$ is the ``right'' learning
function: it is the free construction satisfying the minimal assumptions.

\subsection{The tock step as a change of base}

The tock step discovers a new event space $E'$ and a morphism $\phi: E \to E'$.
In monadic terms, this is a \emph{change of base}: the counting monad
$\mathbf{C}_E$ on $E$ is replaced by $\mathbf{C}_{E'}$ on $E'$, with
the push-forward $\mathbf{F}(\phi)$ transferring counts.  The tock step
is the functor $\mathbf{F}(\phi): \mathbf{C}_E \to \mathbf{C}_{E'}$.

\subsection{Open questions}

\begin{enumerate}
\item \textbf{Higher monads:} The factorization tower
  $E_0 \to E_1 \to \cdots \to E_n$ defines a sequence of monads.
  Is there a ``2-monad'' or ``monad transformer'' that captures the
  entire tower?
\item \textbf{Bayesian monad:} The Bayesian update rule (dividing by
  marginals) is not a monad operation.  Can Bayes be captured as a
  \emph{comonad} on the category of count tables?
\item \textbf{Tropical vs.\ standard:} The relationship between
  $\mathbf{C}$ and $\mathbf{C}^{\mathrm{trop}}$ deserves further study.
  Is there a ``tropicalization'' functor between the monad categories?
\end{enumerate}


\begin{thebibliography}{99}
\bibitem{cmp}
Michaeljohn Clement.
\newblock \emph{CMP}.
\newblock \url{https://cmpr.ai/cmp.pdf}, 2026.

\bibitem{bayes-counting}
Claude and MJC.
\newblock \emph{Bayes from Counting: Partial Quotients, GCD, and the
Symmetric Learning Function on $E = I \times O$}.
\newblock Hutter archive, 12 Feb 2026.

\bibitem{logic-counting}
Claude and MJC.
\newblock \emph{Logic from Counting}.
\newblock Hutter archive, 12 Feb 2026.

\bibitem{tropical-gcd}
Claude and MJC.
\newblock \emph{The Tropical--Integer GCD Bridge}.
\newblock Hutter archive, 12 Feb 2026.

\bibitem{tock-step}
Claude and MJC.
\newblock \emph{The Tock Step: Domain-Native Architecture from Evidence}.
\newblock Hutter archive, 12 Feb 2026.

\bibitem{mac-lane}
Saunders Mac Lane.
\newblock \emph{Categories for the Working Mathematician}.
\newblock Springer, 2nd ed., 1998.
\end{thebibliography}

\end{document}
