Honeycombs in the Pascal triangle and beyond

Abstract: In this paper we present a geometric approach to discovering some known and some new identities using triangular arrays. Our main aim is to demonstrate how to use the geometric patterns (by Carlitz), in the Pascal and Hosoya triangles to rediscover some classical identities and integer sequences. Therefore, we use new techniques in classical settings which then provide a new perspective in undergraduate research.

\section{Carlitz's patterns in the Pascal triangle}\label{pascal:triangle}

In this section we use Carlitz's patterns seen in Figure \ref{carlitz_patterns:1}  to rediscover some well-known binomial identities. The patterns in Figure \ref{carlitz_patterns:1} (a)--(d) can also be seen as Patterns 1, 2, in Figure \ref{hex_patterns:1}(a) and Patterns 3, 4 in Figure \ref{hex_patterns:1}(b).
Note that the identity present in Proposition \ref{propo:3} can only be found partially in literature while the identity in Theorem \ref{theorem:4} is new.

 Here we observe that the hexagonal tiles used in the Pascal triangle is constructed using the entires of the triangle in every other row as shown in Figure \ref{hex_patterns:1}. In this figure, each dot is an entry of the triangle.
This setup of the hexagonal tiles can also be seen in Figure \ref{alternatingpatterns:2}.
This construction of the honeycomb pattern can also be seen in Figure \ref{alternatingpatterns:2}.

Based on this honeycomb pattern we can construct another triangular array with entries of the from each hexagonal cells. These entries are of the form $\binom{2k}{i}$ where $k>0$ and $i$ is odd.
The entries of this triangular array are $ 2,  4, 4, 6, 20, 6, 8, 56, 8,\ldots$. We leave it to the readers to conduct
\begin{figure}[htbp]
\includegraphics[scale=0.30]{alternating_patterns.pdf}
\caption{Hexagonal cells in the triangles and patterns.}
\label{hex_patterns:1}
\end{figure}

The first identity in Proposition \ref{propo:1}\eqref{part:a} is obtained by using Pattern 1 in Figure \ref{hex_patterns:1}(a)  (or Carlitz's pattern, Figure \ref{carlitz_patterns:1}(b)), in Pascal's triangle.
For example, using Figure \ref{hex_patterns:1} (a), Pattern 1, we have that  $$\underbrace{\binom{6}{1}}_{\color{blue}{\textbf a}}+ \underbrace{\binom{6}{3}}_{\color{blue}{\textbf b}} +\underbrace{\binom{6}{5}}_{\color{blue}{\textbf c}}= 2^5.$$
This identity is also clear from Figure \ref{alternatingpatterns:2}.

The second identity in Proposition \ref{propo:1}\eqref{part:b} is obtained by using Pattern 2 in Figure \ref{hex_patterns:1}(a)  (or Carlitz's pattern, Figure \ref{carlitz_patterns:1}(b)), in Pascal's triangle.
For example, using in Pattern 2 we have that

 $$\underbrace{\binom{10}{1}}_{\color{blue}{\textbf d}}+\underbrace{\binom{8}{1}}_{\color{blue}{\textbf e}}+\underbrace{\binom{10}{3}}_{\color{blue}{\textbf f}}+\underbrace{\binom{8}{3}}_{\color{blue}{\textbf g}}+\underbrace{\binom{10}{5}}_{\color{blue}{\textbf h}}+\underbrace{\binom{8}{5}}_{\color{blue}{\textbf i}}+\underbrace{\binom{10}{7}}_{\color{blue}{\textbf j}}+\underbrace{\binom{8}{7}}_{\color{blue}{\textbf k}}+\underbrace{\binom{10}{9}}_{\color{blue}{\textbf l}}=5(2^7) .$$

 Therefore, in general, we obtain the following identities.
\begin{proposition}\label{propo:1}
If $n=2k$ for $k>0$, then the following hold
\begin{enumerate}[(a)]
\item \label{part:a} $\displaystyle \sum_{i=1}^{k}\binom{2k}{2i-1}=2^{2k-1}$,
\item \label{part:b} $\displaystyle \sum_{i=1}^{k}\binom{2k}{2i-1}+\sum_{i=1}^{k-1}\binom{2k-2}{2i-1}=5(2^{2k-3})$.
\end{enumerate}
\end{proposition}

\begin{figure}[htbp]
\begin{center}
\includegraphics[scale=0.3]{powerof2.pdf}
\caption{ Power of 2 in Pascal triangle.}
\end{center}
\label{alternatingpatterns:2}
\end{figure}

The identity in Proposition \ref{propo:3} is obtained by using Pattern 3 in Figure \ref{hex_patterns:1}(b). For example, using this pattern we have that
$$\underbrace{\binom{12}{1}}_{\color{blue}{\textbf s}}+\underbrace{\binom{12}{3}}_{\color{blue}{\textbf t}}+\underbrace{\binom{10}{3}}_{\color{blue}{\textbf u}}+\underbrace{\binom{12}{5}}_{\color{blue}{\textbf v}}+\underbrace{\binom{12}{7}}_{\color{blue}{\textbf w}}+\underbrace{\binom{10}{7}}_{\color{blue}{\textbf x}}+\underbrace{\binom{12}{9}}_{\color{blue}{\textbf y}}+\underbrace{\binom{12}{11}}_{\color{blue}{\textbf z}}=2^4[9(2^4)-1]=2288.$$

\begin{proposition}\label{propo:3}
If $n\ge 2$ is any positive integer, then
\[\sum_{i=1}^{2n}\binom{4n}{2i-1}+\sum_{i=1}^{n-1}\binom{4n-2}{4i-1}=
\begin{cases}
2^{2n-2}[9 (2^{2n-2})+1], & \text{ if  $n$  is even};\\
2^{2n-2}[9 (2^{2n-2})-1], & \text{ if  $n$  is odd}.
\end{cases}\]
\end{proposition}

The identity in Theorem \ref{theorem:4} below is not present in existing literature and is  obtained by using the pattern in Figure \ref{carlitz_patterns:1}(d)
(or Pattern 4 in Figure \ref{hex_patterns:1}(b)).  For example, uisng Figure \ref{hex_patterns:1}, Pattern 4 we have that

$$\underbrace{\binom{6}{1}}_{\color{blue}{ \textbf m}}+ \underbrace{\binom{4}{1}}_{\color{blue}{ \textbf n}} +\underbrace{\binom{4}{3}}_{\color{blue}{ \textbf o}}+\underbrace{\binom{6}{5}}_ {\color{blue}{ \textbf p}}= 20.$$
In general, this sum is congruent to $20\bmod 42$.

\begin{theorem}\label{theorem:4} If $n\ge 1$, then
$$\sum_{i=0}^{n-1}\left[\binom{6n}{6i+1}+\binom{6n}{6i+5}+\binom{6n-2}{6i+1}+\binom{6n-2}{6i+3}\right]\equiv \binom{6}{1}+ \binom{6}{5} +\binom{4}{1}+\binom{4}{3}= 20 \bmod\, 42.$$
\end{theorem}

Finally, the Carlitz's pattern see in Figure \ref{carlitz_patterns:1}(e) gives rise to the integer sequence
$$2, 4, 4, 6, 20, 6, 8, 56, 56, 8, 10, 120, 252, \ldots=\binom{2n}{2m+1}, \text{ for } n>1 \text{ and }0\le m\le n-1.$$
This sequence is related to the integer sequence  {A091044} in \cite{sloane} with terms $1, 2, 2, 3, 10, 3, 4, 28, 28, \ldots$.
In fact, the sequence  {A091044}, is one half of the odd-numbered entries of even-numbered rows of the Pascal triangle.

\section{Carlitz's patterns in the Hosoya triangle}\label{hosoya:triangle}

The Hosoya triangle \cite{hosoya}, is a triangular array where the entries are products of Fibonacci numbers.
The geometry of the Hosoya triangle presents an interesting method to explore properties of Fibonacci numbers.
Several properties and identities have already been discovered and published. Some articles on this topic are  by Hosoya,  Koshy, and Fl\'orez \emph{et al.} \cite{Blair,florezHiguitaJunesGCD, florezjunes, hosoya, koshy}.

\begin{table} [!h] \small
\begin{center} \addtolength{\tabcolsep}{-3pt} \scalebox{.9}{%
\begin{tabular}{cccccccccccccccccccccccccccc}
&&&&&&&&&&&&&                                                  1                                             		  &&&&&&&&&&&&&&\\
&&&&&&&&&&&&                                   	       1    &&    1                                        		    &&&&&&&&&&&&&\\
&&&&&&&&&&&                                		   2    &&     1    &&     2                                     	       &&&&&&&&&&&&\\
&&&&&&&&&&                                    3    &&     2    &&     2     &&   3                                 	            &&&&&&&&&&\\
&&&&&&&&&                         	    5    &&      3    &&    4     &&   3    && 5                                           &&&&&&&&&\\
&&&&&&&&                           8    &&      5    &&    6    && 6     &&   5     &&  8                                         &&&&&&&&\\
&&&&&&&               	    13   &&     8    && 10  &&   9     &&   10    &&   8       &&   13            		    &&&&&&&\\
&&&&&&                     21  &&  13  &&  16 && 15 && 15 && 16 && 13 && 21                                               &&&&&&\\
\end{tabular}

\begin{tabular}{cccccccccccccccccccccccccccc}
&&&&&&&&&&&& &                                                   1                                             		  &&&&&&&&&&&&&&\\
&&&&&&&&&&&&                                  	       1    &&   2                                        		   &&&&&&&&&&&&&\\
&&&&&&&&&&&                               		   1    &&     3    &&     2                                     	       &&&&&&&&&&&\\
&&&&&&&&& &                                   1   &&     4    &&     5     &&   2                                	           & &&&&&&&&&\\
&&&&&&&& &                        	    1    &&      5    &&    9    &&   7    && 2                                           &&&&&&&&&\\
&&&&&&&&                          1    &&     6    &&    14   && 16     &&  9     &&  2                                      & &&&&&&&\\
&&&&&&&               	    1   &&     7    && 20  &&   30    &&   25    &&  11       &&   2            		           & &&&&&&\\
&&&&&&                    1 && 8  &&  27 && 50 && 55 &&  36      &&      13          &&       2                                               &&&&&&\\
\end{tabular}}
\end{center}
\caption{(a) Hosoya triangle \hspace{4.0cm} (b) Lucas triangle.} \label{tabla1}
\end{table}

We represent each entry of the Hosoya triangle as $H_{r,k}= F_kF_{r-k+1}$ for positive integers $r$ and $k$ with $r \ge k\ge 1$,
(see \cite[p.~188]{koshy}).

Recently Blair  et al. \cite{Blair1}, published the article titled ``Honeycombs in the Hosoya Triangle". In this article the authors look into the patterns in Figure \ref{carlitz_patterns:1} (a) and (b) (or Patterns 1 and 2 in Figure \ref{hex_patterns:1}) inside the Hosoya triangle and this leads to discovery of some new and geometric interpretation of some known identities. In addition, we obtain a sequence of integers by
following the pattern seen in Figure \ref{carlitz_patterns:1}(e). The examples and propositions that follow appeared in \cite{Blair1}.

For example, using Pattern 1 of Figure  \ref{hex_patterns:1}(a) we have that

$$\underbrace{H_{7,2}}_ {\color{blue}{ \textbf a}}+ \underbrace{H_{7,4}}_{\color{blue}{ \textbf  b}}+\underbrace{H_{7,6}}_{\color{blue}{ \textbf  c}}=F_2F_6+F_4F_4+F_6F_4= 25.$$

In general this gives rise to the integer sequence  {A001871} in OEIS (\cite{sloane}). Each term of this sequence is described as the convolution of the even-indexed Fibonacci numbers with itself.
This generalizes to the following identity which is a special case of a result by Fl\'{o}rez et al. in \cite{Czabarka}.

\begin{proposition}[\cite{Blair1}] \label{prop:6}
If $n>0$ is odd, then
\[ \sum_{k=1}^{(n-1)/2}H_{(n,2k)}=\sum_{k=1}^{(n-1)/2}F_{2k}F_{n-2k+1}=\left[(2n-6)F_{n-3}+(3n-5)F_{n-2}+[(n-1)/2] \,F_{n-2}\right]/5.\]
\end{proposition}

Next, we consider Pattern 2 in Figure \ref{hex_patterns:1}(a) embedded in the Hosoya triangle.

As an example, using Pattern 2 we have that

        $$ \underbrace{H_{11,2}}_{\color{blue}{ \textbf d}}+\underbrace{H_{9,2}}_ {\color{blue}{ \textbf e}}+\underbrace{H_{11,4}}_{\color{blue}{ \textbf  f}}+\underbrace{H_{9,4}}_{\color{blue}{ \textbf g}}+\underbrace{H_{11,6}}_{\color{blue}{ \textbf h}}+\underbrace{H_{9,6}}_{\color{blue}{ \textbf i}}+\underbrace{H_{11,8}}_{\color{blue}{ \textbf j}}+\underbrace{H_{9,8}}_{\color{blue}{ \textbf k}}+\underbrace{H_{11,10}}_{\color{blue}{ \textbf l}}$$
equals the sum $2(F_2F_{10}+F_4F_8)+F_6F_6+2(F_2F_8+F_4F_6)=390$.

Here we observe that the sums of Hosoya triangle entries $H_{n,k}$ following this pattern  ---with $n$ an odd integer--- gives rise to the integer sequence  {A197649} in OEIS (\cite{sloane}). In general we have the following identity.

\begin{proposition}[\cite{Blair1}]\label{prop:7}
If $n>0$ is odd, then
\[\sum_{k=1}^{(n-1)/2}H_{n,2k}+\sum_{k=1}^{(n+1)/2}H_{n+2,2k}=\sum_{k=1}^{(n-1)/2}F_{2k}F_{n-2k+1}+\sum_{k=1}^{(n+1)/2}F_{2k}F_{n-2k+3}=[(n+1)F_{n+2}-2F_{n+1}]/2.\]
\end{proposition}

Finally, the pattern seen in Figure \ref{carlitz_patterns:1} (e) gives rise to the integer sequence $1, 3, 3, 8, 9, 8, 21, 24,\ldots$, which is identified as sequence  {A141678} in OEIS (\cite{sloane}).
This sequence is defined as symmetrical triangle of coefficients based on invert transform of the sequence  {A001906}. Note that the sequence  {A001906} is the bisection of the Fibonacci sequence
given by the recursive sequence $a(n)=3a_{n-1}-a_{n-2}$ for $n\ge 3$, with initial conditions $a_1=0$ and $a_1=1$.

\section{Carlitz's patterns in other triangles}\label{other:triangle}

Carlitz's patterns can be used in other triangles like the Lucas triangle, Josef's triangle, and Liebniz's harmonic triangle to explore and obtain identities related to several different integer sequences.
The Lucas triangle (Table \ref{tabla1}(b)) is constructed using coefficients of polynomials of the form $f_n(x,y)=(x+y)^{(n-1)(x+2y)}$. If this triangle is left-justified, then its rising diagonals give rise to Lucas numbers.
On the other hand, Josef's triangles can be constructed like the Hosoya triangle by starting with two outermost columns of Lucas and Fibonacci number sequences (Table \ref{tabla2}(a)). Leibniz's triangle (Table \ref{tabla2}(b)) can be obtained by using the following recursive relation

\[L(n, 1) = 1/n; L(n, k) = L(n-1, k-1)-L(n, k-1), \text{for } k>1.\]
All three of these triangles are described in detail in  \cite{koshy2}.

For all three triangles Patterns 1 and 2 in Figure \ref{hex_patterns:1} yields some known identities or integer sequences found in \cite{sloane}.

In particular for the Lucas triangle, Pattern 1 (sum of every other entry in the odd-numbered rows), yields the integer sequence $3,12,48,192, \dots$
which is given by the formula $a_n=3(4^{n-1})$ for $n\ge 1$. This is also present in  OEIS \cite{sloane} as sequence  {A002001}.
Pattern 2 in the Lucas triangle yields the sequence $3,15,60,240,\ldots$.

 Similarly, for Josef's triangle, Pattern 1, yields the integer sequence $2, 10, 40,1 42, 470\ldots$, which is related to the sequence of self-convoluted
 Fibonacci numbers (  {A001629} in OEIS). In fact, one-half of each term of this sequence, namely $1, 5, 20, 71, 235,\ldots$, is the sequence of
 even-indexed terms of  {A001629}. The sequence $1, 5, 20, 71, 235,\ldots$, is identified in OEIS (\cite{sloane} ) as  {A054444}.

 In Josef's triangle, using Pattern 2 we get the sequence $12,50,182,612,\ldots$ where each term is the sum of consecutive terms of the sequence
 $2, 10, 40, 142, 470, \ldots$, obtained from Pattern 1 above.

 Finally, for the Leibniz's harmonic denominator triangle, Pattern 1 gives us the following sequence $$6, 40, 224,1 152, 5632,\ldots.$$ This sequence can be given by the recursive relation $D_n=8D_{n-1}-16D_{n-2}$ for $n>3$ with initial conditions $D_1=1, D_2=6$, and $D_3=40$.
 This sequence is identified in  \cite{sloane} as sequence  {A229580}. In the same triangle, Pattern 2 gives us the sequence $46,264,1376,7784,\ldots$. Each term of this sequence is the sum of consecutive terms of the sequence  $6,40,224,1152, 5632,\ldots.$ obtained from Pattern 1.

\begin{table} [!h] \small
\begin{center} \addtolength{\tabcolsep}{-3pt} \scalebox{.8}{%
\begin{tabular}{cccccccccccccccccccccccccccc}
&&&&&&&&&&&&&                                                  2                                             		  &&&&&&&&&&&&&&\\
&&&&&&&&&&&&                                   	       1    &&    1                                        		    &&&&&&&&&&&&&\\
&&&&&&&&&&&                                		   3    &&     2    &&     3                                    	       &&&&&&&&&&&&\\
&&&&&&&&&&                                    4   &&     3    &&     3     &&   4                                 	            &&&&&&&&&&\\
&&&&&&&&&                         	    7    &&      5    &&    6     &&   5    && 7                                           &&&&&&&&&\\
&&&&&&&&                           11   &&      8   &&   9    && 9     &&   8     && 11                                       &&&&&&&&\\
&&&&&&&               	    18   &&     13    && 15  &&  14     &&   15    &&  13       &&  18            		    &&&&&&&\\
&&&&&&                     29  &&  21  &&  24 && 23 && 23 && 24 &&   21 &&      29                                              &&&&&&\\
\end{tabular}
\begin{tabular}{cccccccccccccccccccccccccccc}
&&&&&&&&&&&& &                                                   1                                             		  &&&&&&&&&&&&&&\\
&&&&&&&&&&&&                                  	      $\frac{1}{2}$   &&   $\frac{1}{2}$                                 &&&&&&&&&&&&&\\
&&&&&&&&&&&                               		  $\frac{1}{3}$    &&    $\frac{1}{6}$   &&    $\frac{1}{3}$             &&&&&&&&&&&\\
&&&&&&&&& &                 $\frac{1}{4}$   &&     $\frac{1}{12}$    &&     $\frac{1}{12}$ &&   $\frac{1}{4}$          & &&&&&&&&&\\
&&&&&&&& &  $\frac{1}{5}$   &&      $\frac{1}{20}$    &&    $\frac{1}{30}$    &&   $\frac{1}{20}$   && $\frac{1}{5}$                                          &&&&&&&&&\\
&&&&&&&&                        $\frac{1}{6}$     &&     $\frac{1}{30}$   &&   $\frac{1}{60}$   &&  $\frac{1}{60}$    &&  $\frac{1}{30}$    &&  $\frac{1}{6}$                                      & &&&&&&&\\
&&&&&&&               	    $\frac{1}{7}$   &&     $\frac{1}{42}$    && $\frac{1}{105}$  &&  $\frac{1}{140}$    &&   $\frac{1}{105}$    &&  $\frac{1}{42}$      &&  $\frac{1}{7}$        		           & &&&&&&\\
&&&&&&                    $\frac{1}{8}$ && $\frac{1}{56}$  &&  $\frac{1}{168}$ && $\frac{1}{280}$ && $\frac{1}{280}$ &&  $\frac{1}{168}$     &&      $\frac{1}{56}$          &&       $\frac{1}{8}$                                               &&&&&&\\
\end{tabular}}
\end{center}
\caption{ (a) Josef's triangle \hspace{3.5cm} (b) Leibniz harmonic triangle} \label{tabla2}
\end{table}

\section{Appendix --Proofs}
In this section we present some proofs of the identities in case the reader is interested in the formal proofs of the results presented earlier.

 First we present some well-known identities involving binomial coefficients which will be used to prove the propositions in this section.
 Lemma \ref{lemma:1} Parts \eqref{lemma:parta} and \eqref{lemma:partb} and their proofs can be found in any combinatorics book, for example
 \cite{gould1, spivey}.   Lemma \ref{lemma:1}, part \eqref{lemma:partc} is a special case of an identity
 found in \cite{gould1}.  The proofs of Proposition \ref{propo:1} Parts \eqref{part:a} and \eqref{part:b} follow directly from Lemma \ref{lemma:1}
 Parts \eqref{lemma:parta} and \eqref{lemma:partb}.

\begin{lemma}\label{lemma:1}
If $n,k$ are positive integers, then the following hold
\begin{enumerate}[(a)]
\item $\displaystyle\binom{n+1}{k}=\binom{n}{k}+\binom{n}{k-1}$.\label{lemma:parta}
\item $\displaystyle\sum_{k=0}^n \binom{n}{k} =2^n$.\label{lemma:partb}
\item $\displaystyle\sum_{k=0}^{2n}(-1)^k\binom{4n}{2k}=(-1)^n2^{2n}$. \label{lemma:partc}
\end{enumerate}
\end{lemma}

\begin{proof}[Proof of Proposition \ref{propo:3}]
We first note that the proof of $\displaystyle\sum_{i=1}^{2n}\binom{4n}{2i-1}=2^{4n-1}$ is well-known (see \cite{gould1, spivey}).
Therefore, we only  prove that
\begin{equation}\label{eqn:1}
\sum_{i=1}^{n-1}\binom{4n-2}{4i-1}=
\begin{cases}
2^{2n-2}[2^{2n-2}+1] & \text{ if } n \text{ is even};\\
2^{2n-2}[2^{2n-2}-1] & \text{ if } n \text{ is odd}.
\end{cases}
\end{equation}

Observe that Lemma  \ref{lemma:1} part (a) implies that \[\displaystyle\sum_{i=1}^{n-1}\binom{4n-2}{4i-1}=\sum_{i=1}^{n-1}\binom{4n-3}{4i-1}+\binom{4n-3}{4i-2}.\]

Using the same identity again and regrouping the terms in the sum, we have that

\[\sum_{i=1}^{n-1}\binom{4n-2}{4i-1}=\sum_{i=0}^{4n-4}\binom{4n-4}{i}-\sum_{i=0}^{n-1}\binom{4n-4}{4i}+\sum_{i=1}^{n-1}\binom{4n-4}{4i-2}.\]

Further simplification gives us, $\displaystyle\sum_{i=1}^{n-1}\binom{4n-2}{4i-1}=\sum_{i=0}^{4n-4}\binom{4n-4}{i}-\sum_{i=0}^{2n-2}(-1)^i\binom{4n-4}{2i}$. Finally, applying Lemma \ref{lemma:1} Parts \eqref{lemma:parta} and \eqref{lemma:partb}, we obtain

\[\sum_{i=1}^{n-1}\binom{4n-2}{4i-1}=
\begin{cases}
2^{2n-2}[2^{2n-2}+1] & \text{ if } n-1 \text{ is odd};\\
2^{2n-2}[2^{2n-2}-1] & \text{ if } n-1\text{ is even,}
\end{cases}
\]
which is equivalent to \eqref{eqn:1}.
This completes the proof.
\end{proof}

\begin{proof}[Proof of Theorem \ref{theorem:4}]
Note that,
$$\sum_{i=0}^{n-1}\left[\binom{6n}{6i+1}+\binom{6n}{6i+5}+\binom{6n-2}{6i+1}+\binom{6n-2}{6i+3}\right]$$
is congruent with  $0 \bmod 2$, is congruent with  $2 \bmod 3$, \text{ and }  is congruent with  $6 \bmod 7.$
By the Chinese Remainder Theorem, we can easily solve this system of equation. This proves the theorem.
\end{proof}

\section{Acknowledgement}

The second and third authors were partially supported by The Citadel Foundation.


Making Markov chains less lazy

Abstract: The mixing time of an ergodic, reversible Markov chain can be bounded in terms of the eigenvalues of the chain: specifically, the second-largest eigenvalue and the smallest eigenvalue. It has become standard to focus only on the second-largest eigenvalue, by making the Markov chain "lazy". (A lazy chain does nothing at each step with probability at least 1/2, and has only nonnegative eigenvalues.)An alternative approach to bounding the smallest eigenvalue was given by Diaconis and Stroock and Diaconis and Saloff-Coste. We give examples to show that using this approach it can be quite easy to obtain a bound on the smallest eigenvalue of a combinatorial Markov chain which is several orders of magnitude below the best-known bound on the second-largest eigenvalue.

\section{Introduction}\label{s:intro}

Let $\mathcal{M}$ be an ergodic, reversible Markov chain with
finite state space $\Omega$ and transition matrix $P$.
It is well known that the eigenvalues of $\mathcal{M}$ satisfy
\[ 1 = \lambda_0 > \lambda_1 \geq \lambda_2 \geq \cdots
           \geq \lambda_{N-1} > -1,\]
where $N=|\Omega|$.
We refer to $\lambda_{N-1}$ as the \emph{smallest eigenvalue}
of $\mathcal{M}$.

The connection between the mixing time of a Markov chain and its
eigenvalues is well-known (see~\cite[Proposition 1]{sinclair}):
\begin{equation}
\label{mix-time} \tau(\varepsilon) \leq (1-\lambda_\ast)^{-1}\,
   \ln \frac{1}{\epsilon\, \pi_{\min}}
\end{equation}
where $\tau(\varepsilon)$ denotes the mixing time of the Markov chain,
$\pi_{\min} = \min_{x\in \Omega} \pi(x)$ and
\[ \lambda_\ast = \max\{ \lambda_1, \, |\lambda_{N-1}|\}.\]
When studying the mixing time of a Markov chain $\mathcal{M}$
using (\ref{mix-time}),
the approach which has become standard is to make the chain $\mathcal{M}$
\emph{lazy} by replacing $P$ by $(I+P)/2$,
where $I$ denotes the identity matrix.  Then all eigenvalues of the lazy
chain are nonnegative, and only the second-largest eigenvalue must
be investigated.

A lazy chain can be implemented so that its expected
running time is the same as the mixing time of the original chain.
So the problem with lazy chains is not their efficiency.
In our opinion, the main problem with lazy Markov chains is conceptual:
in order to prove that a Markov chain is fast, we first slow it down.
The device of using lazy Markov chains has been called
``crude''~\cite[p.~110]{SJ89} and
``unnatural''~\cite[Chapter 5]{jerrum-book}.

In this note, we aim to advertise an approach for bounding the
smallest eigenvalue of a Markov chain.
This approach was first proposed by Diaconis and Stroock
in 1991~\cite[Proposition 2]{DS91}, and a modified version was
presented by
Diaconis and Saloff-Coste two years later~\cite[p.702]{DSC}
(restated as Lemma~\ref{lazy} below).
The method of~\cite{DSC} has been applied in~\cite{DSC,DS91,goel},
but in the theoretical computer science community it has become common to
work with lazy chains.  We urge researchers to first try
the approach of~\cite{DSC,DS91}
before choosing to work with a lazy version of their chain.

Finally we remark that in~\cite{directed} the author wrongly claimed
that their~\cite[Lemma 1.3]{directed} was new, when in fact it is precisely
the result of~\cite[p.702]{DSC}. We sincerely apologise for this error.

\subsection{The method}

See~\cite{jerrum-book} for Markov chain definitions not given here.
Write $\mathcal{G}$ for the underlying directed graph of the Markov chain
$\mathcal{M}$, where $\mathcal{G} = (\Omega, \Gamma)$
and each directed edge $e\in \Gamma$ corresponds to a transition of
$\mathcal{M}$.
If $P(x,x)>0$ then the edge $xx$ is called a \emph{self-loop}
at $x$. Define $Q(e) = Q(x,y) = \pi(x)P(x,y)$ for the edge $e=xy$.
A walk in $\mathcal{G}$ is a sequence of states
$x_0 x_1 \cdots x_\ell$
such that $P(x_j,x_{j+1})> 0$ for $j=0,\ldots, \ell-1$.
The walk is \emph{closed} if $x_\ell=x_0$.
If a walk has odd length then we call it an \emph{odd walk}.

For each $x\in\Omega$ let $w_x$ be an  odd
walk from $x$ to $x$ in $\mathcal{G}$.
(Such a walk exists for each $x$, since the Markov chain
is aperiodic.)  Define
$\mathcal{W} = \{ w_x : x\in\Omega\}$, a set
of ``canonical closed odd walks''.
For each transition $e\in\Gamma$ and each $w\in\mathcal{W}$,
let $r(e,w)$ denote the number of times that $e$ appears as a
directed edge of $w$.  We can assume that $r(e,w)\leq 2$ for all
transitions $e$ (indeed, if $e$ is a self-loop then we can assume
that $r(e,w)\leq 1$.)  The \emph{congestion} of $\mathcal{W}$,
denoted by $\eta(\mathcal{W})$, is defined by
\[ \eta(\mathcal{W}) = \max_{e\in\Gamma} \, Q(e)^{-1}\,
           \sum_{x\in\Omega,\, e\in w_x}\, r(e,w_x)\, \pi(x)\, |w_x|.\]

\begin{lemma} \emph{\cite[p.702]{DSC}}\
Suppose that $\mathcal{M}$ is a reversible, ergodic Markov chain with
state space $\Omega$, and let $\mathcal{W}$ be a set of odd walks
defined as above.  Then
\[ (1 + \lambda_{N-1})^{-1} \leq  \frac{\eta(\mathcal{W})}{2}.\]
\label{lazy}
\end{lemma}

If $|w_x|=1$ for all $x\in\Omega$ then
the bound of Lemma~\ref{lazy} simplifies further to
\begin{equation}
\label{all-loops}
 (1+\lambda_{N-1})^{-1} \leq \dfrac{1}{2}\operatorname{max}_{x\in\Omega}
P(x,x)^{-1}.
\end{equation}

\begin{remark}
\emph{
Suppose that the graph underlying a Markov chain $\mathcal{M}$
can be obtained from a connected bipartite graph by adding loops
to an exponentially small proportion of states.
For example, many instances
of the \emph{knapsack chain}~\cite{MS99} satisfy this property.
Since every closed odd walk must traverse at least one of
these self-loop edges, it is very difficult to define a set of
canonical closed odd walks with low congestion.
So Lemma~\ref{lazy} is unlikely to be easy to apply in this case.
}
\end{remark}

\section{Applications of the method}\label{s:applications}

We illustrate the use of Lemma~\ref{lazy}
by applying it to three combinatorial Markov chains.
Our applications are all ergodic and reversible with uniform stationary
distribution, and no edge will be used more than once
in any walk $w_x$ that we define.  In this case the congestion
can be simplified to
\begin{equation}
\label{simple}
 \eta(\mathcal{W}) = \operatorname{max}_{e\in\Gamma} \, P(e)^{-1}
\, \sum_{x\in\Omega,\,\, e\in w_x} \,  |w_x|,
\end{equation}
where $P(e) = P(x,y) = P(y,x)$ for the transition $e=xy$.

\subsection{The switch chain for sampling regular graphs}

Our first application is to the Markov chain for sampling regular
graphs known as the \emph{switch chain}.
A transition of the chain is performed as follows:
from the current state $G$  (a $d$-regular graph on vertex set $[n]$)
choose an unordered pair of non-incident edges uniformly at random,
let $G'$ be the multigraph obtained from $G$ by deleting these edges
and inserting a perfect matching
of their four endvertices, selected uniformly at random.   If $G'$ has no
repeated edges then the new state is $G'$, otherwise it is $G$.

The lazy version
of this chain was analysed by
Cooper et al.~\cite{CDG,CDG-corrigendum}.
Clearly $P(G,G)\geq \nfrac{1}{3}$ for every
state $G$ of this chain, so by (\ref{all-loops}) we immediately
conclude that
\[ (1 + \lambda_{N-1})^{-1}\leq \nfrac{3}{2}.
\]
This is several orders of magnitude smaller than the best-known
bound on $(1-\lambda_1)^{-1}$, which is $O(d^{23} n^8)$
(see~\cite{CDG-corrigendum}).

\subsection{Jerrum and Sinclair's matchings chain }

The next application is to the well-known
Markov chain for sampling perfect and near-perfect matchings
of a fixed graph $G$.  A transition of the chain is performed as
follows: from the current state $M$ (which is a
perfect or near-perfect matching of $G$), choose an edge $e\in E(G)$
uniformly at random.  If $M$ is a perfect matching and $e\in M$ then the
new state is $M - \{e\}$.  If $M$ is a near-perfect matching and both
endvertices of $e$ are unmatched in $M$ then the new state is $M \cup \{e\}$.
If $M$ is a near-perfect matching, and exactly one endvertex of $e$
is unmatched in $M$ then let $e'$ be the edge of $M$ which matches the other
endvertex of $e$: the new state is $(M - \{e'\})\cup\{e\}$.
In all other cases the new state is $M$.

The lazy version of this chain was analysed by Jerrum and Sinclair~\cite{JS89,JS96},
If $G$ itself is not a
perfect matching then $P(M,M)\geq 1/|E|$ for all states $M$
of the chain (that is, for all perfect or near-perfect matchings $M$
of $G$).
Therefore (\ref{all-loops}) implies that
\[ (1 + \lambda_{N-1})^{-1} \leq \frac{|E|}{2}.\]
This bound is at least a factor $n^2$ smaller than the smallest-known
bound on $(1-\lambda_1)^{-1}$, which is $O(n|E|q(n))$
for graphs $G$ for which the ratio between the number of
near-perfect and perfect matchings is $q(n)$ (see~\cite{JS96}).

\subsection{A heat-bath chain for sampling contingency tables}

Our final application involves contingency tables.
Let $\mathbf{r}=(r_1,\ldots, r_m)$ and $\mathbf{c}=(c_1,\ldots, c_n)$
be two vectors of positive
integers with the same sum.  A \emph{contingency table} with row sums $r$ and
column sums $c$ is an $m\times n$ matrix $X=(x_{i,j})$ with nonnegative
integer entries, such that
$\sum_{j=1}^n x_{i,j} = r_i$ for $i=1,\ldots, m$ and
$\sum_{i=1}^m x_{i,j} = c_j$ for $j=1,\ldots, n$.
Let $\Omega_{\mathbf{r},\mathbf{c}}$ denote the set of all contingency tables
with row sums $\mathbf{r}$ and column sums $\mathbf{c}$.
To avoid trivialities we assume throughout this section that
$\min\{ m,n\}\geq 2$.

Dyer and Greenhill~\cite{DG-contingency} proposed a Markov chain for sampling
contingency tables, which we will call the \emph{contingency chain}.
A transition of the chain is performed as follows:  choose a $2\times 2$
subsquare of the current table uniformly at random, then
replace this $2\times 2$ subsquare by a uniformly chosen $2\times 2$
nonnegative integer matrix with the same row and column sums.

The \emph{lazy} contingency
chain does nothing at each step with probability $\nfrac{1}{2}$, and otherwise
performs a transition as described above.
Cryan et al.~\cite{CDGJM} analysed the lazy contingency chain for a constant
number of rows.  They proved that $(1-\lambda_1)^{-1}\leq n^{f(m)}$ for
$m$-rowed contingency tables with $n$ columns, where $m$ is constant
and $f(m)$ is an expression satisfying $f(m)\geq 68m^4$.
We now analyse the smallest eigenvalue of the (non-lazy) contingency chain.

There is always a positive probability that the next state $X'$ of the
contingency chain is equal to the current state $X$,
since the heat-bath step may simply replace the
chosen $2\times 2$ subsquare with its current contents.
However,
the minimum of $P(X,X)$ over all states $X$ depends on
$\mathbf{r}$ and $\mathbf{c}$.
(To see this, consider $2\times 2$ squares.)
We prefer a bound which depends only on $m$ and $d$, and so we do not
simply apply (\ref{all-loops}).

\begin{lemma}
\label{non-lazy-contingency}
Let $\mathbf{r}=(r_1,\ldots, r_m)$ and $\mathbf{c}=(c_1,\ldots, c_n)$
be vectors of positive
integers with a common sum which satisfy
\[ r_1\geq r_2\geq \cdots \geq r_m\quad \text{ and } \quad
   c_1\geq c_2 \geq \cdots \geq c_n.\]
Suppose that $\min\{r_1,\, c_1\}\geq 2$ and $\max\{ m,n\}\geq 3$.
The smallest eigenvalue of the contingency chain on
$\Omega_{\mathbf{r},\mathbf{c}}$ satisfies
\[ (1+\lambda_{N-1})^{-1} \leq 45\, m^3n^3. \]
\end{lemma}

\begin{proof}
Write $[a] = \{ 1,2,\ldots, a\}$ for $a\in\mathbb{Z}^+$.
From $X = (x_{i,j})\in\Omega_{\mathbf{r},\mathbf{c}}$,
first suppose that there exists a
5-tuple $(i_1,i_2,i_3,j_1,j_2)$ such
that
\begin{itemize}
\item $i_1,i_2,i_3$ are distinct elements of $[m]$,
\item $j_1,j_2$ are distinct elements of $[n]$,
\item $x_{i_1,j_1},\, x_{i_2,j_1},\, x_{i_3,j_2}$ are all positive.
\end{itemize}
Then $(i_1,i_2,i_3,j_1,j_2)$ is called \emph{row-good for $X$}, and $X$ is called \emph{row-good}.
If $X$ is row-good, fix the lexicographically least 5-tuple $(i_1,i_2,i_3,j_1,j_2)$ which is row-good for
$X$ and consider the following sequence of three transitions on the $3\times 2$ subsquare defined
by rows $i_1,i_2,i_3$ and columns $j_1,j_2$:
\[ \begin{pmatrix}  y_{1,1} & y_{1,2}\\  y_{2,1} & y_{2,2}\\ y_{3,1} & y_{3,2}\end{pmatrix} \,\, \Longrightarrow
  \,\, \begin{pmatrix}  y_{1,1}-1 & y_{1,2}+1\\  y_{2,1} & y_{2,2}\\ y_{3,1}+1 & y_{3,2}-1\end{pmatrix} \,\, \Longrightarrow
\,\,  \begin{pmatrix}  y_{1,1} & y_{1,2}\\  y_{2,1} -1 & y_{2,2}+1\\ y_{3,1}+1 & y_{3,2}-1\end{pmatrix} \,\, \Longrightarrow
\begin{pmatrix}  y_{1,1} & y_{1,2}\\  y_{2,1} & y_{2,2}\\ y_{3,1} & y_{3,2}\end{pmatrix}.
\]
(For notational convenience we have written $y_{k,\ell}$ for $x_{i_k,j_\ell}$ in the above.)
Note that all intermediate matrices are nonnegative, due to the row-good property.
This defines a walk $w_X$ of length 3 from $X$ to $X$ in the graph underlying the contingency chain.

We can define 5-tuples $(i_1,i_2,j_1,j_2,j_3)$ which are \emph{column-good for $X$}
in the analogous way, and say that
$X$ is column-good if there is a 5-tuple which is column-good for $X$.   If $X$ is column-good then
taking the transpose of each matrix in the sequence of transitions above defines
an odd walk $w_X$ of length 3 from $X$ to $X$.

Finally, suppose that $X\in\Omega_{\mathbf{r},\mathbf{c}}$ is not row-good and
is not column-good.
Such an $X$ is said to be \emph{bad}.
Then no row or column of $X$ contains more than one positive entry.   Since all row and column
sums are positive, it follows that $m=n\geq 3$ and that every row and column contains exactly one
positive entry.
Let $(i_1,i_2,i_3,j_1,j_2,j_3)$ be the lexicographically-least 6-tuple such that
\begin{itemize}
\item $i_1,i_2,i_3$ are distinct elements of $[m]$,
\item $j_1,j_2,j_3$ are distinct elements of $[n]$,
\item $x_{i_1,j_1} \geq 2$, while  $x_{i_2,j_2}$ and $x_{i_3,j_3}$ are positive.
\end{itemize}
(The conditions on $\mathbf{r}$ and $\mathbf{c}$ guarantee that such a 6-tuple exists.)  Consider the following sequence
of 5 transitions, performed on the $3\times 3$ subsquare defined by rows $i_1,i_2,i_3$ and columns
$j_1,j_2,j_3$:
\begin{align*}
\begin{pmatrix} y_{1,1} & 0 & 0\\ 0 & y_{2,2} & 0\\ 0 & 0 & y_{3,3}\end{pmatrix}
  & \Longrightarrow \,\,
  \begin{pmatrix} y_{1,1}-1 & 1 & 0\\ 1 & y_{2,2}-1 &0\\ 0 & 0 & y_{3,3}\end{pmatrix} \,\, \Longrightarrow \,\,
  \begin{pmatrix} y_{1,1}-1 & 1 & 0\\ 0 & y_{2,2}-1 &1\\ 1 & 0 & y_{3,3}-1\end{pmatrix} \\
   & \Longrightarrow \,\,
    \begin{pmatrix} y_{1,1}-2 & 1 & 1\\ 1 & y_{2,2}-1 &0\\ 1 & 0 & y_{3,3}-1\end{pmatrix} \,\, \Longrightarrow \,\,
    \begin{pmatrix} y_{1,1}-1& 0 & 1\\ 0 & y_{2,2} &0\\ 1 & 0 & y_{3,3}-1\end{pmatrix} \\
      & \Longrightarrow \,\,
    \begin{pmatrix} y_{1,1}& 0 & 0\\ 0 & y_{2,2} &0\\ 0 & 0 & y_{3,3}\end{pmatrix}.
\end{align*}
This defines a walk $w_X$ of length 5 from $X$ to $X$ in the graph underlying the chain.

Now we must analyse the set
$\mathcal{W} = \{ w_X : X\in\Omega_{\mathbf{r},\mathbf{c}}\}$ of odd walks
defined above.
Let $e=(Z,Z')$ be a transition of the contingency chain.
Then $Z$ and $Z'$ only differ in a $2\times 2$
subsquare defined by rows $i,i'$ and columns $j,j'$.

First we seek row-good $X$ with $e\in w_X$.  Let $i''\not\in\{i,i'\}$
be another row index, and fix one of
the 6 ways to arrange $(i,i',i'',j,j')$ as $(i_1,i_2,i_3,j_1,j_2)$.  This gives enough information to uniquely
identify a potential candidate for $X$.   For example, if the transition $e$ involves rows $i_1$ and $i_3$ then
$X=Z$, while if the transition $e$ involves rows $i_2$ and $i_3$ then $X=Z'$.  If $e$ involves rows $i_1$ and $i_2$
then $e$ is the second transition in the sequence, and $X$ can be obtained from $Z$ by reversing
the first transition in the sequence: namely, adding 1 to entries $(i_1,j_1)$ and $(i_3,j_2)$ and
subtracting 1 from entries $(i_1,j_2)$ and $(i_3,j_1)$.  If $X$ is a valid contingency table then
$(i_1,i_2,i_3,j_1,j_2)$ is row-good for $X$.  If it is the lexicographically
least such 5-tuple for $X$ then $e\in w_X$.
This identifies at most $12(m-2)$ tables $X$ such that $e\in w_X$.
(This is an overcount, but good enough for our purposes.)

By choosing a third column index $j''\not\in\{j,j'\}$, an analogous argument shows that there are at most
$12(n-2)$ column-good tables $X$ with $e\in w_X$.

Finally, we seek bad tables $X$  such that $e\in w_X$.
Choose a row index $i''\not\in\{ i,i'\}$ and a column index
$j''\not\in\{j,j'\}$, and fix one of the at most
36 ways to arrange $(i,i',i'',j,j',j'')$ as $(i_1,i_2,i_3,j_1,j_2,j_3)$.  Now
each transition in the sequence alters a different $2\times 2$ subsquare except the first and fourth,
which both alter rows $i_1,i_2$ and columns $j_1,j_2$.  Hence, arguing as above, there are at
most two choices for $X$, for each fixed 6-tuple.   This gives at most $72(m-2)(n-2)$ bad tables
$X$ such that $e\in w_X$.

Combining all this, we find that the congestion parameter $\eta(\mathcal{W})$ satisfies
\[ \eta(\mathcal{W}) \leq \binom{m}{2}\,\binom{n}{2}\, \left(36(m-2)+36(n-2)+360(m-2)(n-2)\right)
    \leq 90\, m^3n^3,
    \]
and applying Lemma~\ref{lazy} completes the proof.
\end{proof}

Again we observe that this bound on $(1+\lambda_{N-1})^{-1}$ is several orders
of magnitude lower than the best-known
bound on the second-largest eigenvalue~\cite{CDGJM}.

\begin{remark}
It has recently been shown~\cite{GU} that the contingency chain described above has no negative eigenvalues.
We include Lemma~\ref{non-lazy-contingency} here to illustrate an application of Lemma~\ref{lazy}
involving walks of length greater than one.
\end{remark}

Combinatorics (math.CO); Discrete Mathematics (cs.DM)

Making Markov chains less lazy


Automated Reasoning for Physical Quantities, Units, and Measurements in Isabelle/HOL

Abstract: Formal verification of cyber-physical and robotic systems requires that we can accurately model physical quantities that exist in the real-world. The use of explicit units in such quantities can allow a higher degree of rigour, since we can ensure compatibility of quantities in calculations. At the same time, improper use of units can be a barrier to safety and therefore it is highly desirable to have automated sanity checking in physical calculations. In this paper, we contribute a mechanisation of the International System of Quantities (ISQ) and the associated SI unit system in Isabelle/HOL. We show how Isabelle can be used to provide a type system for physical quantities, and automated proof support. Quantities are parameterised by dimension types, which correspond to base vectors, and thus only quantities of the same dimension can be equated. Since the underlying "algebra of quantities" induces congruences on quantity and SI types, specific tactic support is developed to capture these. Our construction is validated by a test-set of known equivalences between both quantities and SI units. Moreover, the presented theory can be used for type-safe conversions between the SI system and others, like the British Imperial System (BIS).

\section{Introduction}
\input{sec/intro}

\section{Related Work}
\label{sec:related}
\input{sec/related}

\section{Dimensions}
\label{sec:dimensions}
\input{sec/dimensions}

\section{Physical Quantities and Measurement}
\label{sec:quantities}
\input{sec/quantities}

\section{Unit Systems and the SI}
\label{sec:units}
\input{sec/units}

\section{Unit Conversions and Non-SI Systems}
\label{sec:conversions}
\input{sec/conversions}

\section{Conclusions}
\label{sec:conclusions}
\input{sec/conclusions}

\bibliographystyle{IEEEtran}

\subsection{Universe of Dimensions}

We begin by defining the dimension universe, the core operators for constructing dimensions, and their properties. If we assume there are $n \in \nat$ base quantities, then a dimension has the form $d_1^{\vec{x}_1} \cdot d_2^{\vec{x}_2} \cdots d_n^{\vec{x}_n}$, a product of dimension symbols ($d_i$) each raised to a power drawn from the vector $\vec{x}$. The encoding of the dimension vector $\vec{x}$ in Isabelle is shown below.

\begin{definition}[Dimension Vectors]
$$\textbf{typedef}~(N, I)~\textit{dimvec} = (\textit{UNIV} :: (I::enum) \Rightarrow N)$$
\end{definition}

\noindent The \textbf{typedef} command in Isabelle/HOL introduces a new type characterised by a non-empty subset of an existing type. In this case, we introduce a new type \textit{dimvec} with two parameters $N$ and $I$. We choose the set $UNIV$ of all total functions from $I$ to $N$ as the characteristic set. In future type definitions, we will use \textbf{typedef} to further restrict the subset.

Conceptually, a dimension vector is simply a total function from an enumerable index type $I$, for the possible dimensions, to a numeric type $N$, which should minimally form a ring (e.g. $\num$). The enumerable ($enum$) sort constraint ($I :: enum$) requires that $I$ is isomorphic to a list of values, and is thus also a finite type. In the ISQ we have $I = \{\mathbf{L}, \mathbf{M}, \mathbf{T}, \mathbf{I}, \Theta, \mathbf{N}, \mathbf{J}\}$, for example.

Next, we define the core dimension constructors. For simplicity, we present these as functions using $\lambda$-terms, but they are technically defined using the lifting package~\cite{Huffman13Lifting}.

\begin{definition}[Dimension Constructors]
  $$\mathbf{1} \defs (\lambda i.~ 0) \quad \textbf{b}(i) \defs \mathbf{1}(i \mapsto 1)$$
  $$\vec{x} \cdot \vec{y} \defs (\lambda i.~ \vec{x}(i) + \vec{y}(i)) \quad \vec{x}^{-1} \defs (\lambda i.~ - \vec{x}(i))$$
\end{definition}
\noindent Here, $\mathbf{1}$ denotes a null dimension, which does not map to any physical quantity. It can characterise dimensionless quantities, such as mathematical constants ($\pi$, $e$, etc.) and functions.

The function $\textbf{b}(i)$, for $i \in I$, constructs a \emph{base dimension} from the base quantity $i$ by updating the mapping for $i$ in $\mathbf{1}$ to have the power $1$. A base dimension has exactly one entry in the vector mapping to 1, with the others all 0. We also define a predicate $\textit{is-BaseDim} :: (N, I)~\textit{dimvec} \Rightarrow \mathbb{B}$, which determines whether a dimension vector corresponds to a base dimension.

A product of two dimensions ($\vec{x} \cdot \vec{y})$ simply pointwise sums together all of the powers, and an inverse ($\vec{x}^{-1}$) negates each of the powers. We can also now obtain division using the usual definition: $\vec{x} / \vec{y} \defs \vec{x} \cdot \vec{y}^{-1}$. With these definitions, we can prove the following group theorem:

\begin{theorem} If $(N, +, 0, -)$ forms an abelian group then also $((N, I)\textit{dimvec}, \cdot, \mathbf{1}, {}^{-1})$ forms an abelian group.
\end{theorem}
\noindent The abelian group laws can therefore be used to equationally rewrite dimension expressions, which is automated using Isabelle's simplifier.

Another avenue to efficient proof for dimensions is provided through the Isabelle code generator~\cite{Haftman2010-CodeGen}. Since the set of base quantities $I$ is enumerable, we can always convert a dimension vector to a list of $N$, and vice-versa. We achieve this using a function \textit{mk-dimvec}, which converts a list of $N$ with length $|\!I\!|$ to a dimension vector in $N$.

\begin{definition}[Converting Lists to Dimensions] $ $ %
\label{def:list-to-dim}

\vspace{1ex}
  \noindent
  $$

  \textit{mk-dvec}(ds) \defs \left(\begin{array}{l} \mathbf{if}~ (length(ds) = ~|\!I\!|) \\
                                       \mathbf{then}~ (\lambda d.~ ds(\textit{enum-ind}(d))) ~\mathbf{else}~ \mathbf{1}\end{array}\right)
  $$
\end{definition}
\noindent Since $I$ is enumerable, every dimension can be assigned a natural number, which also denotes its position in the underlying list. The function $\textit{enum-ind} :: (I\!::\!enum) \Rightarrow \nat$ extracts this positional index of a value in an enumerable type. For ISQ, we have $\textit{enum-ind}(\mathbf{L}) = 0$ and $\textit{enum-ind}(\mathbf{T}) = 2$, for example. We can then construct a dimension from a list $ds$ simply by looking up the value at the enumeration index.

Every possible dimension can be constructed using \textit{mk-dvec}, and so we can use it as a so-called ``code datatype'' for the Isabelle code generator. Dimensions are then encoded in SML or Haskell as an algebraic datatype with a single constructor corresponding to \textit{mk-dvec}, for example:

\begin{alltt}
 \textbf{datatype} ('a, 'b) dimvec = Mk_dimvec of 'a list
\end{alltt}

\noindent We then prove code equation theorems for the group operators, which are homomorphism laws, and enable efficient execution:

\begin{theorem} For a dimension vector space $(N, I)~dimvec$, with $|\!xs\!| \,=\, |\!ys\!| \,=\, |\!I\!|$, the following code equations hold:
\begin{align*}
  \mathbf{1} &= \textit{mk-dvec}\,(\textit{replicate}\,|\!I\!|\,0) \\
  \textit{mk-dvec}(xs) \cdot \textit{mk-dvec}(ys) &= map~(\lambda (x, y).\, x + y)~(zip~xs~ys) \\
  (\textit{mk-dvec}(xs))^n &= \textit{mk-dvec}~(map~(\lambda x.\, n \cdot x)~xs \\
  (\textit{mk-dvec}(xs))^{-1} &= \textit{mk-dvec}~(map~(\lambda x.\, - x)~xs
\end{align*}
\end{theorem}
\noindent These theorems give concrete definitional equations for the executable functions on the datatype. The null dimension is a list of $0$ powers of length $|\!I\!|$. Multiplication of two equilength lists $xs$ and $ys$ is pairwise addition of each element. Raising to the $n$th power multiplies each list element by $n$. Taking the inverse power negates each element. With such equations we can perform efficient dimension arithmetic on dimensions constructed from lists.

Dimensions in the ISQ are represented using the concrete dimension index type \textit{sdim}:

\begin{definition}[ISQ Base Quantities]
\label{def:isq-base-quant}
\begin{align*}
\textbf{datatype}~ sdim &= Length | Mass | Time | Current \\
                        &| Temperature | Amount | Intensity \\[1ex]
\textbf{type-synonym}~ Dimension &= (\num, sdim)~dimvec
\end{align*}
\end{definition}
\noindent
It suffices to show that \textit{sdim} is enumerable, using a type class instantiation, and then we can create a specific type synonym \textit{Dimension}, for dimension vectors in the ISQ. For convenience, we then define dimension vectors for each of the base quantities, for example $\mathbf{L} \defs \mathbf{b}(Length)$.

\subsection{Dimension Types}
\label{sec:dim-types}

Having defined our dimension universe, the next step is to characterise the family of dimension types. These dimension types will be used to parameterise our quantities, and ensure only quantities of the type dimension may be compared. We avoid the need for dependent types by first introducing a type class for dimension types.

\begin{definition}[Dimension Type Classes] $ $ %

\vspace{1ex}

$\begin{array}{l}
\textbf{class}~\textit{dim-type} = unitary~ + \\
~~ \textbf{fixes}~\textit{dim-ty-sem} :: \mathcal{D}~itself \Rightarrow Dimension \\[2ex]
\textbf{class}~\textit{basedim-type} = \textit{dim-type}~ + \\
~~ \textbf{assumes}~ \textit{is-BaseDim}\!: \textit{is-BaseDim}(QD(\mathcal{D}))
\end{array}
$

\end{definition}
\noindent A type class characterises a family of types that each implement a given function signature with certain properties, such as algebraic structures like monoids and groups. The \textbf{class} command introduces a type class with a given name, potentially extending existing classes. The \textbf{fixes} subcommand declares a new typed symbol in the signature, and \textbf{assumes} introduces a property of the symbols in the signature.

The \textit{dim-type} class characterises a $unitary$ type $\mathcal{D}$ (i.e. a type with cardinality 1) and associates it to a particular dimension. The type $\mathcal{D}~itself$ represents a type as a value in Isabelle/HOL. Thus, \textit{dim-ty-sem} can be seen as a function from types inhabiting the \textit{dim-type} class to particular dimensions, as shown in Figure~\ref{fig:dim-mapping}. We can use the syntactic constructor $TYPE(\alpha)$ to obtain a value of type $\alpha~itself$, for a particular type $\alpha$. This effectively introduces an isomorphism between dimensions at the value level and the type level. For convenience, we introduce the notation $QD(\mathcal{D}) \defs \textit{dim-ty-sem}~\textit{TYPE}(\mathcal{D})$, which obtains the dimension of a given dimension type. The class \textit{basedim-type} further specialises \textit{dim-type} by requiring that the mapped dimension is a base dimension.

We use these classes to capture the set of type constructors for dimension types. First we construct types to denote the base dimensions, as unitary types. For example, we define the type length as below:
$$\textbf{typedef}~Length = (UNIV :: unit~set)$$
which exploits the fact that a type definition generates a fresh type name from a set
(in this case, the set that just contains the only element of the \textit{unit} type).
Though there is a seeming clash with the \textit{Length} constructor introduced in \cref{def:isq-base-quant}, these names inhabit different name spaces. \textit{Length} here is a ``tag type'' whose members do not convey information, but  represent dimension types syntactically.

We define seven such types, one for each of the ISQ base quantities, and also a further special type called \textbf{1}, which corresponds to a dimensionless quantity. Each of the base dimensions instantiates the \textit{basedim-type} class by mapping to the corresponding dimension symbol introduced in the previous section, such that, for example, $QD(Length) = Length$.

Next, we introduce the arithmetic operators for dimensions at the type level. The product and inverse type constructors are defined as shown below:
\begin{align*}
    &\textbf{typedef}~(\mathcal{D}_1\!::\!\textit{dim-ty},\mathcal{D}_2\!::\!\textit{dim-ty})~\textit{DimTimes} = (\textit{UNIV} :: unit set) \\
    &\textbf{typedef}~(\mathcal{D}\!::\!\textit{dim-ty})~DimInv = (\textit{UNIV} :: unit set)
\end{align*}
They are similarly tag types, but the parameters must inhabit the \textit{dim-ty} class. This ensures that the dimension types are closed under products and inverse. Using these type constructors, and the base dimension types, we can inductively define algebraic dimensions at the type level. We assign the type constructors the following implementations of \textit{dim-ty-sem}:
\begin{definition}[Semantic Interpretation of Dimension Types] \label{def:sem-dim}
\begin{align*}
    \textit{dim-ty-sem}(d :: (\mathcal{D}_1, \mathcal{D}_2)~\textit{DimTimes}) &= QD(\mathcal{D}_1) \cdot QD(\mathcal{D}_2) \\
    \textit{dim-ty-sem}(d :: (\mathcal{D})~\textit{DimInv}) &= QD(\mathcal{D})^{-1}
\end{align*}
\end{definition}
These link together the type constructors and the underlying dimension operators. The semantics of a \textit{DimTimes} type calculates the underlying value-level dimension of each parameter $\mathcal{D}_1$ and $\mathcal{D}_2$, and multiplies them together. The \textit{DimInv} type similarly calculates the dimension and then takes the inverse. We give these type constructors the usual mathematical syntax, so that we can write dimension types like $\mathbf{M} \cdot \mathbf{L}$ and $\mathbf{T}^{-1}$. We also define a type synonym for division, namely $(\mathcal{D}_1, \mathcal{D}_2)~\textit{DimDiv} \defs \mathcal{D}_1 \cdot \mathcal{D}_2^{-1}$, and give it the usual syntax. Moreover, we define a fixed number of powers and inverse powers at the type level, such as $\mathcal{D}^{-3} = (\mathcal{D} \cdot \mathcal{D} \cdot \mathcal{D})^{-1}$.

We can now also create the set of derived dimensions specified in the ISQ using type synonyms. For example, we define $\textit{Velocity} \defs \mathbf{L} \cdot \mathbf{T}^{-1}$ and $\textit{Pressure} \defs \mathbf{L}^{-1} \cdot \mathbf{M} \cdot \mathbf{T}^{-1}$, which provides a terminology of dimensions for use in formal specifications. We show further example in Figure~\ref{fig:derived-dimensions}, which also demonstrates the mathematical syntax for dimensions implemented in Isabelle/HOL.

\begin{figure}[t]
  \centering
  \includegraphics[width=.6\linewidth]{figures/Derived_Dimensions.png}

  \caption{Derived dimension type expressions in Isabelle/HOL}
  \label{fig:derived-dimensions}

  \vspace{-1ex}
\end{figure}

\subsection{Dimension Normalisation}
\label{sec:dim-norm}

Unlike dimensions at the value level, dimension types with different syntactic forms are incomparable, because they are distinct type expressions. For example, it is intuitively a fact that $\mathbf{L} \cdot \mathbf{T}^{-1} \cdot \mathbf{T} = \mathbf{L}$, which can be proved using the group laws. However, at the type level $\mathbf{L} \cdot \mathbf{T}^{-1} \cdot \mathbf{T}$ and $\mathbf{L}$ are different type expressions, and no built-in normalisation is available in Isabelle. As a result, we need to implement our own normalisation function, $\textit{normalise}(\mathcal{D})$, for ISQ dimensions in Isabelle/ML, so that quantities over dimensions with distinct syntactic forms can be related. Our normalisation function evaluates the dimension vector of a dimension expression, and then uses this to produce a normal form.

We implement dimension type evaluation using the ML function $\textit{typ-to-dim} :: typ \Rightarrow int~list$. It converts types formed of the base dimensions and dimension arithmetic operators into a dimension vector list, using the representation given in \cref{def:list-to-dim}. For example, $\textit{typ-to-dim}(\mathbf{L}) = [1, 0, 0, 0, 0, 0, 0]$ and $\textit{typ-to-dim}(\mathcal{D}^{-1}) = map~(\lambda x.\, - x)~(\textit{typ-to-dim}(x))$.

Having evaluated the dimension expression, we can use it to construct the normal form. This is an ordered dimension expression of the form $\mathbf{L}^{\vec{x}_1} \cdot \mathbf{M}^{\vec{x}_2} \cdot \mathbf{T}^{\vec{x}_3} \cdots \mathbf{J}^{\vec{x}_7}$, except that we omit terms where $\vec{x}_i = 0$. If every such term is $0$, then the function produces the dimensionless quantity, $\mathbf{1}$. As an example, $\textit{normalise}(\mathbf{T}^4 \cdot \mathbf{L}^{-2} \cdot \mathbf{M}^{-1} \cdot \mathbf{I}^2 \cdot \mathbf{M})$ yields the dimension type $\mathbf{L}^{-2} \cdot \mathbf{T}^4 \cdot \mathbf{I}^2$. This normalisation function is used later in this paper to facilitate coercion between quantities with distinct dimension expressions.

\subsection{Quantity Universe and Measurement Systems}
\label{sec:quant-univ}

We specify our quantity universe as a record with fields for the magnitude and dimension of the quantity.

\begin{definition}[Quantity Universe]
$$\begin{array}{rl}
  \textbf{record}~ (N, I\!::\!enum)\, Quantity =&\hspace{-1.5ex} mag\!::\!N\ \\ &\hspace{-1.5ex} dim\!::\!(int, I)\,dimvec
\end{array}$$
\end{definition}

\noindent The \textit{Quantity} type is parametric over a numeric type $N$ (e.g. $\mathbb{Q}$, $\mathbb{R}$), which should form a field, and the dimension index type $I$. The magnitude is then a number in $N$, and a dimension vector in over $I$. We can now specify the core arithmetic operators on quantities. For convenience of presentation, we use tuple syntax $(x, \mathcal{D})$, though in Isabelle the record fields are used.

\begin{definition}[Quantity Arithmetic Operators] \label{def:quant-arith}
  \begin{align*}
    0 &\defs (0, \mathbf{1}) \\
    1 &\defs (1, \mathbf{1}) \\
    (x, \mathcal{D}_1) \cdot (y, \mathcal{D}_2) &= (x \cdot y, \mathcal{D}_1 \cdot \mathcal{D}_2) \\
    (x, \mathcal{D})^{-1} &= (x^{-1}, \mathcal{D}^{-1}) \\
    (x, \mathcal{D}_1) / (y, \mathcal{D}_2) &= (x / y, \mathcal{D}_1 / \mathcal{D}_2) \\
    (x, \mathcal{D}) + (y, \mathcal{D}) &= (x + y, \mathcal{D}) \\
    (x, \mathcal{D}) - (y, \mathcal{D}) &= (x - y, \mathcal{D}) \\
    (x, \mathcal{D}_1) \le (y, \mathcal{D}_2) &\iff (x \le y \land \mathcal{D}_1 = \mathcal{D}_2)
  \end{align*}
\end{definition}

\noindent The arithmetic operators are overloaded in Isabelle/HOL, which is why they can validly appear on both sides of these equations. The ``0'' and ``1'' quantities are specified as dimensionless quantities with magnitude 0 and 1, respectively. Multiplication, inverse, and division are total operations that simply distribute through the pair. When multiplying two quantities, we need to multiply both the magnitudes and dimensions. For example, $(7, \mathbf{L}\cdot\mathbf{T}^{-1}) \cdot (2, \mathbf{T}) = (14, \mathbf{L})$. In contrast, addition and subtraction are partial operators that may be applied only when the two quantities have the same dimension. In Isabelle/HOL, the value of an addition or subtraction for different quantities of different dimensions is unspecified. Finally, the order on quantities is simply the order on the magnitudes, but with the requirement that the two dimensions are equal.

Quantities as formalised so far specify the form of dimension, but not the system of units being employed. For this, we extend the \textit{Quantity} type to create ``measurement systems'':

\begin{definition}[Measurement Systems]
$$
\begin{array}{l}
\textbf{record} (N, I\!::\!enum, \mathcal{S}\!::\!\textit{unit-system})~\textit{Measurement-System} \\
  \quad = (N, I)~Quantity + \textit{unit-sys} :: \mathcal{S}
\end{array}
$$
\end{definition}

\noindent We extend the \textit{Quantity} record with an additional field \textit{unit-sys}. A measurement system is a quantity that specifies the system of units being used via an additional type parameter $\mathcal{S}$, which must inhabit the type class \textit{unit-system}. A unit system type is a unitary type that effectively allows us to tag quantities. This allows us to distinguish quantities using different systems of units, and so prevent improper mixing.

For example, the presence of the \textit{SI} tag means that a quantity of length is fundamentally measured in metres, whereas the presence of a tag such as \textit{BIS} may indicate that length is measured in yards. Later, we will use these to facilitate type-safe conversions between different unit systems.

All the arithmetic operators can be straightforwardly lifted to measurement systems. Since all such functions are monomorphic (e.g. of type $\alpha \Rightarrow \alpha \Rightarrow \alpha$), mixing of systems is avoided by construction.

\subsection{Dimension Typed Quantities}

Having defined our universe for quantities, we next enrich this representation with type-level dimensions. For expediency, we assume that all such quantities also have a measurement system attached. Moreover, we focus on quantities with dimensions from the ISQ.

\begin{definition}[Quantity Type]
$$
\begin{array}{l}
\textbf{typedef}~ (N, \mathcal{D}\!::\!\textit{dim-type}, \mathcal{S}::\textit{unit-system})~\textit{QuantT} \\
\quad = \{x :: (N, sdim, S)~\textit{Measurement-System}.~~ dim(x) = QD(\mathcal{D})\}
\end{array}
$$
\end{definition}
\noindent The $(N, \mathcal{D}, \mathcal{S})~\textit{QuantT}$ type represents a quantity with numeric type $N$, dimension type $\mathcal{D}$, and unit system $\mathcal{S}$. The type definition introduces an invariant that requires that the dimension of the underlying quantity $x$ agrees with the one specified in the dimension type. At this level, we use \textit{sdim} as the concrete interpretation of dimensions, as this is required by the \textit{dim-type} class. For convenience, we introduce the type syntax $N[\mathcal{D}, \mathcal{S}]$ to stand for $(N, \mathcal{D}, \mathcal{S})~\textit{QuantT}$. Our \textbf{typedef} also induces two functions for converting between typed and untyped quantitities: $\textit{fromQ} :: N[\mathcal{D}, \mathcal{S}] \Rightarrow (N, \textit{sdim}, \mathcal{S})~\textit{Measurement-System}$ and \textit{toQ} in the opposite direction.

Lifting of arithmetic operators $x + y$ and $x - y$ is straightforward for typed quantities, since they are monomorphic and only defined when the dimensions of $x$ and $y$ agree. We can then easily show that typed quantities form an additive abelian group. We also define a scalar multiplication $\textit{scaleQ} :: N \Rightarrow N[\mathcal{D}, \mathcal{S}] \Rightarrow N[\mathcal{D}, \mathcal{S}]$, with notation $n \mathop{*_\qsub} x$, which scales a quantity by a given number without changing the dimension. We can then show that typed quantities form an additive abelian group, and a real vector space, with $(\mathop{*_\qsub})$ as the scalar multiplication operator.

Things are more involved when dealing with general multiplication and division, since these need to perform dimension arithmetic at the type level. For example, if we have quantities $x :: \mathbb{R}[\mathbf{I}, SI]$ and $y :: \mathbb{R}[\mathbf{T}, SI]$, then multiplication of $x$ and $y$ is well-defined, and should have the type $\mathbb{R}[\mathbf{I}\cdot\mathbf{T}, SI]$. As a result, we introduce bespoke functions, $qtimes$, $qinverse$, and $qdivide$. We first give the types for these functions:
\begin{align*}
  qtimes &:: N[\mathcal{D}_1, \mathcal{S}] \Rightarrow N[\mathcal{D}_2, \mathcal{S}] \Rightarrow N[\mathcal{D}_1 \cdot \mathcal{D}_2, \mathcal{S}] \\
  qinverse &:: N[\mathcal{D}, \mathcal{S}] \Rightarrow N[\mathcal{D}^{-1}, \mathcal{S}] \\
  qdivide &:: N[\mathcal{D}_1, \mathcal{S}] \Rightarrow N[\mathcal{D}_2, \mathcal{S}] \Rightarrow N[\mathcal{D}_1 / \mathcal{D}_2, \mathcal{S}]
\end{align*}
\noindent The first function multiplies two quantities, with the same measurement system, and ``multiplies'' the dimension types using the type constructors introduced in \S\ref{sec:dim-types}. Technically, no multiplication computation takes place, but rather a type constructor denoting multiplication is inserted. Similarly, \textit{qinverse} represents the inverse of the parametrised dimension, and \textit{qdivide} stands for a division. What is achieved here is analogous to dependent types, though we require additional machinery for normalising dimension types (cf. \S\ref{sec:dim-norm}).

The definitions of \textit{qtimes} and \textit{qinverse} are obtained simply by lifting of the corresponding functions on quantities in Definition~\ref{def:quant-arith}, which is technically achieved using the \emph{lifting} package~\cite{Huffman13Lifting}. In order to do this we need to prove that the invariant of the \textit{QuantT} type is satisfied, which involves showing that the family of typed quantities is closed under the two functions. For \textit{qmult}, we need to prove that $dim(x \cdot y) = QD(\mathcal{D}_1 \cdot \mathcal{D}_2)$, whenever $dim(x) = QD(\mathcal{D}_1)$ and $dim(y) = QD(\mathcal{D}_2)$, which follows simply through Definitions \ref{def:sem-dim} and \ref{def:quant-arith}. For convenience, we give these functions the usual notation of $x \bullet y$, $x^{-1}$, and $x / y$, but in Isabelle we embolden the operators to syntactically distinguish them. With \textit{qtimes} and \textit{qinverse}, we can also define positive and negative powers, such as $x^{-2} = (x \bullet x)^{-1}$.

Equality ($x = y$) in HOL is a homogeneous function of type $\alpha \to \alpha \to \mathbb{B}$; therefore, it cannot be used to compare objects of different types. Consequently, it cannot be used to compare quantities whose dimension types have different syntactic forms (e.g. $\mathbf{L} \cdot \mathbf{T}^{-1} \cdot \mathbf{T}$ and $\mathbf{L}$). This motivates a definition of \emph{heterogeneous (in)equality} for quantities:
\begin{align*}
  \textit{qequiv} &:: N[\mathcal{D}_1, \mathcal{S}] \Rightarrow N[\mathcal{D}_2, \mathcal{S}] \Rightarrow \mathbb{B} \\
  \textit{qless-eq} &:: N[\mathcal{D}_1, \mathcal{S}] \Rightarrow N[\mathcal{D}_2, \mathcal{S}] \Rightarrow \mathbb{B}
\end{align*}

\noindent These functions are defined simply by lifting the functions $(=)$ and $(\le)$ on the underlying quantities. They ignore the dimension types, but the underlying dimensions must nevertheless be the same as per the definitions in \S\ref{sec:quant-univ}. We give these functions the notation $x \cong y$ and $x \lesssim y$, respectively. Relation $(\cong)$ forms an equivalence relation, and $(\lesssim)$ forms a preorder. Moreover, $(\cong)$ is a congruence relation for $(\bullet)$, $({}^{-1})$, and $(\mathop{*_\qsub})$.

\subsection{Proof Support}

We implement an interpretation-based proof strategy for typed quantity (in)equalities, which allows us to split a conjecture into two parts: (1) equality of the magnitudes; and (2) equivalence of the dimensions. This is supported by a function $\textit{magQ} :: N[\mathcal{D}, \mathcal{S}] \Rightarrow N$, with syntax $\qsem{-}$, which extracts the magnitude from a typed quantity. We can calculate magnitudes using interpretation laws, like the ones below:
  $$\qsem{x + y} = \qsem{x} + \qsem{y} \quad \qsem{x \bullet y} = \qsem{x} \cdot \qsem{y}$$
\noindent Such laws derive directly from the definition of the quantity operators in Definition~\ref{def:quant-arith}. The equation for addition implicitly makes use of the fact that $x$ and $y$ have the same dimension, and so addition is well-defined in the quantity universe. We then have the following transfer theorems, for the case of two quantitites with the same (syntactic) dimensions:
\begin{theorem}[Quantity Transfer Laws]
  $$x = y \iff (\qsem{x} = \qsem{y}) \qquad x \le y \iff (\qsem{x} \le \qsem{y})$$
\end{theorem}
\noindent In both cases, we need not check the equivalence of the dimensions as by construction we know that $x$ and $y$ have the same type, and so also have the same dimensions. It is sufficient simply to check the relation holds of the underlying magnitudes. For our heterogeneous (in)equality relations, we have the following transfer theorems:
\begin{theorem}[Heterogeneous Transfer Laws] Given quantities $x :: N[\mathcal{D}_1, \mathcal{S}]$ and $y :: N[\mathcal{D}_2, \mathcal{S}]$, we have
  $$x \cong y \iff \left(\qsem{x} = \qsem{y} \land QD(\mathcal{D}_1) = QD(\mathcal{D}_2)\right).$$
\end{theorem}
\noindent We can then prove heterogeneous equalities by calculation of the underlying magnitudes and dimensions, and use of the numeric and dimensions laws. We supply a proof method called \textit{si-simp}, which uses the simplifier to perform transfer and interpretation, and additionally invokes field simplification laws. An additional method called \textit{si-calc} also compiles dimension vectors (cf. Definition~\ref{def:list-to-dim}) using the code generator, and can thus efficiently prove dimension equalities. We can, for example, prove the following algebraic laws automatically:
\begin{theorem}[Quantity Algebraic Laws]
  \begin{align*}
    a \mathop{*_\qsub} (x + y) &= (a \mathop{*_\qsub} x) + (a \mathop{*_\qsub} y) \\
    x \bullet y &\cong y \bullet x \\
    (x \bullet y)^{-1} &\cong x^{-1} \bullet y^{-1}
  \end{align*}
\end{theorem}

\subsection{Coercion and Dimension Normalisation}

The need for heterogeneous quantity relations ($\cong$, $\lesssim$) can be avoided by the use of coercions to convert between two syntactic representations of the same dimension. Moreover, we can use Isabelle's sophisticated syntax and checking pipeline to normalise dimensions, and so automatically coerce quantities to a normal form. This improves the usability of the library, since the usual relations $(=)$ and $(\le)$ can be used directly.

We implement a function $\textit{dnorm} :: N[\mathcal{D}_1, \mathcal{S}] \Rightarrow N[\mathcal{D}_2, \mathcal{S}]$, which can convert between quantities with different dimension forms. In order to use it effectively, it is necessary to know the target dimension $\mathcal{D}_2$ in advance. It is defined below:
$$\textit{dnorm}(x) \defs (\mathbf{if}~QD(\mathcal{D}_1) = QD(\mathcal{D}_2)~\mathbf{then}~toQ~(\,fromQ(x))~\mathbf{else}~0)$$
The function checks whether the source and target dimensions ($\mathcal{D}_1$ and $\mathcal{D}_2$) are the same. If they agree, then it performs the coercion by erasing the types with \textit{fromQ} and reinstating the new dimension type with \textit{toQ}. Otherwise, it returns a valid quantity of the target dimension, but with magnitude $0$. For example, if we have $x :: \mathbb{R}[\mathbf{L} \cdot \mathbf{T}^{-1} \cdot \mathbf{T}, SI]$, then we can use $\textit{dnorm}(x) :: \mathbb{R}[\mathbf{L}, SI]$ to obtain a quantity with an equivalent dimension, since $QD(\mathbf{L} \cdot \mathbf{T}^{-1} \cdot \mathbf{T}) = QD(\mathbf{L})$. In general, for two equivalent quantities $x \cong y$, we have it that $\textit{dnorm}(x) = y$.

Next, we extend Isabelle's checking pipeline to allow dimension normalisation, so that $\mathcal{D}_2$ can be automatically calculated. We do this by implementing an SML function \textit{check-quant}, which takes a term and enriches it with dimension information. Whenever it encounters an instance of $\textit{dnorm}(t)$, it extracts the type of $t$, which should be $N[\mathcal{D}, \mathcal{S}]$. This being the case, we enrich the instance of \textit{dnorm} to have the type $N[\mathcal{D}, \mathcal{S}] \Rightarrow N[\textit{normalise}(\mathcal{D}), \mathcal{S}]$.

We then insert \textit{check-quant} into Isabelle's term checking pipeline. Technically, this is achieved using an Isabelle/ML API function called \texttt{Syntax\_Phases.term\_check}, which allow us to add a new phase into the term checking process. In this case, we add it after type inference has occurred so that we can use the unnormalised dimension type expression as an input to \textit{check-quant}.

The soundness of this transformation does not depend on the correctness of \textit{normalise}, since if an incorrect dimension is calculated, \textit{dnorm} will return 0. Nevertheless, the effect is to achieve something akin to dependent types, but in a first-order polymorphic type system.