9670
|
1 |
\begin{isabelle}%
|
|
2 |
%
|
|
3 |
\begin{isamarkuptext}%
|
|
4 |
\noindent
|
|
5 |
Now that we have learned about rules and logic, we take another look at the
|
|
6 |
finer points of induction. The two questions we answer are: what to do if the
|
|
7 |
proposition to be proved is not directly amenable to induction, and how to
|
|
8 |
utilize and even derive new induction schemas.%
|
|
9 |
\end{isamarkuptext}%
|
|
10 |
%
|
|
11 |
\isamarkupsubsection{Massaging the proposition\label{sec:ind-var-in-prems}}
|
|
12 |
%
|
|
13 |
\begin{isamarkuptext}%
|
|
14 |
\noindent
|
|
15 |
So far we have assumed that the theorem we want to prove is already in a form
|
|
16 |
that is amenable to induction, but this is not always the case:%
|
|
17 |
\end{isamarkuptext}%
|
|
18 |
\isacommand{lemma}\ {"}xs\ {\isasymnoteq}\ []\ {\isasymLongrightarrow}\ hd(rev\ xs)\ =\ last\ xs{"}\isanewline
|
|
19 |
\isacommand{apply}(induct\_tac\ xs)%
|
|
20 |
\begin{isamarkuptxt}%
|
|
21 |
\noindent
|
|
22 |
(where \isa{hd} and \isa{last} return the first and last element of a
|
|
23 |
non-empty list)
|
|
24 |
produces the warning
|
|
25 |
\begin{quote}\tt
|
|
26 |
Induction variable occurs also among premises!
|
|
27 |
\end{quote}
|
|
28 |
and leads to the base case
|
|
29 |
\begin{isabellepar}%
|
|
30 |
\ 1.\ xs\ {\isasymnoteq}\ []\ {\isasymLongrightarrow}\ hd\ (rev\ [])\ =\ last\ []
|
|
31 |
\end{isabellepar}%
|
|
32 |
which, after simplification, becomes
|
|
33 |
\begin{isabellepar}%
|
|
34 |
\ 1.\ xs\ {\isasymnoteq}\ []\ {\isasymLongrightarrow}\ hd\ []\ =\ last\ []
|
|
35 |
\end{isabellepar}%
|
|
36 |
We cannot prove this equality because we do not know what \isa{hd} and
|
|
37 |
\isa{last} return when applied to \isa{[]}.
|
|
38 |
|
|
39 |
The point is that we have violated the above warning. Because the induction
|
|
40 |
formula is only the conclusion, the occurrence of \isa{xs} in the premises is
|
|
41 |
not modified by induction. Thus the case that should have been trivial
|
|
42 |
becomes unprovable. Fortunately, the solution is easy:
|
|
43 |
\begin{quote}
|
|
44 |
\emph{Pull all occurrences of the induction variable into the conclusion
|
|
45 |
using \isa{\isasymlongrightarrow}.}
|
|
46 |
\end{quote}
|
|
47 |
This means we should prove%
|
|
48 |
\end{isamarkuptxt}%
|
|
49 |
\isacommand{lemma}\ hd\_rev:\ {"}xs\ {\isasymnoteq}\ []\ {\isasymlongrightarrow}\ hd(rev\ xs)\ =\ last\ xs{"}%
|
|
50 |
\begin{isamarkuptext}%
|
|
51 |
\noindent
|
|
52 |
This time, induction leaves us with the following base case
|
|
53 |
\begin{isabellepar}%
|
|
54 |
\ 1.\ []\ {\isasymnoteq}\ []\ {\isasymlongrightarrow}\ hd\ (rev\ [])\ =\ last\ []
|
|
55 |
\end{isabellepar}%
|
|
56 |
which is trivial, and \isa{auto} finishes the whole proof.
|
|
57 |
|
|
58 |
If \isa{hd\_rev} is meant to be simplification rule, you are done. But if you
|
|
59 |
really need the \isa{\isasymLongrightarrow}-version of \isa{hd\_rev}, for
|
|
60 |
example because you want to apply it as an introduction rule, you need to
|
|
61 |
derive it separately, by combining it with modus ponens:%
|
|
62 |
\end{isamarkuptext}%
|
|
63 |
\isacommand{lemmas}\ hd\_revI\ =\ hd\_rev[THEN\ mp]%
|
|
64 |
\begin{isamarkuptext}%
|
|
65 |
\noindent
|
|
66 |
which yields the lemma we originally set out to prove.
|
|
67 |
|
|
68 |
In case there are multiple premises $A@1$, \dots, $A@n$ containing the
|
|
69 |
induction variable, you should turn the conclusion $C$ into
|
|
70 |
\[ A@1 \longrightarrow \cdots A@n \longrightarrow C \]
|
|
71 |
(see the remark?? in \S\ref{??}).
|
|
72 |
Additionally, you may also have to universally quantify some other variables,
|
|
73 |
which can yield a fairly complex conclusion.
|
|
74 |
Here is a simple example (which is proved by \isa{blast}):%
|
|
75 |
\end{isamarkuptext}%
|
|
76 |
\isacommand{lemma}\ simple:\ {"}{\isasymforall}\ y.\ A\ y\ {\isasymlongrightarrow}\ B\ y\ {\isasymlongrightarrow}\ B\ y\ \&\ A\ y{"}%
|
|
77 |
\begin{isamarkuptext}%
|
|
78 |
\noindent
|
|
79 |
You can get the desired lemma by explicit
|
|
80 |
application of modus ponens and \isa{spec}:%
|
|
81 |
\end{isamarkuptext}%
|
|
82 |
\isacommand{lemmas}\ myrule\ =\ simple[THEN\ spec,\ THEN\ mp,\ THEN\ mp]%
|
|
83 |
\begin{isamarkuptext}%
|
|
84 |
\noindent
|
|
85 |
or the wholesale stripping of \isa{\isasymforall} and
|
|
86 |
\isa{\isasymlongrightarrow} in the conclusion via \isa{rulify}%
|
|
87 |
\end{isamarkuptext}%
|
|
88 |
\isacommand{lemmas}\ myrule\ =\ simple[rulify]%
|
|
89 |
\begin{isamarkuptext}%
|
|
90 |
\noindent
|
|
91 |
yielding \isa{{\isasymlbrakk}\mbox{?A}\ \mbox{?y};\ \mbox{?B}\ \mbox{?y}{\isasymrbrakk}\ {\isasymLongrightarrow}\ \mbox{?B}\ \mbox{?y}\ {\isasymand}\ \mbox{?A}\ \mbox{?y}}.
|
|
92 |
You can go one step further and include these derivations already in the
|
|
93 |
statement of your original lemma, thus avoiding the intermediate step:%
|
|
94 |
\end{isamarkuptext}%
|
|
95 |
\isacommand{lemma}\ myrule[rulify]:\ \ {"}{\isasymforall}\ y.\ A\ y\ {\isasymlongrightarrow}\ B\ y\ {\isasymlongrightarrow}\ B\ y\ \&\ A\ y{"}%
|
|
96 |
\begin{isamarkuptext}%
|
|
97 |
\bigskip
|
|
98 |
|
|
99 |
A second reason why your proposition may not be amenable to induction is that
|
|
100 |
you want to induct on a whole term, rather than an individual variable. In
|
|
101 |
general, when inducting on some term $t$ you must rephrase the conclusion as
|
|
102 |
\[ \forall y@1 \dots y@n.~ x = t \longrightarrow C \] where $y@1 \dots y@n$
|
|
103 |
are the free variables in $t$ and $x$ is new, and perform induction on $x$
|
|
104 |
afterwards. An example appears below.%
|
|
105 |
\end{isamarkuptext}%
|
|
106 |
%
|
|
107 |
\isamarkupsubsection{Beyond structural induction}
|
|
108 |
%
|
|
109 |
\begin{isamarkuptext}%
|
|
110 |
So far, inductive proofs where by structural induction for
|
|
111 |
primitive recursive functions and recursion induction for total recursive
|
|
112 |
functions. But sometimes structural induction is awkward and there is no
|
|
113 |
recursive function in sight either that could furnish a more appropriate
|
|
114 |
induction schema. In such cases some existing standard induction schema can
|
|
115 |
be helpful. We show how to apply such induction schemas by an example.
|
|
116 |
|
|
117 |
Structural induction on \isa{nat} is
|
|
118 |
usually known as ``mathematical induction''. There is also ``complete
|
|
119 |
induction'', where you must prove $P(n)$ under the assumption that $P(m)$
|
|
120 |
holds for all $m<n$. In Isabelle, this is the theorem \isa{less\_induct}:
|
|
121 |
\begin{quote}
|
|
122 |
|
|
123 |
\begin{isabelle}%
|
|
124 |
({\isasymAnd}\mbox{n}.\ {\isasymforall}\mbox{m}.\ \mbox{m}\ <\ \mbox{n}\ {\isasymlongrightarrow}\ \mbox{?P}\ \mbox{m}\ {\isasymLongrightarrow}\ \mbox{?P}\ \mbox{n})\ {\isasymLongrightarrow}\ \mbox{?P}\ \mbox{?n}
|
|
125 |
\end{isabelle}%
|
|
126 |
|
|
127 |
\end{quote}
|
|
128 |
Here is an example of its application.%
|
|
129 |
\end{isamarkuptext}%
|
|
130 |
\isacommand{consts}\ f\ ::\ {"}nat\ =>\ nat{"}\isanewline
|
|
131 |
\isacommand{axioms}\ f\_ax:\ {"}f(f(n))\ <\ f(Suc(n)){"}%
|
|
132 |
\begin{isamarkuptext}%
|
|
133 |
\noindent
|
|
134 |
From the above axiom\footnote{In general, the use of axioms is strongly
|
|
135 |
discouraged, because of the danger of inconsistencies. The above axiom does
|
|
136 |
not introduce an inconsistency because, for example, the identity function
|
|
137 |
satisfies it.}
|
|
138 |
for \isa{f} it follows that \isa{\mbox{n}\ {\isasymle}\ f\ \mbox{n}}, which can
|
|
139 |
be proved by induction on \isa{f\ \mbox{n}}. Following the recipy outlined
|
|
140 |
above, we have to phrase the proposition as follows to allow induction:%
|
|
141 |
\end{isamarkuptext}%
|
|
142 |
\isacommand{lemma}\ f\_incr\_lem:\ {"}{\isasymforall}i.\ k\ =\ f\ i\ {\isasymlongrightarrow}\ i\ {\isasymle}\ f\ i{"}%
|
|
143 |
\begin{isamarkuptxt}%
|
|
144 |
\noindent
|
|
145 |
To perform induction on \isa{k} using \isa{less\_induct}, we use the same
|
|
146 |
general induction method as for recursion induction (see
|
|
147 |
\S\ref{sec:recdef-induction}):%
|
|
148 |
\end{isamarkuptxt}%
|
|
149 |
\isacommand{apply}(induct\_tac\ k\ rule:less\_induct)%
|
|
150 |
\begin{isamarkuptxt}%
|
|
151 |
\noindent
|
|
152 |
which leaves us with the following proof state:
|
|
153 |
\begin{isabellepar}%
|
|
154 |
\ 1.\ {\isasymAnd}\mbox{n}.\ {\isasymforall}\mbox{m}.\ \mbox{m}\ <\ \mbox{n}\ {\isasymlongrightarrow}\ ({\isasymforall}\mbox{i}.\ \mbox{m}\ =\ f\ \mbox{i}\ {\isasymlongrightarrow}\ \mbox{i}\ {\isasymle}\ f\ \mbox{i})\isanewline
|
|
155 |
\ \ \ \ \ \ \ {\isasymLongrightarrow}\ {\isasymforall}\mbox{i}.\ \mbox{n}\ =\ f\ \mbox{i}\ {\isasymlongrightarrow}\ \mbox{i}\ {\isasymle}\ f\ \mbox{i}
|
|
156 |
\end{isabellepar}%
|
|
157 |
After stripping the \isa{\isasymforall i}, the proof continues with a case
|
|
158 |
distinction on \isa{i}. The case \isa{i = 0} is trivial and we focus on the
|
|
159 |
other case:
|
|
160 |
\begin{isabellepar}%
|
|
161 |
\ 1.\ {\isasymAnd}\mbox{n}\ \mbox{i}\ \mbox{nat}.\isanewline
|
|
162 |
\ \ \ \ \ \ \ {\isasymlbrakk}{\isasymforall}\mbox{m}.\ \mbox{m}\ <\ \mbox{n}\ {\isasymlongrightarrow}\ ({\isasymforall}\mbox{i}.\ \mbox{m}\ =\ f\ \mbox{i}\ {\isasymlongrightarrow}\ \mbox{i}\ {\isasymle}\ f\ \mbox{i});\ \mbox{i}\ =\ Suc\ \mbox{nat}{\isasymrbrakk}\isanewline
|
|
163 |
\ \ \ \ \ \ \ {\isasymLongrightarrow}\ \mbox{n}\ =\ f\ \mbox{i}\ {\isasymlongrightarrow}\ \mbox{i}\ {\isasymle}\ f\ \mbox{i}
|
|
164 |
\end{isabellepar}%%
|
|
165 |
\end{isamarkuptxt}%
|
|
166 |
\isacommand{by}(blast\ intro!:\ f\_ax\ Suc\_leI\ intro:le\_less\_trans)%
|
|
167 |
\begin{isamarkuptext}%
|
|
168 |
\noindent
|
|
169 |
It is not surprising if you find the last step puzzling.
|
|
170 |
The proof goes like this (writing \isa{j} instead of \isa{nat}).
|
|
171 |
Since \isa{\mbox{i}\ =\ Suc\ \mbox{j}} it suffices to show
|
|
172 |
\isa{\mbox{j}\ <\ f\ (Suc\ \mbox{j})} (by \isa{Suc\_leI}: \isa{\mbox{?m}\ <\ \mbox{?n}\ {\isasymLongrightarrow}\ Suc\ \mbox{?m}\ {\isasymle}\ \mbox{?n}}). This is
|
|
173 |
proved as follows. From \isa{f\_ax} we have \isa{f\ (f\ \mbox{j})\ <\ f\ (Suc\ \mbox{j})}
|
|
174 |
(1) which implies \isa{f\ \mbox{j}\ {\isasymle}\ f\ (f\ \mbox{j})} (by the induction hypothesis).
|
|
175 |
Using (1) once more we obtain \isa{f\ \mbox{j}\ <\ f\ (Suc\ \mbox{j})} (2) by transitivity
|
|
176 |
(\isa{le_less_trans}: \isa{{\isasymlbrakk}\mbox{?i}\ {\isasymle}\ \mbox{?j};\ \mbox{?j}\ <\ \mbox{?k}{\isasymrbrakk}\ {\isasymLongrightarrow}\ \mbox{?i}\ <\ \mbox{?k}}).
|
|
177 |
Using the induction hypothesis once more we obtain \isa{\mbox{j}\ {\isasymle}\ f\ \mbox{j}}
|
|
178 |
which, together with (2) yields \isa{\mbox{j}\ <\ f\ (Suc\ \mbox{j})} (again by
|
|
179 |
\isa{le_less_trans}).
|
|
180 |
|
|
181 |
This last step shows both the power and the danger of automatic proofs: they
|
|
182 |
will usually not tell you how the proof goes, because it can be very hard to
|
|
183 |
translate the internal proof into a human-readable format. Therefore
|
|
184 |
\S\ref{sec:part2?} introduces a language for writing readable yet concise
|
|
185 |
proofs.
|
|
186 |
|
|
187 |
We can now derive the desired \isa{\mbox{i}\ {\isasymle}\ f\ \mbox{i}} from \isa{f\_incr}:%
|
|
188 |
\end{isamarkuptext}%
|
|
189 |
\isacommand{lemmas}\ f\_incr\ =\ f\_incr\_lem[rulify,\ OF\ refl]%
|
|
190 |
\begin{isamarkuptext}%
|
|
191 |
The final \isa{refl} gets rid of the premise \isa{?k = f ?i}. Again, we could
|
|
192 |
have included this derivation in the original statement of the lemma:%
|
|
193 |
\end{isamarkuptext}%
|
|
194 |
\isacommand{lemma}\ f\_incr[rulify,\ OF\ refl]:\ {"}{\isasymforall}i.\ k\ =\ f\ i\ {\isasymlongrightarrow}\ i\ {\isasymle}\ f\ i{"}%
|
|
195 |
\begin{isamarkuptext}%
|
|
196 |
\begin{exercise}
|
|
197 |
From the above axiom and lemma for \isa{f} show that \isa{f} is the identity.
|
|
198 |
\end{exercise}
|
|
199 |
|
|
200 |
In general, \isa{induct\_tac} can be applied with any rule \isa{r}
|
|
201 |
whose conclusion is of the form \isa{?P ?x1 \dots ?xn}, in which case the
|
|
202 |
format is
|
|
203 |
\begin{ttbox}
|
|
204 |
apply(induct_tac y1 ... yn rule: r)
|
|
205 |
\end{ttbox}\index{*induct_tac}%
|
|
206 |
where \isa{y1}, \dots, \isa{yn} are variables in the first subgoal.
|
|
207 |
In fact, \isa{induct\_tac} even allows the conclusion of
|
|
208 |
\isa{r} to be an (iterated) conjunction of formulae of the above form, in
|
|
209 |
which case the application is
|
|
210 |
\begin{ttbox}
|
|
211 |
apply(induct_tac y1 ... yn and ... and z1 ... zm rule: r)
|
|
212 |
\end{ttbox}
|
|
213 |
|
|
214 |
Finally we should mention that HOL already provides the mother of all
|
|
215 |
inductions, \emph{wellfounded induction} (\isa{wf\_induct}):
|
|
216 |
\begin{quote}
|
|
217 |
|
|
218 |
\begin{isabelle}%
|
|
219 |
{\isasymlbrakk}wf\ \mbox{?r};\ {\isasymAnd}\mbox{x}.\ {\isasymforall}\mbox{y}.\ (\mbox{y},\ \mbox{x})\ {\isasymin}\ \mbox{?r}\ {\isasymlongrightarrow}\ \mbox{?P}\ \mbox{y}\ {\isasymLongrightarrow}\ \mbox{?P}\ \mbox{x}{\isasymrbrakk}\ {\isasymLongrightarrow}\ \mbox{?P}\ \mbox{?a}
|
|
220 |
\end{isabelle}%
|
|
221 |
|
|
222 |
\end{quote}
|
|
223 |
For details see the library.%
|
|
224 |
\end{isamarkuptext}%
|
|
225 |
\end{isabelle}%
|
|
226 |
%%% Local Variables:
|
|
227 |
%%% mode: latex
|
|
228 |
%%% TeX-master: "root"
|
|
229 |
%%% End:
|