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