doc-src/Intro/advanced.tex
author clasohm
Thu, 07 Dec 1995 14:24:32 +0100
changeset 1387 9bcad9c22fd4
parent 1366 3f3c25d3ec04
child 1467 3d19a5ddc21e
permissions -rw-r--r--
removed quotes from syntax and consts sections
lcp@105
     1
%% $Id$
lcp@284
     2
\part{Advanced Methods}
lcp@105
     3
Before continuing, it might be wise to try some of your own examples in
lcp@105
     4
Isabelle, reinforcing your knowledge of the basic functions.
lcp@105
     5
lcp@105
     6
Look through {\em Isabelle's Object-Logics\/} and try proving some simple
lcp@105
     7
theorems.  You probably should begin with first-order logic ({\tt FOL}
lcp@105
     8
or~{\tt LK}).  Try working some of the examples provided, and others from
lcp@105
     9
the literature.  Set theory~({\tt ZF}) and Constructive Type Theory~({\tt
lcp@105
    10
  CTT}) form a richer world for mathematical reasoning and, again, many
lcp@105
    11
examples are in the literature.  Higher-order logic~({\tt HOL}) is
lcp@331
    12
Isabelle's most sophisticated logic because its types and functions are
lcp@331
    13
identified with those of the meta-logic.
lcp@105
    14
lcp@105
    15
Choose a logic that you already understand.  Isabelle is a proof
lcp@105
    16
tool, not a teaching tool; if you do not know how to do a particular proof
lcp@105
    17
on paper, then you certainly will not be able to do it on the machine.
lcp@105
    18
Even experienced users plan large proofs on paper.
lcp@105
    19
lcp@105
    20
We have covered only the bare essentials of Isabelle, but enough to perform
lcp@105
    21
substantial proofs.  By occasionally dipping into the {\em Reference
lcp@105
    22
Manual}, you can learn additional tactics, subgoal commands and tacticals.
lcp@105
    23
lcp@105
    24
lcp@105
    25
\section{Deriving rules in Isabelle}
lcp@105
    26
\index{rules!derived}
lcp@105
    27
A mathematical development goes through a progression of stages.  Each
lcp@105
    28
stage defines some concepts and derives rules about them.  We shall see how
lcp@105
    29
to derive rules, perhaps involving definitions, using Isabelle.  The
lcp@310
    30
following section will explain how to declare types, constants, rules and
lcp@105
    31
definitions.
lcp@105
    32
lcp@105
    33
lcp@296
    34
\subsection{Deriving a rule using tactics and meta-level assumptions} 
lcp@296
    35
\label{deriving-example}
lcp@310
    36
\index{examples!of deriving rules}\index{assumptions!of main goal}
lcp@296
    37
lcp@307
    38
The subgoal module supports the derivation of rules, as discussed in
lcp@307
    39
\S\ref{deriving}.  The \ttindex{goal} command, when supplied a goal of the
lcp@307
    40
form $\List{\theta@1; \ldots; \theta@k} \Imp \phi$, creates $\phi\Imp\phi$
lcp@307
    41
as the initial proof state and returns a list consisting of the theorems
lcp@307
    42
${\theta@i\;[\theta@i]}$, for $i=1$, \ldots,~$k$.  These meta-assumptions
lcp@310
    43
are also recorded internally, allowing {\tt result} to discharge them
lcp@307
    44
in the original order.
lcp@105
    45
lcp@307
    46
Let us derive $\conj$ elimination using Isabelle.
lcp@310
    47
Until now, calling {\tt goal} has returned an empty list, which we have
lcp@105
    48
thrown away.  In this example, the list contains the two premises of the
lcp@105
    49
rule.  We bind them to the \ML\ identifiers {\tt major} and {\tt
lcp@105
    50
minor}:\footnote{Some ML compilers will print a message such as {\em
lcp@105
    51
binding not exhaustive}.  This warns that {\tt goal} must return a
lcp@105
    52
2-element list.  Otherwise, the pattern-match will fail; ML will
lcp@310
    53
raise exception \xdx{Match}.}
lcp@105
    54
\begin{ttbox}
lcp@105
    55
val [major,minor] = goal FOL.thy
lcp@105
    56
    "[| P&Q;  [| P; Q |] ==> R |] ==> R";
lcp@105
    57
{\out Level 0}
lcp@105
    58
{\out R}
lcp@105
    59
{\out  1. R}
lcp@105
    60
{\out val major = "P & Q  [P & Q]" : thm}
lcp@105
    61
{\out val minor = "[| P; Q |] ==> R  [[| P; Q |] ==> R]" : thm}
lcp@105
    62
\end{ttbox}
lcp@105
    63
Look at the minor premise, recalling that meta-level assumptions are
lcp@105
    64
shown in brackets.  Using {\tt minor}, we reduce $R$ to the subgoals
lcp@105
    65
$P$ and~$Q$:
lcp@105
    66
\begin{ttbox}
lcp@105
    67
by (resolve_tac [minor] 1);
lcp@105
    68
{\out Level 1}
lcp@105
    69
{\out R}
lcp@105
    70
{\out  1. P}
lcp@105
    71
{\out  2. Q}
lcp@105
    72
\end{ttbox}
lcp@105
    73
Deviating from~\S\ref{deriving}, we apply $({\conj}E1)$ forwards from the
lcp@105
    74
assumption $P\conj Q$ to obtain the theorem~$P\;[P\conj Q]$.
lcp@105
    75
\begin{ttbox}
lcp@105
    76
major RS conjunct1;
lcp@105
    77
{\out val it = "P  [P & Q]" : thm}
lcp@105
    78
\ttbreak
lcp@105
    79
by (resolve_tac [major RS conjunct1] 1);
lcp@105
    80
{\out Level 2}
lcp@105
    81
{\out R}
lcp@105
    82
{\out  1. Q}
lcp@105
    83
\end{ttbox}
lcp@105
    84
Similarly, we solve the subgoal involving~$Q$.
lcp@105
    85
\begin{ttbox}
lcp@105
    86
major RS conjunct2;
lcp@105
    87
{\out val it = "Q  [P & Q]" : thm}
lcp@105
    88
by (resolve_tac [major RS conjunct2] 1);
lcp@105
    89
{\out Level 3}
lcp@105
    90
{\out R}
lcp@105
    91
{\out No subgoals!}
lcp@105
    92
\end{ttbox}
lcp@105
    93
Calling \ttindex{topthm} returns the current proof state as a theorem.
lcp@105
    94
Note that it contains assumptions.  Calling \ttindex{result} discharges the
lcp@105
    95
assumptions --- both occurrences of $P\conj Q$ are discharged as one ---
lcp@105
    96
and makes the variables schematic.
lcp@105
    97
\begin{ttbox}
lcp@105
    98
topthm();
lcp@105
    99
{\out val it = "R  [P & Q, P & Q, [| P; Q |] ==> R]" : thm}
lcp@105
   100
val conjE = result();
lcp@105
   101
{\out val conjE = "[| ?P & ?Q; [| ?P; ?Q |] ==> ?R |] ==> ?R" : thm}
lcp@105
   102
\end{ttbox}
lcp@105
   103
lcp@105
   104
lcp@105
   105
\subsection{Definitions and derived rules} \label{definitions}
lcp@310
   106
\index{rules!derived}\index{definitions!and derived rules|(}
lcp@310
   107
lcp@105
   108
Definitions are expressed as meta-level equalities.  Let us define negation
lcp@105
   109
and the if-and-only-if connective:
lcp@105
   110
\begin{eqnarray*}
lcp@105
   111
  \neg \Var{P}          & \equiv & \Var{P}\imp\bot \\
lcp@105
   112
  \Var{P}\bimp \Var{Q}  & \equiv & 
lcp@105
   113
                (\Var{P}\imp \Var{Q}) \conj (\Var{Q}\imp \Var{P})
lcp@105
   114
\end{eqnarray*}
lcp@331
   115
\index{meta-rewriting}%
lcp@105
   116
Isabelle permits {\bf meta-level rewriting} using definitions such as
lcp@105
   117
these.  {\bf Unfolding} replaces every instance
lcp@331
   118
of $\neg \Var{P}$ by the corresponding instance of ${\Var{P}\imp\bot}$.  For
lcp@105
   119
example, $\forall x.\neg (P(x)\conj \neg R(x,0))$ unfolds to
lcp@105
   120
\[ \forall x.(P(x)\conj R(x,0)\imp\bot)\imp\bot.  \]
lcp@105
   121
{\bf Folding} a definition replaces occurrences of the right-hand side by
lcp@105
   122
the left.  The occurrences need not be free in the entire formula.
lcp@105
   123
lcp@105
   124
When you define new concepts, you should derive rules asserting their
lcp@105
   125
abstract properties, and then forget their definitions.  This supports
lcp@331
   126
modularity: if you later change the definitions without affecting their
lcp@105
   127
abstract properties, then most of your proofs will carry through without
lcp@105
   128
change.  Indiscriminate unfolding makes a subgoal grow exponentially,
lcp@105
   129
becoming unreadable.
lcp@105
   130
lcp@105
   131
Taking this point of view, Isabelle does not unfold definitions
lcp@105
   132
automatically during proofs.  Rewriting must be explicit and selective.
lcp@105
   133
Isabelle provides tactics and meta-rules for rewriting, and a version of
lcp@105
   134
the {\tt goal} command that unfolds the conclusion and premises of the rule
lcp@105
   135
being derived.
lcp@105
   136
lcp@105
   137
For example, the intuitionistic definition of negation given above may seem
lcp@105
   138
peculiar.  Using Isabelle, we shall derive pleasanter negation rules:
lcp@105
   139
\[  \infer[({\neg}I)]{\neg P}{\infer*{\bot}{[P]}}   \qquad
lcp@105
   140
    \infer[({\neg}E)]{Q}{\neg P & P}  \]
lcp@296
   141
This requires proving the following meta-formulae:
lcp@105
   142
$$ (P\Imp\bot)    \Imp \neg P   \eqno(\neg I)$$
lcp@105
   143
$$ \List{\neg P; P} \Imp Q.       \eqno(\neg E)$$
lcp@105
   144
lcp@105
   145
lcp@296
   146
\subsection{Deriving the $\neg$ introduction rule}
lcp@310
   147
To derive $(\neg I)$, we may call {\tt goal} with the appropriate
lcp@105
   148
formula.  Again, {\tt goal} returns a list consisting of the rule's
lcp@296
   149
premises.  We bind this one-element list to the \ML\ identifier {\tt
lcp@296
   150
  prems}.
lcp@105
   151
\begin{ttbox}
lcp@105
   152
val prems = goal FOL.thy "(P ==> False) ==> ~P";
lcp@105
   153
{\out Level 0}
lcp@105
   154
{\out ~P}
lcp@105
   155
{\out  1. ~P}
lcp@105
   156
{\out val prems = ["P ==> False  [P ==> False]"] : thm list}
lcp@105
   157
\end{ttbox}
lcp@310
   158
Calling \ttindex{rewrite_goals_tac} with \tdx{not_def}, which is the
lcp@105
   159
definition of negation, unfolds that definition in the subgoals.  It leaves
lcp@105
   160
the main goal alone.
lcp@105
   161
\begin{ttbox}
lcp@105
   162
not_def;
lcp@105
   163
{\out val it = "~?P == ?P --> False" : thm}
lcp@105
   164
by (rewrite_goals_tac [not_def]);
lcp@105
   165
{\out Level 1}
lcp@105
   166
{\out ~P}
lcp@105
   167
{\out  1. P --> False}
lcp@105
   168
\end{ttbox}
lcp@310
   169
Using \tdx{impI} and the premise, we reduce subgoal~1 to a triviality:
lcp@105
   170
\begin{ttbox}
lcp@105
   171
by (resolve_tac [impI] 1);
lcp@105
   172
{\out Level 2}
lcp@105
   173
{\out ~P}
lcp@105
   174
{\out  1. P ==> False}
lcp@105
   175
\ttbreak
lcp@105
   176
by (resolve_tac prems 1);
lcp@105
   177
{\out Level 3}
lcp@105
   178
{\out ~P}
lcp@105
   179
{\out  1. P ==> P}
lcp@105
   180
\end{ttbox}
lcp@296
   181
The rest of the proof is routine.  Note the form of the final result.
lcp@105
   182
\begin{ttbox}
lcp@105
   183
by (assume_tac 1);
lcp@105
   184
{\out Level 4}
lcp@105
   185
{\out ~P}
lcp@105
   186
{\out No subgoals!}
lcp@296
   187
\ttbreak
lcp@105
   188
val notI = result();
lcp@105
   189
{\out val notI = "(?P ==> False) ==> ~?P" : thm}
lcp@105
   190
\end{ttbox}
lcp@310
   191
\indexbold{*notI theorem}
lcp@105
   192
lcp@105
   193
There is a simpler way of conducting this proof.  The \ttindex{goalw}
lcp@310
   194
command starts a backward proof, as does {\tt goal}, but it also
lcp@296
   195
unfolds definitions.  Thus there is no need to call
lcp@296
   196
\ttindex{rewrite_goals_tac}:
lcp@105
   197
\begin{ttbox}
lcp@105
   198
val prems = goalw FOL.thy [not_def]
lcp@105
   199
    "(P ==> False) ==> ~P";
lcp@105
   200
{\out Level 0}
lcp@105
   201
{\out ~P}
lcp@105
   202
{\out  1. P --> False}
lcp@105
   203
{\out val prems = ["P ==> False  [P ==> False]"] : thm list}
lcp@105
   204
\end{ttbox}
lcp@105
   205
lcp@105
   206
lcp@296
   207
\subsection{Deriving the $\neg$ elimination rule}
lcp@284
   208
Let us derive the rule $(\neg E)$.  The proof follows that of~{\tt conjE}
lcp@296
   209
above, with an additional step to unfold negation in the major premise.
lcp@296
   210
Although the {\tt goalw} command is best for this, let us
lcp@310
   211
try~{\tt goal} to see another way of unfolding definitions.  After
lcp@310
   212
binding the premises to \ML\ identifiers, we apply \tdx{FalseE}:
lcp@105
   213
\begin{ttbox}
lcp@105
   214
val [major,minor] = goal FOL.thy "[| ~P;  P |] ==> R";
lcp@105
   215
{\out Level 0}
lcp@105
   216
{\out R}
lcp@105
   217
{\out  1. R}
lcp@105
   218
{\out val major = "~ P  [~ P]" : thm}
lcp@105
   219
{\out val minor = "P  [P]" : thm}
lcp@105
   220
\ttbreak
lcp@105
   221
by (resolve_tac [FalseE] 1);
lcp@105
   222
{\out Level 1}
lcp@105
   223
{\out R}
lcp@105
   224
{\out  1. False}
lcp@284
   225
\end{ttbox}
lcp@284
   226
Everything follows from falsity.  And we can prove falsity using the
lcp@284
   227
premises and Modus Ponens:
lcp@284
   228
\begin{ttbox}
lcp@105
   229
by (resolve_tac [mp] 1);
lcp@105
   230
{\out Level 2}
lcp@105
   231
{\out R}
lcp@105
   232
{\out  1. ?P1 --> False}
lcp@105
   233
{\out  2. ?P1}
lcp@105
   234
\end{ttbox}
lcp@105
   235
For subgoal~1, we transform the major premise from~$\neg P$
lcp@105
   236
to~${P\imp\bot}$.  The function \ttindex{rewrite_rule}, given a list of
lcp@296
   237
definitions, unfolds them in a theorem.  Rewriting does not
lcp@105
   238
affect the theorem's hypothesis, which remains~$\neg P$:
lcp@105
   239
\begin{ttbox}
lcp@105
   240
rewrite_rule [not_def] major;
lcp@105
   241
{\out val it = "P --> False  [~P]" : thm}
lcp@105
   242
by (resolve_tac [it] 1);
lcp@105
   243
{\out Level 3}
lcp@105
   244
{\out R}
lcp@105
   245
{\out  1. P}
lcp@105
   246
\end{ttbox}
lcp@296
   247
The subgoal {\tt?P1} has been instantiated to~{\tt P}, which we can prove
lcp@284
   248
using the minor premise:
lcp@105
   249
\begin{ttbox}
lcp@105
   250
by (resolve_tac [minor] 1);
lcp@105
   251
{\out Level 4}
lcp@105
   252
{\out R}
lcp@105
   253
{\out No subgoals!}
lcp@105
   254
val notE = result();
lcp@105
   255
{\out val notE = "[| ~?P; ?P |] ==> ?R" : thm}
lcp@105
   256
\end{ttbox}
lcp@310
   257
\indexbold{*notE theorem}
lcp@105
   258
lcp@105
   259
\medskip
lcp@284
   260
Again, there is a simpler way of conducting this proof.  Recall that
lcp@284
   261
the \ttindex{goalw} command unfolds definitions the conclusion; it also
lcp@284
   262
unfolds definitions in the premises:
lcp@105
   263
\begin{ttbox}
lcp@105
   264
val [major,minor] = goalw FOL.thy [not_def]
lcp@105
   265
    "[| ~P;  P |] ==> R";
lcp@105
   266
{\out val major = "P --> False  [~ P]" : thm}
lcp@105
   267
{\out val minor = "P  [P]" : thm}
lcp@105
   268
\end{ttbox}
lcp@296
   269
Observe the difference in {\tt major}; the premises are unfolded without
lcp@296
   270
calling~\ttindex{rewrite_rule}.  Incidentally, the four calls to
lcp@296
   271
\ttindex{resolve_tac} above can be collapsed to one, with the help
lcp@284
   272
of~\ttindex{RS}; this is a typical example of forward reasoning from a
lcp@284
   273
complex premise.
lcp@105
   274
\begin{ttbox}
lcp@105
   275
minor RS (major RS mp RS FalseE);
lcp@105
   276
{\out val it = "?P  [P, ~P]" : thm}
lcp@105
   277
by (resolve_tac [it] 1);
lcp@105
   278
{\out Level 1}
lcp@105
   279
{\out R}
lcp@105
   280
{\out No subgoals!}
lcp@105
   281
\end{ttbox}
lcp@310
   282
\index{definitions!and derived rules|)}
lcp@105
   283
lcp@310
   284
\goodbreak\medskip\index{*"!"! symbol!in main goal}
lcp@284
   285
Finally, here is a trick that is sometimes useful.  If the goal
lcp@105
   286
has an outermost meta-quantifier, then \ttindex{goal} and \ttindex{goalw}
lcp@284
   287
do not return the rule's premises in the list of theorems;  instead, the
lcp@284
   288
premises become assumptions in subgoal~1.  
lcp@284
   289
%%%It does not matter which variables are quantified over.
lcp@105
   290
\begin{ttbox}
lcp@105
   291
goalw FOL.thy [not_def] "!!P R. [| ~P;  P |] ==> R";
lcp@105
   292
{\out Level 0}
lcp@105
   293
{\out !!P R. [| ~ P; P |] ==> R}
lcp@105
   294
{\out  1. !!P R. [| P --> False; P |] ==> R}
lcp@105
   295
val it = [] : thm list
lcp@105
   296
\end{ttbox}
lcp@105
   297
The proof continues as before.  But instead of referring to \ML\
lcp@310
   298
identifiers, we refer to assumptions using {\tt eresolve_tac} or
lcp@310
   299
{\tt assume_tac}: 
lcp@105
   300
\begin{ttbox}
lcp@105
   301
by (resolve_tac [FalseE] 1);
lcp@105
   302
{\out Level 1}
lcp@105
   303
{\out !!P R. [| ~ P; P |] ==> R}
lcp@105
   304
{\out  1. !!P R. [| P --> False; P |] ==> False}
lcp@105
   305
\ttbreak
lcp@105
   306
by (eresolve_tac [mp] 1);
lcp@105
   307
{\out Level 2}
lcp@105
   308
{\out !!P R. [| ~ P; P |] ==> R}
lcp@105
   309
{\out  1. !!P R. P ==> P}
lcp@105
   310
\ttbreak
lcp@105
   311
by (assume_tac 1);
lcp@105
   312
{\out Level 3}
lcp@105
   313
{\out !!P R. [| ~ P; P |] ==> R}
lcp@105
   314
{\out No subgoals!}
lcp@105
   315
\end{ttbox}
lcp@105
   316
Calling \ttindex{result} strips the meta-quantifiers, so the resulting
lcp@105
   317
theorem is the same as before.
lcp@105
   318
\begin{ttbox}
lcp@105
   319
val notE = result();
lcp@105
   320
{\out val notE = "[| ~?P; ?P |] ==> ?R" : thm}
lcp@105
   321
\end{ttbox}
lcp@105
   322
Do not use the {\tt!!}\ trick if the premises contain meta-level
lcp@105
   323
connectives, because \ttindex{eresolve_tac} and \ttindex{assume_tac} would
lcp@105
   324
not be able to handle the resulting assumptions.  The trick is not suitable
lcp@105
   325
for deriving the introduction rule~$(\neg I)$.
lcp@105
   326
lcp@105
   327
lcp@284
   328
\section{Defining theories}\label{sec:defining-theories}
lcp@105
   329
\index{theories!defining|(}
lcp@310
   330
lcp@105
   331
Isabelle makes no distinction between simple extensions of a logic --- like
lcp@105
   332
defining a type~$bool$ with constants~$true$ and~$false$ --- and defining
lcp@105
   333
an entire logic.  A theory definition has the form
lcp@105
   334
\begin{ttbox}
lcp@105
   335
\(T\) = \(S@1\) + \(\cdots\) + \(S@n\) +
lcp@105
   336
classes      {\it class declarations}
lcp@105
   337
default      {\it sort}
lcp@331
   338
types        {\it type declarations and synonyms}
lcp@105
   339
arities      {\it arity declarations}
lcp@105
   340
consts       {\it constant declarations}
wenzelm@841
   341
translations {\it translation declarations}
lcp@1185
   342
defs         {\it definitions}
lcp@105
   343
rules        {\it rule declarations}
lcp@105
   344
end
lcp@105
   345
ML           {\it ML code}
lcp@105
   346
\end{ttbox}
lcp@105
   347
This declares the theory $T$ to extend the existing theories
lcp@105
   348
$S@1$,~\ldots,~$S@n$.  It may declare new classes, types, arities
lcp@105
   349
(overloadings of existing types), constants and rules; it can specify the
lcp@105
   350
default sort for type variables.  A constant declaration can specify an
lcp@105
   351
associated concrete syntax.  The translations section specifies rewrite
lcp@105
   352
rules on abstract syntax trees, for defining notations and abbreviations.
lcp@310
   353
\index{*ML section}
lcp@310
   354
The {\tt ML} section contains code to perform arbitrary syntactic
lcp@284
   355
transformations.  The main declaration forms are discussed below.
nipkow@303
   356
The full syntax can be found in \iflabelundefined{app:TheorySyntax}{the
lcp@307
   357
  appendix of the {\it Reference Manual}}{App.\ts\ref{app:TheorySyntax}}.
lcp@105
   358
lcp@1084
   359
All the declaration parts can be omitted or repeated and may appear in any
lcp@1084
   360
order, except that the {\ML} section must be last.  In the simplest case, $T$
lcp@1084
   361
is just the union of $S@1$,~\ldots,~$S@n$.  New theories always extend one or
lcp@1084
   362
more other theories, inheriting their types, constants, syntax, etc.  The
lcp@1084
   363
theory \thydx{Pure} contains nothing but Isabelle's meta-logic.
lcp@105
   364
lcp@331
   365
Each theory definition must reside in a separate file, whose name is the
lcp@331
   366
theory's with {\tt.thy} appended.  For example, theory {\tt ListFn} resides
lcp@331
   367
on a file named {\tt ListFn.thy}.  Isabelle uses this convention to locate the
lcp@331
   368
file containing a given theory; \ttindexbold{use_thy} automatically loads a
lcp@331
   369
theory's parents before loading the theory itself.
lcp@105
   370
lcp@331
   371
Calling \ttindexbold{use_thy}~{\tt"{\it T\/}"} reads a theory from the
lcp@331
   372
file {\it T}{\tt.thy}, writes the corresponding {\ML} code to the file
lcp@331
   373
{\tt.{\it T}.thy.ML}, reads the latter file, and deletes it if no errors
lcp@331
   374
occurred.  This declares the {\ML} structure~$T$, which contains a component
clasohm@109
   375
{\tt thy} denoting the new theory, a component for each rule, and everything
clasohm@109
   376
declared in {\it ML code}.
lcp@105
   377
lcp@105
   378
Errors may arise during the translation to {\ML} (say, a misspelled keyword)
lcp@105
   379
or during creation of the new theory (say, a type error in a rule).  But if
lcp@331
   380
all goes well, {\tt use_thy} will finally read the file {\it T}{\tt.ML}, if
lcp@105
   381
it exists.  This file typically begins with the {\ML} declaration {\tt
lcp@105
   382
open}~$T$ and contains proofs that refer to the components of~$T$.
lcp@105
   383
lcp@296
   384
When a theory file is modified, many theories may have to be reloaded.
lcp@296
   385
Isabelle records the modification times and dependencies of theory files.
lcp@331
   386
See 
lcp@331
   387
\iflabelundefined{sec:reloading-theories}{the {\em Reference Manual\/}}%
lcp@331
   388
                 {\S\ref{sec:reloading-theories}}
lcp@296
   389
for more details.
lcp@296
   390
lcp@105
   391
lcp@1084
   392
\subsection{Declaring constants, definitions and rules}
lcp@310
   393
\indexbold{constants!declaring}\index{rules!declaring}
lcp@310
   394
lcp@1084
   395
Most theories simply declare constants, definitions and rules.  The {\bf
lcp@1084
   396
  constant declaration part} has the form
lcp@105
   397
\begin{ttbox}
clasohm@1387
   398
consts  \(c@1\) :: \(\tau@1\)
lcp@105
   399
        \vdots
clasohm@1387
   400
        \(c@n\) :: \(\tau@n\)
lcp@105
   401
\end{ttbox}
lcp@105
   402
where $c@1$, \ldots, $c@n$ are constants and $\tau@1$, \ldots, $\tau@n$ are
clasohm@1387
   403
types.  The types must be enclosed in quotation marks if they contain
clasohm@1387
   404
user-declared infix type constructors like {\tt *}.  Each
lcp@105
   405
constant must be enclosed in quotation marks unless it is a valid
lcp@105
   406
identifier.  To declare $c@1$, \ldots, $c@n$ as constants of type $\tau$,
lcp@105
   407
the $n$ declarations may be abbreviated to a single line:
lcp@105
   408
\begin{ttbox}
clasohm@1387
   409
        \(c@1\), \ldots, \(c@n\) :: \(\tau\)
lcp@105
   410
\end{ttbox}
lcp@105
   411
The {\bf rule declaration part} has the form
lcp@105
   412
\begin{ttbox}
lcp@105
   413
rules   \(id@1\) "\(rule@1\)"
lcp@105
   414
        \vdots
lcp@105
   415
        \(id@n\) "\(rule@n\)"
lcp@105
   416
\end{ttbox}
lcp@105
   417
where $id@1$, \ldots, $id@n$ are \ML{} identifiers and $rule@1$, \ldots,
lcp@284
   418
$rule@n$ are expressions of type~$prop$.  Each rule {\em must\/} be
lcp@284
   419
enclosed in quotation marks.
lcp@284
   420
lcp@1084
   421
\indexbold{definitions} The {\bf definition part} is similar, but with the
lcp@1084
   422
keyword {\tt defs} instead of {\tt rules}.  {\bf Definitions} are rules of the
lcp@1084
   423
form $t\equiv u$, and should serve only as abbreviations.  Isabelle checks for
lcp@1084
   424
common errors in definitions, such as extra variables on the right-hard side.
lcp@1084
   425
Determined users can write non-conservative `definitions' by using mutual
lcp@1084
   426
recursion, for example; the consequences of such actions are their
lcp@1084
   427
responsibility.
lcp@105
   428
lcp@1084
   429
lcp@1084
   430
\index{examples!of theories} 
lcp@1084
   431
This theory extends first-order logic by declaring and defining two constants,
lcp@1084
   432
{\em nand} and {\em xor}:
lcp@284
   433
\begin{ttbox}
lcp@105
   434
Gate = FOL +
clasohm@1387
   435
consts  nand,xor :: [o,o] => o
lcp@1084
   436
defs    nand_def "nand(P,Q) == ~(P & Q)"
lcp@105
   437
        xor_def  "xor(P,Q)  == P & ~Q | ~P & Q"
lcp@105
   438
end
lcp@105
   439
\end{ttbox}
lcp@105
   440
nipkow@1366
   441
\begin{warn}
nipkow@1366
   442
A common mistake when writing definitions is to introduce extra free variables
nipkow@1366
   443
on the right-hard side as in the following fictitious definition:
nipkow@1366
   444
\begin{ttbox}
nipkow@1366
   445
defs  prime_def "prime(p) == (m divides p) --> (m=1 | m=p)"
nipkow@1366
   446
\end{ttbox}
nipkow@1366
   447
Isabelle rejects this ``definition'' because of the extra {\tt m} on the
nipkow@1366
   448
right-hard side, which would introduce an inconsistency. What you should have
nipkow@1366
   449
written is
nipkow@1366
   450
\begin{ttbox}
nipkow@1366
   451
defs  prime_def "prime(p) == ALL m. (m divides p) --> (m=1 | m=p)"
nipkow@1366
   452
\end{ttbox}
nipkow@1366
   453
\end{warn}
lcp@105
   454
lcp@105
   455
\subsection{Declaring type constructors}
nipkow@303
   456
\indexbold{types!declaring}\indexbold{arities!declaring}
lcp@284
   457
%
lcp@105
   458
Types are composed of type variables and {\bf type constructors}.  Each
lcp@284
   459
type constructor takes a fixed number of arguments.  They are declared
lcp@284
   460
with an \ML-like syntax.  If $list$ takes one type argument, $tree$ takes
lcp@284
   461
two arguments and $nat$ takes no arguments, then these type constructors
lcp@284
   462
can be declared by
lcp@284
   463
\begin{ttbox}
lcp@284
   464
types 'a list
lcp@284
   465
      ('a,'b) tree
lcp@284
   466
      nat
lcp@284
   467
\end{ttbox}
lcp@105
   468
lcp@284
   469
The {\bf type declaration part} has the general form
lcp@105
   470
\begin{ttbox}
lcp@284
   471
types   \(tids@1\) \(id@1\)
lcp@105
   472
        \vdots
wenzelm@841
   473
        \(tids@n\) \(id@n\)
lcp@105
   474
\end{ttbox}
lcp@284
   475
where $id@1$, \ldots, $id@n$ are identifiers and $tids@1$, \ldots, $tids@n$
lcp@284
   476
are type argument lists as shown in the example above.  It declares each
lcp@284
   477
$id@i$ as a type constructor with the specified number of argument places.
lcp@105
   478
lcp@105
   479
The {\bf arity declaration part} has the form
lcp@105
   480
\begin{ttbox}
lcp@105
   481
arities \(tycon@1\) :: \(arity@1\)
lcp@105
   482
        \vdots
lcp@105
   483
        \(tycon@n\) :: \(arity@n\)
lcp@105
   484
\end{ttbox}
lcp@105
   485
where $tycon@1$, \ldots, $tycon@n$ are identifiers and $arity@1$, \ldots,
lcp@105
   486
$arity@n$ are arities.  Arity declarations add arities to existing
lcp@296
   487
types; they do not declare the types themselves.
lcp@105
   488
In the simplest case, for an 0-place type constructor, an arity is simply
lcp@105
   489
the type's class.  Let us declare a type~$bool$ of class $term$, with
lcp@284
   490
constants $tt$ and~$ff$.  (In first-order logic, booleans are
lcp@284
   491
distinct from formulae, which have type $o::logic$.)
lcp@105
   492
\index{examples!of theories}
lcp@284
   493
\begin{ttbox}
lcp@105
   494
Bool = FOL +
lcp@284
   495
types   bool
lcp@105
   496
arities bool    :: term
clasohm@1387
   497
consts  tt,ff   :: bool
lcp@105
   498
end
lcp@105
   499
\end{ttbox}
lcp@296
   500
A $k$-place type constructor may have arities of the form
lcp@296
   501
$(s@1,\ldots,s@k)c$, where $s@1,\ldots,s@n$ are sorts and $c$ is a class.
lcp@296
   502
Each sort specifies a type argument; it has the form $\{c@1,\ldots,c@m\}$,
lcp@296
   503
where $c@1$, \dots,~$c@m$ are classes.  Mostly we deal with singleton
lcp@296
   504
sorts, and may abbreviate them by dropping the braces.  The arity
lcp@296
   505
$(term)term$ is short for $(\{term\})term$.  Recall the discussion in
lcp@296
   506
\S\ref{polymorphic}.
lcp@105
   507
lcp@105
   508
A type constructor may be overloaded (subject to certain conditions) by
lcp@296
   509
appearing in several arity declarations.  For instance, the function type
lcp@331
   510
constructor~$fun$ has the arity $(logic,logic)logic$; in higher-order
lcp@105
   511
logic, it is declared also to have arity $(term,term)term$.
lcp@105
   512
lcp@105
   513
Theory {\tt List} declares the 1-place type constructor $list$, gives
lcp@284
   514
it arity $(term)term$, and declares constants $Nil$ and $Cons$ with
lcp@296
   515
polymorphic types:%
lcp@296
   516
\footnote{In the {\tt consts} part, type variable {\tt'a} has the default
lcp@296
   517
  sort, which is {\tt term}.  See the {\em Reference Manual\/}
lcp@296
   518
\iflabelundefined{sec:ref-defining-theories}{}%
lcp@296
   519
{(\S\ref{sec:ref-defining-theories})} for more information.}
lcp@105
   520
\index{examples!of theories}
lcp@284
   521
\begin{ttbox}
lcp@105
   522
List = FOL +
lcp@284
   523
types   'a list
lcp@105
   524
arities list    :: (term)term
clasohm@1387
   525
consts  Nil     :: 'a list
clasohm@1387
   526
        Cons    :: ['a, 'a list] => 'a list
lcp@105
   527
end
lcp@105
   528
\end{ttbox}
lcp@284
   529
Multiple arity declarations may be abbreviated to a single line:
lcp@105
   530
\begin{ttbox}
lcp@105
   531
arities \(tycon@1\), \ldots, \(tycon@n\) :: \(arity\)
lcp@105
   532
\end{ttbox}
lcp@105
   533
lcp@105
   534
\begin{warn}
lcp@105
   535
Arity declarations resemble constant declarations, but there are {\it no\/}
lcp@105
   536
quotation marks!  Types and rules must be quoted because the theory
lcp@105
   537
translator passes them verbatim to the {\ML} output file.
lcp@105
   538
\end{warn}
lcp@105
   539
lcp@331
   540
\subsection{Type synonyms}\indexbold{type synonyms}
nipkow@303
   541
Isabelle supports {\bf type synonyms} ({\bf abbreviations}) which are similar
lcp@307
   542
to those found in \ML.  Such synonyms are defined in the type declaration part
nipkow@303
   543
and are fairly self explanatory:
nipkow@303
   544
\begin{ttbox}
clasohm@1387
   545
types gate       = [o,o] => o
clasohm@1387
   546
      'a pred    = 'a => o
clasohm@1387
   547
      ('a,'b)nuf = 'b => 'a
nipkow@303
   548
\end{ttbox}
nipkow@303
   549
Type declarations and synonyms can be mixed arbitrarily:
nipkow@303
   550
\begin{ttbox}
nipkow@303
   551
types nat
clasohm@1387
   552
      'a stream = nat => 'a
clasohm@1387
   553
      signal    = nat stream
nipkow@303
   554
      'a list
nipkow@303
   555
\end{ttbox}
lcp@307
   556
A synonym is merely an abbreviation for some existing type expression.  Hence
lcp@307
   557
synonyms may not be recursive!  Internally all synonyms are fully expanded.  As
lcp@307
   558
a consequence Isabelle output never contains synonyms.  Their main purpose is
lcp@307
   559
to improve the readability of theories.  Synonyms can be used just like any
nipkow@303
   560
other type:
nipkow@303
   561
\begin{ttbox}
clasohm@1387
   562
consts and,or :: gate
clasohm@1387
   563
       negate :: signal => signal
nipkow@303
   564
\end{ttbox}
nipkow@303
   565
lcp@348
   566
\subsection{Infix and mixfix operators}
lcp@310
   567
\index{infixes}\index{examples!of theories}
lcp@310
   568
lcp@310
   569
Infix or mixfix syntax may be attached to constants.  Consider the
lcp@310
   570
following theory:
lcp@284
   571
\begin{ttbox}
lcp@105
   572
Gate2 = FOL +
clasohm@1387
   573
consts  "~&"     :: [o,o] => o         (infixl 35)
clasohm@1387
   574
        "#"      :: [o,o] => o         (infixl 30)
lcp@1084
   575
defs    nand_def "P ~& Q == ~(P & Q)"    
lcp@105
   576
        xor_def  "P # Q  == P & ~Q | ~P & Q"
lcp@105
   577
end
lcp@105
   578
\end{ttbox}
lcp@310
   579
The constant declaration part declares two left-associating infix operators
lcp@310
   580
with their priorities, or precedences; they are $\nand$ of priority~35 and
lcp@310
   581
$\xor$ of priority~30.  Hence $P \xor Q \xor R$ is parsed as $(P\xor Q)
lcp@310
   582
\xor R$ and $P \xor Q \nand R$ as $P \xor (Q \nand R)$.  Note the quotation
lcp@310
   583
marks in \verb|"~&"| and \verb|"#"|.
lcp@105
   584
lcp@105
   585
The constants \hbox{\verb|op ~&|} and \hbox{\verb|op #|} are declared
lcp@105
   586
automatically, just as in \ML.  Hence you may write propositions like
lcp@105
   587
\verb|op #(True) == op ~&(True)|, which asserts that the functions $\lambda
lcp@105
   588
Q.True \xor Q$ and $\lambda Q.True \nand Q$ are identical.
lcp@105
   589
lcp@310
   590
\bigskip\index{mixfix declarations}
lcp@310
   591
{\bf Mixfix} operators may have arbitrary context-free syntaxes.  Let us
lcp@310
   592
add a line to the constant declaration part:
lcp@284
   593
\begin{ttbox}
clasohm@1387
   594
        If :: [o,o,o] => o       ("if _ then _ else _")
lcp@105
   595
\end{ttbox}
lcp@310
   596
This declares a constant $If$ of type $[o,o,o] \To o$ with concrete syntax {\tt
lcp@296
   597
  if~$P$ then~$Q$ else~$R$} as well as {\tt If($P$,$Q$,$R$)}.  Underscores
lcp@310
   598
denote argument positions.  
lcp@105
   599
lcp@310
   600
The declaration above does not allow the {\tt if}-{\tt then}-{\tt else}
lcp@310
   601
construct to be split across several lines, even if it is too long to fit
lcp@310
   602
on one line.  Pretty-printing information can be added to specify the
lcp@310
   603
layout of mixfix operators.  For details, see
lcp@310
   604
\iflabelundefined{Defining-Logics}%
lcp@310
   605
    {the {\it Reference Manual}, chapter `Defining Logics'}%
lcp@310
   606
    {Chap.\ts\ref{Defining-Logics}}.
lcp@310
   607
lcp@310
   608
Mixfix declarations can be annotated with priorities, just like
lcp@105
   609
infixes.  The example above is just a shorthand for
lcp@284
   610
\begin{ttbox}
clasohm@1387
   611
        If :: [o,o,o] => o       ("if _ then _ else _" [0,0,0] 1000)
lcp@105
   612
\end{ttbox}
lcp@310
   613
The numeric components determine priorities.  The list of integers
lcp@310
   614
defines, for each argument position, the minimal priority an expression
lcp@310
   615
at that position must have.  The final integer is the priority of the
lcp@105
   616
construct itself.  In the example above, any argument expression is
lcp@310
   617
acceptable because priorities are non-negative, and conditionals may
lcp@310
   618
appear everywhere because 1000 is the highest priority.  On the other
lcp@310
   619
hand, the declaration
lcp@284
   620
\begin{ttbox}
clasohm@1387
   621
        If :: [o,o,o] => o       ("if _ then _ else _" [100,0,0] 99)
lcp@105
   622
\end{ttbox}
lcp@284
   623
defines concrete syntax for a conditional whose first argument cannot have
lcp@310
   624
the form {\tt if~$P$ then~$Q$ else~$R$} because it must have a priority
lcp@310
   625
of at least~100.  We may of course write
lcp@284
   626
\begin{quote}\tt
lcp@284
   627
if (if $P$ then $Q$ else $R$) then $S$ else $T$
lcp@156
   628
\end{quote}
lcp@310
   629
because expressions in parentheses have maximal priority.  
lcp@105
   630
lcp@105
   631
Binary type constructors, like products and sums, may also be declared as
lcp@105
   632
infixes.  The type declaration below introduces a type constructor~$*$ with
lcp@105
   633
infix notation $\alpha*\beta$, together with the mixfix notation
lcp@1084
   634
${<}\_,\_{>}$ for pairs.  We also see a rule declaration part.
lcp@310
   635
\index{examples!of theories}\index{mixfix declarations}
lcp@105
   636
\begin{ttbox}
lcp@105
   637
Prod = FOL +
lcp@284
   638
types   ('a,'b) "*"                           (infixl 20)
lcp@105
   639
arities "*"     :: (term,term)term
lcp@105
   640
consts  fst     :: "'a * 'b => 'a"
lcp@105
   641
        snd     :: "'a * 'b => 'b"
lcp@105
   642
        Pair    :: "['a,'b] => 'a * 'b"       ("(1<_,/_>)")
lcp@105
   643
rules   fst     "fst(<a,b>) = a"
lcp@105
   644
        snd     "snd(<a,b>) = b"
lcp@105
   645
end
lcp@105
   646
\end{ttbox}
lcp@105
   647
lcp@105
   648
\begin{warn}
lcp@105
   649
The name of the type constructor is~{\tt *} and not {\tt op~*}, as it would
lcp@105
   650
be in the case of an infix constant.  Only infix type constructors can have
lcp@105
   651
symbolic names like~{\tt *}.  There is no general mixfix syntax for types.
lcp@105
   652
\end{warn}
lcp@105
   653
lcp@105
   654
lcp@105
   655
\subsection{Overloading}
lcp@105
   656
\index{overloading}\index{examples!of theories}
lcp@105
   657
The {\bf class declaration part} has the form
lcp@105
   658
\begin{ttbox}
lcp@105
   659
classes \(id@1\) < \(c@1\)
lcp@105
   660
        \vdots
lcp@105
   661
        \(id@n\) < \(c@n\)
lcp@105
   662
\end{ttbox}
lcp@105
   663
where $id@1$, \ldots, $id@n$ are identifiers and $c@1$, \ldots, $c@n$ are
lcp@105
   664
existing classes.  It declares each $id@i$ as a new class, a subclass
lcp@105
   665
of~$c@i$.  In the general case, an identifier may be declared to be a
lcp@105
   666
subclass of $k$ existing classes:
lcp@105
   667
\begin{ttbox}
lcp@105
   668
        \(id\) < \(c@1\), \ldots, \(c@k\)
lcp@105
   669
\end{ttbox}
lcp@296
   670
Type classes allow constants to be overloaded.  As suggested in
lcp@307
   671
\S\ref{polymorphic}, let us define the class $arith$ of arithmetic
lcp@296
   672
types with the constants ${+} :: [\alpha,\alpha]\To \alpha$ and $0,1 {::}
lcp@296
   673
\alpha$, for $\alpha{::}arith$.  We introduce $arith$ as a subclass of
lcp@296
   674
$term$ and add the three polymorphic constants of this class.
lcp@310
   675
\index{examples!of theories}\index{constants!overloaded}
lcp@105
   676
\begin{ttbox}
lcp@105
   677
Arith = FOL +
lcp@105
   678
classes arith < term
clasohm@1387
   679
consts  "0"     :: 'a::arith                  ("0")
clasohm@1387
   680
        "1"     :: 'a::arith                  ("1")
clasohm@1387
   681
        "+"     :: ['a::arith,'a] => 'a       (infixl 60)
lcp@105
   682
end
lcp@105
   683
\end{ttbox}
lcp@105
   684
No rules are declared for these constants: we merely introduce their
lcp@105
   685
names without specifying properties.  On the other hand, classes
lcp@105
   686
with rules make it possible to prove {\bf generic} theorems.  Such
lcp@105
   687
theorems hold for all instances, all types in that class.
lcp@105
   688
lcp@105
   689
We can now obtain distinct versions of the constants of $arith$ by
lcp@105
   690
declaring certain types to be of class $arith$.  For example, let us
lcp@105
   691
declare the 0-place type constructors $bool$ and $nat$:
lcp@105
   692
\index{examples!of theories}
lcp@105
   693
\begin{ttbox}
lcp@105
   694
BoolNat = Arith +
lcp@348
   695
types   bool  nat
lcp@348
   696
arities bool, nat   :: arith
clasohm@1387
   697
consts  Suc         :: nat=>nat
lcp@284
   698
\ttbreak
lcp@105
   699
rules   add0        "0 + n = n::nat"
lcp@105
   700
        addS        "Suc(m)+n = Suc(m+n)"
lcp@105
   701
        nat1        "1 = Suc(0)"
lcp@105
   702
        or0l        "0 + x = x::bool"
lcp@105
   703
        or0r        "x + 0 = x::bool"
lcp@105
   704
        or1l        "1 + x = 1::bool"
lcp@105
   705
        or1r        "x + 1 = 1::bool"
lcp@105
   706
end
lcp@105
   707
\end{ttbox}
lcp@105
   708
Because $nat$ and $bool$ have class $arith$, we can use $0$, $1$ and $+$ at
lcp@105
   709
either type.  The type constraints in the axioms are vital.  Without
lcp@105
   710
constraints, the $x$ in $1+x = x$ would have type $\alpha{::}arith$
lcp@105
   711
and the axiom would hold for any type of class $arith$.  This would
lcp@284
   712
collapse $nat$ to a trivial type:
lcp@105
   713
\[ Suc(1) = Suc(0+1) = Suc(0)+1 = 1+1 = 1! \]
lcp@105
   714
lcp@296
   715
lcp@296
   716
\section{Theory example: the natural numbers}
lcp@296
   717
lcp@296
   718
We shall now work through a small example of formalized mathematics
lcp@105
   719
demonstrating many of the theory extension features.
lcp@105
   720
lcp@105
   721
lcp@105
   722
\subsection{Extending first-order logic with the natural numbers}
lcp@105
   723
\index{examples!of theories}
lcp@105
   724
lcp@284
   725
Section\ts\ref{sec:logical-syntax} has formalized a first-order logic,
lcp@284
   726
including a type~$nat$ and the constants $0::nat$ and $Suc::nat\To nat$.
lcp@284
   727
Let us introduce the Peano axioms for mathematical induction and the
lcp@310
   728
freeness of $0$ and~$Suc$:\index{axioms!Peano}
lcp@307
   729
\[ \vcenter{\infer[(induct)]{P[n/x]}{P[0/x] & \infer*{P[Suc(x)/x]}{[P]}}}
lcp@105
   730
 \qquad \parbox{4.5cm}{provided $x$ is not free in any assumption except~$P$}
lcp@105
   731
\]
lcp@105
   732
\[ \infer[(Suc\_inject)]{m=n}{Suc(m)=Suc(n)} \qquad
lcp@105
   733
   \infer[(Suc\_neq\_0)]{R}{Suc(m)=0}
lcp@105
   734
\]
lcp@105
   735
Mathematical induction asserts that $P(n)$ is true, for any $n::nat$,
lcp@105
   736
provided $P(0)$ holds and that $P(x)$ implies $P(Suc(x))$ for all~$x$.
lcp@105
   737
Some authors express the induction step as $\forall x. P(x)\imp P(Suc(x))$.
lcp@105
   738
To avoid making induction require the presence of other connectives, we
lcp@105
   739
formalize mathematical induction as
lcp@105
   740
$$ \List{P(0); \Forall x. P(x)\Imp P(Suc(x))} \Imp P(n). \eqno(induct) $$
lcp@105
   741
lcp@105
   742
\noindent
lcp@105
   743
Similarly, to avoid expressing the other rules using~$\forall$, $\imp$
lcp@105
   744
and~$\neg$, we take advantage of the meta-logic;\footnote
lcp@105
   745
{On the other hand, the axioms $Suc(m)=Suc(n) \bimp m=n$
lcp@105
   746
and $\neg(Suc(m)=0)$ are logically equivalent to those given, and work
lcp@105
   747
better with Isabelle's simplifier.} 
lcp@105
   748
$(Suc\_neq\_0)$ is
lcp@105
   749
an elimination rule for $Suc(m)=0$:
lcp@105
   750
$$ Suc(m)=Suc(n) \Imp m=n  \eqno(Suc\_inject) $$
lcp@105
   751
$$ Suc(m)=0      \Imp R    \eqno(Suc\_neq\_0) $$
lcp@105
   752
lcp@105
   753
\noindent
lcp@105
   754
We shall also define a primitive recursion operator, $rec$.  Traditionally,
lcp@105
   755
primitive recursion takes a natural number~$a$ and a 2-place function~$f$,
lcp@105
   756
and obeys the equations
lcp@105
   757
\begin{eqnarray*}
lcp@105
   758
  rec(0,a,f)            & = & a \\
lcp@105
   759
  rec(Suc(m),a,f)       & = & f(m, rec(m,a,f))
lcp@105
   760
\end{eqnarray*}
lcp@105
   761
Addition, defined by $m+n \equiv rec(m,n,\lambda x\,y.Suc(y))$,
lcp@105
   762
should satisfy
lcp@105
   763
\begin{eqnarray*}
lcp@105
   764
  0+n      & = & n \\
lcp@105
   765
  Suc(m)+n & = & Suc(m+n)
lcp@105
   766
\end{eqnarray*}
lcp@296
   767
Primitive recursion appears to pose difficulties: first-order logic has no
lcp@296
   768
function-valued expressions.  We again take advantage of the meta-logic,
lcp@296
   769
which does have functions.  We also generalise primitive recursion to be
lcp@105
   770
polymorphic over any type of class~$term$, and declare the addition
lcp@105
   771
function:
lcp@105
   772
\begin{eqnarray*}
lcp@105
   773
  rec   & :: & [nat, \alpha{::}term, [nat,\alpha]\To\alpha] \To\alpha \\
lcp@105
   774
  +     & :: & [nat,nat]\To nat 
lcp@105
   775
\end{eqnarray*}
lcp@105
   776
lcp@105
   777
lcp@105
   778
\subsection{Declaring the theory to Isabelle}
lcp@105
   779
\index{examples!of theories}
lcp@310
   780
Let us create the theory \thydx{Nat} starting from theory~\verb$FOL$,
lcp@105
   781
which contains only classical logic with no natural numbers.  We declare
lcp@307
   782
the 0-place type constructor $nat$ and the associated constants.  Note that
lcp@307
   783
the constant~0 requires a mixfix annotation because~0 is not a legal
lcp@307
   784
identifier, and could not otherwise be written in terms:
lcp@310
   785
\begin{ttbox}\index{mixfix declarations}
lcp@105
   786
Nat = FOL +
lcp@284
   787
types   nat
lcp@105
   788
arities nat         :: term
clasohm@1387
   789
consts  "0"         :: nat                              ("0")
clasohm@1387
   790
        Suc         :: nat=>nat
clasohm@1387
   791
        rec         :: [nat, 'a, [nat,'a]=>'a] => 'a
clasohm@1387
   792
        "+"         :: [nat, nat] => nat                (infixl 60)
lcp@296
   793
rules   Suc_inject  "Suc(m)=Suc(n) ==> m=n"
lcp@105
   794
        Suc_neq_0   "Suc(m)=0      ==> R"
lcp@296
   795
        induct      "[| P(0);  !!x. P(x) ==> P(Suc(x)) |]  ==> P(n)"
lcp@105
   796
        rec_0       "rec(0,a,f) = a"
lcp@105
   797
        rec_Suc     "rec(Suc(m), a, f) = f(m, rec(m,a,f))"
lcp@296
   798
        add_def     "m+n == rec(m, n, \%x y. Suc(y))"
lcp@105
   799
end
lcp@105
   800
\end{ttbox}
lcp@105
   801
In axiom {\tt add_def}, recall that \verb|%| stands for~$\lambda$.
lcp@296
   802
Loading this theory file creates the \ML\ structure {\tt Nat}, which
lcp@296
   803
contains the theory and axioms.  Opening structure {\tt Nat} lets us write
lcp@296
   804
{\tt induct} instead of {\tt Nat.induct}, and so forth.
lcp@105
   805
\begin{ttbox}
lcp@105
   806
open Nat;
lcp@105
   807
\end{ttbox}
lcp@296
   808
lcp@296
   809
\subsection{Proving some recursion equations}
lcp@331
   810
File {\tt FOL/ex/Nat.ML} contains proofs involving this theory of the
lcp@105
   811
natural numbers.  As a trivial example, let us derive recursion equations
lcp@105
   812
for \verb$+$.  Here is the zero case:
lcp@284
   813
\begin{ttbox}
lcp@105
   814
goalw Nat.thy [add_def] "0+n = n";
lcp@105
   815
{\out Level 0}
lcp@105
   816
{\out 0 + n = n}
lcp@284
   817
{\out  1. rec(0,n,\%x y. Suc(y)) = n}
lcp@105
   818
\ttbreak
lcp@105
   819
by (resolve_tac [rec_0] 1);
lcp@105
   820
{\out Level 1}
lcp@105
   821
{\out 0 + n = n}
lcp@105
   822
{\out No subgoals!}
lcp@105
   823
val add_0 = result();
lcp@284
   824
\end{ttbox}
lcp@105
   825
And here is the successor case:
lcp@284
   826
\begin{ttbox}
lcp@105
   827
goalw Nat.thy [add_def] "Suc(m)+n = Suc(m+n)";
lcp@105
   828
{\out Level 0}
lcp@105
   829
{\out Suc(m) + n = Suc(m + n)}
lcp@284
   830
{\out  1. rec(Suc(m),n,\%x y. Suc(y)) = Suc(rec(m,n,\%x y. Suc(y)))}
lcp@105
   831
\ttbreak
lcp@105
   832
by (resolve_tac [rec_Suc] 1);
lcp@105
   833
{\out Level 1}
lcp@105
   834
{\out Suc(m) + n = Suc(m + n)}
lcp@105
   835
{\out No subgoals!}
lcp@105
   836
val add_Suc = result();
lcp@284
   837
\end{ttbox}
lcp@105
   838
The induction rule raises some complications, which are discussed next.
lcp@105
   839
\index{theories!defining|)}
lcp@105
   840
lcp@105
   841
lcp@105
   842
\section{Refinement with explicit instantiation}
lcp@310
   843
\index{resolution!with instantiation}
lcp@310
   844
\index{instantiation|(}
lcp@310
   845
lcp@105
   846
In order to employ mathematical induction, we need to refine a subgoal by
lcp@105
   847
the rule~$(induct)$.  The conclusion of this rule is $\Var{P}(\Var{n})$,
lcp@105
   848
which is highly ambiguous in higher-order unification.  It matches every
lcp@105
   849
way that a formula can be regarded as depending on a subterm of type~$nat$.
lcp@105
   850
To get round this problem, we could make the induction rule conclude
lcp@105
   851
$\forall n.\Var{P}(n)$ --- but putting a subgoal into this form requires
lcp@105
   852
refinement by~$(\forall E)$, which is equally hard!
lcp@105
   853
lcp@105
   854
The tactic {\tt res_inst_tac}, like {\tt resolve_tac}, refines a subgoal by
lcp@105
   855
a rule.  But it also accepts explicit instantiations for the rule's
lcp@105
   856
schematic variables.  
lcp@105
   857
\begin{description}
lcp@310
   858
\item[\ttindex{res_inst_tac} {\it insts} {\it thm} {\it i}]
lcp@105
   859
instantiates the rule {\it thm} with the instantiations {\it insts}, and
lcp@105
   860
then performs resolution on subgoal~$i$.
lcp@105
   861
lcp@310
   862
\item[\ttindex{eres_inst_tac}] 
lcp@310
   863
and \ttindex{dres_inst_tac} are similar, but perform elim-resolution
lcp@105
   864
and destruct-resolution, respectively.
lcp@105
   865
\end{description}
lcp@105
   866
The list {\it insts} consists of pairs $[(v@1,e@1), \ldots, (v@n,e@n)]$,
lcp@105
   867
where $v@1$, \ldots, $v@n$ are names of schematic variables in the rule ---
lcp@307
   868
with no leading question marks! --- and $e@1$, \ldots, $e@n$ are
lcp@105
   869
expressions giving their instantiations.  The expressions are type-checked
lcp@105
   870
in the context of a particular subgoal: free variables receive the same
lcp@105
   871
types as they have in the subgoal, and parameters may appear.  Type
lcp@105
   872
variable instantiations may appear in~{\it insts}, but they are seldom
lcp@105
   873
required: {\tt res_inst_tac} instantiates type variables automatically
lcp@105
   874
whenever the type of~$e@i$ is an instance of the type of~$\Var{v@i}$.
lcp@105
   875
lcp@105
   876
\subsection{A simple proof by induction}
lcp@310
   877
\index{examples!of induction}
lcp@105
   878
Let us prove that no natural number~$k$ equals its own successor.  To
lcp@105
   879
use~$(induct)$, we instantiate~$\Var{n}$ to~$k$; Isabelle finds a good
lcp@105
   880
instantiation for~$\Var{P}$.
lcp@284
   881
\begin{ttbox}
lcp@105
   882
goal Nat.thy "~ (Suc(k) = k)";
lcp@105
   883
{\out Level 0}
lcp@459
   884
{\out Suc(k) ~= k}
lcp@459
   885
{\out  1. Suc(k) ~= k}
lcp@105
   886
\ttbreak
lcp@105
   887
by (res_inst_tac [("n","k")] induct 1);
lcp@105
   888
{\out Level 1}
lcp@459
   889
{\out Suc(k) ~= k}
lcp@459
   890
{\out  1. Suc(0) ~= 0}
lcp@459
   891
{\out  2. !!x. Suc(x) ~= x ==> Suc(Suc(x)) ~= Suc(x)}
lcp@284
   892
\end{ttbox}
lcp@105
   893
We should check that Isabelle has correctly applied induction.  Subgoal~1
lcp@105
   894
is the base case, with $k$ replaced by~0.  Subgoal~2 is the inductive step,
lcp@105
   895
with $k$ replaced by~$Suc(x)$ and with an induction hypothesis for~$x$.
lcp@310
   896
The rest of the proof demonstrates~\tdx{notI}, \tdx{notE} and the
lcp@310
   897
other rules of theory {\tt Nat}.  The base case holds by~\ttindex{Suc_neq_0}:
lcp@284
   898
\begin{ttbox}
lcp@105
   899
by (resolve_tac [notI] 1);
lcp@105
   900
{\out Level 2}
lcp@459
   901
{\out Suc(k) ~= k}
lcp@105
   902
{\out  1. Suc(0) = 0 ==> False}
lcp@459
   903
{\out  2. !!x. Suc(x) ~= x ==> Suc(Suc(x)) ~= Suc(x)}
lcp@105
   904
\ttbreak
lcp@105
   905
by (eresolve_tac [Suc_neq_0] 1);
lcp@105
   906
{\out Level 3}
lcp@459
   907
{\out Suc(k) ~= k}
lcp@459
   908
{\out  1. !!x. Suc(x) ~= x ==> Suc(Suc(x)) ~= Suc(x)}
lcp@284
   909
\end{ttbox}
lcp@105
   910
The inductive step holds by the contrapositive of~\ttindex{Suc_inject}.
lcp@284
   911
Negation rules transform the subgoal into that of proving $Suc(x)=x$ from
lcp@284
   912
$Suc(Suc(x)) = Suc(x)$:
lcp@284
   913
\begin{ttbox}
lcp@105
   914
by (resolve_tac [notI] 1);
lcp@105
   915
{\out Level 4}
lcp@459
   916
{\out Suc(k) ~= k}
lcp@459
   917
{\out  1. !!x. [| Suc(x) ~= x; Suc(Suc(x)) = Suc(x) |] ==> False}
lcp@105
   918
\ttbreak
lcp@105
   919
by (eresolve_tac [notE] 1);
lcp@105
   920
{\out Level 5}
lcp@459
   921
{\out Suc(k) ~= k}
lcp@105
   922
{\out  1. !!x. Suc(Suc(x)) = Suc(x) ==> Suc(x) = x}
lcp@105
   923
\ttbreak
lcp@105
   924
by (eresolve_tac [Suc_inject] 1);
lcp@105
   925
{\out Level 6}
lcp@459
   926
{\out Suc(k) ~= k}
lcp@105
   927
{\out No subgoals!}
lcp@284
   928
\end{ttbox}
lcp@105
   929
lcp@105
   930
lcp@105
   931
\subsection{An example of ambiguity in {\tt resolve_tac}}
lcp@105
   932
\index{examples!of induction}\index{unification!higher-order}
lcp@105
   933
If you try the example above, you may observe that {\tt res_inst_tac} is
lcp@105
   934
not actually needed.  Almost by chance, \ttindex{resolve_tac} finds the right
lcp@105
   935
instantiation for~$(induct)$ to yield the desired next state.  With more
lcp@105
   936
complex formulae, our luck fails.  
lcp@284
   937
\begin{ttbox}
lcp@105
   938
goal Nat.thy "(k+m)+n = k+(m+n)";
lcp@105
   939
{\out Level 0}
lcp@105
   940
{\out k + m + n = k + (m + n)}
lcp@105
   941
{\out  1. k + m + n = k + (m + n)}
lcp@105
   942
\ttbreak
lcp@105
   943
by (resolve_tac [induct] 1);
lcp@105
   944
{\out Level 1}
lcp@105
   945
{\out k + m + n = k + (m + n)}
lcp@105
   946
{\out  1. k + m + n = 0}
lcp@105
   947
{\out  2. !!x. k + m + n = x ==> k + m + n = Suc(x)}
lcp@284
   948
\end{ttbox}
lcp@284
   949
This proof requires induction on~$k$.  The occurrence of~0 in subgoal~1
lcp@284
   950
indicates that induction has been applied to the term~$k+(m+n)$; this
lcp@284
   951
application is sound but will not lead to a proof here.  Fortunately,
lcp@284
   952
Isabelle can (lazily!) generate all the valid applications of induction.
lcp@284
   953
The \ttindex{back} command causes backtracking to an alternative outcome of
lcp@284
   954
the tactic.
lcp@284
   955
\begin{ttbox}
lcp@105
   956
back();
lcp@105
   957
{\out Level 1}
lcp@105
   958
{\out k + m + n = k + (m + n)}
lcp@105
   959
{\out  1. k + m + n = k + 0}
lcp@105
   960
{\out  2. !!x. k + m + n = k + x ==> k + m + n = k + Suc(x)}
lcp@284
   961
\end{ttbox}
lcp@284
   962
Now induction has been applied to~$m+n$.  This is equally useless.  Let us
lcp@284
   963
call \ttindex{back} again.
lcp@284
   964
\begin{ttbox}
lcp@105
   965
back();
lcp@105
   966
{\out Level 1}
lcp@105
   967
{\out k + m + n = k + (m + n)}
lcp@105
   968
{\out  1. k + m + 0 = k + (m + 0)}
lcp@284
   969
{\out  2. !!x. k + m + x = k + (m + x) ==>}
lcp@284
   970
{\out          k + m + Suc(x) = k + (m + Suc(x))}
lcp@284
   971
\end{ttbox}
lcp@105
   972
Now induction has been applied to~$n$.  What is the next alternative?
lcp@284
   973
\begin{ttbox}
lcp@105
   974
back();
lcp@105
   975
{\out Level 1}
lcp@105
   976
{\out k + m + n = k + (m + n)}
lcp@105
   977
{\out  1. k + m + n = k + (m + 0)}
lcp@105
   978
{\out  2. !!x. k + m + n = k + (m + x) ==> k + m + n = k + (m + Suc(x))}
lcp@284
   979
\end{ttbox}
lcp@105
   980
Inspecting subgoal~1 reveals that induction has been applied to just the
lcp@105
   981
second occurrence of~$n$.  This perfectly legitimate induction is useless
lcp@310
   982
here.  
lcp@310
   983
lcp@310
   984
The main goal admits fourteen different applications of induction.  The
lcp@310
   985
number is exponential in the size of the formula.
lcp@105
   986
lcp@105
   987
\subsection{Proving that addition is associative}
lcp@331
   988
Let us invoke the induction rule properly, using~{\tt
lcp@310
   989
  res_inst_tac}.  At the same time, we shall have a glimpse at Isabelle's
lcp@310
   990
simplification tactics, which are described in 
lcp@310
   991
\iflabelundefined{simp-chap}%
lcp@310
   992
    {the {\em Reference Manual}}{Chap.\ts\ref{simp-chap}}.
lcp@105
   993
lcp@310
   994
\index{simplification}\index{examples!of simplification} 
lcp@284
   995
lcp@310
   996
Isabelle's simplification tactics repeatedly apply equations to a subgoal,
lcp@310
   997
perhaps proving it.  For efficiency, the rewrite rules must be
lcp@310
   998
packaged into a {\bf simplification set},\index{simplification sets} 
lcp@310
   999
or {\bf simpset}.  We take the standard simpset for first-order logic and
lcp@331
  1000
insert the equations proved in the previous section, namely
lcp@310
  1001
$0+n=n$ and ${\tt Suc}(m)+n={\tt Suc}(m+n)$:
lcp@284
  1002
\begin{ttbox}
lcp@459
  1003
val add_ss = FOL_ss addsimps [add_0, add_Suc];
lcp@284
  1004
\end{ttbox}
lcp@105
  1005
We state the goal for associativity of addition, and
lcp@105
  1006
use \ttindex{res_inst_tac} to invoke induction on~$k$:
lcp@284
  1007
\begin{ttbox}
lcp@105
  1008
goal Nat.thy "(k+m)+n = k+(m+n)";
lcp@105
  1009
{\out Level 0}
lcp@105
  1010
{\out k + m + n = k + (m + n)}
lcp@105
  1011
{\out  1. k + m + n = k + (m + n)}
lcp@105
  1012
\ttbreak
lcp@105
  1013
by (res_inst_tac [("n","k")] induct 1);
lcp@105
  1014
{\out Level 1}
lcp@105
  1015
{\out k + m + n = k + (m + n)}
lcp@105
  1016
{\out  1. 0 + m + n = 0 + (m + n)}
lcp@284
  1017
{\out  2. !!x. x + m + n = x + (m + n) ==>}
lcp@284
  1018
{\out          Suc(x) + m + n = Suc(x) + (m + n)}
lcp@284
  1019
\end{ttbox}
lcp@105
  1020
The base case holds easily; both sides reduce to $m+n$.  The
lcp@105
  1021
tactic~\ttindex{simp_tac} rewrites with respect to the given simplification
lcp@331
  1022
set, applying the rewrite rules for addition:
lcp@284
  1023
\begin{ttbox}
lcp@105
  1024
by (simp_tac add_ss 1);
lcp@105
  1025
{\out Level 2}
lcp@105
  1026
{\out k + m + n = k + (m + n)}
lcp@284
  1027
{\out  1. !!x. x + m + n = x + (m + n) ==>}
lcp@284
  1028
{\out          Suc(x) + m + n = Suc(x) + (m + n)}
lcp@284
  1029
\end{ttbox}
lcp@331
  1030
The inductive step requires rewriting by the equations for addition
lcp@105
  1031
together the induction hypothesis, which is also an equation.  The
lcp@105
  1032
tactic~\ttindex{asm_simp_tac} rewrites using a simplification set and any
lcp@105
  1033
useful assumptions:
lcp@284
  1034
\begin{ttbox}
lcp@105
  1035
by (asm_simp_tac add_ss 1);
lcp@105
  1036
{\out Level 3}
lcp@105
  1037
{\out k + m + n = k + (m + n)}
lcp@105
  1038
{\out No subgoals!}
lcp@284
  1039
\end{ttbox}
lcp@310
  1040
\index{instantiation|)}
lcp@105
  1041
lcp@105
  1042
lcp@284
  1043
\section{A Prolog interpreter}
lcp@105
  1044
\index{Prolog interpreter|bold}
lcp@284
  1045
To demonstrate the power of tacticals, let us construct a Prolog
lcp@105
  1046
interpreter and execute programs involving lists.\footnote{To run these
lcp@331
  1047
examples, see the file {\tt FOL/ex/Prolog.ML}.} The Prolog program
lcp@105
  1048
consists of a theory.  We declare a type constructor for lists, with an
lcp@105
  1049
arity declaration to say that $(\tau)list$ is of class~$term$
lcp@105
  1050
provided~$\tau$ is:
lcp@105
  1051
\begin{eqnarray*}
lcp@105
  1052
  list  & :: & (term)term
lcp@105
  1053
\end{eqnarray*}
lcp@105
  1054
We declare four constants: the empty list~$Nil$; the infix list
lcp@105
  1055
constructor~{:}; the list concatenation predicate~$app$; the list reverse
lcp@284
  1056
predicate~$rev$.  (In Prolog, functions on lists are expressed as
lcp@105
  1057
predicates.)
lcp@105
  1058
\begin{eqnarray*}
lcp@105
  1059
    Nil         & :: & \alpha list \\
lcp@105
  1060
    {:}         & :: & [\alpha,\alpha list] \To \alpha list \\
lcp@105
  1061
    app & :: & [\alpha list,\alpha list,\alpha list] \To o \\
lcp@105
  1062
    rev & :: & [\alpha list,\alpha list] \To o 
lcp@105
  1063
\end{eqnarray*}
lcp@284
  1064
The predicate $app$ should satisfy the Prolog-style rules
lcp@105
  1065
\[ {app(Nil,ys,ys)} \qquad
lcp@105
  1066
   {app(xs,ys,zs) \over app(x:xs, ys, x:zs)} \]
lcp@105
  1067
We define the naive version of $rev$, which calls~$app$:
lcp@105
  1068
\[ {rev(Nil,Nil)} \qquad
lcp@105
  1069
   {rev(xs,ys)\quad  app(ys, x:Nil, zs) \over
lcp@105
  1070
    rev(x:xs, zs)} 
lcp@105
  1071
\]
lcp@105
  1072
lcp@105
  1073
\index{examples!of theories}
lcp@310
  1074
Theory \thydx{Prolog} extends first-order logic in order to make use
lcp@105
  1075
of the class~$term$ and the type~$o$.  The interpreter does not use the
lcp@310
  1076
rules of~{\tt FOL}.
lcp@105
  1077
\begin{ttbox}
lcp@105
  1078
Prolog = FOL +
lcp@296
  1079
types   'a list
lcp@105
  1080
arities list    :: (term)term
clasohm@1387
  1081
consts  Nil     :: 'a list
clasohm@1387
  1082
        ":"     :: ['a, 'a list]=> 'a list            (infixr 60)
clasohm@1387
  1083
        app     :: ['a list, 'a list, 'a list] => o
clasohm@1387
  1084
        rev     :: ['a list, 'a list] => o
lcp@105
  1085
rules   appNil  "app(Nil,ys,ys)"
lcp@105
  1086
        appCons "app(xs,ys,zs) ==> app(x:xs, ys, x:zs)"
lcp@105
  1087
        revNil  "rev(Nil,Nil)"
lcp@105
  1088
        revCons "[| rev(xs,ys); app(ys,x:Nil,zs) |] ==> rev(x:xs,zs)"
lcp@105
  1089
end
lcp@105
  1090
\end{ttbox}
lcp@105
  1091
\subsection{Simple executions}
lcp@284
  1092
Repeated application of the rules solves Prolog goals.  Let us
lcp@105
  1093
append the lists $[a,b,c]$ and~$[d,e]$.  As the rules are applied, the
lcp@105
  1094
answer builds up in~{\tt ?x}.
lcp@105
  1095
\begin{ttbox}
lcp@105
  1096
goal Prolog.thy "app(a:b:c:Nil, d:e:Nil, ?x)";
lcp@105
  1097
{\out Level 0}
lcp@105
  1098
{\out app(a : b : c : Nil, d : e : Nil, ?x)}
lcp@105
  1099
{\out  1. app(a : b : c : Nil, d : e : Nil, ?x)}
lcp@105
  1100
\ttbreak
lcp@105
  1101
by (resolve_tac [appNil,appCons] 1);
lcp@105
  1102
{\out Level 1}
lcp@105
  1103
{\out app(a : b : c : Nil, d : e : Nil, a : ?zs1)}
lcp@105
  1104
{\out  1. app(b : c : Nil, d : e : Nil, ?zs1)}
lcp@105
  1105
\ttbreak
lcp@105
  1106
by (resolve_tac [appNil,appCons] 1);
lcp@105
  1107
{\out Level 2}
lcp@105
  1108
{\out app(a : b : c : Nil, d : e : Nil, a : b : ?zs2)}
lcp@105
  1109
{\out  1. app(c : Nil, d : e : Nil, ?zs2)}
lcp@105
  1110
\end{ttbox}
lcp@105
  1111
At this point, the first two elements of the result are~$a$ and~$b$.
lcp@105
  1112
\begin{ttbox}
lcp@105
  1113
by (resolve_tac [appNil,appCons] 1);
lcp@105
  1114
{\out Level 3}
lcp@105
  1115
{\out app(a : b : c : Nil, d : e : Nil, a : b : c : ?zs3)}
lcp@105
  1116
{\out  1. app(Nil, d : e : Nil, ?zs3)}
lcp@105
  1117
\ttbreak
lcp@105
  1118
by (resolve_tac [appNil,appCons] 1);
lcp@105
  1119
{\out Level 4}
lcp@105
  1120
{\out app(a : b : c : Nil, d : e : Nil, a : b : c : d : e : Nil)}
lcp@105
  1121
{\out No subgoals!}
lcp@105
  1122
\end{ttbox}
lcp@105
  1123
lcp@284
  1124
Prolog can run functions backwards.  Which list can be appended
lcp@105
  1125
with $[c,d]$ to produce $[a,b,c,d]$?
lcp@105
  1126
Using \ttindex{REPEAT}, we find the answer at once, $[a,b]$:
lcp@105
  1127
\begin{ttbox}
lcp@105
  1128
goal Prolog.thy "app(?x, c:d:Nil, a:b:c:d:Nil)";
lcp@105
  1129
{\out Level 0}
lcp@105
  1130
{\out app(?x, c : d : Nil, a : b : c : d : Nil)}
lcp@105
  1131
{\out  1. app(?x, c : d : Nil, a : b : c : d : Nil)}
lcp@105
  1132
\ttbreak
lcp@105
  1133
by (REPEAT (resolve_tac [appNil,appCons] 1));
lcp@105
  1134
{\out Level 1}
lcp@105
  1135
{\out app(a : b : Nil, c : d : Nil, a : b : c : d : Nil)}
lcp@105
  1136
{\out No subgoals!}
lcp@105
  1137
\end{ttbox}
lcp@105
  1138
lcp@105
  1139
lcp@310
  1140
\subsection{Backtracking}\index{backtracking!Prolog style}
lcp@296
  1141
Prolog backtracking can answer questions that have multiple solutions.
lcp@296
  1142
Which lists $x$ and $y$ can be appended to form the list $[a,b,c,d]$?  This
lcp@296
  1143
question has five solutions.  Using \ttindex{REPEAT} to apply the rules, we
lcp@296
  1144
quickly find the first solution, namely $x=[]$ and $y=[a,b,c,d]$:
lcp@105
  1145
\begin{ttbox}
lcp@105
  1146
goal Prolog.thy "app(?x, ?y, a:b:c:d:Nil)";
lcp@105
  1147
{\out Level 0}
lcp@105
  1148
{\out app(?x, ?y, a : b : c : d : Nil)}
lcp@105
  1149
{\out  1. app(?x, ?y, a : b : c : d : Nil)}
lcp@105
  1150
\ttbreak
lcp@105
  1151
by (REPEAT (resolve_tac [appNil,appCons] 1));
lcp@105
  1152
{\out Level 1}
lcp@105
  1153
{\out app(Nil, a : b : c : d : Nil, a : b : c : d : Nil)}
lcp@105
  1154
{\out No subgoals!}
lcp@105
  1155
\end{ttbox}
lcp@284
  1156
Isabelle can lazily generate all the possibilities.  The \ttindex{back}
lcp@284
  1157
command returns the tactic's next outcome, namely $x=[a]$ and $y=[b,c,d]$:
lcp@105
  1158
\begin{ttbox}
lcp@105
  1159
back();
lcp@105
  1160
{\out Level 1}
lcp@105
  1161
{\out app(a : Nil, b : c : d : Nil, a : b : c : d : Nil)}
lcp@105
  1162
{\out No subgoals!}
lcp@105
  1163
\end{ttbox}
lcp@105
  1164
The other solutions are generated similarly.
lcp@105
  1165
\begin{ttbox}
lcp@105
  1166
back();
lcp@105
  1167
{\out Level 1}
lcp@105
  1168
{\out app(a : b : Nil, c : d : Nil, a : b : c : d : Nil)}
lcp@105
  1169
{\out No subgoals!}
lcp@105
  1170
\ttbreak
lcp@105
  1171
back();
lcp@105
  1172
{\out Level 1}
lcp@105
  1173
{\out app(a : b : c : Nil, d : Nil, a : b : c : d : Nil)}
lcp@105
  1174
{\out No subgoals!}
lcp@105
  1175
\ttbreak
lcp@105
  1176
back();
lcp@105
  1177
{\out Level 1}
lcp@105
  1178
{\out app(a : b : c : d : Nil, Nil, a : b : c : d : Nil)}
lcp@105
  1179
{\out No subgoals!}
lcp@105
  1180
\end{ttbox}
lcp@105
  1181
lcp@105
  1182
lcp@105
  1183
\subsection{Depth-first search}
lcp@105
  1184
\index{search!depth-first}
lcp@105
  1185
Now let us try $rev$, reversing a list.
lcp@105
  1186
Bundle the rules together as the \ML{} identifier {\tt rules}.  Naive
lcp@105
  1187
reverse requires 120 inferences for this 14-element list, but the tactic
lcp@105
  1188
terminates in a few seconds.
lcp@105
  1189
\begin{ttbox}
lcp@105
  1190
goal Prolog.thy "rev(a:b:c:d:e:f:g:h:i:j:k:l:m:n:Nil, ?w)";
lcp@105
  1191
{\out Level 0}
lcp@105
  1192
{\out rev(a : b : c : d : e : f : g : h : i : j : k : l : m : n : Nil, ?w)}
lcp@284
  1193
{\out  1. rev(a : b : c : d : e : f : g : h : i : j : k : l : m : n : Nil,}
lcp@284
  1194
{\out         ?w)}
lcp@284
  1195
\ttbreak
lcp@105
  1196
val rules = [appNil,appCons,revNil,revCons];
lcp@105
  1197
\ttbreak
lcp@105
  1198
by (REPEAT (resolve_tac rules 1));
lcp@105
  1199
{\out Level 1}
lcp@105
  1200
{\out rev(a : b : c : d : e : f : g : h : i : j : k : l : m : n : Nil,}
lcp@105
  1201
{\out     n : m : l : k : j : i : h : g : f : e : d : c : b : a : Nil)}
lcp@105
  1202
{\out No subgoals!}
lcp@105
  1203
\end{ttbox}
lcp@105
  1204
We may execute $rev$ backwards.  This, too, should reverse a list.  What
lcp@105
  1205
is the reverse of $[a,b,c]$?
lcp@105
  1206
\begin{ttbox}
lcp@105
  1207
goal Prolog.thy "rev(?x, a:b:c:Nil)";
lcp@105
  1208
{\out Level 0}
lcp@105
  1209
{\out rev(?x, a : b : c : Nil)}
lcp@105
  1210
{\out  1. rev(?x, a : b : c : Nil)}
lcp@105
  1211
\ttbreak
lcp@105
  1212
by (REPEAT (resolve_tac rules 1));
lcp@105
  1213
{\out Level 1}
lcp@105
  1214
{\out rev(?x1 : Nil, a : b : c : Nil)}
lcp@105
  1215
{\out  1. app(Nil, ?x1 : Nil, a : b : c : Nil)}
lcp@105
  1216
\end{ttbox}
lcp@105
  1217
The tactic has failed to find a solution!  It reached a dead end at
lcp@331
  1218
subgoal~1: there is no~$\Var{x@1}$ such that [] appended with~$[\Var{x@1}]$
lcp@105
  1219
equals~$[a,b,c]$.  Backtracking explores other outcomes.
lcp@105
  1220
\begin{ttbox}
lcp@105
  1221
back();
lcp@105
  1222
{\out Level 1}
lcp@105
  1223
{\out rev(?x1 : a : Nil, a : b : c : Nil)}
lcp@105
  1224
{\out  1. app(Nil, ?x1 : Nil, b : c : Nil)}
lcp@105
  1225
\end{ttbox}
lcp@105
  1226
This too is a dead end, but the next outcome is successful.
lcp@105
  1227
\begin{ttbox}
lcp@105
  1228
back();
lcp@105
  1229
{\out Level 1}
lcp@105
  1230
{\out rev(c : b : a : Nil, a : b : c : Nil)}
lcp@105
  1231
{\out No subgoals!}
lcp@105
  1232
\end{ttbox}
lcp@310
  1233
\ttindex{REPEAT} goes wrong because it is only a repetition tactical, not a
lcp@310
  1234
search tactical.  {\tt REPEAT} stops when it cannot continue, regardless of
lcp@310
  1235
which state is reached.  The tactical \ttindex{DEPTH_FIRST} searches for a
lcp@310
  1236
satisfactory state, as specified by an \ML{} predicate.  Below,
lcp@105
  1237
\ttindex{has_fewer_prems} specifies that the proof state should have no
lcp@310
  1238
subgoals.
lcp@105
  1239
\begin{ttbox}
lcp@105
  1240
val prolog_tac = DEPTH_FIRST (has_fewer_prems 1) 
lcp@105
  1241
                             (resolve_tac rules 1);
lcp@105
  1242
\end{ttbox}
lcp@284
  1243
Since Prolog uses depth-first search, this tactic is a (slow!) 
lcp@296
  1244
Prolog interpreter.  We return to the start of the proof using
lcp@296
  1245
\ttindex{choplev}, and apply {\tt prolog_tac}:
lcp@105
  1246
\begin{ttbox}
lcp@105
  1247
choplev 0;
lcp@105
  1248
{\out Level 0}
lcp@105
  1249
{\out rev(?x, a : b : c : Nil)}
lcp@105
  1250
{\out  1. rev(?x, a : b : c : Nil)}
lcp@105
  1251
\ttbreak
lcp@105
  1252
by (DEPTH_FIRST (has_fewer_prems 1) (resolve_tac rules 1));
lcp@105
  1253
{\out Level 1}
lcp@105
  1254
{\out rev(c : b : a : Nil, a : b : c : Nil)}
lcp@105
  1255
{\out No subgoals!}
lcp@105
  1256
\end{ttbox}
lcp@105
  1257
Let us try {\tt prolog_tac} on one more example, containing four unknowns:
lcp@105
  1258
\begin{ttbox}
lcp@105
  1259
goal Prolog.thy "rev(a:?x:c:?y:Nil, d:?z:b:?u)";
lcp@105
  1260
{\out Level 0}
lcp@105
  1261
{\out rev(a : ?x : c : ?y : Nil, d : ?z : b : ?u)}
lcp@105
  1262
{\out  1. rev(a : ?x : c : ?y : Nil, d : ?z : b : ?u)}
lcp@105
  1263
\ttbreak
lcp@105
  1264
by prolog_tac;
lcp@105
  1265
{\out Level 1}
lcp@105
  1266
{\out rev(a : b : c : d : Nil, d : c : b : a : Nil)}
lcp@105
  1267
{\out No subgoals!}
lcp@105
  1268
\end{ttbox}
lcp@284
  1269
Although Isabelle is much slower than a Prolog system, Isabelle
lcp@156
  1270
tactics can exploit logic programming techniques.  
lcp@156
  1271