doc-src/Ref/theories.tex
author wenzelm
Tue, 04 May 1999 11:27:25 +0200
changeset 6571 971f238ef3ec
parent 6569 66c941ea1f01
child 6592 c120262044b6
permissions -rw-r--r--
tuned;
wenzelm@3201
     1
lcp@104
     2
%% $Id$
wenzelm@145
     3
lcp@104
     4
\chapter{Theories, Terms and Types} \label{theories}
lcp@104
     5
\index{theories|(}\index{signatures|bold}
wenzelm@3108
     6
\index{reading!axioms|see{{\tt assume_ax}}} Theories organize the
wenzelm@3108
     7
syntax, declarations and axioms of a mathematical development.  They
wenzelm@3108
     8
are built, starting from the {\Pure} or {\CPure} theory, by extending
wenzelm@3108
     9
and merging existing theories.  They have the \ML\ type
wenzelm@3108
    10
\mltydx{theory}.  Theory operations signal errors by raising exception
wenzelm@3108
    11
\xdx{THEORY}, returning a message and a list of theories.
lcp@104
    12
lcp@104
    13
Signatures, which contain information about sorts, types, constants and
lcp@332
    14
syntax, have the \ML\ type~\mltydx{Sign.sg}.  For identification, each
wenzelm@864
    15
signature carries a unique list of \bfindex{stamps}, which are \ML\
lcp@324
    16
references to strings.  The strings serve as human-readable names; the
lcp@104
    17
references serve as unique identifiers.  Each primitive signature has a
lcp@104
    18
single stamp.  When two signatures are merged, their lists of stamps are
lcp@104
    19
also merged.  Every theory carries a unique signature.
lcp@104
    20
lcp@104
    21
Terms and types are the underlying representation of logical syntax.  Their
nipkow@275
    22
\ML\ definitions are irrelevant to naive Isabelle users.  Programmers who
nipkow@275
    23
wish to extend Isabelle may need to know such details, say to code a tactic
nipkow@275
    24
that looks for subgoals of a particular form.  Terms and types may be
lcp@104
    25
`certified' to be well-formed with respect to a given signature.
lcp@104
    26
lcp@324
    27
lcp@324
    28
\section{Defining theories}\label{sec:ref-defining-theories}
lcp@104
    29
wenzelm@6571
    30
Theories are defined via theory files $name$\texttt{.thy} (there are also
wenzelm@6571
    31
\ML-level interfaces which are only intended for people building advanced
wenzelm@6571
    32
theory definition packages).  Appendix~\ref{app:TheorySyntax} presents the
wenzelm@6571
    33
concrete syntax for theory files; here follows an explanation of the
wenzelm@6571
    34
constituent parts.
wenzelm@864
    35
\begin{description}
wenzelm@6568
    36
\item[{\it theoryDef}] is the full definition.  The new theory is called $id$.
wenzelm@6568
    37
  It is the union of the named {\bf parent
wenzelm@3108
    38
    theories}\indexbold{theories!parent}, possibly extended with new
wenzelm@6568
    39
  components.  \thydx{Pure} and \thydx{CPure} are the basic theories, which
wenzelm@6568
    40
  contain only the meta-logic.  They differ just in their concrete syntax for
wenzelm@6568
    41
  function applications.
wenzelm@6571
    42
  
wenzelm@6571
    43
  The new theory begins as a merge of its parents.
wenzelm@6571
    44
  \begin{ttbox}
wenzelm@6571
    45
    Attempt to merge different versions of theories: "\(T@1\)", \(\ldots\), "\(T@n\)"
wenzelm@6571
    46
  \end{ttbox}
wenzelm@6571
    47
  This error may especially occur when a theory is redeclared --- say to
wenzelm@6571
    48
  change an inappropriate definition --- and bindings to old versions persist.
wenzelm@6571
    49
  Isabelle ensures that old and new theories of the same name are not involved
wenzelm@6571
    50
  in a proof.
lcp@324
    51
wenzelm@864
    52
\item[$classes$]
wenzelm@864
    53
  is a series of class declarations.  Declaring {\tt$id$ < $id@1$ \dots\
lcp@324
    54
    $id@n$} makes $id$ a subclass of the existing classes $id@1\dots
lcp@324
    55
  id@n$.  This rules out cyclic class structures.  Isabelle automatically
lcp@324
    56
  computes the transitive closure of subclass hierarchies; it is not
lcp@324
    57
  necessary to declare {\tt c < e} in addition to {\tt c < d} and {\tt d <
lcp@324
    58
    e}.
lcp@324
    59
wenzelm@864
    60
\item[$default$]
wenzelm@864
    61
  introduces $sort$ as the new default sort for type variables.  This applies
wenzelm@864
    62
  to unconstrained type variables in an input string but not to type
wenzelm@864
    63
  variables created internally.  If omitted, the default sort is the listwise
wenzelm@864
    64
  union of the default sorts of the parent theories (i.e.\ their logical
wenzelm@864
    65
  intersection).
wenzelm@3108
    66
  
wenzelm@3108
    67
\item[$sort$] is a finite set of classes.  A single class $id$
wenzelm@3108
    68
  abbreviates the sort $\ttlbrace id\ttrbrace$.
lcp@324
    69
wenzelm@864
    70
\item[$types$]
lcp@324
    71
  is a series of type declarations.  Each declares a new type constructor
lcp@324
    72
  or type synonym.  An $n$-place type constructor is specified by
lcp@324
    73
  $(\alpha@1,\dots,\alpha@n)name$, where the type variables serve only to
lcp@324
    74
  indicate the number~$n$.
lcp@324
    75
lcp@332
    76
  A {\bf type synonym}\indexbold{type synonyms} is an abbreviation
clasohm@1387
    77
  $(\alpha@1,\dots,\alpha@n)name = \tau$, where $name$ and $\tau$ can
clasohm@1387
    78
  be strings.
wenzelm@864
    79
wenzelm@864
    80
\item[$infix$]
lcp@324
    81
  declares a type or constant to be an infix operator of priority $nat$
paulson@3485
    82
  associating to the left ({\tt infixl}) or right ({\tt infixr}).  Only
wenzelm@864
    83
  2-place type constructors can have infix status; an example is {\tt
wenzelm@3108
    84
  ('a,'b)~"*"~(infixr~20)}, which may express binary product types.
lcp@324
    85
wenzelm@3108
    86
\item[$arities$] is a series of type arity declarations.  Each assigns
wenzelm@3108
    87
  arities to type constructors.  The $name$ must be an existing type
wenzelm@3108
    88
  constructor, which is given the additional arity $arity$.
wenzelm@3108
    89
  
wenzelm@5369
    90
\item[$nonterminals$]\index{*nonterminal symbols} declares purely
wenzelm@5369
    91
  syntactic types to be used as nonterminal symbols of the context
wenzelm@5369
    92
  free grammar.
wenzelm@5369
    93
wenzelm@3108
    94
\item[$consts$] is a series of constant declarations.  Each new
wenzelm@3108
    95
  constant $name$ is given the specified type.  The optional $mixfix$
wenzelm@3108
    96
  annotations may attach concrete syntax to the constant.
wenzelm@3108
    97
  
wenzelm@3108
    98
\item[$syntax$] \index{*syntax section}\index{print mode} is a variant
wenzelm@3108
    99
  of $consts$ which adds just syntax without actually declaring
wenzelm@3108
   100
  logical constants.  This gives full control over a theory's context
paulson@3485
   101
  free grammar.  The optional $mode$ specifies the print mode where the
paulson@3485
   102
  mixfix productions should be added.  If there is no \texttt{output}
wenzelm@3108
   103
  option given, all productions are also added to the input syntax
wenzelm@3108
   104
  (regardless of the print mode).
lcp@324
   105
lcp@324
   106
\item[$mixfix$] \index{mixfix declarations}
lcp@324
   107
  annotations can take three forms:
nipkow@273
   108
  \begin{itemize}
nipkow@273
   109
  \item A mixfix template given as a $string$ of the form
nipkow@273
   110
    {\tt"}\dots{\tt\_}\dots{\tt\_}\dots{\tt"} where the $i$-th underscore
lcp@324
   111
    indicates the position where the $i$-th argument should go.  The list
lcp@324
   112
    of numbers gives the priority of each argument.  The final number gives
lcp@324
   113
    the priority of the whole construct.
lcp@104
   114
lcp@324
   115
  \item A constant $f$ of type $\tau@1\To(\tau@2\To\tau)$ can be given {\bf
lcp@324
   116
    infix} status.
lcp@104
   117
lcp@324
   118
  \item A constant $f$ of type $(\tau@1\To\tau@2)\To\tau$ can be given {\bf
lcp@324
   119
    binder} status.  The declaration {\tt binder} $\cal Q$ $p$ causes
lcp@286
   120
  ${\cal Q}\,x.F(x)$ to be treated
lcp@286
   121
  like $f(F)$, where $p$ is the priority.
nipkow@273
   122
  \end{itemize}
lcp@324
   123
wenzelm@864
   124
\item[$trans$]
wenzelm@864
   125
  specifies syntactic translation rules (macros).  There are three forms:
wenzelm@864
   126
  parse rules ({\tt =>}), print rules ({\tt <=}), and parse/print rules ({\tt
wenzelm@864
   127
  ==}).
lcp@324
   128
nipkow@1650
   129
\item[$rules$]
wenzelm@864
   130
  is a series of rule declarations.  Each has a name $id$ and the formula is
wenzelm@864
   131
  given by the $string$.  Rule names must be distinct within any single
wenzelm@3108
   132
  theory.
lcp@324
   133
paulson@1905
   134
\item[$defs$] is a series of definitions.  They are just like $rules$, except
paulson@1905
   135
  that every $string$ must be a definition (see below for details).
nipkow@1650
   136
nipkow@1650
   137
\item[$constdefs$] combines the declaration of constants and their
paulson@3485
   138
  definition.  The first $string$ is the type, the second the definition.
wenzelm@3108
   139
  
wenzelm@3108
   140
\item[$axclass$] \index{*axclass section} defines an
wenzelm@3108
   141
  \rmindex{axiomatic type class} as the intersection of existing
paulson@3485
   142
  classes, with additional axioms holding.  Class axioms may not
paulson@3485
   143
  contain more than one type variable.  The class axioms (with implicit
wenzelm@3108
   144
  sort constraints added) are bound to the given names.  Furthermore a
wenzelm@3108
   145
  class introduction rule is generated, which is automatically
wenzelm@3108
   146
  employed by $instance$ to prove instantiations of this class.
wenzelm@3108
   147
  
wenzelm@3108
   148
\item[$instance$] \index{*instance section} proves class inclusions or
wenzelm@3108
   149
  type arities at the logical level and then transfers these to the
paulson@3485
   150
  type signature.  The instantiation is proven and checked properly.
wenzelm@3108
   151
  The user has to supply sufficient witness information: theorems
wenzelm@3108
   152
  ($longident$), axioms ($string$), or even arbitrary \ML{} tactic
wenzelm@3108
   153
  code $verbatim$.
nipkow@1650
   154
paulson@1846
   155
\item[$oracle$] links the theory to a trusted external reasoner.  It is
paulson@1846
   156
  allowed to create theorems, but each theorem carries a proof object
paulson@1846
   157
  describing the oracle invocation.  See \S\ref{sec:oracles} for details.
wenzelm@4543
   158
  
wenzelm@5369
   159
\item[$local$, $global$] change the current name declaration mode.
wenzelm@4543
   160
  Initially, theories start in $local$ mode, causing all names of
wenzelm@4543
   161
  types, constants, axioms etc.\ to be automatically qualified by the
wenzelm@4543
   162
  theory name.  Changing this to $global$ causes all names to be
wenzelm@4543
   163
  declared as short base names only.
wenzelm@4543
   164
  
wenzelm@4543
   165
  The $local$ and $global$ declarations act like switches, affecting
wenzelm@4543
   166
  all following theory sections until changed again explicitly.  Also
wenzelm@4543
   167
  note that the final state at the end of the theory will persist.  In
wenzelm@4543
   168
  particular, this determines how the names of theorems stored later
wenzelm@4543
   169
  on are handled.
wenzelm@5369
   170
  
wenzelm@5369
   171
\item[$setup$]\index{*setup!theory} applies a list of ML functions to
wenzelm@5369
   172
  the theory.  The argument should denote a value of type
wenzelm@5369
   173
  \texttt{(theory -> theory) list}.  Typically, ML packages are
wenzelm@5369
   174
  initialized in this way.
paulson@1846
   175
lcp@324
   176
\item[$ml$] \index{*ML section}
wenzelm@864
   177
  consists of \ML\ code, typically for parse and print translation functions.
lcp@104
   178
\end{description}
lcp@324
   179
%
wenzelm@864
   180
Chapters~\ref{Defining-Logics} and \ref{chap:syntax} explain mixfix
wenzelm@864
   181
declarations, translation rules and the {\tt ML} section in more detail.
wenzelm@145
   182
wenzelm@145
   183
paulson@1905
   184
\subsection{Definitions}\indexbold{definitions}
paulson@1905
   185
paulson@3485
   186
{\bf Definitions} are intended to express abbreviations.  The simplest
wenzelm@3108
   187
form of a definition is $f \equiv t$, where $f$ is a constant.
wenzelm@3108
   188
Isabelle also allows a derived forms where the arguments of~$f$ appear
wenzelm@3108
   189
on the left, abbreviating a string of $\lambda$-abstractions.
paulson@1905
   190
paulson@1905
   191
Isabelle makes the following checks on definitions:
paulson@1905
   192
\begin{itemize}
wenzelm@3108
   193
\item Arguments (on the left-hand side) must be distinct variables.
paulson@1905
   194
\item All variables on the right-hand side must also appear on the left-hand
paulson@1905
   195
  side. 
wenzelm@3108
   196
\item All type variables on the right-hand side must also appear on
wenzelm@3108
   197
  the left-hand side; this prohibits definitions such as {\tt
wenzelm@3108
   198
    (zero::nat) == length ([]::'a list)}.
paulson@1905
   199
\item The definition must not be recursive.  Most object-logics provide
paulson@1905
   200
  definitional principles that can be used to express recursion safely.
paulson@1905
   201
\end{itemize}
paulson@1905
   202
These checks are intended to catch the sort of errors that might be made
paulson@1905
   203
accidentally.  Misspellings, for instance, might result in additional
paulson@1905
   204
variables appearing on the right-hand side.  More elaborate checks could be
paulson@1905
   205
made, but the cost might be overly strict rules on declaration order, etc.
paulson@1905
   206
paulson@1905
   207
nipkow@275
   208
\subsection{*Classes and arities}
lcp@324
   209
\index{classes!context conditions}\index{arities!context conditions}
wenzelm@145
   210
lcp@286
   211
In order to guarantee principal types~\cite{nipkow-prehofer},
lcp@324
   212
arity declarations must obey two conditions:
wenzelm@145
   213
\begin{itemize}
wenzelm@3108
   214
\item There must not be any two declarations $ty :: (\vec{r})c$ and
wenzelm@3108
   215
  $ty :: (\vec{s})c$ with $\vec{r} \neq \vec{s}$.  For example, this
wenzelm@3108
   216
  excludes the following:
wenzelm@145
   217
\begin{ttbox}
wenzelm@864
   218
arities
wenzelm@3108
   219
  foo :: ({\ttlbrace}logic{\ttrbrace}) logic
wenzelm@3108
   220
  foo :: ({\ttlbrace}{\ttrbrace})logic
wenzelm@145
   221
\end{ttbox}
lcp@286
   222
wenzelm@145
   223
\item If there are two declarations $ty :: (s@1,\dots,s@n)c$ and $ty ::
wenzelm@145
   224
  (s@1',\dots,s@n')c'$ such that $c' < c$ then $s@i' \preceq s@i$ must hold
wenzelm@145
   225
  for $i=1,\dots,n$.  The relationship $\preceq$, defined as
wenzelm@145
   226
\[ s' \preceq s \iff \forall c\in s. \exists c'\in s'.~ c'\le c, \]
wenzelm@3108
   227
expresses that the set of types represented by $s'$ is a subset of the
wenzelm@3108
   228
set of types represented by $s$.  Assuming $term \preceq logic$, the
wenzelm@3108
   229
following is forbidden:
wenzelm@145
   230
\begin{ttbox}
wenzelm@864
   231
arities
wenzelm@3108
   232
  foo :: ({\ttlbrace}logic{\ttrbrace})logic
wenzelm@3108
   233
  foo :: ({\ttlbrace}{\ttrbrace})term
wenzelm@145
   234
\end{ttbox}
lcp@286
   235
wenzelm@145
   236
\end{itemize}
wenzelm@145
   237
lcp@104
   238
wenzelm@6568
   239
\section{The theory loader}\label{sec:more-theories}
wenzelm@6568
   240
\index{theories!reading}\index{files!reading}
wenzelm@6568
   241
wenzelm@6568
   242
Isabelle's theory loader manages dependencies of the internal graph of theory
wenzelm@6568
   243
nodes (the \emph{theory database}) and the external view of the file system.
wenzelm@6568
   244
See \S\ref{sec:intro-theories} for its most basic commands, such as
wenzelm@6568
   245
\texttt{use_thy}.  There are a few more operations available.
wenzelm@6568
   246
wenzelm@864
   247
\begin{ttbox}
wenzelm@6568
   248
use_thy_only    : string -> unit
wenzelm@6568
   249
update_thy      : string -> unit
wenzelm@6568
   250
touch_thy       : string -> unit
lcp@286
   251
delete_tmpfiles : bool ref \hfill{\bf initially true}
lcp@286
   252
\end{ttbox}
nipkow@275
   253
lcp@324
   254
\begin{ttdescription}
wenzelm@6569
   255
\item[\ttindexbold{use_thy_only} "$name$";] is similar to \texttt{use_thy},
wenzelm@6569
   256
  but processes the actual theory file $name$\texttt{.thy} only, ignoring
wenzelm@6568
   257
  $name$\texttt{.ML}.  This might be useful in replaying proof scripts by hand
wenzelm@6568
   258
  from the very beginning, starting with the fresh theory.
wenzelm@6568
   259
  
wenzelm@6569
   260
\item[\ttindexbold{update_thy} "$name$";] is similar to \texttt{use_thy}, but
wenzelm@6568
   261
  ensures that theory $name$ is fully up-to-date with respect to the file
wenzelm@6571
   262
  system --- apart from $name$ itself any of its ancestors may be reloaded as
wenzelm@6571
   263
  well.
wenzelm@6568
   264
  
wenzelm@6569
   265
\item[\ttindexbold{touch_thy} "$name$";] marks theory node $name$ of the
wenzelm@6568
   266
  internal graph as outdated.  While the theory remains usable, subsequent
wenzelm@6568
   267
  operations such as \texttt{use_thy} may cause a reload.
wenzelm@6568
   268
  
wenzelm@6568
   269
\item[reset \ttindexbold{delete_tmpfiles};] processing theory files usually
wenzelm@6568
   270
  involves temporary {\ML} files to be created.  By default, these are deleted
wenzelm@6568
   271
  afterwards.  Resetting the \texttt{delete_tmpfiles} flag inhibits this,
wenzelm@6568
   272
  leaving the generated code for debugging purposes.  The basic location for
wenzelm@6568
   273
  temporary files is determined by the \texttt{ISABELLE_TMP} environment
wenzelm@6571
   274
  variable (which is private to the running Isabelle process and may be
wenzelm@6568
   275
  retrieved by \ttindex{getenv} from {\ML}).
wenzelm@6568
   276
\end{ttdescription}
clasohm@138
   277
wenzelm@6568
   278
\medskip Theory and {\ML} files are located by skimming through the
wenzelm@6568
   279
directories listed in Isabelle's internal load path, which merely contains the
wenzelm@6568
   280
current directory ``\texttt{.}'' by default.  The load path may be accessed by
wenzelm@6568
   281
the following operations.
lcp@286
   282
wenzelm@864
   283
\begin{ttbox}
wenzelm@6568
   284
show_path: unit -> string list
wenzelm@6568
   285
add_path: string -> unit
wenzelm@6568
   286
del_path: string -> unit
wenzelm@6568
   287
reset_path: unit -> unit
lcp@286
   288
\end{ttbox}
clasohm@138
   289
lcp@324
   290
\begin{ttdescription}
wenzelm@6568
   291
\item[\ttindexbold{show_path}();] displays the load path components in
wenzelm@6571
   292
  canonical string representation (which is always according to Unix rules).
wenzelm@6568
   293
  
wenzelm@6569
   294
\item[\ttindexbold{add_path} "$dir$";] adds component $dir$ to the beginning
wenzelm@6569
   295
  of the load path.
wenzelm@6568
   296
  
wenzelm@6569
   297
\item[\ttindexbold{del_path} "$dir$";] removes any occurrences of component
wenzelm@6568
   298
  $dir$ from the load path.
wenzelm@6568
   299
  
wenzelm@6568
   300
\item[\ttindexbold{reset_path}();] resets the load path to ``\texttt{.}''
wenzelm@6568
   301
  (current directory) only.
lcp@324
   302
\end{ttdescription}
clasohm@138
   303
wenzelm@6571
   304
In operations referring indirectly to some file, the argument may be prefixed
wenzelm@6571
   305
by a directory that will be used as temporary load path, e.g.\ 
wenzelm@6571
   306
\texttt{use_thy~"$dir/name$"}.  Note that, depending on which parts of the
wenzelm@6571
   307
ancestry of $name$ are already loaded, the dynamic change of paths might be
wenzelm@6571
   308
hard to predict.  Use this feature with care only.
lcp@104
   309
lcp@104
   310
clasohm@866
   311
\section{Basic operations on theories}\label{BasicOperationsOnTheories}
wenzelm@4384
   312
wenzelm@4384
   313
\subsection{*Theory inclusion}
wenzelm@4384
   314
\begin{ttbox}
wenzelm@4384
   315
subthy      : theory * theory -> bool
wenzelm@4384
   316
eq_thy      : theory * theory -> bool
wenzelm@4384
   317
transfer    : theory -> thm -> thm
wenzelm@4384
   318
transfer_sg : Sign.sg -> thm -> thm
wenzelm@4384
   319
\end{ttbox}
wenzelm@4384
   320
wenzelm@4384
   321
Inclusion and equality of theories is determined by unique
wenzelm@4384
   322
identification stamps that are created when declaring new components.
wenzelm@4384
   323
Theorems contain a reference to the theory (actually to its signature)
wenzelm@4384
   324
they have been derived in.  Transferring theorems to super theories
wenzelm@4384
   325
has no logical significance, but may affect some operations in subtle
wenzelm@4384
   326
ways (e.g.\ implicit merges of signatures when applying rules, or
wenzelm@4384
   327
pretty printing of theorems).
wenzelm@4384
   328
wenzelm@4384
   329
\begin{ttdescription}
wenzelm@4384
   330
wenzelm@4384
   331
\item[\ttindexbold{subthy} ($thy@1$, $thy@2$)] determines if $thy@1$
wenzelm@4384
   332
  is included in $thy@2$ wrt.\ identification stamps.
wenzelm@4384
   333
wenzelm@4384
   334
\item[\ttindexbold{eq_thy} ($thy@1$, $thy@2$)] determines if $thy@1$
wenzelm@4384
   335
  is exactly the same as $thy@2$.
wenzelm@4384
   336
wenzelm@4384
   337
\item[\ttindexbold{transfer} $thy$ $thm$] transfers theorem $thm$ to
wenzelm@4384
   338
  theory $thy$, provided the latter includes the theory of $thm$.
wenzelm@4384
   339
  
wenzelm@4384
   340
\item[\ttindexbold{transfer_sg} $sign$ $thm$] is similar to
wenzelm@4384
   341
  \texttt{transfer}, but identifies the super theory via its
wenzelm@4384
   342
  signature.
wenzelm@4384
   343
wenzelm@4384
   344
\end{ttdescription}
wenzelm@4384
   345
wenzelm@4384
   346
wenzelm@6571
   347
\subsection{*Primitive theories}
wenzelm@864
   348
\begin{ttbox}
wenzelm@4317
   349
ProtoPure.thy  : theory
wenzelm@3108
   350
Pure.thy       : theory
wenzelm@3108
   351
CPure.thy      : theory
lcp@286
   352
\end{ttbox}
wenzelm@3108
   353
\begin{description}
wenzelm@4317
   354
\item[\ttindexbold{ProtoPure.thy}, \ttindexbold{Pure.thy},
wenzelm@4317
   355
  \ttindexbold{CPure.thy}] contain the syntax and signature of the
wenzelm@4317
   356
  meta-logic.  There are basically no axioms: meta-level inferences
wenzelm@4317
   357
  are carried out by \ML\ functions.  \texttt{Pure} and \texttt{CPure}
wenzelm@4317
   358
  just differ in their concrete syntax of prefix function application:
wenzelm@4317
   359
  $t(u@1, \ldots, u@n)$ in \texttt{Pure} vs.\ $t\,u@1,\ldots\,u@n$ in
wenzelm@4317
   360
  \texttt{CPure}.  \texttt{ProtoPure} is their common parent,
wenzelm@4317
   361
  containing no syntax for printing prefix applications at all!
wenzelm@6571
   362
wenzelm@6571
   363
%%FIXME  
wenzelm@6571
   364
%\item[\ttindexbold{merge_theories} $name$ ($thy@1$, $thy@2$)] merges
wenzelm@6571
   365
%  the two theories $thy@1$ and $thy@2$, creating a new named theory
wenzelm@6571
   366
%  node.  The resulting theory contains all of the syntax, signature
wenzelm@6571
   367
%  and axioms of the constituent theories.  Merging theories that
wenzelm@6571
   368
%  contain different identification stamps of the same name fails with
wenzelm@6571
   369
%  the following message
wenzelm@864
   370
wenzelm@864
   371
%% FIXME
nipkow@478
   372
%\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"} $\cdots$] extends
nipkow@478
   373
%  the theory $thy$ with new types, constants, etc.  $T$ identifies the theory
nipkow@478
   374
%  internally.  When a theory is redeclared, say to change an incorrect axiom,
nipkow@478
   375
%  bindings to the old axiom may persist.  Isabelle ensures that the old and
nipkow@478
   376
%  new theories are not involved in the same proof.  Attempting to combine
nipkow@478
   377
%  different theories having the same name $T$ yields the fatal error
nipkow@478
   378
%extend_theory  : theory -> string -> \(\cdots\) -> theory
wenzelm@864
   379
%\begin{ttbox}
wenzelm@864
   380
%Attempt to merge different versions of theory: \(T\)
wenzelm@864
   381
%\end{ttbox}
wenzelm@3108
   382
\end{description}
lcp@286
   383
wenzelm@864
   384
%% FIXME
nipkow@275
   385
%\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"}
nipkow@275
   386
%      ($classes$, $default$, $types$, $arities$, $consts$, $sextopt$) $rules$]
nipkow@275
   387
%\hfill\break   %%% include if line is just too short
lcp@286
   388
%is the \ML{} equivalent of the following theory definition:
nipkow@275
   389
%\begin{ttbox}
nipkow@275
   390
%\(T\) = \(thy\) +
nipkow@275
   391
%classes \(c\) < \(c@1\),\(\dots\),\(c@m\)
nipkow@275
   392
%        \dots
nipkow@275
   393
%default {\(d@1,\dots,d@r\)}
nipkow@275
   394
%types   \(tycon@1\),\dots,\(tycon@i\) \(n\)
nipkow@275
   395
%        \dots
nipkow@275
   396
%arities \(tycon@1'\),\dots,\(tycon@j'\) :: (\(s@1\),\dots,\(s@n\))\(c\)
nipkow@275
   397
%        \dots
nipkow@275
   398
%consts  \(b@1\),\dots,\(b@k\) :: \(\tau\)
nipkow@275
   399
%        \dots
nipkow@275
   400
%rules   \(name\) \(rule\)
nipkow@275
   401
%        \dots
nipkow@275
   402
%end
nipkow@275
   403
%\end{ttbox}
nipkow@275
   404
%where
nipkow@275
   405
%\begin{tabular}[t]{l@{~=~}l}
nipkow@275
   406
%$classes$ & \tt[("$c$",["$c@1$",\dots,"$c@m$"]),\dots] \\
nipkow@275
   407
%$default$ & \tt["$d@1$",\dots,"$d@r$"]\\
nipkow@275
   408
%$types$   & \tt[([$tycon@1$,\dots,$tycon@i$], $n$),\dots] \\
nipkow@275
   409
%$arities$ & \tt[([$tycon'@1$,\dots,$tycon'@j$], ([$s@1$,\dots,$s@n$],$c$)),\dots]
nipkow@275
   410
%\\
nipkow@275
   411
%$consts$  & \tt[([$b@1$,\dots,$b@k$],$\tau$),\dots] \\
nipkow@275
   412
%$rules$   & \tt[("$name$",$rule$),\dots]
nipkow@275
   413
%\end{tabular}
lcp@104
   414
lcp@104
   415
wenzelm@864
   416
\subsection{Inspecting a theory}\label{sec:inspct-thy}
lcp@104
   417
\index{theories!inspecting|bold}
wenzelm@864
   418
\begin{ttbox}
wenzelm@4317
   419
print_syntax        : theory -> unit
wenzelm@4317
   420
print_theory        : theory -> unit
wenzelm@4317
   421
parents_of          : theory -> theory list
wenzelm@4317
   422
ancestors_of        : theory -> theory list
wenzelm@4317
   423
sign_of             : theory -> Sign.sg
wenzelm@4317
   424
Sign.stamp_names_of : Sign.sg -> string list
lcp@104
   425
\end{ttbox}
wenzelm@864
   426
These provide means of viewing a theory's components.
lcp@324
   427
\begin{ttdescription}
wenzelm@3108
   428
\item[\ttindexbold{print_syntax} $thy$] prints the syntax of $thy$
wenzelm@3108
   429
  (grammar, macros, translation functions etc., see
wenzelm@3108
   430
  page~\pageref{pg:print_syn} for more details).
wenzelm@3108
   431
  
wenzelm@3108
   432
\item[\ttindexbold{print_theory} $thy$] prints the logical parts of
wenzelm@3108
   433
  $thy$, excluding the syntax.
wenzelm@4317
   434
  
wenzelm@4317
   435
\item[\ttindexbold{parents_of} $thy$] returns the direct ancestors
wenzelm@4317
   436
  of~$thy$.
wenzelm@4317
   437
  
wenzelm@4317
   438
\item[\ttindexbold{ancestors_of} $thy$] returns all ancestors of~$thy$
wenzelm@4317
   439
  (not including $thy$ itself).
wenzelm@4317
   440
  
wenzelm@4317
   441
\item[\ttindexbold{sign_of} $thy$] returns the signature associated
wenzelm@4317
   442
  with~$thy$.  It is useful with functions like {\tt
wenzelm@4317
   443
    read_instantiate_sg}, which take a signature as an argument.
wenzelm@4317
   444
  
wenzelm@4317
   445
\item[\ttindexbold{Sign.stamp_names_of} $sg$]\index{signatures}
wenzelm@4317
   446
  returns the names of the identification \rmindex{stamps} of ax
wenzelm@4317
   447
  signature.  These coincide with the names of its full ancestry
wenzelm@4317
   448
  including that of $sg$ itself.
lcp@104
   449
lcp@324
   450
\end{ttdescription}
lcp@104
   451
clasohm@1369
   452
lcp@104
   453
\section{Terms}
lcp@104
   454
\index{terms|bold}
lcp@324
   455
Terms belong to the \ML\ type \mltydx{term}, which is a concrete datatype
wenzelm@3108
   456
with six constructors:
lcp@104
   457
\begin{ttbox}
lcp@104
   458
type indexname = string * int;
lcp@104
   459
infix 9 $;
lcp@104
   460
datatype term = Const of string * typ
lcp@104
   461
              | Free  of string * typ
lcp@104
   462
              | Var   of indexname * typ
lcp@104
   463
              | Bound of int
lcp@104
   464
              | Abs   of string * typ * term
lcp@104
   465
              | op $  of term * term;
lcp@104
   466
\end{ttbox}
lcp@324
   467
\begin{ttdescription}
wenzelm@4317
   468
\item[\ttindexbold{Const} ($a$, $T$)] \index{constants|bold}
lcp@286
   469
  is the {\bf constant} with name~$a$ and type~$T$.  Constants include
lcp@286
   470
  connectives like $\land$ and $\forall$ as well as constants like~0
lcp@286
   471
  and~$Suc$.  Other constants may be required to define a logic's concrete
wenzelm@864
   472
  syntax.
lcp@104
   473
wenzelm@4317
   474
\item[\ttindexbold{Free} ($a$, $T$)] \index{variables!free|bold}
lcp@324
   475
  is the {\bf free variable} with name~$a$ and type~$T$.
lcp@104
   476
wenzelm@4317
   477
\item[\ttindexbold{Var} ($v$, $T$)] \index{unknowns|bold}
lcp@324
   478
  is the {\bf scheme variable} with indexname~$v$ and type~$T$.  An
lcp@324
   479
  \mltydx{indexname} is a string paired with a non-negative index, or
lcp@324
   480
  subscript; a term's scheme variables can be systematically renamed by
lcp@324
   481
  incrementing their subscripts.  Scheme variables are essentially free
lcp@324
   482
  variables, but may be instantiated during unification.
lcp@104
   483
lcp@324
   484
\item[\ttindexbold{Bound} $i$] \index{variables!bound|bold}
lcp@324
   485
  is the {\bf bound variable} with de Bruijn index~$i$, which counts the
lcp@324
   486
  number of lambdas, starting from zero, between a variable's occurrence
lcp@324
   487
  and its binding.  The representation prevents capture of variables.  For
lcp@324
   488
  more information see de Bruijn \cite{debruijn72} or
lcp@324
   489
  Paulson~\cite[page~336]{paulson91}.
lcp@104
   490
wenzelm@4317
   491
\item[\ttindexbold{Abs} ($a$, $T$, $u$)]
lcp@324
   492
  \index{lambda abs@$\lambda$-abstractions|bold}
lcp@324
   493
  is the $\lambda$-{\bf abstraction} with body~$u$, and whose bound
lcp@324
   494
  variable has name~$a$ and type~$T$.  The name is used only for parsing
lcp@324
   495
  and printing; it has no logical significance.
lcp@104
   496
lcp@324
   497
\item[$t$ \$ $u$] \index{$@{\tt\$}|bold} \index{function applications|bold}
wenzelm@864
   498
is the {\bf application} of~$t$ to~$u$.
lcp@324
   499
\end{ttdescription}
lcp@286
   500
Application is written as an infix operator to aid readability.
lcp@332
   501
Here is an \ML\ pattern to recognize \FOL{} formulae of
lcp@104
   502
the form~$A\imp B$, binding the subformulae to~$A$ and~$B$:
wenzelm@864
   503
\begin{ttbox}
lcp@104
   504
Const("Trueprop",_) $ (Const("op -->",_) $ A $ B)
lcp@104
   505
\end{ttbox}
lcp@104
   506
lcp@104
   507
wenzelm@4317
   508
\section{*Variable binding}
lcp@286
   509
\begin{ttbox}
lcp@286
   510
loose_bnos     : term -> int list
lcp@286
   511
incr_boundvars : int -> term -> term
lcp@286
   512
abstract_over  : term*term -> term
lcp@286
   513
variant_abs    : string * typ * term -> string * term
wenzelm@4374
   514
aconv          : term * term -> bool\hfill{\bf infix}
lcp@286
   515
\end{ttbox}
lcp@286
   516
These functions are all concerned with the de Bruijn representation of
lcp@286
   517
bound variables.
lcp@324
   518
\begin{ttdescription}
wenzelm@864
   519
\item[\ttindexbold{loose_bnos} $t$]
lcp@286
   520
  returns the list of all dangling bound variable references.  In
lcp@286
   521
  particular, {\tt Bound~0} is loose unless it is enclosed in an
lcp@286
   522
  abstraction.  Similarly {\tt Bound~1} is loose unless it is enclosed in
lcp@286
   523
  at least two abstractions; if enclosed in just one, the list will contain
lcp@286
   524
  the number 0.  A well-formed term does not contain any loose variables.
lcp@286
   525
wenzelm@864
   526
\item[\ttindexbold{incr_boundvars} $j$]
lcp@332
   527
  increases a term's dangling bound variables by the offset~$j$.  This is
lcp@286
   528
  required when moving a subterm into a context where it is enclosed by a
lcp@286
   529
  different number of abstractions.  Bound variables with a matching
lcp@286
   530
  abstraction are unaffected.
lcp@286
   531
wenzelm@864
   532
\item[\ttindexbold{abstract_over} $(v,t)$]
lcp@286
   533
  forms the abstraction of~$t$ over~$v$, which may be any well-formed term.
lcp@286
   534
  It replaces every occurrence of \(v\) by a {\tt Bound} variable with the
lcp@286
   535
  correct index.
lcp@286
   536
wenzelm@864
   537
\item[\ttindexbold{variant_abs} $(a,T,u)$]
lcp@286
   538
  substitutes into $u$, which should be the body of an abstraction.
lcp@286
   539
  It replaces each occurrence of the outermost bound variable by a free
lcp@286
   540
  variable.  The free variable has type~$T$ and its name is a variant
lcp@332
   541
  of~$a$ chosen to be distinct from all constants and from all variables
lcp@286
   542
  free in~$u$.
lcp@286
   543
wenzelm@864
   544
\item[$t$ \ttindexbold{aconv} $u$]
lcp@286
   545
  tests whether terms~$t$ and~$u$ are \(\alpha\)-convertible: identical up
lcp@286
   546
  to renaming of bound variables.
lcp@286
   547
\begin{itemize}
lcp@286
   548
  \item
lcp@286
   549
    Two constants, {\tt Free}s, or {\tt Var}s are \(\alpha\)-convertible
lcp@286
   550
    if their names and types are equal.
lcp@286
   551
    (Variables having the same name but different types are thus distinct.
lcp@286
   552
    This confusing situation should be avoided!)
lcp@286
   553
  \item
lcp@286
   554
    Two bound variables are \(\alpha\)-convertible
lcp@286
   555
    if they have the same number.
lcp@286
   556
  \item
lcp@286
   557
    Two abstractions are \(\alpha\)-convertible
lcp@286
   558
    if their bodies are, and their bound variables have the same type.
lcp@286
   559
  \item
lcp@286
   560
    Two applications are \(\alpha\)-convertible
lcp@286
   561
    if the corresponding subterms are.
lcp@286
   562
\end{itemize}
lcp@286
   563
lcp@324
   564
\end{ttdescription}
lcp@286
   565
wenzelm@864
   566
\section{Certified terms}\index{terms!certified|bold}\index{signatures}
wenzelm@864
   567
A term $t$ can be {\bf certified} under a signature to ensure that every type
wenzelm@864
   568
in~$t$ is well-formed and every constant in~$t$ is a type instance of a
wenzelm@864
   569
constant declared in the signature.  The term must be well-typed and its use
wenzelm@864
   570
of bound variables must be well-formed.  Meta-rules such as {\tt forall_elim}
wenzelm@864
   571
take certified terms as arguments.
lcp@104
   572
lcp@324
   573
Certified terms belong to the abstract type \mltydx{cterm}.
lcp@104
   574
Elements of the type can only be created through the certification process.
lcp@104
   575
In case of error, Isabelle raises exception~\ttindex{TERM}\@.
lcp@104
   576
lcp@104
   577
\subsection{Printing terms}
lcp@324
   578
\index{terms!printing of}
wenzelm@864
   579
\begin{ttbox}
nipkow@275
   580
     string_of_cterm :           cterm -> string
lcp@104
   581
Sign.string_of_term  : Sign.sg -> term -> string
lcp@104
   582
\end{ttbox}
lcp@324
   583
\begin{ttdescription}
wenzelm@864
   584
\item[\ttindexbold{string_of_cterm} $ct$]
lcp@104
   585
displays $ct$ as a string.
lcp@104
   586
wenzelm@864
   587
\item[\ttindexbold{Sign.string_of_term} $sign$ $t$]
lcp@104
   588
displays $t$ as a string, using the syntax of~$sign$.
lcp@324
   589
\end{ttdescription}
lcp@104
   590
lcp@104
   591
\subsection{Making and inspecting certified terms}
wenzelm@864
   592
\begin{ttbox}
wenzelm@4543
   593
cterm_of          : Sign.sg -> term -> cterm
wenzelm@4543
   594
read_cterm        : Sign.sg -> string * typ -> cterm
wenzelm@4543
   595
cert_axm          : Sign.sg -> string * term -> string * term
wenzelm@4543
   596
read_axm          : Sign.sg -> string * string -> string * term
wenzelm@4543
   597
rep_cterm         : cterm -> {\ttlbrace}T:typ, t:term, sign:Sign.sg, maxidx:int\ttrbrace
wenzelm@4543
   598
Sign.certify_term : Sign.sg -> term -> term * typ * int
lcp@104
   599
\end{ttbox}
lcp@324
   600
\begin{ttdescription}
wenzelm@4543
   601
  
wenzelm@4543
   602
\item[\ttindexbold{cterm_of} $sign$ $t$] \index{signatures} certifies
wenzelm@4543
   603
  $t$ with respect to signature~$sign$.
wenzelm@4543
   604
  
wenzelm@4543
   605
\item[\ttindexbold{read_cterm} $sign$ ($s$, $T$)] reads the string~$s$
wenzelm@4543
   606
  using the syntax of~$sign$, creating a certified term.  The term is
wenzelm@4543
   607
  checked to have type~$T$; this type also tells the parser what kind
wenzelm@4543
   608
  of phrase to parse.
wenzelm@4543
   609
  
wenzelm@4543
   610
\item[\ttindexbold{cert_axm} $sign$ ($name$, $t$)] certifies $t$ with
wenzelm@4543
   611
  respect to $sign$ as a meta-proposition and converts all exceptions
wenzelm@4543
   612
  to an error, including the final message
wenzelm@864
   613
\begin{ttbox}
wenzelm@864
   614
The error(s) above occurred in axiom "\(name\)"
wenzelm@864
   615
\end{ttbox}
wenzelm@864
   616
wenzelm@4543
   617
\item[\ttindexbold{read_axm} $sign$ ($name$, $s$)] similar to {\tt
wenzelm@4543
   618
    cert_axm}, but first reads the string $s$ using the syntax of
wenzelm@4543
   619
  $sign$.
wenzelm@4543
   620
  
wenzelm@4543
   621
\item[\ttindexbold{rep_cterm} $ct$] decomposes $ct$ as a record
wenzelm@4543
   622
  containing its type, the term itself, its signature, and the maximum
wenzelm@4543
   623
  subscript of its unknowns.  The type and maximum subscript are
wenzelm@4543
   624
  computed during certification.
wenzelm@4543
   625
  
wenzelm@4543
   626
\item[\ttindexbold{Sign.certify_term}] is a more primitive version of
wenzelm@4543
   627
  \texttt{cterm_of}, returning the internal representation instead of
wenzelm@4543
   628
  an abstract \texttt{cterm}.
wenzelm@864
   629
lcp@324
   630
\end{ttdescription}
lcp@104
   631
lcp@104
   632
wenzelm@864
   633
\section{Types}\index{types|bold}
wenzelm@864
   634
Types belong to the \ML\ type \mltydx{typ}, which is a concrete datatype with
wenzelm@864
   635
three constructor functions.  These correspond to type constructors, free
wenzelm@864
   636
type variables and schematic type variables.  Types are classified by sorts,
wenzelm@864
   637
which are lists of classes (representing an intersection).  A class is
wenzelm@864
   638
represented by a string.
lcp@104
   639
\begin{ttbox}
lcp@104
   640
type class = string;
lcp@104
   641
type sort  = class list;
lcp@104
   642
lcp@104
   643
datatype typ = Type  of string * typ list
lcp@104
   644
             | TFree of string * sort
lcp@104
   645
             | TVar  of indexname * sort;
lcp@104
   646
lcp@104
   647
infixr 5 -->;
wenzelm@864
   648
fun S --> T = Type ("fun", [S, T]);
lcp@104
   649
\end{ttbox}
lcp@324
   650
\begin{ttdescription}
wenzelm@4317
   651
\item[\ttindexbold{Type} ($a$, $Ts$)] \index{type constructors|bold}
lcp@324
   652
  applies the {\bf type constructor} named~$a$ to the type operands~$Ts$.
lcp@324
   653
  Type constructors include~\tydx{fun}, the binary function space
lcp@324
   654
  constructor, as well as nullary type constructors such as~\tydx{prop}.
lcp@324
   655
  Other type constructors may be introduced.  In expressions, but not in
lcp@324
   656
  patterns, \hbox{\tt$S$-->$T$} is a convenient shorthand for function
lcp@324
   657
  types.
lcp@104
   658
wenzelm@4317
   659
\item[\ttindexbold{TFree} ($a$, $s$)] \index{type variables|bold}
lcp@324
   660
  is the {\bf type variable} with name~$a$ and sort~$s$.
lcp@104
   661
wenzelm@4317
   662
\item[\ttindexbold{TVar} ($v$, $s$)] \index{type unknowns|bold}
lcp@324
   663
  is the {\bf type unknown} with indexname~$v$ and sort~$s$.
lcp@324
   664
  Type unknowns are essentially free type variables, but may be
lcp@324
   665
  instantiated during unification.
lcp@324
   666
\end{ttdescription}
lcp@104
   667
lcp@104
   668
lcp@104
   669
\section{Certified types}
lcp@104
   670
\index{types!certified|bold}
wenzelm@864
   671
Certified types, which are analogous to certified terms, have type
nipkow@275
   672
\ttindexbold{ctyp}.
lcp@104
   673
lcp@104
   674
\subsection{Printing types}
lcp@324
   675
\index{types!printing of}
wenzelm@864
   676
\begin{ttbox}
nipkow@275
   677
     string_of_ctyp :           ctyp -> string
lcp@104
   678
Sign.string_of_typ  : Sign.sg -> typ -> string
lcp@104
   679
\end{ttbox}
lcp@324
   680
\begin{ttdescription}
wenzelm@864
   681
\item[\ttindexbold{string_of_ctyp} $cT$]
lcp@104
   682
displays $cT$ as a string.
lcp@104
   683
wenzelm@864
   684
\item[\ttindexbold{Sign.string_of_typ} $sign$ $T$]
lcp@104
   685
displays $T$ as a string, using the syntax of~$sign$.
lcp@324
   686
\end{ttdescription}
lcp@104
   687
lcp@104
   688
lcp@104
   689
\subsection{Making and inspecting certified types}
wenzelm@864
   690
\begin{ttbox}
wenzelm@4543
   691
ctyp_of          : Sign.sg -> typ -> ctyp
wenzelm@4543
   692
rep_ctyp         : ctyp -> {\ttlbrace}T: typ, sign: Sign.sg\ttrbrace
wenzelm@4543
   693
Sign.certify_typ : Sign.sg -> typ -> typ
lcp@104
   694
\end{ttbox}
lcp@324
   695
\begin{ttdescription}
wenzelm@4543
   696
  
wenzelm@4543
   697
\item[\ttindexbold{ctyp_of} $sign$ $T$] \index{signatures} certifies
wenzelm@4543
   698
  $T$ with respect to signature~$sign$.
wenzelm@4543
   699
  
wenzelm@4543
   700
\item[\ttindexbold{rep_ctyp} $cT$] decomposes $cT$ as a record
wenzelm@4543
   701
  containing the type itself and its signature.
wenzelm@4543
   702
  
wenzelm@4543
   703
\item[\ttindexbold{Sign.certify_typ}] is a more primitive version of
wenzelm@4543
   704
  \texttt{ctyp_of}, returning the internal representation instead of
wenzelm@4543
   705
  an abstract \texttt{ctyp}.
lcp@104
   706
lcp@324
   707
\end{ttdescription}
lcp@104
   708
paulson@1846
   709
wenzelm@4317
   710
\section{Oracles: calling trusted external reasoners}
paulson@1846
   711
\label{sec:oracles}
paulson@1846
   712
\index{oracles|(}
paulson@1846
   713
paulson@1846
   714
Oracles allow Isabelle to take advantage of external reasoners such as
paulson@1846
   715
arithmetic decision procedures, model checkers, fast tautology checkers or
paulson@1846
   716
computer algebra systems.  Invoked as an oracle, an external reasoner can
paulson@1846
   717
create arbitrary Isabelle theorems.  It is your responsibility to ensure that
paulson@1846
   718
the external reasoner is as trustworthy as your application requires.
paulson@1846
   719
Isabelle's proof objects~(\S\ref{sec:proofObjects}) record how each theorem
paulson@1846
   720
depends upon oracle calls.
paulson@1846
   721
paulson@1846
   722
\begin{ttbox}
wenzelm@4317
   723
invoke_oracle     : theory -> xstring -> Sign.sg * object -> thm
paulson@4597
   724
Theory.add_oracle : bstring * (Sign.sg * object -> term) -> theory 
paulson@4597
   725
                    -> theory
paulson@1846
   726
\end{ttbox}
paulson@1846
   727
\begin{ttdescription}
wenzelm@4317
   728
\item[\ttindexbold{invoke_oracle} $thy$ $name$ ($sign$, $data$)]
wenzelm@4317
   729
  invokes the oracle $name$ of theory $thy$ passing the information
wenzelm@4317
   730
  contained in the exception value $data$ and creating a theorem
wenzelm@4317
   731
  having signature $sign$.  Note that type \ttindex{object} is just an
wenzelm@4317
   732
  abbreviation for \texttt{exn}.  Errors arise if $thy$ does not have
wenzelm@4317
   733
  an oracle called $name$, if the oracle rejects its arguments or if
wenzelm@4317
   734
  its result is ill-typed.
wenzelm@4317
   735
  
wenzelm@4317
   736
\item[\ttindexbold{Theory.add_oracle} $name$ $fun$ $thy$] extends
wenzelm@4317
   737
  $thy$ by oracle $fun$ called $name$.  It is seldom called
wenzelm@4317
   738
  explicitly, as there is concrete syntax for oracles in theory files.
paulson@1846
   739
\end{ttdescription}
paulson@1846
   740
paulson@1846
   741
A curious feature of {\ML} exceptions is that they are ordinary constructors.
paulson@1846
   742
The {\ML} type {\tt exn} is a datatype that can be extended at any time.  (See
paulson@1846
   743
my {\em {ML} for the Working Programmer}~\cite{paulson-ml2}, especially
paulson@1846
   744
page~136.)  The oracle mechanism takes advantage of this to allow an oracle to
paulson@1846
   745
take any information whatever.
paulson@1846
   746
paulson@1846
   747
There must be some way of invoking the external reasoner from \ML, either
paulson@1846
   748
because it is coded in {\ML} or via an operating system interface.  Isabelle
paulson@1846
   749
expects the {\ML} function to take two arguments: a signature and an
wenzelm@4317
   750
exception object.
paulson@1846
   751
\begin{itemize}
paulson@1846
   752
\item The signature will typically be that of a desendant of the theory
paulson@1846
   753
  declaring the oracle.  The oracle will use it to distinguish constants from
paulson@1846
   754
  variables, etc., and it will be attached to the generated theorems.
paulson@1846
   755
paulson@1846
   756
\item The exception is used to pass arbitrary information to the oracle.  This
paulson@1846
   757
  information must contain a full description of the problem to be solved by
paulson@1846
   758
  the external reasoner, including any additional information that might be
paulson@1846
   759
  required.  The oracle may raise the exception to indicate that it cannot
paulson@1846
   760
  solve the specified problem.
paulson@1846
   761
\end{itemize}
paulson@1846
   762
wenzelm@4317
   763
A trivial example is provided in theory {\tt FOL/ex/IffOracle}.  This
wenzelm@4317
   764
oracle generates tautologies of the form $P\bimp\cdots\bimp P$, with
wenzelm@4317
   765
an even number of $P$s.
paulson@1846
   766
wenzelm@4317
   767
The \texttt{ML} section of \texttt{IffOracle.thy} begins by declaring
wenzelm@4317
   768
a few auxiliary functions (suppressed below) for creating the
wenzelm@4317
   769
tautologies.  Then it declares a new exception constructor for the
wenzelm@4317
   770
information required by the oracle: here, just an integer. It finally
wenzelm@4317
   771
defines the oracle function itself.
paulson@1846
   772
\begin{ttbox}
wenzelm@4317
   773
exception IffOracleExn of int;\medskip
wenzelm@4317
   774
fun mk_iff_oracle (sign, IffOracleExn n) =
wenzelm@4317
   775
  if n > 0 andalso n mod 2 = 0
wenzelm@4317
   776
  then Trueprop $ mk_iff n
wenzelm@4317
   777
  else raise IffOracleExn n;
paulson@1846
   778
\end{ttbox}
wenzelm@4317
   779
Observe the function's two arguments, the signature {\tt sign} and the
wenzelm@4317
   780
exception given as a pattern.  The function checks its argument for
wenzelm@4317
   781
validity.  If $n$ is positive and even then it creates a tautology
wenzelm@4317
   782
containing $n$ occurrences of~$P$.  Otherwise it signals error by
wenzelm@4317
   783
raising its own exception (just by happy coincidence).  Errors may be
wenzelm@4317
   784
signalled by other means, such as returning the theorem {\tt True}.
wenzelm@4317
   785
Please ensure that the oracle's result is correctly typed; Isabelle
wenzelm@4317
   786
will reject ill-typed theorems by raising a cryptic exception at top
wenzelm@4317
   787
level.
paulson@1846
   788
wenzelm@4317
   789
The \texttt{oracle} section of {\tt IffOracle.thy} installs above
wenzelm@4317
   790
\texttt{ML} function as follows:
paulson@1846
   791
\begin{ttbox}
wenzelm@4317
   792
IffOracle = FOL +\medskip
wenzelm@4317
   793
oracle
wenzelm@4317
   794
  iff = mk_iff_oracle\medskip
paulson@1846
   795
end
paulson@1846
   796
\end{ttbox}
paulson@1846
   797
wenzelm@4317
   798
Now in \texttt{IffOracle.ML} we first define a wrapper for invoking
wenzelm@4317
   799
the oracle:
paulson@1846
   800
\begin{ttbox}
paulson@4597
   801
fun iff_oracle n = invoke_oracle IffOracle.thy "iff"
paulson@4597
   802
                      (sign_of IffOracle.thy, IffOracleExn n);
wenzelm@4317
   803
\end{ttbox}
wenzelm@4317
   804
wenzelm@4317
   805
Here are some example applications of the \texttt{iff} oracle.  An
wenzelm@4317
   806
argument of 10 is allowed, but one of 5 is forbidden:
wenzelm@4317
   807
\begin{ttbox}
wenzelm@4317
   808
iff_oracle 10;
paulson@1846
   809
{\out  "P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P" : thm}
wenzelm@4317
   810
iff_oracle 5;
paulson@1846
   811
{\out Exception- IffOracleExn 5 raised}
paulson@1846
   812
\end{ttbox}
paulson@1846
   813
paulson@1846
   814
\index{oracles|)}
lcp@104
   815
\index{theories|)}
wenzelm@5369
   816
wenzelm@5369
   817
wenzelm@5369
   818
%%% Local Variables: 
wenzelm@5369
   819
%%% mode: latex
wenzelm@5369
   820
%%% TeX-master: "ref"
wenzelm@5369
   821
%%% End: