8745
|
1 |
(*<*)
|
|
2 |
theory simplification = Main:;
|
|
3 |
(*>*)
|
|
4 |
|
|
5 |
text{*
|
|
6 |
Once we have succeeded in proving all termination conditions, the recursion
|
|
7 |
equations become simplification rules, just as with
|
|
8 |
\isacommand{primrec}. In most cases this works fine, but there is a subtle
|
|
9 |
problem that must be mentioned: simplification may not
|
10171
|
10 |
terminate because of automatic splitting of @{text if}.
|
8745
|
11 |
Let us look at an example:
|
|
12 |
*}
|
|
13 |
|
10171
|
14 |
consts gcd :: "nat\<times>nat \<Rightarrow> nat";
|
|
15 |
recdef gcd "measure (\<lambda>(m,n).n)"
|
8745
|
16 |
"gcd (m, n) = (if n=0 then m else gcd(n, m mod n))";
|
|
17 |
|
|
18 |
text{*\noindent
|
|
19 |
According to the measure function, the second argument should decrease with
|
|
20 |
each recursive call. The resulting termination condition
|
9541
|
21 |
@{term[display]"n ~= 0 ==> m mod n < n"}
|
10885
|
22 |
is proved automatically because it is already present as a lemma in
|
|
23 |
HOL\@. Thus the recursion equation becomes a simplification
|
8745
|
24 |
rule. Of course the equation is nonterminating if we are allowed to unfold
|
10171
|
25 |
the recursive call inside the @{text else} branch, which is why programming
|
8745
|
26 |
languages and our simplifier don't do that. Unfortunately the simplifier does
|
10171
|
27 |
something else which leads to the same problem: it splits @{text if}s if the
|
|
28 |
condition simplifies to neither @{term True} nor @{term False}. For
|
8745
|
29 |
example, simplification reduces
|
9541
|
30 |
@{term[display]"gcd(m,n) = k"}
|
8745
|
31 |
in one step to
|
9541
|
32 |
@{term[display]"(if n=0 then m else gcd(n, m mod n)) = k"}
|
8745
|
33 |
where the condition cannot be reduced further, and splitting leads to
|
9541
|
34 |
@{term[display]"(n=0 --> m=k) & (n ~= 0 --> gcd(n, m mod n)=k)"}
|
|
35 |
Since the recursive call @{term"gcd(n, m mod n)"} is no longer protected by
|
10171
|
36 |
an @{text if}, it is unfolded again, which leads to an infinite chain of
|
9541
|
37 |
simplification steps. Fortunately, this problem can be avoided in many
|
|
38 |
different ways.
|
8745
|
39 |
|
11215
|
40 |
The most radical solution is to disable the offending @{thm[source]split_if}
|
|
41 |
as shown in \S\ref{sec:AutoCaseSplits}. However, we do not recommend this
|
|
42 |
because it means you will often have to invoke the rule explicitly when
|
|
43 |
@{text if} is involved.
|
8745
|
44 |
|
|
45 |
If possible, the definition should be given by pattern matching on the left
|
10171
|
46 |
rather than @{text if} on the right. In the case of @{term gcd} the
|
8745
|
47 |
following alternative definition suggests itself:
|
|
48 |
*}
|
|
49 |
|
10171
|
50 |
consts gcd1 :: "nat\<times>nat \<Rightarrow> nat";
|
|
51 |
recdef gcd1 "measure (\<lambda>(m,n).n)"
|
8745
|
52 |
"gcd1 (m, 0) = m"
|
|
53 |
"gcd1 (m, n) = gcd1(n, m mod n)";
|
|
54 |
|
|
55 |
|
|
56 |
text{*\noindent
|
|
57 |
Note that the order of equations is important and hides the side condition
|
9754
|
58 |
@{prop"n ~= 0"}. Unfortunately, in general the case distinction
|
8745
|
59 |
may not be expressible by pattern matching.
|
|
60 |
|
10171
|
61 |
A very simple alternative is to replace @{text if} by @{text case}, which
|
10178
|
62 |
is also available for @{typ bool} but is not split automatically:
|
8745
|
63 |
*}
|
|
64 |
|
10171
|
65 |
consts gcd2 :: "nat\<times>nat \<Rightarrow> nat";
|
|
66 |
recdef gcd2 "measure (\<lambda>(m,n).n)"
|
|
67 |
"gcd2(m,n) = (case n=0 of True \<Rightarrow> m | False \<Rightarrow> gcd2(n,m mod n))";
|
8745
|
68 |
|
|
69 |
text{*\noindent
|
|
70 |
In fact, this is probably the neatest solution next to pattern matching.
|
|
71 |
|
|
72 |
A final alternative is to replace the offending simplification rules by
|
10171
|
73 |
derived conditional ones. For @{term gcd} it means we have to prove
|
10795
|
74 |
these lemmas:
|
8745
|
75 |
*}
|
|
76 |
|
|
77 |
lemma [simp]: "gcd (m, 0) = m";
|
10171
|
78 |
apply(simp);
|
|
79 |
done
|
|
80 |
lemma [simp]: "n \<noteq> 0 \<Longrightarrow> gcd(m, n) = gcd(n, m mod n)";
|
|
81 |
apply(simp);
|
|
82 |
done
|
8745
|
83 |
|
|
84 |
text{*\noindent
|
10795
|
85 |
Now we can disable the original simplification rule:
|
8745
|
86 |
*}
|
|
87 |
|
9933
|
88 |
declare gcd.simps [simp del]
|
8745
|
89 |
|
|
90 |
(*<*)
|
|
91 |
end
|
|
92 |
(*>*)
|