doc-src/TutorialI/Advanced/document/WFrec.tex
author haftmann
Mon, 02 May 2005 11:03:27 +0200
changeset 15905 0a4cc9b113c7
parent 15481 fc075ae929e4
child 16069 3f2a9f400168
permissions -rw-r--r--
introduced @{const ...} antiquotation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     1
%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     2
\begin{isabellebody}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     3
\def\isabellecontext{WFrec}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
     4
\isamarkupfalse%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     5
%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     6
\begin{isamarkuptext}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     7
\noindent
11161
166f7d87b37f *** empty log message ***
nipkow
parents: 10878
diff changeset
     8
So far, all recursive definitions were shown to terminate via measure
11494
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
     9
functions. Sometimes this can be inconvenient or
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    10
impossible. Fortunately, \isacommand{recdef} supports much more
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    11
general definitions. For example, termination of Ackermann's function
10654
458068404143 *** empty log message ***
nipkow
parents: 10577
diff changeset
    12
can be shown by means of the \rmindex{lexicographic product} \isa{{\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}}:%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    13
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    14
\isamarkuptrue%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    15
\isacommand{consts}\ ack\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    16
\isamarkupfalse%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    17
\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
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    18
\ \ {\isachardoublequote}ack{\isacharparenleft}{\isadigit{0}}{\isacharcomma}n{\isacharparenright}\ \ \ \ \ \ \ \ \ {\isacharequal}\ Suc\ n{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    19
\ \ {\isachardoublequote}ack{\isacharparenleft}Suc\ m{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    20
\ \ {\isachardoublequote}ack{\isacharparenleft}Suc\ m{\isacharcomma}Suc\ n{\isacharparenright}\ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}ack{\isacharparenleft}Suc\ m{\isacharcomma}n{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    21
%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    22
\begin{isamarkuptext}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    23
\noindent
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    24
The lexicographic product decreases if either its first component
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    25
decreases (as in the second equation and in the outer call in the
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    26
third equation) or its first component stays the same and the second
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    27
component decreases (as in the inner call in the third equation).
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    28
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    29
In general, \isacommand{recdef} supports termination proofs based on
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    30
arbitrary well-founded relations as introduced in \S\ref{sec:Well-founded}.
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    31
This is called \textbf{well-founded
11494
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    32
recursion}\indexbold{recursion!well-founded}.  A function definition
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    33
is total if and only if the set of 
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    34
all pairs $(r,l)$, where $l$ is the argument on the
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    35
left-hand side of an equation and $r$ the argument of some recursive call on
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    36
the corresponding right-hand side, induces a well-founded relation.  For a
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    37
systematic account of termination proofs via well-founded relations see, for
10878
b254d5ad6dd4 auto update
paulson
parents: 10842
diff changeset
    38
example, Baader and Nipkow~\cite{Baader-Nipkow}.
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    39
11494
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    40
Each \isacommand{recdef} definition should be accompanied (after the function's
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    41
name) by a well-founded relation on the function's argument type.  
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    42
Isabelle/HOL formalizes some of the most important
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    43
constructions of well-founded relations (see \S\ref{sec:Well-founded}). For
11494
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    44
example, \isa{measure\ f} is always well-founded.   The lexicographic
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    45
product of two well-founded relations is again well-founded, which we relied
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    46
on when defining Ackermann's function above.
11308
b28bbb153603 *** empty log message ***
nipkow
parents: 11196
diff changeset
    47
Of course the lexicographic product can also be iterated:%
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    48
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    49
\isamarkuptrue%
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    50
\isacommand{consts}\ contrived\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymtimes}\ nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    51
\isamarkupfalse%
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    52
\isacommand{recdef}\ contrived\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    53
\ \ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}i{\isachardot}\ i{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}j{\isachardot}\ j{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}k{\isachardot}\ k{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    54
{\isachardoublequote}contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}Suc\ k{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}k{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    55
{\isachardoublequote}contrived{\isacharparenleft}i{\isacharcomma}Suc\ j{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}j{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    56
{\isachardoublequote}contrived{\isacharparenleft}Suc\ i{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}i{\isacharcomma}i{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    57
{\isachardoublequote}contrived{\isacharparenleft}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ {\isadigit{0}}{\isachardoublequote}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    58
%
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    59
\begin{isamarkuptext}%
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    60
Lexicographic products of measure functions already go a long
10878
b254d5ad6dd4 auto update
paulson
parents: 10842
diff changeset
    61
way. Furthermore, you may embed a type in an
10396
5ab08609e6c8 *** empty log message ***
nipkow
parents: 10241
diff changeset
    62
existing well-founded relation via the inverse image construction \isa{inv{\isacharunderscore}image}. All these constructions are known to \isacommand{recdef}. Thus you
10241
e0428c2778f1 wellfounded -> well-founded
paulson
parents: 10190
diff changeset
    63
will never have to prove well-foundedness of any relation composed
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    64
solely of these building blocks. But of course the proof of
11494
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    65
termination of your function definition --- that the arguments
23a118849801 revisions and indexing
paulson
parents: 11429
diff changeset
    66
decrease with every recursive call --- may still require you to provide
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    67
additional lemmas.
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    68
10842
4649e5e3905d auto update
paulson
parents: 10795
diff changeset
    69
It is also possible to use your own well-founded relations with
4649e5e3905d auto update
paulson
parents: 10795
diff changeset
    70
\isacommand{recdef}.  For example, the greater-than relation can be made
4649e5e3905d auto update
paulson
parents: 10795
diff changeset
    71
well-founded by cutting it off at a certain point.  Here is an example
4649e5e3905d auto update
paulson
parents: 10795
diff changeset
    72
of a recursive function that calls itself with increasing values up to ten:%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    73
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    74
\isamarkuptrue%
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    75
\isacommand{consts}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    76
\isamarkupfalse%
11706
885e053ae664 *** empty log message ***
wenzelm
parents: 11636
diff changeset
    77
\isacommand{recdef}\ f\ {\isachardoublequote}{\isacharbraceleft}{\isacharparenleft}i{\isacharcomma}j{\isacharparenright}{\isachardot}\ j{\isacharless}i\ {\isasymand}\ i\ {\isasymle}\ {\isacharparenleft}{\isadigit{1}}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharbraceright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    78
{\isachardoublequote}f\ i\ {\isacharequal}\ {\isacharparenleft}if\ {\isadigit{1}}{\isadigit{0}}\ {\isasymle}\ i\ then\ {\isadigit{0}}\ else\ i\ {\isacharasterisk}\ f{\isacharparenleft}Suc\ i{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    79
%
11636
wenzelm
parents: 11627
diff changeset
    80
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
    81
\noindent
wenzelm
parents: 11627
diff changeset
    82
Since \isacommand{recdef} is not prepared for the relation supplied above,
wenzelm
parents: 11627
diff changeset
    83
Isabelle rejects the definition.  We should first have proved that
wenzelm
parents: 11627
diff changeset
    84
our relation was well-founded:%
wenzelm
parents: 11627
diff changeset
    85
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    86
\isamarkuptrue%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    87
\isacommand{lemma}\ wf{\isacharunderscore}greater{\isacharcolon}\ {\isachardoublequote}wf\ {\isacharbraceleft}{\isacharparenleft}i{\isacharcomma}j{\isacharparenright}{\isachardot}\ j{\isacharless}i\ {\isasymand}\ i\ {\isasymle}\ {\isacharparenleft}N{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharbraceright}{\isachardoublequote}\isamarkupfalse%
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents: 13778
diff changeset
    88
\isamarkuptrue%
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents: 13778
diff changeset
    89
\isamarkupfalse%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    90
\isamarkuptrue%
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents: 13778
diff changeset
    91
\isamarkupfalse%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    92
\isamarkuptrue%
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents: 13778
diff changeset
    93
\isamarkupfalse%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    94
%
11636
wenzelm
parents: 11627
diff changeset
    95
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
    96
\noindent
wenzelm
parents: 11627
diff changeset
    97
wenzelm
parents: 11627
diff changeset
    98
Armed with this lemma, we use the \attrdx{recdef_wf} attribute to attach a
13111
2d6782e71702 *** empty log message ***
nipkow
parents: 11866
diff changeset
    99
crucial hint\cmmdx{hints} to our definition:%
11636
wenzelm
parents: 11627
diff changeset
   100
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   101
\isamarkuptrue%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   102
\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   103
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}wf{\isacharcolon}\ wf{\isacharunderscore}greater{\isacharparenright}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   104
%
11636
wenzelm
parents: 11627
diff changeset
   105
\begin{isamarkuptext}%
wenzelm
parents: 11627
diff changeset
   106
\noindent
11706
885e053ae664 *** empty log message ***
wenzelm
parents: 11636
diff changeset
   107
Alternatively, we could have given \isa{measure\ {\isacharparenleft}{\isasymlambda}k{\isacharcolon}{\isacharcolon}nat{\isachardot}\ {\isadigit{1}}{\isadigit{0}}{\isacharminus}k{\isacharparenright}} for the
11636
wenzelm
parents: 11627
diff changeset
   108
well-founded relation in our \isacommand{recdef}.  However, the arithmetic
wenzelm
parents: 11627
diff changeset
   109
goal in the lemma above would have arisen instead in the \isacommand{recdef}
wenzelm
parents: 11627
diff changeset
   110
termination proof, where we have less control.  A tailor-made termination
wenzelm
parents: 11627
diff changeset
   111
relation makes even more sense when it can be used in several function
wenzelm
parents: 11627
diff changeset
   112
declarations.%
wenzelm
parents: 11627
diff changeset
   113
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   114
\isamarkuptrue%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   115
\isamarkupfalse%
11636
wenzelm
parents: 11627
diff changeset
   116
\end{isabellebody}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   117
%%% Local Variables:
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   118
%%% mode: latex
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   119
%%% TeX-master: "root"
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   120
%%% End: