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