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
|
9754
|
10 |
terminate because of automatic splitting of @{name"if"}.
|
8745
|
11 |
Let us look at an example:
|
|
12 |
*}
|
|
13 |
|
|
14 |
consts gcd :: "nat*nat \\<Rightarrow> nat";
|
|
15 |
recdef gcd "measure (\\<lambda>(m,n).n)"
|
|
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 |
\begin{quote}
|
|
22 |
@{term[display]"n ~= 0 ==> m mod n < n"}
|
|
23 |
\end{quote}
|
8745
|
24 |
is provded automatically because it is already present as a lemma in the
|
|
25 |
arithmetic library. Thus the recursion equation becomes a simplification
|
|
26 |
rule. Of course the equation is nonterminating if we are allowed to unfold
|
9754
|
27 |
the recursive call inside the @{name"if"} branch, which is why programming
|
8745
|
28 |
languages and our simplifier don't do that. Unfortunately the simplifier does
|
9754
|
29 |
something else which leads to the same problem: it splits @{name"if"}s if the
|
|
30 |
condition simplifies to neither @{term"True"} nor @{term"False"}. For
|
8745
|
31 |
example, simplification reduces
|
9541
|
32 |
\begin{quote}
|
|
33 |
@{term[display]"gcd(m,n) = k"}
|
|
34 |
\end{quote}
|
8745
|
35 |
in one step to
|
9541
|
36 |
\begin{quote}
|
|
37 |
@{term[display]"(if n=0 then m else gcd(n, m mod n)) = k"}
|
|
38 |
\end{quote}
|
8745
|
39 |
where the condition cannot be reduced further, and splitting leads to
|
9541
|
40 |
\begin{quote}
|
|
41 |
@{term[display]"(n=0 --> m=k) & (n ~= 0 --> gcd(n, m mod n)=k)"}
|
|
42 |
\end{quote}
|
|
43 |
Since the recursive call @{term"gcd(n, m mod n)"} is no longer protected by
|
9754
|
44 |
an @{name"if"}, it is unfolded again, which leads to an infinite chain of
|
9541
|
45 |
simplification steps. Fortunately, this problem can be avoided in many
|
|
46 |
different ways.
|
8745
|
47 |
|
9754
|
48 |
The most radical solution is to disable the offending \@{name"split_if"} as
|
|
49 |
shown in the section on case splits in \S\ref{sec:Simplification}. However,
|
|
50 |
we do not recommend this because it means you will often have to invoke the
|
|
51 |
rule explicitly when @{name"if"} is involved.
|
8745
|
52 |
|
|
53 |
If possible, the definition should be given by pattern matching on the left
|
9754
|
54 |
rather than @{name"if"} on the right. In the case of @{term"gcd"} the
|
8745
|
55 |
following alternative definition suggests itself:
|
|
56 |
*}
|
|
57 |
|
|
58 |
consts gcd1 :: "nat*nat \\<Rightarrow> nat";
|
|
59 |
recdef gcd1 "measure (\\<lambda>(m,n).n)"
|
|
60 |
"gcd1 (m, 0) = m"
|
|
61 |
"gcd1 (m, n) = gcd1(n, m mod n)";
|
|
62 |
|
|
63 |
|
|
64 |
text{*\noindent
|
|
65 |
Note that the order of equations is important and hides the side condition
|
9754
|
66 |
@{prop"n ~= 0"}. Unfortunately, in general the case distinction
|
8745
|
67 |
may not be expressible by pattern matching.
|
|
68 |
|
9754
|
69 |
A very simple alternative is to replace @{name"if"} by @{name"case"}, which
|
|
70 |
is also available for @{typ"bool"} but is not split automatically:
|
8745
|
71 |
*}
|
|
72 |
|
|
73 |
consts gcd2 :: "nat*nat \\<Rightarrow> nat";
|
|
74 |
recdef gcd2 "measure (\\<lambda>(m,n).n)"
|
|
75 |
"gcd2(m,n) = (case n=0 of True \\<Rightarrow> m | False \\<Rightarrow> gcd2(n,m mod n))";
|
|
76 |
|
|
77 |
text{*\noindent
|
|
78 |
In fact, this is probably the neatest solution next to pattern matching.
|
|
79 |
|
|
80 |
A final alternative is to replace the offending simplification rules by
|
9754
|
81 |
derived conditional ones. For @{term"gcd"} it means we have to prove
|
8745
|
82 |
*}
|
|
83 |
|
|
84 |
lemma [simp]: "gcd (m, 0) = m";
|
9458
|
85 |
by(simp);
|
8745
|
86 |
lemma [simp]: "n \\<noteq> 0 \\<Longrightarrow> gcd(m, n) = gcd(n, m mod n)";
|
9458
|
87 |
by(simp);
|
8745
|
88 |
|
|
89 |
text{*\noindent
|
|
90 |
after which we can disable the original simplification rule:
|
|
91 |
*}
|
|
92 |
|
|
93 |
lemmas [simp del] = gcd.simps;
|
|
94 |
|
|
95 |
(*<*)
|
|
96 |
end
|
|
97 |
(*>*)
|