| 9722 |      1 | %
 | 
|  |      2 | \begin{isabellebody}%
 | 
| 9924 |      3 | \def\isabellecontext{Itrev}%
 | 
| 11866 |      4 | \isamarkupfalse%
 | 
| 8749 |      5 | %
 | 
| 10878 |      6 | \isamarkupsection{Induction Heuristics%
 | 
| 10397 |      7 | }
 | 
| 11866 |      8 | \isamarkuptrue%
 | 
| 9844 |      9 | %
 | 
| 8749 |     10 | \begin{isamarkuptext}%
 | 
| 9844 |     11 | \label{sec:InductionHeuristics}
 | 
| 11458 |     12 | \index{induction heuristics|(}%
 | 
| 9844 |     13 | The purpose of this section is to illustrate some simple heuristics for
 | 
|  |     14 | inductive proofs. The first one we have already mentioned in our initial
 | 
|  |     15 | example:
 | 
|  |     16 | \begin{quote}
 | 
|  |     17 | \emph{Theorems about recursive functions are proved by induction.}
 | 
|  |     18 | \end{quote}
 | 
|  |     19 | In case the function has more than one argument
 | 
|  |     20 | \begin{quote}
 | 
|  |     21 | \emph{Do induction on argument number $i$ if the function is defined by
 | 
|  |     22 | recursion in argument number $i$.}
 | 
|  |     23 | \end{quote}
 | 
| 11458 |     24 | When we look at the proof of \isa{{\isacharparenleft}xs{\isacharat}ys{\isacharparenright}\ {\isacharat}\ zs\ {\isacharequal}\ xs\ {\isacharat}\ {\isacharparenleft}ys{\isacharat}zs{\isacharparenright}}
 | 
|  |     25 | in \S\ref{sec:intro-proof} we find
 | 
|  |     26 | \begin{itemize}
 | 
|  |     27 | \item \isa{{\isacharat}} is recursive in
 | 
|  |     28 | the first argument
 | 
|  |     29 | \item \isa{xs}  occurs only as the first argument of
 | 
|  |     30 | \isa{{\isacharat}}
 | 
|  |     31 | \item both \isa{ys} and \isa{zs} occur at least once as
 | 
|  |     32 | the second argument of \isa{{\isacharat}}
 | 
|  |     33 | \end{itemize}
 | 
|  |     34 | Hence it is natural to perform induction on~\isa{xs}.
 | 
| 9844 |     35 | 
 | 
|  |     36 | The key heuristic, and the main point of this section, is to
 | 
| 11458 |     37 | \emph{generalize the goal before induction}.
 | 
|  |     38 | The reason is simple: if the goal is
 | 
| 9844 |     39 | too specific, the induction hypothesis is too weak to allow the induction
 | 
| 10971 |     40 | step to go through. Let us illustrate the idea with an example.
 | 
| 9844 |     41 | 
 | 
| 11458 |     42 | Function \cdx{rev} has quadratic worst-case running time
 | 
| 9792 |     43 | because it calls function \isa{{\isacharat}} for each element of the list and
 | 
|  |     44 | \isa{{\isacharat}} is linear in its first argument.  A linear time version of
 | 
| 9493 |     45 | \isa{rev} reqires an extra argument where the result is accumulated
 | 
| 11458 |     46 | gradually, using only~\isa{{\isacharhash}}:%
 | 
| 8749 |     47 | \end{isamarkuptext}%
 | 
| 11866 |     48 | \isamarkuptrue%
 | 
| 9673 |     49 | \isacommand{consts}\ itrev\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
 | 
| 11866 |     50 | \isamarkupfalse%
 | 
| 8749 |     51 | \isacommand{primrec}\isanewline
 | 
| 9673 |     52 | {\isachardoublequote}itrev\ {\isacharbrackleft}{\isacharbrackright}\ \ \ \ \ ys\ {\isacharequal}\ ys{\isachardoublequote}\isanewline
 | 
| 11866 |     53 | {\isachardoublequote}itrev\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ ys\ {\isacharequal}\ itrev\ xs\ {\isacharparenleft}x{\isacharhash}ys{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
 | 
|  |     54 | %
 | 
| 8749 |     55 | \begin{isamarkuptext}%
 | 
| 9792 |     56 | \noindent
 | 
| 11458 |     57 | The behaviour of \cdx{itrev} is simple: it reverses
 | 
| 9493 |     58 | its first argument by stacking its elements onto the second argument,
 | 
|  |     59 | and returning that second argument when the first one becomes
 | 
| 11458 |     60 | empty. Note that \isa{itrev} is tail-recursive: it can be
 | 
| 9493 |     61 | compiled into a loop.
 | 
|  |     62 | 
 | 
|  |     63 | Naturally, we would like to show that \isa{itrev} does indeed reverse
 | 
|  |     64 | its first argument provided the second one is empty:%
 | 
| 8749 |     65 | \end{isamarkuptext}%
 | 
| 11866 |     66 | \isamarkuptrue%
 | 
|  |     67 | \isacommand{lemma}\ {\isachardoublequote}itrev\ xs\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ rev\ xs{\isachardoublequote}\isamarkupfalse%
 | 
| 16069 |     68 | %
 | 
|  |     69 | \begin{isamarkuptxt}%
 | 
|  |     70 | \noindent
 | 
|  |     71 | There is no choice as to the induction variable, and we immediately simplify:%
 | 
|  |     72 | \end{isamarkuptxt}%
 | 
| 11866 |     73 | \isamarkuptrue%
 | 
| 16069 |     74 | \isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isamarkupfalse%
 | 
|  |     75 | %
 | 
|  |     76 | \begin{isamarkuptxt}%
 | 
|  |     77 | \noindent
 | 
|  |     78 | Unfortunately, this attempt does not prove
 | 
|  |     79 | the induction step:
 | 
|  |     80 | \begin{isabelle}%
 | 
|  |     81 | \ {\isadigit{1}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
 | 
|  |     82 | \isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }itrev\ list\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ rev\ list\ {\isasymLongrightarrow}\ itrev\ list\ {\isacharbrackleft}a{\isacharbrackright}\ {\isacharequal}\ rev\ list\ {\isacharat}\ {\isacharbrackleft}a{\isacharbrackright}%
 | 
|  |     83 | \end{isabelle}
 | 
|  |     84 | The induction hypothesis is too weak.  The fixed
 | 
|  |     85 | argument,~\isa{{\isacharbrackleft}{\isacharbrackright}}, prevents it from rewriting the conclusion.  
 | 
|  |     86 | This example suggests a heuristic:
 | 
|  |     87 | \begin{quote}\index{generalizing induction formulae}%
 | 
|  |     88 | \emph{Generalize goals for induction by replacing constants by variables.}
 | 
|  |     89 | \end{quote}
 | 
|  |     90 | Of course one cannot do this na\"{\i}vely: \isa{itrev\ xs\ ys\ {\isacharequal}\ rev\ xs} is
 | 
|  |     91 | just not true.  The correct generalization is%
 | 
|  |     92 | \end{isamarkuptxt}%
 | 
| 11866 |     93 | \isamarkuptrue%
 | 
|  |     94 | \isamarkupfalse%
 | 
| 13791 |     95 | \isacommand{lemma}\ {\isachardoublequote}itrev\ xs\ ys\ {\isacharequal}\ rev\ xs\ {\isacharat}\ ys{\isachardoublequote}\isamarkupfalse%
 | 
| 11866 |     96 | \isamarkupfalse%
 | 
| 16069 |     97 | %
 | 
|  |     98 | \begin{isamarkuptxt}%
 | 
|  |     99 | \noindent
 | 
|  |    100 | If \isa{ys} is replaced by \isa{{\isacharbrackleft}{\isacharbrackright}}, the right-hand side simplifies to
 | 
|  |    101 | \isa{rev\ xs}, as required.
 | 
|  |    102 | 
 | 
|  |    103 | In this instance it was easy to guess the right generalization.
 | 
|  |    104 | Other situations can require a good deal of creativity.  
 | 
|  |    105 | 
 | 
|  |    106 | Although we now have two variables, only \isa{xs} is suitable for
 | 
|  |    107 | induction, and we repeat our proof attempt. Unfortunately, we are still
 | 
|  |    108 | not there:
 | 
|  |    109 | \begin{isabelle}%
 | 
|  |    110 | \ {\isadigit{1}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
 | 
|  |    111 | \isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }itrev\ list\ ys\ {\isacharequal}\ rev\ list\ {\isacharat}\ ys\ {\isasymLongrightarrow}\isanewline
 | 
|  |    112 | \isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }itrev\ list\ {\isacharparenleft}a\ {\isacharhash}\ ys{\isacharparenright}\ {\isacharequal}\ rev\ list\ {\isacharat}\ a\ {\isacharhash}\ ys%
 | 
|  |    113 | \end{isabelle}
 | 
|  |    114 | The induction hypothesis is still too weak, but this time it takes no
 | 
|  |    115 | intuition to generalize: the problem is that \isa{ys} is fixed throughout
 | 
|  |    116 | the subgoal, but the induction hypothesis needs to be applied with
 | 
|  |    117 | \isa{a\ {\isacharhash}\ ys} instead of \isa{ys}. Hence we prove the theorem
 | 
|  |    118 | for all \isa{ys} instead of a fixed one:%
 | 
|  |    119 | \end{isamarkuptxt}%
 | 
| 11866 |    120 | \isamarkuptrue%
 | 
|  |    121 | \isamarkupfalse%
 | 
| 13791 |    122 | \isacommand{lemma}\ {\isachardoublequote}{\isasymforall}ys{\isachardot}\ itrev\ xs\ ys\ {\isacharequal}\ rev\ xs\ {\isacharat}\ ys{\isachardoublequote}\isamarkupfalse%
 | 
| 11866 |    123 | \isamarkupfalse%
 | 
|  |    124 | %
 | 
| 9844 |    125 | \begin{isamarkuptext}%
 | 
| 8749 |    126 | \noindent
 | 
|  |    127 | This time induction on \isa{xs} followed by simplification succeeds. This
 | 
|  |    128 | leads to another heuristic for generalization:
 | 
|  |    129 | \begin{quote}
 | 
| 9754 |    130 | \emph{Generalize goals for induction by universally quantifying all free
 | 
| 8749 |    131 | variables {\em(except the induction variable itself!)}.}
 | 
|  |    132 | \end{quote}
 | 
| 11458 |    133 | This prevents trivial failures like the one above and does not affect the
 | 
|  |    134 | validity of the goal.  However, this heuristic should not be applied blindly.
 | 
|  |    135 | It is not always required, and the additional quantifiers can complicate
 | 
| 13081 |    136 | matters in some cases. The variables that should be quantified are typically
 | 
| 11458 |    137 | those that change in recursive calls.
 | 
| 9644 |    138 | 
 | 
| 9844 |    139 | A final point worth mentioning is the orientation of the equation we just
 | 
|  |    140 | proved: the more complex notion (\isa{itrev}) is on the left-hand
 | 
|  |    141 | side, the simpler one (\isa{rev}) on the right-hand side. This constitutes
 | 
|  |    142 | another, albeit weak heuristic that is not restricted to induction:
 | 
|  |    143 | \begin{quote}
 | 
|  |    144 |   \emph{The right-hand side of an equation should (in some sense) be simpler
 | 
|  |    145 |     than the left-hand side.}
 | 
|  |    146 | \end{quote}
 | 
|  |    147 | This heuristic is tricky to apply because it is not obvious that
 | 
|  |    148 | \isa{rev\ xs\ {\isacharat}\ ys} is simpler than \isa{itrev\ xs\ ys}. But see what
 | 
| 10971 |    149 | happens if you try to prove \isa{rev\ xs\ {\isacharat}\ ys\ {\isacharequal}\ itrev\ xs\ ys}!
 | 
|  |    150 | 
 | 
| 11458 |    151 | If you have tried these heuristics and still find your
 | 
| 10971 |    152 | induction does not go through, and no obvious lemma suggests itself, you may
 | 
|  |    153 | need to generalize your proposition even further. This requires insight into
 | 
| 11458 |    154 | the problem at hand and is beyond simple rules of thumb.  
 | 
|  |    155 | Additionally, you can read \S\ref{sec:advanced-ind}
 | 
| 10971 |    156 | to learn about some advanced techniques for inductive proofs.%
 | 
| 11458 |    157 | \index{induction heuristics|)}%
 | 
| 9844 |    158 | \end{isamarkuptext}%
 | 
| 11866 |    159 | \isamarkuptrue%
 | 
|  |    160 | \isamarkupfalse%
 | 
| 9722 |    161 | \end{isabellebody}%
 | 
| 9145 |    162 | %%% Local Variables:
 | 
|  |    163 | %%% mode: latex
 | 
|  |    164 | %%% TeX-master: "root"
 | 
|  |    165 | %%% End:
 |