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