author | nipkow |
Sat, 08 Feb 2014 20:34:10 +0100 | |
changeset 55361 | d459a63ca42f |
parent 55359 | 2d8222c76020 |
child 55389 | 33f833231fa2 |
permissions | -rw-r--r-- |
47269 | 1 |
(*<*) |
2 |
theory Isar |
|
3 |
imports LaTeXsugar |
|
4 |
begin |
|
52059 | 5 |
declare [[quick_and_dirty]] |
47269 | 6 |
(*>*) |
7 |
text{* |
|
8 |
Apply-scripts are unreadable and hard to maintain. The language of choice |
|
9 |
for larger proofs is \concept{Isar}. The two key features of Isar are: |
|
10 |
\begin{itemize} |
|
11 |
\item It is structured, not linear. |
|
12 |
\item It is readable without running it because |
|
13 |
you need to state what you are proving at any given point. |
|
14 |
\end{itemize} |
|
15 |
Whereas apply-scripts are like assembly language programs, Isar proofs |
|
16 |
are like structured programs with comments. A typical Isar proof looks like this: |
|
17 |
*}text{* |
|
18 |
\begin{tabular}{@ {}l} |
|
19 |
\isacom{proof}\\ |
|
20 |
\quad\isacom{assume} @{text"\""}$\mathit{formula}_0$@{text"\""}\\ |
|
21 |
\quad\isacom{have} @{text"\""}$\mathit{formula}_1$@{text"\""} \quad\isacom{by} @{text simp}\\ |
|
22 |
\quad\vdots\\ |
|
23 |
\quad\isacom{have} @{text"\""}$\mathit{formula}_n$@{text"\""} \quad\isacom{by} @{text blast}\\ |
|
24 |
\quad\isacom{show} @{text"\""}$\mathit{formula}_{n+1}$@{text"\""} \quad\isacom{by} @{text \<dots>}\\ |
|
25 |
\isacom{qed} |
|
26 |
\end{tabular} |
|
27 |
*}text{* |
|
28 |
It proves $\mathit{formula}_0 \Longrightarrow \mathit{formula}_{n+1}$ |
|
47704 | 29 |
(provided each proof step succeeds). |
47269 | 30 |
The intermediate \isacom{have} statements are merely stepping stones |
31 |
on the way towards the \isacom{show} statement that proves the actual |
|
32 |
goal. In more detail, this is the Isar core syntax: |
|
33 |
\medskip |
|
34 |
||
35 |
\begin{tabular}{@ {}lcl@ {}} |
|
55359 | 36 |
\textit{proof} &=& \indexed{\isacom{by}}{by} \textit{method}\\ |
37 |
&$\mid$& \indexed{\isacom{proof}}{proof} [\textit{method}] \ \textit{step}$^*$ \ \indexed{\isacom{qed}}{qed} |
|
47269 | 38 |
\end{tabular} |
39 |
\medskip |
|
40 |
||
41 |
\begin{tabular}{@ {}lcl@ {}} |
|
55359 | 42 |
\textit{step} &=& \indexed{\isacom{fix}}{fix} \textit{variables} \\ |
43 |
&$\mid$& \indexed{\isacom{assume}}{assume} \textit{proposition} \\ |
|
44 |
&$\mid$& [\indexed{\isacom{from}}{from} \textit{fact}$^+$] (\indexed{\isacom{have}}{have} $\mid$ \indexed{\isacom{show}}{show}) \ \textit{proposition} \ \textit{proof} |
|
47269 | 45 |
\end{tabular} |
46 |
\medskip |
|
47 |
||
48 |
\begin{tabular}{@ {}lcl@ {}} |
|
49 |
\textit{proposition} &=& [\textit{name}:] @{text"\""}\textit{formula}@{text"\""} |
|
50 |
\end{tabular} |
|
51 |
\medskip |
|
52 |
||
53 |
\begin{tabular}{@ {}lcl@ {}} |
|
54 |
\textit{fact} &=& \textit{name} \ $\mid$ \ \dots |
|
55 |
\end{tabular} |
|
56 |
\medskip |
|
57 |
||
58 |
\noindent A proof can either be an atomic \isacom{by} with a single proof |
|
59 |
method which must finish off the statement being proved, for example @{text |
|
60 |
auto}. Or it can be a \isacom{proof}--\isacom{qed} block of multiple |
|
61 |
steps. Such a block can optionally begin with a proof method that indicates |
|
62 |
how to start off the proof, e.g.\ \mbox{@{text"(induction xs)"}}. |
|
63 |
||
64 |
A step either assumes a proposition or states a proposition |
|
65 |
together with its proof. The optional \isacom{from} clause |
|
66 |
indicates which facts are to be used in the proof. |
|
67 |
Intermediate propositions are stated with \isacom{have}, the overall goal |
|
68 |
with \isacom{show}. A step can also introduce new local variables with |
|
69 |
\isacom{fix}. Logically, \isacom{fix} introduces @{text"\<And>"}-quantified |
|
70 |
variables, \isacom{assume} introduces the assumption of an implication |
|
71 |
(@{text"\<Longrightarrow>"}) and \isacom{have}/\isacom{show} the conclusion. |
|
72 |
||
73 |
Propositions are optionally named formulas. These names can be referred to in |
|
74 |
later \isacom{from} clauses. In the simplest case, a fact is such a name. |
|
75 |
But facts can also be composed with @{text OF} and @{text of} as shown in |
|
76 |
\S\ref{sec:forward-proof}---hence the \dots\ in the above grammar. Note |
|
77 |
that assumptions, intermediate \isacom{have} statements and global lemmas all |
|
78 |
have the same status and are thus collectively referred to as |
|
55317 | 79 |
\conceptidx{facts}{fact}. |
47269 | 80 |
|
81 |
Fact names can stand for whole lists of facts. For example, if @{text f} is |
|
82 |
defined by command \isacom{fun}, @{text"f.simps"} refers to the whole list of |
|
83 |
recursion equations defining @{text f}. Individual facts can be selected by |
|
84 |
writing @{text"f.simps(2)"}, whole sublists by @{text"f.simps(2-4)"}. |
|
85 |
||
86 |
||
52361 | 87 |
\section{Isar by Example} |
47269 | 88 |
|
47704 | 89 |
We show a number of proofs of Cantor's theorem that a function from a set to |
47269 | 90 |
its powerset cannot be surjective, illustrating various features of Isar. The |
91 |
constant @{const surj} is predefined. |
|
92 |
*} |
|
93 |
||
94 |
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)" |
|
95 |
proof |
|
96 |
assume 0: "surj f" |
|
97 |
from 0 have 1: "\<forall>A. \<exists>a. A = f a" by(simp add: surj_def) |
|
98 |
from 1 have 2: "\<exists>a. {x. x \<notin> f x} = f a" by blast |
|
99 |
from 2 show "False" by blast |
|
100 |
qed |
|
101 |
||
102 |
text{* |
|
103 |
The \isacom{proof} command lacks an explicit method how to perform |
|
104 |
the proof. In such cases Isabelle tries to use some standard introduction |
|
105 |
rule, in the above case for @{text"\<not>"}: |
|
106 |
\[ |
|
107 |
\inferrule{ |
|
108 |
\mbox{@{thm (prem 1) notI}}} |
|
109 |
{\mbox{@{thm (concl) notI}}} |
|
110 |
\] |
|
111 |
In order to prove @{prop"~ P"}, assume @{text P} and show @{text False}. |
|
112 |
Thus we may assume @{prop"surj f"}. The proof shows that names of propositions |
|
113 |
may be (single!) digits---meaningful names are hard to invent and are often |
|
114 |
not necessary. Both \isacom{have} steps are obvious. The second one introduces |
|
115 |
the diagonal set @{term"{x. x \<notin> f x}"}, the key idea in the proof. |
|
116 |
If you wonder why @{text 2} directly implies @{text False}: from @{text 2} |
|
117 |
it follows that @{prop"a \<notin> f a \<longleftrightarrow> a \<in> f a"}. |
|
118 |
||
55359 | 119 |
\subsection{\indexed{@{text this}}{this}, \indexed{\isacom{then}}{then}, \indexed{\isacom{hence}}{hence} and \indexed{\isacom{thus}}{thus}} |
47269 | 120 |
|
121 |
Labels should be avoided. They interrupt the flow of the reader who has to |
|
122 |
scan the context for the point where the label was introduced. Ideally, the |
|
123 |
proof is a linear flow, where the output of one step becomes the input of the |
|
124 |
next step, piping the previously proved fact into the next proof, just like |
|
125 |
in a UNIX pipe. In such cases the predefined name @{text this} can be used |
|
126 |
to refer to the proposition proved in the previous step. This allows us to |
|
127 |
eliminate all labels from our proof (we suppress the \isacom{lemma} statement): |
|
128 |
*} |
|
129 |
(*<*) |
|
130 |
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)" |
|
131 |
(*>*) |
|
132 |
proof |
|
133 |
assume "surj f" |
|
134 |
from this have "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def) |
|
135 |
from this show "False" by blast |
|
136 |
qed |
|
137 |
||
138 |
text{* We have also taken the opportunity to compress the two \isacom{have} |
|
139 |
steps into one. |
|
140 |
||
141 |
To compact the text further, Isar has a few convenient abbreviations: |
|
142 |
\medskip |
|
143 |
||
54839 | 144 |
\begin{tabular}{r@ {\quad=\quad}l} |
145 |
\isacom{then} & \isacom{from} @{text this}\\ |
|
146 |
\isacom{thus} & \isacom{then} \isacom{show}\\ |
|
147 |
\isacom{hence} & \isacom{then} \isacom{have} |
|
47269 | 148 |
\end{tabular} |
149 |
\medskip |
|
150 |
||
151 |
\noindent |
|
152 |
With the help of these abbreviations the proof becomes |
|
153 |
*} |
|
154 |
(*<*) |
|
155 |
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)" |
|
156 |
(*>*) |
|
157 |
proof |
|
158 |
assume "surj f" |
|
159 |
hence "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def) |
|
160 |
thus "False" by blast |
|
161 |
qed |
|
162 |
text{* |
|
163 |
||
164 |
There are two further linguistic variations: |
|
165 |
\medskip |
|
166 |
||
54839 | 167 |
\begin{tabular}{r@ {\quad=\quad}l} |
55359 | 168 |
(\isacom{have}$\mid$\isacom{show}) \ \textit{prop} \ \indexed{\isacom{using}}{using} \ \textit{facts} |
54839 | 169 |
& |
47269 | 170 |
\isacom{from} \ \textit{facts} \ (\isacom{have}$\mid$\isacom{show}) \ \textit{prop}\\ |
55359 | 171 |
\indexed{\isacom{with}}{with} \ \textit{facts} & \isacom{from} \ \textit{facts} \isa{this} |
47269 | 172 |
\end{tabular} |
173 |
\medskip |
|
174 |
||
47711 | 175 |
\noindent The \isacom{using} idiom de-emphasizes the used facts by moving them |
47269 | 176 |
behind the proposition. |
177 |
||
55359 | 178 |
\subsection{Structured Lemma Statements: \indexed{\isacom{fixes}}{fixes}, \indexed{\isacom{assumes}}{assumes}, \indexed{\isacom{shows}}{shows}} |
179 |
\index{lemma@\isacom{lemma}} |
|
47269 | 180 |
Lemmas can also be stated in a more structured fashion. To demonstrate this |
47306 | 181 |
feature with Cantor's theorem, we rephrase @{prop"\<not> surj f"} |
47269 | 182 |
a little: |
183 |
*} |
|
184 |
||
185 |
lemma |
|
186 |
fixes f :: "'a \<Rightarrow> 'a set" |
|
187 |
assumes s: "surj f" |
|
188 |
shows "False" |
|
189 |
||
190 |
txt{* The optional \isacom{fixes} part allows you to state the types of |
|
191 |
variables up front rather than by decorating one of their occurrences in the |
|
192 |
formula with a type constraint. The key advantage of the structured format is |
|
47306 | 193 |
the \isacom{assumes} part that allows you to name each assumption; multiple |
194 |
assumptions can be separated by \isacom{and}. The |
|
47269 | 195 |
\isacom{shows} part gives the goal. The actual theorem that will come out of |
196 |
the proof is @{prop"surj f \<Longrightarrow> False"}, but during the proof the assumption |
|
197 |
@{prop"surj f"} is available under the name @{text s} like any other fact. |
|
198 |
*} |
|
199 |
||
200 |
proof - |
|
201 |
have "\<exists> a. {x. x \<notin> f x} = f a" using s |
|
202 |
by(auto simp: surj_def) |
|
203 |
thus "False" by blast |
|
204 |
qed |
|
205 |
||
206 |
text{* In the \isacom{have} step the assumption @{prop"surj f"} is now |
|
207 |
referenced by its name @{text s}. The duplication of @{prop"surj f"} in the |
|
208 |
above proofs (once in the statement of the lemma, once in its proof) has been |
|
209 |
eliminated. |
|
210 |
||
211 |
\begin{warn} |
|
212 |
Note the dash after the \isacom{proof} |
|
213 |
command. It is the null method that does nothing to the goal. Leaving it out |
|
214 |
would ask Isabelle to try some suitable introduction rule on the goal @{const |
|
215 |
False}---but there is no suitable introduction rule and \isacom{proof} |
|
216 |
would fail. |
|
217 |
\end{warn} |
|
218 |
||
47704 | 219 |
Stating a lemma with \isacom{assumes}-\isacom{shows} implicitly introduces the |
55359 | 220 |
name \indexed{@{text assms}}{assms} that stands for the list of all assumptions. You can refer |
47269 | 221 |
to individual assumptions by @{text"assms(1)"}, @{text"assms(2)"} etc, |
222 |
thus obviating the need to name them individually. |
|
223 |
||
52361 | 224 |
\section{Proof Patterns} |
47269 | 225 |
|
226 |
We show a number of important basic proof patterns. Many of them arise from |
|
227 |
the rules of natural deduction that are applied by \isacom{proof} by |
|
228 |
default. The patterns are phrased in terms of \isacom{show} but work for |
|
229 |
\isacom{have} and \isacom{lemma}, too. |
|
230 |
||
47711 | 231 |
We start with two forms of \concept{case analysis}: |
47269 | 232 |
starting from a formula @{text P} we have the two cases @{text P} and |
233 |
@{prop"~P"}, and starting from a fact @{prop"P \<or> Q"} |
|
234 |
we have the two cases @{text P} and @{text Q}: |
|
235 |
*}text_raw{* |
|
236 |
\begin{tabular}{@ {}ll@ {}} |
|
237 |
\begin{minipage}[t]{.4\textwidth} |
|
238 |
\isa{% |
|
239 |
*} |
|
240 |
(*<*)lemma "R" proof-(*>*) |
|
241 |
show "R" |
|
242 |
proof cases |
|
243 |
assume "P" |
|
244 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
245 |
show "R" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
246 |
next |
|
247 |
assume "\<not> P" |
|
248 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
249 |
show "R" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
250 |
qed(*<*)oops(*>*) |
|
251 |
text_raw {* } |
|
55361 | 252 |
\end{minipage}\index{cases@@{text cases}} |
47269 | 253 |
& |
254 |
\begin{minipage}[t]{.4\textwidth} |
|
255 |
\isa{% |
|
256 |
*} |
|
257 |
(*<*)lemma "R" proof-(*>*) |
|
258 |
have "P \<or> Q" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
259 |
then show "R" |
|
260 |
proof |
|
261 |
assume "P" |
|
262 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
263 |
show "R" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
264 |
next |
|
265 |
assume "Q" |
|
266 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
267 |
show "R" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
268 |
qed(*<*)oops(*>*) |
|
269 |
||
270 |
text_raw {* } |
|
271 |
\end{minipage} |
|
272 |
\end{tabular} |
|
273 |
\medskip |
|
274 |
\begin{isamarkuptext}% |
|
275 |
How to prove a logical equivalence: |
|
276 |
\end{isamarkuptext}% |
|
277 |
\isa{% |
|
278 |
*} |
|
279 |
(*<*)lemma "P\<longleftrightarrow>Q" proof-(*>*) |
|
280 |
show "P \<longleftrightarrow> Q" |
|
281 |
proof |
|
282 |
assume "P" |
|
283 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
284 |
show "Q" (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
285 |
next |
|
286 |
assume "Q" |
|
287 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
288 |
show "P" (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
289 |
qed(*<*)qed(*>*) |
|
290 |
text_raw {* } |
|
291 |
\medskip |
|
292 |
\begin{isamarkuptext}% |
|
55361 | 293 |
Proofs by contradiction (@{thm[source] ccontr} stands for ``classical contradiction''): |
47269 | 294 |
\end{isamarkuptext}% |
295 |
\begin{tabular}{@ {}ll@ {}} |
|
296 |
\begin{minipage}[t]{.4\textwidth} |
|
297 |
\isa{% |
|
298 |
*} |
|
299 |
(*<*)lemma "\<not> P" proof-(*>*) |
|
300 |
show "\<not> P" |
|
301 |
proof |
|
302 |
assume "P" |
|
303 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
304 |
show "False" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
305 |
qed(*<*)oops(*>*) |
|
306 |
||
307 |
text_raw {* } |
|
308 |
\end{minipage} |
|
309 |
& |
|
310 |
\begin{minipage}[t]{.4\textwidth} |
|
311 |
\isa{% |
|
312 |
*} |
|
313 |
(*<*)lemma "P" proof-(*>*) |
|
314 |
show "P" |
|
315 |
proof (rule ccontr) |
|
316 |
assume "\<not>P" |
|
317 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
318 |
show "False" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
319 |
qed(*<*)oops(*>*) |
|
320 |
||
321 |
text_raw {* } |
|
322 |
\end{minipage} |
|
323 |
\end{tabular} |
|
324 |
\medskip |
|
325 |
\begin{isamarkuptext}% |
|
326 |
How to prove quantified formulas: |
|
327 |
\end{isamarkuptext}% |
|
328 |
\begin{tabular}{@ {}ll@ {}} |
|
329 |
\begin{minipage}[t]{.4\textwidth} |
|
330 |
\isa{% |
|
331 |
*} |
|
332 |
(*<*)lemma "ALL x. P x" proof-(*>*) |
|
333 |
show "\<forall>x. P(x)" |
|
334 |
proof |
|
335 |
fix x |
|
336 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
337 |
show "P(x)" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
338 |
qed(*<*)oops(*>*) |
|
339 |
||
340 |
text_raw {* } |
|
341 |
\end{minipage} |
|
342 |
& |
|
343 |
\begin{minipage}[t]{.4\textwidth} |
|
344 |
\isa{% |
|
345 |
*} |
|
346 |
(*<*)lemma "EX x. P(x)" proof-(*>*) |
|
347 |
show "\<exists>x. P(x)" |
|
348 |
proof |
|
349 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
350 |
show "P(witness)" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
351 |
qed |
|
352 |
(*<*)oops(*>*) |
|
353 |
||
354 |
text_raw {* } |
|
355 |
\end{minipage} |
|
356 |
\end{tabular} |
|
357 |
\medskip |
|
358 |
\begin{isamarkuptext}% |
|
359 |
In the proof of \noquotes{@{prop[source]"\<forall>x. P(x)"}}, |
|
55361 | 360 |
the step \indexed{\isacom{fix}}{fix}~@{text x} introduces a locally fixed variable @{text x} |
47269 | 361 |
into the subproof, the proverbial ``arbitrary but fixed value''. |
362 |
Instead of @{text x} we could have chosen any name in the subproof. |
|
363 |
In the proof of \noquotes{@{prop[source]"\<exists>x. P(x)"}}, |
|
364 |
@{text witness} is some arbitrary |
|
365 |
term for which we can prove that it satisfies @{text P}. |
|
366 |
||
367 |
How to reason forward from \noquotes{@{prop[source] "\<exists>x. P(x)"}}: |
|
368 |
\end{isamarkuptext}% |
|
369 |
*} |
|
370 |
(*<*)lemma True proof- assume 1: "EX x. P x"(*>*) |
|
371 |
have "\<exists>x. P(x)" (*<*)by(rule 1)(*>*)txt_raw{*\ $\dots$\\*} |
|
372 |
then obtain x where p: "P(x)" by blast |
|
373 |
(*<*)oops(*>*) |
|
374 |
text{* |
|
375 |
After the \isacom{obtain} step, @{text x} (we could have chosen any name) |
|
376 |
is a fixed local |
|
377 |
variable, and @{text p} is the name of the fact |
|
378 |
\noquotes{@{prop[source] "P(x)"}}. |
|
379 |
This pattern works for one or more @{text x}. |
|
380 |
As an example of the \isacom{obtain} command, here is the proof of |
|
381 |
Cantor's theorem in more detail: |
|
382 |
*} |
|
383 |
||
384 |
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)" |
|
385 |
proof |
|
386 |
assume "surj f" |
|
387 |
hence "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def) |
|
388 |
then obtain a where "{x. x \<notin> f x} = f a" by blast |
|
389 |
hence "a \<notin> f a \<longleftrightarrow> a \<in> f a" by blast |
|
390 |
thus "False" by blast |
|
391 |
qed |
|
392 |
||
393 |
text_raw{* |
|
394 |
\begin{isamarkuptext}% |
|
47306 | 395 |
|
396 |
Finally, how to prove set equality and subset relationship: |
|
47269 | 397 |
\end{isamarkuptext}% |
398 |
\begin{tabular}{@ {}ll@ {}} |
|
399 |
\begin{minipage}[t]{.4\textwidth} |
|
400 |
\isa{% |
|
401 |
*} |
|
402 |
(*<*)lemma "A = (B::'a set)" proof-(*>*) |
|
403 |
show "A = B" |
|
404 |
proof |
|
405 |
show "A \<subseteq> B" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
406 |
next |
|
407 |
show "B \<subseteq> A" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
408 |
qed(*<*)qed(*>*) |
|
409 |
||
410 |
text_raw {* } |
|
411 |
\end{minipage} |
|
412 |
& |
|
413 |
\begin{minipage}[t]{.4\textwidth} |
|
414 |
\isa{% |
|
415 |
*} |
|
416 |
(*<*)lemma "A <= (B::'a set)" proof-(*>*) |
|
417 |
show "A \<subseteq> B" |
|
418 |
proof |
|
419 |
fix x |
|
420 |
assume "x \<in> A" |
|
421 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
422 |
show "x \<in> B" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
423 |
qed(*<*)qed(*>*) |
|
424 |
||
425 |
text_raw {* } |
|
426 |
\end{minipage} |
|
427 |
\end{tabular} |
|
428 |
\begin{isamarkuptext}% |
|
52361 | 429 |
\section{Streamlining Proofs} |
47269 | 430 |
|
52361 | 431 |
\subsection{Pattern Matching and Quotations} |
47269 | 432 |
|
433 |
In the proof patterns shown above, formulas are often duplicated. |
|
434 |
This can make the text harder to read, write and maintain. Pattern matching |
|
435 |
is an abbreviation mechanism to avoid such duplication. Writing |
|
436 |
\begin{quote} |
|
55359 | 437 |
\isacom{show} \ \textit{formula} @{text"("}\indexed{\isacom{is}}{is} \textit{pattern}@{text")"} |
47269 | 438 |
\end{quote} |
439 |
matches the pattern against the formula, thus instantiating the unknowns in |
|
440 |
the pattern for later use. As an example, consider the proof pattern for |
|
441 |
@{text"\<longleftrightarrow>"}: |
|
442 |
\end{isamarkuptext}% |
|
443 |
*} |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
444 |
(*<*)lemma "formula\<^sub>1 \<longleftrightarrow> formula\<^sub>2" proof-(*>*) |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
445 |
show "formula\<^sub>1 \<longleftrightarrow> formula\<^sub>2" (is "?L \<longleftrightarrow> ?R") |
47269 | 446 |
proof |
447 |
assume "?L" |
|
448 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
449 |
show "?R" (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
450 |
next |
|
451 |
assume "?R" |
|
452 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
453 |
show "?L" (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
454 |
qed(*<*)qed(*>*) |
|
455 |
||
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
456 |
text{* Instead of duplicating @{text"formula\<^sub>i"} in the text, we introduce |
47269 | 457 |
the two abbreviations @{text"?L"} and @{text"?R"} by pattern matching. |
458 |
Pattern matching works wherever a formula is stated, in particular |
|
459 |
with \isacom{have} and \isacom{lemma}. |
|
460 |
||
55359 | 461 |
The unknown \indexed{@{text"?thesis"}}{thesis} is implicitly matched against any goal stated by |
47269 | 462 |
\isacom{lemma} or \isacom{show}. Here is a typical example: *} |
463 |
||
464 |
lemma "formula" |
|
465 |
proof - |
|
466 |
txt_raw{*\\\mbox{}\quad$\vdots$\\\mbox{}\hspace{-1.4ex}*} |
|
467 |
show ?thesis (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
468 |
qed |
|
469 |
||
470 |
text{* |
|
55359 | 471 |
Unknowns can also be instantiated with \indexed{\isacom{let}}{let} commands |
47269 | 472 |
\begin{quote} |
473 |
\isacom{let} @{text"?t"} = @{text"\""}\textit{some-big-term}@{text"\""} |
|
474 |
\end{quote} |
|
475 |
Later proof steps can refer to @{text"?t"}: |
|
476 |
\begin{quote} |
|
477 |
\isacom{have} @{text"\""}\dots @{text"?t"} \dots@{text"\""} |
|
478 |
\end{quote} |
|
479 |
\begin{warn} |
|
480 |
Names of facts are introduced with @{text"name:"} and refer to proved |
|
481 |
theorems. Unknowns @{text"?X"} refer to terms or formulas. |
|
482 |
\end{warn} |
|
483 |
||
484 |
Although abbreviations shorten the text, the reader needs to remember what |
|
485 |
they stand for. Similarly for names of facts. Names like @{text 1}, @{text 2} |
|
486 |
and @{text 3} are not helpful and should only be used in short proofs. For |
|
47704 | 487 |
longer proofs, descriptive names are better. But look at this example: |
47269 | 488 |
\begin{quote} |
489 |
\isacom{have} \ @{text"x_gr_0: \"x > 0\""}\\ |
|
490 |
$\vdots$\\ |
|
491 |
\isacom{from} @{text "x_gr_0"} \dots |
|
492 |
\end{quote} |
|
493 |
The name is longer than the fact it stands for! Short facts do not need names, |
|
494 |
one can refer to them easily by quoting them: |
|
495 |
\begin{quote} |
|
496 |
\isacom{have} \ @{text"\"x > 0\""}\\ |
|
497 |
$\vdots$\\ |
|
55359 | 498 |
\isacom{from} @{text "`x>0`"} \dots\index{$IMP053@@{text"`...`"}} |
47269 | 499 |
\end{quote} |
55317 | 500 |
Note that the quotes around @{text"x>0"} are \conceptnoidx{back quotes}. |
47269 | 501 |
They refer to the fact not by name but by value. |
502 |
||
55359 | 503 |
\subsection{\indexed{\isacom{moreover}}{moreover}} |
504 |
\index{ultimately@\isacom{ultimately}} |
|
47269 | 505 |
|
506 |
Sometimes one needs a number of facts to enable some deduction. Of course |
|
507 |
one can name these facts individually, as shown on the right, |
|
508 |
but one can also combine them with \isacom{moreover}, as shown on the left: |
|
509 |
*}text_raw{* |
|
510 |
\begin{tabular}{@ {}ll@ {}} |
|
511 |
\begin{minipage}[t]{.4\textwidth} |
|
512 |
\isa{% |
|
513 |
*} |
|
514 |
(*<*)lemma "P" proof-(*>*) |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
515 |
have "P\<^sub>1" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
516 |
moreover have "P\<^sub>2" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
47269 | 517 |
moreover |
518 |
txt_raw{*\\$\vdots$\\\hspace{-1.4ex}*}(*<*)have "True" ..(*>*) |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
519 |
moreover have "P\<^sub>n" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
47269 | 520 |
ultimately have "P" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
521 |
(*<*)oops(*>*) |
|
522 |
||
523 |
text_raw {* } |
|
524 |
\end{minipage} |
|
525 |
& |
|
526 |
\qquad |
|
527 |
\begin{minipage}[t]{.4\textwidth} |
|
528 |
\isa{% |
|
529 |
*} |
|
530 |
(*<*)lemma "P" proof-(*>*) |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
531 |
have lab\<^sub>1: "P\<^sub>1" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
532 |
have lab\<^sub>2: "P\<^sub>2" (*<*)sorry(*>*)txt_raw{*\ $\dots$*} |
47269 | 533 |
txt_raw{*\\$\vdots$\\\hspace{-1.4ex}*} |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
534 |
have lab\<^sub>n: "P\<^sub>n" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
535 |
from lab\<^sub>1 lab\<^sub>2 txt_raw{*\ $\dots$\\*} |
47269 | 536 |
have "P" (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
537 |
(*<*)oops(*>*) |
|
538 |
||
539 |
text_raw {* } |
|
540 |
\end{minipage} |
|
541 |
\end{tabular} |
|
542 |
\begin{isamarkuptext}% |
|
543 |
The \isacom{moreover} version is no shorter but expresses the structure more |
|
544 |
clearly and avoids new names. |
|
545 |
||
52361 | 546 |
\subsection{Raw Proof Blocks} |
47269 | 547 |
|
47306 | 548 |
Sometimes one would like to prove some lemma locally within a proof. |
47269 | 549 |
A lemma that shares the current context of assumptions but that |
47711 | 550 |
has its own assumptions and is generalized over its locally fixed |
47269 | 551 |
variables at the end. This is what a \concept{raw proof block} does: |
55359 | 552 |
\begin{quote}\index{$IMP053@@{text"{ ... }"} (proof block)} |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
553 |
@{text"{"} \isacom{fix} @{text"x\<^sub>1 \<dots> x\<^sub>n"}\\ |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
554 |
\mbox{}\ \ \ \isacom{assume} @{text"A\<^sub>1 \<dots> A\<^sub>m"}\\ |
47269 | 555 |
\mbox{}\ \ \ $\vdots$\\ |
556 |
\mbox{}\ \ \ \isacom{have} @{text"B"}\\ |
|
557 |
@{text"}"} |
|
558 |
\end{quote} |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
559 |
proves @{text"\<lbrakk> A\<^sub>1; \<dots> ; A\<^sub>m \<rbrakk> \<Longrightarrow> B"} |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
560 |
where all @{text"x\<^sub>i"} have been replaced by unknowns @{text"?x\<^sub>i"}. |
47269 | 561 |
\begin{warn} |
562 |
The conclusion of a raw proof block is \emph{not} indicated by \isacom{show} |
|
563 |
but is simply the final \isacom{have}. |
|
564 |
\end{warn} |
|
565 |
||
566 |
As an example we prove a simple fact about divisibility on integers. |
|
567 |
The definition of @{text "dvd"} is @{thm dvd_def}. |
|
568 |
\end{isamarkuptext}% |
|
569 |
*} |
|
570 |
||
571 |
lemma fixes a b :: int assumes "b dvd (a+b)" shows "b dvd a" |
|
572 |
proof - |
|
573 |
{ fix k assume k: "a+b = b*k" |
|
574 |
have "\<exists>k'. a = b*k'" |
|
575 |
proof |
|
576 |
show "a = b*(k - 1)" using k by(simp add: algebra_simps) |
|
577 |
qed } |
|
578 |
then show ?thesis using assms by(auto simp add: dvd_def) |
|
579 |
qed |
|
580 |
||
581 |
text{* Note that the result of a raw proof block has no name. In this example |
|
582 |
it was directly piped (via \isacom{then}) into the final proof, but it can |
|
583 |
also be named for later reference: you simply follow the block directly by a |
|
584 |
\isacom{note} command: |
|
585 |
\begin{quote} |
|
55359 | 586 |
\indexed{\isacom{note}}{note} \ @{text"name = this"} |
47269 | 587 |
\end{quote} |
588 |
This introduces a new name @{text name} that refers to @{text this}, |
|
589 |
the fact just proved, in this case the preceding block. In general, |
|
590 |
\isacom{note} introduces a new name for one or more facts. |
|
591 |
||
54436 | 592 |
\subsection*{Exercises} |
52706 | 593 |
|
52661 | 594 |
\exercise |
595 |
Give a readable, structured proof of the following lemma: |
|
596 |
*} |
|
54218 | 597 |
lemma assumes T: "\<forall>x y. T x y \<or> T y x" |
598 |
and A: "\<forall>x y. A x y \<and> A y x \<longrightarrow> x = y" |
|
599 |
and TA: "\<forall>x y. T x y \<longrightarrow> A x y" and "A x y" |
|
600 |
shows "T x y" |
|
52661 | 601 |
(*<*)oops(*>*) |
602 |
text{* |
|
603 |
\endexercise |
|
604 |
||
52706 | 605 |
\exercise |
606 |
Give a readable, structured proof of the following lemma: |
|
607 |
*} |
|
608 |
lemma "(\<exists>ys zs. xs = ys @ zs \<and> length ys = length zs) |
|
609 |
\<or> (\<exists>ys zs. xs = ys @ zs \<and> length ys = length zs + 1)" |
|
610 |
(*<*)oops(*>*) |
|
611 |
text{* |
|
612 |
Hint: There are predefined functions @{const_typ take} and @{const_typ drop} |
|
613 |
such that @{text"take k [x\<^sub>1,\<dots>] = [x\<^sub>1,\<dots>,x\<^sub>k]"} and |
|
54218 | 614 |
@{text"drop k [x\<^sub>1,\<dots>] = [x\<^bsub>k+1\<^esub>,\<dots>]"}. Let sledgehammer find and apply |
615 |
the relevant @{const take} and @{const drop} lemmas for you. |
|
52706 | 616 |
\endexercise |
617 |
||
54218 | 618 |
|
52361 | 619 |
\section{Case Analysis and Induction} |
55361 | 620 |
|
52361 | 621 |
\subsection{Datatype Case Analysis} |
55361 | 622 |
\index{case analysis|(} |
47269 | 623 |
|
47711 | 624 |
We have seen case analysis on formulas. Now we want to distinguish |
47269 | 625 |
which form some term takes: is it @{text 0} or of the form @{term"Suc n"}, |
626 |
is it @{term"[]"} or of the form @{term"x#xs"}, etc. Here is a typical example |
|
47711 | 627 |
proof by case analysis on the form of @{text xs}: |
47269 | 628 |
*} |
629 |
||
630 |
lemma "length(tl xs) = length xs - 1" |
|
631 |
proof (cases xs) |
|
632 |
assume "xs = []" |
|
633 |
thus ?thesis by simp |
|
634 |
next |
|
635 |
fix y ys assume "xs = y#ys" |
|
636 |
thus ?thesis by simp |
|
637 |
qed |
|
638 |
||
55361 | 639 |
text{*\index{cases@@{text"cases"}|(}Function @{text tl} (''tail'') is defined by @{thm tl.simps(1)} and |
47269 | 640 |
@{thm tl.simps(2)}. Note that the result type of @{const length} is @{typ nat} |
641 |
and @{prop"0 - 1 = (0::nat)"}. |
|
642 |
||
643 |
This proof pattern works for any term @{text t} whose type is a datatype. |
|
644 |
The goal has to be proved for each constructor @{text C}: |
|
645 |
\begin{quote} |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
646 |
\isacom{fix} \ @{text"x\<^sub>1 \<dots> x\<^sub>n"} \isacom{assume} @{text"\"t = C x\<^sub>1 \<dots> x\<^sub>n\""} |
55361 | 647 |
\end{quote}\index{case@\isacom{case}|(} |
47269 | 648 |
Each case can be written in a more compact form by means of the \isacom{case} |
649 |
command: |
|
650 |
\begin{quote} |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
651 |
\isacom{case} @{text "(C x\<^sub>1 \<dots> x\<^sub>n)"} |
47269 | 652 |
\end{quote} |
47704 | 653 |
This is equivalent to the explicit \isacom{fix}-\isacom{assume} line |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
654 |
but also gives the assumption @{text"\"t = C x\<^sub>1 \<dots> x\<^sub>n\""} a name: @{text C}, |
47269 | 655 |
like the constructor. |
656 |
Here is the \isacom{case} version of the proof above: |
|
657 |
*} |
|
658 |
(*<*)lemma "length(tl xs) = length xs - 1"(*>*) |
|
659 |
proof (cases xs) |
|
660 |
case Nil |
|
661 |
thus ?thesis by simp |
|
662 |
next |
|
663 |
case (Cons y ys) |
|
664 |
thus ?thesis by simp |
|
665 |
qed |
|
666 |
||
667 |
text{* Remember that @{text Nil} and @{text Cons} are the alphanumeric names |
|
668 |
for @{text"[]"} and @{text"#"}. The names of the assumptions |
|
669 |
are not used because they are directly piped (via \isacom{thus}) |
|
670 |
into the proof of the claim. |
|
55361 | 671 |
\index{case analysis|)} |
47269 | 672 |
|
52361 | 673 |
\subsection{Structural Induction} |
55361 | 674 |
\index{induction|(} |
675 |
\index{structural induction|(} |
|
47269 | 676 |
|
677 |
We illustrate structural induction with an example based on natural numbers: |
|
678 |
the sum (@{text"\<Sum>"}) of the first @{text n} natural numbers |
|
679 |
(@{text"{0..n::nat}"}) is equal to \mbox{@{term"n*(n+1) div 2::nat"}}. |
|
680 |
Never mind the details, just focus on the pattern: |
|
681 |
*} |
|
682 |
||
47711 | 683 |
lemma "\<Sum>{0..n::nat} = n*(n+1) div 2" |
47269 | 684 |
proof (induction n) |
685 |
show "\<Sum>{0..0::nat} = 0*(0+1) div 2" by simp |
|
686 |
next |
|
687 |
fix n assume "\<Sum>{0..n::nat} = n*(n+1) div 2" |
|
47711 | 688 |
thus "\<Sum>{0..Suc n} = Suc n*(Suc n+1) div 2" by simp |
47269 | 689 |
qed |
690 |
||
691 |
text{* Except for the rewrite steps, everything is explicitly given. This |
|
692 |
makes the proof easily readable, but the duplication means it is tedious to |
|
693 |
write and maintain. Here is how pattern |
|
694 |
matching can completely avoid any duplication: *} |
|
695 |
||
696 |
lemma "\<Sum>{0..n::nat} = n*(n+1) div 2" (is "?P n") |
|
697 |
proof (induction n) |
|
698 |
show "?P 0" by simp |
|
699 |
next |
|
700 |
fix n assume "?P n" |
|
701 |
thus "?P(Suc n)" by simp |
|
702 |
qed |
|
703 |
||
704 |
text{* The first line introduces an abbreviation @{text"?P n"} for the goal. |
|
705 |
Pattern matching @{text"?P n"} with the goal instantiates @{text"?P"} to the |
|
706 |
function @{term"\<lambda>n. \<Sum>{0..n::nat} = n*(n+1) div 2"}. Now the proposition to |
|
707 |
be proved in the base case can be written as @{text"?P 0"}, the induction |
|
708 |
hypothesis as @{text"?P n"}, and the conclusion of the induction step as |
|
709 |
@{text"?P(Suc n)"}. |
|
710 |
||
711 |
Induction also provides the \isacom{case} idiom that abbreviates |
|
712 |
the \isacom{fix}-\isacom{assume} step. The above proof becomes |
|
713 |
*} |
|
714 |
(*<*)lemma "\<Sum>{0..n::nat} = n*(n+1) div 2"(*>*) |
|
715 |
proof (induction n) |
|
716 |
case 0 |
|
717 |
show ?case by simp |
|
718 |
next |
|
719 |
case (Suc n) |
|
720 |
thus ?case by simp |
|
721 |
qed |
|
722 |
||
723 |
text{* |
|
55361 | 724 |
The unknown @{text"?case"}\index{case?@@{text"?case"}|(} is set in each case to the required |
47269 | 725 |
claim, i.e.\ @{text"?P 0"} and \mbox{@{text"?P(Suc n)"}} in the above proof, |
726 |
without requiring the user to define a @{text "?P"}. The general |
|
727 |
pattern for induction over @{typ nat} is shown on the left-hand side: |
|
728 |
*}text_raw{* |
|
729 |
\begin{tabular}{@ {}ll@ {}} |
|
730 |
\begin{minipage}[t]{.4\textwidth} |
|
731 |
\isa{% |
|
732 |
*} |
|
733 |
(*<*)lemma "P(n::nat)" proof -(*>*) |
|
734 |
show "P(n)" |
|
735 |
proof (induction n) |
|
736 |
case 0 |
|
737 |
txt_raw{*\\\mbox{}\ \ $\vdots$\\\mbox{}\hspace{-1ex}*} |
|
738 |
show ?case (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
739 |
next |
|
740 |
case (Suc n) |
|
741 |
txt_raw{*\\\mbox{}\ \ $\vdots$\\\mbox{}\hspace{-1ex}*} |
|
742 |
show ?case (*<*)sorry(*>*) txt_raw{*\ $\dots$\\*} |
|
743 |
qed(*<*)qed(*>*) |
|
744 |
||
745 |
text_raw {* } |
|
746 |
\end{minipage} |
|
747 |
& |
|
748 |
\begin{minipage}[t]{.4\textwidth} |
|
749 |
~\\ |
|
750 |
~\\ |
|
751 |
\isacom{let} @{text"?case = \"P(0)\""}\\ |
|
752 |
~\\ |
|
753 |
~\\ |
|
754 |
~\\[1ex] |
|
755 |
\isacom{fix} @{text n} \isacom{assume} @{text"Suc: \"P(n)\""}\\ |
|
756 |
\isacom{let} @{text"?case = \"P(Suc n)\""}\\ |
|
757 |
\end{minipage} |
|
758 |
\end{tabular} |
|
759 |
\medskip |
|
760 |
*} |
|
761 |
text{* |
|
762 |
On the right side you can see what the \isacom{case} command |
|
763 |
on the left stands for. |
|
764 |
||
765 |
In case the goal is an implication, induction does one more thing: the |
|
766 |
proposition to be proved in each case is not the whole implication but only |
|
767 |
its conclusion; the premises of the implication are immediately made |
|
768 |
assumptions of that case. That is, if in the above proof we replace |
|
49837 | 769 |
\isacom{show}~@{text"\"P(n)\""} by |
770 |
\mbox{\isacom{show}~@{text"\"A(n) \<Longrightarrow> P(n)\""}} |
|
47269 | 771 |
then \isacom{case}~@{text 0} stands for |
772 |
\begin{quote} |
|
773 |
\isacom{assume} \ @{text"0: \"A(0)\""}\\ |
|
774 |
\isacom{let} @{text"?case = \"P(0)\""} |
|
775 |
\end{quote} |
|
776 |
and \isacom{case}~@{text"(Suc n)"} stands for |
|
777 |
\begin{quote} |
|
778 |
\isacom{fix} @{text n}\\ |
|
779 |
\isacom{assume} @{text"Suc:"} |
|
47306 | 780 |
\begin{tabular}[t]{l}@{text"\"A(n) \<Longrightarrow> P(n)\""}\\@{text"\"A(Suc n)\""}\end{tabular}\\ |
47269 | 781 |
\isacom{let} @{text"?case = \"P(Suc n)\""} |
782 |
\end{quote} |
|
783 |
The list of assumptions @{text Suc} is actually subdivided |
|
784 |
into @{text"Suc.IH"}, the induction hypotheses (here @{text"A(n) \<Longrightarrow> P(n)"}) |
|
785 |
and @{text"Suc.prems"}, the premises of the goal being proved |
|
786 |
(here @{text"A(Suc n)"}). |
|
787 |
||
788 |
Induction works for any datatype. |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
789 |
Proving a goal @{text"\<lbrakk> A\<^sub>1(x); \<dots>; A\<^sub>k(x) \<rbrakk> \<Longrightarrow> P(x)"} |
47269 | 790 |
by induction on @{text x} generates a proof obligation for each constructor |
55361 | 791 |
@{text C} of the datatype. The command \isacom{case}~@{text"(C x\<^sub>1 \<dots> x\<^sub>n)"} |
47269 | 792 |
performs the following steps: |
793 |
\begin{enumerate} |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
794 |
\item \isacom{fix} @{text"x\<^sub>1 \<dots> x\<^sub>n"} |
55361 | 795 |
\item \isacom{assume} the induction hypotheses (calling them @{text C.IH}\index{IH@@{text".IH"}}) |
796 |
and the premises \mbox{@{text"A\<^sub>i(C x\<^sub>1 \<dots> x\<^sub>n)"}} (calling them @{text"C.prems"}\index{prems@@{text".prems"}}) |
|
47269 | 797 |
and calling the whole list @{text C} |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
798 |
\item \isacom{let} @{text"?case = \"P(C x\<^sub>1 \<dots> x\<^sub>n)\""} |
47269 | 799 |
\end{enumerate} |
55361 | 800 |
\index{structural induction|)} |
47269 | 801 |
|
52361 | 802 |
\subsection{Rule Induction} |
55361 | 803 |
\index{rule induction|(} |
47269 | 804 |
|
805 |
Recall the inductive and recursive definitions of even numbers in |
|
806 |
\autoref{sec:inductive-defs}: |
|
807 |
*} |
|
808 |
||
809 |
inductive ev :: "nat \<Rightarrow> bool" where |
|
810 |
ev0: "ev 0" | |
|
811 |
evSS: "ev n \<Longrightarrow> ev(Suc(Suc n))" |
|
812 |
||
813 |
fun even :: "nat \<Rightarrow> bool" where |
|
814 |
"even 0 = True" | |
|
815 |
"even (Suc 0) = False" | |
|
816 |
"even (Suc(Suc n)) = even n" |
|
817 |
||
818 |
text{* We recast the proof of @{prop"ev n \<Longrightarrow> even n"} in Isar. The |
|
819 |
left column shows the actual proof text, the right column shows |
|
820 |
the implicit effect of the two \isacom{case} commands:*}text_raw{* |
|
821 |
\begin{tabular}{@ {}l@ {\qquad}l@ {}} |
|
822 |
\begin{minipage}[t]{.5\textwidth} |
|
823 |
\isa{% |
|
824 |
*} |
|
825 |
||
826 |
lemma "ev n \<Longrightarrow> even n" |
|
827 |
proof(induction rule: ev.induct) |
|
828 |
case ev0 |
|
829 |
show ?case by simp |
|
830 |
next |
|
831 |
case evSS |
|
832 |
||
833 |
||
834 |
||
835 |
thus ?case by simp |
|
836 |
qed |
|
837 |
||
838 |
text_raw {* } |
|
839 |
\end{minipage} |
|
840 |
& |
|
841 |
\begin{minipage}[t]{.5\textwidth} |
|
842 |
~\\ |
|
843 |
~\\ |
|
844 |
\isacom{let} @{text"?case = \"even 0\""}\\ |
|
845 |
~\\ |
|
846 |
~\\ |
|
847 |
\isacom{fix} @{text n}\\ |
|
848 |
\isacom{assume} @{text"evSS:"} |
|
47306 | 849 |
\begin{tabular}[t]{l} @{text"\"ev n\""}\\@{text"\"even n\""}\end{tabular}\\ |
850 |
\isacom{let} @{text"?case = \"even(Suc(Suc n))\""}\\ |
|
47269 | 851 |
\end{minipage} |
852 |
\end{tabular} |
|
853 |
\medskip |
|
854 |
*} |
|
855 |
text{* |
|
856 |
The proof resembles structural induction, but the induction rule is given |
|
857 |
explicitly and the names of the cases are the names of the rules in the |
|
858 |
inductive definition. |
|
859 |
Let us examine the two assumptions named @{thm[source]evSS}: |
|
860 |
@{prop "ev n"} is the premise of rule @{thm[source]evSS}, which we may assume |
|
861 |
because we are in the case where that rule was used; @{prop"even n"} |
|
862 |
is the induction hypothesis. |
|
863 |
\begin{warn} |
|
864 |
Because each \isacom{case} command introduces a list of assumptions |
|
865 |
named like the case name, which is the name of a rule of the inductive |
|
866 |
definition, those rules now need to be accessed with a qualified name, here |
|
867 |
@{thm[source] ev.ev0} and @{thm[source] ev.evSS} |
|
868 |
\end{warn} |
|
869 |
||
870 |
In the case @{thm[source]evSS} of the proof above we have pretended that the |
|
871 |
system fixes a variable @{text n}. But unless the user provides the name |
|
872 |
@{text n}, the system will just invent its own name that cannot be referred |
|
873 |
to. In the above proof, we do not need to refer to it, hence we do not give |
|
874 |
it a specific name. In case one needs to refer to it one writes |
|
875 |
\begin{quote} |
|
876 |
\isacom{case} @{text"(evSS m)"} |
|
877 |
\end{quote} |
|
878 |
just like \isacom{case}~@{text"(Suc n)"} in earlier structural inductions. |
|
879 |
The name @{text m} is an arbitrary choice. As a result, |
|
880 |
case @{thm[source] evSS} is derived from a renamed version of |
|
881 |
rule @{thm[source] evSS}: @{text"ev m \<Longrightarrow> ev(Suc(Suc m))"}. |
|
882 |
Here is an example with a (contrived) intermediate step that refers to @{text m}: |
|
883 |
*} |
|
884 |
||
885 |
lemma "ev n \<Longrightarrow> even n" |
|
886 |
proof(induction rule: ev.induct) |
|
887 |
case ev0 show ?case by simp |
|
888 |
next |
|
889 |
case (evSS m) |
|
890 |
have "even(Suc(Suc m)) = even m" by simp |
|
891 |
thus ?case using `even m` by blast |
|
892 |
qed |
|
893 |
||
894 |
text{* |
|
895 |
\indent |
|
896 |
In general, let @{text I} be a (for simplicity unary) inductively defined |
|
897 |
predicate and let the rules in the definition of @{text I} |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
898 |
be called @{text "rule\<^sub>1"}, \dots, @{text "rule\<^sub>n"}. A proof by rule |
55361 | 899 |
induction follows this pattern:\index{inductionrule@@{text"induction ... rule:"}} |
47269 | 900 |
*} |
901 |
||
902 |
(*<*) |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
903 |
inductive I where rule\<^sub>1: "I()" | rule\<^sub>2: "I()" | rule\<^sub>n: "I()" |
47269 | 904 |
lemma "I x \<Longrightarrow> P x" proof-(*>*) |
905 |
show "I x \<Longrightarrow> P x" |
|
906 |
proof(induction rule: I.induct) |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
907 |
case rule\<^sub>1 |
47269 | 908 |
txt_raw{*\\[-.4ex]\mbox{}\ \ $\vdots$\\[-.4ex]\mbox{}\hspace{-1ex}*} |
909 |
show ?case (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
910 |
next |
|
911 |
txt_raw{*\\[-.4ex]$\vdots$\\[-.4ex]\mbox{}\hspace{-1ex}*} |
|
912 |
(*<*) |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
913 |
case rule\<^sub>2 |
47269 | 914 |
show ?case sorry |
915 |
(*>*) |
|
916 |
next |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
917 |
case rule\<^sub>n |
47269 | 918 |
txt_raw{*\\[-.4ex]\mbox{}\ \ $\vdots$\\[-.4ex]\mbox{}\hspace{-1ex}*} |
919 |
show ?case (*<*)sorry(*>*)txt_raw{*\ $\dots$\\*} |
|
920 |
qed(*<*)qed(*>*) |
|
921 |
||
922 |
text{* |
|
923 |
One can provide explicit variable names by writing |
|
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
924 |
\isacom{case}~@{text"(rule\<^sub>i x\<^sub>1 \<dots> x\<^sub>k)"}, thus renaming the first @{text k} |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
925 |
free variables in rule @{text i} to @{text"x\<^sub>1 \<dots> x\<^sub>k"}, |
47269 | 926 |
going through rule @{text i} from left to right. |
927 |
||
52361 | 928 |
\subsection{Assumption Naming} |
51443 | 929 |
\label{sec:assm-naming} |
47269 | 930 |
|
931 |
In any induction, \isacom{case}~@{text name} sets up a list of assumptions |
|
932 |
also called @{text name}, which is subdivided into three parts: |
|
933 |
\begin{description} |
|
55361 | 934 |
\item[@{text name.IH}]\index{IH@@{text".IH"}} contains the induction hypotheses. |
935 |
\item[@{text name.hyps}]\index{hyps@@{text".hyps"}} contains all the other hypotheses of this case in the |
|
47269 | 936 |
induction rule. For rule inductions these are the hypotheses of rule |
937 |
@{text name}, for structural inductions these are empty. |
|
55361 | 938 |
\item[@{text name.prems}]\index{prems@@{text".prems"}} contains the (suitably instantiated) premises |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
939 |
of the statement being proved, i.e. the @{text A\<^sub>i} when |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52718
diff
changeset
|
940 |
proving @{text"\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A"}. |
47269 | 941 |
\end{description} |
942 |
\begin{warn} |
|
943 |
Proof method @{text induct} differs from @{text induction} |
|
944 |
only in this naming policy: @{text induct} does not distinguish |
|
945 |
@{text IH} from @{text hyps} but subsumes @{text IH} under @{text hyps}. |
|
946 |
\end{warn} |
|
947 |
||
948 |
More complicated inductive proofs than the ones we have seen so far |
|
949 |
often need to refer to specific assumptions---just @{text name} or even |
|
950 |
@{text name.prems} and @{text name.IH} can be too unspecific. |
|
951 |
This is where the indexing of fact lists comes in handy, e.g.\ |
|
952 |
@{text"name.IH(2)"} or @{text"name.prems(1-2)"}. |
|
953 |
||
52361 | 954 |
\subsection{Rule Inversion} |
955 |
\label{sec:rule-inversion} |
|
55361 | 956 |
\index{rule inversion|(} |
47269 | 957 |
|
47711 | 958 |
Rule inversion is case analysis of which rule could have been used to |
55361 | 959 |
derive some fact. The name \conceptnoidx{rule inversion} emphasizes that we are |
47269 | 960 |
reasoning backwards: by which rules could some given fact have been proved? |
961 |
For the inductive definition of @{const ev}, rule inversion can be summarized |
|
962 |
like this: |
|
963 |
@{prop[display]"ev n \<Longrightarrow> n = 0 \<or> (EX k. n = Suc(Suc k) \<and> ev k)"} |
|
47711 | 964 |
The realisation in Isabelle is a case analysis. |
47269 | 965 |
A simple example is the proof that @{prop"ev n \<Longrightarrow> ev (n - 2)"}. We |
966 |
already went through the details informally in \autoref{sec:Logic:even}. This |
|
967 |
is the Isar proof: |
|
968 |
*} |
|
969 |
(*<*) |
|
970 |
notepad |
|
971 |
begin fix n |
|
972 |
(*>*) |
|
973 |
assume "ev n" |
|
974 |
from this have "ev(n - 2)" |
|
975 |
proof cases |
|
976 |
case ev0 thus "ev(n - 2)" by (simp add: ev.ev0) |
|
977 |
next |
|
978 |
case (evSS k) thus "ev(n - 2)" by (simp add: ev.evSS) |
|
979 |
qed |
|
980 |
(*<*) |
|
981 |
end |
|
982 |
(*>*) |
|
983 |
||
47711 | 984 |
text{* The key point here is that a case analysis over some inductively |
47269 | 985 |
defined predicate is triggered by piping the given fact |
986 |
(here: \isacom{from}~@{text this}) into a proof by @{text cases}. |
|
987 |
Let us examine the assumptions available in each case. In case @{text ev0} |
|
988 |
we have @{text"n = 0"} and in case @{text evSS} we have @{prop"n = Suc(Suc k)"} |
|
989 |
and @{prop"ev k"}. In each case the assumptions are available under the name |
|
990 |
of the case; there is no fine grained naming schema like for induction. |
|
991 |
||
47704 | 992 |
Sometimes some rules could not have been used to derive the given fact |
47269 | 993 |
because constructors clash. As an extreme example consider |
994 |
rule inversion applied to @{prop"ev(Suc 0)"}: neither rule @{text ev0} nor |
|
995 |
rule @{text evSS} can yield @{prop"ev(Suc 0)"} because @{text"Suc 0"} unifies |
|
996 |
neither with @{text 0} nor with @{term"Suc(Suc n)"}. Impossible cases do not |
|
997 |
have to be proved. Hence we can prove anything from @{prop"ev(Suc 0)"}: |
|
998 |
*} |
|
999 |
(*<*) |
|
1000 |
notepad begin fix P |
|
1001 |
(*>*) |
|
1002 |
assume "ev(Suc 0)" then have P by cases |
|
1003 |
(*<*) |
|
1004 |
end |
|
1005 |
(*>*) |
|
1006 |
||
1007 |
text{* That is, @{prop"ev(Suc 0)"} is simply not provable: *} |
|
1008 |
||
1009 |
lemma "\<not> ev(Suc 0)" |
|
1010 |
proof |
|
1011 |
assume "ev(Suc 0)" then show False by cases |
|
1012 |
qed |
|
1013 |
||
1014 |
text{* Normally not all cases will be impossible. As a simple exercise, |
|
1015 |
prove that \mbox{@{prop"\<not> ev(Suc(Suc(Suc 0)))"}.} |
|
51443 | 1016 |
|
52361 | 1017 |
\subsection{Advanced Rule Induction} |
51445 | 1018 |
\label{sec:advanced-rule-induction} |
51443 | 1019 |
|
1020 |
So far, rule induction was always applied to goals of the form @{text"I x y z \<Longrightarrow> \<dots>"} |
|
1021 |
where @{text I} is some inductively defined predicate and @{text x}, @{text y}, @{text z} |
|
1022 |
are variables. In some rare situations one needs to deal with an assumption where |
|
1023 |
not all arguments @{text r}, @{text s}, @{text t} are variables: |
|
1024 |
\begin{isabelle} |
|
1025 |
\isacom{lemma} @{text[source]"I r s t \<Longrightarrow> \<dots>"} |
|
1026 |
\end{isabelle} |
|
1027 |
Applying the standard form of |
|
54577 | 1028 |
rule induction in such a situation will lead to strange and typically unprovable goals. |
51443 | 1029 |
We can easily reduce this situation to the standard one by introducing |
1030 |
new variables @{text x}, @{text y}, @{text z} and reformulating the goal like this: |
|
1031 |
\begin{isabelle} |
|
1032 |
\isacom{lemma} @{text[source]"I x y z \<Longrightarrow> x = r \<Longrightarrow> y = s \<Longrightarrow> z = t \<Longrightarrow> \<dots>"} |
|
1033 |
\end{isabelle} |
|
1034 |
Standard rule induction will worke fine now, provided the free variables in |
|
1035 |
@{text r}, @{text s}, @{text t} are generalized via @{text"arbitrary"}. |
|
1036 |
||
1037 |
However, induction can do the above transformation for us, behind the curtains, so we never |
|
1038 |
need to see the expanded version of the lemma. This is what we need to write: |
|
1039 |
\begin{isabelle} |
|
1040 |
\isacom{lemma} @{text[source]"I r s t \<Longrightarrow> \<dots>"}\isanewline |
|
55361 | 1041 |
\isacom{proof}@{text"(induction \"r\" \"s\" \"t\" arbitrary: \<dots> rule: I.induct)"}\index{inductionrule@@{text"induction ... rule:"}}\index{arbitrary@@{text"arbitrary:"}} |
51443 | 1042 |
\end{isabelle} |
1043 |
Just like for rule inversion, cases that are impossible because of constructor clashes |
|
1044 |
will not show up at all. Here is a concrete example: *} |
|
1045 |
||
1046 |
lemma "ev (Suc m) \<Longrightarrow> \<not> ev m" |
|
1047 |
proof(induction "Suc m" arbitrary: m rule: ev.induct) |
|
1048 |
fix n assume IH: "\<And>m. n = Suc m \<Longrightarrow> \<not> ev m" |
|
1049 |
show "\<not> ev (Suc n)" |
|
54577 | 1050 |
proof --"contradiction" |
51443 | 1051 |
assume "ev(Suc n)" |
1052 |
thus False |
|
1053 |
proof cases --"rule inversion" |
|
1054 |
fix k assume "n = Suc k" "ev k" |
|
1055 |
thus False using IH by auto |
|
1056 |
qed |
|
1057 |
qed |
|
1058 |
qed |
|
1059 |
||
1060 |
text{* |
|
1061 |
Remarks: |
|
1062 |
\begin{itemize} |
|
1063 |
\item |
|
1064 |
Instead of the \isacom{case} and @{text ?case} magic we have spelled all formulas out. |
|
1065 |
This is merely for greater clarity. |
|
1066 |
\item |
|
1067 |
We only need to deal with one case because the @{thm[source] ev0} case is impossible. |
|
1068 |
\item |
|
1069 |
The form of the @{text IH} shows us that internally the lemma was expanded as explained |
|
1070 |
above: \noquotes{@{prop[source]"ev x \<Longrightarrow> x = Suc m \<Longrightarrow> \<not> ev m"}}. |
|
1071 |
\item |
|
1072 |
The goal @{prop"\<not> ev (Suc n)"} may suprise. The expanded version of the lemma |
|
1073 |
would suggest that we have a \isacom{fix} @{text m} \isacom{assume} @{prop"Suc(Suc n) = Suc m"} |
|
1074 |
and need to show @{prop"\<not> ev m"}. What happened is that Isabelle immediately |
|
1075 |
simplified @{prop"Suc(Suc n) = Suc m"} to @{prop"Suc n = m"} and could then eliminate |
|
1076 |
@{text m}. Beware of such nice surprises with this advanced form of induction. |
|
1077 |
\end{itemize} |
|
1078 |
\begin{warn} |
|
1079 |
This advanced form of induction does not support the @{text IH} |
|
1080 |
naming schema explained in \autoref{sec:assm-naming}: |
|
1081 |
the induction hypotheses are instead found under the name @{text hyps}, like for the simpler |
|
1082 |
@{text induct} method. |
|
1083 |
\end{warn} |
|
55361 | 1084 |
\index{induction|)} |
1085 |
\index{cases@@{text"cases"}|)} |
|
1086 |
\index{case@\isacom{case}|)} |
|
1087 |
\index{case?@@{text"?case"}|)} |
|
1088 |
\index{rule induction|)} |
|
1089 |
\index{rule inversion|)} |
|
54218 | 1090 |
|
54436 | 1091 |
\subsection*{Exercises} |
52593 | 1092 |
|
54232 | 1093 |
|
1094 |
\exercise |
|
54292 | 1095 |
Give a structured proof by rule inversion: |
54232 | 1096 |
*} |
1097 |
||
1098 |
lemma assumes a: "ev(Suc(Suc n))" shows "ev n" |
|
1099 |
(*<*)oops(*>*) |
|
1100 |
||
1101 |
text{* |
|
1102 |
\endexercise |
|
1103 |
||
52593 | 1104 |
\begin{exercise} |
54232 | 1105 |
Give a structured proof of @{prop "\<not> ev(Suc(Suc(Suc 0)))"} |
1106 |
by rule inversions. If there are no cases to be proved you can close |
|
54218 | 1107 |
a proof immediateley with \isacom{qed}. |
1108 |
\end{exercise} |
|
1109 |
||
1110 |
\begin{exercise} |
|
54292 | 1111 |
Recall predicate @{text star} from \autoref{sec:star} and @{text iter} |
1112 |
from Exercise~\ref{exe:iter}. Prove @{prop "iter r n x y \<Longrightarrow> star r x y"} |
|
1113 |
in a structured style, do not just sledgehammer each case of the |
|
1114 |
required induction. |
|
1115 |
\end{exercise} |
|
1116 |
||
1117 |
\begin{exercise} |
|
52593 | 1118 |
Define a recursive function @{text "elems ::"} @{typ"'a list \<Rightarrow> 'a set"} |
1119 |
and prove @{prop "x : elems xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> elems ys"}. |
|
1120 |
\end{exercise} |
|
47269 | 1121 |
*} |
1122 |
||
1123 |
(*<*) |
|
1124 |
end |
|
1125 |
(*>*) |