8749
|
1 |
\begin{isabelle}%
|
|
2 |
%
|
|
3 |
\begin{isamarkuptext}%
|
|
4 |
We define a tail-recursive version of list-reversal,
|
|
5 |
i.e.\ one that can be compiled into a loop:%
|
|
6 |
\end{isamarkuptext}%
|
|
7 |
\isacommand{consts}~itrev~::~{"}'a~list~{\isasymRightarrow}~'a~list~{\isasymRightarrow}~'a~list{"}\isanewline
|
|
8 |
\isacommand{primrec}\isanewline
|
|
9 |
{"}itrev~[]~~~~~ys~=~ys{"}\isanewline
|
|
10 |
{"}itrev~(x\#xs)~ys~=~itrev~xs~(x\#ys){"}%
|
|
11 |
\begin{isamarkuptext}%
|
|
12 |
\noindent
|
|
13 |
The behaviour of \isa{itrev} is simple: it reverses its first argument by
|
|
14 |
stacking its elements onto the second argument, and returning that second
|
|
15 |
argument when the first one becomes empty.
|
|
16 |
We need to show that \isa{itrev} does indeed reverse its first argument
|
|
17 |
provided the second one is empty:%
|
|
18 |
\end{isamarkuptext}%
|
|
19 |
\isacommand{lemma}~{"}itrev~xs~[]~=~rev~xs{"}%
|
|
20 |
\begin{isamarkuptxt}%
|
|
21 |
\noindent
|
|
22 |
There is no choice as to the induction variable, and we immediately simplify:%
|
|
23 |
\end{isamarkuptxt}%
|
|
24 |
\isacommand{apply}(induct\_tac~xs,~auto)%
|
|
25 |
\begin{isamarkuptxt}%
|
|
26 |
\noindent
|
|
27 |
Unfortunately, this is not a complete success:
|
|
28 |
\begin{isabellepar}%
|
|
29 |
~1.~\dots~itrev~list~[]~=~rev~list~{\isasymLongrightarrow}~itrev~list~[a]~=~rev~list~@~[a]%
|
|
30 |
\end{isabellepar}%
|
|
31 |
Just as predicted above, the overall goal, and hence the induction
|
|
32 |
hypothesis, is too weak to solve the induction step because of the fixed
|
|
33 |
\isa{[]}. The corresponding heuristic:
|
|
34 |
\begin{quote}
|
|
35 |
{\em 3. Generalize goals for induction by replacing constants by variables.}
|
|
36 |
\end{quote}
|
|
37 |
|
|
38 |
Of course one cannot do this na\"{\i}vely: \isa{itrev xs ys = rev xs} is
|
|
39 |
just not true---the correct generalization is%
|
|
40 |
\end{isamarkuptxt}%
|
|
41 |
\isacommand{lemma}~{"}itrev~xs~ys~=~rev~xs~@~ys{"}%
|
|
42 |
\begin{isamarkuptxt}%
|
|
43 |
\noindent
|
|
44 |
If \isa{ys} is replaced by \isa{[]}, the right-hand side simplifies to
|
|
45 |
\isa{rev xs}, just as required.
|
|
46 |
|
|
47 |
In this particular instance it was easy to guess the right generalization,
|
|
48 |
but in more complex situations a good deal of creativity is needed. This is
|
|
49 |
the main source of complications in inductive proofs.
|
|
50 |
|
|
51 |
Although we now have two variables, only \isa{xs} is suitable for
|
|
52 |
induction, and we repeat our above proof attempt. Unfortunately, we are still
|
|
53 |
not there:
|
|
54 |
\begin{isabellepar}%
|
|
55 |
~1.~{\isasymAnd}a~list.\isanewline
|
|
56 |
~~~~~~~itrev~list~ys~=~rev~list~@~ys~{\isasymLongrightarrow}\isanewline
|
|
57 |
~~~~~~~itrev~list~(a~\#~ys)~=~rev~list~@~a~\#~ys%
|
|
58 |
\end{isabellepar}%
|
|
59 |
The induction hypothesis is still too weak, but this time it takes no
|
|
60 |
intuition to generalize: the problem is that \isa{ys} is fixed throughout
|
|
61 |
the subgoal, but the induction hypothesis needs to be applied with
|
|
62 |
\isa{a \# ys} instead of \isa{ys}. Hence we prove the theorem
|
|
63 |
for all \isa{ys} instead of a fixed one:%
|
|
64 |
\end{isamarkuptxt}%
|
|
65 |
\isacommand{lemma}~{"}{\isasymforall}ys.~itrev~xs~ys~=~rev~xs~@~ys{"}%
|
|
66 |
\begin{isamarkuptxt}%
|
|
67 |
\noindent
|
|
68 |
This time induction on \isa{xs} followed by simplification succeeds. This
|
|
69 |
leads to another heuristic for generalization:
|
|
70 |
\begin{quote}
|
|
71 |
{\em 4. Generalize goals for induction by universally quantifying all free
|
|
72 |
variables {\em(except the induction variable itself!)}.}
|
|
73 |
\end{quote}
|
|
74 |
This prevents trivial failures like the above and does not change the
|
|
75 |
provability of the goal. Because it is not always required, and may even
|
|
76 |
complicate matters in some cases, this heuristic is often not
|
|
77 |
applied blindly.%
|
|
78 |
\end{isamarkuptxt}%
|
|
79 |
\end{isabelle}%
|
9145
|
80 |
%%% Local Variables:
|
|
81 |
%%% mode: latex
|
|
82 |
%%% TeX-master: "root"
|
|
83 |
%%% End:
|