\chapter{The reduce Library}

This manual describes the use of the \ml{reduce} library, as well as discussing
its design. The library is intended to ease the burden of performing tedious
and intellectually trivial tasks of arithmetic such as proving:

\begin{hol}\begin{verbatim}
  |- 2 EXP 6 = 64
\end{verbatim}\end{hol}

\noindent Anyone trying to prove the above by hand will testify to its extreme
tediousness. However, using the \ml{reduce} library, the evaluation of:

\begin{hol}\begin{verbatim}
  REDUCE_CONV "2 EXP 6"
\end{verbatim}\end{hol}

\noindent will perform the above automatically, and probably in far fewer
primitive inferences than a human would take. The library will also take care
of certain boolean expressions. This is mainly for the sake of completeness,
since the same effect can be achieved by careful use of rewriting.


\section{The representation of numbers}

The approach to representing natural number constants in \HOL\ is to provide a
conversion \ml{num\_CONV} which generates the definition of any nonzero numeral
in terms of its predecessor, for example:

\begin{hol}\begin{verbatim}
   #num_CONV "1";;
   |- 1 = SUC 0

   #num_CONV "256";;
   |- 256 = SUC 255
\end{verbatim}\end{hol}

\noindent This conversion uses \ml{mk\_thm}, so could be regarded as
unsatisfactory; however it is arguably no worse than expanding a constant
defined through the normal constant definition mechanism.

The \ml{reduce} library uses only the above conversion, together with certain
definitions and preproved theorems concerning the various arithmetic and
boolean operators, to derive, strictly by inference, reduction theorems for
certain expressions.


\section{Sample algorithm}

The following is an example of an algorithm for reducing addition expressions.
In fact, for reasons of efficiency, the library's \ml{ADD\_CONV} conversion is
implemented in a slightly more sophisticated way, but this example gives the
general flavour of how the various arithmetic conversions are defined.  Suppose
we want to apply the conversion to \ml{"m + n"} where \ml{m} and \ml{n} are
both numerals. Then

\begin{itemize}
  \item If the first numeral is zero, we need only specialize the first
  conjunct of the inbuilt definition \ml{ADD}:

  {\small\begin{verbatim}
    SPEC "n" (CONJUNCT1 ADD)
  \end{verbatim}}

  \item If the first numeral is not zero, then we call the conversion
  recursively to give:

  {\small\begin{verbatim}
    |- p + n = s
  \end{verbatim}}

  where \ml{p} is the predecessor of \ml{m}, and \ml{s} the corresponding sum.
  Now we can apply the following inferences:

  {\small\begin{verbatim}
    |- p + n = s                % [1] From recursive call             %
    |- (SUC p) + n = SUC(p + n) % [2] Instance of 2nd conjunct of ADD %
    |- (SUC p) + n = SUC s      % [3] Substituting [1] in [2]         %
    |- SUC p = m                % [4] SYM (num_CONV m)                %
    |- SUC s = t                % [5] SYM (num_CONV (s+1))            %
    |- m + n = t                % [6] Substituting [4] and [5] in [6] %
  \end{verbatim}}

  This gives the desired result. The above can easily be converted into a
  simple recursive procedure.

\end{itemize}

\section{Using the library}

The straightforward way to use the library is simply to do:

\begin{hol}\begin{verbatim}
   load_library `reduce`;;
\end{verbatim}\end{hol}

\noindent This will work only if the \HOL\/ being used has been installed
correctly; if this is not the case the user should refer to the \TUTORIAL\/ or
the \DESCRIPTION.

The library provides the following three ML bindings, which should be all that
is needed for most purposes:

\begin{itemize}

  \item \ml{REDUCE\_CONV} is a conversion which, when given an expression, will
  return a theorem expressing its equivalence with a reduced version, which in
  many cases will be simply a single numeral or boolean literal. For example:

  {\small\begin{verbatim}
    #REDUCE_CONV "(50 < 51) => (2 * 25) | (60 - 17)";;
    |- (50 < 51 => 2 * 25 | 60 - 17) = 50

    #REDUCE_CONV "(3 * x) + (1 + 2)";;
    |- (3 * x) + (1 + 2) = (3 * x) + 3

    #REDUCE_CONV "(1 < 2) /\ (2 <= 2)";;
    |- 1 < 2 /\ 2 <= 2 = T
  \end{verbatim}}

  \item \ml{REDUCE\_RULE} is a rule which applies the reductions corresponding
  to \ml{REDUCE\_CONV} to subterms of the theorem's conclusion

  \item \ml{REDUCE\_TAC} performs the same reductions on a goal.

\end{itemize}

\noindent For more sophisticated use, there are conversions specific to each
operator, for example \ml{ADD\_CONV} and \ml{OR\_CONV}. For more details of
these, refer to the next chapter. The arithmetic conversions and boolean
conversions may be loaded separately by loading (with \ml{loadt} for instance)
\ml{arithconv.ml} and \ml{boolconv.ml} respectively, in the main library
directory.
