doc-src/TutorialI/Recdef/document/termination.tex
author wenzelm
Sat, 27 Oct 2001 23:13:42 +0200
changeset 11964 828ea309dc21
parent 11866 fbd097aec213
child 12332 aea72a834c85
permissions -rw-r--r--
updated;
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: 9792
diff changeset
     3
\def\isabellecontext{termination}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
     4
\isamarkupfalse%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     5
%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     6
\begin{isamarkuptext}%
11309
d666f11ca2d4 minor suggestions by Tanja Vos
paulson
parents: 10971
diff changeset
     7
When a function~$f$ is defined via \isacommand{recdef}, Isabelle tries to prove
d666f11ca2d4 minor suggestions by Tanja Vos
paulson
parents: 10971
diff changeset
     8
its termination with the help of the user-supplied measure.  Each of the examples
d666f11ca2d4 minor suggestions by Tanja Vos
paulson
parents: 10971
diff changeset
     9
above is simple enough that Isabelle can automatically prove that the
d666f11ca2d4 minor suggestions by Tanja Vos
paulson
parents: 10971
diff changeset
    10
argument's measure decreases in each recursive call. As a result,
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    11
$f$\isa{{\isachardot}simps} will contain the defining equations (or variants derived
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    12
from them) as theorems. For example, look (via \isacommand{thm}) at
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10186
diff changeset
    13
\isa{sep{\isachardot}simps} and \isa{sep{\isadigit{1}}{\isachardot}simps} to see that they define
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    14
the same function. What is more, those equations are automatically declared as
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    15
simplification rules.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    16
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11429
diff changeset
    17
Isabelle may fail to prove the termination condition for some
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11429
diff changeset
    18
recursive call.  Let us try the following artificial function:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    19
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    20
\isamarkuptrue%
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9924
diff changeset
    21
\isacommand{consts}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    22
\isamarkupfalse%
11636
wenzelm
parents: 11627
diff changeset
    23
\isacommand{recdef}\ f\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharminus}y{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    24
\ \ {\isachardoublequote}f{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ x\ {\isasymle}\ y\ then\ x\ else\ f{\isacharparenleft}x{\isacharcomma}y{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    25
%
11636
wenzelm
parents: 11627
diff changeset
    26
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
    27
\noindent
wenzelm
parents: 11627
diff changeset
    28
Isabelle prints a
wenzelm
parents: 11627
diff changeset
    29
\REMARK{error or warning?  change this part?  rename g to f?}
wenzelm
parents: 11627
diff changeset
    30
message showing you what it was unable to prove. You will then
wenzelm
parents: 11627
diff changeset
    31
have to prove it as a separate lemma before you attempt the definition
wenzelm
parents: 11627
diff changeset
    32
of your function once more. In our case the required lemma is the obvious one:%
wenzelm
parents: 11627
diff changeset
    33
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    34
\isamarkuptrue%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    35
\isacommand{lemma}\ termi{\isacharunderscore}lem{\isacharcolon}\ {\isachardoublequote}{\isasymnot}\ x\ {\isasymle}\ y\ {\isasymLongrightarrow}\ x\ {\isacharminus}\ Suc\ y\ {\isacharless}\ x\ {\isacharminus}\ y{\isachardoublequote}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    36
%
11636
wenzelm
parents: 11627
diff changeset
    37
\begin{isamarkuptxt}%
wenzelm
parents: 11627
diff changeset
    38
\noindent
wenzelm
parents: 11627
diff changeset
    39
It was not proved automatically because of the awkward behaviour of subtraction
wenzelm
parents: 11627
diff changeset
    40
on type \isa{nat}. This requires more arithmetic than is tried by default:%
wenzelm
parents: 11627
diff changeset
    41
\end{isamarkuptxt}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    42
\isamarkuptrue%
11636
wenzelm
parents: 11627
diff changeset
    43
\isacommand{apply}{\isacharparenleft}arith{\isacharparenright}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    44
\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    45
\isacommand{done}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    46
%
11636
wenzelm
parents: 11627
diff changeset
    47
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
    48
\noindent
wenzelm
parents: 11627
diff changeset
    49
Because \isacommand{recdef}'s termination prover involves simplification,
wenzelm
parents: 11627
diff changeset
    50
we include in our second attempt a hint: the \attrdx{recdef_simp} attribute 
wenzelm
parents: 11627
diff changeset
    51
says to use \isa{termi{\isacharunderscore}lem} as
wenzelm
parents: 11627
diff changeset
    52
a simplification rule.%
wenzelm
parents: 11627
diff changeset
    53
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    54
\isamarkuptrue%
11636
wenzelm
parents: 11627
diff changeset
    55
\isacommand{consts}\ g\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    56
\isamarkupfalse%
11636
wenzelm
parents: 11627
diff changeset
    57
\isacommand{recdef}\ g\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharminus}y{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm
parents: 11627
diff changeset
    58
\ \ {\isachardoublequote}g{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ x\ {\isasymle}\ y\ then\ x\ else\ g{\isacharparenleft}x{\isacharcomma}y{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    59
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}simp{\isacharcolon}\ termi{\isacharunderscore}lem{\isacharparenright}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    60
%
11636
wenzelm
parents: 11627
diff changeset
    61
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
    62
\noindent
wenzelm
parents: 11627
diff changeset
    63
This time everything works fine. Now \isa{g{\isachardot}simps} contains precisely
wenzelm
parents: 11627
diff changeset
    64
the stated recursion equation for \isa{g}, which has been stored as a
wenzelm
parents: 11627
diff changeset
    65
simplification rule.  Thus we can automatically prove results such as this one:%
wenzelm
parents: 11627
diff changeset
    66
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    67
\isamarkuptrue%
11636
wenzelm
parents: 11627
diff changeset
    68
\isacommand{theorem}\ {\isachardoublequote}g{\isacharparenleft}{\isadigit{1}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ g{\isacharparenleft}{\isadigit{1}}{\isacharcomma}{\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    69
\isamarkupfalse%
11636
wenzelm
parents: 11627
diff changeset
    70
\isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    71
\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    72
\isacommand{done}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    73
%
11636
wenzelm
parents: 11627
diff changeset
    74
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
    75
\noindent
wenzelm
parents: 11627
diff changeset
    76
More exciting theorems require induction, which is discussed below.
wenzelm
parents: 11627
diff changeset
    77
wenzelm
parents: 11627
diff changeset
    78
If the termination proof requires a new lemma that is of general use, you can
wenzelm
parents: 11627
diff changeset
    79
turn it permanently into a simplification rule, in which case the above
wenzelm
parents: 11627
diff changeset
    80
\isacommand{hint} is not necessary. But our \isa{termi{\isacharunderscore}lem} is not
wenzelm
parents: 11627
diff changeset
    81
sufficiently general to warrant this distinction.
wenzelm
parents: 11627
diff changeset
    82
wenzelm
parents: 11627
diff changeset
    83
The attentive reader may wonder why we chose to call our function \isa{g}
wenzelm
parents: 11627
diff changeset
    84
rather than \isa{f} the second time around. The reason is that, despite
wenzelm
parents: 11627
diff changeset
    85
the failed termination proof, the definition of \isa{f} did not
wenzelm
parents: 11627
diff changeset
    86
fail, and thus we could not define it a second time. However, all theorems
wenzelm
parents: 11627
diff changeset
    87
about \isa{f}, for example \isa{f{\isachardot}simps}, carry as a precondition
wenzelm
parents: 11627
diff changeset
    88
the unproved termination condition. Moreover, the theorems
wenzelm
parents: 11627
diff changeset
    89
\isa{f{\isachardot}simps} are not stored as simplification rules. 
wenzelm
parents: 11627
diff changeset
    90
However, this mechanism
wenzelm
parents: 11627
diff changeset
    91
allows a delayed proof of termination: instead of proving
wenzelm
parents: 11627
diff changeset
    92
\isa{termi{\isacharunderscore}lem} up front, we could prove 
wenzelm
parents: 11627
diff changeset
    93
it later on and then use it to remove the preconditions from the theorems
wenzelm
parents: 11627
diff changeset
    94
about \isa{f}. In most cases this is more cumbersome than proving things
wenzelm
parents: 11627
diff changeset
    95
up front.
wenzelm
parents: 11627
diff changeset
    96
\REMARK{FIXME, with one exception: nested recursion.}%
wenzelm
parents: 11627
diff changeset
    97
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    98
\isamarkuptrue%
fbd097aec213 updated;
wenzelm
parents: 11636
diff changeset
    99
\isamarkupfalse%
11636
wenzelm
parents: 11627
diff changeset
   100
\end{isabellebody}%
9145
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   101
%%% Local Variables:
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   102
%%% mode: latex
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   103
%%% TeX-master: "root"
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   104
%%% End: