author | paulson |
Mon, 29 Jan 2001 10:28:00 +0100 | |
changeset 10987 | c36733b147e8 |
parent 10983 | 59961d32b1ae |
child 11077 | 8f4fa58e6fba |
permissions | -rw-r--r-- |
10792 | 1 |
% $Id$ |
10295 | 2 |
\chapter{The Rules of the Game} |
3 |
\label{chap:rules} |
|
4 |
||
5 |
Until now, we have proved everything using only induction and simplification. |
|
6 |
Substantial proofs require more elaborate forms of inference. This chapter |
|
7 |
outlines the concepts and techniques that underlie reasoning in Isabelle. The examples |
|
8 |
are mainly drawn from predicate logic. The first examples in this |
|
9 |
chapter will consist of detailed, low-level proof steps. Later, we shall |
|
10 |
see how to automate such reasoning using the methods \isa{blast}, |
|
11 |
\isa{auto} and others. |
|
12 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
13 |
\section{Natural Deduction} |
10295 | 14 |
|
15 |
In Isabelle, proofs are constructed using inference rules. The |
|
16 |
most familiar inference rule is probably \emph{modus ponens}: |
|
17 |
\[ \infer{Q}{P\imp Q & P} \] |
|
18 |
This rule says that from $P\imp Q$ and $P$ |
|
19 |
we may infer~$Q$. |
|
20 |
||
21 |
%Early logical formalisms had this |
|
22 |
%rule and at most one or two others, along with many complicated |
|
23 |
%axioms. Any desired theorem could be obtained by applying \emph{modus |
|
24 |
%ponens} or other rules to the axioms, but proofs were |
|
25 |
%hard to find. For example, a standard inference system has |
|
26 |
%these two axioms (amongst others): |
|
27 |
%\begin{gather*} |
|
28 |
% P\imp(Q\imp P) \tag{K}\\ |
|
29 |
% (P\imp(Q\imp R))\imp ((P\imp Q)\imp(P\imp R)) \tag{S} |
|
30 |
%\end{gather*} |
|
31 |
%Try proving the trivial fact $P\imp P$ using these axioms and \emph{modus |
|
32 |
%ponens}! |
|
33 |
||
34 |
\textbf{Natural deduction} is an attempt to formalize logic in a way |
|
35 |
that mirrors human reasoning patterns. |
|
36 |
% |
|
37 |
%Instead of having a few |
|
38 |
%inference rules and many axioms, it has many inference rules |
|
39 |
%and few axioms. |
|
40 |
% |
|
41 |
For each logical symbol (say, $\conj$), there |
|
42 |
are two kinds of rules: \textbf{introduction} and \textbf{elimination} rules. |
|
43 |
The introduction rules allow us to infer this symbol (say, to |
|
44 |
infer conjunctions). The elimination rules allow us to deduce |
|
45 |
consequences from this symbol. Ideally each rule should mention |
|
46 |
one symbol only. For predicate logic this can be |
|
47 |
done, but when users define their own concepts they typically |
|
48 |
have to refer to other symbols as well. It is best not be dogmatic. |
|
49 |
||
50 |
Natural deduction generally deserves its name. It is easy to use. Each |
|
51 |
proof step consists of identifying the outermost symbol of a formula and |
|
52 |
applying the corresponding rule. It creates new subgoals in |
|
53 |
an obvious way from parts of the chosen formula. Expanding the |
|
54 |
definitions of constants can blow up the goal enormously. Deriving natural |
|
55 |
deduction rules for such constants lets us reason in terms of their key |
|
56 |
properties, which might otherwise be obscured by the technicalities of its |
|
57 |
definition. Natural deduction rules also lend themselves to automation. |
|
58 |
Isabelle's |
|
59 |
\textbf{classical reasoner} accepts any suitable collection of natural deduction |
|
60 |
rules and uses them to search for proofs automatically. Isabelle is designed around |
|
61 |
natural deduction and many of its tools use the terminology of introduction and |
|
62 |
elimination rules. |
|
63 |
||
64 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
65 |
\section{Introduction Rules} |
10295 | 66 |
|
67 |
An \textbf{introduction} rule tells us when we can infer a formula |
|
68 |
containing a specific logical symbol. For example, the conjunction |
|
69 |
introduction rule says that if we have $P$ and if we have $Q$ then |
|
70 |
we have $P\conj Q$. In a mathematics text, it is typically shown |
|
71 |
like this: |
|
72 |
\[ \infer{P\conj Q}{P & Q} \] |
|
73 |
The rule introduces the conjunction |
|
10971 | 74 |
symbol~($\conj$) in its conclusion. In Isabelle proofs we |
10295 | 75 |
mainly reason backwards. When we apply this rule, the subgoal already has |
76 |
the form of a conjunction; the proof step makes this conjunction symbol |
|
77 |
disappear. |
|
78 |
||
79 |
In Isabelle notation, the rule looks like this: |
|
80 |
\begin{isabelle} |
|
81 |
\isasymlbrakk?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P\ \isasymand\ ?Q\rulename{conjI} |
|
82 |
\end{isabelle} |
|
83 |
Carefully examine the syntax. The premises appear to the |
|
84 |
left of the arrow and the conclusion to the right. The premises (if |
|
85 |
more than one) are grouped using the fat brackets. The question marks |
|
86 |
indicate \textbf{schematic variables} (also called \textbf{unknowns}): they may |
|
87 |
be replaced by arbitrary formulas. If we use the rule backwards, Isabelle |
|
88 |
tries to unify the current subgoal with the conclusion of the rule, which |
|
89 |
has the form \isa{?P\ \isasymand\ ?Q}. (Unification is discussed below, |
|
90 |
\S\ref{sec:unification}.) If successful, |
|
91 |
it yields new subgoals given by the formulas assigned to |
|
92 |
\isa{?P} and \isa{?Q}. |
|
93 |
||
94 |
The following trivial proof illustrates this point. |
|
95 |
\begin{isabelle} |
|
10596 | 96 |
\isacommand{lemma}\ conj_rule:\ "\isasymlbrakk P;\ |
10295 | 97 |
Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\ |
10301 | 98 |
(Q\ \isasymand\ P)"\isanewline |
10295 | 99 |
\isacommand{apply}\ (rule\ conjI)\isanewline |
100 |
\ \isacommand{apply}\ assumption\isanewline |
|
101 |
\isacommand{apply}\ (rule\ conjI)\isanewline |
|
102 |
\ \isacommand{apply}\ assumption\isanewline |
|
103 |
\isacommand{apply}\ assumption |
|
104 |
\end{isabelle} |
|
105 |
At the start, Isabelle presents |
|
106 |
us with the assumptions (\isa{P} and~\isa{Q}) and with the goal to be proved, |
|
107 |
\isa{P\ \isasymand\ |
|
108 |
(Q\ \isasymand\ P)}. We are working backwards, so when we |
|
109 |
apply conjunction introduction, the rule removes the outermost occurrence |
|
110 |
of the \isa{\isasymand} symbol. To apply a rule to a subgoal, we apply |
|
10596 | 111 |
the proof method \isa{rule} --- here with {\isa{conjI}}, the conjunction |
10295 | 112 |
introduction rule. |
113 |
\begin{isabelle} |
|
10596 | 114 |
%\isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\ Q\ |
10295 | 115 |
%\isasymand\ P\isanewline |
10596 | 116 |
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\isanewline |
117 |
\ 2.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ Q\ \isasymand\ P |
|
10295 | 118 |
\end{isabelle} |
119 |
Isabelle leaves two new subgoals: the two halves of the original conjunction. |
|
120 |
The first is simply \isa{P}, which is trivial, since \isa{P} is among |
|
10596 | 121 |
the assumptions. We can apply the \isa{assumption} |
10295 | 122 |
method, which proves a subgoal by finding a matching assumption. |
123 |
\begin{isabelle} |
|
10596 | 124 |
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ |
10295 | 125 |
Q\ \isasymand\ P |
126 |
\end{isabelle} |
|
127 |
We are left with the subgoal of proving |
|
128 |
\isa{Q\ \isasymand\ P} from the assumptions \isa{P} and~\isa{Q}. We apply |
|
129 |
\isa{rule conjI} again. |
|
130 |
\begin{isabelle} |
|
10596 | 131 |
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ Q\isanewline |
132 |
\ 2.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P |
|
10295 | 133 |
\end{isabelle} |
134 |
We are left with two new subgoals, \isa{Q} and~\isa{P}, each of which can be proved |
|
10596 | 135 |
using the \isa{assumption} method. |
10295 | 136 |
|
137 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
138 |
\section{Elimination Rules} |
10295 | 139 |
|
140 |
\textbf{Elimination} rules work in the opposite direction from introduction |
|
141 |
rules. In the case of conjunction, there are two such rules. |
|
142 |
From $P\conj Q$ we infer $P$. also, from $P\conj Q$ |
|
143 |
we infer $Q$: |
|
144 |
\[ \infer{P}{P\conj Q} \qquad \infer{Q}{P\conj Q} \] |
|
145 |
||
146 |
Now consider disjunction. There are two introduction rules, which resemble inverted forms of the |
|
147 |
conjunction elimination rules: |
|
148 |
\[ \infer{P\disj Q}{P} \qquad \infer{P\disj Q}{Q} \] |
|
149 |
||
150 |
What is the disjunction elimination rule? The situation is rather different from |
|
151 |
conjunction. From $P\disj Q$ we cannot conclude that $P$ is true and we |
|
152 |
cannot conclude that $Q$ is true; there are no direct |
|
153 |
elimination rules of the sort that we have seen for conjunction. Instead, |
|
154 |
there is an elimination rule that works indirectly. If we are trying to prove |
|
155 |
something else, say $R$, and we know that $P\disj Q$ holds, then we have to consider |
|
156 |
two cases. We can assume that $P$ is true and prove $R$ and then assume that $Q$ is |
|
157 |
true and prove $R$ a second time. Here we see a fundamental concept used in natural |
|
158 |
deduction: that of the \textbf{assumptions}. We have to prove $R$ twice, under |
|
159 |
different assumptions. The assumptions are local to these subproofs and are visible |
|
160 |
nowhere else. |
|
161 |
||
162 |
In a logic text, the disjunction elimination rule might be shown |
|
163 |
like this: |
|
164 |
\[ \infer{R}{P\disj Q & \infer*{R}{[P]} & \infer*{R}{[Q]}} \] |
|
165 |
The assumptions $[P]$ and $[Q]$ are bracketed |
|
166 |
to emphasize that they are local to their subproofs. In Isabelle |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
167 |
notation, the already-familiar \isa{\isasymLongrightarrow} syntax serves the |
10295 | 168 |
same purpose: |
169 |
\begin{isabelle} |
|
170 |
\isasymlbrakk?P\ \isasymor\ ?Q;\ ?P\ \isasymLongrightarrow\ ?R;\ ?Q\ \isasymLongrightarrow\ ?R\isasymrbrakk\ \isasymLongrightarrow\ ?R\rulename{disjE} |
|
171 |
\end{isabelle} |
|
172 |
When we use this sort of elimination rule backwards, it produces |
|
10971 | 173 |
a case split. (We have seen this before, in proofs by induction.) The following proof |
10295 | 174 |
illustrates the use of disjunction elimination. |
175 |
\begin{isabelle} |
|
10301 | 176 |
\isacommand{lemma}\ disj_swap:\ "P\ \isasymor\ Q\ |
10295 | 177 |
\isasymLongrightarrow\ Q\ \isasymor\ P"\isanewline |
178 |
\isacommand{apply}\ (erule\ disjE)\isanewline |
|
179 |
\ \isacommand{apply}\ (rule\ disjI2)\isanewline |
|
180 |
\ \isacommand{apply}\ assumption\isanewline |
|
181 |
\isacommand{apply}\ (rule\ disjI1)\isanewline |
|
182 |
\isacommand{apply}\ assumption |
|
183 |
\end{isabelle} |
|
184 |
We assume \isa{P\ \isasymor\ Q} and |
|
185 |
must prove \isa{Q\ \isasymor\ P}\@. Our first step uses the disjunction |
|
186 |
elimination rule, \isa{disjE}. The method {\isa{erule}} applies an |
|
10971 | 187 |
elimination rule, searching for an assumption that matches the |
10295 | 188 |
rule's first premise. Deleting that assumption, it |
189 |
return the subgoals for the remaining premises. Most of the |
|
190 |
time, this is the best way to use elimination rules; only rarely is there |
|
191 |
any point in keeping the assumption. |
|
192 |
||
193 |
\begin{isabelle} |
|
194 |
%P\ \isasymor\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P\isanewline |
|
195 |
\ 1.\ P\ \isasymLongrightarrow\ Q\ \isasymor\ P\isanewline |
|
196 |
\ 2.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P |
|
197 |
\end{isabelle} |
|
198 |
Here it leaves us with two subgoals. The first assumes \isa{P} and the |
|
199 |
second assumes \isa{Q}. Tackling the first subgoal, we need to |
|
200 |
show \isa{Q\ \isasymor\ P}\@. The second introduction rule (\isa{disjI2}) |
|
201 |
can reduce this to \isa{P}, which matches the assumption. So, we apply the |
|
10596 | 202 |
\isa{rule} method with \isa{disjI2} \ldots |
10295 | 203 |
\begin{isabelle} |
204 |
\ 1.\ P\ \isasymLongrightarrow\ P\isanewline |
|
205 |
\ 2.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P |
|
206 |
\end{isabelle} |
|
10596 | 207 |
\ldots and finish off with the \isa{assumption} |
10295 | 208 |
method. We are left with the other subgoal, which |
209 |
assumes \isa{Q}. |
|
210 |
\begin{isabelle} |
|
211 |
\ 1.\ Q\ \isasymLongrightarrow\ Q\ \isasymor\ P |
|
212 |
\end{isabelle} |
|
213 |
Its proof is similar, using the introduction |
|
214 |
rule \isa{disjI1}. |
|
215 |
||
216 |
The result of this proof is a new inference rule \isa{disj_swap}, which is neither |
|
217 |
an introduction nor an elimination rule, but which might |
|
218 |
be useful. We can use it to replace any goal of the form $Q\disj P$ |
|
219 |
by a one of the form $P\disj Q$. |
|
220 |
||
221 |
||
222 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
223 |
\section{Destruction Rules: Some Examples} |
10295 | 224 |
|
225 |
Now let us examine the analogous proof for conjunction. |
|
226 |
\begin{isabelle} |
|
10301 | 227 |
\isacommand{lemma}\ conj_swap:\ "P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P"\isanewline |
10295 | 228 |
\isacommand{apply}\ (rule\ conjI)\isanewline |
229 |
\ \isacommand{apply}\ (drule\ conjunct2)\isanewline |
|
230 |
\ \isacommand{apply}\ assumption\isanewline |
|
231 |
\isacommand{apply}\ (drule\ conjunct1)\isanewline |
|
232 |
\isacommand{apply}\ assumption |
|
233 |
\end{isabelle} |
|
234 |
Recall that the conjunction elimination rules --- whose Isabelle names are |
|
235 |
\isa{conjunct1} and \isa{conjunct2} --- simply return the first or second half |
|
236 |
of a conjunction. Rules of this sort (where the conclusion is a subformula of a |
|
10978 | 237 |
premise) are called \textbf{destruction} rules because they take apart and destroy |
238 |
a premise.% |
|
10295 | 239 |
\footnote{This Isabelle terminology has no counterpart in standard logic texts, |
240 |
although the distinction between the two forms of elimination rule is well known. |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
241 |
Girard \cite[page 74]{girard89}, for example, writes ``The elimination rules |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
242 |
[for $\disj$ and $\exists$] are very |
10295 | 243 |
bad. What is catastrophic about them is the parasitic presence of a formula [$R$] |
244 |
which has no structural link with the formula which is eliminated.''} |
|
245 |
||
246 |
The first proof step applies conjunction introduction, leaving |
|
247 |
two subgoals: |
|
248 |
\begin{isabelle} |
|
249 |
%P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P\isanewline |
|
250 |
\ 1.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\isanewline |
|
251 |
\ 2.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ P |
|
252 |
\end{isabelle} |
|
253 |
||
254 |
To invoke the elimination rule, we apply a new method, \isa{drule}. |
|
255 |
Think of the \isa{d} as standing for \textbf{destruction} (or \textbf{direct}, if |
|
256 |
you prefer). Applying the |
|
257 |
second conjunction rule using \isa{drule} replaces the assumption |
|
258 |
\isa{P\ \isasymand\ Q} by \isa{Q}. |
|
259 |
\begin{isabelle} |
|
260 |
\ 1.\ Q\ \isasymLongrightarrow\ Q\isanewline |
|
261 |
\ 2.\ P\ \isasymand\ Q\ \isasymLongrightarrow\ P |
|
262 |
\end{isabelle} |
|
263 |
The resulting subgoal can be proved by applying \isa{assumption}. |
|
264 |
The other subgoal is similarly proved, using the \isa{conjunct1} rule and the |
|
265 |
\isa{assumption} method. |
|
266 |
||
267 |
Choosing among the methods \isa{rule}, \isa{erule} and \isa{drule} is up to |
|
268 |
you. Isabelle does not attempt to work out whether a rule |
|
269 |
is an introduction rule or an elimination rule. The |
|
270 |
method determines how the rule will be interpreted. Many rules |
|
271 |
can be used in more than one way. For example, \isa{disj_swap} can |
|
272 |
be applied to assumptions as well as to goals; it replaces any |
|
273 |
assumption of the form |
|
274 |
$P\disj Q$ by a one of the form $Q\disj P$. |
|
275 |
||
276 |
Destruction rules are simpler in form than indirect rules such as \isa{disjE}, |
|
277 |
but they can be inconvenient. Each of the conjunction rules discards half |
|
278 |
of the formula, when usually we want to take both parts of the conjunction as new |
|
279 |
assumptions. The easiest way to do so is by using an |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
280 |
alternative conjunction elimination rule that resembles \isa{disjE}\@. It is |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
281 |
seldom, if ever, seen in logic books. In Isabelle syntax it looks like this: |
10295 | 282 |
\begin{isabelle} |
283 |
\isasymlbrakk?P\ \isasymand\ ?Q;\ \isasymlbrakk?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?R\isasymrbrakk\ \isasymLongrightarrow\ ?R\rulename{conjE} |
|
284 |
\end{isabelle} |
|
285 |
||
286 |
\begin{exercise} |
|
287 |
Use the rule {\isa{conjE}} to shorten the proof above. |
|
288 |
\end{exercise} |
|
289 |
||
290 |
||
291 |
\section{Implication} |
|
292 |
||
293 |
At the start of this chapter, we saw the rule \textit{modus ponens}. It is, in fact, |
|
294 |
a destruction rule. The matching introduction rule looks like this |
|
295 |
in Isabelle: |
|
296 |
\begin{isabelle} |
|
297 |
(?P\ \isasymLongrightarrow\ ?Q)\ \isasymLongrightarrow\ ?P\ |
|
298 |
\isasymlongrightarrow\ ?Q\rulename{impI} |
|
299 |
\end{isabelle} |
|
300 |
And this is \textit{modus ponens}: |
|
301 |
\begin{isabelle} |
|
302 |
\isasymlbrakk?P\ \isasymlongrightarrow\ ?Q;\ ?P\isasymrbrakk\ |
|
303 |
\isasymLongrightarrow\ ?Q |
|
304 |
\rulename{mp} |
|
305 |
\end{isabelle} |
|
306 |
||
307 |
Here is a proof using the rules for implication. This |
|
308 |
lemma performs a sort of uncurrying, replacing the two antecedents |
|
309 |
of a nested implication by a conjunction. |
|
310 |
\begin{isabelle} |
|
311 |
\isacommand{lemma}\ imp_uncurry:\ |
|
10301 | 312 |
"P\ \isasymlongrightarrow\ (Q\ |
10295 | 313 |
\isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\ |
314 |
\isasymand\ Q\ \isasymlongrightarrow\ |
|
315 |
R"\isanewline |
|
316 |
\isacommand{apply}\ (rule\ impI)\isanewline |
|
317 |
\isacommand{apply}\ (erule\ conjE)\isanewline |
|
318 |
\isacommand{apply}\ (drule\ mp)\isanewline |
|
319 |
\ \isacommand{apply}\ assumption\isanewline |
|
320 |
\isacommand{apply}\ (drule\ mp)\isanewline |
|
321 |
\ \ \isacommand{apply}\ assumption\isanewline |
|
322 |
\ \isacommand{apply}\ assumption |
|
323 |
\end{isabelle} |
|
324 |
First, we state the lemma and apply implication introduction (\isa{rule impI}), |
|
325 |
which moves the conjunction to the assumptions. |
|
326 |
\begin{isabelle} |
|
327 |
%P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R\ \isasymLongrightarrow\ P\ |
|
328 |
%\isasymand\ Q\ \isasymlongrightarrow\ R\isanewline |
|
10596 | 329 |
\ 1.\ \isasymlbrakk P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R;\ P\ \isasymand\ Q\isasymrbrakk\ \isasymLongrightarrow\ R |
10295 | 330 |
\end{isabelle} |
331 |
Next, we apply conjunction elimination (\isa{erule conjE}), which splits this |
|
332 |
conjunction into two parts. |
|
333 |
\begin{isabelle} |
|
10596 | 334 |
\ 1.\ \isasymlbrakk P\ \isasymlongrightarrow\ Q\ \isasymlongrightarrow\ R;\ P;\ |
10295 | 335 |
Q\isasymrbrakk\ \isasymLongrightarrow\ R |
336 |
\end{isabelle} |
|
337 |
Now, we work on the assumption \isa{P\ \isasymlongrightarrow\ (Q\ |
|
338 |
\isasymlongrightarrow\ R)}, where the parentheses have been inserted for |
|
339 |
clarity. The nested implication requires two applications of |
|
340 |
\textit{modus ponens}: \isa{drule mp}. The first use yields the |
|
341 |
implication \isa{Q\ |
|
342 |
\isasymlongrightarrow\ R}, but first we must prove the extra subgoal |
|
343 |
\isa{P}, which we do by assumption. |
|
344 |
\begin{isabelle} |
|
10596 | 345 |
\ 1.\ \isasymlbrakk P;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P\isanewline |
346 |
\ 2.\ \isasymlbrakk P;\ Q;\ Q\ \isasymlongrightarrow\ R\isasymrbrakk\ \isasymLongrightarrow\ R |
|
10295 | 347 |
\end{isabelle} |
348 |
Repeating these steps for \isa{Q\ |
|
349 |
\isasymlongrightarrow\ R} yields the conclusion we seek, namely~\isa{R}. |
|
350 |
\begin{isabelle} |
|
10596 | 351 |
\ 1.\ \isasymlbrakk P;\ Q;\ Q\ \isasymlongrightarrow\ R\isasymrbrakk\ |
10295 | 352 |
\isasymLongrightarrow\ R |
353 |
\end{isabelle} |
|
354 |
||
355 |
The symbols \isa{\isasymLongrightarrow} and \isa{\isasymlongrightarrow} |
|
356 |
both stand for implication, but they differ in many respects. Isabelle |
|
357 |
uses \isa{\isasymLongrightarrow} to express inference rules; the symbol is |
|
358 |
built-in and Isabelle's inference mechanisms treat it specially. On the |
|
359 |
other hand, \isa{\isasymlongrightarrow} is just one of the many connectives |
|
360 |
available in higher-order logic. We reason about it using inference rules |
|
361 |
such as \isa{impI} and \isa{mp}, just as we reason about the other |
|
362 |
connectives. You will have to use \isa{\isasymlongrightarrow} in any |
|
363 |
context that requires a formula of higher-order logic. Use |
|
364 |
\isa{\isasymLongrightarrow} to separate a theorem's preconditions from its |
|
365 |
conclusion. |
|
366 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
367 |
\medskip |
10978 | 368 |
\indexbold{*by} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
369 |
The \isacommand{by} command is useful for proofs like these that use |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
370 |
\isa{assumption} heavily. It executes an |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
371 |
\isacommand{apply} command, then tries to prove all remaining subgoals using |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
372 |
\isa{assumption}. Since (if successful) it ends the proof, it also replaces the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
373 |
\isacommand{done} symbol. For example, the proof above can be shortened: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
374 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
375 |
\isacommand{lemma}\ imp_uncurry:\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
376 |
"P\ \isasymlongrightarrow\ (Q\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
377 |
\isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
378 |
\isasymand\ Q\ \isasymlongrightarrow\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
379 |
R"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
380 |
\isacommand{apply}\ (rule\ impI)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
381 |
\isacommand{apply}\ (erule\ conjE)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
382 |
\isacommand{apply}\ (drule\ mp)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
383 |
\ \isacommand{apply}\ assumption\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
384 |
\isacommand{by}\ (drule\ mp) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
385 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
386 |
We could use \isacommand{by} to replace the final \isacommand{apply} and |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
387 |
\isacommand{done} in any proof, but typically we use it |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
388 |
to eliminate calls to \isa{assumption}. It is also a nice way of expressing a |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
389 |
one-line proof. |
10295 | 390 |
|
391 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
392 |
\section{Unification and Substitution}\label{sec:unification} |
10295 | 393 |
|
394 |
As we have seen, Isabelle rules involve variables that begin with a |
|
395 |
question mark. These are called \textbf{schematic} variables and act as |
|
396 |
placeholders for terms. \textbf{Unification} refers to the process of |
|
10971 | 397 |
making two terms identical, possibly by replacing their schematic variables by |
10295 | 398 |
terms. The simplest case is when the two terms are already the same. Next |
399 |
simplest is when the variables in only one of the term |
|
400 |
are replaced; this is called \textbf{pattern-matching}. The |
|
10596 | 401 |
\isa{rule} method typically matches the rule's conclusion |
10295 | 402 |
against the current subgoal. In the most complex case, variables in both |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
403 |
terms are replaced; the \isa{rule} method can do this if the goal |
10295 | 404 |
itself contains schematic variables. Other occurrences of the variables in |
405 |
the rule or proof state are updated at the same time. |
|
406 |
||
407 |
Schematic variables in goals are sometimes called \textbf{unknowns}. They |
|
408 |
are useful because they let us proceed with a proof even when we do not |
|
409 |
know what certain terms should be --- as when the goal is $\exists x.\,P$. |
|
410 |
They can be filled in later, often automatically. |
|
411 |
||
412 |
Unification is well known to Prolog programmers. Isabelle uses \textbf{higher-order} |
|
413 |
unification, which is unification in the |
|
414 |
typed $\lambda$-calculus. The general case is |
|
415 |
undecidable, but for our purposes, the differences from ordinary |
|
416 |
unification are straightforward. It handles bound variables |
|
417 |
correctly, avoiding capture. The two terms \isa{{\isasymlambda}x.\ ?P} and |
|
418 |
\isa{{\isasymlambda}x.\ t x} are not unifiable; replacing \isa{?P} by |
|
419 |
\isa{t x} is forbidden because the free occurrence of~\isa{x} would become |
|
420 |
bound. The two terms |
|
421 |
\isa{{\isasymlambda}x.\ f(x,z)} and \isa{{\isasymlambda}y.\ f(y,z)} are |
|
422 |
trivially unifiable because they differ only by a bound variable renaming. |
|
423 |
||
424 |
Higher-order unification sometimes must invent |
|
425 |
$\lambda$-terms to replace function variables, |
|
426 |
which can lead to a combinatorial explosion. However, Isabelle proofs tend |
|
427 |
to involve easy cases where there are few possibilities for the |
|
428 |
$\lambda$-term being constructed. In the easiest case, the |
|
429 |
function variable is applied only to bound variables, |
|
430 |
as when we try to unify \isa{{\isasymlambda}x\ y.\ f(?h x y)} and |
|
431 |
\isa{{\isasymlambda}x\ y.\ f(x+y+a)}. The only solution is to replace |
|
432 |
\isa{?h} by \isa{{\isasymlambda}x\ y.\ x+y+a}. Such cases admit at most |
|
433 |
one unifier, like ordinary unification. A harder case is |
|
434 |
unifying \isa{?h a} with~\isa{a+b}; it admits two solutions for \isa{?h}, |
|
435 |
namely \isa{{\isasymlambda}x.~a+b} and \isa{{\isasymlambda}x.~x+b}. |
|
436 |
Unifying \isa{?h a} with~\isa{a+a+b} admits four solutions; their number is |
|
437 |
exponential in the number of occurrences of~\isa{a} in the second term. |
|
438 |
||
439 |
Isabelle also uses function variables to express \textbf{substitution}. |
|
440 |
A typical substitution rule allows us to replace one term by |
|
441 |
another if we know that two terms are equal. |
|
442 |
\[ \infer{P[t/x]}{s=t & P[s/x]} \] |
|
10971 | 443 |
The rule uses a notation for substitution: $P[t/x]$ is the result of |
10295 | 444 |
replacing $x$ by~$t$ in~$P$. The rule only substitutes in the positions |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
445 |
designated by~$x$. For example, it can |
10295 | 446 |
derive symmetry of equality from reflexivity. Using $x=s$ for~$P$ |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
447 |
replaces just the first $s$ in $s=s$ by~$t$: |
10295 | 448 |
\[ \infer{t=s}{s=t & \infer{s=s}{}} \] |
449 |
||
450 |
The Isabelle version of the substitution rule looks like this: |
|
451 |
\begin{isabelle} |
|
452 |
\isasymlbrakk?t\ =\ ?s;\ ?P\ ?s\isasymrbrakk\ \isasymLongrightarrow\ ?P\ |
|
453 |
?t |
|
454 |
\rulename{ssubst} |
|
455 |
\end{isabelle} |
|
456 |
Crucially, \isa{?P} is a function |
|
457 |
variable: it can be replaced by a $\lambda$-expression |
|
458 |
involving one bound variable whose occurrences identify the places |
|
459 |
in which $s$ will be replaced by~$t$. The proof above requires |
|
460 |
\isa{{\isasymlambda}x.~x=s}. |
|
461 |
||
462 |
The \isa{simp} method replaces equals by equals, but using the substitution |
|
463 |
rule gives us more control. Consider this proof: |
|
464 |
\begin{isabelle} |
|
465 |
\isacommand{lemma}\ |
|
10596 | 466 |
"\isasymlbrakk \ x\ =\ f\ x;\ odd(f\ x)\ \isasymrbrakk\ \isasymLongrightarrow\ |
467 |
odd\ x"\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
468 |
\isacommand{by}\ (erule\ ssubst) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
469 |
\end{isabelle} |
10295 | 470 |
% |
471 |
The simplifier might loop, replacing \isa{x} by \isa{f x} and then by |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
472 |
\isa{f(f x)} and so forth. (Here \isa{simp} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
473 |
can see the danger and would re-orient the equality, but in more complicated |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
474 |
cases it can be fooled.) When we apply substitution, Isabelle replaces every |
10295 | 475 |
\isa{x} in the subgoal by \isa{f x} just once: it cannot loop. The |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
476 |
resulting subgoal is trivial by assumption, so the \isacommand{by} command |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
477 |
proves it implicitly. |
10295 | 478 |
|
479 |
We are using the \isa{erule} method it in a novel way. Hitherto, |
|
480 |
the conclusion of the rule was just a variable such as~\isa{?R}, but it may |
|
481 |
be any term. The conclusion is unified with the subgoal just as |
|
482 |
it would be with the \isa{rule} method. At the same time \isa{erule} looks |
|
483 |
for an assumption that matches the rule's first premise, as usual. With |
|
484 |
\isa{ssubst} the effect is to find, use and delete an equality |
|
485 |
assumption. |
|
486 |
||
487 |
||
488 |
Higher-order unification can be tricky, as this example indicates: |
|
489 |
\begin{isabelle} |
|
10596 | 490 |
\isacommand{lemma}\ "\isasymlbrakk \ x\ =\ |
10295 | 491 |
f\ x;\ triple\ (f\ x)\ |
492 |
(f\ x)\ x\ \isasymrbrakk\ |
|
493 |
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline |
|
494 |
\isacommand{apply}\ (erule\ ssubst)\isanewline |
|
495 |
\isacommand{back}\isanewline |
|
496 |
\isacommand{back}\isanewline |
|
497 |
\isacommand{back}\isanewline |
|
498 |
\isacommand{back}\isanewline |
|
499 |
\isacommand{apply}\ assumption\isanewline |
|
500 |
\isacommand{done} |
|
501 |
\end{isabelle} |
|
502 |
% |
|
503 |
By default, Isabelle tries to substitute for all the |
|
504 |
occurrences. Applying \isa{erule\ ssubst} yields this subgoal: |
|
505 |
\begin{isabelle} |
|
506 |
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ (f\ x)\ (f\ x) |
|
507 |
\end{isabelle} |
|
508 |
The substitution should have been done in the first two occurrences |
|
509 |
of~\isa{x} only. Isabelle has gone too far. The \isa{back} |
|
510 |
method allows us to reject this possibility and get a new one: |
|
511 |
\begin{isabelle} |
|
512 |
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ x\ (f\ x)\ (f\ x) |
|
513 |
\end{isabelle} |
|
514 |
% |
|
515 |
Now Isabelle has left the first occurrence of~\isa{x} alone. That is |
|
516 |
promising but it is not the desired combination. So we use \isa{back} |
|
517 |
again: |
|
518 |
\begin{isabelle} |
|
519 |
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ x\ (f\ x) |
|
520 |
\end{isabelle} |
|
521 |
% |
|
522 |
This also is wrong, so we use \isa{back} again: |
|
523 |
\begin{isabelle} |
|
524 |
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ x\ x\ (f\ x) |
|
525 |
\end{isabelle} |
|
526 |
% |
|
527 |
And this one is wrong too. Looking carefully at the series |
|
528 |
of alternatives, we see a binary countdown with reversed bits: 111, |
|
529 |
011, 101, 001. Invoke \isa{back} again: |
|
530 |
\begin{isabelle} |
|
531 |
\ 1.\ triple\ (f\ x)\ (f\ x)\ x\ \isasymLongrightarrow\ triple\ (f\ x)\ (f\ x)\ x% |
|
532 |
\end{isabelle} |
|
533 |
At last, we have the right combination! This goal follows by assumption. |
|
534 |
||
535 |
Never use {\isa{back}} in the final version of a proof. |
|
536 |
It should only be used for exploration. One way to get rid of {\isa{back}} |
|
537 |
to combine two methods in a single \textbf{apply} command. Isabelle |
|
538 |
applies the first method and then the second. If the second method |
|
539 |
fails then Isabelle automatically backtracks. This process continues until |
|
540 |
the first method produces an output that the second method can |
|
541 |
use. We get a one-line proof of our example: |
|
542 |
\begin{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
543 |
\isacommand{lemma}\ "\isasymlbrakk \ x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\ |
10295 | 544 |
\isasymrbrakk\ |
545 |
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline |
|
546 |
\isacommand{apply}\ (erule\ ssubst,\ assumption)\isanewline |
|
547 |
\isacommand{done} |
|
548 |
\end{isabelle} |
|
549 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
550 |
\noindent |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
551 |
The \isacommand{by} command works too, since it backtracks when |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
552 |
proving subgoals by assumption: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
553 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
554 |
\isacommand{lemma}\ "\isasymlbrakk \ x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
555 |
\isasymrbrakk\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
556 |
\isasymLongrightarrow\ triple\ x\ x\ x"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
557 |
\isacommand{by}\ (erule\ ssubst) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
558 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
559 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
560 |
|
10295 | 561 |
The most general way to get rid of the {\isa{back}} command is |
10596 | 562 |
to instantiate variables in the rule. The method \isa{rule_tac} is |
10295 | 563 |
similar to \isa{rule}, but it |
564 |
makes some of the rule's variables denote specified terms. |
|
10596 | 565 |
Also available are {\isa{drule_tac}} and \isa{erule_tac}. Here we need |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
566 |
\isa{erule_tac} since above we used \isa{erule}. |
10295 | 567 |
\begin{isabelle} |
10596 | 568 |
\isacommand{lemma}\ "\isasymlbrakk \ x\ =\ f\ x;\ triple\ (f\ x)\ (f\ x)\ x\ \isasymrbrakk\ \isasymLongrightarrow\ triple\ x\ x\ x"\isanewline |
10971 | 569 |
\isacommand{by}\ (erule_tac\ P = "\isasymlambda u.\ P\ u\ u\ x"\ |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
570 |
\isakeyword{in}\ ssubst) |
10295 | 571 |
\end{isabelle} |
572 |
% |
|
573 |
To specify a desired substitution |
|
574 |
requires instantiating the variable \isa{?P} with a $\lambda$-expression. |
|
575 |
The bound variable occurrences in \isa{{\isasymlambda}u.\ P\ u\ |
|
576 |
u\ x} indicate that the first two arguments have to be substituted, leaving |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
577 |
the third unchanged. With this instantiation, backtracking is neither necessary |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
578 |
nor possible. |
10295 | 579 |
|
10596 | 580 |
An alternative to \isa{rule_tac} is to use \isa{rule} with the |
581 |
\isa{of} directive, described in \S\ref{sec:forward} below. An |
|
582 |
advantage of \isa{rule_tac} is that the instantiations may refer to |
|
10971 | 583 |
\isasymAnd-bound variables in the current subgoal. |
10295 | 584 |
|
585 |
||
586 |
\section{Negation} |
|
587 |
||
588 |
Negation causes surprising complexity in proofs. Its natural |
|
589 |
deduction rules are straightforward, but additional rules seem |
|
590 |
necessary in order to handle negated assumptions gracefully. |
|
591 |
||
592 |
Negation introduction deduces $\neg P$ if assuming $P$ leads to a |
|
593 |
contradiction. Negation elimination deduces any formula in the |
|
594 |
presence of $\neg P$ together with~$P$: |
|
595 |
\begin{isabelle} |
|
596 |
(?P\ \isasymLongrightarrow\ False)\ \isasymLongrightarrow\ \isasymnot\ ?P% |
|
597 |
\rulename{notI}\isanewline |
|
598 |
\isasymlbrakk{\isasymnot}\ ?P;\ ?P\isasymrbrakk\ \isasymLongrightarrow\ ?R% |
|
599 |
\rulename{notE} |
|
600 |
\end{isabelle} |
|
601 |
% |
|
602 |
Classical logic allows us to assume $\neg P$ |
|
603 |
when attempting to prove~$P$: |
|
604 |
\begin{isabelle} |
|
605 |
(\isasymnot\ ?P\ \isasymLongrightarrow\ ?P)\ \isasymLongrightarrow\ ?P% |
|
606 |
\rulename{classical} |
|
607 |
\end{isabelle} |
|
608 |
% |
|
609 |
Three further rules are variations on the theme of contrapositive. |
|
610 |
They differ in the placement of the negation symbols: |
|
611 |
\begin{isabelle} |
|
612 |
\isasymlbrakk?Q;\ \isasymnot\ ?P\ \isasymLongrightarrow\ \isasymnot\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P% |
|
613 |
\rulename{contrapos_pp}\isanewline |
|
614 |
\isasymlbrakk{\isasymnot}\ ?Q;\ \isasymnot\ ?P\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P% |
|
615 |
\rulename{contrapos_np}\isanewline |
|
616 |
\isasymlbrakk{\isasymnot}\ ?Q;\ ?P\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ \isasymnot\ ?P% |
|
617 |
\rulename{contrapos_nn} |
|
618 |
\end{isabelle} |
|
619 |
% |
|
620 |
These rules are typically applied using the {\isa{erule}} method, where |
|
621 |
their effect is to form a contrapositive from an |
|
622 |
assumption and the goal's conclusion. |
|
623 |
||
624 |
The most important of these is \isa{contrapos_np}. It is useful |
|
625 |
for applying introduction rules to negated assumptions. For instance, |
|
626 |
the assumption $\neg(P\imp Q)$ is equivalent to the conclusion $P\imp Q$ and we |
|
627 |
might want to use conjunction introduction on it. |
|
628 |
Before we can do so, we must move that assumption so that it |
|
629 |
becomes the conclusion. The following proof demonstrates this |
|
630 |
technique: |
|
631 |
\begin{isabelle} |
|
632 |
\isacommand{lemma}\ "\isasymlbrakk{\isasymnot}(P{\isasymlongrightarrow}Q);\ |
|
633 |
\isasymnot(R{\isasymlongrightarrow}Q)\isasymrbrakk\ \isasymLongrightarrow\ |
|
634 |
R"\isanewline |
|
10971 | 635 |
\isacommand{apply}\ (erule_tac\ Q = "R{\isasymlongrightarrow}Q"\ \isakeyword{in}\ |
10295 | 636 |
contrapos_np)\isanewline |
637 |
\isacommand{apply}\ intro\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
638 |
\isacommand{by}\ (erule\ notE) |
10295 | 639 |
\end{isabelle} |
640 |
% |
|
641 |
There are two negated assumptions and we need to exchange the conclusion with the |
|
642 |
second one. The method \isa{erule contrapos_np} would select the first assumption, |
|
643 |
which we do not want. So we specify the desired assumption explicitly, using |
|
644 |
\isa{erule_tac}. This is the resulting subgoal: |
|
645 |
\begin{isabelle} |
|
646 |
\ 1.\ \isasymlbrakk{\isasymnot}\ (P\ \isasymlongrightarrow\ Q);\ \isasymnot\ |
|
647 |
R\isasymrbrakk\ \isasymLongrightarrow\ R\ \isasymlongrightarrow\ Q% |
|
648 |
\end{isabelle} |
|
649 |
The former conclusion, namely \isa{R}, now appears negated among the assumptions, |
|
650 |
while the negated formula \isa{R\ \isasymlongrightarrow\ Q} becomes the new |
|
651 |
conclusion. |
|
652 |
||
653 |
We can now apply introduction rules. We use the {\isa{intro}} method, which |
|
654 |
repeatedly applies built-in introduction rules. Here its effect is equivalent |
|
10596 | 655 |
to \isa{rule impI}. |
656 |
\begin{isabelle} |
|
10295 | 657 |
\ 1.\ \isasymlbrakk{\isasymnot}\ (P\ \isasymlongrightarrow\ Q);\ \isasymnot\ R;\ |
658 |
R\isasymrbrakk\ \isasymLongrightarrow\ Q% |
|
659 |
\end{isabelle} |
|
660 |
We can see a contradiction in the form of assumptions \isa{\isasymnot\ R} |
|
661 |
and~\isa{R}, which suggests using negation elimination. If applied on its own, |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
662 |
\isa{notE} will select the first negated assumption, which is useless. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
663 |
Instead, we invoke the rule using the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
664 |
\isa{by} command. |
10295 | 665 |
Now when Isabelle selects the first assumption, it tries to prove \isa{P\ |
666 |
\isasymlongrightarrow\ Q} and fails; it then backtracks, finds the |
|
10971 | 667 |
assumption \isa{\isasymnot~R} and finally proves \isa{R} by assumption. That |
10295 | 668 |
concludes the proof. |
669 |
||
670 |
\medskip |
|
671 |
||
672 |
Here is another example. |
|
673 |
\begin{isabelle} |
|
674 |
\isacommand{lemma}\ "(P\ \isasymor\ Q)\ \isasymand\ R\ |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
675 |
\isasymLongrightarrow\ P\ \isasymor\ (Q\ \isasymand\ R)"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
676 |
\isacommand{apply}\ intro\isanewline |
10295 | 677 |
\isacommand{apply}\ (elim\ conjE\ disjE)\isanewline |
678 |
\ \isacommand{apply}\ assumption |
|
679 |
\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
680 |
\isacommand{by}\ (erule\ contrapos_np,\ rule\ conjI) |
10295 | 681 |
\end{isabelle} |
682 |
% |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
683 |
The first proof step applies the {\isa{intro}} method, which repeatedly uses |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
684 |
built-in introduction rules. Here it creates the negative assumption |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
685 |
\hbox{\isa{\isasymnot(Q\ \isasymand\ R)}}. That comes from \isa{disjCI}, a |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
686 |
disjunction introduction rule that combines the effects of |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
687 |
\isa{disjI1} and \isa{disjI2}. |
10295 | 688 |
\begin{isabelle} |
689 |
\ 1.\ \isasymlbrakk(P\ \isasymor\ Q)\ \isasymand\ R;\ \isasymnot\ (Q\ \isasymand\ |
|
690 |
R)\isasymrbrakk\ \isasymLongrightarrow\ P% |
|
691 |
\end{isabelle} |
|
692 |
Next we apply the {\isa{elim}} method, which repeatedly applies |
|
693 |
elimination rules; here, the elimination rules given |
|
10971 | 694 |
in the command. One of the subgoals is trivial (\isa{\isacommand{apply} assumption}), |
695 |
leaving us with one other: |
|
10295 | 696 |
\begin{isabelle} |
697 |
\ 1.\ \isasymlbrakk{\isasymnot}\ (Q\ \isasymand\ R);\ R;\ Q\isasymrbrakk\ \isasymLongrightarrow\ P% |
|
698 |
\end{isabelle} |
|
699 |
% |
|
700 |
Now we must move the formula \isa{Q\ \isasymand\ R} to be the conclusion. The |
|
701 |
combination |
|
702 |
\begin{isabelle} |
|
703 |
\ \ \ \ \ (erule\ contrapos_np,\ rule\ conjI) |
|
704 |
\end{isabelle} |
|
705 |
is robust: the \isa{conjI} forces the \isa{erule} to select a |
|
10301 | 706 |
conjunction. The two subgoals are the ones we would expect from applying |
10295 | 707 |
conjunction introduction to |
10971 | 708 |
\isa{Q~\isasymand~R}: |
10295 | 709 |
\begin{isabelle} |
10596 | 710 |
\ 1.\ \isasymlbrakk R;\ Q;\ \isasymnot\ P\isasymrbrakk\ \isasymLongrightarrow\ |
10295 | 711 |
Q\isanewline |
10596 | 712 |
\ 2.\ \isasymlbrakk R;\ Q;\ \isasymnot\ P\isasymrbrakk\ \isasymLongrightarrow\ R% |
10295 | 713 |
\end{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
714 |
They are proved by assumption, which is implicit in the \isacommand{by} command. |
10295 | 715 |
|
716 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
717 |
\section{Quantifiers} |
10295 | 718 |
|
719 |
Quantifiers require formalizing syntactic substitution and the notion of \textbf{arbitrary |
|
720 |
value}. Consider the universal quantifier. In a logic book, its |
|
721 |
introduction rule looks like this: |
|
722 |
\[ \infer{\forall x.\,P}{P} \] |
|
723 |
Typically, a proviso written in English says that $x$ must not |
|
724 |
occur in the assumptions. This proviso guarantees that $x$ can be regarded as |
|
725 |
arbitrary, since it has not been assumed to satisfy any special conditions. |
|
726 |
Isabelle's underlying formalism, called the |
|
727 |
\textbf{meta-logic}, eliminates the need for English. It provides its own universal |
|
728 |
quantifier (\isasymAnd) to express the notion of an arbitrary value. We have |
|
729 |
already seen another symbol of the meta-logic, namely |
|
730 |
\isa\isasymLongrightarrow, which expresses inference rules and the treatment of |
|
731 |
assumptions. The only other symbol in the meta-logic is \isa\isasymequiv, which |
|
732 |
can be used to define constants. |
|
733 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
734 |
\subsection{The Universal Introduction Rule} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
735 |
|
10295 | 736 |
Returning to the universal quantifier, we find that having a similar quantifier |
737 |
as part of the meta-logic makes the introduction rule trivial to express: |
|
738 |
\begin{isabelle} |
|
10596 | 739 |
(\isasymAnd x.\ ?P\ x)\ \isasymLongrightarrow\ {\isasymforall}x.\ ?P\ x\rulename{allI} |
10295 | 740 |
\end{isabelle} |
741 |
||
742 |
||
743 |
The following trivial proof demonstrates how the universal introduction |
|
744 |
rule works. |
|
745 |
\begin{isabelle} |
|
746 |
\isacommand{lemma}\ "{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ x"\isanewline |
|
747 |
\isacommand{apply}\ (rule\ allI)\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
748 |
\isacommand{by}\ (rule\ impI) |
10295 | 749 |
\end{isabelle} |
750 |
The first step invokes the rule by applying the method \isa{rule allI}. |
|
751 |
\begin{isabelle} |
|
10596 | 752 |
\ 1.\ \isasymAnd x.\ P\ x\ \isasymlongrightarrow\ P\ x |
10295 | 753 |
\end{isabelle} |
754 |
Note that the resulting proof state has a bound variable, |
|
755 |
namely~\bigisa{x}. The rule has replaced the universal quantifier of |
|
756 |
higher-order logic by Isabelle's meta-level quantifier. Our goal is to |
|
757 |
prove |
|
758 |
\isa{P\ x\ \isasymlongrightarrow\ P\ x} for arbitrary~\isa{x}; it is |
|
759 |
an implication, so we apply the corresponding introduction rule (\isa{impI}). |
|
760 |
\begin{isabelle} |
|
10596 | 761 |
\ 1.\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow\ P\ x |
10295 | 762 |
\end{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
763 |
This last subgoal is implicitly proved by assumption. |
10295 | 764 |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
765 |
\subsection{The Universal Elimination Rule} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
766 |
|
10295 | 767 |
Now consider universal elimination. In a logic text, |
768 |
the rule looks like this: |
|
769 |
\[ \infer{P[t/x]}{\forall x.\,P} \] |
|
770 |
The conclusion is $P$ with $t$ substituted for the variable~$x$. |
|
771 |
Isabelle expresses substitution using a function variable: |
|
772 |
\begin{isabelle} |
|
773 |
{\isasymforall}x.\ ?P\ x\ \isasymLongrightarrow\ ?P\ ?x\rulename{spec} |
|
774 |
\end{isabelle} |
|
775 |
This destruction rule takes a |
|
776 |
universally quantified formula and removes the quantifier, replacing |
|
777 |
the bound variable \bigisa{x} by the schematic variable \bigisa{?x}. Recall that a |
|
778 |
schematic variable starts with a question mark and acts as a |
|
779 |
placeholder: it can be replaced by any term. |
|
780 |
||
781 |
To see how this works, let us derive a rule about reducing |
|
782 |
the scope of a universal quantifier. In mathematical notation we write |
|
783 |
\[ \infer{P\imp\forall x.\,Q}{\forall x.\,P\imp Q} \] |
|
10978 | 784 |
with the proviso ``$x$ not free in~$P$.'' Isabelle's treatment of |
10295 | 785 |
substitution makes the proviso unnecessary. The conclusion is expressed as |
786 |
\isa{P\ |
|
787 |
\isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x)}. No substitution for the |
|
788 |
variable \isa{P} can introduce a dependence upon~\isa{x}: that would be a |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
789 |
bound variable capture. Here is the Isabelle proof in full: |
10295 | 790 |
\begin{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
791 |
\isacommand{lemma}\ "(\isasymforall x.\ P\ \isasymlongrightarrow \ Q\ x)\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
792 |
\isasymLongrightarrow \ P\ \isasymlongrightarrow \ (\isasymforall x.\ Q\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
793 |
x)"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
794 |
\isacommand{apply}\ (rule\ impI,\ rule\ allI)\isanewline |
10295 | 795 |
\isacommand{apply}\ (drule\ spec)\isanewline |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
796 |
\isacommand{by}\ (drule\ mp) |
10295 | 797 |
\end{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
798 |
First we apply implies introduction (\isa{impI}), |
10295 | 799 |
which moves the \isa{P} from the conclusion to the assumptions. Then |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
800 |
we apply universal introduction (\isa{allI}). |
10295 | 801 |
\begin{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
802 |
\ 1.\ \isasymAnd x.\ \isasymlbrakk{\isasymforall}x.\ P\ \isasymlongrightarrow\ Q\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
803 |
x;\ P\isasymrbrakk\ \isasymLongrightarrow\ Q\ x |
10295 | 804 |
\end{isabelle} |
805 |
As before, it replaces the HOL |
|
806 |
quantifier by a meta-level quantifier, producing a subgoal that |
|
807 |
binds the variable~\bigisa{x}. The leading bound variables |
|
808 |
(here \isa{x}) and the assumptions (here \isa{{\isasymforall}x.\ P\ |
|
809 |
\isasymlongrightarrow\ Q\ x} and \isa{P}) form the \textbf{context} for the |
|
810 |
conclusion, here \isa{Q\ x}. At each proof step, the subgoals inherit the |
|
811 |
previous context, though some context elements may be added or deleted. |
|
812 |
Applying \isa{erule} deletes an assumption, while many natural deduction |
|
813 |
rules add bound variables or assumptions. |
|
814 |
||
815 |
Now, to reason from the universally quantified |
|
10967 | 816 |
assumption, we apply the elimination rule using the \isa{drule} |
10295 | 817 |
method. This rule is called \isa{spec} because it specializes a universal formula |
818 |
to a particular term. |
|
819 |
\begin{isabelle} |
|
10596 | 820 |
\ 1.\ \isasymAnd x.\ \isasymlbrakk P;\ P\ \isasymlongrightarrow\ Q\ (?x2\ |
821 |
x)\isasymrbrakk\ \isasymLongrightarrow\ Q\ x |
|
10295 | 822 |
\end{isabelle} |
823 |
Observe how the context has changed. The quantified formula is gone, |
|
824 |
replaced by a new assumption derived from its body. Informally, we have |
|
825 |
removed the quantifier. The quantified variable |
|
826 |
has been replaced by the curious term |
|
827 |
\bigisa{?x2~x}; it acts as a placeholder that may be replaced |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
828 |
by any term that can be built from~\bigisa{x}. (Formally, \bigisa{?x2} is an |
10295 | 829 |
unknown of function type, applied to the argument~\bigisa{x}.) This new assumption is |
830 |
an implication, so we can use \emph{modus ponens} on it. As before, it requires |
|
831 |
proving the antecedent (in this case \isa{P}) and leaves us with the consequent. |
|
832 |
\begin{isabelle} |
|
10596 | 833 |
\ 1.\ \isasymAnd x.\ \isasymlbrakk P;\ Q\ (?x2\ x)\isasymrbrakk\ |
10295 | 834 |
\isasymLongrightarrow\ Q\ x |
835 |
\end{isabelle} |
|
836 |
The consequent is \isa{Q} applied to that placeholder. It may be replaced by any |
|
837 |
term built from~\bigisa{x}, and here |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
838 |
it should simply be~\bigisa{x}. The \isa{assumption} method, implicit in the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
839 |
\isacommand{by} command, proves this subgoal. The assumption need not |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
840 |
be identical to the conclusion, provided the two formulas are unifiable. |
10295 | 841 |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
842 |
|
10967 | 843 |
\subsection{Renaming an Assumption: {\tt\slshape rename_tac}} |
844 |
||
845 |
When you apply a rule such as \isa{allI}, the quantified variable becomes a new |
|
846 |
bound variable of the new subgoal. Isabelle tries to avoid changing its name, but |
|
847 |
sometimes it has to choose a new name in order to avoid a clash. Here is a |
|
848 |
contrived example: |
|
849 |
\begin{isabelle} |
|
850 |
\isacommand{lemma}\ "x\ <\ y\ \isasymLongrightarrow \ \isasymforall x\ y.\ P\ x\ |
|
851 |
(f\ y)"\isanewline |
|
852 |
\isacommand{apply}\ intro\isanewline |
|
853 |
\ 1.\ \isasymAnd xa\ ya.\ x\ <\ y\ \isasymLongrightarrow \ P\ xa\ (f\ ya) |
|
854 |
\end{isabelle} |
|
855 |
% |
|
856 |
The names \isa{x} and \isa{y} were already in use, so the new bound variables are |
|
857 |
called \isa{xa} and~\isa{ya}. You can rename them by invoking \isa{rename_tac}: |
|
858 |
||
859 |
\begin{isabelle} |
|
860 |
\isacommand{apply}\ (rename_tac\ v\ w)\isanewline |
|
861 |
\ 1.\ \isasymAnd v\ w.\ x\ <\ y\ \isasymLongrightarrow \ P\ v\ (f\ w) |
|
862 |
\end{isabelle} |
|
10978 | 863 |
Recall that \isa{rule_tac}\index{*rule_tac!and renaming} instantiates a |
10967 | 864 |
theorem with specified terms. These terms may involve the goal's bound |
865 |
variables, but beware of referring to variables |
|
866 |
like~\isa{xa}. A future change to your theories could change the set of names |
|
867 |
produced at top level, so that \isa{xa} changes to~\isa{xb} or reverts to~\isa{x}. |
|
868 |
It is safer to rename automatically-generated variables before mentioning them. |
|
869 |
||
870 |
If the subgoal has more bound variables than there are names given to |
|
871 |
\isa{rename_tac}, the rightmost ones are renamed. |
|
872 |
||
873 |
||
874 |
\subsection{Reusing an Assumption: {\tt\slshape frule}} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
875 |
|
10295 | 876 |
Note that \isa{drule spec} removes the universal quantifier and --- as |
877 |
usual with elimination rules --- discards the original formula. Sometimes, a |
|
878 |
universal formula has to be kept so that it can be used again. Then we use a new |
|
879 |
method: \isa{frule}. It acts like \isa{drule} but copies rather than replaces |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
880 |
the selected assumption. The \isa{f} is for \emph{forward}. |
10295 | 881 |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
882 |
In this example, going from \isa{P\ a} to \isa{P(h(h~a))} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
883 |
requires two uses of the quantified assumption, one for each~\isa{h} being |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
884 |
affixed to the term~\isa{a}. |
10295 | 885 |
\begin{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
886 |
\isacommand{lemma}\ "\isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\ x); |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
887 |
\ P\ a\isasymrbrakk\ \isasymLongrightarrow\ P(h\ (h\ a))"\isanewline |
10295 | 888 |
\isacommand{apply}\ (frule\ spec)\isanewline |
889 |
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline |
|
890 |
\isacommand{apply}\ (drule\ spec)\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
891 |
\isacommand{by}\ (drule\ mp) |
10295 | 892 |
\end{isabelle} |
893 |
% |
|
894 |
Applying \isa{frule\ spec} leaves this subgoal: |
|
895 |
\begin{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
896 |
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\ x);\ P\ a;\ P\ ?x\ \isasymlongrightarrow\ P\ (h\ ?x)\isasymrbrakk\ \isasymLongrightarrow\ P\ (h\ (h\ a)) |
10295 | 897 |
\end{isabelle} |
898 |
It is just what \isa{drule} would have left except that the quantified |
|
899 |
assumption is still present. The next step is to apply \isa{mp} to the |
|
900 |
implication and the assumption \isa{P\ a}, which leaves this subgoal: |
|
901 |
\begin{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
902 |
\ 1.\ \isasymlbrakk{\isasymforall}x.\ P\ x\ \isasymlongrightarrow\ P\ (h\ x);\ P\ a;\ P\ (h\ a)\isasymrbrakk\ \isasymLongrightarrow\ P\ (h\ (h\ a)) |
10295 | 903 |
\end{isabelle} |
904 |
% |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
905 |
We have created the assumption \isa{P(h\ a)}, which is progress. To finish the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
906 |
proof, we apply \isa{spec} one last time, using \isa{drule}. |
10854 | 907 |
|
908 |
\medskip |
|
909 |
\emph{A final remark}. Applying \isa{spec} by the command |
|
10295 | 910 |
\begin{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
911 |
\isacommand{apply}\ (drule\ mp,\ assumption) |
10295 | 912 |
\end{isabelle} |
10854 | 913 |
would not work a second time: it would add a second copy of \isa{P(h~a)} instead |
914 |
of the desired assumption, \isa{P(h(h~a))}. The \isacommand{by} |
|
915 |
command forces Isabelle to backtrack until it finds the correct one. |
|
916 |
Alternatively, we could have used the \isacommand{apply} command and bundled the |
|
917 |
\isa{drule mp} with \emph{two} calls of \isa{assumption}. |
|
10295 | 918 |
|
919 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
920 |
\subsection{The Existential Quantifier} |
10295 | 921 |
|
922 |
The concepts just presented also apply to the existential quantifier, |
|
923 |
whose introduction rule looks like this in Isabelle: |
|
924 |
\begin{isabelle} |
|
925 |
?P\ ?x\ \isasymLongrightarrow\ {\isasymexists}x.\ ?P\ x\rulename{exI} |
|
926 |
\end{isabelle} |
|
927 |
If we can exhibit some $x$ such that $P(x)$ is true, then $\exists x. |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
928 |
P(x)$ is also true. It is a dual of the universal elimination rule, and |
10971 | 929 |
logic texts present it using the same notation for substitution. |
930 |
||
931 |
The existential |
|
10295 | 932 |
elimination rule looks like this |
933 |
in a logic text: |
|
10971 | 934 |
\[ \infer{Q}{\exists x.\,P & \infer*{Q}{[P]}} \] |
10295 | 935 |
% |
936 |
It looks like this in Isabelle: |
|
937 |
\begin{isabelle} |
|
10596 | 938 |
\isasymlbrakk{\isasymexists}x.\ ?P\ x;\ \isasymAnd x.\ ?P\ x\ \isasymLongrightarrow\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?Q\rulename{exE} |
10295 | 939 |
\end{isabelle} |
940 |
% |
|
941 |
Given an existentially quantified theorem and some |
|
942 |
formula $Q$ to prove, it creates a new assumption by removing the quantifier. As with |
|
943 |
the universal introduction rule, the textbook version imposes a proviso on the |
|
944 |
quantified variable, which Isabelle expresses using its meta-logic. Note that it is |
|
945 |
enough to have a universal quantifier in the meta-logic; we do not need an existential |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
946 |
quantifier to be built in as well. |
10295 | 947 |
|
948 |
||
949 |
\begin{exercise} |
|
950 |
Prove the lemma |
|
951 |
\[ \exists x.\, P\conj Q(x)\Imp P\conj(\exists x.\, Q(x)). \] |
|
952 |
\emph{Hint}: the proof is similar |
|
953 |
to the one just above for the universal quantifier. |
|
954 |
\end{exercise} |
|
955 |
||
956 |
||
10887
7fb42b97413a
the \\epsilon character causes font errors in a section title
paulson
parents:
10854
diff
changeset
|
957 |
\section{Hilbert's Epsilon-Operator} |
10971 | 958 |
\label{sec:SOME} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
959 |
|
10971 | 960 |
HOL provides Hilbert's |
961 |
$\varepsilon$-operator. The term $\varepsilon x. P(x)$ denotes some $x$ such that $P(x)$ is |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
962 |
true, provided such a value exists. Using this operator, we can express an |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
963 |
existential destruction rule: |
10971 | 964 |
\[ \infer{P[(\varepsilon x. P) / \, x]}{\exists x.\,P} \] |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
965 |
This rule is seldom used, for it can cause exponential blow-up. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
966 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
967 |
\subsection{Definite Descriptions} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
968 |
|
10983 | 969 |
In \textsc{ascii}, we write \isa{SOME} for $\varepsilon$. |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
970 |
\REMARK{the internal constant is \isa{Eps}} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
971 |
The main use of \hbox{\isa{SOME\ x.\ P\ x}} is as a \textbf{definite |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
972 |
description}: when \isa{P} is satisfied by a unique value,~\isa{a}. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
973 |
We reason using this rule: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
974 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
975 |
\isasymlbrakk P\ a;\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow \ x\ =\ a\isasymrbrakk \ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
976 |
\isasymLongrightarrow \ (SOME\ x.\ P\ x)\ =\ a% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
977 |
\rulename{some_equality} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
978 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
979 |
For instance, we can define the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
980 |
cardinality of a finite set~$A$ to be that |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
981 |
$n$ such that $A$ is in one-to-one correspondence with $\{1,\ldots,n\}$. We can then |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
982 |
prove that the cardinality of the empty set is zero (since $n=0$ satisfies the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
983 |
description) and proceed to prove other facts. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
984 |
|
10971 | 985 |
Here is another example. Isabelle/HOL defines \isa{inv},\indexbold{*inv (constant)} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
986 |
which expresses inverses of functions, as a definite |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
987 |
description: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
988 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
989 |
inv\ f\ \isasymequiv \ \isasymlambda y.\ SOME\ x.\ f\ x\ =\ y% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
990 |
\rulename{inv_def} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
991 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
992 |
The inverse of \isa{f}, when applied to \isa{y}, returns some {x} such that |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
993 |
\isa{f~x~=~y}. For example, we can prove \isa{inv~Suc} really is the inverse |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
994 |
of the \isa{Suc} function |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
995 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
996 |
\isacommand{lemma}\ "inv\ Suc\ (Suc\ x)\ =\ x"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
997 |
\isacommand{by}\ (simp\ add:\ inv_def) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
998 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
999 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1000 |
\noindent |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1001 |
The proof is a one-liner: the subgoal simplifies to a degenerate application of |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1002 |
\isa{SOME}, which is then erased. The definition says nothing about what |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1003 |
\isa{inv~Suc} returns when applied to zero. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1004 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1005 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1006 |
A more challenging example illustrates how Isabelle/HOL defines the least-number |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1007 |
operator, which denotes the least \isa{x} satisfying~\isa{P}: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1008 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1009 |
(LEAST\ x.\ P\ x)\ \isasymequiv \ SOME\ x.\ P\ x\ \isasymand \ (\isasymforall y.\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1010 |
P\ y\ \isasymlongrightarrow \ x\ \isasymle \ y) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1011 |
\rulename{Least_def} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1012 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1013 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1014 |
Let us prove the counterpart of \isa{some_equality} for this operator. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1015 |
The first step merely unfolds the definition: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1016 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1017 |
\isacommand{theorem}\ Least_equality:\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1018 |
\ \ \ \ \ "\isasymlbrakk \ P\ (k::nat);\ \ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ \isasymle \ x\ \isasymrbrakk \ \isasymLongrightarrow \ (LEAST\ x.\ P\ x)\ =\ k"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1019 |
\isacommand{apply}\ (simp\ add:\ Least_def)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1020 |
%\ 1.\ \isasymlbrakk P\ k;\ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1021 |
%\isasymle \ x\isasymrbrakk \isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1022 |
%\ \ \ \ \isasymLongrightarrow \ (SOME\ x.\ P\ x\ \isasymand \ (\isasymforall y.\ P\ y\ \isasymlongrightarrow \ x\ \isasymle \ y))\ =\ k% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1023 |
\isacommand{apply}\ (rule\ some_equality)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1024 |
\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1025 |
\ 1.\ \isasymlbrakk P\ k;\ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1026 |
\isasymle \ x\isasymrbrakk \ \isasymLongrightarrow \ P\ k\ \isasymand \ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1027 |
(\isasymforall y.\ P\ y\ \isasymlongrightarrow \ k\ \isasymle \ y)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1028 |
\ 2.\ \isasymAnd x.\ \isasymlbrakk P\ k;\ \isasymforall x.\ P\ x\ \isasymlongrightarrow \ k\ \isasymle \ x;\ P\ x\ \isasymand \ (\isasymforall y.\ P\ y\ \isasymlongrightarrow \ x\ \isasymle \ y)\isasymrbrakk \isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1029 |
\ \ \ \ \ \ \ \ \isasymLongrightarrow \ x\ =\ k% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1030 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1031 |
As always with \isa{some_equality}, we must show existence and |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1032 |
uniqueness of the claimed solution,~\isa{k}. Existence, the first |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1033 |
subgoal, is trivial. Uniqueness, the second subgoal, follows by antisymmetry: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1034 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1035 |
\isasymlbrakk x\ \isasymle \ y;\ y\ \isasymle \ x\isasymrbrakk \ \isasymLongrightarrow \ x\ =\ y% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1036 |
\rulename{order_antisym} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1037 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1038 |
The assumptions imply both \isa{k~\isasymle~x} and \isa{x~\isasymle~k}. One call |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1039 |
to \isa{auto} does it all: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1040 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1041 |
\isacommand{by}\ (auto\ intro:\ order_antisym) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1042 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1043 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1044 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1045 |
\subsection{Indefinite Descriptions} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1046 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1047 |
Occasionally, \hbox{\isa{SOME\ x.\ P\ x}} serves as an \textbf{indefinite |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1048 |
description}, when it makes an arbitrary selection from the values |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1049 |
satisfying~\isa{P}\@. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1050 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1051 |
P\ x\ \isasymLongrightarrow \ P\ (SOME\ x.\ P\ x) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1052 |
\rulename{someI}\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1053 |
\isasymlbrakk P\ a;\ \isasymAnd x.\ P\ x\ \isasymLongrightarrow \ Q\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1054 |
x\isasymrbrakk \ \isasymLongrightarrow \ Q\ (SOME\ x.\ P\ x) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1055 |
\rulename{someI2} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1056 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1057 |
Rule \isa{someI} is basic (if anything satisfies \isa{P} then so does |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1058 |
\hbox{\isa{SOME\ x.\ P\ x}}). Rule \isa{someI2} is easier to apply in a backward |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1059 |
proof. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1060 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1061 |
\medskip |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1062 |
For example, let us prove the Axiom of Choice: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1063 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1064 |
\isacommand{theorem}\ axiom_of_choice: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1065 |
\ "(\isasymforall x.\ \isasymexists \ y.\ P\ x\ y)\ \isasymLongrightarrow \ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1066 |
\isasymexists f.\ \isasymforall x.\ P\ x\ (f\ x)"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1067 |
\isacommand{apply}\ (rule\ exI,\ rule\ allI)\isanewline |
10971 | 1068 |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1069 |
\ 1.\ \isasymAnd x.\ \isasymforall x.\ \isasymexists y.\ P\ x\ y\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1070 |
\isasymLongrightarrow \ P\ x\ (?f\ x) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1071 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1072 |
% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1073 |
We have applied the introduction rules; now it is time to apply the elimination |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1074 |
rules. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1075 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1076 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1077 |
\isacommand{apply}\ (drule\ spec,\ erule\ exE)\isanewline |
10971 | 1078 |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1079 |
\ 1.\ \isasymAnd x\ y.\ P\ (?x2\ x)\ y\ \isasymLongrightarrow \ P\ x\ (?f\ x) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1080 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1081 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1082 |
\noindent |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1083 |
The rule \isa{someI} automatically instantiates |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1084 |
\isa{f} to \hbox{\isa{\isasymlambda x.\ SOME y.\ P\ x\ y}}, which is the choice |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1085 |
function. It also instantiates \isa{?x2\ x} to \isa{x}. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1086 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1087 |
\isacommand{by}\ (rule\ someI)\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1088 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1089 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1090 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1091 |
\section{Some Proofs That Fail} |
10295 | 1092 |
|
1093 |
Most of the examples in this tutorial involve proving theorems. But not every |
|
1094 |
conjecture is true, and it can be instructive to see how |
|
1095 |
proofs fail. Here we attempt to prove a distributive law involving |
|
1096 |
the existential quantifier and conjunction. |
|
1097 |
\begin{isabelle} |
|
1098 |
\isacommand{lemma}\ "({\isasymexists}x.\ P\ x)\ \isasymand\ ({\isasymexists}x.\ Q\ x)\ \isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x"\isanewline |
|
1099 |
\isacommand{apply}\ (erule\ conjE)\isanewline |
|
1100 |
\isacommand{apply}\ (erule\ exE)\isanewline |
|
1101 |
\isacommand{apply}\ (erule\ exE)\isanewline |
|
1102 |
\isacommand{apply}\ (rule\ exI)\isanewline |
|
1103 |
\isacommand{apply}\ (rule\ conjI)\isanewline |
|
1104 |
\ \isacommand{apply}\ assumption\isanewline |
|
1105 |
\isacommand{oops} |
|
1106 |
\end{isabelle} |
|
1107 |
The first steps are routine. We apply conjunction elimination (\isa{erule |
|
1108 |
conjE}) to split the assumption in two, leaving two existentially quantified |
|
1109 |
assumptions. Applying existential elimination (\isa{erule exE}) removes one of |
|
1110 |
the quantifiers. |
|
1111 |
\begin{isabelle} |
|
1112 |
%({\isasymexists}x.\ P\ x)\ \isasymand\ ({\isasymexists}x.\ Q\ x)\ |
|
1113 |
%\isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x\isanewline |
|
10596 | 1114 |
\ 1.\ \isasymAnd x.\ \isasymlbrakk{\isasymexists}x.\ Q\ x;\ P\ x\isasymrbrakk\ \isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x |
10295 | 1115 |
\end{isabelle} |
1116 |
% |
|
1117 |
When we remove the other quantifier, we get a different bound |
|
1118 |
variable in the subgoal. (The name \isa{xa} is generated automatically.) |
|
1119 |
\begin{isabelle} |
|
10596 | 1120 |
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\ |
10295 | 1121 |
\isasymLongrightarrow\ {\isasymexists}x.\ P\ x\ \isasymand\ Q\ x |
1122 |
\end{isabelle} |
|
1123 |
The proviso of the existential elimination rule has forced the variables to |
|
1124 |
differ: we can hardly expect two arbitrary values to be equal! There is |
|
1125 |
no way to prove this subgoal. Removing the |
|
1126 |
conclusion's existential quantifier yields two |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1127 |
identical placeholders, which can become any term involving the variables \isa{x} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1128 |
and~\isa{xa}. We need one to become \isa{x} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1129 |
and the other to become~\isa{xa}, but Isabelle requires all instances of a |
10295 | 1130 |
placeholder to be identical. |
1131 |
\begin{isabelle} |
|
10596 | 1132 |
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\ |
10295 | 1133 |
\isasymLongrightarrow\ P\ (?x3\ x\ xa)\isanewline |
10596 | 1134 |
\ 2.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\ \isasymLongrightarrow\ Q\ (?x3\ x\ xa) |
10295 | 1135 |
\end{isabelle} |
1136 |
We can prove either subgoal |
|
1137 |
using the \isa{assumption} method. If we prove the first one, the placeholder |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1138 |
changes into~\isa{x}. |
10295 | 1139 |
\begin{isabelle} |
10596 | 1140 |
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk P\ x;\ Q\ xa\isasymrbrakk\ |
10295 | 1141 |
\isasymLongrightarrow\ Q\ x |
1142 |
\end{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1143 |
We are left with a subgoal that cannot be proved. Applying the \isa{assumption} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1144 |
method results in an error message: |
10295 | 1145 |
\begin{isabelle} |
1146 |
*** empty result sequence -- proof command failed |
|
1147 |
\end{isabelle} |
|
10971 | 1148 |
If we interact with Isabelle via the shell interface, |
1149 |
we abandon a failed proof with the \isacommand{oops} command. |
|
10295 | 1150 |
|
1151 |
\medskip |
|
1152 |
||
1153 |
Here is another abortive proof, illustrating the interaction between |
|
1154 |
bound variables and unknowns. |
|
1155 |
If $R$ is a reflexive relation, |
|
1156 |
is there an $x$ such that $R\,x\,y$ holds for all $y$? Let us see what happens when |
|
1157 |
we attempt to prove it. |
|
1158 |
\begin{isabelle} |
|
1159 |
\isacommand{lemma}\ "{\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ |
|
1160 |
{\isasymexists}x.\ {\isasymforall}y.\ R\ x\ y"\isanewline |
|
1161 |
\isacommand{apply}\ (rule\ exI)\isanewline |
|
1162 |
\isacommand{apply}\ (rule\ allI)\isanewline |
|
1163 |
\isacommand{apply}\ (drule\ spec)\isanewline |
|
1164 |
\isacommand{oops} |
|
1165 |
\end{isabelle} |
|
1166 |
First, |
|
1167 |
we remove the existential quantifier. The new proof state has |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1168 |
an unknown, namely~\isa{?x}. |
10295 | 1169 |
\begin{isabelle} |
1170 |
%{\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ {\isasymexists}x.\ |
|
1171 |
%{\isasymforall}y.\ R\ x\ y\isanewline |
|
1172 |
\ 1.\ {\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ {\isasymforall}y.\ R\ ?x\ y |
|
1173 |
\end{isabelle} |
|
1174 |
Next, we remove the universal quantifier |
|
1175 |
from the conclusion, putting the bound variable~\isa{y} into the subgoal. |
|
1176 |
\begin{isabelle} |
|
10596 | 1177 |
\ 1.\ \isasymAnd y.\ {\isasymforall}z.\ R\ z\ z\ \isasymLongrightarrow\ R\ ?x\ y |
10295 | 1178 |
\end{isabelle} |
1179 |
Finally, we try to apply our reflexivity assumption. We obtain a |
|
1180 |
new assumption whose identical placeholders may be replaced by |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1181 |
any term involving~\isa{y}. |
10295 | 1182 |
\begin{isabelle} |
10596 | 1183 |
\ 1.\ \isasymAnd y.\ R\ (?z2\ y)\ (?z2\ y)\ \isasymLongrightarrow\ R\ ?x\ y |
10295 | 1184 |
\end{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1185 |
This subgoal can only be proved by putting \isa{y} for all the placeholders, |
10295 | 1186 |
making the assumption and conclusion become \isa{R\ y\ y}. |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1187 |
But Isabelle refuses to substitute \isa{y}, a bound variable, for |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1188 |
\isa{?x}; that would be a bound variable capture. The proof fails. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1189 |
Note that Isabelle can replace \isa{?z2~y} by \isa{y}; this involves |
10295 | 1190 |
instantiating |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1191 |
\isa{?z2} to the identity function. |
10295 | 1192 |
|
1193 |
This example is typical of how Isabelle enforces sound quantifier reasoning. |
|
1194 |
||
1195 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1196 |
\section{Proving Theorems Using the {\tt\slshape blast} Method} |
10295 | 1197 |
|
1198 |
It is hard to prove substantial theorems using the methods |
|
1199 |
described above. A proof may be dozens or hundreds of steps long. You |
|
1200 |
may need to search among different ways of proving certain |
|
1201 |
subgoals. Often a choice that proves one subgoal renders another |
|
1202 |
impossible to prove. There are further complications that we have not |
|
1203 |
discussed, concerning negation and disjunction. Isabelle's |
|
1204 |
\textbf{classical reasoner} is a family of tools that perform such |
|
1205 |
proofs automatically. The most important of these is the |
|
10596 | 1206 |
\isa{blast} method. |
10295 | 1207 |
|
1208 |
In this section, we shall first see how to use the classical |
|
1209 |
reasoner in its default mode and then how to insert additional |
|
1210 |
rules, enabling it to work in new problem domains. |
|
1211 |
||
1212 |
We begin with examples from pure predicate logic. The following |
|
1213 |
example is known as Andrew's challenge. Peter Andrews designed |
|
1214 |
it to be hard to prove by automatic means.% |
|
1215 |
\footnote{Pelletier~\cite{pelletier86} describes it and many other |
|
1216 |
problems for automatic theorem provers.} |
|
1217 |
The nested biconditionals cause an exponential explosion: the formal |
|
10596 | 1218 |
proof is enormous. However, the \isa{blast} method proves it in |
10295 | 1219 |
a fraction of a second. |
1220 |
\begin{isabelle} |
|
1221 |
\isacommand{lemma}\ |
|
1222 |
"(({\isasymexists}x.\ |
|
1223 |
{\isasymforall}y.\ |
|
10301 | 1224 |
p(x){=}p(y))\ |
10295 | 1225 |
=\ |
1226 |
(({\isasymexists}x.\ |
|
10301 | 1227 |
q(x))=({\isasymforall}y.\ |
1228 |
p(y))))\ |
|
10295 | 1229 |
\ \ =\ \ \ \ \isanewline |
1230 |
\ \ \ \ \ \ \ \ |
|
1231 |
(({\isasymexists}x.\ |
|
1232 |
{\isasymforall}y.\ |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1233 |
q(x){=}q(y))\ =\ (({\isasymexists}x.\ p(x))=({\isasymforall}y.\ q(y))))"\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1234 |
\isacommand{by}\ blast |
10295 | 1235 |
\end{isabelle} |
1236 |
The next example is a logic problem composed by Lewis Carroll. |
|
10596 | 1237 |
The \isa{blast} method finds it trivial. Moreover, it turns out |
10295 | 1238 |
that not all of the assumptions are necessary. We can easily |
1239 |
experiment with variations of this formula and see which ones |
|
1240 |
can be proved. |
|
1241 |
\begin{isabelle} |
|
1242 |
\isacommand{lemma}\ |
|
1243 |
"({\isasymforall}x.\ |
|
1244 |
honest(x)\ \isasymand\ |
|
1245 |
industrious(x)\ \isasymlongrightarrow\ |
|
10301 | 1246 |
healthy(x))\ |
10295 | 1247 |
\isasymand\ \ \isanewline |
1248 |
\ \ \ \ \ \ \ \ \isasymnot\ ({\isasymexists}x.\ |
|
1249 |
grocer(x)\ \isasymand\ |
|
10301 | 1250 |
healthy(x))\ |
10295 | 1251 |
\isasymand\ \isanewline |
1252 |
\ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
|
1253 |
industrious(x)\ \isasymand\ |
|
1254 |
grocer(x)\ \isasymlongrightarrow\ |
|
10301 | 1255 |
honest(x))\ |
10295 | 1256 |
\isasymand\ \isanewline |
1257 |
\ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
|
1258 |
cyclist(x)\ \isasymlongrightarrow\ |
|
10301 | 1259 |
industrious(x))\ |
10295 | 1260 |
\isasymand\ \isanewline |
1261 |
\ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
|
1262 |
{\isasymnot}healthy(x)\ \isasymand\ |
|
1263 |
cyclist(x)\ \isasymlongrightarrow\ |
|
10301 | 1264 |
{\isasymnot}honest(x))\ |
10295 | 1265 |
\ \isanewline |
1266 |
\ \ \ \ \ \ \ \ \isasymlongrightarrow\ |
|
1267 |
({\isasymforall}x.\ |
|
1268 |
grocer(x)\ \isasymlongrightarrow\ |
|
10301 | 1269 |
{\isasymnot}cyclist(x))"\isanewline |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1270 |
\isacommand{by}\ blast |
10295 | 1271 |
\end{isabelle} |
10596 | 1272 |
The \isa{blast} method is also effective for set theory, which is |
10295 | 1273 |
described in the next chapter. This formula below may look horrible, but |
1274 |
the \isa{blast} method proves it easily. |
|
1275 |
\begin{isabelle} |
|
10301 | 1276 |
\isacommand{lemma}\ "({\isasymUnion}i{\isasymin}I.\ A(i))\ \isasyminter\ ({\isasymUnion}j{\isasymin}J.\ B(j))\ =\isanewline |
1277 |
\ \ \ \ \ \ \ \ ({\isasymUnion}i{\isasymin}I.\ {\isasymUnion}j{\isasymin}J.\ A(i)\ \isasyminter\ B(j))"\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1278 |
\isacommand{by}\ blast |
10295 | 1279 |
\end{isabelle} |
1280 |
||
1281 |
Few subgoals are couched purely in predicate logic and set theory. |
|
1282 |
We can extend the scope of the classical reasoner by giving it new rules. |
|
1283 |
Extending it effectively requires understanding the notions of |
|
1284 |
introduction, elimination and destruction rules. Moreover, there is a |
|
1285 |
distinction between safe and unsafe rules. A \textbf{safe} rule is one |
|
1286 |
that can be applied backwards without losing information; an |
|
1287 |
\textbf{unsafe} rule loses information, perhaps transforming the subgoal |
|
1288 |
into one that cannot be proved. The safe/unsafe |
|
1289 |
distinction affects the proof search: if a proof attempt fails, the |
|
1290 |
classical reasoner backtracks to the most recent unsafe rule application |
|
1291 |
and makes another choice. |
|
1292 |
||
1293 |
An important special case avoids all these complications. A logical |
|
1294 |
equivalence, which in higher-order logic is an equality between |
|
1295 |
formulas, can be given to the classical |
|
10596 | 1296 |
reasoner and simplifier by using the attribute \isa{iff}. You |
10295 | 1297 |
should do so if the right hand side of the equivalence is |
1298 |
simpler than the left-hand side. |
|
1299 |
||
1300 |
For example, here is a simple fact about list concatenation. |
|
1301 |
The result of appending two lists is empty if and only if both |
|
1302 |
of the lists are themselves empty. Obviously, applying this equivalence |
|
1303 |
will result in a simpler goal. When stating this lemma, we include |
|
10596 | 1304 |
the \isa{iff} attribute. Once we have proved the lemma, Isabelle |
10295 | 1305 |
will make it known to the classical reasoner (and to the simplifier). |
1306 |
\begin{isabelle} |
|
1307 |
\isacommand{lemma}\ |
|
10854 | 1308 |
[iff]:\ "(xs{\isacharat}ys\ =\ [])\ =\ |
10971 | 1309 |
(xs=[]\ \isasymand\ ys=[])"\isanewline |
10854 | 1310 |
\isacommand{apply}\ (induct_tac\ xs)\isanewline |
1311 |
\isacommand{apply}\ (simp_all)\isanewline |
|
10295 | 1312 |
\isacommand{done} |
1313 |
\end{isabelle} |
|
1314 |
% |
|
1315 |
This fact about multiplication is also appropriate for |
|
10854 | 1316 |
the \isa{iff} attribute:\REMARK{the ?s are ugly here but we need |
1317 |
them again when talking about \isa{of}; we need a consistent style} |
|
10295 | 1318 |
\begin{isabelle} |
10596 | 1319 |
(\mbox{?m}\ *\ \mbox{?n}\ =\ 0)\ =\ (\mbox{?m}\ =\ 0\ \isasymor\ \mbox{?n}\ =\ 0) |
10295 | 1320 |
\end{isabelle} |
1321 |
A product is zero if and only if one of the factors is zero. The |
|
10971 | 1322 |
reasoning involves a disjunction. Proving new rules for |
10295 | 1323 |
disjunctive reasoning is hard, but translating to an actual disjunction |
1324 |
works: the classical reasoner handles disjunction properly. |
|
1325 |
||
10596 | 1326 |
In more detail, this is how the \isa{iff} attribute works. It converts |
10295 | 1327 |
the equivalence $P=Q$ to a pair of rules: the introduction |
1328 |
rule $Q\Imp P$ and the destruction rule $P\Imp Q$. It gives both to the |
|
1329 |
classical reasoner as safe rules, ensuring that all occurrences of $P$ in |
|
1330 |
a subgoal are replaced by~$Q$. The simplifier performs the same |
|
1331 |
replacement, since \isa{iff} gives $P=Q$ to the |
|
1332 |
simplifier. But classical reasoning is different from |
|
1333 |
simplification. Simplification is deterministic: it applies rewrite rules |
|
1334 |
repeatedly, as long as possible, in order to \emph{transform} a goal. Classical |
|
1335 |
reasoning uses search and backtracking in order to \emph{prove} a goal. |
|
1336 |
||
1337 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1338 |
\section{Proving the Correctness of Euclid's Algorithm} |
10295 | 1339 |
\label{sec:proving-euclid} |
1340 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1341 |
A brief development will illustrate the advanced use of |
10854 | 1342 |
\isa{blast}. We shall also see \isa{case_tac} used to perform a Boolean |
1343 |
case split. |
|
1344 |
||
1345 |
In \S\ref{sec:recdef-simplification}, we declared the |
|
10596 | 1346 |
recursive function \isa{gcd}: |
10295 | 1347 |
\begin{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1348 |
\isacommand{consts}\ gcd\ ::\ "nat*nat\ \isasymRightarrow\ nat"\isanewline |
10301 | 1349 |
\isacommand{recdef}\ gcd\ "measure\ ((\isasymlambda(m,n).n)\ |
10596 | 1350 |
::nat*nat\ \isasymRightarrow\ nat)"\isanewline |
10301 | 1351 |
\ \ \ \ "gcd\ (m,n)\ =\ (if\ n=0\ then\ m\ else\ gcd(n,\ m\ mod\ n))" |
10295 | 1352 |
\end{isabelle} |
1353 |
Let us prove that it computes the greatest common |
|
1354 |
divisor of its two arguments. |
|
1355 |
The theorem is expressed in terms of the familiar |
|
1356 |
\textbf{divides} relation from number theory: |
|
1357 |
\begin{isabelle} |
|
10596 | 1358 |
?m\ dvd\ ?n\ \isasymequiv\ {\isasymexists}k.\ ?n\ =\ ?m\ *\ k |
10295 | 1359 |
\rulename{dvd_def} |
1360 |
\end{isabelle} |
|
1361 |
% |
|
1362 |
A simple induction proves the theorem. Here \isa{gcd.induct} refers to the |
|
1363 |
induction rule returned by \isa{recdef}. The proof relies on the simplification |
|
1364 |
rules proved in \S\ref{sec:recdef-simplification}, since rewriting by the |
|
1365 |
definition of \isa{gcd} can cause looping. |
|
1366 |
\begin{isabelle} |
|
10301 | 1367 |
\isacommand{lemma}\ gcd_dvd_both:\ "(gcd(m,n)\ dvd\ m)\ \isasymand\ (gcd(m,n)\ dvd\ n)"\isanewline |
1368 |
\isacommand{apply}\ (induct_tac\ m\ n\ rule:\ gcd.induct)\isanewline |
|
1369 |
\isacommand{apply}\ (case_tac\ "n=0")\isanewline |
|
10295 | 1370 |
\isacommand{apply}\ (simp_all)\isanewline |
1371 |
\isacommand{apply}\ (blast\ dest:\ dvd_mod_imp_dvd)\isanewline |
|
1372 |
\isacommand{done}% |
|
1373 |
\end{isabelle} |
|
1374 |
Notice that the induction formula |
|
1375 |
is a conjunction. This is necessary: in the inductive step, each |
|
1376 |
half of the conjunction establishes the other. The first three proof steps |
|
1377 |
are applying induction, performing a case analysis on \isa{n}, |
|
10854 | 1378 |
and simplifying. Let us pass over these quickly --- we shall discuss |
1379 |
\isa{case_tac} below --- and consider the use of \isa{blast}. We have reached the |
|
1380 |
following subgoal: |
|
10295 | 1381 |
\begin{isabelle} |
1382 |
%gcd\ (m,\ n)\ dvd\ m\ \isasymand\ gcd\ (m,\ n)\ dvd\ n\isanewline |
|
10596 | 1383 |
\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk0\ \isacharless\ n;\isanewline |
1384 |
\ \ \ \ \ \ \ \ \ \ \ \ gcd\ (n,\ m\ mod\ n)\ dvd\ n\ \isasymand\ gcd\ (n,\ m\ mod\ n)\ dvd\ (m\ mod\ n)\isasymrbrakk\isanewline |
|
10546 | 1385 |
\ \ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow\ gcd\ (n,\ m\ mod\ n)\ dvd\ m |
10295 | 1386 |
\end{isabelle} |
1387 |
% |
|
1388 |
One of the assumptions, the induction hypothesis, is a conjunction. |
|
1389 |
The two divides relationships it asserts are enough to prove |
|
1390 |
the conclusion, for we have the following theorem at our disposal: |
|
1391 |
\begin{isabelle} |
|
1392 |
\isasymlbrakk?k\ dvd\ (?m\ mod\ ?n){;}\ ?k\ dvd\ ?n\isasymrbrakk\ \isasymLongrightarrow\ ?k\ dvd\ ?m% |
|
1393 |
\rulename{dvd_mod_imp_dvd} |
|
1394 |
\end{isabelle} |
|
1395 |
% |
|
1396 |
This theorem can be applied in various ways. As an introduction rule, it |
|
1397 |
would cause backward chaining from the conclusion (namely |
|
10854 | 1398 |
\isa{?k~dvd~?m}) to the two premises, which |
10295 | 1399 |
also involve the divides relation. This process does not look promising |
1400 |
and could easily loop. More sensible is to apply the rule in the forward |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1401 |
direction; each step would eliminate an occurrence of the \isa{mod} symbol, so the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1402 |
process must terminate. |
10295 | 1403 |
|
1404 |
So the final proof step applies the \isa{blast} method. |
|
1405 |
Attaching the {\isa{dest}} attribute to \isa{dvd_mod_imp_dvd} tells \isa{blast} |
|
1406 |
to use it as destruction rule: in the forward direction. |
|
1407 |
||
1408 |
\medskip |
|
1409 |
We have proved a conjunction. Now, let us give names to each of the |
|
1410 |
two halves: |
|
1411 |
\begin{isabelle} |
|
1412 |
\isacommand{lemmas}\ gcd_dvd1\ [iff]\ =\ gcd_dvd_both\ [THEN\ conjunct1]\isanewline |
|
1413 |
\isacommand{lemmas}\ gcd_dvd2\ [iff]\ =\ gcd_dvd_both\ [THEN\ conjunct2]% |
|
1414 |
\end{isabelle} |
|
1415 |
||
1416 |
Several things are happening here. The keyword \isacommand{lemmas} |
|
1417 |
tells Isabelle to transform a theorem in some way and to |
|
1418 |
give a name to the resulting theorem. Attributes can be given, |
|
1419 |
here \isa{iff}, which supplies the new theorems to the classical reasoner |
|
10854 | 1420 |
and the simplifier. The directive \isa{THEN}, described in |
1421 |
\S\ref{sec:forward} below, supplies the lemma |
|
10295 | 1422 |
\isa{gcd_dvd_both} to the |
10854 | 1423 |
destruction rule \isa{conjunct1}. The effect is to extract the first part: |
10295 | 1424 |
\begin{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1425 |
\ \ \ \ \ gcd\ (?m1,\ ?n1)\ dvd\ ?m1 |
10295 | 1426 |
\end{isabelle} |
1427 |
The variable names \isa{?m1} and \isa{?n1} arise because |
|
1428 |
Isabelle renames schematic variables to prevent |
|
1429 |
clashes. The second \isacommand{lemmas} declaration yields |
|
1430 |
\begin{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1431 |
\ \ \ \ \ gcd\ (?m1,\ ?n1)\ dvd\ ?n1 |
10295 | 1432 |
\end{isabelle} |
1433 |
||
10854 | 1434 |
\medskip |
10596 | 1435 |
To complete the verification of the \isa{gcd} function, we must |
10295 | 1436 |
prove that it returns the greatest of all the common divisors |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1437 |
of its arguments. The proof is by induction, case analysis and simplification. |
10295 | 1438 |
\begin{isabelle} |
1439 |
\isacommand{lemma}\ gcd_greatest\ |
|
10301 | 1440 |
[rule_format]:\isanewline |
10792 | 1441 |
\ \ \ \ \ \ \ "k\ dvd\ m\ \isasymlongrightarrow\ k\ dvd\ n\ \isasymlongrightarrow\ |
1442 |
k\ dvd\ gcd(m,n)"\isanewline |
|
10854 | 1443 |
\isacommand{apply}\ (induct_tac\ m\ n\ rule:\ gcd.induct)\isanewline |
10301 | 1444 |
\isacommand{apply}\ (case_tac\ "n=0")\isanewline |
10295 | 1445 |
\isacommand{apply}\ (simp_all\ add:\ gcd_non_0\ dvd_mod)\isanewline |
1446 |
\isacommand{done} |
|
1447 |
\end{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1448 |
|
10854 | 1449 |
The case analysis is performed by \isa{case_tac~"n=0"}: the operand has type |
1450 |
\isa{bool}. Until now, we have used \isa{case_tac} only with datatypes, and since |
|
1451 |
\isa{nat} is a datatype, we could have written |
|
1452 |
\isa{case_tac~"n"} with a similar effect. However, the definition of \isa{gcd} |
|
1453 |
makes a Boolean decision: |
|
1454 |
\begin{isabelle} |
|
1455 |
\ \ \ \ "gcd\ (m,n)\ =\ (if\ n=0\ then\ m\ else\ gcd(n,\ m\ mod\ n))" |
|
1456 |
\end{isabelle} |
|
1457 |
Proofs about a function frequently follow the function's definition, so we perform |
|
1458 |
case analysis over the same formula. |
|
1459 |
||
1460 |
\begingroup\samepage |
|
1461 |
\begin{isabelle} |
|
1462 |
\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk n\ \isasymnoteq \ 0\ \isasymlongrightarrow \isanewline |
|
1463 |
\ \ \ \ \ \ \ \ \ \ \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ m\ mod\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ (n,\ m\ mod\ n);\isanewline |
|
1464 |
\ \ \ \ \ \ \ \ \ \ \ \ \ n\ =\ 0\isasymrbrakk \isanewline |
|
1465 |
\ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ k\ dvd\ m\ \isasymlongrightarrow \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ (m,\ n) |
|
1466 |
\pagebreak[0]\isanewline |
|
1467 |
\ 2.\ \isasymAnd m\ n.\ \isasymlbrakk n\ \isasymnoteq \ 0\ \isasymlongrightarrow \isanewline |
|
1468 |
\ \ \ \ \ \ \ \ \ \ \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ m\ mod\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ (n,\ m\ mod\ n);\isanewline |
|
1469 |
\ \ \ \ \ \ \ \ \ \ \ \ \ n\ \isasymnoteq \ 0\isasymrbrakk \isanewline |
|
1470 |
\ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ k\ dvd\ m\ \isasymlongrightarrow \ k\ dvd\ n\ \isasymlongrightarrow \ k\ dvd\ gcd\ (m,\ n) |
|
1471 |
\end{isabelle} |
|
1472 |
\endgroup |
|
1473 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1474 |
\begin{warn} |
10295 | 1475 |
Note that the theorem has been expressed using HOL implication, |
1476 |
\isa{\isasymlongrightarrow}, because the induction affects the two |
|
1477 |
preconditions. The directive \isa{rule_format} tells Isabelle to replace |
|
1478 |
each \isa{\isasymlongrightarrow} by \isa{\isasymLongrightarrow} before |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1479 |
storing the theorem we have proved. This directive can also remove outer |
10295 | 1480 |
universal quantifiers, converting a theorem into the usual format for |
10854 | 1481 |
inference rules. (See \S\ref{sec:forward}.) |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1482 |
\end{warn} |
10295 | 1483 |
|
1484 |
The facts proved above can be summarized as a single logical |
|
1485 |
equivalence. This step gives us a chance to see another application |
|
1486 |
of \isa{blast}, and it is worth doing for sound logical reasons. |
|
1487 |
\begin{isabelle} |
|
10301 | 1488 |
\isacommand{theorem}\ gcd_greatest_iff\ [iff]:\isanewline |
10792 | 1489 |
\ \ \ \ \ \ \ \ \ "(k\ dvd\ gcd(m,n))\ =\ (k\ dvd\ m\ \isasymand\ k\ dvd\ |
1490 |
n)"\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1491 |
\isacommand{by}\ (blast\ intro!:\ gcd_greatest\ intro:\ dvd_trans) |
10295 | 1492 |
\end{isabelle} |
10596 | 1493 |
This theorem concisely expresses the correctness of the \isa{gcd} |
10295 | 1494 |
function. |
10596 | 1495 |
We state it with the \isa{iff} attribute so that |
1496 |
Isabelle can use it to remove some occurrences of \isa{gcd}. |
|
10295 | 1497 |
The theorem has a one-line |
10971 | 1498 |
proof using \isa{blast} supplied with two additional introduction |
1499 |
rules. The exclamation mark |
|
10295 | 1500 |
({\isa{intro}}{\isa{!}})\ signifies safe rules, which are |
1501 |
applied aggressively. Rules given without the exclamation mark |
|
1502 |
are applied reluctantly and their uses can be undone if |
|
1503 |
the search backtracks. Here the unsafe rule expresses transitivity |
|
1504 |
of the divides relation: |
|
1505 |
\begin{isabelle} |
|
1506 |
\isasymlbrakk?m\ dvd\ ?n;\ ?n\ dvd\ ?p\isasymrbrakk\ \isasymLongrightarrow\ ?m\ dvd\ ?p% |
|
1507 |
\rulename{dvd_trans} |
|
1508 |
\end{isabelle} |
|
1509 |
Applying \isa{dvd_trans} as |
|
1510 |
an introduction rule entails a risk of looping, for it multiplies |
|
1511 |
occurrences of the divides symbol. However, this proof relies |
|
1512 |
on transitivity reasoning. The rule {\isa{gcd\_greatest}} is safe to apply |
|
1513 |
aggressively because it yields simpler subgoals. The proof implicitly |
|
1514 |
uses \isa{gcd_dvd1} and \isa{gcd_dvd2} as safe rules, because they were |
|
1515 |
declared using \isa{iff}. |
|
1516 |
||
1517 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1518 |
\section{Other Classical Reasoning Methods} |
10295 | 1519 |
|
10596 | 1520 |
The \isa{blast} method is our main workhorse for proving theorems |
10295 | 1521 |
automatically. Other components of the classical reasoner interact |
1522 |
with the simplifier. Still others perform classical reasoning |
|
1523 |
to a limited extent, giving the user fine control over the proof. |
|
1524 |
||
1525 |
Of the latter methods, the most useful is {\isa{clarify}}. It performs |
|
1526 |
all obvious reasoning steps without splitting the goal into multiple |
|
10971 | 1527 |
parts. It does not apply unsafe rules that could render the |
1528 |
goal unprovable. By performing the obvious |
|
10295 | 1529 |
steps, {\isa{clarify}} lays bare the difficult parts of the problem, |
1530 |
where human intervention is necessary. |
|
1531 |
||
1532 |
For example, the following conjecture is false: |
|
1533 |
\begin{isabelle} |
|
1534 |
\isacommand{lemma}\ "({\isasymforall}x.\ P\ x)\ \isasymand\ |
|
1535 |
({\isasymexists}x.\ Q\ x)\ \isasymlongrightarrow\ ({\isasymforall}x.\ P\ x\ |
|
1536 |
\isasymand\ Q\ x)"\isanewline |
|
1537 |
\isacommand{apply}\ clarify |
|
1538 |
\end{isabelle} |
|
10596 | 1539 |
The \isa{blast} method would simply fail, but {\isa{clarify}} presents |
10295 | 1540 |
a subgoal that helps us see why we cannot continue the proof. |
1541 |
\begin{isabelle} |
|
10596 | 1542 |
\ 1.\ \isasymAnd x\ xa.\ \isasymlbrakk{\isasymforall}x.\ P\ x;\ Q\ |
10295 | 1543 |
xa\isasymrbrakk\ \isasymLongrightarrow\ P\ x\ \isasymand\ Q\ x |
1544 |
\end{isabelle} |
|
1545 |
The proof must fail because the assumption \isa{Q\ xa} and conclusion \isa{Q\ x} |
|
1546 |
refer to distinct bound variables. To reach this state, \isa{clarify} applied |
|
1547 |
the introduction rules for \isa{\isasymlongrightarrow} and \isa{\isasymforall} |
|
1548 |
and the elimination rule for ~\isa{\isasymand}. It did not apply the introduction |
|
1549 |
rule for \isa{\isasymand} because of its policy never to split goals. |
|
1550 |
||
1551 |
Also available is {\isa{clarsimp}}, a method that interleaves {\isa{clarify}} |
|
1552 |
and {\isa{simp}}. Also there is \isa{safe}, which like \isa{clarify} performs |
|
1553 |
obvious steps and even applies those that split goals. |
|
1554 |
||
10546 | 1555 |
The \isa{force} method applies the classical reasoner and simplifier |
10295 | 1556 |
to one goal. |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1557 |
%% \REMARK{example needed of \isa{force}?} |
10295 | 1558 |
Unless it can prove the goal, it fails. Contrast |
10546 | 1559 |
that with the \isa{auto} method, which also combines classical reasoning |
10295 | 1560 |
with simplification. The latter's purpose is to prove all the |
1561 |
easy subgoals and parts of subgoals. Unfortunately, it can produce |
|
1562 |
large numbers of new subgoals; also, since it proves some subgoals |
|
1563 |
and splits others, it obscures the structure of the proof tree. |
|
10546 | 1564 |
The \isa{force} method does not have these drawbacks. Another |
1565 |
difference: \isa{force} tries harder than {\isa{auto}} to prove |
|
10295 | 1566 |
its goal, so it can take much longer to terminate. |
1567 |
||
1568 |
Older components of the classical reasoner have largely been |
|
10596 | 1569 |
superseded by \isa{blast}, but they still have niche applications. |
1570 |
Most important among these are \isa{fast} and \isa{best}. While \isa{blast} |
|
10295 | 1571 |
searches for proofs using a built-in first-order reasoner, these |
1572 |
earlier methods search for proofs using standard Isabelle inference. |
|
1573 |
That makes them slower but enables them to work correctly in the |
|
1574 |
presence of the more unusual features of Isabelle rules, such |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1575 |
as type classes and function unknowns. For example, recall the introduction rule |
10971 | 1576 |
for Hilbert's $\varepsilon$-operator: |
10295 | 1577 |
\begin{isabelle} |
10546 | 1578 |
?P\ ?x\ \isasymLongrightarrow\ ?P\ (SOME\ x.\ ?P x) |
10295 | 1579 |
\rulename{someI} |
1580 |
\end{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1581 |
% |
10295 | 1582 |
The repeated occurrence of the variable \isa{?P} makes this rule tricky |
1583 |
to apply. Consider this contrived example: |
|
1584 |
\begin{isabelle} |
|
10596 | 1585 |
\isacommand{lemma}\ "\isasymlbrakk Q\ a;\ P\ a\isasymrbrakk\isanewline |
10295 | 1586 |
\ \ \ \ \ \ \ \ \,\isasymLongrightarrow\ P\ (SOME\ x.\ P\ x\ \isasymand\ Q\ x)\ |
1587 |
\isasymand\ Q\ (SOME\ x.\ P\ x\ \isasymand\ Q\ x)"\isanewline |
|
1588 |
\isacommand{apply}\ (rule\ someI) |
|
1589 |
\end{isabelle} |
|
1590 |
% |
|
1591 |
We can apply rule \isa{someI} explicitly. It yields the |
|
1592 |
following subgoal: |
|
1593 |
\begin{isabelle} |
|
10596 | 1594 |
\ 1.\ \isasymlbrakk Q\ a;\ P\ a\isasymrbrakk\ \isasymLongrightarrow\ P\ ?x\ |
10295 | 1595 |
\isasymand\ Q\ ?x% |
1596 |
\end{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1597 |
The proof from this point is trivial. Could we have |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1598 |
proved the theorem with a single command? Not using \isa{blast}: it |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1599 |
cannot perform the higher-order unification needed here. The |
10596 | 1600 |
\isa{fast} method succeeds: |
10295 | 1601 |
\begin{isabelle} |
1602 |
\isacommand{apply}\ (fast\ intro!:\ someI) |
|
1603 |
\end{isabelle} |
|
1604 |
||
10596 | 1605 |
The \isa{best} method is similar to \isa{fast} but it uses a |
10295 | 1606 |
best-first search instead of depth-first search. Accordingly, |
1607 |
it is slower but is less susceptible to divergence. Transitivity |
|
10596 | 1608 |
rules usually cause \isa{fast} to loop where often \isa{best} |
10295 | 1609 |
can manage. |
1610 |
||
1611 |
Here is a summary of the classical reasoning methods: |
|
1612 |
\begin{itemize} |
|
1613 |
\item \isa{blast} works automatically and is the fastest |
|
1614 |
\item \isa{clarify} and \isa{clarsimp} perform obvious steps without splitting the |
|
1615 |
goal; \isa{safe} even splits goals |
|
1616 |
\item \isa{force} uses classical reasoning and simplification to prove a goal; |
|
1617 |
\isa{auto} is similar but leaves what it cannot prove |
|
1618 |
\item \isa{fast} and \isa{best} are legacy methods that work well with rules involving |
|
1619 |
unusual features |
|
1620 |
\end{itemize} |
|
1621 |
A table illustrates the relationships among four of these methods. |
|
1622 |
\begin{center} |
|
1623 |
\begin{tabular}{r|l|l|} |
|
1624 |
& no split & split \\ \hline |
|
1625 |
no simp & \isa{clarify} & \isa{safe} \\ \hline |
|
1626 |
simp & \isa{clarsimp} & \isa{auto} \\ \hline |
|
1627 |
\end{tabular} |
|
1628 |
\end{center} |
|
1629 |
||
1630 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1631 |
\section{Directives for Forward Proof}\label{sec:forward} |
10295 | 1632 |
|
1633 |
Forward proof means deriving new facts from old ones. It is the |
|
1634 |
most fundamental type of proof. Backward proof, by working from goals to |
|
1635 |
subgoals, can help us find a difficult proof. But it is |
|
1636 |
not always the best way of presenting the proof so found. Forward |
|
10301 | 1637 |
proof is particularly good for reasoning from the general |
10295 | 1638 |
to the specific. For example, consider the following distributive law for |
1639 |
the |
|
1640 |
\isa{gcd} function: |
|
1641 |
\[ k\times\gcd(m,n) = \gcd(k\times m,k\times n)\] |
|
1642 |
||
1643 |
Putting $m=1$ we get (since $\gcd(1,n)=1$ and $k\times1=k$) |
|
1644 |
\[ k = \gcd(k,k\times n)\] |
|
1645 |
We have derived a new fact about \isa{gcd}; if re-oriented, it might be |
|
1646 |
useful for simplification. After re-orienting it and putting $n=1$, we |
|
1647 |
derive another useful law: |
|
1648 |
\[ \gcd(k,k)=k \] |
|
1649 |
Substituting values for variables --- instantiation --- is a forward step. |
|
1650 |
Re-orientation works by applying the symmetry of equality to |
|
1651 |
an equation, so it too is a forward step. |
|
1652 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1653 |
\subsection{The {\tt\slshape of} and {\tt\slshape THEN} Directives} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1654 |
|
10295 | 1655 |
Now let us reproduce our examples in Isabelle. Here is the distributive |
1656 |
law: |
|
1657 |
\begin{isabelle}% |
|
10596 | 1658 |
?k\ *\ gcd\ (?m,\ ?n)\ =\ gcd\ (?k\ *\ ?m,\ ?k\ *\ ?n) |
10295 | 1659 |
\rulename{gcd_mult_distrib2} |
1660 |
\end{isabelle}% |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1661 |
The first step is to replace \isa{?m} by~1 in this law. The \isa{of} directive |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1662 |
refers to variables not by name but by their order of occurrence in the theorem. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1663 |
In this case, the variables are \isa{?k}, \isa{?m} and~\isa{?n}. So, the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1664 |
expression |
10295 | 1665 |
\hbox{\texttt{[of k 1]}} replaces \isa{?k} by~\isa{k} and \isa{?m} |
1666 |
by~\isa{1}. |
|
1667 |
\begin{isabelle} |
|
1668 |
\isacommand{lemmas}\ gcd_mult_0\ =\ gcd_mult_distrib2\ [of\ k\ 1] |
|
1669 |
\end{isabelle} |
|
1670 |
% |
|
1671 |
The command |
|
1672 |
\isa{thm gcd_mult_0} |
|
1673 |
displays the resulting theorem: |
|
1674 |
\begin{isabelle} |
|
10596 | 1675 |
\ \ \ \ \ k\ *\ gcd\ (1,\ ?n)\ =\ gcd\ (k\ *\ 1,\ k\ *\ ?n) |
10295 | 1676 |
\end{isabelle} |
1677 |
Something is odd: {\isa{k}} is an ordinary variable, while {\isa{?n}} |
|
1678 |
is schematic. We did not specify an instantiation |
|
1679 |
for {\isa{?n}}. In its present form, the theorem does not allow |
|
1680 |
substitution for {\isa{k}}. One solution is to avoid giving an instantiation for |
|
1681 |
\isa{?k}: instead of a term we can put an underscore~(\isa{_}). For example, |
|
1682 |
\begin{isabelle} |
|
1683 |
\ \ \ \ \ gcd_mult_distrib2\ [of\ _\ 1] |
|
1684 |
\end{isabelle} |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1685 |
replaces \isa{?m} by~\isa{1} but leaves \isa{?k} unchanged. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1686 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1687 |
The next step is to put |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1688 |
the theorem \isa{gcd_mult_0} into a simplified form, performing the steps |
10978 | 1689 |
$\gcd(1,n)=1$ and $k\times1=k$. The \isaindexbold{simplified} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1690 |
attribute takes a theorem |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1691 |
and returns the result of simplifying it, with respect to the default |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1692 |
simplification rules: |
10295 | 1693 |
\begin{isabelle} |
1694 |
\isacommand{lemmas}\ |
|
1695 |
gcd_mult_1\ =\ gcd_mult_0\ |
|
1696 |
[simplified]% |
|
1697 |
\end{isabelle} |
|
1698 |
% |
|
1699 |
Again, we display the resulting theorem: |
|
1700 |
\begin{isabelle} |
|
10596 | 1701 |
\ \ \ \ \ k\ =\ gcd\ (k,\ k\ *\ ?n) |
10295 | 1702 |
\end{isabelle} |
1703 |
% |
|
1704 |
To re-orient the equation requires the symmetry rule: |
|
1705 |
\begin{isabelle} |
|
1706 |
?s\ =\ ?t\ |
|
1707 |
\isasymLongrightarrow\ ?t\ =\ |
|
1708 |
?s% |
|
1709 |
\rulename{sym} |
|
1710 |
\end{isabelle} |
|
1711 |
The following declaration gives our equation to \isa{sym}: |
|
1712 |
\begin{isabelle} |
|
1713 |
\ \ \ \isacommand{lemmas}\ gcd_mult\ =\ gcd_mult_1\ |
|
1714 |
[THEN\ sym] |
|
1715 |
\end{isabelle} |
|
1716 |
% |
|
1717 |
Here is the result: |
|
1718 |
\begin{isabelle} |
|
10596 | 1719 |
\ \ \ \ \ gcd\ (k,\ k\ *\ ?n)\ =\ k% |
10295 | 1720 |
\end{isabelle} |
1721 |
\isa{THEN~sym} gives the current theorem to the rule \isa{sym} and returns the |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1722 |
resulting conclusion. The effect is to exchange the |
10596 | 1723 |
two operands of the equality. Typically \isa{THEN} is used with destruction |
10295 | 1724 |
rules. Above we have used |
1725 |
\isa{THEN~conjunct1} to extract the first part of the theorem |
|
1726 |
\isa{gcd_dvd_both}. Also useful is \isa{THEN~spec}, which removes the quantifier |
|
1727 |
from a theorem of the form $\forall x.\,P$, and \isa{THEN~mp}, which converts the |
|
1728 |
implication $P\imp Q$ into the rule $\vcenter{\infer{Q}{P}}$. |
|
1729 |
Similar to \isa{mp} are the following two rules, which extract |
|
1730 |
the two directions of reasoning about a boolean equivalence: |
|
1731 |
\begin{isabelle} |
|
10596 | 1732 |
\isasymlbrakk?Q\ =\ ?P;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P% |
10295 | 1733 |
\rulename{iffD1}% |
1734 |
\isanewline |
|
10596 | 1735 |
\isasymlbrakk?P\ =\ ?Q;\ ?Q\isasymrbrakk\ \isasymLongrightarrow\ ?P% |
10295 | 1736 |
\rulename{iffD2} |
1737 |
\end{isabelle} |
|
1738 |
% |
|
1739 |
Normally we would never name the intermediate theorems |
|
1740 |
such as \isa{gcd_mult_0} and |
|
1741 |
\isa{gcd_mult_1} but would combine |
|
1742 |
the three forward steps: |
|
1743 |
\begin{isabelle} |
|
1744 |
\isacommand{lemmas}\ gcd_mult\ =\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym]% |
|
1745 |
\end{isabelle} |
|
1746 |
The directives, or attributes, are processed from left to right. This |
|
1747 |
declaration of \isa{gcd_mult} is equivalent to the |
|
1748 |
previous one. |
|
1749 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1750 |
Such declarations can make the proof script hard to read. Better |
10295 | 1751 |
is to state the new lemma explicitly and to prove it using a single |
1752 |
\isa{rule} method whose operand is expressed using forward reasoning: |
|
1753 |
\begin{isabelle} |
|
1754 |
\isacommand{lemma}\ gcd_mult\ |
|
10301 | 1755 |
[simp]:\ |
10596 | 1756 |
"gcd(k,\ k*n)\ =\ k"\isanewline |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1757 |
\isacommand{by}\ (rule\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym]) |
10295 | 1758 |
\end{isabelle} |
1759 |
Compared with the previous proof of \isa{gcd_mult}, this |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1760 |
version shows the reader what has been proved. Also, the result will be processed |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1761 |
in the normal way. In particular, Isabelle generalizes over all variables: the |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1762 |
resulting theorem will have {\isa{?k}} instead of {\isa{k}}. |
10295 | 1763 |
|
1764 |
At the start of this section, we also saw a proof of $\gcd(k,k)=k$. Here |
|
1765 |
is the Isabelle version: |
|
1766 |
\begin{isabelle} |
|
10301 | 1767 |
\isacommand{lemma}\ gcd_self\ [simp]:\ "gcd(k,k)\ =\ k"\isanewline |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1768 |
\isacommand{by}\ (rule\ gcd_mult\ [of\ k\ 1,\ simplified]) |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1769 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1770 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1771 |
\medskip |
10978 | 1772 |
The \isaindexbold{rule_format} directive replaces a common usage |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1773 |
of \isa{THEN}\@. It is needed in proofs by induction when the induction formula must be |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1774 |
expressed using |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1775 |
\isa{\isasymlongrightarrow} and \isa{\isasymforall}. For example, in |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1776 |
Sect.\ts\ref{sec:proving-euclid} above we were able to write just this: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1777 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1778 |
\isacommand{lemma}\ gcd_greatest\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1779 |
[rule_format]:\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1780 |
\ \ \ \ \ \ \ "k\ dvd\ m\ \isasymlongrightarrow\ k\ dvd\ n\ \isasymlongrightarrow\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1781 |
k\ dvd\ gcd(m,n)" |
10295 | 1782 |
\end{isabelle} |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1783 |
% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1784 |
It replaces a clumsy use of \isa{THEN}: |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1785 |
\begin{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1786 |
\isacommand{lemma}\ gcd_greatest\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1787 |
[THEN mp, THEN mp]:\isanewline |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1788 |
\ \ \ \ \ \ \ "k\ dvd\ m\ \isasymlongrightarrow\ k\ dvd\ n\ \isasymlongrightarrow\ |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1789 |
k\ dvd\ gcd(m,n)" |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1790 |
\end{isabelle} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1791 |
% |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1792 |
Through \isa{rule_format} we can replace any number of applications of \isa{THEN} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1793 |
with the rules \isa{mp} and \isa{spec}, eliminating the outermost occurrences of |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1794 |
\isa{\isasymlongrightarrow} and \isa{\isasymforall}. |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1795 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1796 |
|
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1797 |
\subsection{The {\tt\slshape OF} Directive} |
10295 | 1798 |
|
1799 |
Recall that \isa{of} generates an instance of a rule by specifying |
|
1800 |
values for its variables. Analogous is \isa{OF}, which generates an |
|
1801 |
instance of a rule by specifying facts for its premises. Let us try |
|
1802 |
it with this rule: |
|
1803 |
\begin{isabelle} |
|
10971 | 1804 |
\isasymlbrakk gcd(?k,?n){=}1;\ ?k\ dvd\ ?m * ?n\isasymrbrakk\ |
10295 | 1805 |
\isasymLongrightarrow\ ?k\ dvd\ ?m |
1806 |
\rulename{relprime_dvd_mult} |
|
1807 |
\end{isabelle} |
|
1808 |
First, we |
|
1809 |
prove an instance of its first premise: |
|
1810 |
\begin{isabelle} |
|
1811 |
\isacommand{lemma}\ relprime_20_81:\ "gcd(\#20,\#81)\ =\ 1"\isanewline |
|
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1812 |
\isacommand{by}\ (simp\ add:\ gcd.simps) |
10295 | 1813 |
\end{isabelle} |
1814 |
We have evaluated an application of the \isa{gcd} function by |
|
1815 |
simplification. Expression evaluation is not guaranteed to terminate, and |
|
1816 |
certainly is not efficient; Isabelle performs arithmetic operations by |
|
10971 | 1817 |
rewriting symbolic bit strings. Here, however, the simplification above takes |
10596 | 1818 |
less than one second. We can specify this new lemma to \isa{OF}, |
10295 | 1819 |
generating an instance of \isa{relprime_dvd_mult}. The expression |
1820 |
\begin{isabelle} |
|
1821 |
\ \ \ \ \ relprime_dvd_mult [OF relprime_20_81] |
|
1822 |
\end{isabelle} |
|
1823 |
yields the theorem |
|
1824 |
\begin{isabelle} |
|
10596 | 1825 |
\ \ \ \ \ \#20\ dvd\ (?m\ *\ \#81)\ \isasymLongrightarrow\ \#20\ dvd\ ?m% |
10295 | 1826 |
\end{isabelle} |
1827 |
% |
|
10596 | 1828 |
\isa{OF} takes any number of operands. Consider |
10295 | 1829 |
the following facts about the divides relation: |
1830 |
\begin{isabelle} |
|
1831 |
\isasymlbrakk?k\ dvd\ ?m;\ |
|
1832 |
?k\ dvd\ ?n\isasymrbrakk\ |
|
1833 |
\isasymLongrightarrow\ ?k\ dvd\ |
|
10971 | 1834 |
?m\ +\ ?n |
10295 | 1835 |
\rulename{dvd_add}\isanewline |
1836 |
?m\ dvd\ ?m% |
|
1837 |
\rulename{dvd_refl} |
|
1838 |
\end{isabelle} |
|
1839 |
Let us supply \isa{dvd_refl} for each of the premises of \isa{dvd_add}: |
|
1840 |
\begin{isabelle} |
|
1841 |
\ \ \ \ \ dvd_add [OF dvd_refl dvd_refl] |
|
1842 |
\end{isabelle} |
|
1843 |
Here is the theorem that we have expressed: |
|
1844 |
\begin{isabelle} |
|
10596 | 1845 |
\ \ \ \ \ ?k\ dvd\ (?k\ +\ ?k) |
10295 | 1846 |
\end{isabelle} |
1847 |
As with \isa{of}, we can use the \isa{_} symbol to leave some positions |
|
1848 |
unspecified: |
|
1849 |
\begin{isabelle} |
|
1850 |
\ \ \ \ \ dvd_add [OF _ dvd_refl] |
|
1851 |
\end{isabelle} |
|
1852 |
The result is |
|
1853 |
\begin{isabelle} |
|
10971 | 1854 |
\ \ \ \ \ ?k\ dvd\ ?m\ \isasymLongrightarrow\ ?k\ dvd\ ?m\ +\ ?k |
10295 | 1855 |
\end{isabelle} |
1856 |
||
10596 | 1857 |
You may have noticed that \isa{THEN} and \isa{OF} are based on |
10295 | 1858 |
the same idea, namely to combine two rules. They differ in the |
1859 |
order of the combination and thus in their effect. We use \isa{THEN} |
|
1860 |
typically with a destruction rule to extract a subformula of the current |
|
1861 |
theorem. We use \isa{OF} with a list of facts to generate an instance of |
|
1862 |
the current theorem. |
|
1863 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1864 |
Here is a summary of some primitives for forward reasoning: |
10295 | 1865 |
\begin{itemize} |
10596 | 1866 |
\item \isa{of} instantiates the variables of a rule to a list of terms |
1867 |
\item \isa{OF} applies a rule to a list of theorems |
|
1868 |
\item \isa{THEN} gives a theorem to a named rule and returns the |
|
10295 | 1869 |
conclusion |
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1870 |
\item \isa{rule_format} puts a theorem into standard form |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1871 |
by removing \isa{\isasymlongrightarrow} and~\isa{\isasymforall} |
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1872 |
\item \isa{simplified} applies the simplifier to a theorem |
10295 | 1873 |
\end{itemize} |
1874 |
||
1875 |
||
10848
7b3ee4695fe6
various changes including the SOME examples, rule_format and "by"
paulson
parents:
10792
diff
changeset
|
1876 |
\section{Methods for Forward Proof} |
10295 | 1877 |
|
10967 | 1878 |
We have seen that the forward proof directives work well within a backward |
1879 |
proof. There are many ways to achieve a forward style, using our existing |
|
1880 |
proof methods and some additional ones that we introduce below. |
|
1881 |
||
1882 |
The methods \isa{drule}, \isa{frule}, \isa{drule_tac}, etc., |
|
1883 |
reason forward from a subgoal. We have seen them already, using rules such as |
|
1884 |
\isa{mp} and |
|
1885 |
\isa{spec} to operate on formulae. They can also operate on terms, using rules |
|
1886 |
such as these: |
|
1887 |
\begin{isabelle} |
|
1888 |
x\ =\ y\ \isasymLongrightarrow \ f\ x\ =\ f\ y% |
|
1889 |
\rulename{arg_cong}\isanewline |
|
1890 |
i\ \isasymle \ j\ \isasymLongrightarrow \ i\ *\ k\ \isasymle \ j\ *\ k% |
|
1891 |
\rulename{mult_le_mono1} |
|
1892 |
\end{isabelle} |
|
1893 |
||
1894 |
For example, let us prove a fact about divisibility in the natural numbers: |
|
1895 |
\begin{isabelle} |
|
1896 |
\isacommand{lemma}\ "\#2\ \isasymle \ u\ \isasymLongrightarrow \ u*m\ \isasymnoteq |
|
1897 |
\ Suc(u*n)"\isanewline |
|
1898 |
\isacommand{apply}\ intro\isanewline |
|
1899 |
\ 1.\ \isasymlbrakk \#2\ \isasymle \ u;\ u\ *\ m\ =\ Suc\ (u\ *\ n)\isasymrbrakk \ \isasymLongrightarrow \ False% |
|
1900 |
\end{isabelle} |
|
1901 |
% |
|
1902 |
The key step is to apply the function \ldots\isa{mod\ u} to both sides of the |
|
1903 |
equation |
|
1904 |
\isa{u*m\ =\ Suc(u*n)}: |
|
1905 |
||
1906 |
\begin{isabelle} |
|
1907 |
\isacommand{apply}\ (drule_tac\ f="\isasymlambda x.\ x\ mod\ u"\ \isakeyword{in}\ |
|
1908 |
arg_cong)\isanewline |
|
1909 |
\ 1.\ \isasymlbrakk \#2\ \isasymle \ u;\ u\ *\ m\ mod\ u\ =\ Suc\ (u\ *\ n)\ mod\ |
|
1910 |
u\isasymrbrakk \ \isasymLongrightarrow \ False |
|
1911 |
\end{isabelle} |
|
1912 |
% |
|
1913 |
Simplification reduces the left side to 0 and the right side to~1, yielding the |
|
1914 |
required contradiction. |
|
1915 |
\begin{isabelle} |
|
1916 |
\isacommand{apply}\ (simp\ add:\ mod_Suc)\isanewline |
|
1917 |
\isacommand{done} |
|
1918 |
\end{isabelle} |
|
1919 |
||
1920 |
Our proof has used a fact about remainder: |
|
1921 |
\begin{isabelle} |
|
1922 |
Suc\ m\ mod\ n\ =\isanewline |
|
1923 |
(if\ Suc\ (m\ mod\ n)\ =\ n\ then\ 0\ else\ Suc\ (m\ mod\ n)) |
|
1924 |
\rulename{mod_Suc} |
|
1925 |
\end{isabelle} |
|
1926 |
||
1927 |
\medskip |
|
1928 |
The methods designed for supporting forward reasoning are \isa{insert} and |
|
1929 |
\isa{subgoal_tac}. The \isa{insert} method inserts a |
|
10295 | 1930 |
given theorem as a new assumption of the current subgoal. This already |
1931 |
is a forward step; moreover, we may (as always when using a theorem) apply |
|
10596 | 1932 |
\isa{of}, \isa{THEN} and other directives. The new assumption can then |
10295 | 1933 |
be used to help prove the subgoal. |
1934 |
||
1935 |
For example, consider this theorem about the divides relation. |
|
1936 |
Only the first proof step is given; it inserts the distributive law for |
|
1937 |
\isa{gcd}. We specify its variables as shown. |
|
1938 |
\begin{isabelle} |
|
1939 |
\isacommand{lemma}\ |
|
1940 |
relprime_dvd_mult:\isanewline |
|
10971 | 1941 |
\ \ \ \ \ \ \ "\isasymlbrakk \ gcd(k,n){=}1;\ k\ dvd\ m*n\ |
10596 | 1942 |
\isasymrbrakk\ |
10295 | 1943 |
\isasymLongrightarrow\ k\ dvd\ |
1944 |
m"\isanewline |
|
1945 |
\isacommand{apply}\ (insert\ gcd_mult_distrib2\ [of\ m\ k\ |
|
1946 |
n]) |
|
1947 |
\end{isabelle} |
|
1948 |
In the resulting subgoal, note how the equation has been |
|
1949 |
inserted: |
|
1950 |
\begin{isabelle} |
|
10971 | 1951 |
\ 1.\ \isasymlbrakk gcd\ (k,\ n)\ =\ 1;\ k\ dvd\ m\ *\ n{;}\ m\ *\ gcd\ |
10295 | 1952 |
(k,\ n)\ |
10596 | 1953 |
=\ gcd\ (m\ *\ k,\ m\ *\ n)\isasymrbrakk\isanewline |
10295 | 1954 |
\ \ \ \ \isasymLongrightarrow\ k\ dvd\ m |
1955 |
\end{isabelle} |
|
1956 |
The next proof step, \isa{\isacommand{apply}(simp)}, |
|
1957 |
utilizes the assumption \isa{gcd(k,n)\ =\ |
|
1958 |
1}. Here is the result: |
|
1959 |
\begin{isabelle} |
|
10971 | 1960 |
\ 1.\ \isasymlbrakk gcd\ (k,\ n)\ =\ 1;\ k\ dvd\ (m\ *\ n){;} |
1961 |
\ m\ =\ gcd\ (m\ *\ k,\ m\ *\ n)\isasymrbrakk\isanewline |
|
10295 | 1962 |
\ \ \ \ \isasymLongrightarrow\ k\ dvd\ m |
1963 |
\end{isabelle} |
|
1964 |
Simplification has yielded an equation for \isa{m} that will be used to |
|
1965 |
complete the proof. |
|
1966 |
||
1967 |
\medskip |
|
10399 | 1968 |
Here is another proof using \isa{insert}. \REMARK{Effect with unknowns?} |
10295 | 1969 |
Division and remainder obey a well-known law: |
1970 |
\begin{isabelle} |
|
10596 | 1971 |
(?m\ div\ ?n)\ *\ ?n\ +\ ?m\ mod\ ?n\ =\ ?m |
10295 | 1972 |
\rulename{mod_div_equality} |
1973 |
\end{isabelle} |
|
1974 |
||
1975 |
We refer to this law explicitly in the following proof: |
|
1976 |
\begin{isabelle} |
|
1977 |
\isacommand{lemma}\ div_mult_self_is_m:\ \isanewline |
|
10596 | 1978 |
\ \ \ \ \ \ "0{\isacharless}n\ \isasymLongrightarrow\ (m*n)\ div\ n\ =\ |
1979 |
(m::nat)"\isanewline |
|
1980 |
\isacommand{apply}\ (insert\ mod_div_equality\ [of\ "m*n"\ n])\isanewline |
|
10295 | 1981 |
\isacommand{apply}\ (simp)\isanewline |
1982 |
\isacommand{done} |
|
1983 |
\end{isabelle} |
|
1984 |
The first step inserts the law, specifying \isa{m*n} and |
|
10301 | 1985 |
\isa{n} for its variables. Notice that non-trivial expressions must be |
10295 | 1986 |
enclosed in quotation marks. Here is the resulting |
1987 |
subgoal, with its new assumption: |
|
1988 |
\begin{isabelle} |
|
1989 |
%0\ \isacharless\ n\ \isasymLongrightarrow\ (m\ |
|
10596 | 1990 |
%*\ n)\ div\ n\ =\ m\isanewline |
10295 | 1991 |
\ 1.\ \isasymlbrakk0\ \isacharless\ |
10596 | 1992 |
n;\ \ (m\ *\ n)\ div\ n\ *\ n\ +\ (m\ *\ n)\ mod\ n\ |
1993 |
=\ m\ *\ n\isasymrbrakk\isanewline |
|
1994 |
\ \ \ \ \isasymLongrightarrow\ (m\ *\ n)\ div\ n\ |
|
10295 | 1995 |
=\ m |
1996 |
\end{isabelle} |
|
10596 | 1997 |
Simplification reduces \isa{(m\ *\ n)\ mod\ n} to zero. |
10295 | 1998 |
Then it cancels the factor~\isa{n} on both |
1999 |
sides of the equation, proving the theorem. |
|
2000 |
||
2001 |
\medskip |
|
10596 | 2002 |
A similar method is {\isa{subgoal_tac}}. Instead of inserting |
10295 | 2003 |
a theorem as an assumption, it inserts an arbitrary formula. |
2004 |
This formula must be proved later as a separate subgoal. The |
|
2005 |
idea is to claim that the formula holds on the basis of the current |
|
2006 |
assumptions, to use this claim to complete the proof, and finally |
|
2007 |
to justify the claim. It is a valuable means of giving the proof |
|
2008 |
some structure. The explicit formula will be more readable than |
|
2009 |
proof commands that yield that formula indirectly. |
|
2010 |
||
2011 |
Look at the following example. |
|
2012 |
\begin{isabelle} |
|
2013 |
\isacommand{lemma}\ "\isasymlbrakk(z::int)\ <\ \#37;\ \#66\ <\ \#2*z;\ z*z\ |
|
2014 |
\isasymnoteq\ \#1225;\ Q(\#34);\ Q(\#36)\isasymrbrakk\isanewline |
|
2015 |
\ \ \ \ \ \ \ \ \,\isasymLongrightarrow\ Q(z)"\isanewline |
|
2016 |
\isacommand{apply}\ (subgoal_tac\ "z\ =\ \#34\ \isasymor\ z\ =\ |
|
2017 |
\#36")\isanewline |
|
2018 |
\isacommand{apply}\ blast\isanewline |
|
2019 |
\isacommand{apply}\ (subgoal_tac\ "z\ \isasymnoteq\ \#35")\isanewline |
|
2020 |
\isacommand{apply}\ arith\isanewline |
|
2021 |
\isacommand{apply}\ force\isanewline |
|
2022 |
\isacommand{done} |
|
2023 |
\end{isabelle} |
|
2024 |
Let us prove it informally. The first assumption tells us |
|
2025 |
that \isa{z} is no greater than 36. The second tells us that \isa{z} |
|
2026 |
is at least 34. The third assumption tells us that \isa{z} cannot be 35, since |
|
2027 |
$35\times35=1225$. So \isa{z} is either 34 or 36, and since \isa{Q} holds for |
|
2028 |
both of those values, we have the conclusion. |
|
2029 |
||
2030 |
The Isabelle proof closely follows this reasoning. The first |
|
2031 |
step is to claim that \isa{z} is either 34 or 36. The resulting proof |
|
2032 |
state gives us two subgoals: |
|
2033 |
\begin{isabelle} |
|
10596 | 2034 |
%\isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ |
10295 | 2035 |
%Q\ \#34;\ Q\ \#36\isasymrbrakk\ \isasymLongrightarrow\ Q\ z\isanewline |
10596 | 2036 |
\ 1.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ Q\ \#34;\ Q\ \#36;\isanewline |
10295 | 2037 |
\ \ \ \ \ z\ =\ \#34\ \isasymor\ z\ =\ \#36\isasymrbrakk\isanewline |
2038 |
\ \ \ \ \isasymLongrightarrow\ Q\ z\isanewline |
|
10596 | 2039 |
\ 2.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk\isanewline |
10295 | 2040 |
\ \ \ \ \isasymLongrightarrow\ z\ =\ \#34\ \isasymor\ z\ =\ \#36 |
2041 |
\end{isabelle} |
|
10971 | 2042 |
The first subgoal is trivial (\isa{blast}), but for the second Isabelle needs help to eliminate |
10295 | 2043 |
the case |
10596 | 2044 |
\isa{z}=35. The second invocation of {\isa{subgoal_tac}} leaves two |
10295 | 2045 |
subgoals: |
2046 |
\begin{isabelle} |
|
10596 | 2047 |
\ 1.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ |
10295 | 2048 |
\#1225;\ Q\ \#34;\ Q\ \#36;\isanewline |
2049 |
\ \ \ \ \ z\ \isasymnoteq\ \#35\isasymrbrakk\isanewline |
|
2050 |
\ \ \ \ \isasymLongrightarrow\ z\ =\ \#34\ \isasymor\ z\ =\ \#36\isanewline |
|
10596 | 2051 |
\ 2.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq\ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk\isanewline |
10295 | 2052 |
\ \ \ \ \isasymLongrightarrow\ z\ \isasymnoteq\ \#35 |
2053 |
\end{isabelle} |
|
2054 |
||
10971 | 2055 |
Assuming that \isa{z} is not 35, the first subgoal follows by linear arithmetic |
2056 |
(\isa{arith}). For the second subgoal we apply the method \isa{force}, |
|
10295 | 2057 |
which proceeds by assuming that \isa{z}=35 and arriving at a contradiction. |
2058 |
||
2059 |
||
2060 |
\medskip |
|
2061 |
Summary of these methods: |
|
2062 |
\begin{itemize} |
|
2063 |
\item {\isa{insert}} adds a theorem as a new assumption |
|
2064 |
\item {\isa{subgoal_tac}} adds a formula as a new assumption and leaves the |
|
2065 |
subgoal of proving that formula |
|
2066 |
\end{itemize} |
|
10967 | 2067 |
|
2068 |
||
2069 |
\section{Managing Large Proofs} |
|
2070 |
||
2071 |
Naturally you should try to divide proofs into manageable parts. Look for lemmas |
|
2072 |
that can be proved separately. Sometimes you will observe that they are |
|
2073 |
instances of much simpler facts. On other occasions, no lemmas suggest themselves |
|
2074 |
and you are forced to cope with a long proof involving many subgoals. |
|
2075 |
||
2076 |
\subsection{Tacticals, or Control Structures} |
|
2077 |
||
2078 |
If the proof is long, perhaps it at least has some regularity. Then you can |
|
2079 |
express it more concisely using \textbf{tacticals}, which provide control |
|
2080 |
structures. Here is a proof (it would be a one-liner using |
|
2081 |
\isa{blast}, but forget that) that contains a series of repeated |
|
2082 |
commands: |
|
2083 |
% |
|
2084 |
\begin{isabelle} |
|
2085 |
\isacommand{lemma}\ "\isasymlbrakk P\isasymlongrightarrow Q;\ |
|
2086 |
Q\isasymlongrightarrow R;\ R\isasymlongrightarrow S;\ P\isasymrbrakk \ |
|
2087 |
\isasymLongrightarrow \ S"\isanewline |
|
2088 |
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline |
|
2089 |
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline |
|
2090 |
\isacommand{apply}\ (drule\ mp,\ assumption)\isanewline |
|
2091 |
\isacommand{apply}\ (assumption)\isanewline |
|
2092 |
\isacommand{done} |
|
2093 |
\end{isabelle} |
|
2094 |
% |
|
2095 |
Each of the three identical commands finds an implication and proves its |
|
2096 |
antecedent by assumption. The first one finds \isa{P\isasymlongrightarrow Q} and |
|
2097 |
\isa{P}, concluding~\isa{Q}; the second one concludes~\isa{R} and the third one |
|
2098 |
concludes~\isa{S}. The final step matches the assumption \isa{S} with the goal to |
|
2099 |
be proved. |
|
2100 |
||
2101 |
Suffixing a method with a plus sign~(\isa+) |
|
2102 |
expresses one or more repetitions: |
|
2103 |
\begin{isabelle} |
|
2104 |
\isacommand{lemma}\ "\isasymlbrakk P\isasymlongrightarrow Q;\ Q\isasymlongrightarrow R;\ R\isasymlongrightarrow S;\ P\isasymrbrakk \ \isasymLongrightarrow \ S"\isanewline |
|
2105 |
\isacommand{by}\ (drule\ mp,\ assumption)+ |
|
2106 |
\end{isabelle} |
|
2107 |
% |
|
2108 |
Using \isacommand{by} takes care of the final use of \isa{assumption}. The new |
|
2109 |
proof is more concise. It is also more general: the repetitive method works |
|
2110 |
for a chain of implications having any length, not just three. |
|
2111 |
||
2112 |
Choice is another control structure. Separating two methods by a vertical |
|
2113 |
bar~(\isa|) gives the effect of applying the first method, and if that fails, |
|
2114 |
trying the second. It can be combined with repetition, when the choice must be |
|
2115 |
made over and over again. Here is a chain of implications in which most of the |
|
2116 |
antecedents are proved by assumption, but one is proved by arithmetic: |
|
2117 |
\begin{isabelle} |
|
2118 |
\isacommand{lemma}\ "\isasymlbrakk Q\isasymlongrightarrow R;\ P\isasymlongrightarrow Q;\ x<\#5\isasymlongrightarrow P;\ |
|
2119 |
Suc\ x\ <\ \#5\isasymrbrakk \ \isasymLongrightarrow \ R"\ \isanewline |
|
2120 |
\isacommand{by}\ (drule\ mp,\ (assumption|arith))+ |
|
2121 |
\end{isabelle} |
|
2122 |
The \isa{arith} |
|
2123 |
method can prove $x<5$ from $x+1<5$, but it cannot duplicate the effect of |
|
2124 |
\isa{assumption}. Therefore, we combine these methods using the choice |
|
2125 |
operator. |
|
2126 |
||
2127 |
A postfixed question mark~(\isa?) expresses zero or one repetitions of a method. |
|
2128 |
It can also be viewed as the choice between executing a method and doing nothing. |
|
2129 |
It is useless at top level but may be valuable within other control structures. |
|
2130 |
||
2131 |
||
2132 |
\subsection{Subgoal Numbering} |
|
2133 |
||
2134 |
Another problem in large proofs is contending with huge |
|
2135 |
subgoals or many subgoals. Induction can produce a proof state that looks |
|
2136 |
like this: |
|
2137 |
\begin{isabelle} |
|
2138 |
\ 1.\ bigsubgoal1\isanewline |
|
2139 |
\ 2.\ bigsubgoal2\isanewline |
|
2140 |
\ 3.\ bigsubgoal3\isanewline |
|
2141 |
\ 4.\ bigsubgoal4\isanewline |
|
2142 |
\ 5.\ bigsubgoal5\isanewline |
|
2143 |
\ 6.\ bigsubgoal6 |
|
2144 |
\end{isabelle} |
|
2145 |
If each \isa{bigsubgoal} is 15 lines or so, the proof state will be too big to |
|
2146 |
scroll through. By default, Isabelle displays at most 10 subgoals. The |
|
2147 |
\isacommand{pr} command lets you change this limit: |
|
2148 |
\begin{isabelle} |
|
2149 |
\isacommand{pr}\ 2\isanewline |
|
2150 |
\ 1.\ bigsubgoal1\isanewline |
|
2151 |
\ 2.\ bigsubgoal2\isanewline |
|
2152 |
A total of 6 subgoals... |
|
2153 |
\end{isabelle} |
|
2154 |
||
2155 |
\medskip |
|
2156 |
All methods apply to the first subgoal. |
|
2157 |
Sometimes, not only in a large proof, you may want to focus on some other |
|
2158 |
subgoal. Then you should try the commands \isacommand{defer} or \isacommand{prefer}. |
|
2159 |
||
2160 |
In the following example, the first subgoal looks hard, while the others |
|
2161 |
look as if \isa{blast} alone could prove them: |
|
2162 |
%\begin{isabelle} |
|
2163 |
%\isacommand{lemma}\ "hard\ \isasymand \ (P\ \isasymor \ \isachartilde P)\ \isasymand \ (Q\isasymlongrightarrow Q)"\isanewline |
|
2164 |
%\isacommand{apply}\ intro |
|
2165 |
%\end{isabelle} |
|
2166 |
\begin{isabelle} |
|
2167 |
\ 1.\ hard\isanewline |
|
2168 |
\ 2.\ \isasymnot \ \isasymnot \ P\ \isasymLongrightarrow \ P\isanewline |
|
2169 |
\ 3.\ Q\ \isasymLongrightarrow \ Q% |
|
2170 |
\end{isabelle} |
|
2171 |
% |
|
2172 |
The \isacommand{defer} command moves the first subgoal into the last position. |
|
2173 |
\begin{isabelle} |
|
2174 |
\isacommand{defer}\ 1\isanewline |
|
2175 |
\ 1.\ \isasymnot \ \isasymnot \ P\ \isasymLongrightarrow \ P\isanewline |
|
2176 |
\ 2.\ Q\ \isasymLongrightarrow \ Q\isanewline |
|
2177 |
\ 3.\ hard% |
|
2178 |
\end{isabelle} |
|
2179 |
% |
|
2180 |
Now we apply \isa{blast} repeatedly to the easy subgoals: |
|
2181 |
\begin{isabelle} |
|
2182 |
\isacommand{apply}\ blast+\isanewline |
|
2183 |
\ 1.\ hard% |
|
2184 |
\end{isabelle} |
|
2185 |
Using \isacommand{defer}, we have cleared away the trivial parts of the proof so |
|
2186 |
that we can devote attention to the difficult part. |
|
2187 |
||
2188 |
\medskip |
|
2189 |
The \isacommand{prefer} command moves the specified subgoal into the |
|
2190 |
first position. For example, if you suspect that one of your subgoals is |
|
2191 |
invalid (not a theorem), then you should investigate that subgoal first. If it |
|
2192 |
cannot be proved, then there is no point in proving the other subgoals. |
|
2193 |
\begin{isabelle} |
|
2194 |
\ 1.\ ok1\isanewline |
|
2195 |
\ 2.\ ok2\isanewline |
|
2196 |
\ 3.\ doubtful% |
|
2197 |
\end{isabelle} |
|
2198 |
% |
|
2199 |
We decide to work on the third subgoal. |
|
2200 |
\begin{isabelle} |
|
2201 |
\isacommand{prefer}\ 3\isanewline |
|
2202 |
\ 1.\ doubtful\isanewline |
|
2203 |
\ 2.\ ok1\isanewline |
|
2204 |
\ 3.\ ok2 |
|
2205 |
\end{isabelle} |
|
2206 |
If we manage to prove \isa{doubtful}, then we can work on the other |
|
2207 |
subgoals, confident that we are not wasting our time. Finally we revise the |
|
2208 |
proof script to remove the \isacommand{prefer} command, since we needed it only to |
|
2209 |
focus our exploration. The previous example is different: its use of |
|
2210 |
\isacommand{defer} stops trivial subgoals from cluttering the rest of the |
|
2211 |
proof. Even there, we should consider proving \isa{hard} as a preliminary |
|
2212 |
lemma. Always seek ways to streamline your proofs. |
|
2213 |
||
2214 |
||
2215 |
\medskip |
|
2216 |
Summary: |
|
2217 |
\begin{itemize} |
|
2218 |
\item the control structures \isa+, \isa? and \isa| help express complicated proofs |
|
2219 |
\item the \isacommand{pr} command can limit the number of subgoals to display |
|
2220 |
\item the \isacommand{defer} and \isacommand{prefer} commands move a |
|
2221 |
subgoal to the last or first position |
|
2222 |
\end{itemize} |
|
2223 |
||
2224 |
\begin{exercise} |
|
2225 |
This proof uses \isa? and \isa+ to \ldots{} can you explain? |
|
2226 |
\begin{isabelle} |
|
2227 |
\isacommand{lemma}\ "\isasymlbrakk P\isasymand Q\isasymlongrightarrow R;\ P\isasymlongrightarrow Q;\ P\isasymrbrakk \ \isasymLongrightarrow \ R"\isanewline |
|
2228 |
\isacommand{by}\ (drule\ mp,\ intro?,\ assumption+)+ |
|
2229 |
\end{isabelle} |
|
2230 |
\end{exercise} |