\documentclass[a4paper]{amsart}
\newcommand{\numberset}[1]{\mathbf{#1}}
\newcommand{\Z}{\numberset{Z}}
\begin{document}
\title{A mathematical description of \TeX{} glue}
\author{egreg@\TeX.SX}
\address{\texttt{tex.stackexchange.com}}
\maketitle
Let $\Z$ be the additive group of integers. We shall disregard the fact that
\TeX{} can only deal with a finite subset of $\Z$, because this is just
incidental.
Consider the set $\Z^{7}$ and denote the projections on the components
by $n$, $S_1$, $S_2$, $S_3$, $s_1$, $s_2$ and $s_3$ respectively.
The set $G$ of \emph{glues} is defined as the subset of $\Z^{7}$ consisting
of the tuples $x\in\Z^{7}$ such that at most one among $S_1(x)$, $S_2(x)$,
$S_3(x)$ is nonzero and at most one among $s_1(x)$, $s_2(x)$, $s_3(x)$ is
nonzero. An element of $\Z^{7}$ is called a \emph{preglue}.
For $x\in\Z^{7}$, we set
\[
O(x)=
\begin{cases}
0 & \text{if $S_1(x)=S_2(x)=S_3(x)=0$}\\
1 & \text{if $S_1(x)\ne0$, $S_2(x)=S_3(x)=0$}\\
2 & \text{if $S_2(x)\ne0$, $S_3(x)=0$}\\
3 & \text{if $S_3(x)\ne0$}
\end{cases}
\]
and call $O(x)$ the \emph{stretching order} of the glue~$x$. Similarly
\[
o(x)=
\begin{cases}
0 & \text{if $s_1(x)=s_2(x)=s_3(x)=0$}\\
1 & \text{if $s_1(x)\ne0$, $s_2(x)=s_3(x)=0$}\\
2 & \text{if $s_2(x)\ne0$, $s_3(x)=0$}\\
3 & \text{if $s_3(x)\ne0$}
\end{cases}
\]
is the \emph{shrinking order} of the preglue~$x$. We finally define
the function $\gamma\colon\Z^{7}\to G$ by
\begin{enumerate}
\item $n(\gamma(x))=n(x)$;
\item $S_k(\gamma(x))=0$ if $k<O(x)$ or $k>O(x)$ ($k=1,2,3$);
\item $S_k(\gamma(x))=s_k(x)$ if $k=O(x)$ ($k=1,2,3$);
\item $s_k(\gamma(x))=0$ if $k<o(x)$ or $k>o(x)$ ($k=1,2,3$);
\item $s_k(\gamma(x))=s_k(x)$ if $k=o(x)$ ($k=1,2,3$).
\end{enumerate}
We define an operation $\oplus$ on $G$ by defining, for $x,y\in G$,
\[
x\oplus y=\gamma(x+y)
\]
where $+$ denotes the componentwise addition in $\Z^{7}$.
The reader should work out the following exercises:
\begin{enumerate}
\item $G$ has a neutral element $0$;
\item for every $x\in G$ there is $y\in G$ such that $x\oplus y=0$;
\item the operation $\oplus$ is commutative;
\item the operation $\oplus$ is not associative.
\end{enumerate}
In spite of the fact that $\oplus$ is not associative, we can define
an action of the integers on $G$; if $a$ is an integer and $x\in G$,
we simply consider $ax$ in the usual sense for the abelian group
$\Z^{7}$, as $\gamma(ax)=ax$.
Final exercise: the set of glues with a fixed stretch order and fixed
shrink order is a group under $\oplus$, isomorphic to $\Z$,
$\Z^{2}$ or $\Z^{3}$.
\end{document}

Important note
This describes the \advance operation on \skip registers, not what TeX does when it builds a box. For that case the work is different:
When \TeX{} is building a box (vertical or horizontal), it computes
the available natural width, stretching component and shrinking component
by doing
\[
\gamma(g_1 + g_2 + \dots + g_n)
\]
not by using the $\oplus$ operation, where $g_1,g_2,\dots,g_n$ are the
available glues in the box.
