src/Doc/Tutorial/Misc/Itrev.thy
author wenzelm
Sat, 05 Jan 2019 17:24:33 +0100
changeset 69597 ff784d5a5bfb
parent 69505 cc2d676d5395
permissions -rw-r--r--
isabelle update -u control_cartouches;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     1
(*<*)
17326
9fe23a5bb021 dealt with name clash with List.itrev
nipkow
parents: 16417
diff changeset
     2
theory Itrev
9fe23a5bb021 dealt with name clash with List.itrev
nipkow
parents: 16417
diff changeset
     3
imports Main
9fe23a5bb021 dealt with name clash with List.itrev
nipkow
parents: 16417
diff changeset
     4
begin
42669
04dfffda5671 more conventional naming scheme: names_long, names_short, names_unique;
wenzelm
parents: 42358
diff changeset
     5
declare [[names_unique = false]]
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     6
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     7
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
     8
section\<open>Induction Heuristics\<close>
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
     9
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    10
text\<open>\label{sec:InductionHeuristics}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    11
\index{induction heuristics|(}%
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    12
The purpose of this section is to illustrate some simple heuristics for
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    13
inductive proofs. The first one we have already mentioned in our initial
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    14
example:
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    15
\begin{quote}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    16
\emph{Theorems about recursive functions are proved by induction.}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    17
\end{quote}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    18
In case the function has more than one argument
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    19
\begin{quote}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    20
\emph{Do induction on argument number $i$ if the function is defined by
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    21
recursion in argument number $i$.}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    22
\end{quote}
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    23
When we look at the proof of \<open>(xs@ys) @ zs = xs @ (ys@zs)\<close>
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    24
in \S\ref{sec:intro-proof} we find
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    25
\begin{itemize}
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    26
\item \<open>@\<close> is recursive in
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    27
the first argument
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    28
\item \<^term>\<open>xs\<close>  occurs only as the first argument of
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    29
\<open>@\<close>
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    30
\item both \<^term>\<open>ys\<close> and \<^term>\<open>zs\<close> occur at least once as
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    31
the second argument of \<open>@\<close>
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    32
\end{itemize}
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    33
Hence it is natural to perform induction on~\<^term>\<open>xs\<close>.
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    34
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    35
The key heuristic, and the main point of this section, is to
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    36
\emph{generalize the goal before induction}.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    37
The reason is simple: if the goal is
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    38
too specific, the induction hypothesis is too weak to allow the induction
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10885
diff changeset
    39
step to go through. Let us illustrate the idea with an example.
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
    40
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    41
Function \cdx{rev} has quadratic worst-case running time
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    42
because it calls function \<open>@\<close> for each element of the list and
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    43
\<open>@\<close> is linear in its first argument.  A linear time version of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    44
\<^term>\<open>rev\<close> reqires an extra argument where the result is accumulated
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    45
gradually, using only~\<open>#\<close>:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    46
\<close>
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    47
27015
f8537d69f514 *** empty log message ***
nipkow
parents: 17326
diff changeset
    48
primrec itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
f8537d69f514 *** empty log message ***
nipkow
parents: 17326
diff changeset
    49
"itrev []     ys = ys" |
f8537d69f514 *** empty log message ***
nipkow
parents: 17326
diff changeset
    50
"itrev (x#xs) ys = itrev xs (x#ys)"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    51
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    52
text\<open>\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    53
The behaviour of \cdx{itrev} is simple: it reverses
9493
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
    54
its first argument by stacking its elements onto the second argument,
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
    55
and returning that second argument when the first one becomes
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    56
empty. Note that \<^term>\<open>itrev\<close> is tail-recursive: it can be
9493
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
    57
compiled into a loop.
494f8cd34df7 *** empty log message ***
nipkow
parents: 9458
diff changeset
    58
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    59
Naturally, we would like to show that \<^term>\<open>itrev\<close> does indeed reverse
9754
a123a64cadeb *** empty log message ***
nipkow
parents: 9723
diff changeset
    60
its first argument provided the second one is empty:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    61
\<close>
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    62
58860
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
    63
lemma "itrev xs [] = rev xs"
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    64
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    65
txt\<open>\noindent
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    66
There is no choice as to the induction variable, and we immediately simplify:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    67
\<close>
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    68
58860
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
    69
apply(induct_tac xs, simp_all)
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    70
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    71
txt\<open>\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    72
Unfortunately, this attempt does not prove
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    73
the induction step:
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10885
diff changeset
    74
@{subgoals[display,indent=0,margin=70]}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    75
The induction hypothesis is too weak.  The fixed
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    76
argument,~\<^term>\<open>[]\<close>, prevents it from rewriting the conclusion.  
11458
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.}
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    80
\end{quote}
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    81
Of course one cannot do this na\"{\i}vely: \<^term>\<open>itrev xs ys = rev xs\<close> is
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    82
just not true.  The correct generalization is
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    83
\<close>
58860
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
    84
(*<*)oops(*>*)
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
    85
lemma "itrev xs ys = rev xs @ ys"
10362
c6b197ccf1f1 *** empty log message ***
nipkow
parents: 9844
diff changeset
    86
(*<*)apply(induct_tac xs, simp_all)(*>*)
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
    87
txt\<open>\noindent
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    88
If \<^term>\<open>ys\<close> is replaced by \<^term>\<open>[]\<close>, the right-hand side simplifies to
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    89
\<^term>\<open>rev xs\<close>, as required.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    90
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    91
In this instance it was easy to guess the right generalization.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    92
Other situations can require a good deal of creativity.  
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    93
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    94
Although we now have two variables, only \<^term>\<open>xs\<close> is suitable for
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
    95
induction, and we repeat our proof attempt. Unfortunately, we are still
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    96
not there:
10362
c6b197ccf1f1 *** empty log message ***
nipkow
parents: 9844
diff changeset
    97
@{subgoals[display,indent=0,goals_limit=1]}
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    98
The induction hypothesis is still too weak, but this time it takes no
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    99
intuition to generalize: the problem is that \<^term>\<open>ys\<close> is fixed throughout
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   100
the subgoal, but the induction hypothesis needs to be applied with
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   101
\<^term>\<open>a # ys\<close> instead of \<^term>\<open>ys\<close>. Hence we prove the theorem
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   102
for all \<^term>\<open>ys\<close> instead of a fixed one:
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
   103
\<close>
58860
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
   104
(*<*)oops(*>*)
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
   105
lemma "\<forall>ys. itrev xs ys = rev xs @ ys"
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   106
(*<*)
58860
fee7cfa69c50 eliminated spurious semicolons;
wenzelm
parents: 48985
diff changeset
   107
by(induct_tac xs, simp_all)
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   108
(*>*)
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   109
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
   110
text\<open>\noindent
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   111
This time induction on \<^term>\<open>xs\<close> followed by simplification succeeds. This
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   112
leads to another heuristic for generalization:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   113
\begin{quote}
9754
a123a64cadeb *** empty log message ***
nipkow
parents: 9723
diff changeset
   114
\emph{Generalize goals for induction by universally quantifying all free
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   115
variables {\em(except the induction variable itself!)}.}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   116
\end{quote}
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   117
This prevents trivial failures like the one above and does not affect the
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   118
validity of the goal.  However, this heuristic should not be applied blindly.
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   119
It is not always required, and the additional quantifiers can complicate
13081
ab4a3aef3591 *** empty log message ***
nipkow
parents: 11458
diff changeset
   120
matters in some cases. The variables that should be quantified are typically
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   121
those that change in recursive calls.
9644
6b0b6b471855 *** empty log message ***
nipkow
parents: 9541
diff changeset
   122
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   123
A final point worth mentioning is the orientation of the equation we just
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   124
proved: the more complex notion (\<^const>\<open>itrev\<close>) is on the left-hand
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   125
side, the simpler one (\<^term>\<open>rev\<close>) on the right-hand side. This constitutes
9844
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   126
another, albeit weak heuristic that is not restricted to induction:
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   127
\begin{quote}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   128
  \emph{The right-hand side of an equation should (in some sense) be simpler
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   129
    than the left-hand side.}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   130
\end{quote}
8016321c7de1 *** empty log message ***
nipkow
parents: 9792
diff changeset
   131
This heuristic is tricky to apply because it is not obvious that
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   132
\<^term>\<open>rev xs @ ys\<close> is simpler than \<^term>\<open>itrev xs ys\<close>. But see what
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
   133
happens if you try to prove \<^prop>\<open>rev xs @ ys = itrev xs ys\<close>!
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10885
diff changeset
   134
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   135
If you have tried these heuristics and still find your
10971
6852682eaf16 *** empty log message ***
nipkow
parents: 10885
diff changeset
   136
induction does not go through, and no obvious lemma suggests itself, you may
6852682eaf16 *** empty log message ***
nipkow
parents: 10885
diff changeset
   137
need to generalize your proposition even further. This requires insight into
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   138
the problem at hand and is beyond simple rules of thumb.  
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   139
Additionally, you can read \S\ref{sec:advanced-ind}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   140
to learn about some advanced techniques for inductive proofs.%
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 10971
diff changeset
   141
\index{induction heuristics|)}
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58860
diff changeset
   142
\<close>
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   143
(*<*)
42669
04dfffda5671 more conventional naming scheme: names_long, names_short, names_unique;
wenzelm
parents: 42358
diff changeset
   144
declare [[names_unique = true]]
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   145
end
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   146
(*>*)