apply. -> by
6 We define a tail-recursive version of list-reversal,
7 i.e.\ one that can be compiled into a loop:
10 consts itrev :: "'a list \\<Rightarrow> 'a list \\<Rightarrow> 'a list";
13 "itrev (x#xs) ys = itrev xs (x#ys)";
16 The behaviour of \isa{itrev} is simple: it reverses its first argument by
17 stacking its elements onto the second argument, and returning that second
18 argument when the first one becomes empty.
19 We need to show that \isa{itrev} does indeed reverse its first argument
20 provided the second one is empty:
23 lemma "itrev xs [] = rev xs";
26 There is no choice as to the induction variable, and we immediately simplify:
29 apply(induct_tac xs, auto);
32 Unfortunately, this is not a complete success:
34 ~1.~\dots~itrev~list~[]~=~rev~list~{\isasymLongrightarrow}~itrev~list~[a]~=~rev~list~@~[a]%
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:
40 {\em 3. Generalize goals for induction by replacing constants by variables.}
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
47 lemma "itrev xs ys = rev xs @ ys";
50 If \isa{ys} is replaced by \isa{[]}, the right-hand side simplifies to
51 \isa{rev xs}, just as required.
53 In this particular instance it was easy to guess the right generalization,
54 but in more complex situations a good deal of creativity is needed. This is
55 the main source of complications in inductive proofs.
57 Although we now have two variables, only \isa{xs} is suitable for
58 induction, and we repeat our above proof attempt. Unfortunately, we are still
61 ~1.~{\isasymAnd}a~list.\isanewline
62 ~~~~~~~itrev~list~ys~=~rev~list~@~ys~{\isasymLongrightarrow}\isanewline
63 ~~~~~~~itrev~list~(a~\#~ys)~=~rev~list~@~a~\#~ys%
65 The induction hypothesis is still too weak, but this time it takes no
66 intuition to generalize: the problem is that \isa{ys} is fixed throughout
67 the subgoal, but the induction hypothesis needs to be applied with
68 \isa{a \# ys} instead of \isa{ys}. Hence we prove the theorem
69 for all \isa{ys} instead of a fixed one:
72 lemma "\\<forall>ys. itrev xs ys = rev xs @ ys";
75 This time induction on \isa{xs} followed by simplification succeeds. This
76 leads to another heuristic for generalization:
78 {\em 4. Generalize goals for induction by universally quantifying all free
79 variables {\em(except the induction variable itself!)}.}
81 This prevents trivial failures like the above and does not change the
82 provability of the goal. Because it is not always required, and may even
83 complicate matters in some cases, this heuristic is often not
88 by(induct_tac xs, simp, simp);