doc-src/Ref/theories.tex
author paulson
Tue, 18 May 1999 12:35:10 +0200
changeset 6669 5f1ce866c497
parent 6658 b44dd06378cc
child 6975 42fbea767673
permissions -rw-r--r--
locale documentation (from Florian)
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}
paulson@6669
     6
\index{reading!axioms|see{\texttt{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
paulson@6669
    57
  necessary to declare \texttt{c < e} in addition to \texttt{c < d} and \texttt{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@6669
    82
  associating to the left (\texttt{infixl}) or right (\texttt{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
paulson@6669
   119
    binder} status.  The declaration \texttt{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:
paulson@6669
   126
  parse rules (\texttt{=>}), print rules (\texttt{<=}), 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@6625
   140
\item[$axclass$] \index{*axclass section} defines an \rmindex{axiomatic type
wenzelm@6625
   141
    class} \cite{Wenzel:1997:TPHOL} as the intersection of existing classes,
wenzelm@6625
   142
  with additional axioms holding.  Class axioms may not contain more than one
wenzelm@6625
   143
  type variable.  The class axioms (with implicit sort constraints added) are
wenzelm@6625
   144
  bound to the given names.  Furthermore a class introduction rule is
wenzelm@6625
   145
  generated, which is automatically employed by $instance$ to prove
wenzelm@6625
   146
  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
paulson@6669
   181
declarations, translation rules and the \texttt{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
wenzelm@6658
   251
remove_thy      : string -> unit
lcp@286
   252
delete_tmpfiles : bool ref \hfill{\bf initially true}
lcp@286
   253
\end{ttbox}
nipkow@275
   254
lcp@324
   255
\begin{ttdescription}
wenzelm@6569
   256
\item[\ttindexbold{use_thy_only} "$name$";] is similar to \texttt{use_thy},
wenzelm@6569
   257
  but processes the actual theory file $name$\texttt{.thy} only, ignoring
wenzelm@6568
   258
  $name$\texttt{.ML}.  This might be useful in replaying proof scripts by hand
wenzelm@6568
   259
  from the very beginning, starting with the fresh theory.
wenzelm@6568
   260
  
wenzelm@6569
   261
\item[\ttindexbold{update_thy} "$name$";] is similar to \texttt{use_thy}, but
wenzelm@6568
   262
  ensures that theory $name$ is fully up-to-date with respect to the file
wenzelm@6571
   263
  system --- apart from $name$ itself any of its ancestors may be reloaded as
wenzelm@6571
   264
  well.
wenzelm@6568
   265
  
wenzelm@6569
   266
\item[\ttindexbold{touch_thy} "$name$";] marks theory node $name$ of the
wenzelm@6568
   267
  internal graph as outdated.  While the theory remains usable, subsequent
wenzelm@6568
   268
  operations such as \texttt{use_thy} may cause a reload.
wenzelm@6568
   269
  
wenzelm@6658
   270
\item[\ttindexbold{remove_thy} "$name$";] deletes theory node $name$,
wenzelm@6658
   271
  including \emph{all} of its descendants.  Beware!  This is a quick way to
wenzelm@6658
   272
  dispose a large number of theories at once.  Note that {\ML} bindings to
wenzelm@6658
   273
  theorems etc.\ of removed theories may still persist.
wenzelm@6658
   274
  
wenzelm@6568
   275
\item[reset \ttindexbold{delete_tmpfiles};] processing theory files usually
wenzelm@6568
   276
  involves temporary {\ML} files to be created.  By default, these are deleted
wenzelm@6568
   277
  afterwards.  Resetting the \texttt{delete_tmpfiles} flag inhibits this,
wenzelm@6568
   278
  leaving the generated code for debugging purposes.  The basic location for
wenzelm@6568
   279
  temporary files is determined by the \texttt{ISABELLE_TMP} environment
wenzelm@6571
   280
  variable (which is private to the running Isabelle process and may be
wenzelm@6568
   281
  retrieved by \ttindex{getenv} from {\ML}).
wenzelm@6568
   282
\end{ttdescription}
clasohm@138
   283
wenzelm@6568
   284
\medskip Theory and {\ML} files are located by skimming through the
wenzelm@6568
   285
directories listed in Isabelle's internal load path, which merely contains the
wenzelm@6568
   286
current directory ``\texttt{.}'' by default.  The load path may be accessed by
wenzelm@6568
   287
the following operations.
lcp@286
   288
wenzelm@864
   289
\begin{ttbox}
wenzelm@6568
   290
show_path: unit -> string list
wenzelm@6568
   291
add_path: string -> unit
wenzelm@6568
   292
del_path: string -> unit
wenzelm@6568
   293
reset_path: unit -> unit
lcp@286
   294
\end{ttbox}
clasohm@138
   295
lcp@324
   296
\begin{ttdescription}
wenzelm@6568
   297
\item[\ttindexbold{show_path}();] displays the load path components in
wenzelm@6571
   298
  canonical string representation (which is always according to Unix rules).
wenzelm@6568
   299
  
wenzelm@6569
   300
\item[\ttindexbold{add_path} "$dir$";] adds component $dir$ to the beginning
wenzelm@6569
   301
  of the load path.
wenzelm@6568
   302
  
wenzelm@6569
   303
\item[\ttindexbold{del_path} "$dir$";] removes any occurrences of component
wenzelm@6568
   304
  $dir$ from the load path.
wenzelm@6568
   305
  
wenzelm@6568
   306
\item[\ttindexbold{reset_path}();] resets the load path to ``\texttt{.}''
wenzelm@6568
   307
  (current directory) only.
lcp@324
   308
\end{ttdescription}
clasohm@138
   309
wenzelm@6571
   310
In operations referring indirectly to some file, the argument may be prefixed
wenzelm@6571
   311
by a directory that will be used as temporary load path, e.g.\ 
wenzelm@6571
   312
\texttt{use_thy~"$dir/name$"}.  Note that, depending on which parts of the
wenzelm@6571
   313
ancestry of $name$ are already loaded, the dynamic change of paths might be
wenzelm@6571
   314
hard to predict.  Use this feature with care only.
lcp@104
   315
lcp@104
   316
paulson@6669
   317
\section{Locales}
paulson@6669
   318
\label{Locales}
paulson@6669
   319
paulson@6669
   320
Locales \cite{kammueller-locales} are a concept of local proof contexts.  They
paulson@6669
   321
are introduced as named syntactic objects within theories and can be
paulson@6669
   322
opened in any descendant theory.
paulson@6669
   323
paulson@6669
   324
\subsection{Declaring Locales}
paulson@6669
   325
paulson@6669
   326
A locale is declared in a theory section that starts with the
paulson@6669
   327
keyword \texttt{locale}.  It consists typically of three parts, the
paulson@6669
   328
\texttt{fixes} part, the \texttt{assumes} part, and the \texttt{defines} part.
paulson@6669
   329
Appendix \ref{app:TheorySyntax} presents the full syntax.
paulson@6669
   330
paulson@6669
   331
\subsubsection{Parts of Locales}
paulson@6669
   332
paulson@6669
   333
The subsection introduced by the keyword \texttt{fixes} declares the locale
paulson@6669
   334
constants in a way that closely resembles a global \texttt{consts}
paulson@6669
   335
declaration.  In particular, there may be an optional pretty printing syntax
paulson@6669
   336
for the locale constants.
paulson@6669
   337
paulson@6669
   338
The subsequent \texttt{assumes} part specifies the locale rules.  They are
paulson@6669
   339
defined like \texttt{rules}: by an identifier followed by the rule
paulson@6669
   340
given as a string.  Locale rules admit the statement of local assumptions
paulson@6669
   341
about the locale constants.  The \texttt{assumes} part is optional.  Non-fixed
paulson@6669
   342
variables in locale rules are automatically bound by the universal quantifier
paulson@6669
   343
\texttt{!!} of the meta-logic.
paulson@6669
   344
paulson@6669
   345
Finally, the \texttt{defines} part introduces the definitions that are
paulson@6669
   346
available in the locale.  Locale constants declared in the \texttt{fixes}
paulson@6669
   347
section are defined using the meta-equality \texttt{==}.  If the
paulson@6669
   348
locale constant is a functiond then its definition can (as usual) have
paulson@6669
   349
variables on the left-hand side acting as formal parameters; they are
paulson@6669
   350
considered as schematic variables and are automatically generalized by
paulson@6669
   351
universal quantification of the meta-logic.  The right hand side of a
paulson@6669
   352
definition must not contain variables that are not already on the left hand
paulson@6669
   353
side.  In so far locale definitions behave like theory level definitions.
paulson@6669
   354
However, the locale concept realizes \emph{dependent definitions}: any variable
paulson@6669
   355
that is fixed as a locale constant can occur on the right hand side of
paulson@6669
   356
definitions.  For an illustration of these dependent definitions see the
paulson@6669
   357
occurrence of the locale constant \texttt{G} on the right hand side of the
paulson@6669
   358
definitions of the locale \texttt{group} below.  Naturally, definitions can
paulson@6669
   359
already use the syntax of the locale constants in the \texttt{fixes}
paulson@6669
   360
subsection.  The \texttt{defines} part is, as the \texttt{assumes} part,
paulson@6669
   361
optional.
paulson@6669
   362
paulson@6669
   363
\subsubsection{Example for Definition}
paulson@6669
   364
The concrete syntax of locale definitions is demonstrated by example below.
paulson@6669
   365
paulson@6669
   366
Locale \texttt{group} assumes the definition of groups in a theory
paulson@6669
   367
file\footnote{This and other examples are from \texttt{HOL/ex}.}.  A locale
paulson@6669
   368
defining a convenient proof environment for group related proofs may be
paulson@6669
   369
added to the theory as follows:
paulson@6669
   370
\begin{ttbox}
paulson@6669
   371
  locale group =
paulson@6669
   372
    fixes 
paulson@6669
   373
      G         :: "'a grouptype"
paulson@6669
   374
      e         :: "'a"
paulson@6669
   375
      binop     :: "'a => 'a => 'a"        (infixr "#" 80)
paulson@6669
   376
      inv       :: "'a => 'a"              ("i(_)" [90] 91)
paulson@6669
   377
    assumes
paulson@6669
   378
      Group_G   "G: Group"
paulson@6669
   379
    defines
paulson@6669
   380
      e_def     "e == unit G"
paulson@6669
   381
      binop_def "x # y == bin_op G x y"
paulson@6669
   382
      inv_def   "i(x) == inverse G x"
paulson@6669
   383
\end{ttbox}
paulson@6669
   384
paulson@6669
   385
\subsubsection{Polymorphism}
paulson@6669
   386
paulson@6669
   387
In contrast to polymorphic definitions in theories, the use of the
paulson@6669
   388
same type variable for the declaration of different locale constants in the
paulson@6669
   389
fixes part means \emph{the same} type.  In other words, the scope of the
paulson@6669
   390
polymorphic variables is extended over all constant declarations of a locale.
paulson@6669
   391
In the above example \texttt{'a} refers to the same type which is fixed inside
paulson@6669
   392
the locale.  In an exported theorem (see \S\ref{sec:locale-export}) the
paulson@6669
   393
constructors of locale \texttt{group} are polymorphic, yet only simultaneously
paulson@6669
   394
instantiatable.
paulson@6669
   395
paulson@6669
   396
\subsubsection{Nested Locales}
paulson@6669
   397
paulson@6669
   398
A locale can be defined as the extension of a previously defined
paulson@6669
   399
locale.  This operation of extension is optional and is syntactically
paulson@6669
   400
expressed as 
paulson@6669
   401
\begin{ttbox}
paulson@6669
   402
locale foo = bar + ...
paulson@6669
   403
\end{ttbox}
paulson@6669
   404
The locale \texttt{foo} builds on the constants and syntax of the locale {\tt
paulson@6669
   405
bar}.  That is, all contents of the locale \texttt{bar} can be used in
paulson@6669
   406
definitions and rules of the corresponding parts of the locale {\tt
paulson@6669
   407
foo}.  Although locale \texttt{foo} assumes the \texttt{fixes} part of \texttt{bar} it
paulson@6669
   408
does not automatically subsume its rules and definitions.  Normally, one
paulson@6669
   409
expects to use locale \texttt{foo} only if locale \texttt{bar} is already
paulson@6669
   410
active.  These aspects of use and activation of locales are considered in the
paulson@6669
   411
subsequent section.
paulson@6669
   412
paulson@6669
   413
paulson@6669
   414
\subsection{Locale Scope}
paulson@6669
   415
paulson@6669
   416
Locales are by default inactive, but they can be invoked.  The list of
paulson@6669
   417
currently active locales is called \emph{scope}.  The process of activating
paulson@6669
   418
them is called \emph{opening}; the reverse is \emph{closing}.
paulson@6669
   419
paulson@6669
   420
\subsubsection{Scope}
paulson@6669
   421
The locale scope is part of each theory.  It is a dynamic stack containing
paulson@6669
   422
all active locales at a certain point in an interactive session.
paulson@6669
   423
The scope lives until all locales are explicitly closed.  At one time there
paulson@6669
   424
can be more than one locale open.  The contents of these various active
paulson@6669
   425
locales are all visible in the scope.  In case of nested locales for example,
paulson@6669
   426
the nesting is actually reflected to the scope, which contains the nested
paulson@6669
   427
locales as layers.  To check the state of the scope during a development the
paulson@6669
   428
function \texttt{Print\_scope} may be used.  It displays the names of all open
paulson@6669
   429
locales on the scope.  The function \texttt{print\_locales} applied to a theory
paulson@6669
   430
displays all locales contained in that theory and in addition also the
paulson@6669
   431
current scope.
paulson@6669
   432
paulson@6669
   433
The scope is manipulated by the commands for opening and closing of locales. 
paulson@6669
   434
paulson@6669
   435
\subsubsection{Opening}
paulson@6669
   436
Locales can be \emph{opened} at any point during a session where
paulson@6669
   437
we want to prove theorems concerning the locale.  Opening a locale means
paulson@6669
   438
making its contents visible by pushing it onto the scope of the current
paulson@6669
   439
theory.  Inside a scope of opened locales, theorems can use all definitions and
paulson@6669
   440
rules contained in the locales on the scope.  The rules and definitions may
paulson@6669
   441
be accessed individually using the function \ttindex{thm}.  This function is
paulson@6669
   442
applied to the names assigned to locale rules and definitions as
paulson@6669
   443
strings.  The opening command is called \texttt{Open\_locale} and takes the 
paulson@6669
   444
name of the locale to be opened as its argument.
paulson@6669
   445
paulson@6669
   446
If one opens a locale \texttt{foo} that is defined by extension from locale
paulson@6669
   447
\texttt{bar}, the function \texttt{Open\_locale} checks if locale \texttt{bar}
paulson@6669
   448
is open.  If so, then it just opens \texttt{foo}, if not, then it prints a
paulson@6669
   449
message and opens \texttt{bar} before opening \texttt{foo}.  Naturally, this
paulson@6669
   450
carries on, if \texttt{bar} is again an extension.
paulson@6669
   451
paulson@6669
   452
\subsubsection{Closing}
paulson@6669
   453
paulson@6669
   454
\emph{Closing} means to cancel the last opened locale, pushing it out of the
paulson@6669
   455
scope.  Theorems proved during the life cycle of this locale will be disabled,
paulson@6669
   456
unless they have been explicitly exported, as described below.  However, when
paulson@6669
   457
the same locale is opened again these theorems may be used again as well,
paulson@6669
   458
provided that they were saved as theorems in the first place, using
paulson@6669
   459
\texttt{qed} or ML assignment.  The command \texttt{Close\_locale} takes a
paulson@6669
   460
locale name as a string and checks if this locale is actually the topmost
paulson@6669
   461
locale on the scope.  If this is the case, it removes this locale, otherwise
paulson@6669
   462
it prints a warning message and does not change the scope.
paulson@6669
   463
paulson@6669
   464
\subsubsection{Export of Theorems}
paulson@6669
   465
\label{sec:locale-export}
paulson@6669
   466
paulson@6669
   467
Export of theorems transports theorems out of the scope of locales.  Locale
paulson@6669
   468
rules that have been used in the proof of an exported theorem inside the
paulson@6669
   469
locale are carried by the exported form of the theorem as its individual
paulson@6669
   470
meta-assumptions.  The locale constants are universally quantified variables
paulson@6669
   471
in these theorems, hence such theorems can be instantiated individually.
paulson@6669
   472
Definitions become unfolded; locale constants that were merely used for
paulson@6669
   473
definitions vanish.  Logically, exporting corresponds to a combined
paulson@6669
   474
application of introduction rules for implication and universal
paulson@6669
   475
quantification.  Exporting forms a kind of normalization of theorems in a
paulson@6669
   476
locale scope.
paulson@6669
   477
paulson@6669
   478
According to the possibility of nested locales there are two different forms
paulson@6669
   479
of export.  The first one is realized by the function \texttt{export} that
paulson@6669
   480
exports theorems through all layers of opened locales of the scope.  Hence,
paulson@6669
   481
the application of export to a theorem yields a theorem of the global level,
paulson@6669
   482
that is, the current theory context without any local assumptions or
paulson@6669
   483
definitions.
paulson@6669
   484
paulson@6669
   485
When locales are nested we might want to export a theorem, not to the global
paulson@6669
   486
level of the current theory but just to the previous level.  The other export
paulson@6669
   487
function, \texttt{Export}, transports theorems one level up in the scope; the
paulson@6669
   488
theorem still uses locale constants, definitions and rules of the locales
paulson@6669
   489
underneath.
paulson@6669
   490
paulson@6669
   491
\subsection{Functions for Locales}
paulson@6669
   492
\label{Syntax}
paulson@6669
   493
\index{locales!functions}
paulson@6669
   494
paulson@6669
   495
Here is a quick reference list of locale functions.
paulson@6669
   496
\begin{ttbox}
paulson@6669
   497
  Open_locale  : xstring -> unit 
paulson@6669
   498
  Close_locale : xstring -> unit
paulson@6669
   499
  export       :     thm -> thm
paulson@6669
   500
  Export       :     thm -> thm
paulson@6669
   501
  thm          : xstring -> thm
paulson@6669
   502
  Print_scope  :    unit -> unit
paulson@6669
   503
  print_locales:  theory -> unit
paulson@6669
   504
\end{ttbox}
paulson@6669
   505
\begin{ttdescription}
paulson@6669
   506
\item[\ttindexbold{Open_locale} $xstring$] 
paulson@6669
   507
    opens the locale {\it xstring}, adding it to the scope of the theory of the
paulson@6669
   508
  current context.  If the opened locale is built by extension, the ancestors
paulson@6669
   509
  are opened automatically.
paulson@6669
   510
  
paulson@6669
   511
\item[\ttindexbold{Close_locale} $xstring$] eliminates the locale {\it
paulson@6669
   512
    xstring} from the scope if it is the topmost item on it, otherwise it does
paulson@6669
   513
  not change the scope and produces a warning.
paulson@6669
   514
paulson@6669
   515
\item[\ttindexbold{export} $thm$] locale definitions become expanded in {\it
paulson@6669
   516
    thm} and locale rules that were used in the proof of {\it thm} become part
paulson@6669
   517
  of its individual assumptions.  This normalization happens with respect to
paulson@6669
   518
  \emph{all open locales} on the scope.
paulson@6669
   519
  
paulson@6669
   520
\item[\ttindexbold{Export} $thm$] works like \texttt{export} but normalizes
paulson@6669
   521
  theorems only up to the previous level of locales on the scope.
paulson@6669
   522
  
paulson@6669
   523
\item[\ttindexbold{thm} $xstring$] applied to the name of a locale definition
paulson@6669
   524
  or rule it returns the definition as a theorem.
paulson@6669
   525
  
paulson@6669
   526
\item[\ttindexbold{Print_scope}()] prints the names of the locales in the
paulson@6669
   527
  current scope of the current theory context.
paulson@6669
   528
  
paulson@6669
   529
\item[\ttindexbold{print_locale} $theory$] prints all locales that are
paulson@6669
   530
  contained in {\it theory} directly or indirectly.  It also displays the
paulson@6669
   531
  current scope similar to \texttt{Print\_scope}.
paulson@6669
   532
\end{ttdescription}
paulson@6669
   533
paulson@6669
   534
clasohm@866
   535
\section{Basic operations on theories}\label{BasicOperationsOnTheories}
wenzelm@4384
   536
wenzelm@4384
   537
\subsection{*Theory inclusion}
wenzelm@4384
   538
\begin{ttbox}
wenzelm@4384
   539
subthy      : theory * theory -> bool
wenzelm@4384
   540
eq_thy      : theory * theory -> bool
wenzelm@4384
   541
transfer    : theory -> thm -> thm
wenzelm@4384
   542
transfer_sg : Sign.sg -> thm -> thm
wenzelm@4384
   543
\end{ttbox}
wenzelm@4384
   544
wenzelm@4384
   545
Inclusion and equality of theories is determined by unique
wenzelm@4384
   546
identification stamps that are created when declaring new components.
wenzelm@4384
   547
Theorems contain a reference to the theory (actually to its signature)
wenzelm@4384
   548
they have been derived in.  Transferring theorems to super theories
wenzelm@4384
   549
has no logical significance, but may affect some operations in subtle
wenzelm@4384
   550
ways (e.g.\ implicit merges of signatures when applying rules, or
wenzelm@4384
   551
pretty printing of theorems).
wenzelm@4384
   552
wenzelm@4384
   553
\begin{ttdescription}
wenzelm@4384
   554
wenzelm@4384
   555
\item[\ttindexbold{subthy} ($thy@1$, $thy@2$)] determines if $thy@1$
wenzelm@4384
   556
  is included in $thy@2$ wrt.\ identification stamps.
wenzelm@4384
   557
wenzelm@4384
   558
\item[\ttindexbold{eq_thy} ($thy@1$, $thy@2$)] determines if $thy@1$
wenzelm@4384
   559
  is exactly the same as $thy@2$.
wenzelm@4384
   560
wenzelm@4384
   561
\item[\ttindexbold{transfer} $thy$ $thm$] transfers theorem $thm$ to
wenzelm@4384
   562
  theory $thy$, provided the latter includes the theory of $thm$.
wenzelm@4384
   563
  
wenzelm@4384
   564
\item[\ttindexbold{transfer_sg} $sign$ $thm$] is similar to
wenzelm@4384
   565
  \texttt{transfer}, but identifies the super theory via its
wenzelm@4384
   566
  signature.
wenzelm@4384
   567
wenzelm@4384
   568
\end{ttdescription}
wenzelm@4384
   569
wenzelm@4384
   570
wenzelm@6571
   571
\subsection{*Primitive theories}
wenzelm@864
   572
\begin{ttbox}
wenzelm@4317
   573
ProtoPure.thy  : theory
wenzelm@3108
   574
Pure.thy       : theory
wenzelm@3108
   575
CPure.thy      : theory
lcp@286
   576
\end{ttbox}
wenzelm@3108
   577
\begin{description}
wenzelm@4317
   578
\item[\ttindexbold{ProtoPure.thy}, \ttindexbold{Pure.thy},
wenzelm@4317
   579
  \ttindexbold{CPure.thy}] contain the syntax and signature of the
wenzelm@4317
   580
  meta-logic.  There are basically no axioms: meta-level inferences
wenzelm@4317
   581
  are carried out by \ML\ functions.  \texttt{Pure} and \texttt{CPure}
wenzelm@4317
   582
  just differ in their concrete syntax of prefix function application:
wenzelm@4317
   583
  $t(u@1, \ldots, u@n)$ in \texttt{Pure} vs.\ $t\,u@1,\ldots\,u@n$ in
wenzelm@4317
   584
  \texttt{CPure}.  \texttt{ProtoPure} is their common parent,
wenzelm@4317
   585
  containing no syntax for printing prefix applications at all!
wenzelm@6571
   586
wenzelm@6571
   587
%%FIXME  
wenzelm@6571
   588
%\item[\ttindexbold{merge_theories} $name$ ($thy@1$, $thy@2$)] merges
wenzelm@6571
   589
%  the two theories $thy@1$ and $thy@2$, creating a new named theory
wenzelm@6571
   590
%  node.  The resulting theory contains all of the syntax, signature
wenzelm@6571
   591
%  and axioms of the constituent theories.  Merging theories that
wenzelm@6571
   592
%  contain different identification stamps of the same name fails with
wenzelm@6571
   593
%  the following message
wenzelm@864
   594
wenzelm@864
   595
%% FIXME
nipkow@478
   596
%\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"} $\cdots$] extends
nipkow@478
   597
%  the theory $thy$ with new types, constants, etc.  $T$ identifies the theory
nipkow@478
   598
%  internally.  When a theory is redeclared, say to change an incorrect axiom,
nipkow@478
   599
%  bindings to the old axiom may persist.  Isabelle ensures that the old and
nipkow@478
   600
%  new theories are not involved in the same proof.  Attempting to combine
nipkow@478
   601
%  different theories having the same name $T$ yields the fatal error
nipkow@478
   602
%extend_theory  : theory -> string -> \(\cdots\) -> theory
wenzelm@864
   603
%\begin{ttbox}
wenzelm@864
   604
%Attempt to merge different versions of theory: \(T\)
wenzelm@864
   605
%\end{ttbox}
wenzelm@3108
   606
\end{description}
lcp@286
   607
wenzelm@864
   608
%% FIXME
nipkow@275
   609
%\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"}
nipkow@275
   610
%      ($classes$, $default$, $types$, $arities$, $consts$, $sextopt$) $rules$]
nipkow@275
   611
%\hfill\break   %%% include if line is just too short
lcp@286
   612
%is the \ML{} equivalent of the following theory definition:
nipkow@275
   613
%\begin{ttbox}
nipkow@275
   614
%\(T\) = \(thy\) +
nipkow@275
   615
%classes \(c\) < \(c@1\),\(\dots\),\(c@m\)
nipkow@275
   616
%        \dots
nipkow@275
   617
%default {\(d@1,\dots,d@r\)}
nipkow@275
   618
%types   \(tycon@1\),\dots,\(tycon@i\) \(n\)
nipkow@275
   619
%        \dots
nipkow@275
   620
%arities \(tycon@1'\),\dots,\(tycon@j'\) :: (\(s@1\),\dots,\(s@n\))\(c\)
nipkow@275
   621
%        \dots
nipkow@275
   622
%consts  \(b@1\),\dots,\(b@k\) :: \(\tau\)
nipkow@275
   623
%        \dots
nipkow@275
   624
%rules   \(name\) \(rule\)
nipkow@275
   625
%        \dots
nipkow@275
   626
%end
nipkow@275
   627
%\end{ttbox}
nipkow@275
   628
%where
nipkow@275
   629
%\begin{tabular}[t]{l@{~=~}l}
nipkow@275
   630
%$classes$ & \tt[("$c$",["$c@1$",\dots,"$c@m$"]),\dots] \\
nipkow@275
   631
%$default$ & \tt["$d@1$",\dots,"$d@r$"]\\
nipkow@275
   632
%$types$   & \tt[([$tycon@1$,\dots,$tycon@i$], $n$),\dots] \\
nipkow@275
   633
%$arities$ & \tt[([$tycon'@1$,\dots,$tycon'@j$], ([$s@1$,\dots,$s@n$],$c$)),\dots]
nipkow@275
   634
%\\
nipkow@275
   635
%$consts$  & \tt[([$b@1$,\dots,$b@k$],$\tau$),\dots] \\
nipkow@275
   636
%$rules$   & \tt[("$name$",$rule$),\dots]
nipkow@275
   637
%\end{tabular}
lcp@104
   638
lcp@104
   639
wenzelm@864
   640
\subsection{Inspecting a theory}\label{sec:inspct-thy}
lcp@104
   641
\index{theories!inspecting|bold}
wenzelm@864
   642
\begin{ttbox}
wenzelm@4317
   643
print_syntax        : theory -> unit
wenzelm@4317
   644
print_theory        : theory -> unit
wenzelm@4317
   645
parents_of          : theory -> theory list
wenzelm@4317
   646
ancestors_of        : theory -> theory list
wenzelm@4317
   647
sign_of             : theory -> Sign.sg
wenzelm@4317
   648
Sign.stamp_names_of : Sign.sg -> string list
lcp@104
   649
\end{ttbox}
wenzelm@864
   650
These provide means of viewing a theory's components.
lcp@324
   651
\begin{ttdescription}
wenzelm@3108
   652
\item[\ttindexbold{print_syntax} $thy$] prints the syntax of $thy$
wenzelm@3108
   653
  (grammar, macros, translation functions etc., see
wenzelm@3108
   654
  page~\pageref{pg:print_syn} for more details).
wenzelm@3108
   655
  
wenzelm@3108
   656
\item[\ttindexbold{print_theory} $thy$] prints the logical parts of
wenzelm@3108
   657
  $thy$, excluding the syntax.
wenzelm@4317
   658
  
wenzelm@4317
   659
\item[\ttindexbold{parents_of} $thy$] returns the direct ancestors
wenzelm@4317
   660
  of~$thy$.
wenzelm@4317
   661
  
wenzelm@4317
   662
\item[\ttindexbold{ancestors_of} $thy$] returns all ancestors of~$thy$
wenzelm@4317
   663
  (not including $thy$ itself).
wenzelm@4317
   664
  
wenzelm@4317
   665
\item[\ttindexbold{sign_of} $thy$] returns the signature associated
wenzelm@4317
   666
  with~$thy$.  It is useful with functions like {\tt
wenzelm@4317
   667
    read_instantiate_sg}, which take a signature as an argument.
wenzelm@4317
   668
  
wenzelm@4317
   669
\item[\ttindexbold{Sign.stamp_names_of} $sg$]\index{signatures}
wenzelm@4317
   670
  returns the names of the identification \rmindex{stamps} of ax
wenzelm@4317
   671
  signature.  These coincide with the names of its full ancestry
wenzelm@4317
   672
  including that of $sg$ itself.
lcp@104
   673
lcp@324
   674
\end{ttdescription}
lcp@104
   675
clasohm@1369
   676
lcp@104
   677
\section{Terms}
lcp@104
   678
\index{terms|bold}
lcp@324
   679
Terms belong to the \ML\ type \mltydx{term}, which is a concrete datatype
wenzelm@3108
   680
with six constructors:
lcp@104
   681
\begin{ttbox}
lcp@104
   682
type indexname = string * int;
lcp@104
   683
infix 9 $;
lcp@104
   684
datatype term = Const of string * typ
lcp@104
   685
              | Free  of string * typ
lcp@104
   686
              | Var   of indexname * typ
lcp@104
   687
              | Bound of int
lcp@104
   688
              | Abs   of string * typ * term
lcp@104
   689
              | op $  of term * term;
lcp@104
   690
\end{ttbox}
lcp@324
   691
\begin{ttdescription}
wenzelm@4317
   692
\item[\ttindexbold{Const} ($a$, $T$)] \index{constants|bold}
lcp@286
   693
  is the {\bf constant} with name~$a$ and type~$T$.  Constants include
lcp@286
   694
  connectives like $\land$ and $\forall$ as well as constants like~0
lcp@286
   695
  and~$Suc$.  Other constants may be required to define a logic's concrete
wenzelm@864
   696
  syntax.
lcp@104
   697
wenzelm@4317
   698
\item[\ttindexbold{Free} ($a$, $T$)] \index{variables!free|bold}
lcp@324
   699
  is the {\bf free variable} with name~$a$ and type~$T$.
lcp@104
   700
wenzelm@4317
   701
\item[\ttindexbold{Var} ($v$, $T$)] \index{unknowns|bold}
lcp@324
   702
  is the {\bf scheme variable} with indexname~$v$ and type~$T$.  An
lcp@324
   703
  \mltydx{indexname} is a string paired with a non-negative index, or
lcp@324
   704
  subscript; a term's scheme variables can be systematically renamed by
lcp@324
   705
  incrementing their subscripts.  Scheme variables are essentially free
lcp@324
   706
  variables, but may be instantiated during unification.
lcp@104
   707
lcp@324
   708
\item[\ttindexbold{Bound} $i$] \index{variables!bound|bold}
lcp@324
   709
  is the {\bf bound variable} with de Bruijn index~$i$, which counts the
lcp@324
   710
  number of lambdas, starting from zero, between a variable's occurrence
lcp@324
   711
  and its binding.  The representation prevents capture of variables.  For
lcp@324
   712
  more information see de Bruijn \cite{debruijn72} or
paulson@6592
   713
  Paulson~\cite[page~376]{paulson-ml2}.
lcp@104
   714
wenzelm@4317
   715
\item[\ttindexbold{Abs} ($a$, $T$, $u$)]
lcp@324
   716
  \index{lambda abs@$\lambda$-abstractions|bold}
lcp@324
   717
  is the $\lambda$-{\bf abstraction} with body~$u$, and whose bound
lcp@324
   718
  variable has name~$a$ and type~$T$.  The name is used only for parsing
lcp@324
   719
  and printing; it has no logical significance.
lcp@104
   720
lcp@324
   721
\item[$t$ \$ $u$] \index{$@{\tt\$}|bold} \index{function applications|bold}
wenzelm@864
   722
is the {\bf application} of~$t$ to~$u$.
lcp@324
   723
\end{ttdescription}
lcp@286
   724
Application is written as an infix operator to aid readability.
lcp@332
   725
Here is an \ML\ pattern to recognize \FOL{} formulae of
lcp@104
   726
the form~$A\imp B$, binding the subformulae to~$A$ and~$B$:
wenzelm@864
   727
\begin{ttbox}
lcp@104
   728
Const("Trueprop",_) $ (Const("op -->",_) $ A $ B)
lcp@104
   729
\end{ttbox}
lcp@104
   730
lcp@104
   731
wenzelm@4317
   732
\section{*Variable binding}
lcp@286
   733
\begin{ttbox}
lcp@286
   734
loose_bnos     : term -> int list
lcp@286
   735
incr_boundvars : int -> term -> term
lcp@286
   736
abstract_over  : term*term -> term
lcp@286
   737
variant_abs    : string * typ * term -> string * term
wenzelm@4374
   738
aconv          : term * term -> bool\hfill{\bf infix}
lcp@286
   739
\end{ttbox}
lcp@286
   740
These functions are all concerned with the de Bruijn representation of
lcp@286
   741
bound variables.
lcp@324
   742
\begin{ttdescription}
wenzelm@864
   743
\item[\ttindexbold{loose_bnos} $t$]
lcp@286
   744
  returns the list of all dangling bound variable references.  In
paulson@6669
   745
  particular, \texttt{Bound~0} is loose unless it is enclosed in an
paulson@6669
   746
  abstraction.  Similarly \texttt{Bound~1} is loose unless it is enclosed in
lcp@286
   747
  at least two abstractions; if enclosed in just one, the list will contain
lcp@286
   748
  the number 0.  A well-formed term does not contain any loose variables.
lcp@286
   749
wenzelm@864
   750
\item[\ttindexbold{incr_boundvars} $j$]
lcp@332
   751
  increases a term's dangling bound variables by the offset~$j$.  This is
lcp@286
   752
  required when moving a subterm into a context where it is enclosed by a
lcp@286
   753
  different number of abstractions.  Bound variables with a matching
lcp@286
   754
  abstraction are unaffected.
lcp@286
   755
wenzelm@864
   756
\item[\ttindexbold{abstract_over} $(v,t)$]
lcp@286
   757
  forms the abstraction of~$t$ over~$v$, which may be any well-formed term.
paulson@6669
   758
  It replaces every occurrence of \(v\) by a \texttt{Bound} variable with the
lcp@286
   759
  correct index.
lcp@286
   760
wenzelm@864
   761
\item[\ttindexbold{variant_abs} $(a,T,u)$]
lcp@286
   762
  substitutes into $u$, which should be the body of an abstraction.
lcp@286
   763
  It replaces each occurrence of the outermost bound variable by a free
lcp@286
   764
  variable.  The free variable has type~$T$ and its name is a variant
lcp@332
   765
  of~$a$ chosen to be distinct from all constants and from all variables
lcp@286
   766
  free in~$u$.
lcp@286
   767
wenzelm@864
   768
\item[$t$ \ttindexbold{aconv} $u$]
lcp@286
   769
  tests whether terms~$t$ and~$u$ are \(\alpha\)-convertible: identical up
lcp@286
   770
  to renaming of bound variables.
lcp@286
   771
\begin{itemize}
lcp@286
   772
  \item
paulson@6669
   773
    Two constants, \texttt{Free}s, or \texttt{Var}s are \(\alpha\)-convertible
lcp@286
   774
    if their names and types are equal.
lcp@286
   775
    (Variables having the same name but different types are thus distinct.
lcp@286
   776
    This confusing situation should be avoided!)
lcp@286
   777
  \item
lcp@286
   778
    Two bound variables are \(\alpha\)-convertible
lcp@286
   779
    if they have the same number.
lcp@286
   780
  \item
lcp@286
   781
    Two abstractions are \(\alpha\)-convertible
lcp@286
   782
    if their bodies are, and their bound variables have the same type.
lcp@286
   783
  \item
lcp@286
   784
    Two applications are \(\alpha\)-convertible
lcp@286
   785
    if the corresponding subterms are.
lcp@286
   786
\end{itemize}
lcp@286
   787
lcp@324
   788
\end{ttdescription}
lcp@286
   789
wenzelm@864
   790
\section{Certified terms}\index{terms!certified|bold}\index{signatures}
wenzelm@864
   791
A term $t$ can be {\bf certified} under a signature to ensure that every type
wenzelm@864
   792
in~$t$ is well-formed and every constant in~$t$ is a type instance of a
wenzelm@864
   793
constant declared in the signature.  The term must be well-typed and its use
paulson@6669
   794
of bound variables must be well-formed.  Meta-rules such as \texttt{forall_elim}
wenzelm@864
   795
take certified terms as arguments.
lcp@104
   796
lcp@324
   797
Certified terms belong to the abstract type \mltydx{cterm}.
lcp@104
   798
Elements of the type can only be created through the certification process.
lcp@104
   799
In case of error, Isabelle raises exception~\ttindex{TERM}\@.
lcp@104
   800
lcp@104
   801
\subsection{Printing terms}
lcp@324
   802
\index{terms!printing of}
wenzelm@864
   803
\begin{ttbox}
nipkow@275
   804
     string_of_cterm :           cterm -> string
lcp@104
   805
Sign.string_of_term  : Sign.sg -> term -> string
lcp@104
   806
\end{ttbox}
lcp@324
   807
\begin{ttdescription}
wenzelm@864
   808
\item[\ttindexbold{string_of_cterm} $ct$]
lcp@104
   809
displays $ct$ as a string.
lcp@104
   810
wenzelm@864
   811
\item[\ttindexbold{Sign.string_of_term} $sign$ $t$]
lcp@104
   812
displays $t$ as a string, using the syntax of~$sign$.
lcp@324
   813
\end{ttdescription}
lcp@104
   814
lcp@104
   815
\subsection{Making and inspecting certified terms}
wenzelm@864
   816
\begin{ttbox}
wenzelm@4543
   817
cterm_of          : Sign.sg -> term -> cterm
wenzelm@4543
   818
read_cterm        : Sign.sg -> string * typ -> cterm
wenzelm@4543
   819
cert_axm          : Sign.sg -> string * term -> string * term
wenzelm@4543
   820
read_axm          : Sign.sg -> string * string -> string * term
wenzelm@4543
   821
rep_cterm         : cterm -> {\ttlbrace}T:typ, t:term, sign:Sign.sg, maxidx:int\ttrbrace
wenzelm@4543
   822
Sign.certify_term : Sign.sg -> term -> term * typ * int
lcp@104
   823
\end{ttbox}
lcp@324
   824
\begin{ttdescription}
wenzelm@4543
   825
  
wenzelm@4543
   826
\item[\ttindexbold{cterm_of} $sign$ $t$] \index{signatures} certifies
wenzelm@4543
   827
  $t$ with respect to signature~$sign$.
wenzelm@4543
   828
  
wenzelm@4543
   829
\item[\ttindexbold{read_cterm} $sign$ ($s$, $T$)] reads the string~$s$
wenzelm@4543
   830
  using the syntax of~$sign$, creating a certified term.  The term is
wenzelm@4543
   831
  checked to have type~$T$; this type also tells the parser what kind
wenzelm@4543
   832
  of phrase to parse.
wenzelm@4543
   833
  
wenzelm@4543
   834
\item[\ttindexbold{cert_axm} $sign$ ($name$, $t$)] certifies $t$ with
wenzelm@4543
   835
  respect to $sign$ as a meta-proposition and converts all exceptions
wenzelm@4543
   836
  to an error, including the final message
wenzelm@864
   837
\begin{ttbox}
wenzelm@864
   838
The error(s) above occurred in axiom "\(name\)"
wenzelm@864
   839
\end{ttbox}
wenzelm@864
   840
wenzelm@4543
   841
\item[\ttindexbold{read_axm} $sign$ ($name$, $s$)] similar to {\tt
wenzelm@4543
   842
    cert_axm}, but first reads the string $s$ using the syntax of
wenzelm@4543
   843
  $sign$.
wenzelm@4543
   844
  
wenzelm@4543
   845
\item[\ttindexbold{rep_cterm} $ct$] decomposes $ct$ as a record
wenzelm@4543
   846
  containing its type, the term itself, its signature, and the maximum
wenzelm@4543
   847
  subscript of its unknowns.  The type and maximum subscript are
wenzelm@4543
   848
  computed during certification.
wenzelm@4543
   849
  
wenzelm@4543
   850
\item[\ttindexbold{Sign.certify_term}] is a more primitive version of
wenzelm@4543
   851
  \texttt{cterm_of}, returning the internal representation instead of
wenzelm@4543
   852
  an abstract \texttt{cterm}.
wenzelm@864
   853
lcp@324
   854
\end{ttdescription}
lcp@104
   855
lcp@104
   856
wenzelm@864
   857
\section{Types}\index{types|bold}
wenzelm@864
   858
Types belong to the \ML\ type \mltydx{typ}, which is a concrete datatype with
wenzelm@864
   859
three constructor functions.  These correspond to type constructors, free
wenzelm@864
   860
type variables and schematic type variables.  Types are classified by sorts,
wenzelm@864
   861
which are lists of classes (representing an intersection).  A class is
wenzelm@864
   862
represented by a string.
lcp@104
   863
\begin{ttbox}
lcp@104
   864
type class = string;
lcp@104
   865
type sort  = class list;
lcp@104
   866
lcp@104
   867
datatype typ = Type  of string * typ list
lcp@104
   868
             | TFree of string * sort
lcp@104
   869
             | TVar  of indexname * sort;
lcp@104
   870
lcp@104
   871
infixr 5 -->;
wenzelm@864
   872
fun S --> T = Type ("fun", [S, T]);
lcp@104
   873
\end{ttbox}
lcp@324
   874
\begin{ttdescription}
wenzelm@4317
   875
\item[\ttindexbold{Type} ($a$, $Ts$)] \index{type constructors|bold}
lcp@324
   876
  applies the {\bf type constructor} named~$a$ to the type operands~$Ts$.
lcp@324
   877
  Type constructors include~\tydx{fun}, the binary function space
lcp@324
   878
  constructor, as well as nullary type constructors such as~\tydx{prop}.
lcp@324
   879
  Other type constructors may be introduced.  In expressions, but not in
lcp@324
   880
  patterns, \hbox{\tt$S$-->$T$} is a convenient shorthand for function
lcp@324
   881
  types.
lcp@104
   882
wenzelm@4317
   883
\item[\ttindexbold{TFree} ($a$, $s$)] \index{type variables|bold}
lcp@324
   884
  is the {\bf type variable} with name~$a$ and sort~$s$.
lcp@104
   885
wenzelm@4317
   886
\item[\ttindexbold{TVar} ($v$, $s$)] \index{type unknowns|bold}
lcp@324
   887
  is the {\bf type unknown} with indexname~$v$ and sort~$s$.
lcp@324
   888
  Type unknowns are essentially free type variables, but may be
lcp@324
   889
  instantiated during unification.
lcp@324
   890
\end{ttdescription}
lcp@104
   891
lcp@104
   892
lcp@104
   893
\section{Certified types}
lcp@104
   894
\index{types!certified|bold}
wenzelm@864
   895
Certified types, which are analogous to certified terms, have type
nipkow@275
   896
\ttindexbold{ctyp}.
lcp@104
   897
lcp@104
   898
\subsection{Printing types}
lcp@324
   899
\index{types!printing of}
wenzelm@864
   900
\begin{ttbox}
nipkow@275
   901
     string_of_ctyp :           ctyp -> string
lcp@104
   902
Sign.string_of_typ  : Sign.sg -> typ -> string
lcp@104
   903
\end{ttbox}
lcp@324
   904
\begin{ttdescription}
wenzelm@864
   905
\item[\ttindexbold{string_of_ctyp} $cT$]
lcp@104
   906
displays $cT$ as a string.
lcp@104
   907
wenzelm@864
   908
\item[\ttindexbold{Sign.string_of_typ} $sign$ $T$]
lcp@104
   909
displays $T$ as a string, using the syntax of~$sign$.
lcp@324
   910
\end{ttdescription}
lcp@104
   911
lcp@104
   912
lcp@104
   913
\subsection{Making and inspecting certified types}
wenzelm@864
   914
\begin{ttbox}
wenzelm@4543
   915
ctyp_of          : Sign.sg -> typ -> ctyp
wenzelm@4543
   916
rep_ctyp         : ctyp -> {\ttlbrace}T: typ, sign: Sign.sg\ttrbrace
wenzelm@4543
   917
Sign.certify_typ : Sign.sg -> typ -> typ
lcp@104
   918
\end{ttbox}
lcp@324
   919
\begin{ttdescription}
wenzelm@4543
   920
  
wenzelm@4543
   921
\item[\ttindexbold{ctyp_of} $sign$ $T$] \index{signatures} certifies
wenzelm@4543
   922
  $T$ with respect to signature~$sign$.
wenzelm@4543
   923
  
wenzelm@4543
   924
\item[\ttindexbold{rep_ctyp} $cT$] decomposes $cT$ as a record
wenzelm@4543
   925
  containing the type itself and its signature.
wenzelm@4543
   926
  
wenzelm@4543
   927
\item[\ttindexbold{Sign.certify_typ}] is a more primitive version of
wenzelm@4543
   928
  \texttt{ctyp_of}, returning the internal representation instead of
wenzelm@4543
   929
  an abstract \texttt{ctyp}.
lcp@104
   930
lcp@324
   931
\end{ttdescription}
lcp@104
   932
paulson@1846
   933
wenzelm@4317
   934
\section{Oracles: calling trusted external reasoners}
paulson@1846
   935
\label{sec:oracles}
paulson@1846
   936
\index{oracles|(}
paulson@1846
   937
paulson@1846
   938
Oracles allow Isabelle to take advantage of external reasoners such as
paulson@1846
   939
arithmetic decision procedures, model checkers, fast tautology checkers or
paulson@1846
   940
computer algebra systems.  Invoked as an oracle, an external reasoner can
paulson@1846
   941
create arbitrary Isabelle theorems.  It is your responsibility to ensure that
paulson@1846
   942
the external reasoner is as trustworthy as your application requires.
paulson@1846
   943
Isabelle's proof objects~(\S\ref{sec:proofObjects}) record how each theorem
paulson@1846
   944
depends upon oracle calls.
paulson@1846
   945
paulson@1846
   946
\begin{ttbox}
wenzelm@4317
   947
invoke_oracle     : theory -> xstring -> Sign.sg * object -> thm
paulson@4597
   948
Theory.add_oracle : bstring * (Sign.sg * object -> term) -> theory 
paulson@4597
   949
                    -> theory
paulson@1846
   950
\end{ttbox}
paulson@1846
   951
\begin{ttdescription}
wenzelm@4317
   952
\item[\ttindexbold{invoke_oracle} $thy$ $name$ ($sign$, $data$)]
wenzelm@4317
   953
  invokes the oracle $name$ of theory $thy$ passing the information
wenzelm@4317
   954
  contained in the exception value $data$ and creating a theorem
wenzelm@4317
   955
  having signature $sign$.  Note that type \ttindex{object} is just an
wenzelm@4317
   956
  abbreviation for \texttt{exn}.  Errors arise if $thy$ does not have
wenzelm@4317
   957
  an oracle called $name$, if the oracle rejects its arguments or if
wenzelm@4317
   958
  its result is ill-typed.
wenzelm@4317
   959
  
wenzelm@4317
   960
\item[\ttindexbold{Theory.add_oracle} $name$ $fun$ $thy$] extends
wenzelm@4317
   961
  $thy$ by oracle $fun$ called $name$.  It is seldom called
wenzelm@4317
   962
  explicitly, as there is concrete syntax for oracles in theory files.
paulson@1846
   963
\end{ttdescription}
paulson@1846
   964
paulson@1846
   965
A curious feature of {\ML} exceptions is that they are ordinary constructors.
paulson@6669
   966
The {\ML} type \texttt{exn} is a datatype that can be extended at any time.  (See
paulson@1846
   967
my {\em {ML} for the Working Programmer}~\cite{paulson-ml2}, especially
paulson@1846
   968
page~136.)  The oracle mechanism takes advantage of this to allow an oracle to
paulson@1846
   969
take any information whatever.
paulson@1846
   970
paulson@1846
   971
There must be some way of invoking the external reasoner from \ML, either
paulson@1846
   972
because it is coded in {\ML} or via an operating system interface.  Isabelle
paulson@1846
   973
expects the {\ML} function to take two arguments: a signature and an
wenzelm@4317
   974
exception object.
paulson@1846
   975
\begin{itemize}
paulson@1846
   976
\item The signature will typically be that of a desendant of the theory
paulson@1846
   977
  declaring the oracle.  The oracle will use it to distinguish constants from
paulson@1846
   978
  variables, etc., and it will be attached to the generated theorems.
paulson@1846
   979
paulson@1846
   980
\item The exception is used to pass arbitrary information to the oracle.  This
paulson@1846
   981
  information must contain a full description of the problem to be solved by
paulson@1846
   982
  the external reasoner, including any additional information that might be
paulson@1846
   983
  required.  The oracle may raise the exception to indicate that it cannot
paulson@1846
   984
  solve the specified problem.
paulson@1846
   985
\end{itemize}
paulson@1846
   986
paulson@6669
   987
A trivial example is provided in theory \texttt{FOL/ex/IffOracle}.  This
wenzelm@4317
   988
oracle generates tautologies of the form $P\bimp\cdots\bimp P$, with
wenzelm@4317
   989
an even number of $P$s.
paulson@1846
   990
wenzelm@4317
   991
The \texttt{ML} section of \texttt{IffOracle.thy} begins by declaring
wenzelm@4317
   992
a few auxiliary functions (suppressed below) for creating the
wenzelm@4317
   993
tautologies.  Then it declares a new exception constructor for the
wenzelm@4317
   994
information required by the oracle: here, just an integer. It finally
wenzelm@4317
   995
defines the oracle function itself.
paulson@1846
   996
\begin{ttbox}
wenzelm@4317
   997
exception IffOracleExn of int;\medskip
wenzelm@4317
   998
fun mk_iff_oracle (sign, IffOracleExn n) =
wenzelm@4317
   999
  if n > 0 andalso n mod 2 = 0
paulson@6669
  1000
  then Trueprop \$ mk_iff n
wenzelm@4317
  1001
  else raise IffOracleExn n;
paulson@1846
  1002
\end{ttbox}
paulson@6669
  1003
Observe the function's two arguments, the signature \texttt{sign} and the
wenzelm@4317
  1004
exception given as a pattern.  The function checks its argument for
wenzelm@4317
  1005
validity.  If $n$ is positive and even then it creates a tautology
wenzelm@4317
  1006
containing $n$ occurrences of~$P$.  Otherwise it signals error by
wenzelm@4317
  1007
raising its own exception (just by happy coincidence).  Errors may be
paulson@6669
  1008
signalled by other means, such as returning the theorem \texttt{True}.
wenzelm@4317
  1009
Please ensure that the oracle's result is correctly typed; Isabelle
wenzelm@4317
  1010
will reject ill-typed theorems by raising a cryptic exception at top
wenzelm@4317
  1011
level.
paulson@1846
  1012
paulson@6669
  1013
The \texttt{oracle} section of \texttt{IffOracle.thy} installs above
wenzelm@4317
  1014
\texttt{ML} function as follows:
paulson@1846
  1015
\begin{ttbox}
wenzelm@4317
  1016
IffOracle = FOL +\medskip
wenzelm@4317
  1017
oracle
wenzelm@4317
  1018
  iff = mk_iff_oracle\medskip
paulson@1846
  1019
end
paulson@1846
  1020
\end{ttbox}
paulson@1846
  1021
wenzelm@4317
  1022
Now in \texttt{IffOracle.ML} we first define a wrapper for invoking
wenzelm@4317
  1023
the oracle:
paulson@1846
  1024
\begin{ttbox}
paulson@4597
  1025
fun iff_oracle n = invoke_oracle IffOracle.thy "iff"
paulson@4597
  1026
                      (sign_of IffOracle.thy, IffOracleExn n);
wenzelm@4317
  1027
\end{ttbox}
wenzelm@4317
  1028
wenzelm@4317
  1029
Here are some example applications of the \texttt{iff} oracle.  An
wenzelm@4317
  1030
argument of 10 is allowed, but one of 5 is forbidden:
wenzelm@4317
  1031
\begin{ttbox}
wenzelm@4317
  1032
iff_oracle 10;
paulson@1846
  1033
{\out  "P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P" : thm}
wenzelm@4317
  1034
iff_oracle 5;
paulson@1846
  1035
{\out Exception- IffOracleExn 5 raised}
paulson@1846
  1036
\end{ttbox}
paulson@1846
  1037
paulson@1846
  1038
\index{oracles|)}
lcp@104
  1039
\index{theories|)}
wenzelm@5369
  1040
wenzelm@5369
  1041
wenzelm@5369
  1042
%%% Local Variables: 
wenzelm@5369
  1043
%%% mode: latex
wenzelm@5369
  1044
%%% TeX-master: "ref"
wenzelm@5369
  1045
%%% End: