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