9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{simplification}%
|
17056
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
17175
|
10 |
\isamarkupfalse%
|
17056
|
11 |
%
|
|
12 |
\endisatagtheory
|
|
13 |
{\isafoldtheory}%
|
|
14 |
%
|
|
15 |
\isadelimtheory
|
|
16 |
%
|
|
17 |
\endisadelimtheory
|
8749
|
18 |
%
|
|
19 |
\begin{isamarkuptext}%
|
11458
|
20 |
Once we have proved all the termination conditions, the \isacommand{recdef}
|
|
21 |
recursion equations become simplification rules, just as with
|
8749
|
22 |
\isacommand{primrec}. In most cases this works fine, but there is a subtle
|
|
23 |
problem that must be mentioned: simplification may not
|
|
24 |
terminate because of automatic splitting of \isa{if}.
|
11458
|
25 |
\index{*if expressions!splitting of}
|
8749
|
26 |
Let us look at an example:%
|
|
27 |
\end{isamarkuptext}%
|
17175
|
28 |
\isamarkuptrue%
|
|
29 |
\isacommand{consts}\isamarkupfalse%
|
|
30 |
\ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
|
|
31 |
\isacommand{recdef}\isamarkupfalse%
|
|
32 |
\ gcd\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
33 |
\ \ {\isachardoublequoteopen}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}{\isachardoublequoteclose}%
|
8749
|
34 |
\begin{isamarkuptext}%
|
|
35 |
\noindent
|
|
36 |
According to the measure function, the second argument should decrease with
|
9541
|
37 |
each recursive call. The resulting termination condition
|
|
38 |
\begin{isabelle}%
|
12473
|
39 |
\ \ \ \ \ n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ m\ mod\ n\ {\isacharless}\ n%
|
9924
|
40 |
\end{isabelle}
|
10878
|
41 |
is proved automatically because it is already present as a lemma in
|
|
42 |
HOL\@. Thus the recursion equation becomes a simplification
|
8749
|
43 |
rule. Of course the equation is nonterminating if we are allowed to unfold
|
10171
|
44 |
the recursive call inside the \isa{else} branch, which is why programming
|
8749
|
45 |
languages and our simplifier don't do that. Unfortunately the simplifier does
|
11458
|
46 |
something else that leads to the same problem: it splits
|
|
47 |
each \isa{if}-expression unless its
|
|
48 |
condition simplifies to \isa{True} or \isa{False}. For
|
9541
|
49 |
example, simplification reduces
|
|
50 |
\begin{isabelle}%
|
16797
|
51 |
\ \ \ \ \ simplification{\isachardot}gcd\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ k%
|
9924
|
52 |
\end{isabelle}
|
9541
|
53 |
in one step to
|
|
54 |
\begin{isabelle}%
|
16797
|
55 |
\ \ \ \ \ {\isacharparenleft}if\ n\ {\isacharequal}\ {\isadigit{0}}\ then\ m\ else\ simplification{\isachardot}gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ k%
|
9924
|
56 |
\end{isabelle}
|
9541
|
57 |
where the condition cannot be reduced further, and splitting leads to
|
|
58 |
\begin{isabelle}%
|
16797
|
59 |
\ \ \ \ \ {\isacharparenleft}n\ {\isacharequal}\ {\isadigit{0}}\ {\isasymlongrightarrow}\ m\ {\isacharequal}\ k{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymlongrightarrow}\ simplification{\isachardot}gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}\ {\isacharequal}\ k{\isacharparenright}%
|
9924
|
60 |
\end{isabelle}
|
16797
|
61 |
Since the recursive call \isa{simplification{\isachardot}gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}} is no longer protected by
|
9541
|
62 |
an \isa{if}, it is unfolded again, which leads to an infinite chain of
|
|
63 |
simplification steps. Fortunately, this problem can be avoided in many
|
|
64 |
different ways.
|
8749
|
65 |
|
11458
|
66 |
The most radical solution is to disable the offending theorem
|
|
67 |
\isa{split{\isacharunderscore}if},
|
11215
|
68 |
as shown in \S\ref{sec:AutoCaseSplits}. However, we do not recommend this
|
11458
|
69 |
approach: you will often have to invoke the rule explicitly when
|
11215
|
70 |
\isa{if} is involved.
|
8749
|
71 |
|
|
72 |
If possible, the definition should be given by pattern matching on the left
|
16797
|
73 |
rather than \isa{if} on the right. In the case of \isa{simplification{\isachardot}gcd} the
|
8749
|
74 |
following alternative definition suggests itself:%
|
|
75 |
\end{isamarkuptext}%
|
17175
|
76 |
\isamarkuptrue%
|
|
77 |
\isacommand{consts}\isamarkupfalse%
|
|
78 |
\ gcd{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
|
|
79 |
\isacommand{recdef}\isamarkupfalse%
|
|
80 |
\ gcd{\isadigit{1}}\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
81 |
\ \ {\isachardoublequoteopen}gcd{\isadigit{1}}\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
|
|
82 |
\ \ {\isachardoublequoteopen}gcd{\isadigit{1}}\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isadigit{1}}{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequoteclose}%
|
8749
|
83 |
\begin{isamarkuptext}%
|
|
84 |
\noindent
|
11458
|
85 |
The order of equations is important: it hides the side condition
|
12473
|
86 |
\isa{n\ {\isasymnoteq}\ {\isadigit{0}}}. Unfortunately, in general the case distinction
|
8749
|
87 |
may not be expressible by pattern matching.
|
|
88 |
|
11458
|
89 |
A simple alternative is to replace \isa{if} by \isa{case},
|
|
90 |
which is also available for \isa{bool} and is not split automatically:%
|
8749
|
91 |
\end{isamarkuptext}%
|
17175
|
92 |
\isamarkuptrue%
|
|
93 |
\isacommand{consts}\isamarkupfalse%
|
|
94 |
\ gcd{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
|
|
95 |
\isacommand{recdef}\isamarkupfalse%
|
|
96 |
\ gcd{\isadigit{2}}\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
97 |
\ \ {\isachardoublequoteopen}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}{\isachardoublequoteclose}%
|
8749
|
98 |
\begin{isamarkuptext}%
|
|
99 |
\noindent
|
11458
|
100 |
This is probably the neatest solution next to pattern matching, and it is
|
|
101 |
always available.
|
8749
|
102 |
|
|
103 |
A final alternative is to replace the offending simplification rules by
|
16797
|
104 |
derived conditional ones. For \isa{simplification{\isachardot}gcd} it means we have to prove
|
10795
|
105 |
these lemmas:%
|
8749
|
106 |
\end{isamarkuptext}%
|
17175
|
107 |
\isamarkuptrue%
|
|
108 |
\isacommand{lemma}\isamarkupfalse%
|
|
109 |
\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}gcd\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
|
17056
|
110 |
%
|
|
111 |
\isadelimproof
|
|
112 |
%
|
|
113 |
\endisadelimproof
|
|
114 |
%
|
|
115 |
\isatagproof
|
17175
|
116 |
\isacommand{apply}\isamarkupfalse%
|
|
117 |
{\isacharparenleft}simp{\isacharparenright}\isanewline
|
|
118 |
\isacommand{done}\isamarkupfalse%
|
|
119 |
%
|
17056
|
120 |
\endisatagproof
|
|
121 |
{\isafoldproof}%
|
|
122 |
%
|
|
123 |
\isadelimproof
|
|
124 |
\isanewline
|
|
125 |
%
|
|
126 |
\endisadelimproof
|
15481
|
127 |
\isanewline
|
17175
|
128 |
\isacommand{lemma}\isamarkupfalse%
|
|
129 |
\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ gcd{\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
17056
|
130 |
%
|
|
131 |
\isadelimproof
|
|
132 |
%
|
|
133 |
\endisadelimproof
|
|
134 |
%
|
|
135 |
\isatagproof
|
17175
|
136 |
\isacommand{apply}\isamarkupfalse%
|
|
137 |
{\isacharparenleft}simp{\isacharparenright}\isanewline
|
|
138 |
\isacommand{done}\isamarkupfalse%
|
|
139 |
%
|
17056
|
140 |
\endisatagproof
|
|
141 |
{\isafoldproof}%
|
|
142 |
%
|
|
143 |
\isadelimproof
|
|
144 |
%
|
|
145 |
\endisadelimproof
|
11866
|
146 |
%
|
8749
|
147 |
\begin{isamarkuptext}%
|
|
148 |
\noindent
|
11458
|
149 |
Simplification terminates for these proofs because the condition of the \isa{if} simplifies to \isa{True} or \isa{False}.
|
10795
|
150 |
Now we can disable the original simplification rule:%
|
8749
|
151 |
\end{isamarkuptext}%
|
17175
|
152 |
\isamarkuptrue%
|
|
153 |
\isacommand{declare}\isamarkupfalse%
|
|
154 |
\ gcd{\isachardot}simps\ {\isacharbrackleft}simp\ del{\isacharbrackright}\isanewline
|
17056
|
155 |
%
|
|
156 |
\isadelimtheory
|
|
157 |
%
|
|
158 |
\endisadelimtheory
|
|
159 |
%
|
|
160 |
\isatagtheory
|
17175
|
161 |
\isamarkupfalse%
|
17056
|
162 |
%
|
|
163 |
\endisatagtheory
|
|
164 |
{\isafoldtheory}%
|
|
165 |
%
|
|
166 |
\isadelimtheory
|
|
167 |
%
|
|
168 |
\endisadelimtheory
|
9722
|
169 |
\end{isabellebody}%
|
9145
|
170 |
%%% Local Variables:
|
|
171 |
%%% mode: latex
|
|
172 |
%%% TeX-master: "root"
|
|
173 |
%%% End:
|