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