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