13999
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Induction}%
|
|
4 |
\isamarkupfalse%
|
|
5 |
%
|
|
6 |
\isamarkupsection{Case distinction and induction \label{sec:Induct}%
|
|
7 |
}
|
|
8 |
\isamarkuptrue%
|
|
9 |
%
|
|
10 |
\begin{isamarkuptext}%
|
|
11 |
Computer science applications abound with inductively defined
|
|
12 |
structures, which is why we treat them in more detail. HOL already
|
|
13 |
comes with a datatype of lists with the two constructors \isa{Nil}
|
|
14 |
and \isa{Cons}. \isa{Nil} is written \isa{{\isacharbrackleft}{\isacharbrackright}} and \isa{Cons\ x\ xs} is written \isa{x\ {\isacharhash}\ xs}.%
|
|
15 |
\end{isamarkuptext}%
|
|
16 |
\isamarkuptrue%
|
|
17 |
%
|
|
18 |
\isamarkupsubsection{Case distinction\label{sec:CaseDistinction}%
|
|
19 |
}
|
|
20 |
\isamarkuptrue%
|
|
21 |
%
|
|
22 |
\begin{isamarkuptext}%
|
|
23 |
We have already met the \isa{cases} method for performing
|
|
24 |
binary case splits. Here is another example:%
|
|
25 |
\end{isamarkuptext}%
|
|
26 |
\isamarkuptrue%
|
|
27 |
\isacommand{lemma}\ {\isachardoublequote}{\isasymnot}\ A\ {\isasymor}\ A{\isachardoublequote}\isanewline
|
|
28 |
\isamarkupfalse%
|
|
29 |
\isacommand{proof}\ cases\isanewline
|
|
30 |
\ \ \isamarkupfalse%
|
|
31 |
\isacommand{assume}\ {\isachardoublequote}A{\isachardoublequote}\ \isamarkupfalse%
|
|
32 |
\isacommand{thus}\ {\isacharquery}thesis\ \isamarkupfalse%
|
|
33 |
\isacommand{{\isachardot}{\isachardot}}\isanewline
|
|
34 |
\isamarkupfalse%
|
|
35 |
\isacommand{next}\isanewline
|
|
36 |
\ \ \isamarkupfalse%
|
|
37 |
\isacommand{assume}\ {\isachardoublequote}{\isasymnot}\ A{\isachardoublequote}\ \isamarkupfalse%
|
|
38 |
\isacommand{thus}\ {\isacharquery}thesis\ \isamarkupfalse%
|
|
39 |
\isacommand{{\isachardot}{\isachardot}}\isanewline
|
|
40 |
\isamarkupfalse%
|
|
41 |
\isacommand{qed}\isamarkupfalse%
|
|
42 |
%
|
|
43 |
\begin{isamarkuptext}%
|
|
44 |
\noindent The two cases must come in this order because \isa{cases} merely abbreviates \isa{{\isacharparenleft}rule\ case{\isacharunderscore}split{\isacharunderscore}thm{\isacharparenright}} where
|
|
45 |
\isa{case{\isacharunderscore}split{\isacharunderscore}thm} is \isa{{\isasymlbrakk}{\isacharquery}P\ {\isasymLongrightarrow}\ {\isacharquery}Q{\isacharsemicolon}\ {\isasymnot}\ {\isacharquery}P\ {\isasymLongrightarrow}\ {\isacharquery}Q{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharquery}Q}. If we reverse
|
|
46 |
the order of the two cases in the proof, the first case would prove
|
|
47 |
\isa{{\isasymnot}\ A\ {\isasymLongrightarrow}\ {\isasymnot}\ A\ {\isasymor}\ A} which would solve the first premise of
|
|
48 |
\isa{case{\isacharunderscore}split{\isacharunderscore}thm}, instantiating \isa{{\isacharquery}P} with \isa{{\isasymnot}\ A}, thus making the second premise \isa{{\isasymnot}\ {\isasymnot}\ A\ {\isasymLongrightarrow}\ {\isasymnot}\ A\ {\isasymor}\ A}.
|
|
49 |
Therefore the order of subgoals is not always completely arbitrary.
|
|
50 |
|
|
51 |
The above proof is appropriate if \isa{A} is textually small.
|
|
52 |
However, if \isa{A} is large, we do not want to repeat it. This can
|
|
53 |
be avoided by the following idiom%
|
|
54 |
\end{isamarkuptext}%
|
|
55 |
\isamarkuptrue%
|
|
56 |
\isacommand{lemma}\ {\isachardoublequote}{\isasymnot}\ A\ {\isasymor}\ A{\isachardoublequote}\isanewline
|
|
57 |
\isamarkupfalse%
|
|
58 |
\isacommand{proof}\ {\isacharparenleft}cases\ {\isachardoublequote}A{\isachardoublequote}{\isacharparenright}\isanewline
|
|
59 |
\ \ \isamarkupfalse%
|
|
60 |
\isacommand{case}\ True\ \isamarkupfalse%
|
|
61 |
\isacommand{thus}\ {\isacharquery}thesis\ \isamarkupfalse%
|
|
62 |
\isacommand{{\isachardot}{\isachardot}}\isanewline
|
|
63 |
\isamarkupfalse%
|
|
64 |
\isacommand{next}\isanewline
|
|
65 |
\ \ \isamarkupfalse%
|
|
66 |
\isacommand{case}\ False\ \isamarkupfalse%
|
|
67 |
\isacommand{thus}\ {\isacharquery}thesis\ \isamarkupfalse%
|
|
68 |
\isacommand{{\isachardot}{\isachardot}}\isanewline
|
|
69 |
\isamarkupfalse%
|
|
70 |
\isacommand{qed}\isamarkupfalse%
|
|
71 |
%
|
|
72 |
\begin{isamarkuptext}%
|
|
73 |
\noindent which is like the previous proof but instantiates
|
|
74 |
\isa{{\isacharquery}P} right away with \isa{A}. Thus we could prove the two
|
|
75 |
cases in any order. The phrase `\isakeyword{case}~\isa{True}'
|
|
76 |
abbreviates `\isakeyword{assume}~\isa{True{\isacharcolon}\ A}' and analogously for
|
|
77 |
\isa{False} and \isa{{\isasymnot}\ A}.
|
|
78 |
|
|
79 |
The same game can be played with other datatypes, for example lists,
|
|
80 |
where \isa{tl} is the tail of a list, and \isa{length} returns a
|
|
81 |
natural number (remember: $0-1=0$):%
|
|
82 |
\end{isamarkuptext}%
|
|
83 |
\isamarkuptrue%
|
|
84 |
\isamarkupfalse%
|
|
85 |
\isacommand{lemma}\ {\isachardoublequote}length{\isacharparenleft}tl\ xs{\isacharparenright}\ {\isacharequal}\ length\ xs\ {\isacharminus}\ {\isadigit{1}}{\isachardoublequote}\isanewline
|
|
86 |
\isamarkupfalse%
|
|
87 |
\isacommand{proof}\ {\isacharparenleft}cases\ xs{\isacharparenright}\isanewline
|
|
88 |
\ \ \isamarkupfalse%
|
|
89 |
\isacommand{case}\ Nil\ \isamarkupfalse%
|
|
90 |
\isacommand{thus}\ {\isacharquery}thesis\ \isamarkupfalse%
|
|
91 |
\isacommand{by}\ simp\isanewline
|
|
92 |
\isamarkupfalse%
|
|
93 |
\isacommand{next}\isanewline
|
|
94 |
\ \ \isamarkupfalse%
|
|
95 |
\isacommand{case}\ Cons\ \isamarkupfalse%
|
|
96 |
\isacommand{thus}\ {\isacharquery}thesis\ \isamarkupfalse%
|
|
97 |
\isacommand{by}\ simp\isanewline
|
|
98 |
\isamarkupfalse%
|
|
99 |
\isacommand{qed}\isamarkupfalse%
|
|
100 |
%
|
|
101 |
\begin{isamarkuptext}%
|
|
102 |
\noindent Here `\isakeyword{case}~\isa{Nil}' abbreviates
|
|
103 |
`\isakeyword{assume}~\isa{Nil{\isacharcolon}}~\isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}}' and
|
|
104 |
`\isakeyword{case}~\isa{Cons}'
|
|
105 |
abbreviates `\isakeyword{fix}~\isa{{\isacharquery}\ {\isacharquery}{\isacharquery}}
|
|
106 |
\isakeyword{assume}~\isa{Cons{\isacharcolon}}~\isa{xs\ {\isacharequal}\ {\isacharquery}\ {\isacharhash}\ {\isacharquery}{\isacharquery}}'
|
|
107 |
where \isa{{\isacharquery}} and \isa{{\isacharquery}{\isacharquery}}
|
|
108 |
stand for variable names that have been chosen by the system.
|
|
109 |
Therefore we cannot refer to them.
|
|
110 |
Luckily, this proof is simple enough we do not need to refer to them.
|
|
111 |
However, sometimes one may have to. Hence Isar offers a simple scheme for
|
|
112 |
naming those variables: replace the anonymous \isa{Cons} by
|
|
113 |
\isa{{\isacharparenleft}Cons\ y\ ys{\isacharparenright}}, which abbreviates `\isakeyword{fix}~\isa{y\ ys}
|
|
114 |
\isakeyword{assume}~\isa{Cons{\isacharcolon}}~\isa{xs\ {\isacharequal}\ y\ {\isacharhash}\ ys}'.
|
|
115 |
In each \isakeyword{case} the assumption can be
|
|
116 |
referred to inside the proof by the name of the constructor. In
|
|
117 |
Section~\ref{sec:full-Ind} below we will come across an example
|
|
118 |
of this.%
|
|
119 |
\end{isamarkuptext}%
|
|
120 |
\isamarkuptrue%
|
|
121 |
%
|
|
122 |
\isamarkupsubsection{Structural induction%
|
|
123 |
}
|
|
124 |
\isamarkuptrue%
|
|
125 |
%
|
|
126 |
\begin{isamarkuptext}%
|
|
127 |
We start with an inductive proof where both cases are proved automatically:%
|
|
128 |
\end{isamarkuptext}%
|
|
129 |
\isamarkuptrue%
|
|
130 |
\isacommand{lemma}\ {\isachardoublequote}{\isadigit{2}}\ {\isacharasterisk}\ {\isacharparenleft}{\isasymSum}i{\isacharless}n{\isacharplus}{\isadigit{1}}{\isachardot}\ i{\isacharparenright}\ {\isacharequal}\ n{\isacharasterisk}{\isacharparenleft}n{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
|
|
131 |
\isamarkupfalse%
|
|
132 |
\isacommand{by}\ {\isacharparenleft}induct\ n{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isamarkupfalse%
|
|
133 |
%
|
|
134 |
\begin{isamarkuptext}%
|
|
135 |
\noindent If we want to expose more of the structure of the
|
|
136 |
proof, we can use pattern matching to avoid having to repeat the goal
|
|
137 |
statement:%
|
|
138 |
\end{isamarkuptext}%
|
|
139 |
\isamarkuptrue%
|
|
140 |
\isacommand{lemma}\ {\isachardoublequote}{\isadigit{2}}\ {\isacharasterisk}\ {\isacharparenleft}{\isasymSum}i{\isacharless}n{\isacharplus}{\isadigit{1}}{\isachardot}\ i{\isacharparenright}\ {\isacharequal}\ n{\isacharasterisk}{\isacharparenleft}n{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isachardoublequote}\ {\isacharparenleft}\isakeyword{is}\ {\isachardoublequote}{\isacharquery}P\ n{\isachardoublequote}{\isacharparenright}\isanewline
|
|
141 |
\isamarkupfalse%
|
|
142 |
\isacommand{proof}\ {\isacharparenleft}induct\ n{\isacharparenright}\isanewline
|
|
143 |
\ \ \isamarkupfalse%
|
|
144 |
\isacommand{show}\ {\isachardoublequote}{\isacharquery}P\ {\isadigit{0}}{\isachardoublequote}\ \isamarkupfalse%
|
|
145 |
\isacommand{by}\ simp\isanewline
|
|
146 |
\isamarkupfalse%
|
|
147 |
\isacommand{next}\isanewline
|
|
148 |
\ \ \isamarkupfalse%
|
|
149 |
\isacommand{fix}\ n\ \isamarkupfalse%
|
|
150 |
\isacommand{assume}\ {\isachardoublequote}{\isacharquery}P\ n{\isachardoublequote}\isanewline
|
|
151 |
\ \ \isamarkupfalse%
|
|
152 |
\isacommand{thus}\ {\isachardoublequote}{\isacharquery}P{\isacharparenleft}Suc\ n{\isacharparenright}{\isachardoublequote}\ \isamarkupfalse%
|
|
153 |
\isacommand{by}\ simp\isanewline
|
|
154 |
\isamarkupfalse%
|
|
155 |
\isacommand{qed}\isamarkupfalse%
|
|
156 |
%
|
|
157 |
\begin{isamarkuptext}%
|
|
158 |
\noindent We could refine this further to show more of the equational
|
|
159 |
proof. Instead we explore the same avenue as for case distinctions:
|
|
160 |
introducing context via the \isakeyword{case} command:%
|
|
161 |
\end{isamarkuptext}%
|
|
162 |
\isamarkuptrue%
|
|
163 |
\isacommand{lemma}\ {\isachardoublequote}{\isadigit{2}}\ {\isacharasterisk}\ {\isacharparenleft}{\isasymSum}i{\isacharless}n{\isacharplus}{\isadigit{1}}{\isachardot}\ i{\isacharparenright}\ {\isacharequal}\ n{\isacharasterisk}{\isacharparenleft}n{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
|
|
164 |
\isamarkupfalse%
|
|
165 |
\isacommand{proof}\ {\isacharparenleft}induct\ n{\isacharparenright}\isanewline
|
|
166 |
\ \ \isamarkupfalse%
|
|
167 |
\isacommand{case}\ {\isadigit{0}}\ \isamarkupfalse%
|
|
168 |
\isacommand{show}\ {\isacharquery}case\ \isamarkupfalse%
|
|
169 |
\isacommand{by}\ simp\isanewline
|
|
170 |
\isamarkupfalse%
|
|
171 |
\isacommand{next}\isanewline
|
|
172 |
\ \ \isamarkupfalse%
|
|
173 |
\isacommand{case}\ Suc\ \isamarkupfalse%
|
|
174 |
\isacommand{thus}\ {\isacharquery}case\ \isamarkupfalse%
|
|
175 |
\isacommand{by}\ simp\isanewline
|
|
176 |
\isamarkupfalse%
|
|
177 |
\isacommand{qed}\isamarkupfalse%
|
|
178 |
%
|
|
179 |
\begin{isamarkuptext}%
|
|
180 |
\noindent The implicitly defined \isa{{\isacharquery}case} refers to the
|
|
181 |
corresponding case to be proved, i.e.\ \isa{{\isacharquery}P\ {\isadigit{0}}} in the first case and
|
|
182 |
\isa{{\isacharquery}P{\isacharparenleft}Suc\ n{\isacharparenright}} in the second case. Context \isakeyword{case}~\isa{{\isadigit{0}}} is
|
|
183 |
empty whereas \isakeyword{case}~\isa{Suc} assumes \isa{{\isacharquery}P\ n}. Again we
|
|
184 |
have the same problem as with case distinctions: we cannot refer to an anonymous \isa{n}
|
|
185 |
in the induction step because it has not been introduced via \isakeyword{fix}
|
|
186 |
(in contrast to the previous proof). The solution is the one outlined for
|
|
187 |
\isa{Cons} above: replace \isa{Suc} by \isa{{\isacharparenleft}Suc\ i{\isacharparenright}}:%
|
|
188 |
\end{isamarkuptext}%
|
|
189 |
\isamarkuptrue%
|
|
190 |
\isacommand{lemma}\ \isakeyword{fixes}\ n{\isacharcolon}{\isacharcolon}nat\ \isakeyword{shows}\ {\isachardoublequote}n\ {\isacharless}\ n{\isacharasterisk}n\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequote}\isanewline
|
|
191 |
\isamarkupfalse%
|
|
192 |
\isacommand{proof}\ {\isacharparenleft}induct\ n{\isacharparenright}\isanewline
|
|
193 |
\ \ \isamarkupfalse%
|
|
194 |
\isacommand{case}\ {\isadigit{0}}\ \isamarkupfalse%
|
|
195 |
\isacommand{show}\ {\isacharquery}case\ \isamarkupfalse%
|
|
196 |
\isacommand{by}\ simp\isanewline
|
|
197 |
\isamarkupfalse%
|
|
198 |
\isacommand{next}\isanewline
|
|
199 |
\ \ \isamarkupfalse%
|
|
200 |
\isacommand{case}\ {\isacharparenleft}Suc\ i{\isacharparenright}\ \isamarkupfalse%
|
|
201 |
\isacommand{thus}\ {\isachardoublequote}Suc\ i\ {\isacharless}\ Suc\ i\ {\isacharasterisk}\ Suc\ i\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequote}\ \isamarkupfalse%
|
|
202 |
\isacommand{by}\ simp\isanewline
|
|
203 |
\isamarkupfalse%
|
|
204 |
\isacommand{qed}\isamarkupfalse%
|
|
205 |
%
|
|
206 |
\begin{isamarkuptext}%
|
|
207 |
\noindent Of course we could again have written
|
|
208 |
\isakeyword{thus}~\isa{{\isacharquery}case} instead of giving the term explicitly
|
|
209 |
but we wanted to use \isa{i} somewhere.%
|
|
210 |
\end{isamarkuptext}%
|
|
211 |
\isamarkuptrue%
|
|
212 |
%
|
|
213 |
\isamarkupsubsection{Induction formulae involving \isa{{\isasymAnd}} or \isa{{\isasymLongrightarrow}}\label{sec:full-Ind}%
|
|
214 |
}
|
|
215 |
\isamarkuptrue%
|
|
216 |
%
|
|
217 |
\begin{isamarkuptext}%
|
|
218 |
Let us now consider the situation where the goal to be proved contains
|
|
219 |
\isa{{\isasymAnd}} or \isa{{\isasymLongrightarrow}}, say \isa{{\isasymAnd}x{\isachardot}\ P\ x\ {\isasymLongrightarrow}\ Q\ x} --- motivation and a
|
|
220 |
real example follow shortly. This means that in each case of the induction,
|
|
221 |
\isa{{\isacharquery}case} would be of the form \isa{{\isasymAnd}x{\isachardot}\ P{\isacharprime}\ x\ {\isasymLongrightarrow}\ Q{\isacharprime}\ x}. Thus the
|
|
222 |
first proof steps will be the canonical ones, fixing \isa{x} and assuming
|
|
223 |
\isa{P{\isacharprime}\ x}. To avoid this tedium, induction performs these steps
|
|
224 |
automatically: for example in case \isa{{\isacharparenleft}Suc\ n{\isacharparenright}}, \isa{{\isacharquery}case} is only
|
|
225 |
\isa{Q{\isacharprime}\ x} whereas the assumptions (named \isa{Suc}!) contain both the
|
|
226 |
usual induction hypothesis \emph{and} \isa{P{\isacharprime}\ x}.
|
|
227 |
It should be clear how this generalises to more complex formulae.
|
|
228 |
|
|
229 |
As an example we will now prove complete induction via
|
|
230 |
structural induction.%
|
|
231 |
\end{isamarkuptext}%
|
|
232 |
\isamarkuptrue%
|
|
233 |
\isacommand{lemma}\ \isakeyword{assumes}\ A{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isasymAnd}n{\isachardot}\ {\isacharparenleft}{\isasymAnd}m{\isachardot}\ m\ {\isacharless}\ n\ {\isasymLongrightarrow}\ P\ m{\isacharparenright}\ {\isasymLongrightarrow}\ P\ n{\isacharparenright}{\isachardoublequote}\isanewline
|
|
234 |
\ \ \isakeyword{shows}\ {\isachardoublequote}P{\isacharparenleft}n{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isachardoublequote}\isanewline
|
|
235 |
\isamarkupfalse%
|
|
236 |
\isacommand{proof}\ {\isacharparenleft}rule\ A{\isacharparenright}\isanewline
|
|
237 |
\ \ \isamarkupfalse%
|
|
238 |
\isacommand{show}\ {\isachardoublequote}{\isasymAnd}m{\isachardot}\ m\ {\isacharless}\ n\ {\isasymLongrightarrow}\ P\ m{\isachardoublequote}\isanewline
|
|
239 |
\ \ \isamarkupfalse%
|
|
240 |
\isacommand{proof}\ {\isacharparenleft}induct\ n{\isacharparenright}\isanewline
|
|
241 |
\ \ \ \ \isamarkupfalse%
|
|
242 |
\isacommand{case}\ {\isadigit{0}}\ \isamarkupfalse%
|
|
243 |
\isacommand{thus}\ {\isacharquery}case\ \isamarkupfalse%
|
|
244 |
\isacommand{by}\ simp\isanewline
|
|
245 |
\ \ \isamarkupfalse%
|
|
246 |
\isacommand{next}\isanewline
|
|
247 |
\ \ \ \ \isamarkupfalse%
|
|
248 |
\isacommand{case}\ {\isacharparenleft}Suc\ n{\isacharparenright}\ \ \ %
|
|
249 |
\isamarkupcmt{\isakeyword{fix} \isa{m} \isakeyword{assume} \isa{Suc}: \isa{{\isachardoublequote}{\isacharquery}m\ {\isacharless}\ n\ {\isasymLongrightarrow}\ P\ {\isacharquery}m{\isachardoublequote}} \isa{{\isachardoublequote}m\ {\isacharless}\ Suc\ n{\isachardoublequote}}%
|
|
250 |
}
|
|
251 |
\isanewline
|
|
252 |
\ \ \ \ \isamarkupfalse%
|
|
253 |
\isacommand{show}\ {\isacharquery}case\ \ \ \ %
|
|
254 |
\isamarkupcmt{\isa{P\ m}%
|
|
255 |
}
|
|
256 |
\isanewline
|
|
257 |
\ \ \ \ \isamarkupfalse%
|
|
258 |
\isacommand{proof}\ cases\isanewline
|
|
259 |
\ \ \ \ \ \ \isamarkupfalse%
|
|
260 |
\isacommand{assume}\ eq{\isacharcolon}\ {\isachardoublequote}m\ {\isacharequal}\ n{\isachardoublequote}\isanewline
|
|
261 |
\ \ \ \ \ \ \isamarkupfalse%
|
|
262 |
\isacommand{from}\ Suc\ \isakeyword{and}\ A\ \isamarkupfalse%
|
|
263 |
\isacommand{have}\ {\isachardoublequote}P\ n{\isachardoublequote}\ \isamarkupfalse%
|
|
264 |
\isacommand{by}\ blast\isanewline
|
|
265 |
\ \ \ \ \ \ \isamarkupfalse%
|
|
266 |
\isacommand{with}\ eq\ \isamarkupfalse%
|
|
267 |
\isacommand{show}\ {\isachardoublequote}P\ m{\isachardoublequote}\ \isamarkupfalse%
|
|
268 |
\isacommand{by}\ simp\isanewline
|
|
269 |
\ \ \ \ \isamarkupfalse%
|
|
270 |
\isacommand{next}\isanewline
|
|
271 |
\ \ \ \ \ \ \isamarkupfalse%
|
|
272 |
\isacommand{assume}\ {\isachardoublequote}m\ {\isasymnoteq}\ n{\isachardoublequote}\isanewline
|
|
273 |
\ \ \ \ \ \ \isamarkupfalse%
|
|
274 |
\isacommand{with}\ Suc\ \isamarkupfalse%
|
|
275 |
\isacommand{have}\ {\isachardoublequote}m\ {\isacharless}\ n{\isachardoublequote}\ \isamarkupfalse%
|
|
276 |
\isacommand{by}\ arith\isanewline
|
|
277 |
\ \ \ \ \ \ \isamarkupfalse%
|
|
278 |
\isacommand{thus}\ {\isachardoublequote}P\ m{\isachardoublequote}\ \isamarkupfalse%
|
|
279 |
\isacommand{by}{\isacharparenleft}rule\ Suc{\isacharparenright}\isanewline
|
|
280 |
\ \ \ \ \isamarkupfalse%
|
|
281 |
\isacommand{qed}\isanewline
|
|
282 |
\ \ \isamarkupfalse%
|
|
283 |
\isacommand{qed}\isanewline
|
|
284 |
\isamarkupfalse%
|
|
285 |
\isacommand{qed}\isamarkupfalse%
|
|
286 |
%
|
|
287 |
\begin{isamarkuptext}%
|
|
288 |
\noindent Given the explanations above and the comments in the
|
|
289 |
proof text (only necessary for novices), the proof should be quite
|
|
290 |
readable.
|
|
291 |
|
|
292 |
The statement of the lemma is interesting because it deviates from the style in
|
|
293 |
the Tutorial~\cite{LNCS2283}, which suggests to introduce \isa{{\isasymforall}} or
|
|
294 |
\isa{{\isasymlongrightarrow}} into a theorem to strengthen it for induction. In Isar
|
|
295 |
proofs we can use \isa{{\isasymAnd}} and \isa{{\isasymLongrightarrow}} instead. This simplifies the
|
|
296 |
proof and means we do not have to convert between the two kinds of
|
|
297 |
connectives.
|
|
298 |
|
|
299 |
Note that in a nested induction over the same data type, the inner
|
|
300 |
case labels hide the outer ones of the same name. If you want to refer
|
|
301 |
to the outer ones inside, you need to name them on the outside, e.g.\
|
|
302 |
\isakeyword{note}~\isa{outer{\isacharunderscore}IH\ {\isacharequal}\ Suc}.%
|
|
303 |
\end{isamarkuptext}%
|
|
304 |
\isamarkuptrue%
|
|
305 |
%
|
|
306 |
\isamarkupsubsection{Rule induction%
|
|
307 |
}
|
|
308 |
\isamarkuptrue%
|
|
309 |
%
|
|
310 |
\begin{isamarkuptext}%
|
|
311 |
HOL also supports inductively defined sets. See \cite{LNCS2283}
|
|
312 |
for details. As an example we define our own version of the reflexive
|
|
313 |
transitive closure of a relation --- HOL provides a predefined one as well.%
|
|
314 |
\end{isamarkuptext}%
|
|
315 |
\isamarkuptrue%
|
|
316 |
\isacommand{consts}\ rtc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set{\isachardoublequote}\ \ \ {\isacharparenleft}{\isachardoublequote}{\isacharunderscore}{\isacharasterisk}{\isachardoublequote}\ {\isacharbrackleft}{\isadigit{1}}{\isadigit{0}}{\isadigit{0}}{\isadigit{0}}{\isacharbrackright}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}{\isacharparenright}\isanewline
|
|
317 |
\isamarkupfalse%
|
|
318 |
\isacommand{inductive}\ {\isachardoublequote}r{\isacharasterisk}{\isachardoublequote}\isanewline
|
|
319 |
\isakeyword{intros}\isanewline
|
|
320 |
refl{\isacharcolon}\ \ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}x{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
|
|
321 |
step{\isacharcolon}\ \ {\isachardoublequote}{\isasymlbrakk}\ {\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharsemicolon}\ {\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isamarkupfalse%
|
|
322 |
%
|
|
323 |
\begin{isamarkuptext}%
|
|
324 |
\noindent
|
|
325 |
First the constant is declared as a function on binary
|
|
326 |
relations (with concrete syntax \isa{r{\isacharasterisk}} instead of \isa{rtc\ r}), then the defining clauses are given. We will now prove that
|
|
327 |
\isa{r{\isacharasterisk}} is indeed transitive:%
|
|
328 |
\end{isamarkuptext}%
|
|
329 |
\isamarkuptrue%
|
|
330 |
\isacommand{lemma}\ \isakeyword{assumes}\ A{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\ \isakeyword{shows}\ {\isachardoublequote}{\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
|
|
331 |
\isamarkupfalse%
|
|
332 |
\isacommand{using}\ A\isanewline
|
|
333 |
\isamarkupfalse%
|
|
334 |
\isacommand{proof}\ induct\isanewline
|
|
335 |
\ \ \isamarkupfalse%
|
|
336 |
\isacommand{case}\ refl\ \isamarkupfalse%
|
|
337 |
\isacommand{thus}\ {\isacharquery}case\ \isamarkupfalse%
|
|
338 |
\isacommand{{\isachardot}}\isanewline
|
|
339 |
\isamarkupfalse%
|
|
340 |
\isacommand{next}\isanewline
|
|
341 |
\ \ \isamarkupfalse%
|
|
342 |
\isacommand{case}\ step\ \isamarkupfalse%
|
|
343 |
\isacommand{thus}\ {\isacharquery}case\ \isamarkupfalse%
|
|
344 |
\isacommand{by}{\isacharparenleft}blast\ intro{\isacharcolon}\ rtc{\isachardot}step{\isacharparenright}\isanewline
|
|
345 |
\isamarkupfalse%
|
|
346 |
\isacommand{qed}\isamarkupfalse%
|
|
347 |
%
|
|
348 |
\begin{isamarkuptext}%
|
|
349 |
\noindent Rule induction is triggered by a fact $(x_1,\dots,x_n)
|
|
350 |
\in R$ piped into the proof, here \isakeyword{using}~\isa{A}. The
|
|
351 |
proof itself follows the inductive definition very
|
|
352 |
closely: there is one case for each rule, and it has the same name as
|
|
353 |
the rule, analogous to structural induction.
|
|
354 |
|
|
355 |
However, this proof is rather terse. Here is a more readable version:%
|
|
356 |
\end{isamarkuptext}%
|
|
357 |
\isamarkuptrue%
|
|
358 |
\isacommand{lemma}\ \isakeyword{assumes}\ A{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\ \isakeyword{and}\ B{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
|
|
359 |
\ \ \isakeyword{shows}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
|
|
360 |
\isamarkupfalse%
|
|
361 |
\isacommand{proof}\ {\isacharminus}\isanewline
|
|
362 |
\ \ \isamarkupfalse%
|
|
363 |
\isacommand{from}\ A\ B\ \isamarkupfalse%
|
|
364 |
\isacommand{show}\ {\isacharquery}thesis\isanewline
|
|
365 |
\ \ \isamarkupfalse%
|
|
366 |
\isacommand{proof}\ induct\isanewline
|
|
367 |
\ \ \ \ \isamarkupfalse%
|
|
368 |
\isacommand{fix}\ x\ \isamarkupfalse%
|
|
369 |
\isacommand{assume}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\ \ %
|
|
370 |
\isamarkupcmt{\isa{B}[\isa{y} := \isa{x}]%
|
|
371 |
}
|
|
372 |
\isanewline
|
|
373 |
\ \ \ \ \isamarkupfalse%
|
|
374 |
\isacommand{thus}\ {\isachardoublequote}{\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\ \isamarkupfalse%
|
|
375 |
\isacommand{{\isachardot}}\isanewline
|
|
376 |
\ \ \isamarkupfalse%
|
|
377 |
\isacommand{next}\isanewline
|
|
378 |
\ \ \ \ \isamarkupfalse%
|
|
379 |
\isacommand{fix}\ x{\isacharprime}\ x\ y\isanewline
|
|
380 |
\ \ \ \ \isamarkupfalse%
|
|
381 |
\isacommand{assume}\ {\isadigit{1}}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}x{\isacharprime}{\isacharcomma}x{\isacharparenright}\ {\isasymin}\ r{\isachardoublequote}\ \isakeyword{and}\isanewline
|
|
382 |
\ \ \ \ \ \ \ \ \ \ \ IH{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}\ {\isasymLongrightarrow}\ {\isacharparenleft}x{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\ \isakeyword{and}\isanewline
|
|
383 |
\ \ \ \ \ \ \ \ \ \ \ B{\isacharcolon}\ \ {\isachardoublequote}{\isacharparenleft}y{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\isanewline
|
|
384 |
\ \ \ \ \isamarkupfalse%
|
|
385 |
\isacommand{from}\ {\isadigit{1}}\ IH{\isacharbrackleft}OF\ B{\isacharbrackright}\ \isamarkupfalse%
|
|
386 |
\isacommand{show}\ {\isachardoublequote}{\isacharparenleft}x{\isacharprime}{\isacharcomma}z{\isacharparenright}\ {\isasymin}\ r{\isacharasterisk}{\isachardoublequote}\ \isamarkupfalse%
|
|
387 |
\isacommand{by}{\isacharparenleft}rule\ rtc{\isachardot}step{\isacharparenright}\isanewline
|
|
388 |
\ \ \isamarkupfalse%
|
|
389 |
\isacommand{qed}\isanewline
|
|
390 |
\isamarkupfalse%
|
|
391 |
\isacommand{qed}\isamarkupfalse%
|
|
392 |
%
|
|
393 |
\begin{isamarkuptext}%
|
|
394 |
\noindent We start the proof with \isakeyword{from}~\isa{A\ B}. Only \isa{A} is ``consumed'' by the induction step.
|
|
395 |
Since \isa{B} is left over we don't just prove \isa{{\isacharquery}thesis} but \isa{B\ {\isasymLongrightarrow}\ {\isacharquery}thesis}, just as in the previous proof. The
|
|
396 |
base case is trivial. In the assumptions for the induction step we can
|
|
397 |
see very clearly how things fit together and permit ourselves the
|
|
398 |
obvious forward step \isa{IH{\isacharbrackleft}OF\ B{\isacharbrackright}}.
|
|
399 |
|
|
400 |
The notation `\isakeyword{case}~\isa{(}\emph{constructor} \emph{vars}\isa{)}'
|
|
401 |
is also supported for inductive definitions. The \emph{constructor} is (the
|
|
402 |
name of) the rule and the \emph{vars} fix the free variables in the
|
|
403 |
rule; the order of the \emph{vars} must correspond to the
|
|
404 |
\emph{alphabetical order} of the variables as they appear in the rule.
|
|
405 |
For example, we could start the above detailed proof of the induction
|
|
406 |
with \isakeyword{case}~\isa{(step x' x y)}. However, we can then only
|
|
407 |
refer to the assumptions named \isa{step} collectively and not
|
|
408 |
individually, as the above proof requires.%
|
|
409 |
\end{isamarkuptext}%
|
|
410 |
\isamarkuptrue%
|
|
411 |
%
|
|
412 |
\isamarkupsubsection{More induction%
|
|
413 |
}
|
|
414 |
\isamarkuptrue%
|
|
415 |
%
|
|
416 |
\begin{isamarkuptext}%
|
|
417 |
We close the section by demonstrating how arbitrary induction
|
|
418 |
rules are applied. As a simple example we have chosen recursion
|
|
419 |
induction, i.e.\ induction based on a recursive function
|
|
420 |
definition. However, most of what we show works for induction in
|
|
421 |
general.
|
|
422 |
|
|
423 |
The example is an unusual definition of rotation:%
|
|
424 |
\end{isamarkuptext}%
|
|
425 |
\isamarkuptrue%
|
|
426 |
\isacommand{consts}\ rot\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
|
|
427 |
\isamarkupfalse%
|
|
428 |
\isacommand{recdef}\ rot\ {\isachardoublequote}measure\ length{\isachardoublequote}\ \ %
|
|
429 |
\isamarkupcmt{for the internal termination proof%
|
|
430 |
}
|
|
431 |
\isanewline
|
|
432 |
{\isachardoublequote}rot\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}\isanewline
|
|
433 |
{\isachardoublequote}rot\ {\isacharbrackleft}x{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}x{\isacharbrackright}{\isachardoublequote}\isanewline
|
|
434 |
{\isachardoublequote}rot\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ y\ {\isacharhash}\ rot{\isacharparenleft}x{\isacharhash}zs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
435 |
%
|
|
436 |
\begin{isamarkuptext}%
|
|
437 |
\noindent This yields, among other things, the induction rule
|
|
438 |
\isa{rot{\isachardot}induct}: \begin{isabelle}%
|
|
439 |
{\isasymlbrakk}P\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ P\ {\isacharbrackleft}x{\isacharbrackright}{\isacharsemicolon}\ {\isasymAnd}x\ y\ zs{\isachardot}\ P\ {\isacharparenleft}x\ {\isacharhash}\ zs{\isacharparenright}\ {\isasymLongrightarrow}\ P\ {\isacharparenleft}x\ {\isacharhash}\ y\ {\isacharhash}\ zs{\isacharparenright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ P\ x%
|
|
440 |
\end{isabelle}
|
|
441 |
In the following proof we rely on a default naming scheme for cases: they are
|
|
442 |
called 1, 2, etc, unless they have been named explicitly. The latter happens
|
|
443 |
only with datatypes and inductively defined sets, but not with recursive
|
|
444 |
functions.%
|
|
445 |
\end{isamarkuptext}%
|
|
446 |
\isamarkuptrue%
|
|
447 |
\isacommand{lemma}\ {\isachardoublequote}xs\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymLongrightarrow}\ rot\ xs\ {\isacharequal}\ tl\ xs\ {\isacharat}\ {\isacharbrackleft}hd\ xs{\isacharbrackright}{\isachardoublequote}\isanewline
|
|
448 |
\isamarkupfalse%
|
|
449 |
\isacommand{proof}\ {\isacharparenleft}induct\ xs\ rule{\isacharcolon}\ rot{\isachardot}induct{\isacharparenright}\isanewline
|
|
450 |
\ \ \isamarkupfalse%
|
|
451 |
\isacommand{case}\ {\isadigit{1}}\ \isamarkupfalse%
|
|
452 |
\isacommand{thus}\ {\isacharquery}case\ \isamarkupfalse%
|
|
453 |
\isacommand{by}\ simp\isanewline
|
|
454 |
\isamarkupfalse%
|
|
455 |
\isacommand{next}\isanewline
|
|
456 |
\ \ \isamarkupfalse%
|
|
457 |
\isacommand{case}\ {\isadigit{2}}\ \isamarkupfalse%
|
|
458 |
\isacommand{show}\ {\isacharquery}case\ \isamarkupfalse%
|
|
459 |
\isacommand{by}\ simp\isanewline
|
|
460 |
\isamarkupfalse%
|
|
461 |
\isacommand{next}\isanewline
|
|
462 |
\ \ \isamarkupfalse%
|
|
463 |
\isacommand{case}\ {\isacharparenleft}{\isadigit{3}}\ a\ b\ cs{\isacharparenright}\isanewline
|
|
464 |
\ \ \isamarkupfalse%
|
|
465 |
\isacommand{have}\ {\isachardoublequote}rot\ {\isacharparenleft}a\ {\isacharhash}\ b\ {\isacharhash}\ cs{\isacharparenright}\ {\isacharequal}\ b\ {\isacharhash}\ rot{\isacharparenleft}a\ {\isacharhash}\ cs{\isacharparenright}{\isachardoublequote}\ \isamarkupfalse%
|
|
466 |
\isacommand{by}\ simp\isanewline
|
|
467 |
\ \ \isamarkupfalse%
|
|
468 |
\isacommand{also}\ \isamarkupfalse%
|
|
469 |
\isacommand{have}\ {\isachardoublequote}{\isasymdots}\ {\isacharequal}\ b\ {\isacharhash}\ tl{\isacharparenleft}a\ {\isacharhash}\ cs{\isacharparenright}\ {\isacharat}\ {\isacharbrackleft}hd{\isacharparenleft}a\ {\isacharhash}\ cs{\isacharparenright}{\isacharbrackright}{\isachardoublequote}\ \isamarkupfalse%
|
|
470 |
\isacommand{by}{\isacharparenleft}simp\ add{\isacharcolon}{\isadigit{3}}{\isacharparenright}\isanewline
|
|
471 |
\ \ \isamarkupfalse%
|
|
472 |
\isacommand{also}\ \isamarkupfalse%
|
|
473 |
\isacommand{have}\ {\isachardoublequote}{\isasymdots}\ {\isacharequal}\ tl\ {\isacharparenleft}a\ {\isacharhash}\ b\ {\isacharhash}\ cs{\isacharparenright}\ {\isacharat}\ {\isacharbrackleft}hd\ {\isacharparenleft}a\ {\isacharhash}\ b\ {\isacharhash}\ cs{\isacharparenright}{\isacharbrackright}{\isachardoublequote}\ \isamarkupfalse%
|
|
474 |
\isacommand{by}\ simp\isanewline
|
|
475 |
\ \ \isamarkupfalse%
|
|
476 |
\isacommand{finally}\ \isamarkupfalse%
|
|
477 |
\isacommand{show}\ {\isacharquery}case\ \isamarkupfalse%
|
|
478 |
\isacommand{{\isachardot}}\isanewline
|
|
479 |
\isamarkupfalse%
|
|
480 |
\isacommand{qed}\isamarkupfalse%
|
|
481 |
%
|
|
482 |
\begin{isamarkuptext}%
|
|
483 |
\noindent
|
|
484 |
The third case is only shown in gory detail (see \cite{BauerW-TPHOLs01}
|
|
485 |
for how to reason with chains of equations) to demonstrate that the
|
|
486 |
`\isakeyword{case}~\isa{(}\emph{constructor} \emph{vars}\isa{)}' notation also
|
|
487 |
works for arbitrary induction theorems with numbered cases. The order
|
|
488 |
of the \emph{vars} corresponds to the order of the
|
|
489 |
\isa{{\isasymAnd}}-quantified variables in each case of the induction
|
|
490 |
theorem. For induction theorems produced by \isakeyword{recdef} it is
|
|
491 |
the order in which the variables appear on the left-hand side of the
|
|
492 |
equation.
|
|
493 |
|
|
494 |
The proof is so simple that it can be condensed to%
|
|
495 |
\end{isamarkuptext}%
|
|
496 |
\isamarkuptrue%
|
|
497 |
\isamarkupfalse%
|
|
498 |
\isacommand{by}\ {\isacharparenleft}induct\ xs\ rule{\isacharcolon}\ rot{\isachardot}induct{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isanewline
|
|
499 |
\isamarkupfalse%
|
|
500 |
\isamarkupfalse%
|
|
501 |
\end{isabellebody}%
|
|
502 |
%%% Local Variables:
|
|
503 |
%%% mode: latex
|
|
504 |
%%% TeX-master: "root"
|
|
505 |
%%% End:
|