doc-src/Ref/theories.tex
author paulson
Mon, 12 Aug 1996 16:26:02 +0200
changeset 1905 81061bd61ad3
parent 1880 78c4b3ddba6c
child 3108 335efc3f5632
permissions -rw-r--r--
Added a new section on Definitions
     1 %% $Id$
     2 
     3 \chapter{Theories, Terms and Types} \label{theories}
     4 \index{theories|(}\index{signatures|bold}
     5 \index{reading!axioms|see{{\tt assume_ax}}}
     6 Theories organize the syntax, declarations and axioms of a mathematical
     7 development.  They are built, starting from the Pure theory, by extending
     8 and merging existing theories.  They have the \ML\ type \mltydx{theory}.
     9 Theory operations signal errors by raising exception \xdx{THEORY},
    10 returning a message and a list of theories.
    11 
    12 Signatures, which contain information about sorts, types, constants and
    13 syntax, have the \ML\ type~\mltydx{Sign.sg}.  For identification, each
    14 signature carries a unique list of \bfindex{stamps}, which are \ML\
    15 references to strings.  The strings serve as human-readable names; the
    16 references serve as unique identifiers.  Each primitive signature has a
    17 single stamp.  When two signatures are merged, their lists of stamps are
    18 also merged.  Every theory carries a unique signature.
    19 
    20 Terms and types are the underlying representation of logical syntax.  Their
    21 \ML\ definitions are irrelevant to naive Isabelle users.  Programmers who
    22 wish to extend Isabelle may need to know such details, say to code a tactic
    23 that looks for subgoals of a particular form.  Terms and types may be
    24 `certified' to be well-formed with respect to a given signature.
    25 
    26 
    27 \section{Defining theories}\label{sec:ref-defining-theories}
    28 
    29 Theories are usually defined using theory definition files (which have a name
    30 suffix {\tt .thy}). There is also a low level interface provided by certain
    31 \ML{} functions (see \S\ref{BuildingATheory}).
    32 Appendix~\ref{app:TheorySyntax} presents the concrete syntax for theory
    33 definitions; here is an explanation of the constituent parts:
    34 \begin{description}
    35 \item[{\it theoryDef}]
    36   is the full definition.  The new theory is called $id$.  It is the union
    37   of the named {\bf parent theories}\indexbold{theories!parent}, possibly
    38   extended with new classes, etc.  The basic theory, which contains only
    39   the meta-logic, is called \thydx{Pure}.
    40 
    41   Normally each {\it name\/} is an identifier, the name of the parent theory.
    42   Quoted strings can be used to document additional file dependencies; see
    43   \S\ref{LoadingTheories} for details.
    44 
    45 \item[$classes$]
    46   is a series of class declarations.  Declaring {\tt$id$ < $id@1$ \dots\
    47     $id@n$} makes $id$ a subclass of the existing classes $id@1\dots
    48   id@n$.  This rules out cyclic class structures.  Isabelle automatically
    49   computes the transitive closure of subclass hierarchies; it is not
    50   necessary to declare {\tt c < e} in addition to {\tt c < d} and {\tt d <
    51     e}.
    52 
    53 \item[$default$]
    54   introduces $sort$ as the new default sort for type variables.  This applies
    55   to unconstrained type variables in an input string but not to type
    56   variables created internally.  If omitted, the default sort is the listwise
    57   union of the default sorts of the parent theories (i.e.\ their logical
    58   intersection).
    59 
    60 \item[$sort$]
    61   is a finite set of classes.  A single class $id$ abbreviates the singleton
    62   set {\tt\{}$id${\tt\}}.
    63 
    64 \item[$types$]
    65   is a series of type declarations.  Each declares a new type constructor
    66   or type synonym.  An $n$-place type constructor is specified by
    67   $(\alpha@1,\dots,\alpha@n)name$, where the type variables serve only to
    68   indicate the number~$n$.
    69 
    70   A {\bf type synonym}\indexbold{type synonyms} is an abbreviation
    71   $(\alpha@1,\dots,\alpha@n)name = \tau$, where $name$ and $\tau$ can
    72   be strings.
    73 
    74 \item[$infix$]
    75   declares a type or constant to be an infix operator of priority $nat$
    76   associating to the left ({\tt infixl}) or right ({\tt infixr}). Only
    77   2-place type constructors can have infix status; an example is {\tt
    78   ('a,'b)~"*"~(infixr~20)}, which expresses binary product types.
    79 
    80 \item[$arities$]
    81   is a series of arity declarations.  Each assigns arities to type
    82   constructors.  The $name$ must be an existing type constructor, which is
    83   given the additional arity $arity$.
    84 
    85 \item[$constDecl$]
    86   is a series of constant declarations.  Each new constant $name$ is given
    87   the specified type.  The optional $mixfix$ annotations may
    88   attach concrete syntax to the constant. A variant of {\tt consts} is the
    89   {\tt syntax} section\index{*syntax section}, which adds just syntax without
    90   declaring logical constants.
    91 
    92 \item[$mixfix$] \index{mixfix declarations}
    93   annotations can take three forms:
    94   \begin{itemize}
    95   \item A mixfix template given as a $string$ of the form
    96     {\tt"}\dots{\tt\_}\dots{\tt\_}\dots{\tt"} where the $i$-th underscore
    97     indicates the position where the $i$-th argument should go.  The list
    98     of numbers gives the priority of each argument.  The final number gives
    99     the priority of the whole construct.
   100 
   101   \item A constant $f$ of type $\tau@1\To(\tau@2\To\tau)$ can be given {\bf
   102     infix} status.
   103 
   104   \item A constant $f$ of type $(\tau@1\To\tau@2)\To\tau$ can be given {\bf
   105     binder} status.  The declaration {\tt binder} $\cal Q$ $p$ causes
   106   ${\cal Q}\,x.F(x)$ to be treated
   107   like $f(F)$, where $p$ is the priority.
   108   \end{itemize}
   109 
   110 \item[$trans$]
   111   specifies syntactic translation rules (macros).  There are three forms:
   112   parse rules ({\tt =>}), print rules ({\tt <=}), and parse/print rules ({\tt
   113   ==}).
   114 
   115 \item[$rules$]
   116   is a series of rule declarations.  Each has a name $id$ and the formula is
   117   given by the $string$.  Rule names must be distinct within any single
   118   theory file.
   119 
   120 \item[$defs$] is a series of definitions.  They are just like $rules$, except
   121   that every $string$ must be a definition (see below for details).
   122 
   123 \item[$constdefs$] combines the declaration of constants and their
   124   definition. The first $string$ is the type, the second the definition.
   125 
   126 \item[$oracle$] links the theory to a trusted external reasoner.  It is
   127   allowed to create theorems, but each theorem carries a proof object
   128   describing the oracle invocation.  See \S\ref{sec:oracles} for details.
   129 
   130 \item[$ml$] \index{*ML section}
   131   consists of \ML\ code, typically for parse and print translation functions.
   132 \end{description}
   133 %
   134 Chapters~\ref{Defining-Logics} and \ref{chap:syntax} explain mixfix
   135 declarations, translation rules and the {\tt ML} section in more detail.
   136 
   137 
   138 \subsection{Definitions}\indexbold{definitions}
   139 
   140 {\bf Definitions} are intended to express abbreviations. The simplest form of
   141 a definition is $f \equiv t$, where $f$ is a constant. Isabelle also allows a
   142 derived form where the arguments of~$f$ appear on the left, abbreviating a
   143 string of $\lambda$-abstractions.
   144 
   145 Isabelle makes the following checks on definitions:
   146 \begin{itemize}
   147 \item Arguments (on the left-hand side) must be distinct variables
   148 \item All variables on the right-hand side must also appear on the left-hand
   149   side. 
   150 \item All type variables on the right-hand side must also appear on the
   151   left-hand side; this prohibits definitions such as {\tt zero == length []}.
   152 \item The definition must not be recursive.  Most object-logics provide
   153   definitional principles that can be used to express recursion safely.
   154 \end{itemize}
   155 These checks are intended to catch the sort of errors that might be made
   156 accidentally.  Misspellings, for instance, might result in additional
   157 variables appearing on the right-hand side.  More elaborate checks could be
   158 made, but the cost might be overly strict rules on declaration order, etc.
   159 
   160 
   161 \subsection{*Classes and arities}
   162 \index{classes!context conditions}\index{arities!context conditions}
   163 
   164 In order to guarantee principal types~\cite{nipkow-prehofer},
   165 arity declarations must obey two conditions:
   166 \begin{itemize}
   167 \item There must be no two declarations $ty :: (\vec{r})c$ and $ty ::
   168   (\vec{s})c$ with $\vec{r} \neq \vec{s}$.  For example, the following is
   169   forbidden:
   170 \begin{ttbox}
   171 types
   172   'a ty
   173 arities
   174   ty :: ({\ttlbrace}logic{\ttrbrace}) logic
   175   ty :: ({\ttlbrace}{\ttrbrace})logic
   176 \end{ttbox}
   177 
   178 \item If there are two declarations $ty :: (s@1,\dots,s@n)c$ and $ty ::
   179   (s@1',\dots,s@n')c'$ such that $c' < c$ then $s@i' \preceq s@i$ must hold
   180   for $i=1,\dots,n$.  The relationship $\preceq$, defined as
   181 \[ s' \preceq s \iff \forall c\in s. \exists c'\in s'.~ c'\le c, \]
   182 expresses that the set of types represented by $s'$ is a subset of the set of
   183 types represented by $s$.  For example, the following is forbidden:
   184 \begin{ttbox}
   185 classes
   186   term < logic
   187 types
   188   'a ty
   189 arities
   190   ty :: ({\ttlbrace}logic{\ttrbrace})logic
   191   ty :: ({\ttlbrace}{\ttrbrace})term
   192 \end{ttbox}
   193 
   194 \end{itemize}
   195 
   196 
   197 \section{Loading a new theory}\label{LoadingTheories}
   198 \index{theories!loading}\index{files!reading}
   199 \begin{ttbox}
   200 use_thy         : string -> unit
   201 time_use_thy    : string -> unit
   202 loadpath        : string list ref \hfill{\bf initially {\tt["."]}}
   203 delete_tmpfiles : bool ref \hfill{\bf initially true}
   204 \end{ttbox}
   205 
   206 \begin{ttdescription}
   207 \item[\ttindexbold{use_thy} $thyname$]
   208   reads the theory $thyname$ and creates an \ML{} structure as described below.
   209 
   210 \item[\ttindexbold{time_use_thy} $thyname$]
   211   calls {\tt use_thy} $thyname$ and reports the time taken.
   212 
   213 \item[\ttindexbold{loadpath}]
   214   contains a list of directories to search when locating the files that
   215   define a theory.  This list is only used if the theory name in {\tt
   216     use_thy} does not specify the path explicitly.
   217 
   218 \item[\ttindexbold{delete_tmpfiles} := false;]
   219 suppresses the deletion of temporary files.
   220 \end{ttdescription}
   221 %
   222 Each theory definition must reside in a separate file.  Let the file {\it
   223   T}{\tt.thy} contain the definition of a theory called~$T$, whose parent
   224 theories are $TB@1$ \dots $TB@n$.  Calling \ttindexbold{use_thy}~{\tt"{\it
   225   T\/}"} reads the file {\it T}{\tt.thy}, writes a temporary \ML{}
   226 file {\tt.{\it T}.thy.ML}, and reads the latter file.  Recursive {\tt
   227   use_thy} calls load those parent theories that have not been loaded
   228 previously; the recursive calls may continue to any depth.  One {\tt use_thy}
   229 call can read an entire logic provided all theories are linked appropriately.
   230 
   231 The result is an \ML\ structure~$T$ containing at least a component {\tt thy}
   232 for the new theory and components for each of the rules.  The structure also
   233 contains the definitions of the {\tt ML} section, if present.  The file
   234 {\tt.{\it T}.thy.ML} is then deleted if {\tt delete_tmpfiles} is set to {\tt
   235 true} and no errors occurred.
   236 
   237 Finally the file {\it T}{\tt.ML} is read, if it exists.  This file normally
   238 begins with the declaration {\tt open~$T$} and contains proofs involving
   239 the new theory.
   240 
   241 Some applications construct theories directly by calling \ML\ functions.  In
   242 this situation there is no {\tt.thy} file, only an {\tt.ML} file.  The
   243 {\tt.ML} file must declare an \ML\ structure having the theory's name and a
   244 component {\tt thy} containing the new theory object.
   245 Section~\ref{sec:pseudo-theories} below describes a way of linking such
   246 theories to their parents.
   247 
   248 \begin{warn}
   249   Temporary files are written to the current directory, so this must be
   250   writable.  Isabelle inherits the current directory from the operating
   251   system; you can change it within Isabelle by typing {\tt
   252   cd"$dir$"}\index{*cd}.
   253 \end{warn}
   254 
   255 
   256 \section{Reloading modified theories}\label{sec:reloading-theories}
   257 \indexbold{theories!reloading}
   258 \begin{ttbox}
   259 update     : unit -> unit
   260 unlink_thy : string -> unit
   261 \end{ttbox}
   262 Changing a theory on disk often makes it necessary to reload all theories
   263 descended from it.  However, {\tt use_thy} reads only one theory, even if
   264 some of the parent theories are out of date.  In this case you should call
   265 {\tt update()}.
   266 
   267 Isabelle keeps track of all loaded theories and their files.  If
   268 \ttindex{use_thy} finds that the theory to be loaded has been read before,
   269 it determines whether to reload the theory as follows.  First it looks for
   270 the theory's files in their previous location.  If it finds them, it
   271 compares their modification times to the internal data and stops if they
   272 are equal.  If the files have been moved, {\tt use_thy} searches for them
   273 as it would for a new theory.  After {\tt use_thy} reloads a theory, it
   274 marks the children as out-of-date.
   275 
   276 \begin{ttdescription}
   277 \item[\ttindexbold{update}()]
   278   reloads all modified theories and their descendants in the correct order.
   279 
   280 \item[\ttindexbold{unlink_thy} $thyname$]\indexbold{theories!removing}
   281   informs Isabelle that theory $thyname$ no longer exists.  If you delete the
   282   theory files for $thyname$ then you must execute {\tt unlink_thy};
   283   otherwise {\tt update} will complain about a missing file.
   284 \end{ttdescription}
   285 
   286 
   287 \goodbreak
   288 \subsection{Important note for Poly/ML users}\index{Poly/{\ML} compiler}
   289 The theory mechanism depends upon reference variables.  At the end of a
   290 Poly/\ML{} session, the contents of references are lost unless they are
   291 declared in the current database.  In particular, assignments to references
   292 of the {\tt Pure} database are lost, including all information about loaded
   293 theories. To avoid losing this information simply call
   294 \begin{ttbox}
   295 init_thy_reader();
   296 \end{ttbox}
   297 when building the new database.
   298 
   299 
   300 \subsection{*Pseudo theories}\label{sec:pseudo-theories}
   301 \indexbold{theories!pseudo}%
   302 Any automatic reloading facility requires complete knowledge of all
   303 dependencies.  Sometimes theories depend on objects created in \ML{} files
   304 with no associated theory definition file.  These objects may be theories but
   305 they could also be theorems, proof procedures, etc.
   306 
   307 Unless such dependencies are documented, {\tt update} fails to reload these
   308 \ML{} files and the system is left in a state where some objects, such as
   309 theorems, still refer to old versions of theories.  This may lead to the
   310 error
   311 \begin{ttbox}
   312 Attempt to merge different versions of theories: \dots
   313 \end{ttbox}
   314 Therefore there is a way to link theories and {\bf orphaned} \ML{} files ---
   315 those not associated with a theory definition.
   316 
   317 Let us assume we have an orphaned \ML{} file named {\tt orphan.ML} and a
   318 theory~$B$ that depends on {\tt orphan.ML} --- for example, {\tt B.ML} uses
   319 theorems proved in {\tt orphan.ML}.  Then {\tt B.thy} should
   320 mention this dependency as follows:
   321 \begin{ttbox}
   322 B = \(\ldots\) + "orphan" + \(\ldots\)
   323 \end{ttbox}
   324 Quoted strings stand for theories which have to be loaded before the
   325 current theory is read but which are not used in building the base of
   326 theory~$B$. Whenever {\tt orphan} changes and is reloaded, Isabelle
   327 knows that $B$ has to be updated, too.
   328 
   329 Note that it's necessary for {\tt orphan} to declare a special ML
   330 object of type {\tt theory} which is present in all theories. This is
   331 normally achieved by adding the file {\tt orphan.thy} to make {\tt
   332 orphan} a {\bf pseudo theory}. A minimum version of {\tt orphan.thy}
   333 would be
   334 
   335 \begin{ttbox}
   336 orphan = Pure
   337 \end{ttbox}
   338 
   339 which uses {\tt Pure} to make a dummy theory. Normally though the
   340 orphaned file has its own dependencies.  If {\tt orphan.ML} depends on
   341 theories or files $A@1$, \ldots, $A@n$, record this by creating the
   342 pseudo theory in the following way:
   343 \begin{ttbox}
   344 orphan = \(A@1\) + \(\ldots\) + \(A@n\)
   345 \end{ttbox}
   346 The resulting theory ensures that {\tt update} reloads {\tt orphan}
   347 whenever it reloads one of the $A@i$.
   348 
   349 For an extensive example of how this technique can be used to link lots of
   350 theory files and load them by just a few {\tt use_thy} calls, consult the
   351 sources of \ZF{} set theory.
   352 
   353 
   354 
   355 \section{Basic operations on theories}\label{BasicOperationsOnTheories}
   356 \subsection{Extracting an axiom or theorem from a theory}
   357 \index{theories!axioms of}\index{axioms!extracting}
   358 \index{theories!theorems of}\index{theorems!extracting}
   359 \begin{ttbox}
   360 get_axiom : theory -> string -> thm
   361 get_thm   : theory -> string -> thm
   362 assume_ax : theory -> string -> thm
   363 \end{ttbox}
   364 \begin{ttdescription}
   365 \item[\ttindexbold{get_axiom} $thy$ $name$]
   366   returns an axiom with the given $name$ from $thy$, raising exception
   367   \xdx{THEORY} if none exists.  Merging theories can cause several axioms
   368   to have the same name; {\tt get_axiom} returns an arbitrary one.
   369 
   370 \item[\ttindexbold{get_thm} $thy$ $name$]
   371   is analogous to {\tt get_axiom}, but looks for a stored theorem. Like
   372   {\tt get_axiom} it searches all parents of a theory if the theorem
   373   is not associated with $thy$.
   374 
   375 \item[\ttindexbold{assume_ax} $thy$ $formula$]
   376   reads the {\it formula} using the syntax of $thy$, following the same
   377   conventions as axioms in a theory definition.  You can thus pretend that
   378   {\it formula} is an axiom and use the resulting theorem like an axiom.
   379   Actually {\tt assume_ax} returns an assumption;  \ttindex{result}
   380   complains about additional assumptions, but \ttindex{uresult} does not.
   381 
   382 For example, if {\it formula} is
   383 \hbox{\tt a=b ==> b=a} then the resulting theorem has the form
   384 \hbox{\verb'?a=?b ==> ?b=?a  [!!a b. a=b ==> b=a]'}
   385 \end{ttdescription}
   386 
   387 \subsection{Building a theory}
   388 \label{BuildingATheory}
   389 \index{theories!constructing|bold}
   390 \begin{ttbox}
   391 pure_thy       : theory
   392 merge_theories : theory * theory -> theory
   393 \end{ttbox}
   394 \begin{ttdescription}
   395 \item[\ttindexbold{pure_thy}] contains just the syntax and signature
   396   of the meta-logic.  There are no axioms: meta-level inferences are carried
   397   out by \ML\ functions.
   398 \item[\ttindexbold{merge_theories} ($thy@1$, $thy@2$)] merges the two
   399   theories $thy@1$ and $thy@2$.  The resulting theory contains all of the
   400   syntax, signature and axioms of the constituent theories. Merging theories
   401   that contain different identification stamps of the same name fails with
   402   the following message
   403 \begin{ttbox}
   404 Attempt to merge different versions of theories: "\(T@1\)", \(\ldots\), "\(T@n\)"
   405 \end{ttbox}
   406   This error may especially occur when a theory is redeclared --- say to
   407   change an incorrect axiom --- and bindings to old versions persist.
   408   Isabelle ensures that old and new theories of the same name are not
   409   involved in a proof.
   410 
   411 %% FIXME
   412 %\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"} $\cdots$] extends
   413 %  the theory $thy$ with new types, constants, etc.  $T$ identifies the theory
   414 %  internally.  When a theory is redeclared, say to change an incorrect axiom,
   415 %  bindings to the old axiom may persist.  Isabelle ensures that the old and
   416 %  new theories are not involved in the same proof.  Attempting to combine
   417 %  different theories having the same name $T$ yields the fatal error
   418 %extend_theory  : theory -> string -> \(\cdots\) -> theory
   419 %\begin{ttbox}
   420 %Attempt to merge different versions of theory: \(T\)
   421 %\end{ttbox}
   422 \end{ttdescription}
   423 
   424 %% FIXME
   425 %\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"}
   426 %      ($classes$, $default$, $types$, $arities$, $consts$, $sextopt$) $rules$]
   427 %\hfill\break   %%% include if line is just too short
   428 %is the \ML{} equivalent of the following theory definition:
   429 %\begin{ttbox}
   430 %\(T\) = \(thy\) +
   431 %classes \(c\) < \(c@1\),\(\dots\),\(c@m\)
   432 %        \dots
   433 %default {\(d@1,\dots,d@r\)}
   434 %types   \(tycon@1\),\dots,\(tycon@i\) \(n\)
   435 %        \dots
   436 %arities \(tycon@1'\),\dots,\(tycon@j'\) :: (\(s@1\),\dots,\(s@n\))\(c\)
   437 %        \dots
   438 %consts  \(b@1\),\dots,\(b@k\) :: \(\tau\)
   439 %        \dots
   440 %rules   \(name\) \(rule\)
   441 %        \dots
   442 %end
   443 %\end{ttbox}
   444 %where
   445 %\begin{tabular}[t]{l@{~=~}l}
   446 %$classes$ & \tt[("$c$",["$c@1$",\dots,"$c@m$"]),\dots] \\
   447 %$default$ & \tt["$d@1$",\dots,"$d@r$"]\\
   448 %$types$   & \tt[([$tycon@1$,\dots,$tycon@i$], $n$),\dots] \\
   449 %$arities$ & \tt[([$tycon'@1$,\dots,$tycon'@j$], ([$s@1$,\dots,$s@n$],$c$)),\dots]
   450 %\\
   451 %$consts$  & \tt[([$b@1$,\dots,$b@k$],$\tau$),\dots] \\
   452 %$rules$   & \tt[("$name$",$rule$),\dots]
   453 %\end{tabular}
   454 
   455 
   456 \subsection{Inspecting a theory}\label{sec:inspct-thy}
   457 \index{theories!inspecting|bold}
   458 \begin{ttbox}
   459 print_theory  : theory -> unit
   460 axioms_of     : theory -> (string * thm) list
   461 thms_of       : theory -> (string * thm) list
   462 parents_of    : theory -> theory list
   463 sign_of       : theory -> Sign.sg
   464 stamps_of_thy : theory -> string ref list
   465 \end{ttbox}
   466 These provide means of viewing a theory's components.
   467 \begin{ttdescription}
   468 \item[\ttindexbold{print_theory} $thy$]
   469 prints the contents of $thy$ excluding the syntax related parts (which are
   470 shown by {\tt print_syntax}).  The output is quite verbose.
   471 
   472 \item[\ttindexbold{axioms_of} $thy$]
   473 returns the additional axioms of the most recent extend node of~$thy$.
   474 
   475 \item[\ttindexbold{thms_of} $thy$]
   476 returns all theorems that are associated with $thy$.
   477 
   478 \item[\ttindexbold{parents_of} $thy$]
   479 returns the direct ancestors of~$thy$.
   480 
   481 \item[\ttindexbold{sign_of} $thy$]
   482 returns the signature associated with~$thy$.  It is useful with functions
   483 like {\tt read_instantiate_sg}, which take a signature as an argument.
   484 
   485 \item[\ttindexbold{stamps_of_thy} $thy$]\index{signatures}
   486 returns the identification \rmindex{stamps} of the signature associated
   487 with~$thy$.
   488 \end{ttdescription}
   489 
   490 
   491 \section{Generating HTML documents}
   492 \index{HTML|bold} 
   493 
   494 Isabelle is able to make HTML documents that show a theory's
   495 definition, the theorems proved in its ML file and the relationship
   496 with its ancestors and descendants. HTML stands for Hypertext Markup
   497 Language and is used in the World Wide Web to represent text
   498 containing images and links to other documents. Web browsers like
   499 {\tt xmosaic} or {\tt netscape} are used to view these documents.
   500 
   501 Besides the three HTML files that are made for every theory
   502 (definition and theorems, ancestors, descendants), Isabelle stores
   503 links to all theories in an index file. These indexes are themself
   504 linked with other indexes to represent the hierarchic structure of
   505 Isabelle's logics.
   506 
   507 To make HTML files for logics that are part of the Isabelle
   508 distribution, simply set the shell environment variable {\tt
   509 MAKE_HTML} before compiling a logic. This works for single logics as
   510 well as for the shell script {\tt make-all} (see
   511 \ref{sec:shell-scripts}). To make HTML files for {\tt FOL} using a
   512 {\tt csh} style shell, the following commands can be used:
   513 
   514 \begin{ttbox}
   515 cd FOL
   516 setenv MAKE_HTML
   517 make
   518 \end{ttbox}
   519 
   520 The databases made this way do not differ from the ones made with an
   521 unset {\tt MAKE_HTML}; in particular no HTML files are written if the
   522 database is used to manually load a theory.
   523 
   524 As you will see below, the HTML generation is controlled by a boolean
   525 reference variable. If you want to make databases which define this
   526 variable's value as {\tt true} and where therefore HTML files are
   527 written each time {\tt use_thy} is invoked, you have to set {\tt
   528 MAKE_HTML} to ``{\tt true}'':
   529 
   530 \begin{ttbox}
   531 cd FOL
   532 setenv MAKE_HTML true
   533 make
   534 \end{ttbox}
   535 
   536 All theories loaded from within the {\tt FOL} database and all
   537 databases derived from it will now cause HTML files to be written.
   538 This behaviour can be changed by assigning a value of {\tt false} to
   539 the boolean reference variable {\tt make_html}. Be careful when making
   540 such databases publicly available since it means that your users will
   541 generate HTML files though they might not intend to do so.
   542 
   543 As some of Isabelle's logics are based on others (e.g. {\tt ZF} on
   544 {\tt FOL}) and because the HTML files list a theory's ancestors, you
   545 should not make HTML files for a logic if the HTML files for the base
   546 logic do not exist. Otherwise some of the hypertext links might point
   547 to non-existing documents.
   548 
   549 The entry point to all logics is the {\tt index.html} file located in
   550 Isabelle's main directory. You can also access a HTML version of the
   551 distribution package at
   552 
   553 \begin{ttbox}
   554 http://www4.informatik.tu-muenchen.de/~nipkow/isabelle
   555 \end{ttbox}
   556 
   557 
   558 \subsection*{Manual HTML generation}
   559 
   560 To manually control the generation of HTML files the following
   561 commands and reference variables are used:
   562 
   563 \begin{ttbox}
   564 init_html   : unit -> unit
   565 make_html   : bool ref
   566 finish_html : unit -> unit
   567 \end{ttbox}
   568 
   569 \begin{ttdescription}
   570 \item[\ttindexbold{init_html}]
   571 activates the HTML facility. It stores the current working directory
   572 as the place where the {\tt index.html} file for all theories loaded
   573 afterwards will be stored.
   574 
   575 \item[\ttindexbold{make_html}]
   576 is a boolean reference variable read by {\tt use_thy} and other
   577 functions to decide whether HTML files should be made. After you have
   578 used {\tt init_html} you can manually change {\tt make_html}'s value
   579 to temporarily disable HTML generation.
   580 
   581 \item[\ttindexbold{finish_html}]
   582 has to be called after all theories have been read that should be
   583 listed in the current {\tt index.html} file. It reads a temporary
   584 file with information about the theories read since the last use of
   585 {\tt init_html} and makes the {\tt index.html} file. If {\tt
   586 make_html} is {\tt false} nothing is done.
   587 
   588 The indexes made by this function also contain a link to the {\tt
   589 README} file if there exists one in the directory where the index is
   590 stored. If there's a {\tt README.html} file it is used instead of
   591 {\tt README}.
   592 
   593 \end{ttdescription}
   594 
   595 The above functions could be used in the following way:
   596 
   597 \begin{ttbox}
   598 init_html();
   599 {\out Setting path for index.html to "/home/clasohm/isabelle/HOL"}
   600 use_thy "List";
   601 \dots
   602 finish_html();
   603 \end{ttbox}
   604 
   605 Please note that HTML files are made only for those theories that are
   606 read while {\tt make_html} is {\tt true}. These files may contain
   607 links to theories that were read with a {\tt false} {\tt make_html}
   608 and therefore point to non-existing files.
   609 
   610 
   611 \subsection*{Extending or adding a logic}
   612 
   613 If you add a new subdirectory to Isabelle's logics (or add a completly
   614 new logic), you would have to call {\tt init_html} at the start of every
   615 directory's {\tt ROOT.ML} file and {\tt finish_html} at the end of
   616 it. This is automatically done if you use
   617 
   618 \begin{ttbox}\index{use_dir}
   619 use_dir : string -> unit
   620 \end{ttbox}
   621 
   622 This function takes a path as its parameter, changes the working
   623 directory, calls {\tt init_html} if {\tt make_html} is {\tt true},
   624 executes {\tt ROOT.ML}, and calls {\tt finish_html}. The {\tt
   625 index.html} file written in this directory will be automatically
   626 linked to the first index found in the (recursively searched)
   627 superdirectories.
   628 
   629 Instead of adding something like
   630 
   631 \begin{ttbox}
   632 use"ex/ROOT.ML";
   633 \end{ttbox}
   634 
   635 to the logic's makefile you have to use this:
   636 
   637 \begin{ttbox}
   638 use_dir"ex";
   639 \end{ttbox}
   640 
   641 Since {\tt use_dir} calls {\tt init_html} only if {\tt make_html} is
   642 {\tt true} the generation of HTML files depends on the value this
   643 reference variable has. It can either be inherited from the used \ML{}
   644 database or set in the makefile before {\tt use_dir} is invoked,
   645 e.g. to set it's value according to the environment variable {\tt
   646 MAKE_HTML}.
   647 
   648 The generated HTML files contain all theorems that were proved in the
   649 theory's \ML{} file with {\tt qed}, {\tt qed_goal} and {\tt qed_goalw},
   650 or stored with {\tt bind_thm} and {\tt store_thm}. Additionally there
   651 is a hypertext link to the whole \ML{} file.
   652 
   653 You can add section headings to the list of theorems by using
   654 
   655 \begin{ttbox}\index{use_dir}
   656 section: string -> unit
   657 \end{ttbox}
   658 
   659 in a theory's ML file, which converts a plain string to a HTML
   660 heading and inserts it before the next theorem proved or stored with
   661 one of the above functions. If {\tt make_html} is {\tt false} nothing
   662 is done.
   663 
   664 
   665 \subsection*{Using someone else's database}
   666 
   667 To make them independent from their storage place, the HTML files only
   668 contain relative paths which are derived from absolute ones like the
   669 current working directory, {\tt gif_path} or {\tt base_path}. The
   670 latter two are reference variables which are initialized at the time
   671 when the {\tt Pure} database is made. Because you need write access
   672 for the current directory to make HTML files and therefore (probably)
   673 generate them in your home directory, the absolute {\tt base_path} is
   674 not correct if you use someone else's database or a database derived
   675 from it.
   676 
   677 In that case you first should set {\tt base_path} to the value of {\em
   678 your} Isabelle main directory, i.e. the directory that contains the
   679 subdirectories where standard logics like {\tt FOL} and {\tt HOL} or
   680 your own logics are stored. If you do not do this, the generated HTML
   681 files will still be usable but may contain incomplete titles and lack
   682 some hypertext links.
   683 
   684 It's also a good idea to set {\tt gif_path} which points to the
   685 directory containing two GIF images used in the HTML
   686 documents. Normally this is the {\tt Tools} subdirectory of Isabelle's
   687 main directory. While its value in general is still valid, your HTML
   688 files would depend on files not owned by you. This prevents you from
   689 changing the location of the HTML files (as they contain relative
   690 paths) and also causes trouble if the database's maker (re)moves the
   691 GIFs.
   692 
   693 Here's what you should do before invoking {\tt init_html} using
   694 someone else's \ML{} database:
   695 
   696 \begin{ttbox}
   697 base_path := "/home/smith/isabelle";
   698 gif_path := "/home/smith/isabelle/Tools";
   699 init_html();
   700 \dots
   701 \end{ttbox}
   702 
   703 \section{Terms}
   704 \index{terms|bold}
   705 Terms belong to the \ML\ type \mltydx{term}, which is a concrete datatype
   706 with six constructors: there are six kinds of term.
   707 \begin{ttbox}
   708 type indexname = string * int;
   709 infix 9 $;
   710 datatype term = Const of string * typ
   711               | Free  of string * typ
   712               | Var   of indexname * typ
   713               | Bound of int
   714               | Abs   of string * typ * term
   715               | op $  of term * term;
   716 \end{ttbox}
   717 \begin{ttdescription}
   718 \item[\ttindexbold{Const}($a$, $T$)] \index{constants|bold}
   719   is the {\bf constant} with name~$a$ and type~$T$.  Constants include
   720   connectives like $\land$ and $\forall$ as well as constants like~0
   721   and~$Suc$.  Other constants may be required to define a logic's concrete
   722   syntax.
   723 
   724 \item[\ttindexbold{Free}($a$, $T$)] \index{variables!free|bold}
   725   is the {\bf free variable} with name~$a$ and type~$T$.
   726 
   727 \item[\ttindexbold{Var}($v$, $T$)] \index{unknowns|bold}
   728   is the {\bf scheme variable} with indexname~$v$ and type~$T$.  An
   729   \mltydx{indexname} is a string paired with a non-negative index, or
   730   subscript; a term's scheme variables can be systematically renamed by
   731   incrementing their subscripts.  Scheme variables are essentially free
   732   variables, but may be instantiated during unification.
   733 
   734 \item[\ttindexbold{Bound} $i$] \index{variables!bound|bold}
   735   is the {\bf bound variable} with de Bruijn index~$i$, which counts the
   736   number of lambdas, starting from zero, between a variable's occurrence
   737   and its binding.  The representation prevents capture of variables.  For
   738   more information see de Bruijn \cite{debruijn72} or
   739   Paulson~\cite[page~336]{paulson91}.
   740 
   741 \item[\ttindexbold{Abs}($a$, $T$, $u$)]
   742   \index{lambda abs@$\lambda$-abstractions|bold}
   743   is the $\lambda$-{\bf abstraction} with body~$u$, and whose bound
   744   variable has name~$a$ and type~$T$.  The name is used only for parsing
   745   and printing; it has no logical significance.
   746 
   747 \item[$t$ \$ $u$] \index{$@{\tt\$}|bold} \index{function applications|bold}
   748 is the {\bf application} of~$t$ to~$u$.
   749 \end{ttdescription}
   750 Application is written as an infix operator to aid readability.
   751 Here is an \ML\ pattern to recognize \FOL{} formulae of
   752 the form~$A\imp B$, binding the subformulae to~$A$ and~$B$:
   753 \begin{ttbox}
   754 Const("Trueprop",_) $ (Const("op -->",_) $ A $ B)
   755 \end{ttbox}
   756 
   757 
   758 \section{Variable binding}
   759 \begin{ttbox}
   760 loose_bnos     : term -> int list
   761 incr_boundvars : int -> term -> term
   762 abstract_over  : term*term -> term
   763 variant_abs    : string * typ * term -> string * term
   764 aconv          : term*term -> bool\hfill{\bf infix}
   765 \end{ttbox}
   766 These functions are all concerned with the de Bruijn representation of
   767 bound variables.
   768 \begin{ttdescription}
   769 \item[\ttindexbold{loose_bnos} $t$]
   770   returns the list of all dangling bound variable references.  In
   771   particular, {\tt Bound~0} is loose unless it is enclosed in an
   772   abstraction.  Similarly {\tt Bound~1} is loose unless it is enclosed in
   773   at least two abstractions; if enclosed in just one, the list will contain
   774   the number 0.  A well-formed term does not contain any loose variables.
   775 
   776 \item[\ttindexbold{incr_boundvars} $j$]
   777   increases a term's dangling bound variables by the offset~$j$.  This is
   778   required when moving a subterm into a context where it is enclosed by a
   779   different number of abstractions.  Bound variables with a matching
   780   abstraction are unaffected.
   781 
   782 \item[\ttindexbold{abstract_over} $(v,t)$]
   783   forms the abstraction of~$t$ over~$v$, which may be any well-formed term.
   784   It replaces every occurrence of \(v\) by a {\tt Bound} variable with the
   785   correct index.
   786 
   787 \item[\ttindexbold{variant_abs} $(a,T,u)$]
   788   substitutes into $u$, which should be the body of an abstraction.
   789   It replaces each occurrence of the outermost bound variable by a free
   790   variable.  The free variable has type~$T$ and its name is a variant
   791   of~$a$ chosen to be distinct from all constants and from all variables
   792   free in~$u$.
   793 
   794 \item[$t$ \ttindexbold{aconv} $u$]
   795   tests whether terms~$t$ and~$u$ are \(\alpha\)-convertible: identical up
   796   to renaming of bound variables.
   797 \begin{itemize}
   798   \item
   799     Two constants, {\tt Free}s, or {\tt Var}s are \(\alpha\)-convertible
   800     if their names and types are equal.
   801     (Variables having the same name but different types are thus distinct.
   802     This confusing situation should be avoided!)
   803   \item
   804     Two bound variables are \(\alpha\)-convertible
   805     if they have the same number.
   806   \item
   807     Two abstractions are \(\alpha\)-convertible
   808     if their bodies are, and their bound variables have the same type.
   809   \item
   810     Two applications are \(\alpha\)-convertible
   811     if the corresponding subterms are.
   812 \end{itemize}
   813 
   814 \end{ttdescription}
   815 
   816 \section{Certified terms}\index{terms!certified|bold}\index{signatures}
   817 A term $t$ can be {\bf certified} under a signature to ensure that every type
   818 in~$t$ is well-formed and every constant in~$t$ is a type instance of a
   819 constant declared in the signature.  The term must be well-typed and its use
   820 of bound variables must be well-formed.  Meta-rules such as {\tt forall_elim}
   821 take certified terms as arguments.
   822 
   823 Certified terms belong to the abstract type \mltydx{cterm}.
   824 Elements of the type can only be created through the certification process.
   825 In case of error, Isabelle raises exception~\ttindex{TERM}\@.
   826 
   827 \subsection{Printing terms}
   828 \index{terms!printing of}
   829 \begin{ttbox}
   830      string_of_cterm :           cterm -> string
   831 Sign.string_of_term  : Sign.sg -> term -> string
   832 \end{ttbox}
   833 \begin{ttdescription}
   834 \item[\ttindexbold{string_of_cterm} $ct$]
   835 displays $ct$ as a string.
   836 
   837 \item[\ttindexbold{Sign.string_of_term} $sign$ $t$]
   838 displays $t$ as a string, using the syntax of~$sign$.
   839 \end{ttdescription}
   840 
   841 \subsection{Making and inspecting certified terms}
   842 \begin{ttbox}
   843 cterm_of   : Sign.sg -> term -> cterm
   844 read_cterm : Sign.sg -> string * typ -> cterm
   845 cert_axm   : Sign.sg -> string * term -> string * term
   846 read_axm   : Sign.sg -> string * string -> string * term
   847 rep_cterm  : cterm -> \{T:typ, t:term, sign:Sign.sg, maxidx:int\}
   848 \end{ttbox}
   849 \begin{ttdescription}
   850 \item[\ttindexbold{cterm_of} $sign$ $t$] \index{signatures}
   851 certifies $t$ with respect to signature~$sign$.
   852 
   853 \item[\ttindexbold{read_cterm} $sign$ ($s$, $T$)]
   854 reads the string~$s$ using the syntax of~$sign$, creating a certified term.
   855 The term is checked to have type~$T$; this type also tells the parser what
   856 kind of phrase to parse.
   857 
   858 \item[\ttindexbold{cert_axm} $sign$ ($name$, $t$)]
   859 certifies $t$ with respect to $sign$ as a meta-proposition and converts all
   860 exceptions to an error, including the final message
   861 \begin{ttbox}
   862 The error(s) above occurred in axiom "\(name\)"
   863 \end{ttbox}
   864 
   865 \item[\ttindexbold{read_axm} $sign$ ($name$, $s$)]
   866 similar to {\tt cert_axm}, but first reads the string $s$ using the syntax of
   867 $sign$.
   868 
   869 \item[\ttindexbold{rep_cterm} $ct$]
   870 decomposes $ct$ as a record containing its type, the term itself, its
   871 signature, and the maximum subscript of its unknowns.  The type and maximum
   872 subscript are computed during certification.
   873 \end{ttdescription}
   874 
   875 
   876 \section{Types}\index{types|bold}
   877 Types belong to the \ML\ type \mltydx{typ}, which is a concrete datatype with
   878 three constructor functions.  These correspond to type constructors, free
   879 type variables and schematic type variables.  Types are classified by sorts,
   880 which are lists of classes (representing an intersection).  A class is
   881 represented by a string.
   882 \begin{ttbox}
   883 type class = string;
   884 type sort  = class list;
   885 
   886 datatype typ = Type  of string * typ list
   887              | TFree of string * sort
   888              | TVar  of indexname * sort;
   889 
   890 infixr 5 -->;
   891 fun S --> T = Type ("fun", [S, T]);
   892 \end{ttbox}
   893 \begin{ttdescription}
   894 \item[\ttindexbold{Type}($a$, $Ts$)] \index{type constructors|bold}
   895   applies the {\bf type constructor} named~$a$ to the type operands~$Ts$.
   896   Type constructors include~\tydx{fun}, the binary function space
   897   constructor, as well as nullary type constructors such as~\tydx{prop}.
   898   Other type constructors may be introduced.  In expressions, but not in
   899   patterns, \hbox{\tt$S$-->$T$} is a convenient shorthand for function
   900   types.
   901 
   902 \item[\ttindexbold{TFree}($a$, $s$)] \index{type variables|bold}
   903   is the {\bf type variable} with name~$a$ and sort~$s$.
   904 
   905 \item[\ttindexbold{TVar}($v$, $s$)] \index{type unknowns|bold}
   906   is the {\bf type unknown} with indexname~$v$ and sort~$s$.
   907   Type unknowns are essentially free type variables, but may be
   908   instantiated during unification.
   909 \end{ttdescription}
   910 
   911 
   912 \section{Certified types}
   913 \index{types!certified|bold}
   914 Certified types, which are analogous to certified terms, have type
   915 \ttindexbold{ctyp}.
   916 
   917 \subsection{Printing types}
   918 \index{types!printing of}
   919 \begin{ttbox}
   920      string_of_ctyp :           ctyp -> string
   921 Sign.string_of_typ  : Sign.sg -> typ -> string
   922 \end{ttbox}
   923 \begin{ttdescription}
   924 \item[\ttindexbold{string_of_ctyp} $cT$]
   925 displays $cT$ as a string.
   926 
   927 \item[\ttindexbold{Sign.string_of_typ} $sign$ $T$]
   928 displays $T$ as a string, using the syntax of~$sign$.
   929 \end{ttdescription}
   930 
   931 
   932 \subsection{Making and inspecting certified types}
   933 \begin{ttbox}
   934 ctyp_of  : Sign.sg -> typ -> ctyp
   935 rep_ctyp : ctyp -> \{T: typ, sign: Sign.sg\}
   936 \end{ttbox}
   937 \begin{ttdescription}
   938 \item[\ttindexbold{ctyp_of} $sign$ $T$] \index{signatures}
   939 certifies $T$ with respect to signature~$sign$.
   940 
   941 \item[\ttindexbold{rep_ctyp} $cT$]
   942 decomposes $cT$ as a record containing the type itself and its signature.
   943 \end{ttdescription}
   944 
   945 
   946 \section{Oracles: calling external reasoners }
   947 \label{sec:oracles}
   948 \index{oracles|(}
   949 
   950 Oracles allow Isabelle to take advantage of external reasoners such as
   951 arithmetic decision procedures, model checkers, fast tautology checkers or
   952 computer algebra systems.  Invoked as an oracle, an external reasoner can
   953 create arbitrary Isabelle theorems.  It is your responsibility to ensure that
   954 the external reasoner is as trustworthy as your application requires.
   955 Isabelle's proof objects~(\S\ref{sec:proofObjects}) record how each theorem
   956 depends upon oracle calls.
   957 
   958 \begin{ttbox}
   959      invoke_oracle : theory * Sign.sg * exn -> thm
   960      set_oracle    : (Sign.sg * exn -> term) -> theory -> theory
   961 \end{ttbox}
   962 \begin{ttdescription}
   963 \item[\ttindexbold{invoke_oracle} ($thy$, $sign$, $exn$)] invokes the oracle
   964   of theory $thy$ passing the information contained in the exception value
   965   $exn$ and creating a theorem having signature $sign$.  Errors arise if $thy$
   966   does not have an oracle, if the oracle rejects its arguments or if its
   967   result is ill-typed.
   968 
   969 \item[\ttindexbold{set_oracle} $fn$ $thy$] sets the oracle of theory $thy$ to
   970   be $fn$.  It is seldom called explicitly, as there is syntax for oracles in
   971   theory files.  A theory can have at most one oracle.
   972 \end{ttdescription}
   973 
   974 A curious feature of {\ML} exceptions is that they are ordinary constructors.
   975 The {\ML} type {\tt exn} is a datatype that can be extended at any time.  (See
   976 my {\em {ML} for the Working Programmer}~\cite{paulson-ml2}, especially
   977 page~136.)  The oracle mechanism takes advantage of this to allow an oracle to
   978 take any information whatever.
   979 
   980 There must be some way of invoking the external reasoner from \ML, either
   981 because it is coded in {\ML} or via an operating system interface.  Isabelle
   982 expects the {\ML} function to take two arguments: a signature and an
   983 exception.
   984 \begin{itemize}
   985 \item The signature will typically be that of a desendant of the theory
   986   declaring the oracle.  The oracle will use it to distinguish constants from
   987   variables, etc., and it will be attached to the generated theorems.
   988 
   989 \item The exception is used to pass arbitrary information to the oracle.  This
   990   information must contain a full description of the problem to be solved by
   991   the external reasoner, including any additional information that might be
   992   required.  The oracle may raise the exception to indicate that it cannot
   993   solve the specified problem.
   994 \end{itemize}
   995 
   996 A trivial example is provided on directory {\tt FOL/ex}.  This oracle
   997 generates tautologies of the form $P\bimp\cdots\bimp P$, with an even number
   998 of $P$s. 
   999 
  1000 File {\tt declIffOracle.ML} begins by declaring a new exception constructor
  1001 for the oracle the information it requires: here, just an integer.  It
  1002 contains some code (suppressed below) for creating the tautologies, and
  1003 finally declares the oracle function itself:
  1004 \begin{ttbox}
  1005 exception IffOracleExn of int;
  1006 \(\vdots\)
  1007 fun mk_iff_oracle (sign, IffOracleExn n) = 
  1008     if n>0 andalso n mod 2 = 0 
  1009     then Trueprop $ mk_iff n
  1010     else raise IffOracleExn n;
  1011 \end{ttbox}
  1012 Observe the function two arguments, the signature {\tt sign} and the exception
  1013 given as a pattern.  The function checks its argument for validity.  If $n$ is
  1014 positive and even then it creates a tautology containing $n$ occurrences
  1015 of~$P$.  Otherwise it signals error by raising its own exception.  Errors may
  1016 be signalled by other means, such as returning the theorem {\tt True}.
  1017 Please ensure that the oracle's result is correctly typed; Isabelle will
  1018 reject ill-typed theorems by raising a cryptic exception at top level.
  1019 
  1020 The theory file {\tt IffOracle.thy} packages up the function above as an
  1021 oracle.  The first line indicates that the new theory depends upon the file
  1022 {\tt declIffOracle.ML} (which declares the {\ML} code) as well as on \FOL.
  1023 The second line informs Isabelle that this theory has an oracle given by the
  1024 function {\tt mk_iff_oracle}.
  1025 \begin{ttbox}
  1026 IffOracle = "declIffOracle" + FOL +
  1027 oracle mk_iff_oracle
  1028 end
  1029 \end{ttbox}
  1030 Because a theory can have at most one oracle, the theory itself serves to
  1031 identify the oracle.
  1032 
  1033 Here are some examples of invoking the oracle.  An argument of 10 is allowed,
  1034 but one of 5 is forbidden:
  1035 \begin{ttbox}
  1036 invoke_oracle (IffOracle.thy, sign_of IffOracle.thy, IffOracleExn 10);
  1037 {\out  "P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P" : thm}
  1038 invoke_oracle (IffOracle.thy, sign_of IffOracle.thy, IffOracleExn 5); 
  1039 {\out Exception- IffOracleExn 5 raised}
  1040 \end{ttbox}
  1041 
  1042 \index{oracles|)}
  1043 \index{theories|)}