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