doc-src/TutorialI/Recdef/document/termination.tex
author wenzelm
Mon, 11 Sep 2000 17:59:53 +0200
changeset 9923 fe13743ffc8b
parent 9792 bbefb6ce5cb2
child 9924 3370f6aa3200
permissions -rw-r--r--
renamed "rulify" to "rulified"; renamed "less_induct" to "nat_less_induct";
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}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     3
%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     4
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     5
When a function is defined via \isacommand{recdef}, Isabelle tries to prove
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     6
its termination with the help of the user-supplied measure.  All of the above
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     7
examples are simple enough that Isabelle can prove automatically that the
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8749
diff changeset
     8
measure of the argument goes down in each recursive call. As a result,
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
     9
$f$\isa{{\isachardot}simps} will contain the defining equations (or variants derived
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    10
from them) as theorems. For example, look (via \isacommand{thm}) at
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    11
\isa{sep{\isachardot}simps} and \isa{sep\isadigit{1}{\isachardot}simps} to see that they define
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    12
the same function. What is more, those equations are automatically declared as
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    13
simplification rules.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    14
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    15
In general, Isabelle may not be able to prove all termination condition
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    16
(there is one for each recursive call) automatically. For example,
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    17
termination of the following artificial function%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    18
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    19
\isacommand{consts}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    20
\isacommand{recdef}\ f\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharminus}y{\isacharparenright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    21
\ \ {\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}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    22
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    23
\noindent
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    24
is not proved automatically (although maybe it should be). Isabelle prints a
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    25
kind of error message showing you what it was unable to prove. You will then
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    26
have to prove it as a separate lemma before you attempt the definition
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    27
of your function once more. In our case the required lemma is the obvious one:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    28
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    29
\isacommand{lemma}\ termi{\isacharunderscore}lem{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}{\isasymnot}\ x\ {\isasymle}\ y\ {\isasymLongrightarrow}\ x\ {\isacharminus}\ Suc\ y\ {\isacharless}\ x\ {\isacharminus}\ y{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    30
\begin{isamarkuptxt}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    31
\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    32
It was not proved automatically because of the special nature of \isa{{\isacharminus}}
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    33
on \isa{nat}. This requires more arithmetic than is tried by default:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    34
\end{isamarkuptxt}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    35
\isacommand{by}{\isacharparenleft}arith{\isacharparenright}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    36
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    37
\noindent
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8749
diff changeset
    38
Because \isacommand{recdef}'s termination prover involves simplification,
026f37a86ea7 *** empty log message ***
nipkow
parents: 8749
diff changeset
    39
we have turned our lemma into a simplification rule. Therefore our second
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    40
attempt to define our function will automatically take it into account:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    41
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    42
\isacommand{consts}\ g\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    43
\isacommand{recdef}\ g\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharminus}y{\isacharparenright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    44
\ \ {\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}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    45
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    46
\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    47
This time everything works fine. Now \isa{g{\isachardot}simps} contains precisely
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    48
the stated recursion equation for \isa{g} and they are simplification
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    49
rules. Thus we can automatically prove%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    50
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    51
\isacommand{theorem}\ wow{\isacharcolon}\ {\isachardoublequote}g{\isacharparenleft}\isadigit{1}{\isacharcomma}\isadigit{0}{\isacharparenright}\ {\isacharequal}\ g{\isacharparenleft}\isadigit{1}{\isacharcomma}\isadigit{1}{\isacharparenright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    52
\isacommand{by}{\isacharparenleft}simp{\isacharparenright}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    53
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    54
\noindent
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    55
More exciting theorems require induction, which is discussed below.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    56
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    57
Because lemma \isa{termi{\isacharunderscore}lem} above was only turned into a
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    58
simplification rule for the sake of the termination proof, we may want to
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    59
disable it again:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    60
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    61
\isacommand{lemmas}\ {\isacharbrackleft}simp\ del{\isacharbrackright}\ {\isacharequal}\ termi{\isacharunderscore}lem%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    62
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    63
The attentive reader may wonder why we chose to call our function \isa{g}
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    64
rather than \isa{f} the second time around. The reason is that, despite
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    65
the failed termination proof, the definition of \isa{f} did not
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    66
fail, and thus we could not define it a second time. However, all theorems
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    67
about \isa{f}, for example \isa{f{\isachardot}simps}, carry as a precondition
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    68
the unproved termination condition. Moreover, the theorems
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    69
\isa{f{\isachardot}simps} are not simplification rules. However, this mechanism
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    70
allows a delayed proof of termination: instead of proving
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    71
\isa{termi{\isacharunderscore}lem} up front, we could prove 
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    72
it later on and then use it to remove the preconditions from the theorems
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    73
about \isa{f}. In most cases this is more cumbersome than proving things
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    74
up front.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    75
%FIXME, with one exception: nested recursion.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    76
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    77
Although all the above examples employ measure functions, \isacommand{recdef}
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    78
allows arbitrary wellfounded relations. For example, termination of
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    79
Ackermann's function requires the lexicographic product \isa{{\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}}:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    80
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    81
\isacommand{consts}\ ack\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    82
\isacommand{recdef}\ ack\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}m{\isachardot}\ m{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequote}\isanewline
9674
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    83
\ \ {\isachardoublequote}ack{\isacharparenleft}\isadigit{0}{\isacharcomma}n{\isacharparenright}\ \ \ \ \ \ \ \ \ {\isacharequal}\ Suc\ n{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    84
\ \ {\isachardoublequote}ack{\isacharparenleft}Suc\ m{\isacharcomma}\isadigit{0}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}\ \isadigit{1}{\isacharparenright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9541
diff changeset
    85
\ \ {\isachardoublequote}ack{\isacharparenleft}Suc\ m{\isacharcomma}Suc\ n{\isacharparenright}\ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}ack{\isacharparenleft}Suc\ m{\isacharcomma}n{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    86
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    87
\noindent
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    88
For details see the manual~\cite{isabelle-HOL} and the examples in the
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    89
library.%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    90
\end{isamarkuptext}%
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
    91
\end{isabellebody}%
9145
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    92
%%% Local Variables:
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    93
%%% mode: latex
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    94
%%% TeX-master: "root"
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    95
%%% End: