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