author | nipkow |
Tue, 31 Oct 2000 13:59:41 +0100 | |
changeset 10363 | 6e8002c1790e |
parent 10328 | bf33cbd76c05 |
child 10396 | 5ab08609e6c8 |
permissions | -rw-r--r-- |
9645 | 1 |
(*<*) |
2 |
theory AdvancedInd = Main:; |
|
3 |
(*>*) |
|
4 |
||
5 |
text{*\noindent |
|
6 |
Now that we have learned about rules and logic, we take another look at the |
|
7 |
finer points of induction. The two questions we answer are: what to do if the |
|
8 |
proposition to be proved is not directly amenable to induction, and how to |
|
10281 | 9 |
utilize and even derive new induction schemas. We conclude with some slightly subtle |
10 |
examples of induction. |
|
9689 | 11 |
*}; |
9645 | 12 |
|
10217 | 13 |
subsection{*Massaging the proposition*}; |
9645 | 14 |
|
10217 | 15 |
text{*\label{sec:ind-var-in-prems} |
9645 | 16 |
So far we have assumed that the theorem we want to prove is already in a form |
17 |
that is amenable to induction, but this is not always the case: |
|
9689 | 18 |
*}; |
9645 | 19 |
|
9933 | 20 |
lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"; |
9645 | 21 |
apply(induct_tac xs); |
22 |
||
23 |
txt{*\noindent |
|
9792 | 24 |
(where @{term"hd"} and @{term"last"} return the first and last element of a |
9645 | 25 |
non-empty list) |
26 |
produces the warning |
|
27 |
\begin{quote}\tt |
|
28 |
Induction variable occurs also among premises! |
|
29 |
\end{quote} |
|
30 |
and leads to the base case |
|
10363 | 31 |
@{subgoals[display,indent=0,goals_limit=1]} |
9645 | 32 |
which, after simplification, becomes |
9723 | 33 |
\begin{isabelle} |
9645 | 34 |
\ 1.\ xs\ {\isasymnoteq}\ []\ {\isasymLongrightarrow}\ hd\ []\ =\ last\ [] |
9723 | 35 |
\end{isabelle} |
10242 | 36 |
We cannot prove this equality because we do not know what @{term hd} and |
37 |
@{term last} return when applied to @{term"[]"}. |
|
9645 | 38 |
|
39 |
The point is that we have violated the above warning. Because the induction |
|
10242 | 40 |
formula is only the conclusion, the occurrence of @{term xs} in the premises is |
9645 | 41 |
not modified by induction. Thus the case that should have been trivial |
10242 | 42 |
becomes unprovable. Fortunately, the solution is easy:\footnote{A very similar |
43 |
heuristic applies to rule inductions; see \S\ref{sec:rtc}.} |
|
9645 | 44 |
\begin{quote} |
45 |
\emph{Pull all occurrences of the induction variable into the conclusion |
|
9792 | 46 |
using @{text"\<longrightarrow>"}.} |
9645 | 47 |
\end{quote} |
48 |
This means we should prove |
|
9689 | 49 |
*}; |
50 |
(*<*)oops;(*>*) |
|
9933 | 51 |
lemma hd_rev: "xs \<noteq> [] \<longrightarrow> hd(rev xs) = last xs"; |
9645 | 52 |
(*<*) |
9689 | 53 |
by(induct_tac xs, auto); |
9645 | 54 |
(*>*) |
55 |
||
56 |
text{*\noindent |
|
57 |
This time, induction leaves us with the following base case |
|
9723 | 58 |
\begin{isabelle} |
9645 | 59 |
\ 1.\ []\ {\isasymnoteq}\ []\ {\isasymlongrightarrow}\ hd\ (rev\ [])\ =\ last\ [] |
9723 | 60 |
\end{isabelle} |
9792 | 61 |
which is trivial, and @{text"auto"} finishes the whole proof. |
9645 | 62 |
|
9792 | 63 |
If @{thm[source]hd_rev} is meant to be a simplification rule, you are |
64 |
done. But if you really need the @{text"\<Longrightarrow>"}-version of |
|
65 |
@{thm[source]hd_rev}, for example because you want to apply it as an |
|
66 |
introduction rule, you need to derive it separately, by combining it with |
|
67 |
modus ponens: |
|
9689 | 68 |
*}; |
9645 | 69 |
|
9689 | 70 |
lemmas hd_revI = hd_rev[THEN mp]; |
9645 | 71 |
|
72 |
text{*\noindent |
|
73 |
which yields the lemma we originally set out to prove. |
|
74 |
||
75 |
In case there are multiple premises $A@1$, \dots, $A@n$ containing the |
|
76 |
induction variable, you should turn the conclusion $C$ into |
|
77 |
\[ A@1 \longrightarrow \cdots A@n \longrightarrow C \] |
|
78 |
(see the remark?? in \S\ref{??}). |
|
79 |
Additionally, you may also have to universally quantify some other variables, |
|
80 |
which can yield a fairly complex conclusion. |
|
9792 | 81 |
Here is a simple example (which is proved by @{text"blast"}): |
9689 | 82 |
*}; |
9645 | 83 |
|
10281 | 84 |
lemma simple: "\<forall>y. A y \<longrightarrow> B y \<longrightarrow> B y \<and> A y"; |
9689 | 85 |
(*<*)by blast;(*>*) |
9645 | 86 |
|
87 |
text{*\noindent |
|
88 |
You can get the desired lemma by explicit |
|
9792 | 89 |
application of modus ponens and @{thm[source]spec}: |
9689 | 90 |
*}; |
9645 | 91 |
|
9689 | 92 |
lemmas myrule = simple[THEN spec, THEN mp, THEN mp]; |
9645 | 93 |
|
94 |
text{*\noindent |
|
9792 | 95 |
or the wholesale stripping of @{text"\<forall>"} and |
9941
fe05af7ec816
renamed atts: rulify to rule_format, elimify to elim_format;
wenzelm
parents:
9933
diff
changeset
|
96 |
@{text"\<longrightarrow>"} in the conclusion via @{text"rule_format"} |
9689 | 97 |
*}; |
9645 | 98 |
|
9941
fe05af7ec816
renamed atts: rulify to rule_format, elimify to elim_format;
wenzelm
parents:
9933
diff
changeset
|
99 |
lemmas myrule = simple[rule_format]; |
9645 | 100 |
|
101 |
text{*\noindent |
|
9689 | 102 |
yielding @{thm"myrule"[no_vars]}. |
9645 | 103 |
You can go one step further and include these derivations already in the |
104 |
statement of your original lemma, thus avoiding the intermediate step: |
|
9689 | 105 |
*}; |
9645 | 106 |
|
10281 | 107 |
lemma myrule[rule_format]: "\<forall>y. A y \<longrightarrow> B y \<longrightarrow> B y \<and> A y"; |
9645 | 108 |
(*<*) |
9689 | 109 |
by blast; |
9645 | 110 |
(*>*) |
111 |
||
112 |
text{* |
|
113 |
\bigskip |
|
114 |
||
115 |
A second reason why your proposition may not be amenable to induction is that |
|
116 |
you want to induct on a whole term, rather than an individual variable. In |
|
10217 | 117 |
general, when inducting on some term $t$ you must rephrase the conclusion $C$ |
118 |
as |
|
119 |
\[ \forall y@1 \dots y@n.~ x = t \longrightarrow C \] |
|
120 |
where $y@1 \dots y@n$ are the free variables in $t$ and $x$ is new, and |
|
121 |
perform induction on $x$ afterwards. An example appears in |
|
122 |
\S\ref{sec:complete-ind} below. |
|
123 |
||
124 |
The very same problem may occur in connection with rule induction. Remember |
|
125 |
that it requires a premise of the form $(x@1,\dots,x@k) \in R$, where $R$ is |
|
126 |
some inductively defined set and the $x@i$ are variables. If instead we have |
|
127 |
a premise $t \in R$, where $t$ is not just an $n$-tuple of variables, we |
|
128 |
replace it with $(x@1,\dots,x@k) \in R$, and rephrase the conclusion $C$ as |
|
129 |
\[ \forall y@1 \dots y@n.~ (x@1,\dots,x@k) = t \longrightarrow C \] |
|
130 |
For an example see \S\ref{sec:CTL-revisited} below. |
|
10281 | 131 |
|
132 |
Of course, all premises that share free variables with $t$ need to be pulled into |
|
133 |
the conclusion as well, under the @{text"\<forall>"}, again using @{text"\<longrightarrow>"} as shown above. |
|
9689 | 134 |
*}; |
9645 | 135 |
|
9689 | 136 |
subsection{*Beyond structural and recursion induction*}; |
9645 | 137 |
|
10217 | 138 |
text{*\label{sec:complete-ind} |
9645 | 139 |
So far, inductive proofs where by structural induction for |
140 |
primitive recursive functions and recursion induction for total recursive |
|
141 |
functions. But sometimes structural induction is awkward and there is no |
|
142 |
recursive function in sight either that could furnish a more appropriate |
|
143 |
induction schema. In such cases some existing standard induction schema can |
|
144 |
be helpful. We show how to apply such induction schemas by an example. |
|
145 |
||
9792 | 146 |
Structural induction on @{typ"nat"} is |
9645 | 147 |
usually known as ``mathematical induction''. There is also ``complete |
148 |
induction'', where you must prove $P(n)$ under the assumption that $P(m)$ |
|
9933 | 149 |
holds for all $m<n$. In Isabelle, this is the theorem @{thm[source]nat_less_induct}: |
150 |
@{thm[display]"nat_less_induct"[no_vars]} |
|
9645 | 151 |
Here is an example of its application. |
9689 | 152 |
*}; |
9645 | 153 |
|
10281 | 154 |
consts f :: "nat \<Rightarrow> nat"; |
9689 | 155 |
axioms f_ax: "f(f(n)) < f(Suc(n))"; |
9645 | 156 |
|
157 |
text{*\noindent |
|
158 |
From the above axiom\footnote{In general, the use of axioms is strongly |
|
159 |
discouraged, because of the danger of inconsistencies. The above axiom does |
|
160 |
not introduce an inconsistency because, for example, the identity function |
|
161 |
satisfies it.} |
|
9792 | 162 |
for @{term"f"} it follows that @{prop"n <= f n"}, which can |
9645 | 163 |
be proved by induction on @{term"f n"}. Following the recipy outlined |
164 |
above, we have to phrase the proposition as follows to allow induction: |
|
9689 | 165 |
*}; |
9645 | 166 |
|
9933 | 167 |
lemma f_incr_lem: "\<forall>i. k = f i \<longrightarrow> i \<le> f i"; |
9645 | 168 |
|
169 |
txt{*\noindent |
|
10363 | 170 |
To perform induction on @{term k} using @{thm[source]nat_less_induct}, we use |
171 |
the same general induction method as for recursion induction (see |
|
9645 | 172 |
\S\ref{sec:recdef-induction}): |
9689 | 173 |
*}; |
9645 | 174 |
|
9923 | 175 |
apply(induct_tac k rule: nat_less_induct); |
10363 | 176 |
|
177 |
txt{*\noindent |
|
178 |
which leaves us with the following proof state: |
|
179 |
@{subgoals[display,indent=0,margin=65]} |
|
180 |
After stripping the @{text"\<forall>i"}, the proof continues with a case |
|
181 |
distinction on @{term"i"}. The case @{prop"i = 0"} is trivial and we focus on |
|
182 |
the other case: |
|
183 |
*} |
|
184 |
||
9689 | 185 |
apply(rule allI); |
9645 | 186 |
apply(case_tac i); |
187 |
apply(simp); |
|
10363 | 188 |
|
189 |
txt{* |
|
190 |
@{subgoals[display,indent=0]} |
|
9689 | 191 |
*}; |
9645 | 192 |
|
9923 | 193 |
by(blast intro!: f_ax Suc_leI intro: le_less_trans); |
9645 | 194 |
|
195 |
text{*\noindent |
|
196 |
It is not surprising if you find the last step puzzling. |
|
9792 | 197 |
The proof goes like this (writing @{term"j"} instead of @{typ"nat"}). |
198 |
Since @{prop"i = Suc j"} it suffices to show |
|
199 |
@{prop"j < f(Suc j)"} (by @{thm[source]Suc_leI}: @{thm"Suc_leI"[no_vars]}). This is |
|
200 |
proved as follows. From @{thm[source]f_ax} we have @{prop"f (f j) < f (Suc j)"} |
|
201 |
(1) which implies @{prop"f j <= f (f j)"} (by the induction hypothesis). |
|
202 |
Using (1) once more we obtain @{prop"f j < f(Suc j)"} (2) by transitivity |
|
203 |
(@{thm[source]le_less_trans}: @{thm"le_less_trans"[no_vars]}). |
|
204 |
Using the induction hypothesis once more we obtain @{prop"j <= f j"} |
|
205 |
which, together with (2) yields @{prop"j < f (Suc j)"} (again by |
|
206 |
@{thm[source]le_less_trans}). |
|
9645 | 207 |
|
208 |
This last step shows both the power and the danger of automatic proofs: they |
|
209 |
will usually not tell you how the proof goes, because it can be very hard to |
|
210 |
translate the internal proof into a human-readable format. Therefore |
|
211 |
\S\ref{sec:part2?} introduces a language for writing readable yet concise |
|
212 |
proofs. |
|
213 |
||
9792 | 214 |
We can now derive the desired @{prop"i <= f i"} from @{text"f_incr"}: |
9689 | 215 |
*}; |
9645 | 216 |
|
9941
fe05af7ec816
renamed atts: rulify to rule_format, elimify to elim_format;
wenzelm
parents:
9933
diff
changeset
|
217 |
lemmas f_incr = f_incr_lem[rule_format, OF refl]; |
9645 | 218 |
|
9689 | 219 |
text{*\noindent |
9792 | 220 |
The final @{thm[source]refl} gets rid of the premise @{text"?k = f ?i"}. Again, |
221 |
we could have included this derivation in the original statement of the lemma: |
|
9689 | 222 |
*}; |
9645 | 223 |
|
9941
fe05af7ec816
renamed atts: rulify to rule_format, elimify to elim_format;
wenzelm
parents:
9933
diff
changeset
|
224 |
lemma f_incr[rule_format, OF refl]: "\<forall>i. k = f i \<longrightarrow> i \<le> f i"; |
9689 | 225 |
(*<*)oops;(*>*) |
9645 | 226 |
|
227 |
text{* |
|
228 |
\begin{exercise} |
|
9792 | 229 |
From the above axiom and lemma for @{term"f"} show that @{term"f"} is the |
230 |
identity. |
|
9645 | 231 |
\end{exercise} |
232 |
||
10236 | 233 |
In general, @{text induct_tac} can be applied with any rule $r$ |
9792 | 234 |
whose conclusion is of the form ${?}P~?x@1 \dots ?x@n$, in which case the |
9645 | 235 |
format is |
9792 | 236 |
\begin{quote} |
237 |
\isacommand{apply}@{text"(induct_tac"} $y@1 \dots y@n$ @{text"rule:"} $r$@{text")"} |
|
238 |
\end{quote}\index{*induct_tac}% |
|
239 |
where $y@1, \dots, y@n$ are variables in the first subgoal. |
|
10236 | 240 |
A further example of a useful induction rule is @{thm[source]length_induct}, |
241 |
induction on the length of a list:\indexbold{*length_induct} |
|
242 |
@{thm[display]length_induct[no_vars]} |
|
243 |
||
9792 | 244 |
In fact, @{text"induct_tac"} even allows the conclusion of |
245 |
$r$ to be an (iterated) conjunction of formulae of the above form, in |
|
9645 | 246 |
which case the application is |
9792 | 247 |
\begin{quote} |
248 |
\isacommand{apply}@{text"(induct_tac"} $y@1 \dots y@n$ @{text"and"} \dots\ @{text"and"} $z@1 \dots z@m$ @{text"rule:"} $r$@{text")"} |
|
249 |
\end{quote} |
|
9689 | 250 |
*}; |
9645 | 251 |
|
9689 | 252 |
subsection{*Derivation of new induction schemas*}; |
253 |
||
254 |
text{*\label{sec:derive-ind} |
|
255 |
Induction schemas are ordinary theorems and you can derive new ones |
|
256 |
whenever you wish. This section shows you how to, using the example |
|
9933 | 257 |
of @{thm[source]nat_less_induct}. Assume we only have structural induction |
9689 | 258 |
available for @{typ"nat"} and want to derive complete induction. This |
259 |
requires us to generalize the statement first: |
|
260 |
*}; |
|
261 |
||
9933 | 262 |
lemma induct_lem: "(\<And>n::nat. \<forall>m<n. P m \<Longrightarrow> P n) \<Longrightarrow> \<forall>m<n. P m"; |
9689 | 263 |
apply(induct_tac n); |
264 |
||
265 |
txt{*\noindent |
|
9792 | 266 |
The base case is trivially true. For the induction step (@{prop"m < |
9933 | 267 |
Suc n"}) we distinguish two cases: case @{prop"m < n"} is true by induction |
268 |
hypothesis and case @{prop"m = n"} follows from the assumption, again using |
|
9689 | 269 |
the induction hypothesis: |
270 |
*}; |
|
271 |
apply(blast); |
|
9933 | 272 |
by(blast elim:less_SucE) |
9689 | 273 |
|
274 |
text{*\noindent |
|
9792 | 275 |
The elimination rule @{thm[source]less_SucE} expresses the case distinction: |
9689 | 276 |
@{thm[display]"less_SucE"[no_vars]} |
277 |
||
278 |
Now it is straightforward to derive the original version of |
|
9933 | 279 |
@{thm[source]nat_less_induct} by manipulting the conclusion of the above lemma: |
9792 | 280 |
instantiate @{term"n"} by @{term"Suc n"} and @{term"m"} by @{term"n"} and |
281 |
remove the trivial condition @{prop"n < Sc n"}. Fortunately, this |
|
9689 | 282 |
happens automatically when we add the lemma as a new premise to the |
283 |
desired goal: |
|
284 |
*}; |
|
285 |
||
9933 | 286 |
theorem nat_less_induct: "(\<And>n::nat. \<forall>m<n. P m \<Longrightarrow> P n) \<Longrightarrow> P n"; |
9689 | 287 |
by(insert induct_lem, blast); |
288 |
||
9933 | 289 |
text{* |
9645 | 290 |
Finally we should mention that HOL already provides the mother of all |
10241 | 291 |
inductions, \textbf{well-founded |
292 |
induction}\indexbold{induction!well-founded}\index{well-founded |
|
293 |
induction|see{induction, well-founded}} (@{thm[source]wf_induct}): |
|
10186 | 294 |
@{thm[display]wf_induct[no_vars]} |
10241 | 295 |
where @{term"wf r"} means that the relation @{term r} is well-founded |
296 |
(see \S\ref{sec:well-founded}). |
|
9933 | 297 |
For example, theorem @{thm[source]nat_less_induct} can be viewed (and |
298 |
derived) as a special case of @{thm[source]wf_induct} where |
|
10186 | 299 |
@{term r} is @{text"<"} on @{typ nat}. The details can be found in the HOL library. |
10241 | 300 |
For a mathematical account of well-founded induction see, for example, \cite{Baader-Nipkow}. |
9689 | 301 |
*}; |
9645 | 302 |
|
303 |
(*<*) |
|
304 |
end |
|
305 |
(*>*) |