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