9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
8749
|
3 |
%
|
|
4 |
\begin{isamarkuptext}%
|
|
5 |
Once we have succeeded in proving all termination conditions, the recursion
|
|
6 |
equations become simplification rules, just as with
|
|
7 |
\isacommand{primrec}. In most cases this works fine, but there is a subtle
|
|
8 |
problem that must be mentioned: simplification may not
|
|
9 |
terminate because of automatic splitting of \isa{if}.
|
|
10 |
Let us look at an example:%
|
|
11 |
\end{isamarkuptext}%
|
9674
|
12 |
\isacommand{consts}\ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
|
|
13 |
\isacommand{recdef}\ gcd\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
|
|
14 |
\ \ {\isachardoublequote}gcd\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ n{\isacharequal}\isadigit{0}\ then\ m\ else\ gcd{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
|
8749
|
15 |
\begin{isamarkuptext}%
|
|
16 |
\noindent
|
|
17 |
According to the measure function, the second argument should decrease with
|
9541
|
18 |
each recursive call. The resulting termination condition
|
|
19 |
\begin{quote}
|
|
20 |
|
|
21 |
\begin{isabelle}%
|
9674
|
22 |
\mbox{n}\ {\isasymnoteq}\ \isadigit{0}\ {\isasymLongrightarrow}\ \mbox{m}\ mod\ \mbox{n}\ {\isacharless}\ \mbox{n}
|
9541
|
23 |
\end{isabelle}%
|
|
24 |
|
|
25 |
\end{quote}
|
8749
|
26 |
is provded automatically because it is already present as a lemma in the
|
|
27 |
arithmetic library. Thus the recursion equation becomes a simplification
|
|
28 |
rule. Of course the equation is nonterminating if we are allowed to unfold
|
9754
|
29 |
the recursive call inside the \isa{if} branch, which is why programming
|
8749
|
30 |
languages and our simplifier don't do that. Unfortunately the simplifier does
|
|
31 |
something else which leads to the same problem: it splits \isa{if}s if the
|
|
32 |
condition simplifies to neither \isa{True} nor \isa{False}. For
|
9541
|
33 |
example, simplification reduces
|
|
34 |
\begin{quote}
|
|
35 |
|
|
36 |
\begin{isabelle}%
|
9674
|
37 |
gcd\ {\isacharparenleft}\mbox{m}{\isacharcomma}\ \mbox{n}{\isacharparenright}\ {\isacharequal}\ \mbox{k}
|
9541
|
38 |
\end{isabelle}%
|
|
39 |
|
|
40 |
\end{quote}
|
|
41 |
in one step to
|
|
42 |
\begin{quote}
|
|
43 |
|
|
44 |
\begin{isabelle}%
|
9674
|
45 |
{\isacharparenleft}if\ \mbox{n}\ {\isacharequal}\ \isadigit{0}\ then\ \mbox{m}\ else\ gcd\ {\isacharparenleft}\mbox{n}{\isacharcomma}\ \mbox{m}\ mod\ \mbox{n}{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ \mbox{k}
|
9541
|
46 |
\end{isabelle}%
|
|
47 |
|
|
48 |
\end{quote}
|
|
49 |
where the condition cannot be reduced further, and splitting leads to
|
|
50 |
\begin{quote}
|
|
51 |
|
|
52 |
\begin{isabelle}%
|
9674
|
53 |
{\isacharparenleft}\mbox{n}\ {\isacharequal}\ \isadigit{0}\ {\isasymlongrightarrow}\ \mbox{m}\ {\isacharequal}\ \mbox{k}{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}\mbox{n}\ {\isasymnoteq}\ \isadigit{0}\ {\isasymlongrightarrow}\ gcd\ {\isacharparenleft}\mbox{n}{\isacharcomma}\ \mbox{m}\ mod\ \mbox{n}{\isacharparenright}\ {\isacharequal}\ \mbox{k}{\isacharparenright}
|
9541
|
54 |
\end{isabelle}%
|
|
55 |
|
|
56 |
\end{quote}
|
9674
|
57 |
Since the recursive call \isa{gcd\ {\isacharparenleft}\mbox{n}{\isacharcomma}\ \mbox{m}\ mod\ \mbox{n}{\isacharparenright}} is no longer protected by
|
9541
|
58 |
an \isa{if}, it is unfolded again, which leads to an infinite chain of
|
|
59 |
simplification steps. Fortunately, this problem can be avoided in many
|
|
60 |
different ways.
|
8749
|
61 |
|
9754
|
62 |
The most radical solution is to disable the offending \\isa{split{\isacharunderscore}if} as
|
|
63 |
shown in the section on case splits in \S\ref{sec:Simplification}. However,
|
|
64 |
we do not recommend this because it means you will often have to invoke the
|
|
65 |
rule explicitly when \isa{if} is involved.
|
8749
|
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}%
|
9674
|
71 |
\isacommand{consts}\ gcd\isadigit{1}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
|
|
72 |
\isacommand{recdef}\ gcd\isadigit{1}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
|
|
73 |
\ \ {\isachardoublequote}gcd\isadigit{1}\ {\isacharparenleft}m{\isacharcomma}\ \isadigit{0}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequote}\isanewline
|
|
74 |
\ \ {\isachardoublequote}gcd\isadigit{1}\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd\isadigit{1}{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequote}%
|
8749
|
75 |
\begin{isamarkuptext}%
|
|
76 |
\noindent
|
|
77 |
Note that the order of equations is important and hides the side condition
|
9754
|
78 |
\isa{\mbox{n}\ {\isasymnoteq}\ \isadigit{0}}. Unfortunately, in general the case distinction
|
8749
|
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}%
|
9674
|
84 |
\isacommand{consts}\ gcd\isadigit{2}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
|
|
85 |
\isacommand{recdef}\ gcd\isadigit{2}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
|
|
86 |
\ \ {\isachardoublequote}gcd\isadigit{2}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}case\ n{\isacharequal}\isadigit{0}\ of\ True\ {\isasymRightarrow}\ m\ {\isacharbar}\ False\ {\isasymRightarrow}\ gcd\isadigit{2}{\isacharparenleft}n{\isacharcomma}m\ mod\ n{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
|
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}%
|
9674
|
94 |
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}gcd\ {\isacharparenleft}m{\isacharcomma}\ \isadigit{0}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequote}\isanewline
|
|
95 |
\isacommand{by}{\isacharparenleft}simp{\isacharparenright}\isanewline
|
|
96 |
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}n\ {\isasymnoteq}\ \isadigit{0}\ {\isasymLongrightarrow}\ gcd{\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequote}\isanewline
|
|
97 |
\isacommand{by}{\isacharparenleft}simp{\isacharparenright}%
|
8749
|
98 |
\begin{isamarkuptext}%
|
|
99 |
\noindent
|
|
100 |
after which we can disable the original simplification rule:%
|
|
101 |
\end{isamarkuptext}%
|
9674
|
102 |
\isacommand{lemmas}\ {\isacharbrackleft}simp\ del{\isacharbrackright}\ {\isacharequal}\ gcd{\isachardot}simps\isanewline
|
9722
|
103 |
\end{isabellebody}%
|
9145
|
104 |
%%% Local Variables:
|
|
105 |
%%% mode: latex
|
|
106 |
%%% TeX-master: "root"
|
|
107 |
%%% End:
|