1\chapter{Miscellaneous Features}
2
3This section describes some of the features
4that exist for managing the interface
5%
6\index{HOL system@\HOL{} system!adjustment of user interface of}
7%
8to the \HOL{} system.
9
10\begin{itemize}
11\item The help system.
12\item The trace system for controlling feedback and printing.
13\item \holmake{}: a tool for dependency maintenance in large developments.
14\item Functions for counting the number of primitive inferences done in
15an evaluation, and timing it.
16\item A tool for embedding pretty-printed HOL theorems, terms and
17  types in \LaTeX{} documents.
18\end{itemize}
19
20\section{Help}
21
22 There are several kinds of help available in \HOL{}, all accessible
23 through the same incantation:
24 \begin{verbatim}
25     help <string>;
26 \end{verbatim}
27
28 The kinds of help available are:
29
30 \begin{description}
31
32 \item [Moscow~ML help.] \index{Moscow ML} (When using Moscow~ML~\HOL{})
33   This is uniformly excellent.
34   Information for library routines is available, whether the library is loaded or not \emph{via} \texttt{help~"Lib"}.
35
36\item [\HOL{} overview.] This is a short summary of important information
37  about \HOL{}.
38
39\item [\HOL{} help.] This on-line help is intended to document all
40  HOL-specific functions available to the user. It is very detailed
41  and often accurate; however, it can be out-of-date, refer to earlier
42  versions of the system, or even be missing!
43
44\item [\HOL{} structure information.]
45  For most structures in the \HOL{} source, one can get a listing of the entrypoints found in the accompanying signature.
46  This is helpful for locating functions and is automatically derived from the system sources, so it is always up-to-date.
47
48\item [Theory facts.]
49  These are automatically derived from theory files, so they are always up-to-date.
50  The signature of each theory is available (since theories are represented by structures in \HOL{}).
51  Also, each axiom, definition, and theorem in the theory can be accessed by name in the help system.
52  As such theorems are pretty-printed into the corresponding \texttt{Theory.sig} file, the help system will find both the declaration in the signature (\eg, \ml{val~nm~:thm}), and the entry for that theorem in the comment-block.
53\end{description}
54
55Therefore the following example queries can be made:
56
57\begin{table}[h]
58  \begin{center}
59    \begin{tabular}{|l|l|} \hline
60      \verb+help "installPP"+ & Moscow ML help \\
61      \verb+help "hol"+ &  \HOL{} overview \\
62      \verb+help "aconv"+ &  on-line \HOL{} help \\
63      \verb+help "Tactic"+ & \HOL{} source structure information \\
64      \verb+help "boolTheory"+ &  theory structure signature \\
65      \verb+help "list_Axiom"+ & theory structure signature and theorem
66      statement \\ \hline
67    \end{tabular}
68  \end{center}
69\end{table}
70
71\section{The Trace System}
72\index{traces, controlling HOL feedback@traces, controlling \HOL{} feedback}
73\label{sec:traces}
74
75The trace system gives the user one central interface with which to control most of \HOL's many different flags, though they be scattered all over the system, and defined in different modules.
76These flags are typically those that determine the level to which \HOL{} tools provide information to the user while operating.
77For example, a trace level of zero will usually make a tool remain completely
78silent while it operates.
79The tool may still raise an exception when it fails, but it won't also
80output any messages saying so.
81
82There are three core functions, all in the \ml{Feedback} structure:
83\begin{hol}
84\begin{verbatim}
85   traces : unit ->
86            {default: int, max: int, name: string, trace_level: int} list
87
88   set_trace : string -> int -> unit
89   trace     : (string * int) -> ('a -> 'b) -> ('a -> 'b)
90\end{verbatim}
91\end{hol}
92
93The \ml{traces} function returns a list of all the traces in the system.
94The \ml{set\_trace} function allows the user to set a trace directly.
95The effect of this might be seen in a subsequent call to \ml{traces()}.
96Finally, the \ml{trace} function allows for a trace to be temporarily set while a function executes, restoring the trace to its old value when the function returns (whether normally, or with an exception).
97
98
99
100\section{\texorpdfstring{Maintaining \HOL{} Formalizations with \holmake}{Maintaining HOL Formalizations with \holmake}}
101\label{Holmake}
102\index{Holmake@\holmake|(}
103
104The purpose of \holmake{} is to maintain dependencies in a \HOL{}
105source directory. A single invocation of \holmake{} will compute
106dependencies between files, (re)compile plain ML code, (re)compile and
107execute theory scripts, and (re)compile the resulting theory modules.
108\holmake{} does not require the user to provide any explicit
109dependency information themselves. \holmake{} can be very convenient
110to use, but there are some conventions and restrictions on it that
111must be followed, described below.
112
113
114
115
116\holmake{} can be accessed through
117\begin{verbatim}
118   <hol-dir>/bin/Holmake.
119\end{verbatim}
120
121The development model that \holmake{} is designed to support is that
122there are two modes of work: theory construction and system revision.
123In `theory construction' mode, the user builds up a theory by
124interacting with HOL, perhaps over many sessions. In `system rebuild'
125mode, a component that others depend on has been altered, so all modules
126dependent on it have to be brought up to date. System rebuild mode is
127simpler so we deal with it first.
128
129\subsection{System rebuild}
130
131A system rebuild happens when an existing theory has been improved in
132some way (augmented with a new theorem, a change to a definition,
133etc.), or perhaps some support ML code has been modified or added to
134the formalization under development. The user needs to find and
135recompile just those modules affected by the change. This is what an
136invocation of \holmake{} does, by identifying the out-of-date modules
137and re-compiling and re-executing them.
138
139
140\subsection{Theory construction}
141
142To start a theory construction, some context (semantic, and also proof
143support) is established, typically by loading parent theories and
144useful libraries. In the course of building the theory, the user keeps
145track of the ML---which, for example, establishes context, makes
146definitions, builds and invokes tactics, and saves theorems---in a
147text file. This file is used to achieve inter-session persistence of
148the theory being constructed.  For example, the text file resulting
149from session $n$ is ``\verb+use+''-d to start session $n+1$; after
150that, theory construction resumes.
151
152Once the user finishes the perhaps long and arduous task of constructing
153a theory, the user should
154\begin{enumerate}
155\item make the script separately compilable;
156\item invoke \holmake{}. This will (a) compile and execute the
157  script file; and (b) compile the resulting theory file. After this,
158  the theory file is available for use.
159\end{enumerate}
160
161\subsection{Source conventions for script and SML files}
162
163\paragraph{Script and theory files}
164The file that generates the \HOL{} theory \textit{my}\texttt{Theory} must be called \textit{my}\texttt{Script.sml}.
165After the theory has been successfully generated, it can be \texttt{open}-ed at the head of other developments:
166\begin{alltt}
167    open myTheory
168\end{alltt}
169\index{load (ML function)@\ml{load} (\ML{} function)}
170and it can be loaded interactively:
171\begin{alltt}
172    load "myTheory";
173\end{alltt}
174The file \texttt{myScript.sml} should begin with the standard boilerplate:
175\begin{alltt}
176    open HolKernel Parse boolLib bossLib
177
178    val _ = new_theory "my"
179\end{alltt}
180This ``boilerplate'' ensures that the standard tactics and SML commands will be in the namespace when the script file is compiled.
181Interactively, these modules have already been loaded and \texttt{open}-ed, so what can be typed directly at \texttt{hol} cannot necessarily be included as-is in a script file.
182In addition, if \texttt{myTheory} depends on other \HOL{} theories, this ancestry should also be recorded in the script file.
183The easiest way to achieve this is simply to \texttt{open} the relevant theories.
184Conventionally, the \texttt{open} declarations for such theories appear just before the call to \texttt{new_theory}.
185For example:
186\begin{alltt}
187    open HolKernel Parse boolLib bossLib
188
189    open myfirstAncestorTheory OtherAncestorTheory
190
191    val _ = new_theory "my"
192\end{alltt}
193\index{load (ML function)@\ml{load} (\ML{} function)}
194Interactively, these may well be the names of theories that have been explicitly loaded into the context with the \texttt{load} function.
195In the interactive system, one has to explicitly \ml{load} modules; on the other hand, the batch compiler will load modules automatically.
196For example, in order to execute \ml{open Foo} (or refer to values in structure \ml{Foo}) in the interactive system, one must first have executed \ml{load "Foo"}.
197(This is on the assumption that structure \ml{Foo} is defined in a file \texttt{Foo.sml}.)
198Contrarily, the batch compiler will reject files having occurrences of \ml{load}, since \ml{load} is only defined for the interactive system.
199
200In addition, simply referring to a theory's theorems using the `dot-notation' will make that theory an ancestor.
201For example,
202\begin{alltt}
203   Theorem mytheorem:
204     ...
205   Proof
206     simp[ThirdAncestorTheory.important_lemma] ...
207   QED
208\end{alltt}
209will record a dependency on \texttt{ThirdAncestoryTheory}, making it just as much an ancestor as the theories that have been explicitly \texttt{open}-ed elsewhere.
210
211Finally, all script files should also end with the invocation:
212\begin{verbatim}
213    val _ = export_theory()
214\end{verbatim}
215When the script is finally executed, this call writes the theory to disk.
216
217The calls to \texttt{new_theory} and \texttt{export_theory} must bracket a sequence of SML declarations.
218A declaration will typically be a \texttt{val}-binding, but might also be a function definition (\emph{via} \texttt{fun}), an \texttt{open}, or even a \texttt{structure} declaration.
219Declarations are \emph{not} expressions.
220This means that script files should \emph{not} include bare calls to \HOL{} functions like \texttt{Datatype}.
221Instead, declarations such as the following need to be used:
222\begin{alltt}
223    val _ = Datatype`tree = Lf | Nd num tree tree`
224\end{alltt}
225This is because (due to restrictions imposed by Moscow~ML\index{Moscow ML}) the script file is required to be an ML structure, and the contents of a structure must be \emph{declarations}, not expressions.
226Indeed, one is allowed to (and generally should) omit the bracketing
227\begin{alltt}
228  structure myScript = struct
229  ...
230  end
231\end{alltt}
232lines, but the contents of the file are still interpreted as if belonging to a structure.
233
234Finally, take care not to have the string ``\texttt{Theory}'' appear at the end of the name of any of your files.
235\HOL{} generates files containing this string, and when it cleans up after itself, it removes such files using a regular expression.
236This will also remove other files with names containing ``\texttt{Theory.sml}'' or ``\texttt{Theory.sig}''.
237For example, if, in your development directory, you had a file of \ML{} code named \texttt{MyTheory.sml} and you were also managing a \HOL{} development there with \holmake, then \texttt{MyTheory.sml} would get deleted if \texttt{Holmake~clean} were invoked.
238
239\paragraph{Other SML code}
240When developing \HOL{} libraries, one should again attempt to follow Moscow~ML's conventions.
241Most importantly, file names should match \texttt{signature} and \texttt{structure} names.
242If this can be done, the automatic dependency analysis done by \holmake{} will work ``out of the box''.
243A signature for module \texttt{foo} should always appear in file \texttt{foo.sig}, and should have the form
244\begin{alltt}
245    signature foo =
246    sig
247      ...
248    end
249\end{alltt}
250The accompanying implementation of \texttt{foo} should appear in file \texttt{foo.sml}, and should have the form
251\begin{alltt}
252    structure foo :> foo =
253    struct
254    ...
255    end
256\end{alltt}
257As with theory files, the contents of a \texttt{structure} must be a sequence of declarations only.
258Neither sort of file should have any other declarations within it (before or after the \texttt{signature} or \texttt{structure}).
259
260Deviations from this general pattern are possible, but life is much simpler if such deviations can be avoided.
261The \HOL{} distribution\footnote{See, for example, the kernel implementation in \texttt{src/0}.} contains some examples of trickier situations where the guidelines need to be ignored.
262Ignoring the guidelines will generally result in the need for quite involved \texttt{Holmakefile}s (see Section~\ref{sec:using-Holmakefiles} below).
263
264\subsection{Summary}
265
266A complete theory construction might be performed by the following steps:
267\begin{itemize}
268\item Construct theory script, perhaps over many sessions;
269\item Transform script into separately compilable form;
270\item Invoke \holmake{} to generate the theory and compile it.
271\end{itemize}
272After that, the theory is usable as an ML module.
273This flow is demonstrated in the Euclid example of \TUTORIAL.
274
275Alternatively, and probably with the help of one of the editor modes,\footnote{There are editor modes for \texttt{emacs} and \texttt{vim}.} one can develop a theory with a script file that is always separately compilable.
276
277\subsection{\holmake{} Limitations}
278
279Without information to the contrary, \holmake{} assumes that all files of interest are in the current directory or in \HOL{}'s master \texttt{sigobj} directory.
280Using a \texttt{Holmakefile} (see Section~\ref{sec:using-Holmakefiles}), it is possible to mention files in other directories, both as dependencies and as explicit targets with rules on how those targets should be built.
281However, it is probably impossible (or at least, painfully difficult) to describe how theories are built from script files using the \texttt{Holmakefile} language.
282Nor will \holmake{} perform its automatic dependency analysis on such ``remote'' files.
283
284\index{Holmake@\holmake!recursive invocation}%
285The right approach when a development spans multiple directories is to indicate that there is a dependency on other directories by using the \texttt{-I} flag, or (better) the \texttt{INCLUDES} variable in a \texttt{Holmakefile}.
286\holmake{} requires that, just as files must have an acyclic dependency graph, the directories containing those files must have a compatible, and acyclic, \texttt{INCLUDES} graph.
287With \texttt{INCLUDES} information to hand, \holmake{} will process the entire \texttt{INCLUDES} graph, looking throughout the graph for theory files that the current directory may depend on, and rebuilding those remote targets as necessary.
288
289\subsection{\holmake{}'s command-line arguments}
290\index{Holmake@\holmake!command-line arguments}
291Like {\tt make}, \holmake{} takes command-line arguments corresponding to the targets that the user desires to build.
292As a special case of this, targets ending with the suffix \ml{Theory} are treated as an instruction to build the theory files that lie behind a \HOL{} theory.
293(If successful such a build will enable other script-files to refer to that theory, and for interactive sessions to issue \ml{load~"xTheory";} commands.)
294If there are no command-line targets, then \holmake{} will look for a \texttt{Holmakefile} in the current directory.
295If there is none, or if that file specifies no targets, then \holmake{} will attempt to build all \ML{} modules and \HOL{} theories it can detect in the current directory.
296If there is a target in the \texttt{Holmakefile}, then \holmake{} will try to build the first such target (only).
297
298In addition, there are three special targets that can be used:
299\begin{description}
300\item[{\tt clean}] Removes all compiled files (unless over-ridden by a
301  make-file target of the same name, see
302  section~\ref{sec:using-Holmakefiles} below).
303\item [{\tt cleanDeps}] Removes all of the pre-computed dependency
304  files.  This can be an important thing to do if, for example, you
305  have introduced a new {\tt .sig} file on top of an existing {\tt
306    .sml} file.
307\item [{\tt cleanAll}] Removes all compiled files as well as all of
308  the hidden dependency information.
309\end{description}
310
311\noindent Finally, users can directly affect the workings of \holmake{}
312with the following command-line options:
313\begin{description}
314\item[\texttt{-f <theory>}] Toggles whether or not a theory should be
315  built in ``fast'' mode.  Fast building causes tactic proofs
316  (invocations of \texttt{prove} and \texttt{store\_thm}) to
317  automatically succeed.  This lack of soundness is marked by the
318  \texttt{fast\_proof} oracle tag.  This tag will appear on all
319  theorems proved in this way and all subsequent theorems that depend
320  on such theorems.  \holmake's default is not to build in fast mode.
321\item[\texttt{--fast}] Makes \holmake's default be to build in fast
322  mode (see above).
323\item[{\tt --help} or {\tt -h}] Prints out a useful option summary and
324  exits.
325\item[\tt --holdir <directory>] Associate this build with the given
326  HOL directory, rather than the one this version of \holmake{} was
327  configured to use by default.
328\item[\tt --holmakefile <file>] Use the given file as a make-file.
329  See section~\ref{sec:using-Holmakefiles} below for more on this.
330\item[\tt -I <directory>]
331  Look in specified directory for additional object files, including other HOL theories.
332  This option can be repeated, with multiple {\tt -I}'s to allow for multiple directories to be referenced.
333  As above, directories specified in this way will also be rebuilt before the current targets are built.
334\item[\texttt{--interactive} or \texttt{-i}] Causes the HOL code that
335  runs when a theory building file is executed to have the flag
336  \texttt{Globals.interactive} set to true.  This will alter the diagnostic
337  output of a number of functions within the system.
338\item[\texttt{-k} or \texttt{--keep-going}] Causes \holmake{} to try
339  to build all specified targets, rather than stopping as soon as one
340  fails to build.
341\item[\texttt{--logging}] Causes \holmake{} to record the times taken
342  to build any theory files it encounters.  The times are logged in a
343  file in the current directory.  The name of this file includes the
344  time when \holmake{} completed, and when on a Unix system, the name
345  of the machine where the job was run.  If \holmake{} exits
346  unsuccessfully, the filename is preceded by the string
347  ``\texttt{bad-}''. Each line in the log-file is of the form
348  \textit{theory-name time-taken}, with the time recorded in seconds.
349\item[\texttt{--no\_holmakefile}]  Do not use a make-file, even if a file
350  called \texttt{Holmakefile} is present in the current directory.
351\item[\texttt{--no\_overlay}] Do not use an overlay file.  All HOL
352  builds require the presence of a special overlay file from the
353  kernel when compiling scripts and libraries.  This is not
354  appropriate for compiling code that has no connection to HOL, so
355  this option makes the compilation not use the overlay file.  This
356  option is also used in building the kernel before the overlay itself
357  has been compiled.
358\item[\texttt{--no\_prereqs}]%
359\index{Holmake@\holmake!recursive invocation}%
360Do not recursively attempt to build ``include'' directories before working in the current directory.
361If a target in the current directory depends on something in another directory that does not exist, \holmake{} will fail to build it.
362If the remote target exists, but is stale, it will be used in its stale state, come what may.
363\item[\texttt{--no\_sigobj}]
364  Do not link against \HOL{} system's directory of \HOL{} system files.
365  Use of this option goes some way towards turning \holmake{} into a general \ML{} \textsf{make} system.
366  However, it will still attempt to do ``\HOL{} things'' with files whose names end in \texttt{Script} and \texttt{Theory}.
367  This option implies \texttt{--no\_overlay}.
368\item[\texttt{--overlay <file>}] Use the given file as the overlay
369  rather than the default.
370\item[\texttt{--qof},\texttt{--noqof}]
371  Where q-o-f stands for ``quit on failure''.
372  By default, if a tactic fails to prove a theorem, the running script exits with a failure.
373  Depending on the presence or absence of the \texttt{-k} flag, this failure to build a theory may cause \holmake{} to also exit (with a failure).
374  With the \texttt{--noqof} option, \holmake{} will cause the running script to use \texttt{mk\_thm} to assert the failed goal, allowing the build to continue and other theorems to be proved.
375\item[\texttt{--quiet}] Minimise the amount of output produced by
376  \holmake{}.  Fatal error messages will still be written to the
377  standard error stream.  Note that other programs called by \holmake{} will not
378  be affected.
379\item[\texttt{-r}]%
380\index{Holmake@\holmake!recursive invocation}%
381Forces \holmake{} to behave more recursively than it would otherwise.
382This overrides the \texttt{--no\_prereqs} option.
383When performing a ``clean'' action (prompted by \texttt{clean}, \texttt{cleanAll} or \texttt{cleanDeps} arguments), this cleaning is done recursively through all ``includes'' directories (which is not done otherwise).
384When building normally, all targets in ``includes'' directories are built; normally only dependencies of targets in the current directory are built.
385\item[{\tt --rebuild\_deps}] Forces \holmake{} to always
386  rebuild the dependency information for files it examines, whether or
387  not it thinks it needs to.  This option is implemented by having
388  \holmake{} wipe all of its dependency cache (as per the
389  \texttt{cleanDeps} option above) before proceeding with the build.
390\end{description}
391
392\noindent \holmake{} should never exit with error messages such as ``Uncaught
393exception''.  Such behaviour is a bug, please report it!
394
395
396\subsection{Using a make-file with \holmake}
397\label{sec:using-Holmakefiles}
398
399\holmake{} will use a make-file to augment its behaviour if one is
400present in the current directory.  By default it will look for a file
401called \texttt{Holmakefile}, but it can be made to look at any file at
402all with the \texttt{--holmakefile} command-line option.  The
403combination of \holmake{} and a make-file is supposed to behave as
404much as possible like a standard implementation of \textsf{make}.
405
406A make-file consists of two types of entries, variable definitions and
407rules.  Outside of these entries, white-space is insignificant, but
408newline and \texttt{TAB} characters are very significant within them.
409Comments can be started with hash (\texttt{\#}) characters and last
410until the end of the line.  Quoting is generally done with use of the
411back-slash (\verb+\+) character.  In particular, a backslash-newline
412pair always allows a line to be continued as if the newline wasn't
413present at all.
414
415A variable definition is of the form
416\[
417\textsl{Ident} \texttt{ = } \textsl{text}  \texttt{ <NEWLINE> }
418\]
419and a rule is of the form
420\[
421\textsl{text } \texttt{:} \textsl{ text} \texttt{ <NEWLINE>}
422(\texttt{<TAB>}\textsl{text}\texttt{ <NEWLINE>})^*
423\]
424Henceforth, the text following a \texttt{TAB} character in a rule will
425be referred to as the \emph{command text}.  Text elsewhere will be
426referred to as \emph{normal text}.  Normal text has comments stripped
427from it, so hash characters there must be escaped with a back-slash
428character.  An \textsl{Ident} is any non-empty sequence of
429alpha-numeric characters, including the underscore (\texttt{\_}).
430
431In some contexts, normal text is interpreted as a list of words.
432These lists use white-space as element separators.  If a word needs to
433include white-space itself, those white-space characters should be
434escaped with back-slashes.
435
436\newcommand{\varref}[1]{\texttt{\$(#1)}}
437\paragraph{Variable definitions} The text on the RHS of a variable
438definition can be substituted into any other context by using a
439\emph{variable reference}, of the form \varref{VARNAME}.  References
440are evaluated \emph{late}, not at time of definition, so it is quite
441permissible to have forward references.  On the other hand, this makes
442it impossible to write things like \[ \texttt{VAR = \varref{VAR}
443something\_new}
444\] because the evaluation of \varref{VAR} would lead to an infinite
445loop. GNU \textsf{make}'s facility for immediate
446definition of variables with \texttt{:=} is not supported.
447
448Note also that white-space around the equals-sign in a variable
449definition is stripped.  This means that
450\[
451\texttt{VAR =<whitespace><NEWLINE>}
452\] gives \texttt{VAR} the empty string as its value.\footnote{It is
453  possible to give a variable a value of pure whitespace by writing \[
454\begin{array}{l}
455\texttt{NOTHING =}\\
456\texttt{ONE\_SPACE = \varref{NOTHING}\textvisiblespace\hspace{1mm}\varref{NOTHING}}\\
457\end{array}\]}
458
459Finally, note that the text inside a variable reference is itself
460evaluated.  This means that one can write something like
461\varref{FOO\_\varref{OS}} and have this first expand the \texttt{OS}
462variable, presumably giving rise to some useful string (such as
463\texttt{unix}), and then have the resulting variable
464(\texttt{FOO\_unix}, say) expanded.  This effectively allows the
465construction of functions by cases (define variables
466\texttt{FOO\_unix}, \texttt{FOO\_macos} etc.; then use the nested
467variable reference above).  If the internal variable expands to
468something containing spaces, this will not turn a normal variable
469reference into a function call (see below).  On the other hand,
470if the initial reference contains a space, the function name component
471\emph{will} be expanded, allowing implementation of a function by
472cases determining which text-manipulation function should be called.
473
474\paragraph{Rules}
475Make-file rules are interpreted in the same way as by traditional
476\textsf{make}.  The files specified after the colon (if any) are those
477files that each target (the files before the colon) is said to
478``depend'' on.  If any of these are newer than a target, then
479\holmake{} rebuilds that target according to the commands.  If there
480are no dependencies, then the commands are executed iff the
481target doesn't exist.  If there are no commands, and the target is not
482of a type that \holmake{} already knows how to build, then it will
483just make sure that the dependencies are up to date (this may or
484may not create the target).  If there are no commands attached to a
485rule, and the target is one that \holmake{} does know how to build,
486then the rule's extra dependencies are added to those that \holmake{}
487has managed to infer for itself, and \holmake{} will build the target
488using its built-in rule.  If commands are provided for a type of file
489that \holmake{} knows how to build itself, then the make-file's
490commands and dependencies take precedence, and only they will be
491executed.
492
493In addition, it is possible to indicate that the built-in process of generating theory files from script files generates side products.
494This is done by writing a command-less rule of the form
495\begin{alltt}
496   target : *thyScript.sml
497\end{alltt}
498where an asterisk character precedes the name of the script file.
499This indicates that the action of executing the code in \texttt{thyScript.sml} will not only generate the usual \texttt{thyTheory.sig} and \texttt{thyTheory.sml} files, but also the file \texttt{target}.
500If \holmake{} is asked to build any of these three files, and any is absent or out of date with respect to \texttt{thyScript.sml} (or any other dependency), then the code in \texttt{thyScript.sml} will be run.
501
502If a command-line is preceded by a hyphen (\verb!-!) character, then
503the rest of the line is executed, but its error-code is ignored.
504(Normally, a command-line raising an error will cause \holmake{} to
505conclude that the target can not be built.)  If a command-line is
506preceded by an at-sign (\verb!@!), then that command-line will not be
507echoed to the screen when it is run.  These two options can be
508combined in either order at the start of a command-line.
509
510Command text is interpreted only minimally by \holmake.  On Unix,
511back-slashes are not interpreted at all.  On Windows, back-slashes
512followed by newlines are turned into spaces. Otherwise, command text
513is passed as is to the underlying command interpreter
514(\texttt{/bin/sh} say, on Unix, or \texttt{COMMAND.COM} on Windows).
515In particular, this means that hash-characters do \emph{not} start
516comments on command-lines, and such ``comments'' will be passed to the
517shell, which may or may not treat them as comments when it sees them.
518
519\paragraph{Special targets}
520Some target names for rules are handled specially by \holmake{}:
521\begin{itemize}
522\item Dependencies associated with the target name \texttt{.PHONY} are taken to be list of other targets in the make-file that are not actually the name of files to be built.
523For example, targets naming conceptual collections of files such as \texttt{all} should be marked as ``phony''.
524If a target is phony, then its dependencies will be built even if a file of that name exists and is newer than the dependencies.
525\item The special way that command-line arguments \texttt{clean}, \texttt{cleanAll} and \texttt{cleanDeps} are handled means that targets of those names will not work.
526In order to extend cleaning behaviour, use the \texttt{EXTRA\_CLEANS} variable (see below).
527\end{itemize}
528\paragraph{Functions}
529\index{Holmake@\holmake!functions for text-manipulation}
530\holmake{} supports some simple functions for manipulating text.  All
531functions are written with the general form
532\texttt{\$(\textsl{function-name}\textvisiblespace{}\hspace{1mm}\textsl{arg}${}_1$,\textsl{arg}${}_2$\dots,\textsl{arg}${}_n$)}.
533Arguments can not include commas (use variable references to variables
534whose value are commas instead), but can otherwise be arbitrary text.
535\begin{description}
536\item[\texttt{\$(dprot arg)}] quotes (or ``protects'') the space
537  characters that occur in a string so that the string will be treated
538  as a unit if it occurs in a rule's dependency list.  For example,
539  the file
540\begin{verbatim}
541   dep = foo bar
542   target: $(dep)
543      do_something
544\end{verbatim}
545  will see \texttt{target} as having two dependencies, not one,
546  because spaces are used to delimit dependencies.  If a dependency's
547  name includes spaces, then this function can be used to quote them
548  for \holmake's benefit.  Note that the \texttt{dprot} function
549  does \emph{not} do the same thing as \texttt{protect} on either Unix
550  or Windows systems.
551\item[\texttt{\$(findstring arg1,arg2)}] checks if \texttt{arg1} occurs
552  in (is a sub-string of) \texttt{arg2}.  If it does so occur, the
553  result is \texttt{arg1}, otherwise the result is the empty string.
554\item[\texttt{\$(if arg1,arg2,arg3)}] examines \texttt{arg1}.  If it
555  is the empty string, then the value of the whole is equal to the
556  value of \texttt{arg3}.  Otherwise, the value is that of \texttt{arg2}.
557\item[\texttt{\$(patsubst arg1,arg2,text)}] splits \texttt{text} into component
558  words, and then transforms each word by attempting to see if it
559  matches the pattern in \texttt{arg1}.  If so, it replaces that word
560  with \texttt{arg2} (suitably instantiated).  If not, the word is
561  left alone.  The modified words are then reassembled into a
562  white-space separated list and returned as the value.
563
564  A pattern is any piece of text including no more than one occurrence
565  of the percent~(\texttt{\%}) character.  The percent character
566  matches any non-empty string.  All other characters must be matched
567  literally.  The instantiation for \texttt{\%} is remembered when the
568  replacement is constructed.  Thus, \[
569    \texttt{\$(patsubst \%.sml,\%.uo,\$(SMLFILES))}
570   \] turns a list of files with suffixes \texttt{.sml} into the same
571  list with the suffixes replaced with \texttt{.uo}.
572\item[\texttt{\$(protect arg)}] wraps \texttt{arg} in appropriate
573  quote characters to ensure that it will pass through the operating
574  system's command shell unscathed.  This is important in the presence
575  of file-names that include spaces or other shell-significant
576  characters like less-than and greater-than.  Those make-file
577  variables that point directly at executables (\texttt{MOSMLC},
578  \texttt{MOSMLLEX} etc.) are automatically protected in this way.
579  Others, which might be used in concatenation with other elements,
580  are not so protected.  Thus, if \texttt{DIR} might include spaces,
581  one should write
582\begin{verbatim}
583   $(protect $(DIR)/subdirectory/program)
584\end{verbatim}
585  so that the above will be read as one unit by the underlying shell.
586\item[\texttt{\$(subst arg1,arg2,text)}] replaces every occurrence
587  of \texttt{arg1} in \texttt{text} with \texttt{arg2}.
588\item[\texttt{\$(which arg)}] is replaced by the full path to an executable and readable occurrence of a file called \texttt{arg} within a directory in the list of directories in the \texttt{PATH} environment variable.
589For example \texttt{\$(which cat)} will usually expand to \texttt{/bin/cat} on Unix-like systems.
590If there is no occurrence of \texttt{arg} in any directory in \texttt{PATH}, this function call expands to the empty string.
591\item[\texttt{\$(wildcard pattern)}] expands the shell ``glob'' pattern (\eg, \texttt{*Script.sml}) into the list of matching filenames.
592If the pattern doesn't match any files, then the function returns \texttt{pattern} unchanged.
593\end{description}
594
595\paragraph{Special and pre-defined variables}
596
597\index{Holmake@\holmake!variables in makefiles}
598If defined, the \texttt{INCLUDES} variable is used to add directories to the list of directories consulted when files are compiled and linked.
599The effect is as if the directories specified had all been included on the command-line with \texttt{-I} options.
600The \texttt{PRE\_INCLUDES} variable works similarly, but the directories specified here are placed before the \mbox{\texttt{-I <sigobj>}} option that is used in invocations of compiler.
601This option gives the user a way of over-riding code in the core distribution as the compiler will find their code before the distribution's.
602
603\index{Holmake@\holmake!recursive invocation}
604By default, directories specified in the \texttt{INCLUDES} and \texttt{PRE\_INCLUDES} directory are also built by \holmake{} before it attempts to build in the current directory.
605If the \texttt{-r} (``force recursion'') command-line flag is used, these directories are also ``clean''-ed when a cleaning target is given to \holmake{}.
606
607\index{CLINE_OPTIONS (Holmakefile variable)@\texttt{CLINE\_OPTIONS} (Holmakefile variable)}%
608The \texttt{CLINE\_OPTIONS} variable is used for the specification of command-line switches that are presumably usually appropriate for calls to \holmake{} in the containing directory.
609The options present in \texttt{CLINE\_OPTIONS} are used to build a ``base environment'' of switches; this base environment is then overridden by whatever was actually passed on the command-line.
610For example, a useful \texttt{CLINE\_OPTIONS} line%
611\footnote{Note that a \texttt{-{}-noqof} option in a makefile might be overridden from the command-line with the otherwise useless seeming \texttt{-{}-qof} option.
612In addition, the \texttt{-{}-no\_hmakefile} command-line option will stop the makefile from being consulted at all.} %
613might be
614\begin{alltt}
615    CLINE_OPTIONS = -j1 --noqof
616\end{alltt}
617Under Poly/ML, the similar \texttt{POLY\_CLINE\_OPTIONS} variable can be used to pass run-time options to the Poly/ML executable that is run during theory construction.
618
619
620\index{EXTRA_CLEANS (Holmakefile variable)@\texttt{EXTRA\_CLEANS} (Holmakefile variable)}%
621The \texttt{EXTRA\_CLEANS} variable is used to
622specify the name of additional files that should be deleted when a
623\texttt{Holmake clean} command is issued.
624
625Within a command, the variable \texttt{\$<} is used to stand for the
626name of the first dependency of the rule.  The variable \texttt{\$@} is
627used to stand for the target of the rule.
628
629Finally there are variables that expand to program names and other
630useful information:
631\begin{description}
632\item[\texttt{CP}] This variable is replaced by an operating-system
633  appropriate program to perform a file copy.  The file to be copied
634  is the first argument, the second is the place to copy to.  The
635  second argument can be a directory.  (Under Unix, \texttt{CP}
636  expands to \texttt{/bin/cp}; under Windows, it expands to
637  \texttt{copy}.)
638\item[\texttt{DEBUG\_FLAG}] This variable is replaced by \texttt{"-{}-dbg"} if that flag was passed to \holmake, or the empty string if not.
639\item[\texttt{DEFAULT\_TARGETS}]\index{DEFAULT_TARGETS (Holmakefile variable)@\texttt{DEFAULT\_TARGETS} (Holmakefile variable)}%
640This variable expands to a list of the targets in the current directory that \holmake{} would build if there was no target in the Holmakefile, and no target was specified on the command-line.
641Thus, if one wishes to continue to have all these defaults built alongside an additional target, an appropriate idiom to use at the head of the file would be
642\begin{alltt}
643    all: $(DEFAULT_TARGETS) mytarget1 mytarget2
644    .PHONY: all
645\end{alltt}
646followed by rules for building the new target(s).
647
648\item[\texttt{HOLDIR}] The root of the HOL installation.
649\item[\texttt{HOLHEAP}]
650Under Poly/ML, this variable expands to the name of the heap that should be used to build this directory (to be used instead of the heap that underlies the \texttt{hol} executable).
651See Section~\ref{sec:polyml-heaps} below for more on using custom heaps with Poly/ML.
652\item[\texttt{HOLMOSMLC}] This variable is replaced by an invocation
653  of the Moscow ML compiler along with the \texttt{-q} flag (necessary
654  for handling quotations), and the usual \texttt{-I} include
655  specifications (pre-includes, the hol-directory include, and the
656  normal includes).
657\item[\texttt{HOLMOSMLC-C}] This variable is the same as
658  \texttt{HOLMOSMLC} except that it finishes with a closing
659  \texttt{-c} option (hence the name) followed by the name of the
660  system's overlay file.  This is needed for compilation of HOL source
661  files, but not for linking of HOL object code, which can be done
662  with \texttt{HOLMOSMLC}.
663\item[\texttt{KERNELID}]
664  The kernel option that was passed to \HOL's \texttt{build} command, stripped of its leading hyphens.
665  This will typically be \texttt{stdknl} (the standard kernel) but may take on other values if other custom kernels are being used.
666\item[\texttt{ML\_SYSNAME}] The name of the ML system being used:
667  either \texttt{mosml} or \texttt{poly}.
668\item[\texttt{MLLEX}] This is the path of the \texttt{mllex} tool that
669  is built as part of HOL's configuration.
670\item[\texttt{MLYACC}] This is the path of the \texttt{mlyacc} tool that
671  is built as part of HOL's configuration.
672\item[\texttt{MOSMLC}] This is replaced by an invocation of the
673  compiler along with just the normal includes.
674\item[\texttt{MOSMLLEX}] This is replaced by an invocation of the
675  \texttt{mosmllex} program that comes with the Moscow ML
676  distribution.
677\item[\texttt{MOSMLYAC}] This is replaced by an invocation of the
678  \texttt{mosmlyac} program that comes with the Moscow ML
679  distribution.
680\item[\texttt{MV}] This variable is replaced by an operating-system
681  appropriate program to perform a file movement.  The file to be
682  moved is the first argument, the second is the place to move to.
683  The second argument can be a directory.  (Under Unix, \texttt{MV}
684  expands to \texttt{mv}; under Windows, it expands to \texttt{rename}.)
685\item[\texttt{OS}] This variable is replaced by the name of the
686  current operating system, which will be one of the strings
687  \texttt{"linux"}, \texttt{"solaris"}, \texttt{"macosx"},
688  \texttt{"unix"} (for all other Unices), or \texttt{"winNT"}, for all
689  Microsoft Windows operating systems (those of the 21st century,
690  anyway).
691\item[\texttt{SIGOBJ}] Effectively \varref{HOLDIR}\texttt{/sigobj},
692  where HOL object code is stored.
693  \item[\texttt{UNQUOTE}] The location of the quotation-filter executable.
694\end{description}
695The \texttt{MOSMLLEX} and \texttt{MOSMLYAC} abbreviations are really
696only useful if the originals aren't necessarily going to be on the
697user's ``path''.  For backwards compatibility, the five variables
698above including the sub-string ``\texttt{MOSML}'' in their names can
699also be used by simply writing their names directly (i.e., without the
700enclosing \varref{\dots}), as long as these references occur first on
701a command-line.
702
703Under Poly/ML, commands involving the variable \texttt{MOSMLC} are interpreted ``appropriately''.
704If the behaviour is not as desired, we recommend using \texttt{ifdef~POLY}~(see below) to write rules that pertain only to HOL under Poly/ML.
705We strongly discourage the use of \texttt{MOSMLYAC} and \texttt{MOSMLLEX}, even when running HOL under Moscow~ML.
706
707If a reference is made to an otherwise undefined string, then it is
708treated as a reference to an environment variable.  If there is no
709such variable in the environment, then the variable is silently given
710the empty string as its value.
711
712\paragraph{Conditional parts of makefiles}
713\index{Holmake@\holmake!conditional inclusion of sections}
714As in GNU~\textsf{make}, parts of a \texttt{Holmakefile} can be included or excluded dynamically, depending on tests that can be performed on strings including variables.
715%
716This is similar to the way directives such as \texttt{\#ifdef} can be used to control the C~preprocessor.
717
718There are four possible directives in a \texttt{Holmakefile}: \texttt{ifdef}, \texttt{ifndef}, \texttt{ifeq} and \texttt{ifneq}.
719%
720The versions including the extra `n' character reverse the boolean sense of the test.
721%
722Conditional directives can be chained together with \texttt{else} directives, and must be terminated by the \texttt{endif} command.
723%
724The following example is a file that only has any content if the \texttt{POLY} variable is defined, which happens when Poly/ML is the underlying \ML{} system.
725\begin{hol}
726\begin{verbatim}
727ifdef POLY
728TARGETS = target1 target2
729
730target1: dependency1
731      build_command -o target1 dependency1
732endif
733\end{verbatim}
734\end{hol}
735The next example includes chained \texttt{else} commands:
736\begin{hol}
737\begin{verbatim}
738ifeq "$(HOLDIR)" "foo"
739VAR = X
740else ifneq "$(HOLDIR)" "bar"
741VAR = Y
742else
743VAR = Z
744endif
745\end{verbatim}
746\end{hol}
747\index{ifeq (Holmake directive)@\texttt{ifeq} (Holmake directive)}
748\index{ifneq (Holmake directive)@\texttt{ifneq} (Holmake directive)}
749The \texttt{ifneq} and \texttt{ifeq} forms test for string equality.
750%
751They can be passed their arguments as in the example, or delimited with apostrophes, or in parentheses with no delimiters, as in:
752\begin{hol}
753\begin{verbatim}
754ifeq ($(HOLDIR),$(OTHERDIR))
755VAR = value
756endif
757\end{verbatim}
758\end{hol}
759
760\index{ifdef (Holmake directive)@\texttt{ifdef} (Holmake directive)}
761\index{ifndef (Holmake directive)@\texttt{ifndef} (Holmake directive)}
762The definedness tests \texttt{ifdef} and \texttt{ifndef} test if a name has a non-null expansion in the current environment.
763%
764This test is just of one level of expansion.
765%
766In the following example, \texttt{VAR} is defined even though it ultimately expands to the emptry string, but \texttt{NULL} is not.
767%
768The variable \texttt{FOOBAR} is also not defined.
769\begin{hol}
770\begin{verbatim}
771NULL =
772VAR = $(NULL)
773\end{verbatim}
774\end{hol}
775Note that environment variables with non-empty values are also considered to be defined.
776
777\index{Holmake@\holmake|)}
778
779
780\section{\texorpdfstring{Generating and Using Heaps in Poly/ML \HOL{}}{Generating and Using Heaps in Poly/ML HOL}}
781\label{sec:polyml-heaps}
782
783\index{heaps (in Poly/ML)|(}
784\index{Poly/ML}
785\index{Moscow ML}
786Poly/ML has a nice facility whereby the state of one of its interactive sessions can be stored on disk and then reloaded.
787This allows for an efficient resumption of work in a known state.
788The \HOL{} implementation uses this facility to implement the \texttt{hol} executable.
789In Poly/ML, \texttt{hol} starts immediately.
790In Moscow~ML, \texttt{hol} starts up by visibly (and relatively slowly) ``loading'' the various source files that provide the system's functionality (\eg, \ml{bossLib}).
791
792Users can use the same basic technology to ``dump'' heaps of their own.
793Such heaps can be preloaded with source code implementing special-purpose reasoning facilities, and with various necessary background theories.
794This can make developing big mechanisations considerably more pleasant.
795
796\subsection{\texorpdfstring{Generating \HOL{} heaps}{Generating HOL heaps}}
797
798The easiest way to generate a \HOL{} heap is to use the \texttt{buildheap} executable that is built as part of the standard build process for (Poly/ML)~\HOL.
799This program takes a list of object files to include in a heap, an optional heap to build upon (use the \texttt{-b} command-line switch; the default is to use the heap behind the core \texttt{hol} executable), and an optional name for the new heap (the default is the traditional Unix \texttt{a.out}).
800Thus the command-line
801\begin{alltt}
802   buildheap -o realheap transcTheory polyTheory
803\end{alltt}
804would build a heap in the current directory called \texttt{realheap}, and would preload it with the standard theories of transcendental numbers and real-valued polynomials.
805
806A reasonable way to manage the generation of heaps is to use a \texttt{Holmakefile}.
807For example, the \texttt{realheap} above might be generated with the source in Figure~\ref{fig:realheap-makefile}.
808The use of the special variable \texttt{HOLHEAP} has a number of nice side effects.
809First, it makes the given file a dependency of all other products in the current directory.
810This means that the \HOL{} heap will be built first.
811Secondly, the other products in the current directory will be built on top of that heap, not the default heap behind \texttt{hol}.
812\begin{figure}
813\begin{alltt}
814   ifdef POLY
815   HOLHEAP = realheap
816   OBJNAMES = polyTheory transcTheory
817   DEPS = $(patsubst %,$(dprot $(SIGOBJ)/%),$(OBJNAMES))
818
819   $(HOLHEAP): $(DEPS)
820           $(protect $(HOLDIR)/bin/buildheap) -o $@ $(OBJNAMES)
821   endif
822\end{alltt}
823\caption{A \texttt{Holmakefile} fragment for building a custom \HOL{} heap embodying the standard real number theories.
824If the heap's dependencies are not core \HOL{} theories as they are here, then both the dependency line and the arguments to \texttt{buildheap} will need to be adjusted to link to the directory containing the files.
825For core \HOL{} theories, the dependency has to mention the \texttt{SIGOBJ} directory, but when passing arguments to \texttt{buildheap}, that information doesn't need to be provided as \texttt{SIGOBJ} is always consulted by all \HOL{} builds.
826Finally, note how the use of the \texttt{dprot} and \texttt{protect} functions will ensure that \holmake{} will do the right thing even when \texttt{HOLDIR} contains spaces.}
827\label{fig:realheap-makefile}
828\end{figure}
829
830\subsection{\texorpdfstring{Using \HOL{} heaps}{Using HOL heaps}}
831As just described, if a \texttt{Holmakefile} specifies a \texttt{HOLHEAP}, then files in that directory will be built on top of that heap rather than the default.
832This is also true if the specified heap is in another directory (\ie, the \texttt{HOLHEAP} line might specify a file such as \texttt{otherdir/myheap}).
833In this case, the \texttt{Holmakefile} won't (shouldn't) include instructions on how to build that heap, but the advantages of that heap are still available.
834Again, that heap is also considered a dependency for all files in the current directory, so that they will be rebuilt if it is newer than they are.
835
836It is obviously important to be able to use heaps interactively.
837If the standard \texttt{hol} executable is invoked in a directory where there is a \texttt{Holmakefile} specifying a heap, the default heap will not be used and the given heap will be used instead.
838The fact that this is happening is mentioned as the interactive session begins.
839For example:
840
841\begin{samepage}
842\begin{alltt}
843---------------------------------------------------------------------
844       HOL-4 [Kananaskis 8 (stdknl, built Tue Jul 24 16:48:44 2012)]
845
846       For introductory HOL help, type: help "hol";
847---------------------------------------------------------------------
848
849[extending loadPath with Holmakefile INCLUDES variable]
850[In non-standard heap: computability-heap]
851Poly/ML 5.4.1 Release
852>
853\end{alltt}
854\end{samepage}
855
856Finally, note that when using the \texttt{HOLHEAP} variable, heaps are required to be built before everything else in a directory, and that such heaps embody theories or \ML{} sources that are \emph{ancestral} to the directory in which the heap occurs.
857Thus, if one wanted to package up a heap embodying the standard theories for the real numbers, and to do it in \texttt{src/real} (which feels natural), this heap could be built using the method described here, but could only be referred to as a \texttt{HOLHEAP} in the directories that used it, \emph{not} in \texttt{src/real}'s \texttt{Holmakefile}.
858Subsequently, developments in other directories could use this heap by specifying\[
859  \texttt{\$(HOLDIR)/src/real/realheap}
860\]
861as the value for their \texttt{HOLHEAP} variables.
862
863\index{heaps (in Poly/ML)|)}
864\section{Timing and Counting Theorems}
865
866\index{counting inferences, in HOL proofs@counting inferences, in \HOL{} proofs|(}
867\index{inferences, in HOL logic@inferences, in \HOL{} logic!counting of|(}
868\index{timing of HOL evaluations@timing of \HOL{} evaluations|(}
869
870
871\HOL{} can be made to record its use of primitive
872inferences, axioms, definitions and use of oracles.  Such recording is
873enabled with the function
874
875\begin{holboxed}
876\index{counting_thms@\ml{counting\_thms}|pin}
877\begin{verbatim}
878   val counting_thms : bool -> unit
879\end{verbatim}
880\end{holboxed}
881
882\noindent (This function as with all the others in this section is
883found in the \texttt{Count} structure.)
884
885Calling \ml{counting\_thms true} enables counting, and
886\ml{counting\_thms false} disables it.  The default is for counting to
887be disabled.  If it is enabled, whenever \HOL{} performs a primitive
888inference (or accepts an axiom or definition) a counter is
889incremented.  A total count as well as counts per primitive inference
890are maintained.  The value of this counter is returned by the
891function:
892
893\begin{holboxed}
894\index{thm_count@\ml{thm\_count}|pin}
895\begin{verbatim}
896  val thm_count : unit ->
897   {ASSUME : int, REFL : int, BETA_CONV : int, SUBST : int,
898    ABS : int, DISCH : int, MP : int, INST_TYPE : int, MK_COMB : int,
899    AP_TERM : int, AP_THM : int, ALPHA : int, ETA_CONV : int,
900    SYM : int, TRANS : int, EQ_MP : int, EQ_IMP_RULE : int,
901    INST : int, SPEC : int, GEN : int, EXISTS : int, CHOOSE : int,
902    CONJ : int, CONJUNCT1 : int, CONJUNCT2 : int, DISJ1 : int,
903    DISJ2 : int, DISJ_CASES : int, NOT_INTRO : int, NOT_ELIM : int,
904    CCONTR : int, GEN_ABS : int, definition : int, axiom : int,
905    from_disk : int, oracle :int, total :int }
906\end{verbatim}\end{holboxed}
907
908\noindent This counter can be reset with the function:
909
910\begin{holboxed}
911\index{reset_thm_count@\ml{reset\_thm\_count}|pin}
912\begin{verbatim}
913   val reset_thm_count : unit -> unit
914\end{verbatim}\end{holboxed}
915
916Finally, the \texttt{Count} structure also includes another function
917which easily enables the number of inferences performed by an \ML{}
918procedure to be assessed:
919
920\begin{holboxed}
921\index{Count.apply@\ml{Count.apply}|pin}
922\begin{verbatim}
923   val apply : ('a -> 'b) -> 'a -> 'b
924\end{verbatim}
925\end{holboxed}
926
927An invocation, \ml{Count.apply f x}, applies the function \ml{f} to
928the argument \ml{x} and performs a count of inferences during this
929time.  This function also records the total time taken in the
930execution of the application.
931
932For example, timing the action of \ml{numLib}'s \ml{ARITH\_CONV}:
933
934\setcounter{sessioncount}{1}
935\begin{session}
936\begin{verbatim}
937- Count.apply numLib.ARITH_CONV ``x > y ==> 2 * x > y``;
938runtime: 0.010s,    gctime: 0.000s,     systime: 0.000s.
939Axioms asserted: 0.
940Definitions made: 0.
941Oracle invocations: 0.
942Theorems loaded from disk: 0.
943HOL primitive inference steps: 165.
944Total: 165.
945> val it = |- x > y ==> 2 * x > y = T : thm
946\end{verbatim}
947\end{session}
948
949\index{counting inferences, in HOL proofs@counting inferences, in \HOL{} proofs|)}
950\index{inferences, in HOL logic@inferences, in \HOL{} logic!counting of|)}
951\index{timing of HOL evaluations@timing of \HOL{} evaluations|)}
952
953
954\section{\texorpdfstring{Embedding \HOL{} in \LaTeX{}}{Embedding HOL in \LaTeX{}}}
955
956\index{LaTeX@\LaTeX!embedding HOL@embedding in \HOL{}|(}
957%
958When writing documents in \LaTeX{} about one's favourite \HOL{} development, one frequently wants to include pretty-printed terms, types and theorems from that development.
959%
960Done manually, this will typically require involved use of the \texttt{alltt} environment, and cutting and pasting from a HOL session or theory file.
961%
962The result is that one must also keep two copies of \HOL{} texts synchronised: if the \HOL{} development changes, the \LaTeX{} document should change as well.
963
964\newcommand{\munge}{\texttt{munge.exe}}
965\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})}
966%
967This manual, and error-prone process is not necessary: the standard \HOL{} distribution comes with a tool called \munge{} to automate the process, and to remove the duplicate copies of \HOL{} text.
968%
969(Strictly speaking, the distribution comes with a tool that itself creates \munge{}; see Section~\ref{sec:munger-creation} below.)
970%
971The basic philosophy is that a \LaTeX{} document can be written ``as normal'', but that three new \LaTeX{}-like commands are available to the author.
972
973The commands are not really processed by \LaTeX{}: instead the source file must first be passed through the \munge{} filter.
974%
975For example, one might write a document called \texttt{article.htex}.
976%
977This document contains instances of the new commands, and cannot be processed as is by \LaTeX{}.
978%
979Instead one first runs
980\begin{alltt}
981   \munge < article.htex > article.tex
982\end{alltt}
983and then runs \LaTeX{} on \texttt{article.tex}.
984%
985One would probably automate this process with a makefile of course.
986
987\subsection{Munging commands}
988\label{sec:munging-commands}
989% need to get backslashes conveniently obscures the real names,
990% preventing the munger from seeing them, which will be useful when we
991% run the munger over this document!
992\newcommand{\holtm}{\texttt{\bs{}HOLtm}}
993\newcommand{\holty}{\texttt{\bs{}HOLty}}
994\newcommand{\holthm}{\texttt{\bs{}HOLthm}}
995\paragraph{Before starting} In order to use the munger, one must ``include'' (use the \texttt{\bs{}usepackage} command) the \texttt{holtexbasic.sty} style-file, which is found in the HOL source directory \texttt{src/TeX}.
996
997\bigskip
998There are then three commands for inserting text corresponding to \HOL{} entities into \LaTeX{} documents: \holtm, \holty{} and \holthm.
999%
1000Each takes one argument, specifying something of the corresponding \HOL{} type.
1001%
1002In addition, options can be specified in square brackets, just as would be done with a genuine \LaTeX{} command.
1003%
1004For example, one can write
1005\begin{alltt}
1006   \holtm{}[tt]\{P(SUC n) /\bs{} q\}
1007\end{alltt}
1008and one will get \[
1009  \texttt{$P$ (SUC $n$) $\land$ $q$}
1010\]
1011or something very close to it, appearing in the resulting document.\footnote{The output is a mixture of typewriter font and math-mode characters embedded in a \texttt{\bs{}texttt} block within an \texttt{\bs{}mbox}.}
1012%
1013Note how the spacing in the input (nothing between the \texttt{P} and the \texttt{SUC n}) is \emph{not} reflected in the output; this is because the input is parsed and pretty-printed with \HOL{}.
1014%
1015This means that if the \HOL{} input is malformed, the \munge{} program will report errors.
1016%
1017Note also how the system knows that \texttt{P}, \texttt{n} and \texttt{q} are variables, and that \texttt{SUC} is not.
1018%
1019This analysis would not be possible without having \HOL{} actually parse and print the term itself.
1020
1021The default behaviours of each command are as follows:
1022\begin{description}
1023\item[\holty\{\textit{\mdseries{string}}\}]
1024%
1025\index{HOLty (munging command)@\holty{} (munging command)}
1026%
1027Parses the string argument as a type (the input must include the leading colon), and prints it.
1028%
1029The output is suited for inclusion in the normal flow of \LaTeX{} (it is an \texttt{\bs{}mbox}).
1030\item[\holtm\{\textit{\mdseries{string}}\}]
1031%
1032\index{HOLtm (munging command)@\holtm{} (munging command)}
1033%
1034Parses the string argument as a term, and prints it.
1035%
1036Again, the output is wrapped in an \texttt{\bs{}mbox}.
1037
1038\paragraph{Important:} If the string argument includes a right-brace character
1039(\ie, the character \rb, which has ASCII code 125), then it must be
1040escaped by preceding it with a backslash~(\bs).  Otherwise, the
1041munger's lexer will incorrectly determine that the argument ends at
1042that right-brace character rather than at a subsequent one.
1043\item[\holthm\{\textit{\mdseries{thmspecifier}}\}]
1044%
1045\index{HOLthm (munging command)@\holthm{} (munging command)}
1046%
1047The argument should be of the form $\langle\mbox{\textit{theory}}\rangle\texttt{.}\langle{\mbox{\textit{theorem-name}}}\rangle$.
1048%
1049For example, \verb|\HOLthm{bool.AND_CLAUSES}|.
1050%
1051This prints the specified theorem with a leading turnstile.
1052%
1053\index{Datatype@\ml{Datatype}!printing in LaTeX@printing in \LaTeX{}}
1054However, as a special case, if the theorem specified is a ``datatype theorem'' (with a name of the form \texttt{datatype\_}$\langle$\textit{type-name}$\rangle$), a BNF-style description of the given type (one that has been defined with \ml{Datatype}) will be printed.
1055%
1056Datatype theorems with these names are automatically generated when \ml{Datatype} is run.
1057If the trace \texttt{EmitTeX:~print~datatypes~compactly} is set to 1 (see the \texttt{tr} option below) the description is printed in a more compact form.
1058Also, if the type is a collection of nullary constants (a type consisting of only ``enumerated constants''), then it will always be printed compactly.
1059When not compact, all of a type's constructors will appear on the same line, or each will be on a separate line.
1060
1061By default, the output is \emph{not} wrapped in an \texttt{\bs{}mbox}, making it best suited for inclusion in an environment such as \texttt{alltt}.
1062(The important characteristics of the \texttt{alltt} environment are that it respects layout in terms of newlines, while also allowing the insertion of \LaTeX{} commands.
1063The \texttt{verbatim} environment does the former, but not the latter.)
1064\end{description}
1065
1066\paragraph{Munging command options}
1067\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})!command options}
1068There are a great many options for controlling the behaviour of each of these commands.
1069%
1070Some apply to all three commands, others are specific to a subset.
1071%
1072\newcommand{\indentoption}{\gt\gt}
1073If multiple options are desired, they should be separated by commas. For example: \texttt{\holthm{}[nosp,p/t,\indentoption]\{bool.AND\_CLAUSES\}}.
1074
1075\begin{description}
1076\item[\texttt{alltt}] Makes the argument suitable for inclusion in an \texttt{alltt} environment.
1077%
1078This is the default for \holthm.
1079\item[\texttt{case}] (Only for use with \holtm.)
1080%
1081Causes the string to be parsed in such a way that any embedded \texttt{case} terms are only partly parsed, allowing their input form to appear when they are output.
1082%
1083This preserves underscore-patterns, for example.
1084
1085\item[\texttt{conj}$n$] (Only for use with \holthm.)
1086Extracts the $n^{\mbox{\scriptsize th}}$ conjunct of a theorem.
1087The conjuncts are numbered starting at $1$, not $0$.
1088For example,
1089\begin{verbatim}
1090  \HOLthm[conj3]{bool.AND_CLAUSES}
1091\end{verbatim}
1092extracts the conjunct $\vdash \texttt{F} \land t \iff \texttt{F}$.
1093
1094\item[\texttt{def}] (Only for use with \holthm.)
1095%
1096Causes the theorem to be split into its constituent conjuncts, for each conjunct to have any outermost universal quantifiers removed, and for each to be printed on a line of its own.
1097%
1098The turnstiles usually printed in front of theorems are also omitted, and a special form of equality is printed for the top-level (``defining'') equality in each clause.
1099%
1100This works well with definitions (or characterising theorems) over multiple data type constructors, changing
1101\begin{alltt}
1102\(\vdash\) (FACT 0 = 1) \(\land\) (\(\forall\)\ensuremath{n}. FACT (SUC \ensuremath{n}) = SUC \ensuremath{n} * FACT \ensuremath{n})
1103\end{alltt}
1104into
1105\begin{alltt}
1106   FACT 0 \HOLTokenDefEquality{} 1
1107   FACT (SUC \ensuremath{n}) \HOLTokenDefEquality{} SUC \ensuremath{n} * FACT \ensuremath{n}
1108\end{alltt}
1109If the special equality is not desired, the option \texttt{nodefsym} can be used to turn this off.
1110The special equality symbol can also be redefined by changing the \LaTeX{} definition of the macro \ml{\bs{}HOLTokenDefEquality}.
1111
1112There are two variations on the \texttt{def} option:
1113\begin{description}
1114\item[\texttt{spaceddef}:]%
1115\index{spaceddef, HOL in LaTeX option@\ml{spaceddef}, \HOL{} in \LaTeX{} option}%
1116This option adds extra blank lines between successive conjuncts of a definition.
1117\item[\ml{aligneddef}:]%
1118\index{aligneddef, HOL in LaTeX option@\ml{aligneddef}, \HOL{} in \LaTeX{} option}%
1119This option puts ampersands around the \ml{\bs{}HOLTokenDefEquality} macro call in the emitted \LaTeX{}.
1120If the \LaTeX{} environment is an \texttt{array} or similar, this can ensure a nice column-based layout for one's definitions.
1121\end{description}
1122Both options can be used together, but \ml{aligneddef} will not work if \ml{nodefsym} is also used; if this combination is really required, it would be better to temporarily redefine (use \ml{\bs{}renewcommand}) \ml{\bs{}HOLTokenDefEquality}.
1123
1124
1125\item[\texttt{depth}=$n$]
1126Causes printing to be done with a maximum print depth of $n$; see Section~\ref{sec:pretty-print-depth}.
1127
1128\item[\texttt{K}] (Only for use with \holtm.)
1129%
1130The argument must be the name of a theorem (as per the \holthm{} command), and the theorem should be of the form
1131\[
1132\vdash f\;x\;t
1133\]
1134for some term $t$.
1135%
1136The command prints the term $t$.
1137%
1138\index{combinators, in HOL logic@combinators, in \HOL{} logic}%
1139\index{K, the HOL constant@\ml{K}, the \HOL{} constant}%
1140The expectation is that $f$ will be the combinator \holtxt{K} from \theoryimp{combin} (see Section~\ref{sec:combinTheory}), and that $x$ will be truth~(\holtxt{T}), allowing $t$ to be anything at all.
1141%
1142In this way, large complicated terms that are not themselves theorems (or even of boolean type), can be stored in \HOL{} theories, and then printed in \LaTeX{} documents.
1143
1144\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})!math mode}
1145\index{math mode (in the LaTeX munger)@math mode (in the \LaTeX{} munger)}
1146\item[\texttt{m}$\mathit{space}$,\texttt{nomath}] The \texttt{m} option makes \HOL{} material be typeset in ``math-mode''.
1147In particular, the output of the pretty-printer will be modified so that newline characters are replaced by \texttt{\bs\bs} commands.
1148This then requires that the surrounding \LaTeX{} environment be array-like, so that the \texttt{\bs\bs} command will have the desired effect.
1149
1150In addition, because raw spaces have minimal effect in math-mode (something like \texttt{f\textvisiblespace{}x} will be typeset as $f x$), math-mode munging also replaces spaces with math-mode macros.
1151By default, the command \texttt{\bs;\bs;} is used, but if the \texttt{m} option is followed by some characters, each is interpreted as a single-letter macro name, with each macro concatenated together to provide the space command that will be used.
1152
1153For example, if the option is \texttt{m;}, then the spacing command will be \texttt{\bs;}.
1154If the option is \texttt{m;!}, then the spacing command will be \texttt{\bs;\bs!}.
1155The comma character cannot be used because it conflicts with parsing the list of options, but one can use \texttt{c} instead, so that the option \texttt{mc} will make the spacing command be \texttt{\bs,}.
1156
1157The \texttt{m} option can be installed globally with the \texttt{-m} command-line option.
1158If this option is enabled globally, it can be cancelled on a case-by-case basis by using the \texttt{nomath} option.
1159The \texttt{nomath} option also takes precedence over any \texttt{m} options that might occur.
1160
1161See also the discussion about math-mode munging in Section~\ref{sec:math-mode-munging} below.
1162
1163\item[\texttt{merge}, \texttt{nomerge}] (For use with \holtm{} and \holthm.)
1164By default, the HOL pretty-printer is paranoid about token-merging, and will insert spaces between the tokens it emits to try to ensure that what is output can be read in again without error.
1165%
1166This behaviour can be frustrating when getting one's \LaTeX{} to look ``just so'', so it can be turned off with the \texttt{nomerge} option.
1167
1168Additionally, this behaviour can be turned off globally with the \texttt{--nomergeanalysis} option to the munger.
1169%
1170If this has been made the default, it may be useful to occasionally turn the merge analysis back on for a particular term or theorem; this is done with the \texttt{merge} option.
1171%
1172(In interactive HOL, the token-merging analysis is controlled by a trace variable called \texttt{"pp\_avoids\_symbol\_merges"}.)
1173
1174
1175\item[\texttt{nodollarparens}] (For use with \holtm{} and \holthm.) Causes the default escaping of syntactic sugar to be suppressed.
1176\index{ dollar sign, in HOL logic parser@\ml{\$} (dollar sign, in \HOL{} logic parser)!as escape character}%
1177\index{tokens!suppressing parsing behaviour of}%
1178The default behaviour is to use parentheses, so that
1179% encode all the special characters below so that emacs font-locking has a reasonable chance of working
1180\begin{alltt}
1181   \holtm\lb\dol/\bs p\rb
1182\end{alltt}
1183would get printed as $(\land)\;\,p$.
1184Note that this doesn't reflect the default behaviour in the interactive loop, which is to use dollar-signs (as in the input above); see Section~\ref{sec:parser:architecture}.
1185However, with the \texttt{nodollarparens} option specified, nothing at all is printed to indicate that the special syntax has been ``escaped''.
1186
1187\item[\texttt{nosp}] (Only for use with \holthm.)
1188%
1189By default, arguments to \holthm{} are fully specialised (\ie, they have \ml{SPEC\_ALL} applied to them), removing outermost universal quantifiers.
1190%
1191The \texttt{nosp} option prevents this.
1192
1193\item[\texttt{nostile}] (Only for use with \holthm.)
1194%
1195By default, arguments to \holthm{} are printed with a turnstile~($\vdash$).
1196%
1197If this option is present, the turnstile is not printed (and the theorem will have its left margin three spaces further left).
1198%
1199For controlling how the turnstile is printed when this option is not present, see the paragraph on Overrides in Section~\ref{sec:running-munger}.
1200
1201\item[\texttt{of}] (Only for use with \holty.)
1202%
1203The argument is a string that parses to a \emph{term}, not a type.
1204%
1205The behaviour is to print the type of this term.
1206%
1207Thus \texttt{\holty{}[of]\{p /\bs{} q\}} will print \texttt{bool}.
1208
1209If the string includes right-braces, they must be escaped with
1210back-slashes, just as with the arguments to \holtm.
1211
1212\item[\texttt{rule}] (Only for use with \holtm{} and \holthm.)
1213Prints a term (or a theorem's conclusion) using the \texttt{\bs{}infer} command (available as part of the \texttt{proof.sty} package).
1214This gives a nice, ``natural deduction'' presentation.
1215\index{natural deduction!presentation style for the LaTeX munger@presentation style for the \LaTeX{} munger}
1216For example, the term
1217\begin{alltt}
1218   (p \bs{}/ q) /\bs{} (p ==> r) /\bs{} (q ==> r) ==> r
1219\end{alltt}
1220will print as
1221\[
1222\infer{r}{p \lor q & p \Rightarrow r & q \Rightarrow r}
1223\]
1224Conjuncts to the left of the outermost implication (if any) will be split into hypotheses separated by whitespace.
1225For large rules, this style of presentation breaks down, as there may not be enough horizontal space on the page to fit in all the hypotheses.
1226In this situation, the \texttt{stackedrule} option is appropriate.
1227
1228The term or theorem must be within a \LaTeX{} math-environment (it is typeset as if inline, with the \texttt{tt} option).
1229
1230For adding a name to the rule, see the \texttt{rulename} option below.
1231
1232\item[\texttt{rulename=}$\mathit{name}$] (Only has an effect with \texttt{rule} or \texttt{stackedrule}.)
1233Adds \textit{name} as the optional argument to the \texttt{\bs{}infer} command when typesetting the rule.
1234The name is wrapped with \texttt{\bs{}HOLRuleName}, which by default is the same as \texttt{\bs{}textsf}.
1235For ease of parsing options, \textit{name} should not contain braces, brackets, or commas.
1236(A name including such special characters could be typeset by renewing the \texttt{\bs{}HOLRuleName} command.)
1237
1238\item[\texttt{showtypes}$n$] (For use with \holthm{} and \holtm.)
1239%
1240Causes the term or theorem to be printed with the \texttt{types} trace set to level~$n$.
1241The $n$ is optional and defaults to $1$ if omitted (equivalent to having the \ml{show\_types} reference set to \ml{true}).
1242
1243\item[\texttt{stackedrule}] (For use with \holthm{} and \holtm.)
1244This is similar to the \texttt{rule} option, but causes implication hypotheses to be presented as a ``stack'', centered in a \LaTeX{} array on top of one another.
1245Thus,
1246\begin{alltt}
1247   (p \bs{}/ q) /\bs{} (p ==> r) /\bs{} (q ==> r) ==> r
1248\end{alltt}
1249will print as
1250\[
1251\infer{r}{\begin{array}{c}p \lor q \\ p \Rightarrow r \\ q \Rightarrow r\end{array}}
1252\]
1253For this purely propositional example with single-letter variable names, the result looks a little odd, but if the hypotheses are textually larger, this option is indispensable.
1254
1255For adding a name to the rule, see the \texttt{rulename} option.
1256
1257\item[\texttt{tr\apost}$\mathit{tracename}$\texttt{\apost=}$n$]
1258This option allows the temporary setting of the provided trace to the integer value $n$.
1259For example, one can set \texttt{pp\_unambiguous\_comprehensions} to $1$ to ensure that set comprehensions are printed with bound variables explicitly identified.
1260See Section~\ref{sec:set-syntax} for more on set comprehensions, and Section~\ref{sec:traces} for more on traces.
1261\index{traces, controlling HOL feedback@traces, controlling \HOL{} feedback!when munging to LaTeX@when munging to \LaTeX{}}
1262
1263\item[\texttt{tt}] %
1264Causes the term to be type-set as the argument to a \LaTeX{} command \texttt{\bs{}HOLinline}.
1265%
1266The default definition for \texttt{\bs{}HOLinline} is
1267\begin{verbatim}
1268   \newcommand{\HOLinline}[1]{\mbox{\textup{\texttt{#1}}}}
1269\end{verbatim}
1270This makes the argument suitable for inclusion in standard \LaTeX{} positions.
1271%
1272This is the default for \holtm{} and \holty.
1273%
1274(The \texttt{\bs{}HOLinline} command is defined in the \texttt{holtexbasic.sty} style file.)
1275
1276\item[\texttt{width=}$n$] Causes the argument to be typeset in lines of width $n$.
1277%
1278The default width is $63$, which seems to work well with 11pt fonts.
1279%
1280This default can also be changed at the time the \munge{} command is
1281run (see Section~\ref{sec:running-munger} below).
1282
1283\item[\texttt{-}$\mathit{name}$]
1284%
1285\index{parsing, of HOL logic@parsing, of \HOL{} logic!overloading}
1286This option causes the printing of the term or theorem to be done with respect to a grammar that has all overloading for $\mathit{name}$ removed.
1287When used with \holty, prints the type with all type abbreviations for $\mathit{name}$ removed.
1288For example, the command \texttt{\holtm[-+]\lb{}x + y\rb} will print as
1289\[
1290\texttt{arithmetic\dol+}\;x\;y
1291\]
1292because the underlying constant will no longer map to the string \texttt{"+"} and, in the absence of any other mappings for it, will be printed as a fully qualified name.
1293
1294\index{integers, the HOL theory of@integers, the \HOL{} theory of}
1295If the theory of integers is loaded, then the command \texttt{\holtm[-+]\lb{}x + y:int\rb} will print as \[
1296\texttt{int\_add}\;x\;y
1297\]
1298because the mapping from the integer addition constant to \texttt{"+"} is removed, but the mapping to \texttt{"int\_add"} remains, allowing that form to be what is printed.
1299
1300The \texttt{-} option can be useful when complicated notation involving overloads is first introduced in a document.
1301
1302\item[\texttt{\indentoption} and \texttt{\indentoption\td}] Indents the argument.
1303%
1304  These options only make sense when used with the \texttt{alltt} option (the additional spaces will have no effect when inside an \texttt{\bs{}mbox}).
1305%
1306  The default indentation is two spaces; if a different indentation is desired, the option can be followed by digits specifying the number of space characters desired.
1307%
1308  For example, \texttt{\holthm{}[\indentoption10,...]\{...\}} will indent by 10 spaces.
1309
1310  Note that simply placing a command such as \holthm{} within its \texttt{alltt} block with a given indentation, for example
1311\begin{alltt}
1312   \bs{}begin\{alltt\}
1313      \holthm\{bool.AND_CLAUSES\}
1314   \bs{}end\{alltt\}
1315\end{alltt}
1316will not do the right thing if the output spans multiple lines.
1317%
1318Rather the first line of \HOL{} output will be indented, and the subsequent lines will not.
1319%
1320The \texttt{\indentoption} option lets the pretty-printer know that it is printing with a given indentation, affecting all lines of its output.
1321
1322The version with the tilde character~(\td) does not add indentation to the first line of output, but adds the specified amount (again 2, if no number is provided) to subsequent lines.
1323This allows one to achieve suitable alignment when other non-HOL text has been put onto the same line.
1324For example,
1325\begin{alltt}
1326  AND_CLAUSES \bs{}HOLthm[width=46,>>~12]\lb{}bool.AND_CLAUSES\rb
1327  TRUTH       \bs{}HOLthm[>>~12]\lb{}bool.TRUTH\rb
1328  MAP         \bs{}HOLthm[>>~12,width=50]\lb{}list.MAP\rb
1329\end{alltt}
1330ensures correct vertical alignment when extra lines are printed, as they will be with the printing of \ml{bool.AND_CLAUSES} and \ml{list.MAP}.
1331
1332\item[$\mathit{nm}_1\mathtt{/}\mathit{nm}_2$] (For use with \holtm{}
1333  and \holthm{}.)
1334%
1335Causes name $\mathit{nm}_1$ to be substituted for name $\mathit{nm}_2$ in the term or theorem.
1336%
1337This will rename both free and bound variables, wherever they occur throughout a term.
1338%
1339Because it uses instantiation, free variables in theorem hypotheses will get renamed, but bound variables in hypotheses are not affected.
1340%
1341(Hypotheses are not printed by default anyway of course.)
1342
1343If $\mathit{nm}_1$ and $\mathit{nm}_2$ both begin with the colon character then they are parsed as types, and type instantiation is performed on the term or theorem argument instead of variable substitution.
1344
1345\item[$s\mathtt{//}t$] (For use with \holtm{}, \holthm, and \holty) Causes \LaTeX{} string $s$ to be substituted for token $t$.
1346This allows one-off manipulation of the override map (see Section~\ref{sec:running-munger} below).
1347The difference between this operation and the ``normal substitution'' done with a single slash (as above) is that it happens as the \HOL{} entity is printed, whereas normal substitution happens before pretty-printing is done.
1348If printing depends on particular variable name choices, the ``last minute'' manipulations possible with this form of substitution may be preferable.
1349The width of the \LaTeX{} string is taken to be the width of the original token $t$.
1350\end{description}
1351
1352\subsection{Math-mode munging}
1353\label{sec:math-mode-munging}
1354\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})!math mode}
1355\index{math mode (in the LaTeX munger)@math mode (in the \LaTeX{} munger)}
1356
1357There are a few steps needed to make math-mode munging a relatively painless affair.
1358First, there are two \LaTeX{} macros from \texttt{holtexbasic.sty} that should probably be overridden:
1359\begin{description}
1360\item[\texttt{\bs{}HOLConst}] By default this will print names in typewriter font.
1361In math mode, this will probably look better in sans serif, suggesting
1362\begin{verbatim}
1363   \renewcommand{\HOLConst}[1]{\textsf{#1}}
1364\end{verbatim}
1365Depending on personal taste, the \texttt{\bs{}HOLKeyword} macro might be redefined similarly.
1366This macro is used for keywords such as \texttt{if}.
1367\item[\texttt{\bs{}HOLinline}] This macro, used to wrap standard \texttt{\holtm} arguments, puts text into typewriter font.
1368One possibility for its redefinition would be
1369\begin{verbatim}
1370   \renewcommand{\HOLinline}[1]{\ensuremath{#1}}
1371\end{verbatim}
1372Note that if the term being typeset causes the pretty-printer to break over multiple lines, \LaTeX{} will complain because of the appearance of \texttt{\bs\bs} commands.
1373If necessary, this can be avoided on a case-by-case basis by setting the \texttt{width} option to a larger than normal width.
1374\end{description}
1375
1376When using math-mode munging, one also has to be aware of how larger pieces of text will appear.
1377In non-math-mode munging, material is usually put into \texttt{alltt} environments.
1378The recommended alternative for math-mode is to use the \texttt{\bs{}HOLmath} environment:
1379\begin{alltt}
1380   \textit{article text}
1381
1382   \bs{}begin\lb{}HOLmath\rb
1383   \holthm\lb{}bool.AND_CLAUSES\rb
1384   \bs{}end\lb{}HOLmath\rb
1385\end{alltt}
1386This uses a standard \texttt{array} environment within a \texttt{displaymath}.
1387
1388Occasionally, one will want to arrange blocks of \HOL{} material within a larger math context.
1389The \texttt{HOLarray} environment is a simple alias for a single-column left-aligned array that one can use in these situations.
1390
1391
1392\subsection{Creating a munger}
1393\label{sec:munger-creation}
1394
1395\newcommand{\mkmunge}{\texttt{mkmunge.exe}}
1396\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})!creating a munger}
1397%
1398The \HOL{} distribution comes with a tool called \mkmunge.
1399%
1400This executable is used to create munge executables that behave as described in this section.
1401%
1402A typical invocation of \mkmunge{} is
1403\begin{alltt}
1404   \mkmunge \(\langle\mathit{thy}\sb{1}\rangle\)Theory ... \(\langle\mathit{thy}\sb{n}\rangle\)Theory
1405\end{alltt}
1406Each commandline argument to \mkmunge{} is the name of a \HOL{} object file, so in addition to theory files, one can also include special purpose SML such as \texttt{monadsyntax}.
1407
1408The \mkmunge{} program can also take an optional \texttt{-o} argument that is used to specify the name of the output munger (the default is \munge).  For example
1409\begin{alltt}
1410   \mkmunge -o bagtexprocess bagTheory
1411\end{alltt}
1412
1413The theories specified as arguments to \mkmunge{} determine what theorems are in scope for calls to \holthm, and also determine the grammars that will govern the parsing and printing of the \HOL{} types, terms and theorems.
1414
1415Under Poly/ML, the \mkmunge{} executable also takes an optional \texttt{-b} option that can be used to specify a heap (see Section~\ref{sec:polyml-heaps}) to use as a base.
1416Doing so allows for the incorporation of many theories at once, and will be more efficient than loading the heap's theories separately on top of the default HOL heap.
1417The use of a base heap argument to \mkmunge{} doesn't affect the efficiency of the resulting munging tool.
1418
1419
1420\subsection{Running a munger}
1421\label{sec:running-munger}
1422
1423\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})!running a munger}
1424Once created, a munger can be run as a filter command, consuming its
1425standard input, and writing to standard output.
1426%
1427It may also write error messages and warnings to its standard error.
1428
1429Thus, a standard pattern of use is something like
1430\begin{alltt}
1431   \munge < article.htex > article.tex
1432\end{alltt}
1433
1434However, there are a number of ways of further modifying the behaviour of the munger with command-line options.
1435
1436\paragraph{Overrides}
1437Most importantly, one can specify an ``overrides file'' to provide
1438token-to-\LaTeX{} replacements of what is pretty-printed.
1439%
1440The command-line would then look like
1441\begin{alltt}
1442   \munge overrides_file < article.htex > article.tex
1443\end{alltt}
1444The overrides file is a text file containing lines of the form
1445\begin{alltt}
1446   tok width tex
1447\end{alltt}
1448where \texttt{tok} is a \HOL{} token, \texttt{width} is a number
1449giving the width of the \LaTeX{}, and \texttt{tex} is a \LaTeX{}
1450string.
1451
1452As a very simple example, an overrides file might consist of just one
1453line:
1454\begin{alltt}
1455   pi1 2 \bs{}ensuremath\{\bs{}pi_1\}
1456\end{alltt}
1457This would cause the string \texttt{pi1} (presumably occurring in the
1458various \HOL{} entities as a variable name) to be replaced with the
1459rather prettier $\pi_1$.
1460%
1461The \texttt{2} records the fact that the
1462printer should record the provided \LaTeX{} as being 2 characters
1463wide.
1464%
1465This is important for the generation of reasonable line-breaks.
1466
1467Overrides for \HOL{} tokens can also be provided within \HOL{}
1468theories, using the
1469\texttt{TeX\_notation} command (see
1470Section~\ref{sec:holtheories-tex-ready} below).
1471
1472By overriding the special token \texttt{\$Turnstile\$}, one can control the printing of the turnstile produced by \holthm{}.
1473The default setup is roughly equivalent to overriding \texttt{\$Turnstile\$} to \texttt{\textbackslash{}HOLTokenTurnstile\{\}} followed by a space, giving a total width of \texttt{3}.
1474Overriding the turnstile in this way will probably be necessary in math-mode printing, where the turnstile character is typically of the same width as 5 \texttt{\bs;} invocations.
1475Providing the correct width is important in order to get lines past the first to line up with the left edge of the mathematical text rather than the turnstile.
1476
1477\paragraph{Default width}
1478A munger can specify the default width in which \HOL{} will print its
1479output with a \texttt{-w} option.
1480%
1481For example,
1482\begin{alltt}
1483   \munge -w70 < article.htex > article.tex
1484\end{alltt}
1485This default width can be overridden on a case-by-case basis with the
1486\texttt{width=} option to any of the commands within a \LaTeX{}
1487document.
1488
1489\paragraph{Preventing Merge Analysis}  As mentioned above in the description of the \texttt{merge} and \texttt{nomerge} options to the \holtm{} and \holthm{} commands, the munger can be configured to not do token-merging avoidance by passing the \texttt{--nomergeanalysis} option to the munger.
1490
1491\smallskip \noindent The \texttt{-w}, \texttt{--nomergeanalysis} and
1492overrides file options can be given in any order.
1493
1494\paragraph{Setting Math-mode Spacing} If one expects to include all of the various \texttt{\bs{}HOL} commands in \LaTeX{} math contexts (as described above), then the \texttt{-m} option both sets the default width for math-mode spaces, and also
1495enables math-mode typesetting by default.
1496
1497The specification of spacing is with a string of characters, as already described.
1498Note that if the command-line option includes any semi-colons or exclamation marks (\eg, \texttt{-mc;}), then they need to be quoted to prevent the shell from getting confused.
1499If the \texttt{-m} option appears without any additional characters, the default math-mode spacing will be \texttt{\bs;\bs;}.
1500
1501
1502\subsection{Holindex}
1503
1504\index{munging (producing LaTeX from HOL)@munging (producing \LaTeX{} from \HOL{})!Holindex}
1505Till now, it has been explained how the munger can be used as a preprocessor of \LaTeX{} sources.
1506Sometimes a tighter interaction with \LaTeX{} is beneficial.
1507Holindex is a \LaTeX{} package that provides genuine \LaTeX{} commands for inserting \HOL{}-theorems, types and terms as well as many related commands.
1508This allows it to generate an index of all \HOL{}-theorems, types and terms that occur in the document as well as providing citation commands for \HOL{} entities in this index.
1509Holindex can be found in \texttt{src/TeX/}.
1510There is also a demonstration file available in this directory.
1511
1512
1513\paragraph{Using Holindex}
1514To use Holindex add \texttt{\bs{}usepackage\{holindex\}} to the header
1515of the \LaTeX{} source file \texttt{article.tex}. Holindex loads the
1516\texttt{underscore} package which might cause trouble with references
1517and citations. In order to avoid problems,
1518\texttt{holindex} should be included after packages
1519like \texttt{natbib}. Holindex is used like BibTex or
1520MakeIndex. A run of \LaTeX{} on \texttt{jobname.tex} creates an
1521auxiliary file called \texttt{article.hix}. The munger is used to
1522process this file via
1523\begin{alltt}
1524   \munge -index article
1525\end{alltt}
1526This call generates two additional auxiliary files,
1527\texttt{article.tde} and \texttt{article.tid}.  The following runs of
1528\LaTeX{} use these files. After modifying the source file, the munger
1529can be rerun to update \texttt{article.tde} and \texttt{article.tid}.
1530If you are using emacs with AUCTeX to write your latex files, you might
1531want to add
1532\begin{verbatim}
1533(eval-after-load "tex" '(add-to-list 'TeX-command-list
1534   '("Holindex" "munge.exe -index %s"
1535     TeX-run-background t t :help "Run Holindex") t))
1536\end{verbatim}
1537to your emacs configuration file. This will allow
1538you to run Holindex using AUCTeX.
1539
1540\paragraph{Holindex commands}
1541\begin{description}
1542\item[\texttt{\bs{}blockHOLthm\{id\}}, \texttt{\bs{}blockHOLtm\{id\}}, \texttt{
1543    \bs{}blockHOLty\{id\}}] These commands typeset the theorem, term
1544  or type with the given \texttt{id} as the argument to a \LaTeX{}
1545  command \texttt{\bs{}HOLblock}. They are intended for
1546  typesetting multiple lines in a new block.
1547  For theorem ids of the form \texttt{theory.thm} are predefined. All
1548  other ids have to be defined before usage as explained below.
1549%
1550\item[\texttt{\bs{}inlineHOLthm\{id\}}, \texttt{\bs{}inlineHOLtm\{id\}}, \texttt{\bs{}inlineHOLty\{id\}}]
1551   These commands are similar to \texttt{\bs{}blockHOLthm\{id\}}, \texttt{\bs{}blockHOLtm\{id\}} and \texttt{
1552    \bs{}blockHOLty\{id\}}. However, they are intended for inline typesetting and
1553   use \texttt{\bs{}HOLinline} instead of \texttt{\bs{}HOLblock}.
1554%
1555\item[\texttt{\bs{}citeHOLthm\{id\}}, \texttt{\bs{}citeHOLtm\{id\}}, \texttt{\bs{}citeHOLty\{id\}}]
1556   These commands cite a theorem, term or type.
1557%
1558\item[\texttt{\bs{}mciteHOLthm\{id,id,...id\}}, \texttt{\bs{}mciteHOLtm\{ids\}}, \texttt{\bs{}mciteHOLty\{ids\}}]
1559   These commands cite multiple theorems, terms or types.
1560%
1561\item[\texttt{\bs{}citePureHOLthm\{id\}}, \texttt{\bs{}citePureHOLtm\{id\}},
1562   \texttt{\bs{}citePureHOLty\{id\}}] These commands\linebreak cite a theorems, terms or types.
1563   They just typeset the number instead of the
1564   verbose form used by the \texttt{citeHOL} and \texttt{mciteHOL} commands.
1565
1566\item[\texttt{\bs{}citeHiddenHOLthm\{id\}},
1567   \texttt{\bs{}citeHiddenHOLtm\{id\}},
1568   \texttt{\bs{}citeHiddenHOLty\{id\}}] These commands cite a
1569   theorems, terms or types, but not typeset anything. These commands
1570   can be used to add a page to the list of pages a theorem, term or
1571   type is cited.
1572
1573 \item[\texttt{\bs{}printHOLIndex}, \texttt{\bs{}printHOLShortIndex},
1574   \texttt{\bs{}printHOLLongIndex}] These commands typeset the index
1575   of all theorems, terms and types cited in the document.  There are
1576   two types of entries in the index: long and short ones. Short
1577   entries contain a unique number, the label of the theorem, term or
1578   type and the pages it is cited.  Long entries contain additionally
1579   a representation as it would be inserted by
1580   \texttt{\bs{}blockHOL...} as well as an optional description.
1581   Theorems use by default short entries, while terms and types use
1582   long ones.  It is possible to change for each item whether a long
1583   or short entry should be used. \texttt{\bs{}printHOLIndex} prints
1584   the default index with mixed long and short entries.
1585   \texttt{\bs{}printHOLLongIndex} typesets just long entries and
1586   \texttt{\bs{}printHOLShortIndex} just short ones.
1587\end{description}
1588
1589
1590\paragraph{Defining and formatting terms, types and theorems}
1591
1592  Most of the Holindex commands require an identifier of a theorem,
1593  term or type as arguments. Theorem identifiers of the form
1594  \texttt{theory.theorem} are predefined. All other identifiers need
1595  defining. Additionally one might want to change the default
1596  formatting options for these new identifiers as well as the old ones.
1597  \HOL{} definition files can be used for defining and
1598  setting the formatting options of identifiers. They are used by
1599  putting the command \texttt{\bs{}useHOLfile\{\textit{filename.hdf}\}} in
1600  the header of your latex source file. These file use a syntax similar to
1601  BibTex. They consist of a list of entries of the form
1602  \begin{verbatim}
1603@EntryType{id,
1604  option = value,
1605  boolFlag,
1606  ...
1607}
1608\end{verbatim}
1609\noindent
1610There are the following entry types
1611\begin{description}
1612\item[\texttt{Thm}, \texttt{Theorem}] used to define and format a
1613  theorem. If the identifier is of the form \texttt{theory.theorem},
1614  the \texttt{content} option can be skipped. Otherwise, the
1615  \texttt{content} option should be of this form and a new identifier
1616  is defined for the given theorem. This is for example useful if the
1617  theorem name contains special characters or if a theorem should
1618  be printed with different formatting options.
1619  \item[\texttt{Term}]
1620    used to define and format a term.
1621  \item[\texttt{Type}]
1622    used to define and format a type.
1623  \item[\texttt{Thms}, \texttt{Theorems}] used to set formatting options for
1624    a list of theorems. For example one might want to print long index entries
1625    for all theorems in a specific theory. For the \texttt{Theorems} entry
1626    the \texttt{id} part of the entry is given in the form
1627    \texttt{ids = [id,id,...]}. These \texttt{ids} may be theorem ids or special
1628    ids of the form \texttt{theorem.thmprefix*}.
1629    For example, the id
1630    \texttt{arithmetic.LESS\_EQ*} represents all theorems in
1631    theory \texttt{arithmetic} whose name starts with \texttt{LESS\_EQ}.
1632\end{description}
1633Options are name/value pairs. The value has to be quoted using
1634quotation marks or \HOL{}'s quotation syntax. There are the following
1635option names available:
1636\begin{description}
1637  \item[\texttt{content}]
1638    the content. For a term or type that's its \HOL{}\ definition.
1639    For theorems it is of the form \texttt{theory.theorem}.
1640  \item[\texttt{options}]
1641    formatting options for the munger as described in section~\ref{sec:munging-commands}.
1642    Please use the Holindex commands for typesetting inline or as a block instead of the options \texttt{tt} or \texttt{alltt}.
1643  \item[\texttt{label}] the label that will appear in the index. For
1644    theorems the label is by default its name and the label given here
1645    will be added after the name.
1646  \item[\texttt{comment}] \LaTeX{} code that gets typeset as a comment / description
1647    for long index entries.
1648  \item[\texttt{latex}] the \LaTeX{} code for the item. There are very rare cases,
1649    when it might be useful to provide handwritten \LaTeX{} code instead of the one
1650    generated by the munger. This option overrides the \LaTeX{} produced by the munger.
1651    It is recommended to use it very carefully.
1652\end{description}
1653Besides options, there are also boolean flags that change the formatting of entries:
1654\begin{description}
1655  \item[\texttt{force-index}]
1656    adds the entry to the index, even if it is not cited in the document.
1657  \item[\texttt{long-index}]
1658    use a long index-entry.
1659  \item[\texttt{short-index}]
1660    use a long index-entry.
1661\end{description}
1662Here is an example of such a \HOL{} definition file:
1663
1664\begin{verbatim}
1665@Term{term_id_1,
1666   content = ``SOME_FUN = SUC a < 0 /\ 0 > SUC b``,
1667   options = "width=20",
1668   label = "a short description of term from external file",
1669   comment = "some lengthy\\comment
1670
1671              with \textbf{formats} and newlines",
1672   force_index
1673}
1674
1675@Type{type_id_1,
1676   content = ``:bool``
1677}
1678
1679@Thm{arithmetic.LESS_SUCC_EQ_COR,
1680   force-index, long-index
1681}
1682
1683@Thm{thm_1,
1684   label = "(second instance)",
1685   content = "arithmetic.LESS_SUC_EQ_COR"
1686}
1687
1688@Theorems{
1689   ids = [arithmetic.LESS_ADD_SUC,
1690          arithmetic.LESS_EQ*],
1691   force-index
1692}
1693\end{verbatim}
1694
1695
1696\paragraph{Configuring Holindex}
1697
1698There are some commands that can be used to change the overall behaviour
1699of Holindex. They should be used in the header directly after \texttt{holindex}
1700is included.
1701\begin{description}
1702\item[\texttt{\bs{}setHOLlinewidth}] sets the default line-width. This
1703   corresponds to the \texttt{-w} option of the munger.
1704
1705\item[\texttt{\bs{}setHOLoverrides}] sets the ``overrides file'' to provide
1706token-to-\LaTeX{} replacements of what is pretty-printed.
1707
1708\item[\texttt{\bs{}useHOLfile}]
1709is used to include a \HOL{} definition file. Several such files might be
1710included.
1711\end{description}
1712
1713
1714\paragraph{Additional documentation}
1715For more information about Holindex, please refer to
1716the demonstration file \texttt{src/TeX/holindex-demo.tex}. This file contains
1717documentation for rarely used commands as well as explanations of how to
1718customise Holindex.
1719
1720
1721\subsection{\texorpdfstring{Making \HOL{} theories \LaTeX{}-ready}{Making HOL theories \LaTeX{}-ready}}
1722\label{sec:holtheories-tex-ready}
1723
1724Though one might specify all one's desired token-replacements in an \texttt{overrides} file, there is also support for specifying token replacements in the theory where tokens are first ``defined''.
1725%
1726(Of course, \emph{tokens} aren't defined \textit{per se}, but the definition of particular constants will naturally give rise to the generation of corresponding tokens when those constants appear in HOL terms, types or theorems.)
1727
1728A token's printing form is given in a script-file with the \ml{TeX\_notation} command (from the \ml{TexTokenMap} module).
1729%
1730This function has type
1731\begin{alltt}
1732   \{ hol : string, TeX : string * int \} -> unit
1733\end{alltt}
1734The \ml{hol} field specifies the string of the token as \HOL{} prints it.
1735%
1736The \ml{TeX} field specifies both the string that should be emitted into the \LaTeX{} output, and the width that this string should be considered to have (as in the \texttt{overrides} file).
1737
1738For example, in \texttt{boolScript.sml}, there are calls:
1739\begin{alltt}
1740   val _ = TeX_notation \{ hol = "!", TeX = ("\bs{}\bs{}HOLTokenForall\{\}", 1)\}
1741   val _ = TeX_notation \{ hol = UChar.forall,
1742                          TeX = ("\bs{}\bs{}HOLTokenForall\{\}", 1)\}
1743\end{alltt}
1744The \texttt{UChar} structure is a local binding in the script-file that points at the standard list of UTF8-encoded Unicode strings in the distribution (\ml{UnicodeChars}).
1745%
1746Note also how the backslashes that are necessary for the \LaTeX{} command have to be doubled because they are appearing in an SML string.
1747
1748Finally, rather than mapping the token directly to the string \texttt{\bs{}forall} as one might expect, the mapping introduces another level of indirection by mapping to \texttt{\bs{}HOLTokenForall}.
1749%
1750Bindings for this, and a number of other \LaTeX{} commands are made in the file
1751\begin{alltt}
1752   src/TeX/holtexbasic.sty
1753\end{alltt}
1754which will need to be included in the \LaTeX{} source file.
1755%
1756(Such bindings can be overridden with the use of the command \texttt{\bs{}renewcommand}.)
1757
1758Finally, all theory-bindings made with \ml{TeX\_notation} can be overridden with \texttt{overrides} files referenced at the time a munger is run.
1759
1760
1761\index{LaTeX@\LaTeX!embedding HOL@embedding in \HOL{}|)}
1762
1763%%% Local Variables:
1764%%% mode: latex
1765%%% TeX-master: "description"
1766%%% End:
1767
1768%  LocalWords:  HOL mechanisations HOLHEAP Holmakefile
1769