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