2 \part{Using Isabelle from the ML Top-Level}\label{chap:getting}
4 Most Isabelle users write proof scripts using the Isar language, as described in the \emph{Tutorial}, and debug them through the Proof General user interface~\cite{proofgeneral}. Isabelle's original user interface --- based on the \ML{} top-level --- is still available, however.
5 Proofs are conducted by
6 applying certain \ML{} functions, which update a stored proof state.
7 All syntax can be expressed using plain {\sc ascii}
8 characters, but Isabelle can support
9 alternative syntaxes, for example using mathematical symbols from a
10 special screen font. The meta-logic and main object-logics already
11 provide such fancy output as an option.
13 Object-logics are built upon Pure Isabelle, which implements the
14 meta-logic and provides certain fundamental data structures: types,
15 terms, signatures, theorems and theories, tactics and tacticals.
16 These data structures have the corresponding \ML{} types \texttt{typ},
17 \texttt{term}, \texttt{Sign.sg}, \texttt{thm}, \texttt{theory} and \texttt{tactic};
18 tacticals have function types such as \texttt{tactic->tactic}. Isabelle
19 users can operate on these data structures by writing \ML{} programs.
22 \section{Forward proof}\label{sec:forward} \index{forward proof|(}
23 This section describes the concrete syntax for types, terms and theorems,
24 and demonstrates forward proof. The examples are set in first-order logic.
25 The command to start Isabelle running first-order logic is
29 Note that just typing \texttt{isabelle} usually brings up higher-order logic
33 \subsection{Lexical matters}
34 \index{identifiers}\index{reserved words}
35 An {\bf identifier} is a string of letters, digits, underscores~(\verb|_|)
36 and single quotes~({\tt'}), beginning with a letter. Single quotes are
37 regarded as primes; for instance \texttt{x'} is read as~$x'$. Identifiers are
38 separated by white space and special characters. {\bf Reserved words} are
39 identifiers that appear in Isabelle syntax definitions.
41 An Isabelle theory can declare symbols composed of special characters, such
42 as {\tt=}, {\tt==}, {\tt=>} and {\tt==>}. (The latter three are part of
43 the syntax of the meta-logic.) Such symbols may be run together; thus if
44 \verb|}| and \verb|{| are used for set brackets then \verb|{{a},{a,b}}| is
45 valid notation for a set of sets --- but only if \verb|}}| and \verb|{{|
46 have not been declared as symbols! The parser resolves any ambiguity by
47 taking the longest possible symbol that has been declared. Thus the string
48 {\tt==>} is read as a single symbol. But \hbox{\tt= =>} is read as two
51 Identifiers that are not reserved words may serve as free variables or
52 constants. A {\bf type identifier} consists of an identifier prefixed by a
53 prime, for example {\tt'a} and \hbox{\tt'hello}. Type identifiers stand
54 for (free) type variables, which remain fixed during a proof.
55 \index{type identifiers}
57 An {\bf unknown}\index{unknowns} (or type unknown) consists of a question
58 mark, an identifier (or type identifier), and a subscript. The subscript,
59 a non-negative integer,
60 allows the renaming of unknowns prior to unification.%
61 \footnote{The subscript may appear after the identifier, separated by a
62 dot; this prevents ambiguity when the identifier ends with a digit. Thus
63 {\tt?z6.0} has identifier {\tt"z6"} and subscript~0, while {\tt?a0.5}
64 has identifier {\tt"a0"} and subscript~5. If the identifier does not
65 end with a digit, then no dot appears and a subscript of~0 is omitted;
66 for example, {\tt?hello} has identifier {\tt"hello"} and subscript
67 zero, while {\tt?z6} has identifier {\tt"z"} and subscript~6. The same
68 conventions apply to type unknowns. The question mark is {\it not\/}
69 part of the identifier!}
72 \subsection{Syntax of types and terms}
73 \index{classes!built-in|bold}\index{syntax!of types and terms}
75 Classes are denoted by identifiers; the built-in class \cldx{logic}
76 contains the `logical' types. Sorts are lists of classes enclosed in
77 braces~\} and \{; singleton sorts may be abbreviated by dropping the braces.
79 \index{types!syntax of|bold}\index{sort constraints} Types are written
80 with a syntax like \ML's. The built-in type \tydx{prop} is the type
81 of propositions. Type variables can be constrained to particular
82 classes or sorts, for example \texttt{'a::term} and \texttt{?'b::\ttlbrace
85 \index{*:: symbol}\index{*=> symbol}
86 \index{{}@{\tt\ttlbrace} symbol}\index{{}@{\tt\ttrbrace} symbol}
87 \index{*[ symbol}\index{*] symbol}
89 \multicolumn{2}{c}{\hbox{ASCII Notation for Types}} \\ \hline
90 \alpha "::" C & \hbox{class constraint} \\
91 \alpha "::" "\ttlbrace" C@1 "," \ldots "," C@n "\ttrbrace" &
92 \hbox{sort constraint} \\
93 \sigma " => " \tau & \hbox{function type } \sigma\To\tau \\
94 "[" \sigma@1 "," \ldots "," \sigma@n "] => " \tau
95 & \hbox{$n$-argument function type} \\
96 "(" \tau@1"," \ldots "," \tau@n ")" tycon & \hbox{type construction}
99 Terms are those of the typed $\lambda$-calculus.
100 \index{terms!syntax of|bold}\index{type constraints}
102 \index{%@{\tt\%} symbol}\index{lambda abs@$\lambda$-abstractions}
105 \multicolumn{2}{c}{\hbox{ASCII Notation for Terms}} \\ \hline
106 t "::" \sigma & \hbox{type constraint} \\
107 "\%" x "." t & \hbox{abstraction } \lambda x.t \\
108 "\%" x@1\ldots x@n "." t & \hbox{abstraction over several arguments} \\
109 t "(" u@1"," \ldots "," u@n ")" &
110 \hbox{application to several arguments (FOL and ZF)} \\
111 t\; u@1 \ldots\; u@n & \hbox{application to several arguments (HOL)}
114 Note that HOL uses its traditional ``higher-order'' syntax for application,
115 which differs from that used in FOL.
117 The theorems and rules of an object-logic are represented by theorems in
118 the meta-logic, which are expressed using meta-formulae. Since the
119 meta-logic is higher-order, meta-formulae~$\phi$, $\psi$, $\theta$,~\ldots{}
120 are just terms of type~\texttt{prop}.
121 \index{meta-implication}
122 \index{meta-quantifiers}\index{meta-equality}
123 \index{*"!"! symbol}\index{*"["| symbol}\index{*"|"] symbol}
124 \index{*== symbol}\index{*=?= symbol}\index{*==> symbol}
126 \begin{array}{l@{\quad}l@{\quad}l}
127 \multicolumn{3}{c}{\hbox{ASCII Notation for Meta-Formulae}} \\ \hline
128 a " == " b & a\equiv b & \hbox{meta-equality} \\
129 a " =?= " b & a\qeq b & \hbox{flex-flex constraint} \\
130 \phi " ==> " \psi & \phi\Imp \psi & \hbox{meta-implication} \\
131 "[|" \phi@1 ";" \ldots ";" \phi@n "|] ==> " \psi &
132 \List{\phi@1;\ldots;\phi@n} \Imp \psi & \hbox{nested implication} \\
133 "!!" x "." \phi & \Forall x.\phi & \hbox{meta-quantification} \\
134 "!!" x@1\ldots x@n "." \phi &
135 \Forall x@1. \ldots x@n.\phi & \hbox{nested quantification}
138 Flex-flex constraints are meta-equalities arising from unification; they
139 require special treatment. See~\S\ref{flexflex}.
140 \index{flex-flex constraints}
142 \index{*Trueprop constant}
143 Most logics define the implicit coercion $Trueprop$ from object-formulae to
144 propositions. This could cause an ambiguity: in $P\Imp Q$, do the
145 variables $P$ and $Q$ stand for meta-formulae or object-formulae? If the
146 latter, $P\Imp Q$ really abbreviates $Trueprop(P)\Imp Trueprop(Q)$. To
147 prevent such ambiguities, Isabelle's syntax does not allow a meta-formula
148 to consist of a variable. Variables of type~\tydx{prop} are seldom
149 useful, but you can make a variable stand for a meta-formula by prefixing
150 it with the symbol \texttt{PROP}:\index{*PROP symbol}
152 PROP ?psi ==> PROP ?theta
155 Symbols of object-logics are typically rendered into {\sc ascii} as
157 \[ \begin{tabular}{l@{\quad}l@{\quad}l}
158 \tt True & $\top$ & true \\
159 \tt False & $\bot$ & false \\
160 \tt $P$ \& $Q$ & $P\conj Q$ & conjunction \\
161 \tt $P$ | $Q$ & $P\disj Q$ & disjunction \\
162 \verb'~' $P$ & $\neg P$ & negation \\
163 \tt $P$ --> $Q$ & $P\imp Q$ & implication \\
164 \tt $P$ <-> $Q$ & $P\bimp Q$ & bi-implication \\
165 \tt ALL $x\,y\,z$ .\ $P$ & $\forall x\,y\,z.P$ & for all \\
166 \tt EX $x\,y\,z$ .\ $P$ & $\exists x\,y\,z.P$ & there exists
169 To illustrate the notation, consider two axioms for first-order logic:
170 $$ \List{P; Q} \Imp P\conj Q \eqno(\conj I) $$
171 $$ \List{\exists x. P(x); \Forall x. P(x)\imp Q} \Imp Q \eqno(\exists E) $$
172 $({\conj}I)$ translates into {\sc ascii} characters as
174 [| ?P; ?Q |] ==> ?P & ?Q
176 The schematic variables let unification instantiate the rule. To avoid
177 cluttering logic definitions with question marks, Isabelle converts any
178 free variables in a rule to schematic variables; we normally declare
183 This variables convention agrees with the treatment of variables in goals.
184 Free variables in a goal remain fixed throughout the proof. After the
185 proof is finished, Isabelle converts them to scheme variables in the
186 resulting theorem. Scheme variables in a goal may be replaced by terms
187 during the proof, supporting answer extraction, program synthesis, and so
190 For a final example, the rule $(\exists E)$ is rendered in {\sc ascii} as
192 [| EX x. P(x); !!x. P(x) ==> Q |] ==> Q
196 \subsection{Basic operations on theorems}
197 \index{theorems!basic operations on|bold}
199 Meta-level theorems have the \ML{} type \mltydx{thm}. They represent the
200 theorems and inference rules of object-logics. Isabelle's meta-logic is
201 implemented using the {\sc lcf} approach: each meta-level inference rule is
202 represented by a function from theorems to theorems. Object-level rules
205 The main theorem printing commands are \texttt{prth}, \texttt{prths} and~{\tt
206 prthq}. Of the other operations on theorems, most useful are \texttt{RS}
207 and \texttt{RSN}, which perform resolution.
209 \index{theorems!printing of}
210 \begin{ttdescription}
211 \item[\ttindex{prth} {\it thm};]
212 pretty-prints {\it thm\/} at the terminal.
214 \item[\ttindex{prths} {\it thms};]
215 pretty-prints {\it thms}, a list of theorems.
217 \item[\ttindex{prthq} {\it thmq};]
218 pretty-prints {\it thmq}, a sequence of theorems; this is useful for
219 inspecting the output of a tactic.
221 \item[$thm1$ RS $thm2$] \index{*RS}
222 resolves the conclusion of $thm1$ with the first premise of~$thm2$.
224 \item[$thm1$ RSN $(i,thm2)$] \index{*RSN}
225 resolves the conclusion of $thm1$ with the $i$th premise of~$thm2$.
227 \item[\ttindex{standard} $thm$]
228 puts $thm$ into a standard format. It also renames schematic variables
229 to have subscript zero, improving readability and reducing subscript
232 The rules of a theory are normally bound to \ML\ identifiers. Suppose we are
233 running an Isabelle session containing theory~FOL, natural deduction
234 first-order logic.\footnote{For a listing of the FOL rules and their \ML{}
236 \iflabelundefined{fol-rules}{{\em Isabelle's Object-Logics}}%
237 {page~\pageref{fol-rules}}.}
238 Let us try an example given in~\S\ref{joining}. We
239 first print \tdx{mp}, which is the rule~$({\imp}E)$, then resolve it with
243 {\out [| ?P --> ?Q; ?P |] ==> ?Q}
244 {\out val it = "[| ?P --> ?Q; ?P |] ==> ?Q" : thm}
246 {\out [| ?P1 --> ?P --> ?Q; ?P1; ?P |] ==> ?Q}
247 {\out val it = "[| ?P1 --> ?P --> ?Q; ?P1; ?P |] ==> ?Q" : thm}
249 User input appears in {\footnotesize\tt typewriter characters}, and output
250 appears in{\out slanted typewriter characters}. \ML's response {\out val
251 }~\ldots{} is compiler-dependent and will sometimes be suppressed. This
252 session illustrates two formats for the display of theorems. Isabelle's
253 top-level displays theorems as \ML{} values, enclosed in quotes. Printing
254 commands like \texttt{prth} omit the quotes and the surrounding \texttt{val
255 \ldots :\ thm}. Ignoring their side-effects, the printing commands are
258 To contrast \texttt{RS} with \texttt{RSN}, we resolve
259 \tdx{conjunct1}, which stands for~$(\conj E1)$, with~\tdx{mp}.
262 {\out val it = "[| (?P --> ?Q) & ?Q1; ?P |] ==> ?Q" : thm}
263 conjunct1 RSN (2,mp);
264 {\out val it = "[| ?P --> ?Q; ?P & ?Q1 |] ==> ?Q" : thm}
266 These correspond to the following proofs:
267 \[ \infer[({\imp}E)]{Q}{\infer[({\conj}E1)]{P\imp Q}{(P\imp Q)\conj Q@1} & P}
269 \infer[({\imp}E)]{Q}{P\imp Q & \infer[({\conj}E1)]{P}{P\conj Q@1}}
272 Rules can be derived by pasting other rules together. Let us join
273 \tdx{spec}, which stands for~$(\forall E)$, with \texttt{mp} and {\tt
274 conjunct1}. In \ML{}, the identifier~\texttt{it} denotes the value just
278 {\out val it = "ALL x. ?P(x) ==> ?P(?x)" : thm}
280 {\out val it = "[| ALL x. ?P3(x) --> ?Q2(x); ?P3(?x1) |] ==>}
281 {\out ?Q2(?x1)" : thm}
283 {\out val it = "[| ALL x. ?P4(x) --> ?P6(x) & ?Q5(x); ?P4(?x2) |] ==>}
284 {\out ?P6(?x2)" : thm}
286 {\out val it = "[| ALL x. ?P(x) --> ?Pa(x) & ?Q(x); ?P(?x) |] ==>}
287 {\out ?Pa(?x)" : thm}
289 By resolving $(\forall E)$ with (${\imp}E)$ and (${\conj}E1)$, we have
290 derived a destruction rule for formulae of the form $\forall x.
291 P(x)\imp(Q(x)\conj R(x))$. Used with destruct-resolution, such specialized
292 rules provide a way of referring to particular assumptions.
293 \index{assumptions!use of}
295 \subsection{*Flex-flex constraints} \label{flexflex}
296 \index{flex-flex constraints|bold}\index{unknowns!function}
297 In higher-order unification, {\bf flex-flex} equations are those where both
298 sides begin with a function unknown, such as $\Var{f}(0)\qeq\Var{g}(0)$.
299 They admit a trivial unifier, here $\Var{f}\equiv \lambda x.\Var{a}$ and
300 $\Var{g}\equiv \lambda y.\Var{a}$, where $\Var{a}$ is a new unknown. They
301 admit many other unifiers, such as $\Var{f} \equiv \lambda x.\Var{g}(0)$
302 and $\{\Var{f} \equiv \lambda x.x,\, \Var{g} \equiv \lambda x.0\}$. Huet's
303 procedure does not enumerate the unifiers; instead, it retains flex-flex
304 equations as constraints on future unifications. Flex-flex constraints
305 occasionally become attached to a proof state; more frequently, they appear
306 during use of \texttt{RS} and~\texttt{RSN}:
309 {\out val it = "?a = ?a" : thm}
311 {\out val it = "?P(?x) ==> EX x. ?P(x)" : thm}
313 {\out val it = "EX x. ?a3(x) = ?a2(x)" [.] : thm}
316 The mysterious symbol \texttt{[.]} indicates that the result is subject to
317 a meta-level hypothesis. We can make all such hypotheses visible by setting the
318 \ttindexbold{show_hyps} flag:
321 {\out val it = true : bool}
323 {\out val it = "EX x. ?a3(x) = ?a2(x)" ["?a3(?x) =?= ?a2(?x)"] : thm}
327 Renaming variables, this is $\exists x.\Var{f}(x)=\Var{g}(x)$ with
328 the constraint ${\Var{f}(\Var{u})\qeq\Var{g}(\Var{u})}$. Instances
329 satisfying the constraint include $\exists x.\Var{f}(x)=\Var{f}(x)$ and
330 $\exists x.x=\Var{u}$. Calling \ttindex{flexflex_rule} removes all
331 constraints by applying the trivial unifier:\index{*prthq}
333 prthq (flexflex_rule it);
334 {\out EX x. ?a4 = ?a4}
336 Isabelle simplifies flex-flex equations to eliminate redundant bound
337 variables. In $\lambda x\,y.\Var{f}(k(y),x) \qeq \lambda x\,y.\Var{g}(y)$,
338 there is no bound occurrence of~$x$ on the right side; thus, there will be
339 none on the left in a common instance of these terms. Choosing a new
340 variable~$\Var{h}$, Isabelle assigns $\Var{f}\equiv \lambda u\,v.?h(u)$,
341 simplifying the left side to $\lambda x\,y.\Var{h}(k(y))$. Dropping $x$
342 from the equation leaves $\lambda y.\Var{h}(k(y)) \qeq \lambda
343 y.\Var{g}(y)$. By $\eta$-conversion, this simplifies to the assignment
344 $\Var{g}\equiv\lambda y.?h(k(y))$.
347 \ttindex{RS} and \ttindex{RSN} fail (by raising exception \texttt{THM}) unless
348 the resolution delivers {\bf exactly one} resolvent. For multiple results,
349 use \ttindex{RL} and \ttindex{RLN}, which operate on theorem lists. The
350 following example uses \ttindex{read_instantiate} to create an instance
351 of \tdx{refl} containing no schematic variables.
353 val reflk = read_instantiate [("a","k")] refl;
354 {\out val reflk = "k = k" : thm}
358 A flex-flex constraint is no longer possible; resolution does not find a
362 {\out uncaught exception}
363 {\out THM ("RSN: multiple unifiers", 1,}
364 {\out ["k = k", "?P(?x) ==> EX x. ?P(x)"])}
366 Using \ttindex{RL} this time, we discover that there are four unifiers, and
370 {\out val it = ["EX x. x = x", "EX x. k = x",}
371 {\out "EX x. x = k", "EX x. k = k"] : thm list}
375 \index{forward proof|)}
377 \section{Backward proof}
378 Although \texttt{RS} and \texttt{RSN} are fine for simple forward reasoning,
379 large proofs require tactics. Isabelle provides a suite of commands for
380 conducting a backward proof using tactics.
382 \subsection{The basic tactics}
383 The tactics \texttt{assume_tac}, {\tt
384 resolve_tac}, \texttt{eresolve_tac}, and \texttt{dresolve_tac} suffice for most
385 single-step proofs. Although \texttt{eresolve_tac} and \texttt{dresolve_tac} are
386 not strictly necessary, they simplify proofs involving elimination and
387 destruction rules. All the tactics act on a subgoal designated by a
388 positive integer~$i$, failing if~$i$ is out of range. The resolution
389 tactics try their list of theorems in left-to-right order.
391 \begin{ttdescription}
392 \item[\ttindex{assume_tac} {\it i}] \index{tactics!assumption}
393 is the tactic that attempts to solve subgoal~$i$ by assumption. Proof by
394 assumption is not a trivial step; it can falsify other subgoals by
395 instantiating shared variables. There may be several ways of solving the
396 subgoal by assumption.
398 \item[\ttindex{resolve_tac} {\it thms} {\it i}]\index{tactics!resolution}
399 is the basic resolution tactic, used for most proof steps. The $thms$
400 represent object-rules, which are resolved against subgoal~$i$ of the
401 proof state. For each rule, resolution forms next states by unifying the
402 conclusion with the subgoal and inserting instantiated premises in its
403 place. A rule can admit many higher-order unifiers. The tactic fails if
404 none of the rules generates next states.
406 \item[\ttindex{eresolve_tac} {\it thms} {\it i}] \index{elim-resolution}
407 performs elim-resolution. Like \texttt{resolve_tac~{\it thms}~{\it i\/}}
408 followed by \texttt{assume_tac~{\it i}}, it applies a rule then solves its
409 first premise by assumption. But \texttt{eresolve_tac} additionally deletes
410 that assumption from any subgoals arising from the resolution.
412 \item[\ttindex{dresolve_tac} {\it thms} {\it i}]
413 \index{forward proof}\index{destruct-resolution}
414 performs destruct-resolution with the~$thms$, as described
415 in~\S\ref{destruct}. It is useful for forward reasoning from the
419 \subsection{Commands for backward proof}
420 \index{proofs!commands for}
421 Tactics are normally applied using the subgoal module, which maintains a
422 proof state and manages the proof construction. It allows interactive
423 backtracking through the proof space, going away to prove lemmas, etc.; of
424 its many commands, most important are the following:
425 \begin{ttdescription}
426 \item[\ttindex{Goal} {\it formula}; ]
427 begins a new proof, where the {\it formula\/} is written as an \ML\ string.
429 \item[\ttindex{by} {\it tactic}; ]
430 applies the {\it tactic\/} to the current proof
431 state, raising an exception if the tactic fails.
433 \item[\ttindex{undo}(); ]
434 reverts to the previous proof state. Undo can be repeated but cannot be
435 undone. Do not omit the parentheses; typing {\tt\ \ undo;\ \ } merely
436 causes \ML\ to echo the value of that function.
438 \item[\ttindex{result}();]
439 returns the theorem just proved, in a standard format. It fails if
440 unproved subgoals are left, etc.
442 \item[\ttindex{qed} {\it name};] is the usual way of ending a proof.
443 It gets the theorem using \texttt{result}, stores it in Isabelle's
444 theorem database and binds it to an \ML{} identifier.
447 The commands and tactics given above are cumbersome for interactive use.
448 Although our examples will use the full commands, you may prefer Isabelle's
451 \index{*br} \index{*be} \index{*bd} \index{*ba}
452 \begin{tabular}{l@{\qquad\rm abbreviates\qquad}l}
453 ba {\it i}; & by (assume_tac {\it i}); \\
455 br {\it thm} {\it i}; & by (resolve_tac [{\it thm}] {\it i}); \\
457 be {\it thm} {\it i}; & by (eresolve_tac [{\it thm}] {\it i}); \\
459 bd {\it thm} {\it i}; & by (dresolve_tac [{\it thm}] {\it i});
463 \subsection{A trivial example in propositional logic}
464 \index{examples!propositional}
466 Directory \texttt{FOL} of the Isabelle distribution defines the theory of
467 first-order logic. Let us try the example from \S\ref{prop-proof},
468 entering the goal $P\disj P\imp P$ in that theory.\footnote{To run these
469 examples, see the file \texttt{FOL/ex/intro.ML}.}
474 {\out 1. P | P --> P}
475 \end{ttbox}\index{level of a proof}
476 Isabelle responds by printing the initial proof state, which has $P\disj
477 P\imp P$ as the main goal and the only subgoal. The {\bf level} of the
478 state is the number of \texttt{by} commands that have been applied to reach
479 it. We now use \ttindex{resolve_tac} to apply the rule \tdx{impI},
480 or~$({\imp}I)$, to subgoal~1:
482 by (resolve_tac [impI] 1);
485 {\out 1. P | P ==> P}
487 In the new proof state, subgoal~1 is $P$ under the assumption $P\disj P$.
488 (The meta-implication {\tt==>} indicates assumptions.) We apply
489 \tdx{disjE}, or~(${\disj}E)$, to that subgoal:
491 by (resolve_tac [disjE] 1);
494 {\out 1. P | P ==> ?P1 | ?Q1}
495 {\out 2. [| P | P; ?P1 |] ==> P}
496 {\out 3. [| P | P; ?Q1 |] ==> P}
498 At Level~2 there are three subgoals, each provable by assumption. We
499 deviate from~\S\ref{prop-proof} by tackling subgoal~3 first, using
500 \ttindex{assume_tac}. This affects subgoal~1, updating {\tt?Q1} to~{\tt
506 {\out 1. P | P ==> ?P1 | P}
507 {\out 2. [| P | P; ?P1 |] ==> P}
509 Next we tackle subgoal~2, instantiating {\tt?P1} to~\texttt{P} in subgoal~1.
514 {\out 1. P | P ==> P | P}
516 Lastly we prove the remaining subgoal by assumption:
523 Isabelle tells us that there are no longer any subgoals: the proof is
524 complete. Calling \texttt{qed} stores the theorem.
527 {\out val mythm = "?P | ?P --> ?P" : thm}
529 Isabelle has replaced the free variable~\texttt{P} by the scheme
530 variable~{\tt?P}\@. Free variables in the proof state remain fixed
531 throughout the proof. Isabelle finally converts them to scheme variables
532 so that the resulting theorem can be instantiated with any formula.
534 As an exercise, try doing the proof as in \S\ref{prop-proof}, observing how
535 instantiations affect the proof state.
538 \subsection{Part of a distributive law}
539 \index{examples!propositional}
540 To demonstrate the tactics \ttindex{eresolve_tac}, \ttindex{dresolve_tac}
541 and the tactical \texttt{REPEAT}, let us prove part of the distributive
543 \[ (P\conj Q)\disj R \,\bimp\, (P\disj R)\conj (Q\disj R). \]
544 We begin by stating the goal to Isabelle and applying~$({\imp}I)$ to it:
546 Goal "(P & Q) | R --> (P | R)";
548 {\out P & Q | R --> P | R}
549 {\out 1. P & Q | R --> P | R}
551 by (resolve_tac [impI] 1);
553 {\out P & Q | R --> P | R}
554 {\out 1. P & Q | R ==> P | R}
556 Previously we applied~(${\disj}E)$ using \texttt{resolve_tac}, but
557 \ttindex{eresolve_tac} deletes the assumption after use. The resulting proof
560 by (eresolve_tac [disjE] 1);
562 {\out P & Q | R --> P | R}
563 {\out 1. P & Q ==> P | R}
564 {\out 2. R ==> P | R}
566 Using \ttindex{dresolve_tac}, we can apply~(${\conj}E1)$ to subgoal~1,
567 replacing the assumption $P\conj Q$ by~$P$. Normally we should apply the
568 rule~(${\conj}E)$, given in~\S\ref{destruct}. That is an elimination rule
569 and requires \texttt{eresolve_tac}; it would replace $P\conj Q$ by the two
570 assumptions~$P$ and~$Q$. Because the present example does not need~$Q$, we
571 may try out \texttt{dresolve_tac}.
573 by (dresolve_tac [conjunct1] 1);
575 {\out P & Q | R --> P | R}
576 {\out 1. P ==> P | R}
577 {\out 2. R ==> P | R}
579 The next two steps apply~(${\disj}I1$) and~(${\disj}I2$) in an obvious manner.
581 by (resolve_tac [disjI1] 1);
583 {\out P & Q | R --> P | R}
585 {\out 2. R ==> P | R}
587 by (resolve_tac [disjI2] 2);
589 {\out P & Q | R --> P | R}
593 Two calls of \texttt{assume_tac} can finish the proof. The
594 tactical~\ttindex{REPEAT} here expresses a tactic that calls \texttt{assume_tac~1}
595 as many times as possible. We can restrict attention to subgoal~1 because
596 the other subgoals move up after subgoal~1 disappears.
598 by (REPEAT (assume_tac 1));
600 {\out P & Q | R --> P | R}
605 \section{Quantifier reasoning}
606 \index{quantifiers}\index{parameters}\index{unknowns}\index{unknowns!function}
607 This section illustrates how Isabelle enforces quantifier provisos.
608 Suppose that $x$, $y$ and~$z$ are parameters of a subgoal. Quantifier
609 rules create terms such as~$\Var{f}(x,z)$, where~$\Var{f}$ is a function
610 unknown. Instantiating $\Var{f}$ to $\lambda x\,z.t$ has the effect of
611 replacing~$\Var{f}(x,z)$ by~$t$, where the term~$t$ may contain free
612 occurrences of~$x$ and~$z$. On the other hand, no instantiation
613 of~$\Var{f}$ can replace~$\Var{f}(x,z)$ by a term containing free
614 occurrences of~$y$, since parameters are bound variables.
616 \subsection{Two quantifier proofs: a success and a failure}
617 \index{examples!with quantifiers}
618 Let us contrast a proof of the theorem $\forall x.\exists y.x=y$ with an
619 attempted proof of the non-theorem $\exists y.\forall x.x=y$. The former
620 proof succeeds, and the latter fails, because of the scope of quantified
621 variables~\cite{paulson-found}. Unification helps even in these trivial
622 proofs. In $\forall x.\exists y.x=y$ the $y$ that `exists' is simply $x$,
623 but we need never say so. This choice is forced by the reflexive law for
624 equality, and happens automatically.
626 \paragraph{The successful proof.}
627 The proof of $\forall x.\exists y.x=y$ demonstrates the introduction rules
628 $(\forall I)$ and~$(\exists I)$. We state the goal and apply $(\forall I)$:
630 Goal "ALL x. EX y. x=y";
632 {\out ALL x. EX y. x = y}
633 {\out 1. ALL x. EX y. x = y}
635 by (resolve_tac [allI] 1);
637 {\out ALL x. EX y. x = y}
638 {\out 1. !!x. EX y. x = y}
640 The variable~\texttt{x} is no longer universally quantified, but is a
641 parameter in the subgoal; thus, it is universally quantified at the
642 meta-level. The subgoal must be proved for all possible values of~\texttt{x}.
644 To remove the existential quantifier, we apply the rule $(\exists I)$:
646 by (resolve_tac [exI] 1);
648 {\out ALL x. EX y. x = y}
649 {\out 1. !!x. x = ?y1(x)}
651 The bound variable \texttt{y} has become {\tt?y1(x)}. This term consists of
652 the function unknown~{\tt?y1} applied to the parameter~\texttt{x}.
653 Instances of {\tt?y1(x)} may or may not contain~\texttt{x}. We resolve the
654 subgoal with the reflexivity axiom.
656 by (resolve_tac [refl] 1);
658 {\out ALL x. EX y. x = y}
661 Let us consider what has happened in detail. The reflexivity axiom is
662 lifted over~$x$ to become $\Forall x.\Var{f}(x)=\Var{f}(x)$, which is
663 unified with $\Forall x.x=\Var{y@1}(x)$. The function unknowns $\Var{f}$
664 and~$\Var{y@1}$ are both instantiated to the identity function, and
665 $x=\Var{y@1}(x)$ collapses to~$x=x$ by $\beta$-reduction.
667 \paragraph{The unsuccessful proof.}
668 We state the goal $\exists y.\forall x.x=y$, which is not a theorem, and
671 Goal "EX y. ALL x. x=y";
673 {\out EX y. ALL x. x = y}
674 {\out 1. EX y. ALL x. x = y}
676 by (resolve_tac [exI] 1);
678 {\out EX y. ALL x. x = y}
679 {\out 1. ALL x. x = ?y}
681 The unknown \texttt{?y} may be replaced by any term, but this can never
682 introduce another bound occurrence of~\texttt{x}. We now apply~$(\forall I)$:
684 by (resolve_tac [allI] 1);
686 {\out EX y. ALL x. x = y}
687 {\out 1. !!x. x = ?y}
689 Compare our position with the previous Level~2. Instead of {\tt?y1(x)} we
690 have~{\tt?y}, whose instances may not contain the bound variable~\texttt{x}.
691 The reflexivity axiom does not unify with subgoal~1.
693 by (resolve_tac [refl] 1);
694 {\out by: tactic failed}
696 There can be no proof of $\exists y.\forall x.x=y$ by the soundness of
697 first-order logic. I have elsewhere proved the faithfulness of Isabelle's
698 encoding of first-order logic~\cite{paulson-found}; there could, of course, be
699 faults in the implementation.
702 \subsection{Nested quantifiers}
703 \index{examples!with quantifiers}
704 Multiple quantifiers create complex terms. Proving
705 \[ (\forall x\,y.P(x,y)) \imp (\forall z\,w.P(w,z)) \]
706 will demonstrate how parameters and unknowns develop. If they appear in
707 the wrong order, the proof will fail.
709 This section concludes with a demonstration of \texttt{REPEAT}
712 Goal "(ALL x y.P(x,y)) --> (ALL z w.P(w,z))";
714 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
715 {\out 1. (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
717 by (resolve_tac [impI] 1);
719 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
720 {\out 1. ALL x y. P(x,y) ==> ALL z w. P(w,z)}
723 \paragraph{The wrong approach.}
724 Using \texttt{dresolve_tac}, we apply the rule $(\forall E)$, bound to the
725 \ML\ identifier \tdx{spec}. Then we apply $(\forall I)$.
727 by (dresolve_tac [spec] 1);
729 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
730 {\out 1. ALL y. P(?x1,y) ==> ALL z w. P(w,z)}
732 by (resolve_tac [allI] 1);
734 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
735 {\out 1. !!z. ALL y. P(?x1,y) ==> ALL w. P(w,z)}
737 The unknown \texttt{?x1} and the parameter \texttt{z} have appeared. We again
738 apply $(\forall E)$ and~$(\forall I)$.
740 by (dresolve_tac [spec] 1);
742 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
743 {\out 1. !!z. P(?x1,?y3(z)) ==> ALL w. P(w,z)}
745 by (resolve_tac [allI] 1);
747 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
748 {\out 1. !!z w. P(?x1,?y3(z)) ==> P(w,z)}
750 The unknown \texttt{?y3} and the parameter \texttt{w} have appeared. Each
751 unknown is applied to the parameters existing at the time of its creation;
752 instances of~\texttt{?x1} cannot contain~\texttt{z} or~\texttt{w}, while instances
753 of {\tt?y3(z)} can only contain~\texttt{z}. Due to the restriction on~\texttt{?x1},
754 proof by assumption will fail.
757 {\out by: tactic failed}
758 {\out uncaught exception ERROR}
761 \paragraph{The right approach.}
762 To do this proof, the rules must be applied in the correct order.
763 Parameters should be created before unknowns. The
764 \ttindex{choplev} command returns to an earlier stage of the proof;
765 let us return to the result of applying~$({\imp}I)$:
769 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
770 {\out 1. ALL x y. P(x,y) ==> ALL z w. P(w,z)}
772 Previously we made the mistake of applying $(\forall E)$ before $(\forall I)$.
774 by (resolve_tac [allI] 1);
776 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
777 {\out 1. !!z. ALL x y. P(x,y) ==> ALL w. P(w,z)}
779 by (resolve_tac [allI] 1);
781 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
782 {\out 1. !!z w. ALL x y. P(x,y) ==> P(w,z)}
784 The parameters \texttt{z} and~\texttt{w} have appeared. We now create the
787 by (dresolve_tac [spec] 1);
789 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
790 {\out 1. !!z w. ALL y. P(?x3(z,w),y) ==> P(w,z)}
792 by (dresolve_tac [spec] 1);
794 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
795 {\out 1. !!z w. P(?x3(z,w),?y4(z,w)) ==> P(w,z)}
797 Both {\tt?x3(z,w)} and~{\tt?y4(z,w)} could become any terms containing {\tt
802 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
806 \paragraph{A one-step proof using tacticals.}
807 \index{tacticals} \index{examples!of tacticals}
809 Repeated application of rules can be effective, but the rules should be
810 attempted in the correct order. Let us return to the original goal using
815 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
816 {\out 1. (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
818 As we have just seen, \tdx{allI} should be attempted
819 before~\tdx{spec}, while \ttindex{assume_tac} generally can be
820 attempted first. Such priorities can easily be expressed
821 using~\ttindex{ORELSE}, and repeated using~\ttindex{REPEAT}.
823 by (REPEAT (assume_tac 1 ORELSE resolve_tac [impI,allI] 1
824 ORELSE dresolve_tac [spec] 1));
826 {\out (ALL x y. P(x,y)) --> (ALL z w. P(w,z))}
831 \subsection{A realistic quantifier proof}
832 \index{examples!with quantifiers}
833 To see the practical use of parameters and unknowns, let us prove half of
835 \[ (\forall x. P(x) \imp Q) \,\bimp\, ((\exists x. P(x)) \imp Q). \]
836 We state the left-to-right half to Isabelle in the normal way.
837 Since $\imp$ is nested to the right, $({\imp}I)$ can be applied twice; we
840 Goal "(ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q";
842 {\out (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
843 {\out 1. (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
845 by (REPEAT (resolve_tac [impI] 1));
847 {\out (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
848 {\out 1. [| ALL x. P(x) --> Q; EX x. P(x) |] ==> Q}
850 We can eliminate the universal or the existential quantifier. The
851 existential quantifier should be eliminated first, since this creates a
852 parameter. The rule~$(\exists E)$ is bound to the
853 identifier~\tdx{exE}.
855 by (eresolve_tac [exE] 1);
857 {\out (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
858 {\out 1. !!x. [| ALL x. P(x) --> Q; P(x) |] ==> Q}
860 The only possibility now is $(\forall E)$, a destruction rule. We use
861 \ttindex{dresolve_tac}, which discards the quantified assumption; it is
864 by (dresolve_tac [spec] 1);
866 {\out (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
867 {\out 1. !!x. [| P(x); P(?x3(x)) --> Q |] ==> Q}
869 Because we applied $(\exists E)$ before $(\forall E)$, the unknown
870 term~{\tt?x3(x)} may depend upon the parameter~\texttt{x}.
872 Although $({\imp}E)$ is a destruction rule, it works with
873 \ttindex{eresolve_tac} to perform backward chaining. This technique is
876 by (eresolve_tac [mp] 1);
878 {\out (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
879 {\out 1. !!x. P(x) ==> P(?x3(x))}
881 The tactic has reduced~\texttt{Q} to~\texttt{P(?x3(x))}, deleting the
882 implication. The final step is trivial, thanks to the occurrence of~\texttt{x}.
886 {\out (ALL x. P(x) --> Q) --> (EX x. P(x)) --> Q}
891 \subsection{The classical reasoner}
892 \index{classical reasoner}
893 Isabelle provides enough automation to tackle substantial examples.
895 reasoner can be set up for any classical natural deduction logic;
896 see \iflabelundefined{chap:classical}{the {\em Reference Manual\/}}%
897 {Chap.\ts\ref{chap:classical}}.
898 It cannot compete with fully automatic theorem provers, but it is
899 competitive with tools found in other interactive provers.
901 Rules are packaged into {\bf classical sets}. The classical reasoner
902 provides several tactics, which apply rules using naive algorithms.
903 Unification handles quantifiers as shown above. The most useful tactic
904 is~\ttindex{Blast_tac}.
906 Let us solve problems~40 and~60 of Pelletier~\cite{pelletier86}. (The
907 backslashes~\hbox{\verb|\|\ldots\verb|\|} are an \ML{} string escape
908 sequence, to break the long string over two lines.)
910 Goal "(EX y. ALL x. J(y,x) <-> ~J(x,x)) \ttback
911 \ttback --> ~ (ALL x. EX y. ALL z. J(z,y) <-> ~ J(z,x))";
913 {\out (EX y. ALL x. J(y,x) <-> ~J(x,x)) -->}
914 {\out ~(ALL x. EX y. ALL z. J(z,y) <-> ~J(z,x))}
915 {\out 1. (EX y. ALL x. J(y,x) <-> ~J(x,x)) -->}
916 {\out ~(ALL x. EX y. ALL z. J(z,y) <-> ~J(z,x))}
918 \ttindex{Blast_tac} proves subgoal~1 at a stroke.
924 {\out (EX y. ALL x. J(y,x) <-> ~J(x,x)) -->}
925 {\out ~(ALL x. EX y. ALL z. J(z,y) <-> ~J(z,x))}
928 Sceptics may examine the proof by calling the package's single-step
929 tactics, such as~\texttt{step_tac}. This would take up much space, however,
930 so let us proceed to the next example:
932 Goal "ALL x. P(x,f(x)) <-> \ttback
933 \ttback (EX y. (ALL z. P(z,y) --> P(z,f(x))) & P(x,y))";
935 {\out ALL x. P(x,f(x)) <-> (EX y. (ALL z. P(z,y) --> P(z,f(x))) & P(x,y))}
936 {\out 1. ALL x. P(x,f(x)) <->}
937 {\out (EX y. (ALL z. P(z,y) --> P(z,f(x))) & P(x,y))}
939 Again, subgoal~1 succumbs immediately.
945 {\out ALL x. P(x,f(x)) <-> (EX y. (ALL z. P(z,y) --> P(z,f(x))) & P(x,y))}
948 The classical reasoner is not restricted to the usual logical connectives.
949 The natural deduction rules for unions and intersections resemble those for
950 disjunction and conjunction. The rules for infinite unions and
951 intersections resemble those for quantifiers. Given such rules, the classical
952 reasoner is effective for reasoning in set theory.