| author | haftmann | 
| Wed, 21 Mar 2007 08:07:19 +0100 | |
| changeset 22490 | 1822ec4fcecd | 
| parent 19627 | b07c46e67e2d | 
| child 30184 | 37969710e61f | 
| permissions | -rw-r--r-- | 
| 3201 | 1 | |
| 104 | 2 | %% $Id$ | 
| 145 | 3 | |
| 104 | 4 | \chapter{Theories, Terms and Types} \label{theories}
 | 
| 5 | \index{theories|(}\index{signatures|bold}
 | |
| 9695 | 6 | \index{reading!axioms|see{\texttt{assume_ax}}} Theories organize the syntax,
 | 
| 7 | declarations and axioms of a mathematical development. They are built, | |
| 8 | starting from the Pure or CPure theory, by extending and merging existing | |
| 9 | theories.  They have the \ML\ type \mltydx{theory}.  Theory operations signal
 | |
| 10 | errors by raising exception \xdx{THEORY}, returning a message and a list of
 | |
| 11 | theories. | |
| 104 | 12 | |
| 13 | Signatures, which contain information about sorts, types, constants and | |
| 332 | 14 | syntax, have the \ML\ type~\mltydx{Sign.sg}.  For identification, each
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 15 | signature carries a unique list of \bfindex{stamps}, which are \ML\
 | 
| 324 | 16 | references to strings. The strings serve as human-readable names; the | 
| 104 | 17 | references serve as unique identifiers. Each primitive signature has a | 
| 18 | single stamp. When two signatures are merged, their lists of stamps are | |
| 19 | also merged. Every theory carries a unique signature. | |
| 20 | ||
| 21 | Terms and types are the underlying representation of logical syntax. Their | |
| 275 | 22 | \ML\ definitions are irrelevant to naive Isabelle users. Programmers who | 
| 23 | wish to extend Isabelle may need to know such details, say to code a tactic | |
| 24 | that looks for subgoals of a particular form. Terms and types may be | |
| 104 | 25 | `certified' to be well-formed with respect to a given signature. | 
| 26 | ||
| 324 | 27 | |
| 28 | \section{Defining theories}\label{sec:ref-defining-theories}
 | |
| 104 | 29 | |
| 6571 | 30 | Theories are defined via theory files $name$\texttt{.thy} (there are also
 | 
| 31 | \ML-level interfaces which are only intended for people building advanced | |
| 32 | theory definition packages).  Appendix~\ref{app:TheorySyntax} presents the
 | |
| 33 | concrete syntax for theory files; here follows an explanation of the | |
| 34 | constituent parts. | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 35 | \begin{description}
 | 
| 6568 | 36 | \item[{\it theoryDef}] is the full definition.  The new theory is called $id$.
 | 
| 8136 | 37 |   It is the union of the named \textbf{parent
 | 
| 3108 | 38 |     theories}\indexbold{theories!parent}, possibly extended with new
 | 
| 6568 | 39 |   components.  \thydx{Pure} and \thydx{CPure} are the basic theories, which
 | 
| 40 | contain only the meta-logic. They differ just in their concrete syntax for | |
| 41 | function applications. | |
| 6571 | 42 | |
| 43 | The new theory begins as a merge of its parents. | |
| 44 |   \begin{ttbox}
 | |
| 45 | Attempt to merge different versions of theories: "\(T@1\)", \(\ldots\), "\(T@n\)" | |
| 46 |   \end{ttbox}
 | |
| 47 | This error may especially occur when a theory is redeclared --- say to | |
| 48 | change an inappropriate definition --- and bindings to old versions persist. | |
| 49 | Isabelle ensures that old and new theories of the same name are not involved | |
| 50 | in a proof. | |
| 324 | 51 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 52 | \item[$classes$] | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 53 |   is a series of class declarations.  Declaring {\tt$id$ < $id@1$ \dots\
 | 
| 324 | 54 | $id@n$} makes $id$ a subclass of the existing classes $id@1\dots | 
| 55 | id@n$. This rules out cyclic class structures. Isabelle automatically | |
| 56 | computes the transitive closure of subclass hierarchies; it is not | |
| 6669 | 57 |   necessary to declare \texttt{c < e} in addition to \texttt{c < d} and \texttt{d <
 | 
| 324 | 58 | e}. | 
| 59 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 60 | \item[$default$] | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 61 | introduces $sort$ as the new default sort for type variables. This applies | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 62 | to unconstrained type variables in an input string but not to type | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 63 | variables created internally. If omitted, the default sort is the listwise | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 64 | union of the default sorts of the parent theories (i.e.\ their logical | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 65 | intersection). | 
| 3108 | 66 | |
| 7168 | 67 | \item[$sort$] is a finite set of classes. A single class $id$ abbreviates the | 
| 68 |   sort $\{id\}$.
 | |
| 324 | 69 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 70 | \item[$types$] | 
| 324 | 71 | is a series of type declarations. Each declares a new type constructor | 
| 72 | or type synonym. An $n$-place type constructor is specified by | |
| 73 | $(\alpha@1,\dots,\alpha@n)name$, where the type variables serve only to | |
| 74 | indicate the number~$n$. | |
| 75 | ||
| 8136 | 76 |   A \textbf{type synonym}\indexbold{type synonyms} is an abbreviation
 | 
| 1387 | 77 | $(\alpha@1,\dots,\alpha@n)name = \tau$, where $name$ and $\tau$ can | 
| 78 | be strings. | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 79 | |
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 80 | \item[$infix$] | 
| 8136 | 81 | declares a type or constant to be an infix operator having priority $nat$ | 
| 82 |   and associating to the left (\texttt{infixl}) or right (\texttt{infixr}).
 | |
| 83 |   Only 2-place type constructors can have infix status; an example is {\tt
 | |
| 3108 | 84 |   ('a,'b)~"*"~(infixr~20)}, which may express binary product types.
 | 
| 324 | 85 | |
| 3108 | 86 | \item[$arities$] is a series of type arity declarations. Each assigns | 
| 87 | arities to type constructors. The $name$ must be an existing type | |
| 88 | constructor, which is given the additional arity $arity$. | |
| 89 | ||
| 5369 | 90 | \item[$nonterminals$]\index{*nonterminal symbols} declares purely
 | 
| 91 | syntactic types to be used as nonterminal symbols of the context | |
| 92 | free grammar. | |
| 93 | ||
| 3108 | 94 | \item[$consts$] is a series of constant declarations. Each new | 
| 95 | constant $name$ is given the specified type. The optional $mixfix$ | |
| 96 | annotations may attach concrete syntax to the constant. | |
| 97 | ||
| 98 | \item[$syntax$] \index{*syntax section}\index{print mode} is a variant
 | |
| 99 | of $consts$ which adds just syntax without actually declaring | |
| 100 | logical constants. This gives full control over a theory's context | |
| 3485 
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
 paulson parents: 
3201diff
changeset | 101 | free grammar. The optional $mode$ specifies the print mode where the | 
| 
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
 paulson parents: 
3201diff
changeset | 102 |   mixfix productions should be added.  If there is no \texttt{output}
 | 
| 3108 | 103 | option given, all productions are also added to the input syntax | 
| 104 | (regardless of the print mode). | |
| 324 | 105 | |
| 106 | \item[$mixfix$] \index{mixfix declarations}
 | |
| 107 | annotations can take three forms: | |
| 273 | 108 |   \begin{itemize}
 | 
| 109 | \item A mixfix template given as a $string$ of the form | |
| 110 |     {\tt"}\dots{\tt\_}\dots{\tt\_}\dots{\tt"} where the $i$-th underscore
 | |
| 324 | 111 | indicates the position where the $i$-th argument should go. The list | 
| 112 | of numbers gives the priority of each argument. The final number gives | |
| 113 | the priority of the whole construct. | |
| 104 | 114 | |
| 324 | 115 |   \item A constant $f$ of type $\tau@1\To(\tau@2\To\tau)$ can be given {\bf
 | 
| 116 | infix} status. | |
| 104 | 117 | |
| 324 | 118 |   \item A constant $f$ of type $(\tau@1\To\tau@2)\To\tau$ can be given {\bf
 | 
| 6669 | 119 |     binder} status.  The declaration \texttt{binder} $\cal Q$ $p$ causes
 | 
| 286 | 120 |   ${\cal Q}\,x.F(x)$ to be treated
 | 
| 121 | like $f(F)$, where $p$ is the priority. | |
| 273 | 122 |   \end{itemize}
 | 
| 324 | 123 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 124 | \item[$trans$] | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 125 | specifies syntactic translation rules (macros). There are three forms: | 
| 6669 | 126 |   parse rules (\texttt{=>}), print rules (\texttt{<=}), and parse/print rules ({\tt
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 127 | ==}). | 
| 324 | 128 | |
| 1650 | 129 | \item[$rules$] | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 130 | is a series of rule declarations. Each has a name $id$ and the formula is | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 131 | given by the $string$. Rule names must be distinct within any single | 
| 3108 | 132 | theory. | 
| 324 | 133 | |
| 1905 | 134 | \item[$defs$] is a series of definitions. They are just like $rules$, except | 
| 135 | that every $string$ must be a definition (see below for details). | |
| 1650 | 136 | |
| 137 | \item[$constdefs$] combines the declaration of constants and their | |
| 3485 
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
 paulson parents: 
3201diff
changeset | 138 | definition. The first $string$ is the type, the second the definition. | 
| 3108 | 139 | |
| 6625 | 140 | \item[$axclass$] \index{*axclass section} defines an \rmindex{axiomatic type
 | 
| 141 |     class} \cite{Wenzel:1997:TPHOL} as the intersection of existing classes,
 | |
| 142 | with additional axioms holding. Class axioms may not contain more than one | |
| 143 | type variable. The class axioms (with implicit sort constraints added) are | |
| 144 | bound to the given names. Furthermore a class introduction rule is | |
| 145 | generated, which is automatically employed by $instance$ to prove | |
| 146 | instantiations of this class. | |
| 3108 | 147 | |
| 148 | \item[$instance$] \index{*instance section} proves class inclusions or
 | |
| 149 | type arities at the logical level and then transfers these to the | |
| 3485 
f27a30a18a17
Now there are TWO spaces after each full stop, so that the Emacs sentence
 paulson parents: 
3201diff
changeset | 150 | type signature. The instantiation is proven and checked properly. | 
| 3108 | 151 | The user has to supply sufficient witness information: theorems | 
| 152 |   ($longident$), axioms ($string$), or even arbitrary \ML{} tactic
 | |
| 153 | code $verbatim$. | |
| 1650 | 154 | |
| 1846 | 155 | \item[$oracle$] links the theory to a trusted external reasoner. It is | 
| 156 | allowed to create theorems, but each theorem carries a proof object | |
| 157 |   describing the oracle invocation.  See \S\ref{sec:oracles} for details.
 | |
| 4543 | 158 | |
| 5369 | 159 | \item[$local$, $global$] change the current name declaration mode. | 
| 4543 | 160 | Initially, theories start in $local$ mode, causing all names of | 
| 161 | types, constants, axioms etc.\ to be automatically qualified by the | |
| 162 | theory name. Changing this to $global$ causes all names to be | |
| 163 | declared as short base names only. | |
| 164 | ||
| 165 | The $local$ and $global$ declarations act like switches, affecting | |
| 166 | all following theory sections until changed again explicitly. Also | |
| 167 | note that the final state at the end of the theory will persist. In | |
| 168 | particular, this determines how the names of theorems stored later | |
| 169 | on are handled. | |
| 5369 | 170 | |
| 171 | \item[$setup$]\index{*setup!theory} applies a list of ML functions to
 | |
| 172 | the theory. The argument should denote a value of type | |
| 173 |   \texttt{(theory -> theory) list}.  Typically, ML packages are
 | |
| 174 | initialized in this way. | |
| 1846 | 175 | |
| 324 | 176 | \item[$ml$] \index{*ML section}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 177 | consists of \ML\ code, typically for parse and print translation functions. | 
| 104 | 178 | \end{description}
 | 
| 324 | 179 | % | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 180 | Chapters~\ref{Defining-Logics} and \ref{chap:syntax} explain mixfix
 | 
| 6669 | 181 | declarations, translation rules and the \texttt{ML} section in more detail.
 | 
| 145 | 182 | |
| 183 | ||
| 275 | 184 | \subsection{*Classes and arities}
 | 
| 324 | 185 | \index{classes!context conditions}\index{arities!context conditions}
 | 
| 145 | 186 | |
| 286 | 187 | In order to guarantee principal types~\cite{nipkow-prehofer},
 | 
| 324 | 188 | arity declarations must obey two conditions: | 
| 145 | 189 | \begin{itemize}
 | 
| 3108 | 190 | \item There must not be any two declarations $ty :: (\vec{r})c$ and
 | 
| 191 |   $ty :: (\vec{s})c$ with $\vec{r} \neq \vec{s}$.  For example, this
 | |
| 192 | excludes the following: | |
| 145 | 193 | \begin{ttbox}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 194 | arities | 
| 8136 | 195 |   foo :: (\{logic{\}}) logic
 | 
| 196 |   foo :: (\{{\}})logic
 | |
| 145 | 197 | \end{ttbox}
 | 
| 286 | 198 | |
| 145 | 199 | \item If there are two declarations $ty :: (s@1,\dots,s@n)c$ and $ty :: | 
| 200 | (s@1',\dots,s@n')c'$ such that $c' < c$ then $s@i' \preceq s@i$ must hold | |
| 201 | for $i=1,\dots,n$. The relationship $\preceq$, defined as | |
| 202 | \[ s' \preceq s \iff \forall c\in s. \exists c'\in s'.~ c'\le c, \] | |
| 3108 | 203 | expresses that the set of types represented by $s'$ is a subset of the | 
| 204 | set of types represented by $s$. Assuming $term \preceq logic$, the | |
| 205 | following is forbidden: | |
| 145 | 206 | \begin{ttbox}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 207 | arities | 
| 8136 | 208 |   foo :: (\{logic{\}})logic
 | 
| 209 |   foo :: (\{{\}})term
 | |
| 145 | 210 | \end{ttbox}
 | 
| 286 | 211 | |
| 145 | 212 | \end{itemize}
 | 
| 213 | ||
| 104 | 214 | |
| 6568 | 215 | \section{The theory loader}\label{sec:more-theories}
 | 
| 216 | \index{theories!reading}\index{files!reading}
 | |
| 217 | ||
| 218 | Isabelle's theory loader manages dependencies of the internal graph of theory | |
| 219 | nodes (the \emph{theory database}) and the external view of the file system.
 | |
| 220 | See \S\ref{sec:intro-theories} for its most basic commands, such as
 | |
| 221 | \texttt{use_thy}.  There are a few more operations available.
 | |
| 222 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 223 | \begin{ttbox}
 | 
| 6568 | 224 | use_thy_only : string -> unit | 
| 7136 | 225 | update_thy_only : string -> unit | 
| 6568 | 226 | touch_thy : string -> unit | 
| 6658 | 227 | remove_thy : string -> unit | 
| 8136 | 228 | delete_tmpfiles : bool ref \hfill\textbf{initially true}
 | 
| 286 | 229 | \end{ttbox}
 | 
| 230 | ||
| 324 | 231 | \begin{ttdescription}
 | 
| 6569 | 232 | \item[\ttindexbold{use_thy_only} "$name$";] is similar to \texttt{use_thy},
 | 
| 233 |   but processes the actual theory file $name$\texttt{.thy} only, ignoring
 | |
| 6568 | 234 |   $name$\texttt{.ML}.  This might be useful in replaying proof scripts by hand
 | 
| 235 | from the very beginning, starting with the fresh theory. | |
| 236 | ||
| 7136 | 237 | \item[\ttindexbold{update_thy_only} "$name$";] is similar to
 | 
| 238 |   \texttt{update_thy}, but processes the actual theory file
 | |
| 239 |   $name$\texttt{.thy} only, ignoring $name$\texttt{.ML}.
 | |
| 240 | ||
| 6569 | 241 | \item[\ttindexbold{touch_thy} "$name$";] marks theory node $name$ of the
 | 
| 6568 | 242 | internal graph as outdated. While the theory remains usable, subsequent | 
| 243 |   operations such as \texttt{use_thy} may cause a reload.
 | |
| 244 | ||
| 6658 | 245 | \item[\ttindexbold{remove_thy} "$name$";] deletes theory node $name$,
 | 
| 246 |   including \emph{all} of its descendants.  Beware!  This is a quick way to
 | |
| 247 |   dispose a large number of theories at once.  Note that {\ML} bindings to
 | |
| 248 | theorems etc.\ of removed theories may still persist. | |
| 249 | ||
| 6568 | 250 | \item[reset \ttindexbold{delete_tmpfiles};] processing theory files usually
 | 
| 251 |   involves temporary {\ML} files to be created.  By default, these are deleted
 | |
| 252 |   afterwards.  Resetting the \texttt{delete_tmpfiles} flag inhibits this,
 | |
| 253 | leaving the generated code for debugging purposes. The basic location for | |
| 254 |   temporary files is determined by the \texttt{ISABELLE_TMP} environment
 | |
| 6571 | 255 | variable (which is private to the running Isabelle process and may be | 
| 6568 | 256 |   retrieved by \ttindex{getenv} from {\ML}).
 | 
| 324 | 257 | \end{ttdescription}
 | 
| 138 
9ba8bff1addc
added chapter "Defining Theories" and made changes for new Readthy functions
 clasohm parents: 
104diff
changeset | 258 | |
| 6568 | 259 | \medskip Theory and {\ML} files are located by skimming through the
 | 
| 260 | directories listed in Isabelle's internal load path, which merely contains the | |
| 261 | current directory ``\texttt{.}'' by default.  The load path may be accessed by
 | |
| 262 | the following operations. | |
| 332 | 263 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 264 | \begin{ttbox}
 | 
| 6568 | 265 | show_path: unit -> string list | 
| 266 | add_path: string -> unit | |
| 267 | del_path: string -> unit | |
| 268 | reset_path: unit -> unit | |
| 7440 | 269 | with_path: string -> ('a -> 'b) -> 'a -> 'b
 | 
| 11052 | 270 | no_document: ('a -> 'b) -> 'a -> 'b
 | 
| 286 | 271 | \end{ttbox}
 | 
| 272 | ||
| 324 | 273 | \begin{ttdescription}
 | 
| 6568 | 274 | \item[\ttindexbold{show_path}();] displays the load path components in
 | 
| 6571 | 275 | canonical string representation (which is always according to Unix rules). | 
| 6568 | 276 | |
| 6569 | 277 | \item[\ttindexbold{add_path} "$dir$";] adds component $dir$ to the beginning
 | 
| 278 | of the load path. | |
| 6568 | 279 | |
| 6569 | 280 | \item[\ttindexbold{del_path} "$dir$";] removes any occurrences of component
 | 
| 6568 | 281 | $dir$ from the load path. | 
| 282 | ||
| 283 | \item[\ttindexbold{reset_path}();] resets the load path to ``\texttt{.}''
 | |
| 284 | (current directory) only. | |
| 7440 | 285 | |
| 286 | \item[\ttindexbold{with_path} "$dir$" $f$ $x$;] temporarily adds component
 | |
| 11052 | 287 | $dir$ to the beginning of the load path while executing $(f~x)$. | 
| 288 | ||
| 289 | \item[\ttindexbold{no_document} $f$ $x$;] temporarily disables {\LaTeX}
 | |
| 290 | document generation while executing $(f~x)$. | |
| 7440 | 291 | |
| 324 | 292 | \end{ttdescription}
 | 
| 286 | 293 | |
| 7440 | 294 | Furthermore, in operations referring indirectly to some file (e.g.\ | 
| 295 | \texttt{use_dir}) the argument may be prefixed by a directory that will be
 | |
| 296 | temporarily appended to the load path, too. | |
| 104 | 297 | |
| 298 | ||
| 6669 | 299 | \section{Locales}
 | 
| 300 | \label{Locales}
 | |
| 301 | ||
| 302 | Locales \cite{kammueller-locales} are a concept of local proof contexts.  They
 | |
| 303 | are introduced as named syntactic objects within theories and can be | |
| 304 | opened in any descendant theory. | |
| 305 | ||
| 306 | \subsection{Declaring Locales}
 | |
| 307 | ||
| 308 | A locale is declared in a theory section that starts with the | |
| 309 | keyword \texttt{locale}.  It consists typically of three parts, the
 | |
| 310 | \texttt{fixes} part, the \texttt{assumes} part, and the \texttt{defines} part.
 | |
| 311 | Appendix \ref{app:TheorySyntax} presents the full syntax.
 | |
| 312 | ||
| 313 | \subsubsection{Parts of Locales}
 | |
| 314 | ||
| 315 | The subsection introduced by the keyword \texttt{fixes} declares the locale
 | |
| 316 | constants in a way that closely resembles a global \texttt{consts}
 | |
| 317 | declaration. In particular, there may be an optional pretty printing syntax | |
| 318 | for the locale constants. | |
| 319 | ||
| 320 | The subsequent \texttt{assumes} part specifies the locale rules.  They are
 | |
| 321 | defined like \texttt{rules}: by an identifier followed by the rule
 | |
| 322 | given as a string. Locale rules admit the statement of local assumptions | |
| 323 | about the locale constants.  The \texttt{assumes} part is optional.  Non-fixed
 | |
| 324 | variables in locale rules are automatically bound by the universal quantifier | |
| 325 | \texttt{!!} of the meta-logic.
 | |
| 326 | ||
| 327 | Finally, the \texttt{defines} part introduces the definitions that are
 | |
| 328 | available in the locale.  Locale constants declared in the \texttt{fixes}
 | |
| 329 | section are defined using the meta-equality \texttt{==}.  If the
 | |
| 330 | locale constant is a functiond then its definition can (as usual) have | |
| 331 | variables on the left-hand side acting as formal parameters; they are | |
| 332 | considered as schematic variables and are automatically generalized by | |
| 333 | universal quantification of the meta-logic. The right hand side of a | |
| 334 | definition must not contain variables that are not already on the left hand | |
| 335 | side. In so far locale definitions behave like theory level definitions. | |
| 336 | However, the locale concept realizes \emph{dependent definitions}: any variable
 | |
| 337 | that is fixed as a locale constant can occur on the right hand side of | |
| 338 | definitions. For an illustration of these dependent definitions see the | |
| 339 | occurrence of the locale constant \texttt{G} on the right hand side of the
 | |
| 340 | definitions of the locale \texttt{group} below.  Naturally, definitions can
 | |
| 341 | already use the syntax of the locale constants in the \texttt{fixes}
 | |
| 342 | subsection.  The \texttt{defines} part is, as the \texttt{assumes} part,
 | |
| 343 | optional. | |
| 344 | ||
| 345 | \subsubsection{Example for Definition}
 | |
| 346 | The concrete syntax of locale definitions is demonstrated by example below. | |
| 347 | ||
| 348 | Locale \texttt{group} assumes the definition of groups in a theory
 | |
| 349 | file\footnote{This and other examples are from \texttt{HOL/ex}.}.  A locale
 | |
| 350 | defining a convenient proof environment for group related proofs may be | |
| 351 | added to the theory as follows: | |
| 352 | \begin{ttbox}
 | |
| 353 | locale group = | |
| 354 | fixes | |
| 355 | G :: "'a grouptype" | |
| 356 | e :: "'a" | |
| 357 | binop :: "'a => 'a => 'a" (infixr "#" 80) | |
| 358 |       inv       :: "'a => 'a"              ("i(_)" [90] 91)
 | |
| 359 | assumes | |
| 360 | Group_G "G: Group" | |
| 361 | defines | |
| 362 | e_def "e == unit G" | |
| 363 | binop_def "x # y == bin_op G x y" | |
| 364 | inv_def "i(x) == inverse G x" | |
| 365 | \end{ttbox}
 | |
| 366 | ||
| 367 | \subsubsection{Polymorphism}
 | |
| 368 | ||
| 369 | In contrast to polymorphic definitions in theories, the use of the | |
| 370 | same type variable for the declaration of different locale constants in the | |
| 371 | fixes part means \emph{the same} type.  In other words, the scope of the
 | |
| 372 | polymorphic variables is extended over all constant declarations of a locale. | |
| 373 | In the above example \texttt{'a} refers to the same type which is fixed inside
 | |
| 374 | the locale.  In an exported theorem (see \S\ref{sec:locale-export}) the
 | |
| 375 | constructors of locale \texttt{group} are polymorphic, yet only simultaneously
 | |
| 376 | instantiatable. | |
| 377 | ||
| 378 | \subsubsection{Nested Locales}
 | |
| 379 | ||
| 380 | A locale can be defined as the extension of a previously defined | |
| 381 | locale. This operation of extension is optional and is syntactically | |
| 382 | expressed as | |
| 383 | \begin{ttbox}
 | |
| 384 | locale foo = bar + ... | |
| 385 | \end{ttbox}
 | |
| 386 | The locale \texttt{foo} builds on the constants and syntax of the locale {\tt
 | |
| 387 | bar}.  That is, all contents of the locale \texttt{bar} can be used in
 | |
| 388 | definitions and rules of the corresponding parts of the locale {\tt
 | |
| 389 | foo}.  Although locale \texttt{foo} assumes the \texttt{fixes} part of \texttt{bar} it
 | |
| 390 | does not automatically subsume its rules and definitions. Normally, one | |
| 391 | expects to use locale \texttt{foo} only if locale \texttt{bar} is already
 | |
| 392 | active. These aspects of use and activation of locales are considered in the | |
| 393 | subsequent section. | |
| 394 | ||
| 395 | ||
| 396 | \subsection{Locale Scope}
 | |
| 397 | ||
| 398 | Locales are by default inactive, but they can be invoked. The list of | |
| 399 | currently active locales is called \emph{scope}.  The process of activating
 | |
| 400 | them is called \emph{opening}; the reverse is \emph{closing}.
 | |
| 401 | ||
| 402 | \subsubsection{Scope}
 | |
| 403 | The locale scope is part of each theory. It is a dynamic stack containing | |
| 404 | all active locales at a certain point in an interactive session. | |
| 405 | The scope lives until all locales are explicitly closed. At one time there | |
| 406 | can be more than one locale open. The contents of these various active | |
| 407 | locales are all visible in the scope. In case of nested locales for example, | |
| 408 | the nesting is actually reflected to the scope, which contains the nested | |
| 409 | locales as layers. To check the state of the scope during a development the | |
| 410 | function \texttt{Print\_scope} may be used.  It displays the names of all open
 | |
| 411 | locales on the scope.  The function \texttt{print\_locales} applied to a theory
 | |
| 412 | displays all locales contained in that theory and in addition also the | |
| 413 | current scope. | |
| 414 | ||
| 415 | The scope is manipulated by the commands for opening and closing of locales. | |
| 416 | ||
| 417 | \subsubsection{Opening}
 | |
| 418 | Locales can be \emph{opened} at any point during a session where
 | |
| 419 | we want to prove theorems concerning the locale. Opening a locale means | |
| 420 | making its contents visible by pushing it onto the scope of the current | |
| 421 | theory. Inside a scope of opened locales, theorems can use all definitions and | |
| 422 | rules contained in the locales on the scope. The rules and definitions may | |
| 423 | be accessed individually using the function \ttindex{thm}.  This function is
 | |
| 424 | applied to the names assigned to locale rules and definitions as | |
| 425 | strings.  The opening command is called \texttt{Open\_locale} and takes the 
 | |
| 426 | name of the locale to be opened as its argument. | |
| 427 | ||
| 428 | If one opens a locale \texttt{foo} that is defined by extension from locale
 | |
| 429 | \texttt{bar}, the function \texttt{Open\_locale} checks if locale \texttt{bar}
 | |
| 430 | is open.  If so, then it just opens \texttt{foo}, if not, then it prints a
 | |
| 431 | message and opens \texttt{bar} before opening \texttt{foo}.  Naturally, this
 | |
| 432 | carries on, if \texttt{bar} is again an extension.
 | |
| 433 | ||
| 434 | \subsubsection{Closing}
 | |
| 435 | ||
| 436 | \emph{Closing} means to cancel the last opened locale, pushing it out of the
 | |
| 437 | scope. Theorems proved during the life cycle of this locale will be disabled, | |
| 438 | unless they have been explicitly exported, as described below. However, when | |
| 439 | the same locale is opened again these theorems may be used again as well, | |
| 440 | provided that they were saved as theorems in the first place, using | |
| 441 | \texttt{qed} or ML assignment.  The command \texttt{Close\_locale} takes a
 | |
| 442 | locale name as a string and checks if this locale is actually the topmost | |
| 443 | locale on the scope. If this is the case, it removes this locale, otherwise | |
| 444 | it prints a warning message and does not change the scope. | |
| 445 | ||
| 446 | \subsubsection{Export of Theorems}
 | |
| 447 | \label{sec:locale-export}
 | |
| 448 | ||
| 449 | Export of theorems transports theorems out of the scope of locales. Locale | |
| 450 | rules that have been used in the proof of an exported theorem inside the | |
| 451 | locale are carried by the exported form of the theorem as its individual | |
| 452 | meta-assumptions. The locale constants are universally quantified variables | |
| 453 | in these theorems, hence such theorems can be instantiated individually. | |
| 454 | Definitions become unfolded; locale constants that were merely used for | |
| 455 | definitions vanish. Logically, exporting corresponds to a combined | |
| 456 | application of introduction rules for implication and universal | |
| 457 | quantification. Exporting forms a kind of normalization of theorems in a | |
| 458 | locale scope. | |
| 459 | ||
| 460 | According to the possibility of nested locales there are two different forms | |
| 461 | of export.  The first one is realized by the function \texttt{export} that
 | |
| 462 | exports theorems through all layers of opened locales of the scope. Hence, | |
| 463 | the application of export to a theorem yields a theorem of the global level, | |
| 464 | that is, the current theory context without any local assumptions or | |
| 465 | definitions. | |
| 466 | ||
| 467 | When locales are nested we might want to export a theorem, not to the global | |
| 468 | level of the current theory but just to the previous level. The other export | |
| 469 | function, \texttt{Export}, transports theorems one level up in the scope; the
 | |
| 470 | theorem still uses locale constants, definitions and rules of the locales | |
| 471 | underneath. | |
| 472 | ||
| 473 | \subsection{Functions for Locales}
 | |
| 474 | \label{Syntax}
 | |
| 475 | \index{locales!functions}
 | |
| 476 | ||
| 477 | Here is a quick reference list of locale functions. | |
| 478 | \begin{ttbox}
 | |
| 479 | Open_locale : xstring -> unit | |
| 480 | Close_locale : xstring -> unit | |
| 481 | export : thm -> thm | |
| 482 | Export : thm -> thm | |
| 483 | thm : xstring -> thm | |
| 484 | Print_scope : unit -> unit | |
| 485 | print_locales: theory -> unit | |
| 486 | \end{ttbox}
 | |
| 487 | \begin{ttdescription}
 | |
| 488 | \item[\ttindexbold{Open_locale} $xstring$] 
 | |
| 489 |     opens the locale {\it xstring}, adding it to the scope of the theory of the
 | |
| 490 | current context. If the opened locale is built by extension, the ancestors | |
| 491 | are opened automatically. | |
| 492 | ||
| 493 | \item[\ttindexbold{Close_locale} $xstring$] eliminates the locale {\it
 | |
| 494 | xstring} from the scope if it is the topmost item on it, otherwise it does | |
| 495 | not change the scope and produces a warning. | |
| 496 | ||
| 497 | \item[\ttindexbold{export} $thm$] locale definitions become expanded in {\it
 | |
| 498 |     thm} and locale rules that were used in the proof of {\it thm} become part
 | |
| 499 | of its individual assumptions. This normalization happens with respect to | |
| 500 |   \emph{all open locales} on the scope.
 | |
| 501 | ||
| 502 | \item[\ttindexbold{Export} $thm$] works like \texttt{export} but normalizes
 | |
| 503 | theorems only up to the previous level of locales on the scope. | |
| 504 | ||
| 505 | \item[\ttindexbold{thm} $xstring$] applied to the name of a locale definition
 | |
| 506 | or rule it returns the definition as a theorem. | |
| 507 | ||
| 508 | \item[\ttindexbold{Print_scope}()] prints the names of the locales in the
 | |
| 509 | current scope of the current theory context. | |
| 510 | ||
| 511 | \item[\ttindexbold{print_locale} $theory$] prints all locales that are
 | |
| 512 |   contained in {\it theory} directly or indirectly.  It also displays the
 | |
| 513 |   current scope similar to \texttt{Print\_scope}.
 | |
| 514 | \end{ttdescription}
 | |
| 515 | ||
| 516 | ||
| 866 
2d3d020eef11
added documentation of bind_thm, qed, qed_goal, get_thm, thms_of
 clasohm parents: 
864diff
changeset | 517 | \section{Basic operations on theories}\label{BasicOperationsOnTheories}
 | 
| 4384 | 518 | |
| 519 | \subsection{*Theory inclusion}
 | |
| 520 | \begin{ttbox}
 | |
| 521 | subthy : theory * theory -> bool | |
| 522 | eq_thy : theory * theory -> bool | |
| 523 | transfer : theory -> thm -> thm | |
| 524 | transfer_sg : Sign.sg -> thm -> thm | |
| 525 | \end{ttbox}
 | |
| 526 | ||
| 527 | Inclusion and equality of theories is determined by unique | |
| 528 | identification stamps that are created when declaring new components. | |
| 529 | Theorems contain a reference to the theory (actually to its signature) | |
| 530 | they have been derived in. Transferring theorems to super theories | |
| 531 | has no logical significance, but may affect some operations in subtle | |
| 532 | ways (e.g.\ implicit merges of signatures when applying rules, or | |
| 533 | pretty printing of theorems). | |
| 534 | ||
| 535 | \begin{ttdescription}
 | |
| 536 | ||
| 537 | \item[\ttindexbold{subthy} ($thy@1$, $thy@2$)] determines if $thy@1$
 | |
| 538 | is included in $thy@2$ wrt.\ identification stamps. | |
| 539 | ||
| 540 | \item[\ttindexbold{eq_thy} ($thy@1$, $thy@2$)] determines if $thy@1$
 | |
| 541 | is exactly the same as $thy@2$. | |
| 542 | ||
| 543 | \item[\ttindexbold{transfer} $thy$ $thm$] transfers theorem $thm$ to
 | |
| 544 | theory $thy$, provided the latter includes the theory of $thm$. | |
| 545 | ||
| 546 | \item[\ttindexbold{transfer_sg} $sign$ $thm$] is similar to
 | |
| 547 |   \texttt{transfer}, but identifies the super theory via its
 | |
| 548 | signature. | |
| 549 | ||
| 550 | \end{ttdescription}
 | |
| 551 | ||
| 552 | ||
| 6571 | 553 | \subsection{*Primitive theories}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 554 | \begin{ttbox}
 | 
| 4317 | 555 | ProtoPure.thy : theory | 
| 3108 | 556 | Pure.thy : theory | 
| 557 | CPure.thy : theory | |
| 286 | 558 | \end{ttbox}
 | 
| 3108 | 559 | \begin{description}
 | 
| 4317 | 560 | \item[\ttindexbold{ProtoPure.thy}, \ttindexbold{Pure.thy},
 | 
| 561 |   \ttindexbold{CPure.thy}] contain the syntax and signature of the
 | |
| 562 | meta-logic. There are basically no axioms: meta-level inferences | |
| 563 |   are carried out by \ML\ functions.  \texttt{Pure} and \texttt{CPure}
 | |
| 564 | just differ in their concrete syntax of prefix function application: | |
| 565 |   $t(u@1, \ldots, u@n)$ in \texttt{Pure} vs.\ $t\,u@1,\ldots\,u@n$ in
 | |
| 566 |   \texttt{CPure}.  \texttt{ProtoPure} is their common parent,
 | |
| 567 | containing no syntax for printing prefix applications at all! | |
| 6571 | 568 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 569 | %% FIXME | 
| 478 | 570 | %\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"} $\cdots$] extends
 | 
| 571 | % the theory $thy$ with new types, constants, etc. $T$ identifies the theory | |
| 572 | % internally. When a theory is redeclared, say to change an incorrect axiom, | |
| 573 | % bindings to the old axiom may persist. Isabelle ensures that the old and | |
| 574 | % new theories are not involved in the same proof. Attempting to combine | |
| 575 | % different theories having the same name $T$ yields the fatal error | |
| 576 | %extend_theory : theory -> string -> \(\cdots\) -> theory | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 577 | %\begin{ttbox}
 | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 578 | %Attempt to merge different versions of theory: \(T\) | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 579 | %\end{ttbox}
 | 
| 3108 | 580 | \end{description}
 | 
| 286 | 581 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 582 | %% FIXME | 
| 275 | 583 | %\item [\ttindexbold{extend_theory} $thy$ {\tt"}$T${\tt"}
 | 
| 584 | % ($classes$, $default$, $types$, $arities$, $consts$, $sextopt$) $rules$] | |
| 585 | %\hfill\break %%% include if line is just too short | |
| 286 | 586 | %is the \ML{} equivalent of the following theory definition:
 | 
| 275 | 587 | %\begin{ttbox}
 | 
| 588 | %\(T\) = \(thy\) + | |
| 589 | %classes \(c\) < \(c@1\),\(\dots\),\(c@m\) | |
| 590 | % \dots | |
| 591 | %default {\(d@1,\dots,d@r\)}
 | |
| 592 | %types \(tycon@1\),\dots,\(tycon@i\) \(n\) | |
| 593 | % \dots | |
| 594 | %arities \(tycon@1'\),\dots,\(tycon@j'\) :: (\(s@1\),\dots,\(s@n\))\(c\) | |
| 595 | % \dots | |
| 596 | %consts \(b@1\),\dots,\(b@k\) :: \(\tau\) | |
| 597 | % \dots | |
| 598 | %rules \(name\) \(rule\) | |
| 599 | % \dots | |
| 600 | %end | |
| 601 | %\end{ttbox}
 | |
| 602 | %where | |
| 603 | %\begin{tabular}[t]{l@{~=~}l}
 | |
| 604 | %$classes$ & \tt[("$c$",["$c@1$",\dots,"$c@m$"]),\dots] \\
 | |
| 605 | %$default$ & \tt["$d@1$",\dots,"$d@r$"]\\ | |
| 606 | %$types$ & \tt[([$tycon@1$,\dots,$tycon@i$], $n$),\dots] \\ | |
| 607 | %$arities$ & \tt[([$tycon'@1$,\dots,$tycon'@j$], ([$s@1$,\dots,$s@n$],$c$)),\dots] | |
| 608 | %\\ | |
| 609 | %$consts$ & \tt[([$b@1$,\dots,$b@k$],$\tau$),\dots] \\ | |
| 610 | %$rules$   & \tt[("$name$",$rule$),\dots]
 | |
| 611 | %\end{tabular}
 | |
| 104 | 612 | |
| 613 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 614 | \subsection{Inspecting a theory}\label{sec:inspct-thy}
 | 
| 104 | 615 | \index{theories!inspecting|bold}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 616 | \begin{ttbox}
 | 
| 4317 | 617 | print_syntax : theory -> unit | 
| 618 | print_theory : theory -> unit | |
| 619 | parents_of : theory -> theory list | |
| 620 | ancestors_of : theory -> theory list | |
| 621 | sign_of : theory -> Sign.sg | |
| 622 | Sign.stamp_names_of : Sign.sg -> string list | |
| 104 | 623 | \end{ttbox}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 624 | These provide means of viewing a theory's components. | 
| 324 | 625 | \begin{ttdescription}
 | 
| 3108 | 626 | \item[\ttindexbold{print_syntax} $thy$] prints the syntax of $thy$
 | 
| 627 | (grammar, macros, translation functions etc., see | |
| 628 |   page~\pageref{pg:print_syn} for more details).
 | |
| 629 | ||
| 630 | \item[\ttindexbold{print_theory} $thy$] prints the logical parts of
 | |
| 631 | $thy$, excluding the syntax. | |
| 4317 | 632 | |
| 633 | \item[\ttindexbold{parents_of} $thy$] returns the direct ancestors
 | |
| 634 | of~$thy$. | |
| 635 | ||
| 636 | \item[\ttindexbold{ancestors_of} $thy$] returns all ancestors of~$thy$
 | |
| 637 | (not including $thy$ itself). | |
| 638 | ||
| 639 | \item[\ttindexbold{sign_of} $thy$] returns the signature associated
 | |
| 640 |   with~$thy$.  It is useful with functions like {\tt
 | |
| 641 | read_instantiate_sg}, which take a signature as an argument. | |
| 642 | ||
| 643 | \item[\ttindexbold{Sign.stamp_names_of} $sg$]\index{signatures}
 | |
| 644 |   returns the names of the identification \rmindex{stamps} of ax
 | |
| 645 | signature. These coincide with the names of its full ancestry | |
| 646 | including that of $sg$ itself. | |
| 104 | 647 | |
| 324 | 648 | \end{ttdescription}
 | 
| 104 | 649 | |
| 1369 | 650 | |
| 11623 | 651 | \section{Terms}\label{sec:terms}
 | 
| 104 | 652 | \index{terms|bold}
 | 
| 324 | 653 | Terms belong to the \ML\ type \mltydx{term}, which is a concrete datatype
 | 
| 3108 | 654 | with six constructors: | 
| 104 | 655 | \begin{ttbox}
 | 
| 656 | type indexname = string * int; | |
| 657 | infix 9 $; | |
| 658 | datatype term = Const of string * typ | |
| 659 | | Free of string * typ | |
| 660 | | Var of indexname * typ | |
| 661 | | Bound of int | |
| 662 | | Abs of string * typ * term | |
| 663 | | op $ of term * term; | |
| 664 | \end{ttbox}
 | |
| 324 | 665 | \begin{ttdescription}
 | 
| 4317 | 666 | \item[\ttindexbold{Const} ($a$, $T$)] \index{constants|bold}
 | 
| 8136 | 667 |   is the \textbf{constant} with name~$a$ and type~$T$.  Constants include
 | 
| 286 | 668 | connectives like $\land$ and $\forall$ as well as constants like~0 | 
| 669 | and~$Suc$. Other constants may be required to define a logic's concrete | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 670 | syntax. | 
| 104 | 671 | |
| 4317 | 672 | \item[\ttindexbold{Free} ($a$, $T$)] \index{variables!free|bold}
 | 
| 8136 | 673 |   is the \textbf{free variable} with name~$a$ and type~$T$.
 | 
| 104 | 674 | |
| 4317 | 675 | \item[\ttindexbold{Var} ($v$, $T$)] \index{unknowns|bold}
 | 
| 8136 | 676 |   is the \textbf{scheme variable} with indexname~$v$ and type~$T$.  An
 | 
| 324 | 677 |   \mltydx{indexname} is a string paired with a non-negative index, or
 | 
| 678 | subscript; a term's scheme variables can be systematically renamed by | |
| 679 | incrementing their subscripts. Scheme variables are essentially free | |
| 680 | variables, but may be instantiated during unification. | |
| 104 | 681 | |
| 324 | 682 | \item[\ttindexbold{Bound} $i$] \index{variables!bound|bold}
 | 
| 8136 | 683 |   is the \textbf{bound variable} with de Bruijn index~$i$, which counts the
 | 
| 324 | 684 | number of lambdas, starting from zero, between a variable's occurrence | 
| 685 | and its binding. The representation prevents capture of variables. For | |
| 686 |   more information see de Bruijn \cite{debruijn72} or
 | |
| 6592 | 687 |   Paulson~\cite[page~376]{paulson-ml2}.
 | 
| 104 | 688 | |
| 4317 | 689 | \item[\ttindexbold{Abs} ($a$, $T$, $u$)]
 | 
| 324 | 690 |   \index{lambda abs@$\lambda$-abstractions|bold}
 | 
| 8136 | 691 |   is the $\lambda$-\textbf{abstraction} with body~$u$, and whose bound
 | 
| 324 | 692 | variable has name~$a$ and type~$T$. The name is used only for parsing | 
| 693 | and printing; it has no logical significance. | |
| 104 | 694 | |
| 324 | 695 | \item[$t$ \$ $u$] \index{$@{\tt\$}|bold} \index{function applications|bold}
 | 
| 8136 | 696 | is the \textbf{application} of~$t$ to~$u$.
 | 
| 324 | 697 | \end{ttdescription}
 | 
| 9695 | 698 | Application is written as an infix operator to aid readability. Here is an | 
| 699 | \ML\ pattern to recognize FOL formulae of the form~$A\imp B$, binding the | |
| 700 | subformulae to~$A$ and~$B$: | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 701 | \begin{ttbox}
 | 
| 104 | 702 | Const("Trueprop",_) $ (Const("op -->",_) $ A $ B)
 | 
| 703 | \end{ttbox}
 | |
| 704 | ||
| 705 | ||
| 4317 | 706 | \section{*Variable binding}
 | 
| 286 | 707 | \begin{ttbox}
 | 
| 708 | loose_bnos : term -> int list | |
| 709 | incr_boundvars : int -> term -> term | |
| 710 | abstract_over : term*term -> term | |
| 711 | variant_abs : string * typ * term -> string * term | |
| 8136 | 712 | aconv          : term * term -> bool\hfill\textbf{infix}
 | 
| 286 | 713 | \end{ttbox}
 | 
| 714 | These functions are all concerned with the de Bruijn representation of | |
| 715 | bound variables. | |
| 324 | 716 | \begin{ttdescription}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 717 | \item[\ttindexbold{loose_bnos} $t$]
 | 
| 286 | 718 | returns the list of all dangling bound variable references. In | 
| 6669 | 719 |   particular, \texttt{Bound~0} is loose unless it is enclosed in an
 | 
| 720 |   abstraction.  Similarly \texttt{Bound~1} is loose unless it is enclosed in
 | |
| 286 | 721 | at least two abstractions; if enclosed in just one, the list will contain | 
| 722 | the number 0. A well-formed term does not contain any loose variables. | |
| 723 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 724 | \item[\ttindexbold{incr_boundvars} $j$]
 | 
| 332 | 725 | increases a term's dangling bound variables by the offset~$j$. This is | 
| 286 | 726 | required when moving a subterm into a context where it is enclosed by a | 
| 727 | different number of abstractions. Bound variables with a matching | |
| 728 | abstraction are unaffected. | |
| 729 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 730 | \item[\ttindexbold{abstract_over} $(v,t)$]
 | 
| 286 | 731 | forms the abstraction of~$t$ over~$v$, which may be any well-formed term. | 
| 6669 | 732 |   It replaces every occurrence of \(v\) by a \texttt{Bound} variable with the
 | 
| 286 | 733 | correct index. | 
| 734 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 735 | \item[\ttindexbold{variant_abs} $(a,T,u)$]
 | 
| 286 | 736 | substitutes into $u$, which should be the body of an abstraction. | 
| 737 | It replaces each occurrence of the outermost bound variable by a free | |
| 738 | variable. The free variable has type~$T$ and its name is a variant | |
| 332 | 739 | of~$a$ chosen to be distinct from all constants and from all variables | 
| 286 | 740 | free in~$u$. | 
| 741 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 742 | \item[$t$ \ttindexbold{aconv} $u$]
 | 
| 286 | 743 | tests whether terms~$t$ and~$u$ are \(\alpha\)-convertible: identical up | 
| 744 | to renaming of bound variables. | |
| 745 | \begin{itemize}
 | |
| 746 | \item | |
| 6669 | 747 |     Two constants, \texttt{Free}s, or \texttt{Var}s are \(\alpha\)-convertible
 | 
| 286 | 748 | if their names and types are equal. | 
| 749 | (Variables having the same name but different types are thus distinct. | |
| 750 | This confusing situation should be avoided!) | |
| 751 | \item | |
| 752 | Two bound variables are \(\alpha\)-convertible | |
| 753 | if they have the same number. | |
| 754 | \item | |
| 755 | Two abstractions are \(\alpha\)-convertible | |
| 756 | if their bodies are, and their bound variables have the same type. | |
| 757 | \item | |
| 758 | Two applications are \(\alpha\)-convertible | |
| 759 | if the corresponding subterms are. | |
| 760 | \end{itemize}
 | |
| 761 | ||
| 324 | 762 | \end{ttdescription}
 | 
| 286 | 763 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 764 | \section{Certified terms}\index{terms!certified|bold}\index{signatures}
 | 
| 8136 | 765 | A term $t$ can be \textbf{certified} under a signature to ensure that every type
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 766 | in~$t$ is well-formed and every constant in~$t$ is a type instance of a | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 767 | constant declared in the signature. The term must be well-typed and its use | 
| 6669 | 768 | of bound variables must be well-formed.  Meta-rules such as \texttt{forall_elim}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 769 | take certified terms as arguments. | 
| 104 | 770 | |
| 324 | 771 | Certified terms belong to the abstract type \mltydx{cterm}.
 | 
| 104 | 772 | Elements of the type can only be created through the certification process. | 
| 773 | In case of error, Isabelle raises exception~\ttindex{TERM}\@.
 | |
| 774 | ||
| 775 | \subsection{Printing terms}
 | |
| 324 | 776 | \index{terms!printing of}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 777 | \begin{ttbox}
 | 
| 275 | 778 | string_of_cterm : cterm -> string | 
| 104 | 779 | Sign.string_of_term : Sign.sg -> term -> string | 
| 780 | \end{ttbox}
 | |
| 324 | 781 | \begin{ttdescription}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 782 | \item[\ttindexbold{string_of_cterm} $ct$]
 | 
| 104 | 783 | displays $ct$ as a string. | 
| 784 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 785 | \item[\ttindexbold{Sign.string_of_term} $sign$ $t$]
 | 
| 104 | 786 | displays $t$ as a string, using the syntax of~$sign$. | 
| 324 | 787 | \end{ttdescription}
 | 
| 104 | 788 | |
| 789 | \subsection{Making and inspecting certified terms}
 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 790 | \begin{ttbox}
 | 
| 8136 | 791 | cterm_of : Sign.sg -> term -> cterm | 
| 792 | read_cterm : Sign.sg -> string * typ -> cterm | |
| 793 | cert_axm : Sign.sg -> string * term -> string * term | |
| 794 | read_axm : Sign.sg -> string * string -> string * term | |
| 795 | rep_cterm      : cterm -> \{T:typ, t:term, sign:Sign.sg, maxidx:int\}
 | |
| 4543 | 796 | Sign.certify_term : Sign.sg -> term -> term * typ * int | 
| 104 | 797 | \end{ttbox}
 | 
| 324 | 798 | \begin{ttdescription}
 | 
| 4543 | 799 | |
| 800 | \item[\ttindexbold{cterm_of} $sign$ $t$] \index{signatures} certifies
 | |
| 801 | $t$ with respect to signature~$sign$. | |
| 802 | ||
| 803 | \item[\ttindexbold{read_cterm} $sign$ ($s$, $T$)] reads the string~$s$
 | |
| 804 | using the syntax of~$sign$, creating a certified term. The term is | |
| 805 | checked to have type~$T$; this type also tells the parser what kind | |
| 806 | of phrase to parse. | |
| 807 | ||
| 808 | \item[\ttindexbold{cert_axm} $sign$ ($name$, $t$)] certifies $t$ with
 | |
| 809 | respect to $sign$ as a meta-proposition and converts all exceptions | |
| 810 | to an error, including the final message | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 811 | \begin{ttbox}
 | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 812 | The error(s) above occurred in axiom "\(name\)" | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 813 | \end{ttbox}
 | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 814 | |
| 4543 | 815 | \item[\ttindexbold{read_axm} $sign$ ($name$, $s$)] similar to {\tt
 | 
| 816 | cert_axm}, but first reads the string $s$ using the syntax of | |
| 817 | $sign$. | |
| 818 | ||
| 819 | \item[\ttindexbold{rep_cterm} $ct$] decomposes $ct$ as a record
 | |
| 820 | containing its type, the term itself, its signature, and the maximum | |
| 821 | subscript of its unknowns. The type and maximum subscript are | |
| 822 | computed during certification. | |
| 823 | ||
| 824 | \item[\ttindexbold{Sign.certify_term}] is a more primitive version of
 | |
| 825 |   \texttt{cterm_of}, returning the internal representation instead of
 | |
| 826 |   an abstract \texttt{cterm}.
 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 827 | |
| 324 | 828 | \end{ttdescription}
 | 
| 104 | 829 | |
| 830 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 831 | \section{Types}\index{types|bold}
 | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 832 | Types belong to the \ML\ type \mltydx{typ}, which is a concrete datatype with
 | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 833 | three constructor functions. These correspond to type constructors, free | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 834 | type variables and schematic type variables. Types are classified by sorts, | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 835 | which are lists of classes (representing an intersection). A class is | 
| 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 836 | represented by a string. | 
| 104 | 837 | \begin{ttbox}
 | 
| 838 | type class = string; | |
| 839 | type sort = class list; | |
| 840 | ||
| 841 | datatype typ = Type of string * typ list | |
| 842 | | TFree of string * sort | |
| 843 | | TVar of indexname * sort; | |
| 844 | ||
| 845 | infixr 5 -->; | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 846 | fun S --> T = Type ("fun", [S, T]);
 | 
| 104 | 847 | \end{ttbox}
 | 
| 324 | 848 | \begin{ttdescription}
 | 
| 4317 | 849 | \item[\ttindexbold{Type} ($a$, $Ts$)] \index{type constructors|bold}
 | 
| 8136 | 850 |   applies the \textbf{type constructor} named~$a$ to the type operand list~$Ts$.
 | 
| 324 | 851 |   Type constructors include~\tydx{fun}, the binary function space
 | 
| 852 |   constructor, as well as nullary type constructors such as~\tydx{prop}.
 | |
| 853 | Other type constructors may be introduced. In expressions, but not in | |
| 854 |   patterns, \hbox{\tt$S$-->$T$} is a convenient shorthand for function
 | |
| 855 | types. | |
| 104 | 856 | |
| 4317 | 857 | \item[\ttindexbold{TFree} ($a$, $s$)] \index{type variables|bold}
 | 
| 8136 | 858 |   is the \textbf{type variable} with name~$a$ and sort~$s$.
 | 
| 104 | 859 | |
| 4317 | 860 | \item[\ttindexbold{TVar} ($v$, $s$)] \index{type unknowns|bold}
 | 
| 8136 | 861 |   is the \textbf{type unknown} with indexname~$v$ and sort~$s$.
 | 
| 324 | 862 | Type unknowns are essentially free type variables, but may be | 
| 863 | instantiated during unification. | |
| 864 | \end{ttdescription}
 | |
| 104 | 865 | |
| 866 | ||
| 867 | \section{Certified types}
 | |
| 868 | \index{types!certified|bold}
 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 869 | Certified types, which are analogous to certified terms, have type | 
| 275 | 870 | \ttindexbold{ctyp}.
 | 
| 104 | 871 | |
| 872 | \subsection{Printing types}
 | |
| 324 | 873 | \index{types!printing of}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 874 | \begin{ttbox}
 | 
| 275 | 875 | string_of_ctyp : ctyp -> string | 
| 104 | 876 | Sign.string_of_typ : Sign.sg -> typ -> string | 
| 877 | \end{ttbox}
 | |
| 324 | 878 | \begin{ttdescription}
 | 
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 879 | \item[\ttindexbold{string_of_ctyp} $cT$]
 | 
| 104 | 880 | displays $cT$ as a string. | 
| 881 | ||
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 882 | \item[\ttindexbold{Sign.string_of_typ} $sign$ $T$]
 | 
| 104 | 883 | displays $T$ as a string, using the syntax of~$sign$. | 
| 324 | 884 | \end{ttdescription}
 | 
| 104 | 885 | |
| 886 | ||
| 887 | \subsection{Making and inspecting certified types}
 | |
| 864 
d63b111b917a
quite a lot of minor and major revisions (inspecting theories, read_axm,
 wenzelm parents: 
478diff
changeset | 888 | \begin{ttbox}
 | 
| 4543 | 889 | ctyp_of : Sign.sg -> typ -> ctyp | 
| 8136 | 890 | rep_ctyp         : ctyp -> \{T: typ, sign: Sign.sg\}
 | 
| 4543 | 891 | Sign.certify_typ : Sign.sg -> typ -> typ | 
| 104 | 892 | \end{ttbox}
 | 
| 324 | 893 | \begin{ttdescription}
 | 
| 4543 | 894 | |
| 895 | \item[\ttindexbold{ctyp_of} $sign$ $T$] \index{signatures} certifies
 | |
| 896 | $T$ with respect to signature~$sign$. | |
| 897 | ||
| 898 | \item[\ttindexbold{rep_ctyp} $cT$] decomposes $cT$ as a record
 | |
| 899 | containing the type itself and its signature. | |
| 900 | ||
| 901 | \item[\ttindexbold{Sign.certify_typ}] is a more primitive version of
 | |
| 902 |   \texttt{ctyp_of}, returning the internal representation instead of
 | |
| 903 |   an abstract \texttt{ctyp}.
 | |
| 104 | 904 | |
| 324 | 905 | \end{ttdescription}
 | 
| 104 | 906 | |
| 1846 | 907 | |
| 4317 | 908 | \section{Oracles: calling trusted external reasoners}
 | 
| 1846 | 909 | \label{sec:oracles}
 | 
| 910 | \index{oracles|(}
 | |
| 911 | ||
| 912 | Oracles allow Isabelle to take advantage of external reasoners such as | |
| 913 | arithmetic decision procedures, model checkers, fast tautology checkers or | |
| 914 | computer algebra systems. Invoked as an oracle, an external reasoner can | |
| 915 | create arbitrary Isabelle theorems. It is your responsibility to ensure that | |
| 916 | the external reasoner is as trustworthy as your application requires. | |
| 917 | Isabelle's proof objects~(\S\ref{sec:proofObjects}) record how each theorem
 | |
| 918 | depends upon oracle calls. | |
| 919 | ||
| 920 | \begin{ttbox}
 | |
| 4317 | 921 | invoke_oracle : theory -> xstring -> Sign.sg * object -> thm | 
| 4597 
a0bdee64194c
Fixed a lot of overfull and underfull lines (hboxes)
 paulson parents: 
4543diff
changeset | 922 | Theory.add_oracle : bstring * (Sign.sg * object -> term) -> theory | 
| 
a0bdee64194c
Fixed a lot of overfull and underfull lines (hboxes)
 paulson parents: 
4543diff
changeset | 923 | -> theory | 
| 1846 | 924 | \end{ttbox}
 | 
| 925 | \begin{ttdescription}
 | |
| 4317 | 926 | \item[\ttindexbold{invoke_oracle} $thy$ $name$ ($sign$, $data$)]
 | 
| 927 | invokes the oracle $name$ of theory $thy$ passing the information | |
| 928 | contained in the exception value $data$ and creating a theorem | |
| 929 |   having signature $sign$.  Note that type \ttindex{object} is just an
 | |
| 930 |   abbreviation for \texttt{exn}.  Errors arise if $thy$ does not have
 | |
| 931 | an oracle called $name$, if the oracle rejects its arguments or if | |
| 932 | its result is ill-typed. | |
| 933 | ||
| 934 | \item[\ttindexbold{Theory.add_oracle} $name$ $fun$ $thy$] extends
 | |
| 935 | $thy$ by oracle $fun$ called $name$. It is seldom called | |
| 936 | explicitly, as there is concrete syntax for oracles in theory files. | |
| 1846 | 937 | \end{ttdescription}
 | 
| 938 | ||
| 939 | A curious feature of {\ML} exceptions is that they are ordinary constructors.
 | |
| 6669 | 940 | The {\ML} type \texttt{exn} is a datatype that can be extended at any time.  (See
 | 
| 1846 | 941 | my {\em {ML} for the Working Programmer}~\cite{paulson-ml2}, especially
 | 
| 942 | page~136.) The oracle mechanism takes advantage of this to allow an oracle to | |
| 943 | take any information whatever. | |
| 944 | ||
| 945 | There must be some way of invoking the external reasoner from \ML, either | |
| 946 | because it is coded in {\ML} or via an operating system interface.  Isabelle
 | |
| 947 | expects the {\ML} function to take two arguments: a signature and an
 | |
| 4317 | 948 | exception object. | 
| 1846 | 949 | \begin{itemize}
 | 
| 950 | \item The signature will typically be that of a desendant of the theory | |
| 951 | declaring the oracle. The oracle will use it to distinguish constants from | |
| 952 | variables, etc., and it will be attached to the generated theorems. | |
| 953 | ||
| 954 | \item The exception is used to pass arbitrary information to the oracle. This | |
| 955 | information must contain a full description of the problem to be solved by | |
| 956 | the external reasoner, including any additional information that might be | |
| 957 | required. The oracle may raise the exception to indicate that it cannot | |
| 958 | solve the specified problem. | |
| 959 | \end{itemize}
 | |
| 960 | ||
| 6669 | 961 | A trivial example is provided in theory \texttt{FOL/ex/IffOracle}.  This
 | 
| 4317 | 962 | oracle generates tautologies of the form $P\bimp\cdots\bimp P$, with | 
| 963 | an even number of $P$s. | |
| 1846 | 964 | |
| 4317 | 965 | The \texttt{ML} section of \texttt{IffOracle.thy} begins by declaring
 | 
| 966 | a few auxiliary functions (suppressed below) for creating the | |
| 967 | tautologies. Then it declares a new exception constructor for the | |
| 968 | information required by the oracle: here, just an integer. It finally | |
| 969 | defines the oracle function itself. | |
| 1846 | 970 | \begin{ttbox}
 | 
| 4317 | 971 | exception IffOracleExn of int;\medskip | 
| 972 | fun mk_iff_oracle (sign, IffOracleExn n) = | |
| 973 | if n > 0 andalso n mod 2 = 0 | |
| 6669 | 974 | then Trueprop \$ mk_iff n | 
| 4317 | 975 | else raise IffOracleExn n; | 
| 1846 | 976 | \end{ttbox}
 | 
| 6669 | 977 | Observe the function's two arguments, the signature \texttt{sign} and the
 | 
| 4317 | 978 | exception given as a pattern. The function checks its argument for | 
| 979 | validity. If $n$ is positive and even then it creates a tautology | |
| 980 | containing $n$ occurrences of~$P$. Otherwise it signals error by | |
| 981 | raising its own exception (just by happy coincidence). Errors may be | |
| 6669 | 982 | signalled by other means, such as returning the theorem \texttt{True}.
 | 
| 4317 | 983 | Please ensure that the oracle's result is correctly typed; Isabelle | 
| 984 | will reject ill-typed theorems by raising a cryptic exception at top | |
| 985 | level. | |
| 1846 | 986 | |
| 6669 | 987 | The \texttt{oracle} section of \texttt{IffOracle.thy} installs above
 | 
| 4317 | 988 | \texttt{ML} function as follows:
 | 
| 1846 | 989 | \begin{ttbox}
 | 
| 4317 | 990 | IffOracle = FOL +\medskip | 
| 991 | oracle | |
| 992 | iff = mk_iff_oracle\medskip | |
| 1846 | 993 | end | 
| 994 | \end{ttbox}
 | |
| 995 | ||
| 4317 | 996 | Now in \texttt{IffOracle.ML} we first define a wrapper for invoking
 | 
| 997 | the oracle: | |
| 1846 | 998 | \begin{ttbox}
 | 
| 4597 
a0bdee64194c
Fixed a lot of overfull and underfull lines (hboxes)
 paulson parents: 
4543diff
changeset | 999 | fun iff_oracle n = invoke_oracle IffOracle.thy "iff" | 
| 
a0bdee64194c
Fixed a lot of overfull and underfull lines (hboxes)
 paulson parents: 
4543diff
changeset | 1000 | (sign_of IffOracle.thy, IffOracleExn n); | 
| 4317 | 1001 | \end{ttbox}
 | 
| 1002 | ||
| 1003 | Here are some example applications of the \texttt{iff} oracle.  An
 | |
| 1004 | argument of 10 is allowed, but one of 5 is forbidden: | |
| 1005 | \begin{ttbox}
 | |
| 1006 | iff_oracle 10; | |
| 1846 | 1007 | {\out  "P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P <-> P" : thm}
 | 
| 4317 | 1008 | iff_oracle 5; | 
| 1846 | 1009 | {\out Exception- IffOracleExn 5 raised}
 | 
| 1010 | \end{ttbox}
 | |
| 1011 | ||
| 1012 | \index{oracles|)}
 | |
| 104 | 1013 | \index{theories|)}
 | 
| 5369 | 1014 | |
| 1015 | ||
| 1016 | %%% Local Variables: | |
| 1017 | %%% mode: latex | |
| 1018 | %%% TeX-master: "ref" | |
| 1019 | %%% End: |