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