10187
|
1 |
(*<*)theory WFrec = Main:(*>*)
|
|
2 |
|
|
3 |
text{*\noindent
|
11161
|
4 |
So far, all recursive definitions were shown to terminate via measure
|
10187
|
5 |
functions. Sometimes this can be quite inconvenient or even
|
|
6 |
impossible. Fortunately, \isacommand{recdef} supports much more
|
|
7 |
general definitions. For example, termination of Ackermann's function
|
10654
|
8 |
can be shown by means of the \rmindex{lexicographic product} @{text"<*lex*>"}:
|
10187
|
9 |
*}
|
|
10 |
|
|
11 |
consts ack :: "nat\<times>nat \<Rightarrow> nat";
|
|
12 |
recdef ack "measure(\<lambda>m. m) <*lex*> measure(\<lambda>n. n)"
|
|
13 |
"ack(0,n) = Suc n"
|
|
14 |
"ack(Suc m,0) = ack(m, 1)"
|
|
15 |
"ack(Suc m,Suc n) = ack(m,ack(Suc m,n))";
|
|
16 |
|
|
17 |
text{*\noindent
|
|
18 |
The lexicographic product decreases if either its first component
|
|
19 |
decreases (as in the second equation and in the outer call in the
|
|
20 |
third equation) or its first component stays the same and the second
|
|
21 |
component decreases (as in the inner call in the third equation).
|
|
22 |
|
|
23 |
In general, \isacommand{recdef} supports termination proofs based on
|
10396
|
24 |
arbitrary well-founded relations as introduced in \S\ref{sec:Well-founded}.
|
|
25 |
This is called \textbf{well-founded
|
10545
|
26 |
recursion}\indexbold{recursion!well-founded}. Clearly, a function definition
|
|
27 |
is total iff the set of all pairs $(r,l)$, where $l$ is the argument on the
|
10396
|
28 |
left-hand side of an equation and $r$ the argument of some recursive call on
|
|
29 |
the corresponding right-hand side, induces a well-founded relation. For a
|
|
30 |
systematic account of termination proofs via well-founded relations see, for
|
10885
|
31 |
example, Baader and Nipkow~\cite{Baader-Nipkow}.
|
10187
|
32 |
|
10396
|
33 |
Each \isacommand{recdef} definition should be accompanied (after the name of
|
|
34 |
the function) by a well-founded relation on the argument type of the
|
11196
|
35 |
function. Isabelle/HOL formalizes some of the most important
|
10396
|
36 |
constructions of well-founded relations (see \S\ref{sec:Well-founded}). For
|
|
37 |
example, @{term"measure f"} is always well-founded, and the lexicographic
|
|
38 |
product of two well-founded relations is again well-founded, which we relied
|
|
39 |
on when defining Ackermann's function above.
|
11308
|
40 |
Of course the lexicographic product can also be iterated:
|
10189
|
41 |
*}
|
10187
|
42 |
|
10189
|
43 |
consts contrived :: "nat \<times> nat \<times> nat \<Rightarrow> nat"
|
|
44 |
recdef contrived
|
|
45 |
"measure(\<lambda>i. i) <*lex*> measure(\<lambda>j. j) <*lex*> measure(\<lambda>k. k)"
|
|
46 |
"contrived(i,j,Suc k) = contrived(i,j,k)"
|
|
47 |
"contrived(i,Suc j,0) = contrived(i,j,j)"
|
|
48 |
"contrived(Suc i,0,0) = contrived(i,i,i)"
|
|
49 |
"contrived(0,0,0) = 0"
|
|
50 |
|
|
51 |
text{*
|
10396
|
52 |
Lexicographic products of measure functions already go a long
|
10885
|
53 |
way. Furthermore, you may embed a type in an
|
10396
|
54 |
existing well-founded relation via the inverse image construction @{term
|
|
55 |
inv_image}. All these constructions are known to \isacommand{recdef}. Thus you
|
10241
|
56 |
will never have to prove well-foundedness of any relation composed
|
10189
|
57 |
solely of these building blocks. But of course the proof of
|
|
58 |
termination of your function definition, i.e.\ that the arguments
|
|
59 |
decrease with every recursive call, may still require you to provide
|
|
60 |
additional lemmas.
|
|
61 |
|
10841
|
62 |
It is also possible to use your own well-founded relations with
|
|
63 |
\isacommand{recdef}. For example, the greater-than relation can be made
|
|
64 |
well-founded by cutting it off at a certain point. Here is an example
|
|
65 |
of a recursive function that calls itself with increasing values up to ten:
|
10187
|
66 |
*}
|
10189
|
67 |
|
|
68 |
consts f :: "nat \<Rightarrow> nat"
|
10841
|
69 |
recdef f "{(i,j). j<i \<and> i \<le> (#10::nat)}"
|
|
70 |
"f i = (if #10 \<le> i then 0 else i * f(Suc i))";
|
10189
|
71 |
|
10396
|
72 |
text{*\noindent
|
10841
|
73 |
Since \isacommand{recdef} is not prepared for the relation supplied above,
|
|
74 |
Isabelle rejects the definition. We should first have proved that
|
|
75 |
our relation was well-founded:
|
10189
|
76 |
*}
|
|
77 |
|
10841
|
78 |
lemma wf_greater: "wf {(i,j). j<i \<and> i \<le> (N::nat)}"
|
|
79 |
|
11196
|
80 |
txt{*\noindent
|
10841
|
81 |
The proof is by showing that our relation is a subset of another well-founded
|
|
82 |
relation: one given by a measure function.
|
|
83 |
*};
|
|
84 |
|
|
85 |
apply (rule wf_subset [of "measure (\<lambda>k::nat. N-k)"], blast);
|
|
86 |
|
|
87 |
txt{*
|
|
88 |
@{subgoals[display,indent=0,margin=65]}
|
|
89 |
|
|
90 |
\noindent
|
|
91 |
The inclusion remains to be proved. After unfolding some definitions,
|
|
92 |
we are left with simple arithmetic:
|
|
93 |
*};
|
|
94 |
|
|
95 |
apply (clarify, simp add: measure_def inv_image_def)
|
|
96 |
|
|
97 |
txt{*
|
|
98 |
@{subgoals[display,indent=0,margin=65]}
|
|
99 |
|
|
100 |
\noindent
|
|
101 |
And that is dispatched automatically:
|
|
102 |
*};
|
|
103 |
|
|
104 |
by arith;
|
10189
|
105 |
|
|
106 |
text{*\noindent
|
10841
|
107 |
|
11429
|
108 |
Armed with this lemma, we use the \attrdx{recdef_wf} attribute to attach a
|
|
109 |
crucial hint to our definition:
|
10189
|
110 |
*}
|
|
111 |
(*<*)
|
|
112 |
consts g :: "nat \<Rightarrow> nat"
|
10841
|
113 |
recdef g "{(i,j). j<i \<and> i \<le> (#10::nat)}"
|
|
114 |
"g i = (if #10 \<le> i then 0 else i * g(Suc i))"
|
10189
|
115 |
(*>*)
|
10841
|
116 |
(hints recdef_wf: wf_greater);
|
|
117 |
|
|
118 |
text{*\noindent
|
|
119 |
Alternatively, we could have given @{text "measure (\<lambda>k::nat. #10-k)"} for the
|
|
120 |
well-founded relation in our \isacommand{recdef}. However, the arithmetic
|
|
121 |
goal in the lemma above would have arisen instead in the \isacommand{recdef}
|
|
122 |
termination proof, where we have less control. A tailor-made termination
|
|
123 |
relation makes even more sense when it can be used in several function
|
|
124 |
declarations.
|
|
125 |
*}
|
|
126 |
|
10396
|
127 |
(*<*)end(*>*)
|