% Document Type: LaTeX
An abstract model of words should encompass all their basic
properties. It should be independent of any concrete representation.
The basic abstract properties of words are:
\begin{itemize}
\item a word is a vector of $n$ elements;
\item the size of a given word $n$ is constant;
\item all elements are of the same type;
\item an individual element is accessed via its index.
\end{itemize}
Suppose that $w$ is a word of size $n$, it can be written as
\[
w = \nword{w_{n-1} w_{n-2} \ldots w_1 w_0}
\]
where $w_i$ represents the $i$th bit of the word $w$.
We adopt the convention that the bits are indexed from the right hand
side starting from 0. The index operation $w[i]$ accesses the $i$th
bit of a word for all $i$ less then $n$. A segment operation extracts a
segment from a word. For example, 
\begin{equation}
w[m,k] = \nword{w_{k + m -1} \ldots w_k}\label{eq-seg}
\end{equation}
where $(k + m) \leq n$ is a $m$-bit segment of the word $w$ starting
from the $k$th bit.

A word concatenation operation $\wcat$ can be defined as
\begin{eqnarray}
A \wcat B & = & \nword{a_{n-1}\ldots a_0} \wcat \nword{b_{m-1}\ldots b_0}\\ \nonumber
          & = & \nword{a_{n-1}\ldots a_0 b_{m-1}\ldots b_0} \label{eq-wordct}
\end{eqnarray}
which builds a word of size $n + m$ from two words of size $n$ and
$m$, respectively.

Since words of all sizes share these basic properties, a base type of
some kind would be a starting point for modelling words. This base
type should then be parameterized with the size and the type of the
elements.  This suggests a dependent type of the form
\[:(\alpha, n)\mbox{\tt word} \]
where $\alpha$ is the type of the elements and $n$ is the size. 
In the current version of the \HOL\ system, it is possible to define a
polymorphic type \word{$\alpha$}\ which takes the element type as a
parameter, but it is not possible to parameterize a type with natural numbers.
There is also difficulty in defining a real abstract type in the current
version of \HOL.

To overcome the difficulties mentioned above, the approach used in
implementing the {\tt word} library  uses  facilities available in the
current version of \HOL\ only. First of all, it defines a polymorphic
type \word{*}\ to represent generic words. This allows one to use
different types to represent the bits according to the requirements of
one's  applications. For example, \word{bool} is suitable for many hardware
applications using two-value logic. 

Dependent types are simulated using restricted universal
quantifications. A restricted universal quantification is written in the form
\[
\forall x ::P.\, t[x]
\]
where if $x:\alpha$ then $P$ can be any term of type $\alpha\rightarrow
\mbox{\sl bool}$; this
denotes the quantification of $x$ over those values satisfying $P$.
The semantics of this quantification is defined by the following
equation:
\begin{equation}
\DEF \forall x ::P.\, t[x] = \forall x. P\,x \IMP t[x]
\end{equation}
Suppose that $P$ is a predicate $\CONST{PWORDLEN}\,n$ which returns
\CONST{T} when applied to a word $w$ if and only if $w$ is an $n$-bit
word, then the expression
\[
\FORALL w \RESDOT \CONST{PWORDLEN}\,n\DOT \ldots
\]
can be read as `{\it for all $n$-bit words $w$, $\ldots$}'. For
a specific value of $n$, say 8, one can define a predicate
\CONST{word8} by the definition
\[
\DEF \CONST{word8} = \CONST{PWORDLEN}\,8.
\]
This predicate can then be used in expressions, such as
$\FORALL w \RESDOT \CONST{word8}\DOT \ldots$
Since the syntax of restricted quantification resembles the syntax of
types closely and the semantics of the quantification is suitably
defined,  using this to simulate dependent types is very comprehensible.

As we cannot define a real abstract type in \HOL, the list type is used as the
underlying representation of the polymorphic type \word{*}. However,
through disciplined use of system functions and properties derived for
the new type, direct reference to the underlying
representation is minimized. For example, when defining new constants,
constant specification is used to specify the abstract properties of the
new constant instead of using constant definition which needs access
to the representation. In the development of the library, the proofs
of some basic theorems about words have to refer to the underlying
lists. After a small number of basic theorems are derived, one can
proceed to reason about words on a more abstract level without
resorting to the underlying representation. 

\section{The library}

\begin{figure}
\begin{center}
\input{thy_hier}
\end{center}
\caption{The ancestry of theories\label{fig-thy_hier}}
\end{figure}
The {\tt word} library consists of several theories and some ML
functions implementing tactics and conversions which manipulate words.
The ancestry of the theories is illustrated in
Figure~\ref{fig-thy_hier}. The theories whose names begin with {\tt
word\_} contains definitions of generic constants and theorems
asserting  general properties of words. These generic constants are
polymorphic and can be applied to words of any types.
There are three such theories in the library, namely {\tt word\_base},
{\tt word\_bitop} and {\tt word\_num}.
As boolean words are used most often, the theories whose names begin
with {\tt bword\_} are about this type of words. The subsections below
describe individual theories in more detail.

\subsection{The basics: the theory {\tt word\_base}}

First of all, the polymorphic type \word{*} is defined in this theory.
It is defined using \mlname{define_type} with the following
specification:
\begin{verbatim}
    `word = WORD (*)list`
\end{verbatim}
The basic constants denoting the functions of indexing, segmenting and
concatenation of words described in Section~\ref{sec-abs} are
\CONST{BIT}, \CONST{WSEG} and \CONST{WCAT},
respectively. The predicate \CONST{PWORDLEN} for discriminating the
size of words and a function named \CONST{WORDLEN} returning the size
of a word are also defined in this theory. The types and specifications
of these constants are listed in Table~\ref{tab-base-const}. 
Several constants denoting some simple functions on words
are also defined for convenient, such as \CONST{MSB} for most significant
bit. These are listed in Table~\ref{tab-other-const}.
\begin{table}
\begin{center}
\begin{constants}
\item[PWORDLEN]\verb":num -> ((*)word -> bool)" \newline
        $\CONST{PWORDLEN} \,n \,w = \CONST{T}$ iff $w$ is an $n$-bit word 
\item[WORDLEN]\verb":(*)word -> num"\newline
        $\CONST{WORDLEN} \,w = n$ 
\item[BIT]\verb":num -> ((*)word -> *)"\newline
        $\CONST{BIT}\,i\, \nword{a_{n-1} \ldots a_i \ldots a_0} = a_i$ 
\item[WSEG]\verb":num -> (num -> ((*)word -> (*)word))" \newline
        $\CONST{WSEG}\,m\, k\,\nword{a_{n-1}\ldots a_{k+m-1}\ldots a_k\ldots a_0} = 
         \nword{a_{k + m -1} \ldots a_k} $ 
\item[WCAT]\verb":(*)word # (*)word -> (*)word" \newline
        $\CONST{WCAT}(\nword{a_{n-1}\ldots a_0}, \nword{b_{m-1}\ldots b_0}) =
         \nword{a_{n-1}\ldots a_0 b_{m-1}\ldots b_0} $
\end{constants}
\end{center}
\caption{Basic constants in the theory {\tt word\_base}\label{tab-base-const}}
\end{table}
\begin{table}
\begin{center}
\begin{constants}
\item[LSB] \verb":(*)word -> *" \newline
        $\THM \FORALL n\DOT
        \FORALL w::\CONST{PWORDLEN} \,n\DOT
                 \CONST{0} \LES  n \IMP 
                    (\CONST{LSB} \,w = \CONST{BIT} \,\CONST{0} \,w)$
\item[MSB] \verb":(*)word -> *" 
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT
        \FORALL w::\CONST{PWORDLEN} \,n\DOT}\\
 & &             \CONST{0} \LES  n \IMP 
                    (\CONST{MSB} \,w =
                        \CONST{BIT} \,(\CONST{PRE} \,n) \,w)
\end{eqnarray*}
\item[WSPLIT] \verb":num -> ((*)word -> (*)word # (*)word)" 
\begin{eqnarray*}
\lefteqn{\THM (\FORALL n\DOT
         \FORALL w::\CONST{PWORDLEN} \,n.}\\
 & & {}\FORALL m\DOT m \LEE  n \IMP 
       (\CONST{WCAT} \,(\CONST{WSPLIT} \,m \,w) = w)) \AND \\
 & &     (\FORALL n \,m\DOT
         \FORALL w_1 :: \CONST{PWORDLEN} \,n\DOT
         \FORALL 2_2 :: \CONST{PWORDLEN} \,m\DOT \\
 & & \quad \CONST{WSPLIT} \,m \,(\CONST{WCAT} \,(w_{1},w_{2})) = w_{1},w_{2}))
\end{eqnarray*}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT
        \FORALL w ::\CONST{PWORDLEN} \,n\DOT}\\
 & & {}\FORALL k\DOT k \LEE  n \IMP 
                        (\CONST{WSPLIT} \,k \,w =
                            \CONST{WSEG} \,(n - k) \,k \,w,
                            \CONST{WSEG} \,k \,\CONST{0} \,w)
\end{eqnarray*}
\end{constants}
\end{center}
\caption{Other constants in the theory {\tt word\_base}\label{tab-other-const}}
\end{table}

A number of theorems stating the properties of the basic constants are
stored in this theory. Some of the more important ones are discussed below.
The theorem \mlname{WSEG_PWORDLEN} states that the size of the
word resulting from taking an $m$-bit segment from an $n$-bit word is $m$
providing that $k + m \leq n$ where $k$ is the starting bit.
\begin{holthm}{WSEG_PWORDLEN}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT \FORALL w ::\CONST{PWORDLEN} \,n\DOT} \\
 & & {}\FORALL m \,k\DOT m + k \LEE  n \IMP 
           \CONST{PWORDLEN} \,m \,(\CONST{WSEG} \,m \,k \,w)
\end{eqnarray*}
\end{holthm}
A nested \CONST{WSEG} expression can be simplified providing that the
sizes and starting bits satisfy certain conditions. This is asserted
by the theorem \mlname{WSEG_WSEG}.
\begin{holthm}{WSEG_WSEG}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT \FORALL w ::\CONST{PWORDLEN} \,n\DOT} \\
 & & {}\FORALL m_{1} \,k_{1} \,m_{2} \,k_{2}\DOT
                     m_{1} + k_{1} \LEE  n \AND 
                     m_{2} + k_{2} \LEE  m_{1} \IMP  \\
 & & \quad (\CONST{WSEG} \,m_{2} \,k_{2}
                            \,(\CONST{WSEG} \,m_{1} \,k_{1} \,w) =
                            \CONST{WSEG} \,m_{2} \,(k_{1} + k_{2})
                               \,w)
\end{eqnarray*}
\end{holthm}
The theorem \mlname{WCAT_PWORDLEN} states that the size of the result
of the word concatenation operation is the sum of the sizes of its
operands.
\begin{holthm}{WCAT_PWORDLEN}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n_{1}\DOT \FORALL w_1 :: \CONST{PWORDLEN} \,n_{1}\DOT}\\
 & & \quad \FORALL n_{2}\DOT \FORALL w_2 :: \CONST{PWORDLEN} \,n_{2}\DOT \\
 & & \qquad \CONST{PWORDLEN} \,(n_{1} + n_{2})
                                 \,(\CONST{WCAT} \,(w_{1},w_{2}))
\end{eqnarray*}
\end{holthm}
The associativity of the \CONST{WCAT} operation  is asserted by the
theorem \mlname{WCAT_ASSOC}.
\begin{holthm}{WCAT_ASSOC}
\begin{eqnarray*}
\lefteqn{\THM \FORALL w_{1} \,w_{2} \,w_{3}\DOT} \\
 & &        \CONST{WCAT} \,(w_{1},\CONST{WCAT} \,(w_{2},w_{3})) =
           \CONST{WCAT} \,(\CONST{WCAT} \,(w_{1},w_{2}),w_{3})
\end{eqnarray*}
\end{holthm}
The theorem \mlname{WSEG_WCAT_WSEG} asserts that taking a segment from a
word which is built by concatenating two words $w_1$ and $w_2$ is
equivalent to taking the appropriate segments from each word and then
concatenating them provided that the segment spans across the boundary
of the two words.
\begin{holthm}{WSEG_WCAT_WSEG}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n_{1}\,\FORALL n_{2}\DOT 
        \FORALL w_1 :: \CONST{PWORDLEN} \,n_{1}\DOT
        \FORALL w_2 :: \CONST{PWORDLEN} \,n_{2}\DOT} \\
 & & {} \FORALL m \,k\DOT \\
 & & \quad m + k \LEE  n_{1} + n_{2} \AND k \LES  n_{2} \AND 
                                  n_{2} \LEE  m + k \IMP \\
 & & \qquad (\CONST{WSEG} \,m \,k \,(\CONST{WCAT} \,(w_{1}, w_{2})) = \\
 & & \qquad \quad \CONST{WCAT} \,
        (\CONST{WSEG} \,((m + k) - n_{2}) \,\CONST{0} \,w_{1},
         \CONST{WSEG} \,(n_{2} - k) \,k \,w_{2}))
\end{eqnarray*}
\end{holthm}

\subsection{Generic bitwise operators: the theory {\tt word\_bitop}}

Definitions in this theory include predicates for bitwise operators,
predicates on properties of bits and shift operators.

Two predicates, \CONST{PBITOP} and \CONST{PBITBOP} are defined for
quantifying bitwise operators.
When applied to a suitably typed word function $op$, they will return
\CONST{T} if and only if $op$ is a bitwise unary or binary operator,
respectively. The meaning of {\it bitwise\/} is that the operator
preserves the size and the operation on each bit is independent of
other bits. Note that as these predicates are polymorphic
the type of the bits can be anything. The exact definitions of these
predicates are as follows:  
\begin{constants}
\item[PBITOP]\verb":((*)word -> (**)word) -> bool" \newline
        $\CONST{PBITOP} \,op = \CONST{T}$ iff $op$ is a bitwise unary operator
\begin{eqnarray*}
\lefteqn{\DEF \FORALL op\DOT \CONST{PBITOP} \,op =} \\
 & & {} (\FORALL n\DOT \FORALL w :: \CONST{PWORDLEN} \,n\DOT \\
 & & \quad \CONST{PWORDLEN} \,n \,(op \,w) \AND \\
 & & \quad (\FORALL m \,k\DOT m + k \LEE  n \IMP  
           (op \,(\CONST{WSEG} \,m \,k \,w) = \CONST{WSEG} \,m \,k \,(op \,w)))
\end{eqnarray*}
\item[PBITBOP]\verb":((*)word -> (**)word -> (***)word) -> bool" \newline
        $\CONST{PBITBOP} \,op = \CONST{T}$ iff $op$ is a bitwise binary operator
\begin{eqnarray*}
\lefteqn{\DEF \FORALL op\DOT \CONST{PBITBOP} \,op =} \\
 & & {} (\FORALL n\DOT \FORALL w_1 :: \CONST{PWORDLEN} \,n\DOT
         \FORALL w_2 \CONST{PWORDLEN} \,n\DOT \\
 & & \quad \CONST{PWORDLEN} \,n \,(op \,w_{1} \,w_{2}) \AND \\
 & & \quad (\FORALL m \,k\DOT m + k \LEE  n \IMP \\
 & & \qquad (op \,(\CONST{WSEG} \,m \,k \,w_{1}) \,(\CONST{WSEG} \,m \,k \,w_{2}) =
        \CONST{WSEG} \,m \,k \,(op \,w_{1} \,w_{2})))
\end{eqnarray*}
\end{constants}

Two higher-order functions, \CONST{FORALLBITS} and \CONST{EXISTSABIT},
are defined for testing whether the bits of a word have certain
properties. The term $\CONST{FORALLBITS}\, P\,w$ evaluates to
\CONST{T} if and only if all the bits in the word $w$ satisfy the
predicate $P$. The term $\CONST{EXISTSABIT}\, P\,w$ evaluates to
\CONST{T} if and only if there exists one or more bits in the word $w$
satisfying the
predicate $P$. The higher-order function \CONST{WMAP} defined in
this theory is analogous to the function \CONST{MAP} on lists. The
meaning of the expression $\CONST{WMAP}\, f\,w$ is to apply the
function $f$ to each bit of the word $w$.

Also in this theory are the definitions of two generic shift operators:
\CONST{SHL} and \CONST{SHR}. Their types and specification is listed
in Table~\ref{tab-shift}.
\begin{table}
\begin{center}
\begin{constants}
\item[SHR]\verb":bool -> * -> (*)word -> ((*)word # *)"\newline
\[
\CONST{SHR}\, f\,b\,\nword{a_{n-1}\ldots a_1 a_0} = 
  \left\{ \begin{array}{ll}
        (\nword{a_{n-1} a_{n-1}\ldots a_1}, a_0) & \mbox{if $f = \CONST{T}$} \\
        (\nword{b\,a_{n-1}\ldots a_1}, a_0) & \mbox{if $f = \CONST{F}$} \\
        \end{array}
        \right.
\]
\item[SHL]\verb":bool -> (*)word -> * -> (* # (*)word)"\newline
\[
\CONST{SHL}\, f\,\nword{a_{n-1}a_{n-2}\ldots a_0} \,b = 
  \left\{ \begin{array}{ll}
        (a_{n-1}, \nword{a_{n-2}\ldots a_0 a_0}) & \mbox{if $f = \CONST{T}$} \\
        (a_{n-1}, \nword{a_{n-2}\ldots a_0\,b}) & \mbox{if $f = \CONST{F}$} \\
        \end{array}
        \right.
\]
\end{constants}
\end{center}
\caption{Shift operators\label{tab-shift}}
\end{table}
Both take three arguments and return a
pair. The first argument is a boolean value indicating the kind of
operation to be performed. The second and the third arguments to
\CONST{SHR} are a single bit and a word, respectively. The order of
these two arguments to \CONST{SHL} is reversed. Depending on the value
of the boolean and single bit argument, these operators can perform
either a logical shift, an arithmetic shift or a rotation operation.
If the boolean argument is \CONST{T}, the single bit argument is not
used. \CONST{SHR} shifts its operand one bit to the right and the
left-most bit is duplicated to fill the vacant position, thus,
implementing an arithmetic shift. If the boolean argument is
\CONST{F}, \CONST{SHR} fills the vacant position with the single bit
argument. If this bit is the right-most bit of the operand, a
rotation is performed. If it has value 0, it results in a logical
shift. The operation performed by \CONST{SHL} is similar. The pair
returned by these operators consists of a word which 
is the operation result and a single bit which is the bit shifted out
of the operand.

A number of theorems asserting the operational behaviour of
these operators and their relationship with the basic constants
\CONST{WCAT} and \CONST{WSEG} are stored in this theory. The theorems
\mlname{SHR_WSEG} and \mlname{SHL_WSEG} state the equivalence between a
shift expression and a combination of \CONST{WCAT} and \CONST{WSEG}.
Thus, an expression involving shift operators can be simplified to one
which only involves the basic word operations.
\begin{holthm}{SHR_WSEG}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT \FORALL w ::\CONST{PWORDLEN} \,n\DOT} \\
 & & {}\FORALL m \,k\DOT
                     m + k \LEE  n \IMP \CONST{0} \LES  m \IMP \\
 & & \quad (\FORALL f \,b \DOT\CONST{SHR} \,f \,b \,(\CONST{WSEG} \,m \,k \,w) =\\
 & & \qquad (f \Rightarrow 
        \CONST{WCAT} \,(\CONST{WSEG} \,\CONST{1} \,(k + (m - \CONST{1})) \,w,
                        \CONST{WSEG} \,(m - \CONST{1}) \,(k + \CONST{1}) \,w) \mid \\
 & & \qquad \qquad \CONST{WCAT} \,(\CONST{WORD} \,[b], \CONST{WSEG} \,(m - \CONST{1}) \,(k + \CONST{1}) \,w)),\\
 & & \qquad \CONST{BIT} \,k \,w)
\end{eqnarray*}
\end{holthm}
\begin{holthm}{SHL_WSEG}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT \FORALL w :: \CONST{PWORDLEN} \,n\DOT}\\
 & & {}\FORALL m \,k\DOT
        m + k \LEE  n \IMP \CONST{0} \LES  m \IMP \\
 & & \quad (\FORALL f \,b \DOT\CONST{SHL} \,f \,(\CONST{WSEG} \,m \,k \,w) \,b = \\
 & & \qquad \CONST{BIT} \,(k + (m - \CONST{1})) \,w,\\
 & & \qquad (f \Rightarrow 
        \CONST{WCAT} \,(\CONST{WSEG} \,(m - \CONST{1}) \,k \,w,
                        \CONST{WSEG} \,\CONST{1} \,k \,w) \mid \\
 & & \qquad \qquad \CONST{WCAT} \,(\CONST{WSEG} \,(m - \CONST{1}) \,k \,w,
                                       \CONST{WORD} \,[b])))
\end{eqnarray*}
\end{holthm}

\subsection{Boolean bitwise operators: the theory {\tt bword\_bitop}}

In this theory, a small set of boolean bitwise operators are defined
and theorems asserting that they are bitwise operators are proved.
The boolean bitwise operators are:
\begin{center}
\begin{tabular}{lll}
\CONST{WNOT}    & \verb":bool word -> bool word" & bitwise negation\\
\CONST{WAND}    & \verb":bool word -> bool word -> bool word" & bitwise AND \\
\CONST{WOR}     & \verb":bool word -> bool word -> bool word" & bitwise OR \\
\CONST{WXOR}    & \verb":bool word -> bool word -> bool word" & bitwise exclusive-OR
\end{tabular}
\end{center}
The theorems stating that they are bitwise are:
\begin{center}
\begin{tabular}{ll}
\mlname{PBITOP_WNOT} & $\THM \CONST{PBITOP}\;\CONST{WNOT}$ \\
\mlname{PBITBOP_WAND} & $\THM \CONST{PBITBOP}\;\CONST{WAND}$ \\
\mlname{PBITBOP_WOR}  & $\THM \CONST{PBITBOP}\;\CONST{WOR}$ \\
\mlname{PBITBOP_WXOR} & $\THM \CONST{PBITBOP}\;\CONST{WXOR}$
\end{tabular}
\end{center}

\subsection{Natural numbers and words: the theory {\tt word\_num}}
\label{sec-word_num}
Words are often interpreted as natural numbers. In this theory, two
constants are defined to map generic words to natural numbers and vice versa:
\begin{center}
\begin{constants}
\item[NVAL] \verb":(* -> num) -> num -> (*)word -> num" \newline
        $\CONST{NVAL}\;f\;b\;w$ returns the numeric value of $w$.
        $f$ is a function mapping a bit to its numeric value and
        $b$ is the base or  radix of the word.
\item[NWORD] \verb":num -> (num -> *) -> num -> num -> (*)word"\newline
        $\CONST{NWORD}\;n\;f'\;b\;m$ returns an $n$-bit word representing
        the value of $m$. $f'$ is a function mapping a number to a bit and
        $b$ is the base.
\end{constants}
\end{center}
The upper bound of the numeric value of a word is stated by the
theorem \mlname{NVAL_MAX}.
\begin{holthm}{NVAL_MAX}
\begin{eqnarray*}
\lefteqn{\THM \FORALL f \,b\DOT
        (\FORALL x\DOT f \,x \LES  b) \IMP } \\
 & & \FORALL n\DOT \FORALL w :: \CONST{PWORDLEN} \,n\DOT 
      \CONST{NVAL} \,f \,b \,w \LES ( b \:\CONST{EXP}\: n )
\end{eqnarray*}
\end{holthm}
Provided that the bit value function $f$ satisfies $\FORALL x\DOT f
\,x \LES  b$, the numeric value of a word $w$ is always less than
$b^n$. The theorem \mlname{NVAL_WCAT} states that the
value of a word can be calculated from the values of its segments.
\begin{holthm}{NVAL_WCAT}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\,m \DOT \FORALL w_1 :: \CONST{PWORDLEN} \,n\DOT} \\
 & & {} \FORALL w_2 :: \CONST{PWORDLEN} \,m\DOT \\
 & & \quad \FORALL f \,b\DOT\\
 & & \qquad \CONST{NVAL} \,f \,b \,(\CONST{WCAT} \,(w_{1},w_{2})) =\\
 & & \qquad \quad (\CONST{NVAL} \,f \,b \,w_{1} \MUL 
                                 ( b \:\CONST{EXP}\: m )) +
                                 (\CONST{NVAL} \,f \,b \,w_{2})
\end{eqnarray*}
\end{holthm}
The theorem stating the size of the result of mapping from natural
number to word is \mlname{NWORD_PWORDLEN}.
\begin{holthm}{NWORD_PWORDLEN}
\[\THM \FORALL n \,f \,b \,m\DOT
        \CONST{PWORDLEN}\,n \,(\CONST{NWORD} \,n \,f \,b \,m)
\]
\end{holthm}

\subsection{Boolean words and numbers: the theory {\tt bword\_num}}\label{sec-bword_num}

In this theory, two functions mapping between a single bit and number
are defined first. Then, the constants denoting the mapping between
boolean words and natural numbers are defined in terms of these bit
mapping functions and the generic word--num mapping functions
described in Section~\ref{sec-word_num}.
\begin{center}
\begin{constants}
\item[BV] \verb":bool -> num"\newline
        $\THM \FORALL b\DOT
        \CONST{BV} \,b =
           (b \Rightarrow  \CONST{SUC} \,\CONST{0} \mid  \CONST{0})$
\item[VB] \verb":num -> bool"\newline
        $\THM \FORALL n\DOT
         \CONST{VB}\,n = \NOT((n\, \CONST{MOD}\,2) = 0)$
\item[BNVAL] \verb":bool word -> num" \newline
        $\CONST{BNVAL}\;w$ returns the numeric value of $w$.
        $\DEF \CONST{BNVAL}\;w = \CONST{NVAL}\;\CONST{BV}\;2\;w$
\item[NBWORD] \verb":num -> num -> bool word"\newline
        $\CONST{NBWORD}\;n\;m$ returns a $n$-bit word representing
        the value of $m$.\linebreak
        $\DEF \CONST{NBWORD}\;n\;m = \CONST{NWORD}\;n\;\CONST{VB}\;2\;m$
\end{constants}
\end{center}
The functions \CONST{BNVAL} and \CONST{NBWORD} are inverse to each
other in the set of numbers less than $2^n$ where $n$ is the size of
the word. The following theorems state the basic properties of these
mapping functions.
\begin{holthm}{VB_BV}
\[ \THM \FORALL x\DOT \CONST{VB} \,(\CONST{BV} \,x) = x \]
\end{holthm}
\begin{holthm}{BV_VB}
\[ \THM \FORALL x\DOT
        x \LES  \CONST{2} \IMP  (\CONST{BV} \,(\CONST{VB} \,x) = x) \]
\end{holthm}
\begin{holthm}{NBWORD_BNVAL}
\[\THM \FORALL n\DOT
        \FORALL w :: \CONST{PWORDLEN} \,n \DOT
                 \CONST{NBWORD} \,n \,(\CONST{BNVAL} \,w) = w \]
\end{holthm}
\begin{holthm}{BNVAL_NBWORD}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n \,m\DOT} \\
 & & m \LES  ( \CONST{2} \:\CONST{EXP}\:n ) \IMP 
           (\CONST{BNVAL} \,(\CONST{NBWORD} \,n \,m) = m)
\end{eqnarray*}
\end{holthm}
\begin{holthm}{NBWORD_PWORDLEN}
\[\THM \FORALL n \,m\DOT \CONST{PWORDLEN}\,n \,(\CONST{NBWORD} \,n \,m)
\]
\end{holthm}
\begin{holthm}{NBWORD_MOD}
\[ \THM \FORALL n \,m\DOT
        \CONST{NBWORD} \,n
           \,(m \:\CONST{MOD}\: ( \CONST{2} \:\CONST{EXP}\: n )) =
           \CONST{NBWORD} \,n \,m \]
\end{holthm}
The theorem \mlname{NBWORD_SUC} asserts the fact that converting a
number $m$ to a word can be performed bit by bit recursively.
\begin{holthm}{NBWORD_SUC}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n \,m\DOT
        \CONST{NBWORD} \,(\CONST{SUC} \,n) \,m =} \\
 & & \CONST{WCAT}
              \,(\CONST{NBWORD} \,n
                    \,(m \:\CONST{DIV}\: \CONST{2}),
                 \CONST{WORD}
                    \,[\CONST{VB} \,(m \:\CONST{MOD}\: \CONST{2})])
\end{eqnarray*}
\end{holthm}
The theorem \mlname{WSEG_NBWORD} states that taking an $m$-bit segment
of an $n$-bit word mapped to by \CONST{NBWORD} from a number $l$ is
equivalent to mapping the quotient of $l$ divided by $2^k$ to an
$m$-bit word.
\begin{holthm}{WSEG_NBWORD}
\begin{eqnarray*}
\lefteqn{\THM \FORALL m \,k \,n\DOT
        m + k \LEE  n \IMP }\\
 & & (\FORALL l\DOT\CONST{WSEG} \,m \,k \,(\CONST{NBWORD} \,n \,l) =
               \CONST{NBWORD} \,m
                  \,(l \:\CONST{DIV}\: ( \CONST{2} \:\CONST{EXP}\: k )))
\end{eqnarray*}
\end{holthm}

\subsection{Boolean word arithmetic: the theory {\tt bword\_arith}}

This theory is about addition of boolean words. Two methods of
computing the carry value of each bit are defined: \CONST{ACARRY} uses
addition and \CONST{ICARRY} uses logical operations $\AND$ and $\OR$.
The theorem asserting the equivalence of these methods is
\mlname{ACARRY_EQ_ICARRY}. 

The theorem \mlname{ADD_WORD_SPLIT} states that addition of two words
can be carried out in segments.
\begin{holthm}{ADD_WORD_SPLIT}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n_{1} \,n_{2}\DOT
        \FORALL w_{1} \,w_{2}\RESDOT \CONST{PWORDLEN}
                                        \,(n_{1} + n_{2})\DOT
           \FORALL cin\DOT} \\
 & & \CONST{NBWORD} \,(n_{1} + n_{2})
                 \,(\CONST{BNVAL} \,w_{1} +
                    \CONST{BNVAL} \,w_{2} +
                    \CONST{BV} \,cin) = \\
 & & \CONST{WCAT} \,(\CONST{NBWORD} \,n_{1}
         \,(\CONST{BNVAL} \,(\CONST{WSEG} \,n_{1} \,n_{2} \,w_{1}) + 
           \CONST{BNVAL} \,(\CONST{WSEG} \,n_{1} \,n_{2} \,w_{2}) + \\
 & & \qquad \CONST{BV} \,(\CONST{ACARRY} \,n_{2} \,w_{1} \,w_{2} \,cin)),\\
 & & \quad \CONST{NBWORD} \,n_{2} 
         \,(\CONST{BNVAL} \,(\CONST{WSEG} \,n_{2} \,\CONST{0} \,w_{1}) +
            \CONST{BNVAL} \,(\CONST{WSEG} \,n_{2} \,\CONST{0} \,w_{2}) +\\
 & & \qquad \CONST{BV} \,cin))
\end{eqnarray*}
\end{holthm}
The theorem \mlname{WSEG_NBWORD_ADD} asserts that taking a segment of
the sum of two words is equal to taking the corresponding segments of
the words then summing them up.
\begin{holthm}{WSEG_NBWORD_ADD}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n\DOT
        \FORALL w_{1} \,w_{2}\RESDOT \CONST{PWORDLEN} \,n\DOT
           \FORALL m \,k \,cin\DOT
              m + k \LEE  n \IMP } \\
 & & (\CONST{WSEG} \,m \,k
      \,(\CONST{NBWORD} \,n \,
        (\CONST{BNVAL} \,w_{1} + \CONST{BNVAL} \,w_{2} + \CONST{BV} \,cin)) =\\
 & & \CONST{NBWORD} \,m \,(\CONST{BNVAL} \,(\CONST{WSEG} \,m \,k \,w_{1}) +
                           \CONST{BNVAL} \,(\CONST{WSEG} \,m \,k \,w_{2}) +\\
 & & \qquad \CONST{BV} \,(\CONST{ACARRY} \,k \,w_{1} \,w_{2} \,cin)))
\end{eqnarray*}
\end{holthm}

\subsection{Proof tools}

The {\tt word} library currently has a small set of tools in the form
of conversions and tactics for manipulating words. These include the
following:
\begin{description}
\item[{\tt BIT\_CONV}] \verb|:conv| When applied to a term as the
left hand side of the following theorem, this conversion returns the theorem
\[
\THM \CONST{BIT}\,k\,(\CONST{WORD}[w_{n-1};\ldots;w_{k};\ldots;w_0]) = w_k
\]

\item[{\tt WSEG\_CONV}] \verb|:conv| When applied to a term as the
left hand side of the following theorem, this conversion returns the theorem
\[
\THM \CONST{WSEG}\,m\,k\,(\CONST{WORD}[w_{n-1};\ldots;w_{k};\ldots;w_0])
= [w_{m+k-1};\ldots; w_k]
\]

\item[{\tt WSEG\_WSEG\_CONV}] \verb|:(term -> conv)| When applied to a
term as the left hand side of the following theorem, the conversion 
{\tt \mlname{WSEG_WSEG_CONV} "n"} returns the theorem 
\[
\CONST{PWORDLEN}\,n\,w\:\THM
\CONST{WSEG}\,m_2\,k_2(\CONST{WSEG}\,m_1\,k_1 w) = \CONST{WSEG}\,m_2\,k\,w
\]
where $k = k_1 + k_2$ and $n$, $k_1$, $k_2$, $m_1$ and $m_2$ are
numeric constants and satisfy the following relations: $k_1 + m_1\leq
n$ and $k_2 + m_2 \leq m_1$.

\item[{\tt PWORDLEN\_CONV}] \verb|:(term list -> conv)| When applied
to the term $\CONST{PWORDLEN}\,m\, tm$, the
conversion {\tt \mlname{PWORDLEN_CONV} tms} returns a
theorem asserting the size of the word $tm$. This theorem is in the form
\[
A \THM \CONST{PWORDLEN}\,m\, tm = \CONST{T}
\]
where the exact form of $A$, $tm$ and the term list argument {\tt tms}
is given in the table below:
\begin{center}\small \font\sfc=cmssc12 at 10pt\def\constfont{\sfc}
\begin{tabular}{lll}
$tm$ & {\tt tms} & theorem\\ \hline
$\CONST{WORD}[ b_{n-1};\ldots;b_0]$ & \tt[ ] & 
$\THM \CONST{PWORDLEN}\,n\, (\CONST{WORD}[ b_{n-1};\ldots;b_0])$ \\
$\CONST{WSEG}\,m\,k\,tm'$ & \tt [ "n" ] &
$\CONST{PWORDLEN}\,n\,tm'$ \\
 & & $\quad\THM \CONST{PWORDLEN}\,m\,(\CONST{WSEG}\,m\,k\,tm')$ \\
$\CONST{WCAT}(tm',tm'')$ & \tt["n1";"n2"] & 
$\CONST{PWORDLEN}\,n_1\,tm',\CONST{PWORDLEN}\,n_2\,tm''$ \\
 & & $\quad\THM\CONST{PWORDLEN}\,n\,(\CONST{WCAT}(tm',tm''))$\\
 & &  where $ n = n1 + n2$ \\
$\CONST{WNOT}\,tm'$ & \tt[ ] & $\CONST{PWORDLEN}\,n\,tm'$ \\
 & & $\quad\THM\CONST{PWORDLEN}\,n\,(\CONST{WNOT}\,tm')$ \\
$\CONST{WAND}\,tm'\,tm''$ & \tt[ ] &
$\CONST{PWORDLEN}\,n\,tm',\CONST{PWORDLEN}\,n\,tm''$ \\
 & & $\quad \THM\CONST{PWORDLEN}\,n\,(\CONST{WAND}\,tm'\,tm'')$ \\
$\CONST{WOR}\,tm'\,tm''$ & \tt[ ] &
$\CONST{PWORDLEN}\,n\,tm',\CONST{PWORDLEN}\,n\,tm''$ \\
 & & $\quad\THM\CONST{PWORDLEN}\,n\,(\CONST{WOR}\,tm'\,tm'')$ \\
$\CONST{WXOR}\,tm'\,tm''$ & \tt[ ] &
$\CONST{PWORDLEN}\,n\,tm',\CONST{PWORDLEN}\,n\,tm''$ \\
 & & $\quad\THM\CONST{PWORDLEN}\,n\,(\CONST{WXOR}\,tm'\,tm'')$ \\
\end{tabular}
\end{center}

\item[{\tt PWORDLEN\_bitop\_CONV}] \verb|:conv|
When applied to a term $\CONST{PWORDLEN}\,n\,tm$ where $tm$ involves
only bitwise operators and variables, this conversion returns
the theorem 
\[
\ldots,\CONST{PWORDLEN}\,n\,w_i,\ldots \THM \CONST{PWORDLEN}\,n\,tm = \CONST{T}
\]
where there is one assumption $\CONST{PWORDLEN}\,n\,w_i$ for each
simple variable $w_i$ in $tm$. This conversion automatically
descends into the subterms until it reaches all variables.

\item[{\tt PWORDLEN\_TAC}] \verb|:(term list -> tactic)|
When applied to a goal of the form {\tt\CONST{PWORDLEN} n tm}, the
tactic {\tt\mlname{PWORDLEN_TAC} tms} solves it if the conversion
{\tt\mlname{PWORDLEN_CONV} tms} returns a theorem without assumptions.
Otherwise, the assumptions of the theorem returned by the conversion
become the new subgoals.
\end{description}

\section{Working with words}

The basic technique for reasoning about words with the {\tt word}
library is by structural induction on the size of the word.
Since the structure of words is linear and symmetric, structural
induction can be carried out from either end using the \CONST{WCAT}
operation as the basic constructor. In addition, structural analysis
can be done at any position of a word. In general, there are three
theorems associated with each basic word function: one for each kind
of structural analysis. Considering the function \CONST{NBWORD} as an
example, the theorem \mlname{NBWORD_SUC} 
described in Section~\ref{sec-bword_num} is for structural induction
from the right hand end. The theorem \mlname{NBWORD_SUC_LEFT} shown
below is for structural induction from the left hand end, and the
theorem \mlname{NBWORD_SPLIT} is for structural analysis at any
position.
\begin{holthm}{NBWORD_SUC_LEFT}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n \,m\DOT
        \CONST{NBWORD} \,(\CONST{SUC} \,n) \,m =}\\
 & & \CONST{WCAT}
              \,(\CONST{WORD}
                    \,[\CONST{VB}
                          \,((m \:\CONST{DIV}\:
                                 ( \CONST{2}
                                   )\:\CONST{EXP}\:( n )) \:\CONST{MOD}\:
                                \CONST{2})],
                 \CONST{NBWORD} \,n \,m)
\end{eqnarray*}
\end{holthm}
\begin{holthm}{NBWORD_SPLIT}
\begin{eqnarray*}
\lefteqn{\THM \FORALL n_{1} \,n_{2} \,m\DOT
        \CONST{NBWORD} \,(n_{1} + n_{2}) \,m =} \\
 & & \CONST{WCAT}
              \,(\CONST{NBWORD} \,n_{1}
                    \,(m \:\CONST{DIV}\:
                          ( \CONST{2} )\:\CONST{EXP}\:( n_{2} )),
                 \CONST{NBWORD} \,n_{2} \,m)
\end{eqnarray*}
\end{holthm}

The following example uses structural induction from the right hand
end to prove a theorem about taking an $n$-bit segment of an
$(n + 1)$-bit word which is the result of converting a natural number
using the function \CONST{NBWORD}. We first set up the goal
\[
\GOAL \FORALL n\,m\DOT \CONST{WSEG}\,n\,\CONST{0}(\CONST{NBWORD}(\CONST{SUC}\,n)\,m)
= \CONST{NBWORD}\,n\,m.
\]
Then, the induction tactic \mlname{INDUCT_TAC} is applied to the size
of the word. This generates two subgoals. The first subgoal,
corresponding to the base case of the induction, is
\[
\GOAL \CONST{WSEG}\,\CONST{0}\,\CONST{0}(\CONST{NBWORD}(\CONST{SUC}\,\CONST{0})\,m)
= \CONST{NBWORD}\,\CONST{0}\,m.
\]
This is trivial to solve since a zero-bit segment of a word is
$\CONST{WORD}[\,]$ and converting a number to a zero-bit word always
gives the same result.
The second subgoal corresponding to the step case of the induction is
\[
\GOAL \FORALL m\DOT
    \CONST{WSEG} \,(\CONST{SUC} \,n) \,\CONST{0}
       \,(\CONST{NBWORD} \,(\CONST{SUC} \,(\CONST{SUC} \,n)) \,m) =
       \CONST{NBWORD} \,(\CONST{SUC} \,n) \,m
\]
The right hand end induction theorem for \CONST{NBWORD},
\mlname{NBWORD_SUC}, can now be used to rewrite the goal. 
Rewriting the resulting goal further with the theorem
\mlname{WSEG_WCAT_WSEG} and simplifying the result reduces it to
\[
\GOAL\CONST{WSEG} \,n \,\CONST{0} \,(\CONST{NBWORD} \,(\CONST{SUC} \,n)
                                   \,(m \:\CONST{DIV}\: \CONST{2})) =
    \CONST{NBWORD} \,n \,(m \:\CONST{DIV}\: \CONST{2}).
\]
The induction hypothesis can then be used to solve the goal. However,
as the theorem \mlname{WSEG_WCAT_WSEG} is restricted universally
quantified, ordinary rewriting tactics, such as \mlname{REWRITE_TAC},
cannot use it to rewrite the goal. Special tactics are required. The
{\tt res\_quan} library provides the basic facilities for manipulating
restricted quantifications\cite{WW:res_quan-manual}.
The complete proof is listed in Figure~\ref{fig-WSEG_NBWORD_SUC}.
\begin{figure}
\begin{center}\small
\makeatletter
\def\verbatim@font{\small\tt}
\makeatother
\begin{verbatim}
let WSEG_NBWORD_SUC = PROVE(
    "!n m. (WSEG n 0(NBWORD (SUC n) m) = NBWORD n m)",
    INDUCT_TAC THENL[
     REWRITE_TAC[NBWORD0;WSEG0];
     GEN_TAC THEN PURE_ONCE_REWRITE_TAC[NBWORD_SUC]
     THEN RESQ_REWRITE1_TAC (SPECL["SUC n"; "1"] WSEG_WCAT_WSEG) THENL[
      MATCH_ACCEPT_TAC PWORDLEN_NBWORD;
      MATCH_ACCEPT_TAC PWORDLEN1;
      PURE_ONCE_REWRITE_TAC[GSYM ADD1] THEN PURE_ONCE_REWRITE_TAC[ADD_0]
      THEN MATCH_ACCEPT_TAC LESS_EQ_SUC_REFL;
      CONV_TAC (RAND_CONV num_CONV) THEN MATCH_ACCEPT_TAC LESS_0;
      CONV_TAC ((RATOR_CONV o RAND_CONV) num_CONV)
      THEN PURE_REWRITE_TAC[ADD_0;LESS_EQ_MONO]
      THEN MATCH_ACCEPT_TAC ZERO_LESS_EQ;
      PURE_REWRITE_TAC[SUB_0;ADD_0;SUC_SUB1]
      THEN PURE_ONCE_ASM_REWRITE_TAC[]
      THEN RESQ_REWRITE1_TAC (SPEC "1" WSEG_WORD_LENGTH)
      THEN REFL_TAC]]);;
\end{verbatim}
\end{center}
\caption{A proof of the theorem {\tt WSEG\_NBWORD\_SUC}\label{fig-WSEG_NBWORD_SUC}}
\end{figure}
% Local Variables: 
% mode: latex
% TeX-master: t
% End: 
