| 
10187
 | 
     1  | 
%
  | 
| 
 | 
     2  | 
\begin{isabellebody}%
 | 
| 
 | 
     3  | 
\def\isabellecontext{WFrec}%
 | 
| 
17056
 | 
     4  | 
%
  | 
| 
 | 
     5  | 
\isadelimtheory
  | 
| 
 | 
     6  | 
%
  | 
| 
 | 
     7  | 
\endisadelimtheory
  | 
| 
 | 
     8  | 
%
  | 
| 
 | 
     9  | 
\isatagtheory
  | 
| 
17175
 | 
    10  | 
\isamarkupfalse%
  | 
| 
17056
 | 
    11  | 
%
  | 
| 
 | 
    12  | 
\endisatagtheory
  | 
| 
 | 
    13  | 
{\isafoldtheory}%
 | 
| 
 | 
    14  | 
%
  | 
| 
 | 
    15  | 
\isadelimtheory
  | 
| 
 | 
    16  | 
%
  | 
| 
 | 
    17  | 
\endisadelimtheory
  | 
| 
10187
 | 
    18  | 
%
  | 
| 
 | 
    19  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    20  | 
\noindent
  | 
| 
11161
 | 
    21  | 
So far, all recursive definitions were shown to terminate via measure
  | 
| 
11494
 | 
    22  | 
functions. Sometimes this can be inconvenient or
  | 
| 
10187
 | 
    23  | 
impossible. Fortunately, \isacommand{recdef} supports much more
 | 
| 
 | 
    24  | 
general definitions. For example, termination of Ackermann's function
  | 
| 
10654
 | 
    25  | 
can be shown by means of the \rmindex{lexicographic product} \isa{{\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}}:%
 | 
| 
10187
 | 
    26  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    27  | 
\isamarkuptrue%
  | 
| 
 | 
    28  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    29  | 
\ ack\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    30  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    31  | 
\ ack\ {\isachardoublequoteopen}measure{\isacharparenleft}{\isasymlambda}m{\isachardot}\ m{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    32  | 
\ \ {\isachardoublequoteopen}ack{\isacharparenleft}{\isadigit{0}}{\isacharcomma}n{\isacharparenright}\ \ \ \ \ \ \ \ \ {\isacharequal}\ Suc\ n{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    33  | 
\ \ {\isachardoublequoteopen}ack{\isacharparenleft}Suc\ m{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    34  | 
\ \ {\isachardoublequoteopen}ack{\isacharparenleft}Suc\ m{\isacharcomma}Suc\ n{\isacharparenright}\ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}ack{\isacharparenleft}Suc\ m{\isacharcomma}n{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
10187
 | 
    35  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    36  | 
\noindent
  | 
| 
 | 
    37  | 
The lexicographic product decreases if either its first component
  | 
| 
 | 
    38  | 
decreases (as in the second equation and in the outer call in the
  | 
| 
 | 
    39  | 
third equation) or its first component stays the same and the second
  | 
| 
 | 
    40  | 
component decreases (as in the inner call in the third equation).
  | 
| 
 | 
    41  | 
  | 
| 
 | 
    42  | 
In general, \isacommand{recdef} supports termination proofs based on
 | 
| 
10396
 | 
    43  | 
arbitrary well-founded relations as introduced in \S\ref{sec:Well-founded}.
 | 
| 
 | 
    44  | 
This is called \textbf{well-founded
 | 
| 
11494
 | 
    45  | 
recursion}\indexbold{recursion!well-founded}.  A function definition
 | 
| 
 | 
    46  | 
is total if and only if the set of 
  | 
| 
 | 
    47  | 
all pairs $(r,l)$, where $l$ is the argument on the
  | 
| 
10396
 | 
    48  | 
left-hand side of an equation and $r$ the argument of some recursive call on
  | 
| 
 | 
    49  | 
the corresponding right-hand side, induces a well-founded relation.  For a
  | 
| 
 | 
    50  | 
systematic account of termination proofs via well-founded relations see, for
  | 
| 
10878
 | 
    51  | 
example, Baader and Nipkow~\cite{Baader-Nipkow}.
 | 
| 
10187
 | 
    52  | 
  | 
| 
11494
 | 
    53  | 
Each \isacommand{recdef} definition should be accompanied (after the function's
 | 
| 
 | 
    54  | 
name) by a well-founded relation on the function's argument type.  
  | 
| 
 | 
    55  | 
Isabelle/HOL formalizes some of the most important
  | 
| 
10396
 | 
    56  | 
constructions of well-founded relations (see \S\ref{sec:Well-founded}). For
 | 
| 
11494
 | 
    57  | 
example, \isa{measure\ f} is always well-founded.   The lexicographic
 | 
| 
10396
 | 
    58  | 
product of two well-founded relations is again well-founded, which we relied
  | 
| 
 | 
    59  | 
on when defining Ackermann's function above.
  | 
| 
11308
 | 
    60  | 
Of course the lexicographic product can also be iterated:%
  | 
| 
10189
 | 
    61  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    62  | 
\isamarkuptrue%
  | 
| 
 | 
    63  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    64  | 
\ contrived\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    65  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    66  | 
\ contrived\isanewline
  | 
| 
 | 
    67  | 
\ \ {\isachardoublequoteopen}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}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    68  | 
{\isachardoublequoteopen}contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}Suc\ k{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}k{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    69  | 
{\isachardoublequoteopen}contrived{\isacharparenleft}i{\isacharcomma}Suc\ j{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}j{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    70  | 
{\isachardoublequoteopen}contrived{\isacharparenleft}Suc\ i{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}i{\isacharcomma}i{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    71  | 
{\isachardoublequoteopen}contrived{\isacharparenleft}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}%
 | 
| 
10189
 | 
    72  | 
\begin{isamarkuptext}%
 | 
| 
10396
 | 
    73  | 
Lexicographic products of measure functions already go a long
  | 
| 
10878
 | 
    74  | 
way. Furthermore, you may embed a type in an
  | 
| 
10396
 | 
    75  | 
existing well-founded relation via the inverse image construction \isa{inv{\isacharunderscore}image}. All these constructions are known to \isacommand{recdef}. Thus you
 | 
| 
10241
 | 
    76  | 
will never have to prove well-foundedness of any relation composed
  | 
| 
10189
 | 
    77  | 
solely of these building blocks. But of course the proof of
  | 
| 
11494
 | 
    78  | 
termination of your function definition --- that the arguments
  | 
| 
 | 
    79  | 
decrease with every recursive call --- may still require you to provide
  | 
| 
10189
 | 
    80  | 
additional lemmas.
  | 
| 
 | 
    81  | 
  | 
| 
10842
 | 
    82  | 
It is also possible to use your own well-founded relations with
  | 
| 
 | 
    83  | 
\isacommand{recdef}.  For example, the greater-than relation can be made
 | 
| 
 | 
    84  | 
well-founded by cutting it off at a certain point.  Here is an example
  | 
| 
 | 
    85  | 
of a recursive function that calls itself with increasing values up to ten:%
  | 
| 
10187
 | 
    86  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    87  | 
\isamarkuptrue%
  | 
| 
 | 
    88  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    89  | 
\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    90  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    91  | 
\ f\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}i{\isacharcomma}j{\isacharparenright}{\isachardot}\ j{\isacharless}i\ {\isasymand}\ i\ {\isasymle}\ {\isacharparenleft}{\isadigit{1}}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharbraceright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    92  | 
{\isachardoublequoteopen}f\ i\ {\isacharequal}\ {\isacharparenleft}if\ {\isadigit{1}}{\isadigit{0}}\ {\isasymle}\ i\ then\ {\isadigit{0}}\ else\ i\ {\isacharasterisk}\ f{\isacharparenleft}Suc\ i{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
11636
 | 
    93  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    94  | 
\noindent
  | 
| 
 | 
    95  | 
Since \isacommand{recdef} is not prepared for the relation supplied above,
 | 
| 
 | 
    96  | 
Isabelle rejects the definition.  We should first have proved that
  | 
| 
 | 
    97  | 
our relation was well-founded:%
  | 
| 
 | 
    98  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    99  | 
\isamarkuptrue%
  | 
| 
 | 
   100  | 
\isacommand{lemma}\isamarkupfalse%
 | 
| 
 | 
   101  | 
\ wf{\isacharunderscore}greater{\isacharcolon}\ {\isachardoublequoteopen}wf\ {\isacharbraceleft}{\isacharparenleft}i{\isacharcomma}j{\isacharparenright}{\isachardot}\ j{\isacharless}i\ {\isasymand}\ i\ {\isasymle}\ {\isacharparenleft}N{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharbraceright}{\isachardoublequoteclose}%
 | 
| 
17056
 | 
   102  | 
\isadelimproof
  | 
| 
 | 
   103  | 
%
  | 
| 
 | 
   104  | 
\endisadelimproof
  | 
| 
 | 
   105  | 
%
  | 
| 
 | 
   106  | 
\isatagproof
  | 
| 
16069
 | 
   107  | 
%
  | 
| 
 | 
   108  | 
\begin{isamarkuptxt}%
 | 
| 
 | 
   109  | 
\noindent
  | 
| 
 | 
   110  | 
The proof is by showing that our relation is a subset of another well-founded
  | 
| 
 | 
   111  | 
relation: one given by a measure function.\index{*wf_subset (theorem)}%
 | 
| 
 | 
   112  | 
\end{isamarkuptxt}%
 | 
| 
17175
 | 
   113  | 
\isamarkuptrue%
  | 
| 
 | 
   114  | 
\isacommand{apply}\isamarkupfalse%
 | 
| 
 | 
   115  | 
\ {\isacharparenleft}rule\ wf{\isacharunderscore}subset\ {\isacharbrackleft}of\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}k{\isacharcolon}{\isacharcolon}nat{\isachardot}\ N{\isacharminus}k{\isacharparenright}{\isachardoublequoteclose}{\isacharbrackright}{\isacharcomma}\ blast{\isacharparenright}%
 | 
| 
16069
 | 
   116  | 
\begin{isamarkuptxt}%
 | 
| 
 | 
   117  | 
\begin{isabelle}%
 | 
| 
 | 
   118  | 
\ {\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}%
 | 
| 
 | 
   119  | 
\end{isabelle}
 | 
| 
 | 
   120  | 
  | 
| 
 | 
   121  | 
\noindent
  | 
| 
 | 
   122  | 
The inclusion remains to be proved. After unfolding some definitions, 
  | 
| 
 | 
   123  | 
we are left with simple arithmetic:%
  | 
| 
 | 
   124  | 
\end{isamarkuptxt}%
 | 
| 
17175
 | 
   125  | 
\isamarkuptrue%
  | 
| 
 | 
   126  | 
\isacommand{apply}\isamarkupfalse%
 | 
| 
 | 
   127  | 
\ {\isacharparenleft}clarify{\isacharcomma}\ simp\ add{\isacharcolon}\ measure{\isacharunderscore}def\ inv{\isacharunderscore}image{\isacharunderscore}def{\isacharparenright}%
 | 
| 
16069
 | 
   128  | 
\begin{isamarkuptxt}%
 | 
| 
 | 
   129  | 
\begin{isabelle}%
 | 
| 
 | 
   130  | 
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}a\ b{\isachardot}\ {\isasymlbrakk}b\ {\isacharless}\ a{\isacharsemicolon}\ a\ {\isasymle}\ N{\isasymrbrakk}\ {\isasymLongrightarrow}\ N\ {\isacharminus}\ a\ {\isacharless}\ N\ {\isacharminus}\ b%
 | 
| 
 | 
   131  | 
\end{isabelle}
 | 
| 
 | 
   132  | 
  | 
| 
 | 
   133  | 
\noindent
  | 
| 
 | 
   134  | 
And that is dispatched automatically:%
  | 
| 
 | 
   135  | 
\end{isamarkuptxt}%
 | 
| 
17175
 | 
   136  | 
\isamarkuptrue%
  | 
| 
 | 
   137  | 
\isacommand{by}\isamarkupfalse%
 | 
| 
 | 
   138  | 
\ arith%
  | 
| 
17056
 | 
   139  | 
\endisatagproof
  | 
| 
 | 
   140  | 
{\isafoldproof}%
 | 
| 
 | 
   141  | 
%
  | 
| 
 | 
   142  | 
\isadelimproof
  | 
| 
 | 
   143  | 
%
  | 
| 
 | 
   144  | 
\endisadelimproof
  | 
| 
11866
 | 
   145  | 
%
  | 
| 
11636
 | 
   146  | 
\begin{isamarkuptext}%
 | 
| 
 | 
   147  | 
\noindent
  | 
| 
 | 
   148  | 
  | 
| 
 | 
   149  | 
Armed with this lemma, we use the \attrdx{recdef_wf} attribute to attach a
 | 
| 
13111
 | 
   150  | 
crucial hint\cmmdx{hints} to our definition:%
 | 
| 
11636
 | 
   151  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
   152  | 
\isamarkuptrue%
  | 
| 
 | 
   153  | 
\isamarkupfalse%
  | 
| 
 | 
   154  | 
\isamarkupfalse%
  | 
| 
 | 
   155  | 
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}wf{\isacharcolon}\ wf{\isacharunderscore}greater{\isacharparenright}%
 | 
| 
11636
 | 
   156  | 
\begin{isamarkuptext}%
 | 
| 
 | 
   157  | 
\noindent
  | 
| 
11706
 | 
   158  | 
Alternatively, we could have given \isa{measure\ {\isacharparenleft}{\isasymlambda}k{\isacharcolon}{\isacharcolon}nat{\isachardot}\ {\isadigit{1}}{\isadigit{0}}{\isacharminus}k{\isacharparenright}} for the
 | 
| 
11636
 | 
   159  | 
well-founded relation in our \isacommand{recdef}.  However, the arithmetic
 | 
| 
 | 
   160  | 
goal in the lemma above would have arisen instead in the \isacommand{recdef}
 | 
| 
 | 
   161  | 
termination proof, where we have less control.  A tailor-made termination
  | 
| 
 | 
   162  | 
relation makes even more sense when it can be used in several function
  | 
| 
 | 
   163  | 
declarations.%
  | 
| 
 | 
   164  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
   165  | 
\isamarkuptrue%
  | 
| 
17056
 | 
   166  | 
%
  | 
| 
 | 
   167  | 
\isadelimtheory
  | 
| 
 | 
   168  | 
%
  | 
| 
 | 
   169  | 
\endisadelimtheory
  | 
| 
 | 
   170  | 
%
  | 
| 
 | 
   171  | 
\isatagtheory
  | 
| 
17175
 | 
   172  | 
\isamarkupfalse%
  | 
| 
17056
 | 
   173  | 
%
  | 
| 
 | 
   174  | 
\endisatagtheory
  | 
| 
 | 
   175  | 
{\isafoldtheory}%
 | 
| 
 | 
   176  | 
%
  | 
| 
 | 
   177  | 
\isadelimtheory
  | 
| 
 | 
   178  | 
%
  | 
| 
 | 
   179  | 
\endisadelimtheory
  | 
| 
11636
 | 
   180  | 
\end{isabellebody}%
 | 
| 
10187
 | 
   181  | 
%%% Local Variables:
  | 
| 
 | 
   182  | 
%%% mode: latex
  | 
| 
 | 
   183  | 
%%% TeX-master: "root"
  | 
| 
 | 
   184  | 
%%% End:
  |