8745
|
1 |
(*<*)
|
|
2 |
theory Itrev = Main:;
|
|
3 |
(*>*)
|
|
4 |
|
9754
|
5 |
text{*
|
|
6 |
Function @{term"rev"} has quadratic worst-case running time
|
9792
|
7 |
because it calls function @{text"@"} for each element of the list and
|
|
8 |
@{text"@"} is linear in its first argument. A linear time version of
|
9754
|
9 |
@{term"rev"} reqires an extra argument where the result is accumulated
|
9792
|
10 |
gradually, using only @{text"#"}:
|
9754
|
11 |
*}
|
8745
|
12 |
|
|
13 |
consts itrev :: "'a list \\<Rightarrow> 'a list \\<Rightarrow> 'a list";
|
|
14 |
primrec
|
|
15 |
"itrev [] ys = ys"
|
|
16 |
"itrev (x#xs) ys = itrev xs (x#ys)";
|
|
17 |
|
9754
|
18 |
text{*\noindent
|
|
19 |
The behaviour of @{term"itrev"} is simple: it reverses
|
9493
|
20 |
its first argument by stacking its elements onto the second argument,
|
|
21 |
and returning that second argument when the first one becomes
|
9754
|
22 |
empty. Note that @{term"itrev"} is tail-recursive, i.e.\ it can be
|
9493
|
23 |
compiled into a loop.
|
|
24 |
|
9754
|
25 |
Naturally, we would like to show that @{term"itrev"} does indeed reverse
|
|
26 |
its first argument provided the second one is empty:
|
|
27 |
*};
|
8745
|
28 |
|
|
29 |
lemma "itrev xs [] = rev xs";
|
|
30 |
|
|
31 |
txt{*\noindent
|
|
32 |
There is no choice as to the induction variable, and we immediately simplify:
|
9458
|
33 |
*};
|
8745
|
34 |
|
9689
|
35 |
apply(induct_tac xs, simp_all);
|
8745
|
36 |
|
|
37 |
txt{*\noindent
|
|
38 |
Unfortunately, this is not a complete success:
|
9723
|
39 |
\begin{isabelle}
|
8745
|
40 |
~1.~\dots~itrev~list~[]~=~rev~list~{\isasymLongrightarrow}~itrev~list~[a]~=~rev~list~@~[a]%
|
9723
|
41 |
\end{isabelle}
|
8745
|
42 |
Just as predicted above, the overall goal, and hence the induction
|
|
43 |
hypothesis, is too weak to solve the induction step because of the fixed
|
9754
|
44 |
@{term"[]"}. The corresponding heuristic:
|
8745
|
45 |
\begin{quote}
|
9754
|
46 |
\emph{Generalize goals for induction by replacing constants by variables.}
|
8745
|
47 |
\end{quote}
|
9754
|
48 |
Of course one cannot do this na\"{\i}vely: @{term"itrev xs ys = rev xs"} is
|
8745
|
49 |
just not true---the correct generalization is
|
9458
|
50 |
*};
|
8745
|
51 |
(*<*)oops;(*>*)
|
|
52 |
lemma "itrev xs ys = rev xs @ ys";
|
|
53 |
|
|
54 |
txt{*\noindent
|
9754
|
55 |
If @{term"ys"} is replaced by @{term"[]"}, the right-hand side simplifies to
|
9541
|
56 |
@{term"rev xs"}, just as required.
|
8745
|
57 |
|
|
58 |
In this particular instance it was easy to guess the right generalization,
|
|
59 |
but in more complex situations a good deal of creativity is needed. This is
|
|
60 |
the main source of complications in inductive proofs.
|
|
61 |
|
9754
|
62 |
Although we now have two variables, only @{term"xs"} is suitable for
|
8745
|
63 |
induction, and we repeat our above proof attempt. Unfortunately, we are still
|
|
64 |
not there:
|
9723
|
65 |
\begin{isabelle}
|
8745
|
66 |
~1.~{\isasymAnd}a~list.\isanewline
|
|
67 |
~~~~~~~itrev~list~ys~=~rev~list~@~ys~{\isasymLongrightarrow}\isanewline
|
9754
|
68 |
~~~~~~~itrev~list~(a~\#~ys)~=~rev~list~@~a~\#~ys
|
9723
|
69 |
\end{isabelle}
|
8745
|
70 |
The induction hypothesis is still too weak, but this time it takes no
|
9754
|
71 |
intuition to generalize: the problem is that @{term"ys"} is fixed throughout
|
8745
|
72 |
the subgoal, but the induction hypothesis needs to be applied with
|
9754
|
73 |
@{term"a # ys"} instead of @{term"ys"}. Hence we prove the theorem
|
|
74 |
for all @{term"ys"} instead of a fixed one:
|
9458
|
75 |
*};
|
8745
|
76 |
(*<*)oops;(*>*)
|
|
77 |
lemma "\\<forall>ys. itrev xs ys = rev xs @ ys";
|
|
78 |
|
|
79 |
txt{*\noindent
|
9754
|
80 |
This time induction on @{term"xs"} followed by simplification succeeds. This
|
8745
|
81 |
leads to another heuristic for generalization:
|
|
82 |
\begin{quote}
|
9754
|
83 |
\emph{Generalize goals for induction by universally quantifying all free
|
8745
|
84 |
variables {\em(except the induction variable itself!)}.}
|
|
85 |
\end{quote}
|
|
86 |
This prevents trivial failures like the above and does not change the
|
|
87 |
provability of the goal. Because it is not always required, and may even
|
|
88 |
complicate matters in some cases, this heuristic is often not
|
|
89 |
applied blindly.
|
9644
|
90 |
|
|
91 |
In general, if you have tried the above heuristics and still find your
|
|
92 |
induction does not go through, and no obvious lemma suggests itself, you may
|
|
93 |
need to generalize your proposition even further. This requires insight into
|
|
94 |
the problem at hand and is beyond simple rules of thumb. In a nutshell: you
|
|
95 |
will need to be creative. Additionally, you can read \S\ref{sec:advanced-ind}
|
|
96 |
to learn about some advanced techniques for inductive proofs.
|
9458
|
97 |
*};
|
8745
|
98 |
|
|
99 |
(*<*)
|
9689
|
100 |
by(induct_tac xs, simp_all);
|
8745
|
101 |
end
|
|
102 |
(*>*)
|