| 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{primrec}\isamarkupfalse%
 | 
| 27015 |     75 | \ itrev\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
 | 
|  |     76 | {\isachardoublequoteopen}itrev\ {\isacharbrackleft}{\isacharbrackright}\ \ \ \ \ ys\ {\isacharequal}\ ys{\isachardoublequoteclose}\ {\isacharbar}\isanewline
 | 
| 17175 |     77 | {\isachardoublequoteopen}itrev\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ ys\ {\isacharequal}\ itrev\ xs\ {\isacharparenleft}x{\isacharhash}ys{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 8749 |     78 | \begin{isamarkuptext}%
 | 
| 9792 |     79 | \noindent
 | 
| 11458 |     80 | The behaviour of \cdx{itrev} is simple: it reverses
 | 
| 9493 |     81 | its first argument by stacking its elements onto the second argument,
 | 
|  |     82 | and returning that second argument when the first one becomes
 | 
| 17327 |     83 | empty. Note that \isa{itrev} is tail-recursive: it can be
 | 
| 9493 |     84 | compiled into a loop.
 | 
|  |     85 | 
 | 
| 17327 |     86 | Naturally, we would like to show that \isa{itrev} does indeed reverse
 | 
| 9493 |     87 | its first argument provided the second one is empty:%
 | 
| 8749 |     88 | \end{isamarkuptext}%
 | 
| 17175 |     89 | \isamarkuptrue%
 | 
|  |     90 | \isacommand{lemma}\isamarkupfalse%
 | 
|  |     91 | \ {\isachardoublequoteopen}itrev\ xs\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ rev\ xs{\isachardoublequoteclose}%
 | 
| 17056 |     92 | \isadelimproof
 | 
|  |     93 | %
 | 
|  |     94 | \endisadelimproof
 | 
|  |     95 | %
 | 
|  |     96 | \isatagproof
 | 
| 16069 |     97 | %
 | 
|  |     98 | \begin{isamarkuptxt}%
 | 
|  |     99 | \noindent
 | 
|  |    100 | There is no choice as to the induction variable, and we immediately simplify:%
 | 
|  |    101 | \end{isamarkuptxt}%
 | 
| 17175 |    102 | \isamarkuptrue%
 | 
|  |    103 | \isacommand{apply}\isamarkupfalse%
 | 
|  |    104 | {\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
 | 
| 16069 |    105 | \begin{isamarkuptxt}%
 | 
|  |    106 | \noindent
 | 
|  |    107 | Unfortunately, this attempt does not prove
 | 
|  |    108 | the induction step:
 | 
|  |    109 | \begin{isabelle}%
 | 
|  |    110 | \ {\isadigit{1}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
 | 
| 17327 |    111 | \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 |    112 | \end{isabelle}
 | 
|  |    113 | The induction hypothesis is too weak.  The fixed
 | 
|  |    114 | argument,~\isa{{\isacharbrackleft}{\isacharbrackright}}, prevents it from rewriting the conclusion.  
 | 
|  |    115 | This example suggests a heuristic:
 | 
|  |    116 | \begin{quote}\index{generalizing induction formulae}%
 | 
|  |    117 | \emph{Generalize goals for induction by replacing constants by variables.}
 | 
|  |    118 | \end{quote}
 | 
| 17327 |    119 | Of course one cannot do this na\"{\i}vely: \isa{itrev\ xs\ ys\ {\isacharequal}\ rev\ xs} is
 | 
| 16069 |    120 | just not true.  The correct generalization is%
 | 
|  |    121 | \end{isamarkuptxt}%
 | 
| 17175 |    122 | \isamarkuptrue%
 | 
| 17056 |    123 | %
 | 
|  |    124 | \endisatagproof
 | 
|  |    125 | {\isafoldproof}%
 | 
|  |    126 | %
 | 
|  |    127 | \isadelimproof
 | 
|  |    128 | %
 | 
|  |    129 | \endisadelimproof
 | 
| 17175 |    130 | \isacommand{lemma}\isamarkupfalse%
 | 
|  |    131 | \ {\isachardoublequoteopen}itrev\ xs\ ys\ {\isacharequal}\ rev\ xs\ {\isacharat}\ ys{\isachardoublequoteclose}%
 | 
| 17056 |    132 | \isadelimproof
 | 
|  |    133 | %
 | 
|  |    134 | \endisadelimproof
 | 
|  |    135 | %
 | 
|  |    136 | \isatagproof
 | 
| 16069 |    137 | %
 | 
|  |    138 | \begin{isamarkuptxt}%
 | 
|  |    139 | \noindent
 | 
|  |    140 | If \isa{ys} is replaced by \isa{{\isacharbrackleft}{\isacharbrackright}}, the right-hand side simplifies to
 | 
|  |    141 | \isa{rev\ xs}, as required.
 | 
|  |    142 | 
 | 
|  |    143 | In this instance it was easy to guess the right generalization.
 | 
|  |    144 | Other situations can require a good deal of creativity.  
 | 
|  |    145 | 
 | 
|  |    146 | Although we now have two variables, only \isa{xs} is suitable for
 | 
|  |    147 | induction, and we repeat our proof attempt. Unfortunately, we are still
 | 
|  |    148 | not there:
 | 
|  |    149 | \begin{isabelle}%
 | 
|  |    150 | \ {\isadigit{1}}{\isachardot}\ {\isasymAnd}a\ list{\isachardot}\isanewline
 | 
| 17327 |    151 | \isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }itrev\ list\ ys\ {\isacharequal}\ rev\ list\ {\isacharat}\ ys\ {\isasymLongrightarrow}\isanewline
 | 
|  |    152 | \isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }itrev\ list\ {\isacharparenleft}a\ {\isacharhash}\ ys{\isacharparenright}\ {\isacharequal}\ rev\ list\ {\isacharat}\ a\ {\isacharhash}\ ys%
 | 
| 16069 |    153 | \end{isabelle}
 | 
|  |    154 | The induction hypothesis is still too weak, but this time it takes no
 | 
|  |    155 | intuition to generalize: the problem is that \isa{ys} is fixed throughout
 | 
|  |    156 | the subgoal, but the induction hypothesis needs to be applied with
 | 
|  |    157 | \isa{a\ {\isacharhash}\ ys} instead of \isa{ys}. Hence we prove the theorem
 | 
|  |    158 | for all \isa{ys} instead of a fixed one:%
 | 
|  |    159 | \end{isamarkuptxt}%
 | 
| 17175 |    160 | \isamarkuptrue%
 | 
| 17056 |    161 | %
 | 
|  |    162 | \endisatagproof
 | 
|  |    163 | {\isafoldproof}%
 | 
|  |    164 | %
 | 
|  |    165 | \isadelimproof
 | 
|  |    166 | %
 | 
|  |    167 | \endisadelimproof
 | 
| 17175 |    168 | \isacommand{lemma}\isamarkupfalse%
 | 
|  |    169 | \ {\isachardoublequoteopen}{\isasymforall}ys{\isachardot}\ itrev\ xs\ ys\ {\isacharequal}\ rev\ xs\ {\isacharat}\ ys{\isachardoublequoteclose}%
 | 
| 17056 |    170 | \isadelimproof
 | 
|  |    171 | %
 | 
|  |    172 | \endisadelimproof
 | 
|  |    173 | %
 | 
|  |    174 | \isatagproof
 | 
|  |    175 | %
 | 
|  |    176 | \endisatagproof
 | 
|  |    177 | {\isafoldproof}%
 | 
|  |    178 | %
 | 
|  |    179 | \isadelimproof
 | 
|  |    180 | %
 | 
|  |    181 | \endisadelimproof
 | 
| 11866 |    182 | %
 | 
| 9844 |    183 | \begin{isamarkuptext}%
 | 
| 8749 |    184 | \noindent
 | 
|  |    185 | This time induction on \isa{xs} followed by simplification succeeds. This
 | 
|  |    186 | leads to another heuristic for generalization:
 | 
|  |    187 | \begin{quote}
 | 
| 9754 |    188 | \emph{Generalize goals for induction by universally quantifying all free
 | 
| 8749 |    189 | variables {\em(except the induction variable itself!)}.}
 | 
|  |    190 | \end{quote}
 | 
| 11458 |    191 | This prevents trivial failures like the one above and does not affect the
 | 
|  |    192 | validity of the goal.  However, this heuristic should not be applied blindly.
 | 
|  |    193 | It is not always required, and the additional quantifiers can complicate
 | 
| 13081 |    194 | matters in some cases. The variables that should be quantified are typically
 | 
| 11458 |    195 | those that change in recursive calls.
 | 
| 9644 |    196 | 
 | 
| 9844 |    197 | A final point worth mentioning is the orientation of the equation we just
 | 
| 17327 |    198 | proved: the more complex notion (\isa{itrev}) is on the left-hand
 | 
| 9844 |    199 | side, the simpler one (\isa{rev}) on the right-hand side. This constitutes
 | 
|  |    200 | another, albeit weak heuristic that is not restricted to induction:
 | 
|  |    201 | \begin{quote}
 | 
|  |    202 |   \emph{The right-hand side of an equation should (in some sense) be simpler
 | 
|  |    203 |     than the left-hand side.}
 | 
|  |    204 | \end{quote}
 | 
|  |    205 | This heuristic is tricky to apply because it is not obvious that
 | 
| 17327 |    206 | \isa{rev\ xs\ {\isacharat}\ ys} is simpler than \isa{itrev\ xs\ ys}. But see what
 | 
|  |    207 | happens if you try to prove \isa{rev\ xs\ {\isacharat}\ ys\ {\isacharequal}\ itrev\ xs\ ys}!
 | 
| 10971 |    208 | 
 | 
| 11458 |    209 | If you have tried these heuristics and still find your
 | 
| 10971 |    210 | induction does not go through, and no obvious lemma suggests itself, you may
 | 
|  |    211 | need to generalize your proposition even further. This requires insight into
 | 
| 11458 |    212 | the problem at hand and is beyond simple rules of thumb.  
 | 
|  |    213 | Additionally, you can read \S\ref{sec:advanced-ind}
 | 
| 10971 |    214 | to learn about some advanced techniques for inductive proofs.%
 | 
| 11458 |    215 | \index{induction heuristics|)}%
 | 
| 9844 |    216 | \end{isamarkuptext}%
 | 
| 17175 |    217 | \isamarkuptrue%
 | 
| 17056 |    218 | %
 | 
| 17327 |    219 | \isadelimML
 | 
|  |    220 | %
 | 
|  |    221 | \endisadelimML
 | 
|  |    222 | %
 | 
|  |    223 | \isatagML
 | 
|  |    224 | %
 | 
|  |    225 | \endisatagML
 | 
|  |    226 | {\isafoldML}%
 | 
|  |    227 | %
 | 
|  |    228 | \isadelimML
 | 
|  |    229 | %
 | 
|  |    230 | \endisadelimML
 | 
|  |    231 | %
 | 
| 17056 |    232 | \isadelimtheory
 | 
|  |    233 | %
 | 
|  |    234 | \endisadelimtheory
 | 
|  |    235 | %
 | 
|  |    236 | \isatagtheory
 | 
|  |    237 | %
 | 
|  |    238 | \endisatagtheory
 | 
|  |    239 | {\isafoldtheory}%
 | 
|  |    240 | %
 | 
|  |    241 | \isadelimtheory
 | 
|  |    242 | %
 | 
|  |    243 | \endisadelimtheory
 | 
| 9722 |    244 | \end{isabellebody}%
 | 
| 9145 |    245 | %%% Local Variables:
 | 
|  |    246 | %%% mode: latex
 | 
|  |    247 | %%% TeX-master: "root"
 | 
|  |    248 | %%% End:
 |