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