9932
|
1 |
(*<*)
|
9922
|
2 |
theory simp = Main:
|
9932
|
3 |
(*>*)
|
9922
|
4 |
|
10885
|
5 |
subsubsection{*Simplification Rules*}
|
9932
|
6 |
|
|
7 |
text{*\indexbold{simplification rule}
|
|
8 |
To facilitate simplification, theorems can be declared to be simplification
|
10795
|
9 |
rules (by the attribute @{text"[simp]"}\index{*simp
|
9932
|
10 |
(attribute)}), in which case proofs by simplification make use of these
|
|
11 |
rules automatically. In addition the constructs \isacommand{datatype} and
|
|
12 |
\isacommand{primrec} (and a few others) invisibly declare useful
|
|
13 |
simplification rules. Explicit definitions are \emph{not} declared
|
|
14 |
simplification rules automatically!
|
|
15 |
|
|
16 |
Not merely equations but pretty much any theorem can become a simplification
|
|
17 |
rule. The simplifier will try to make sense of it. For example, a theorem
|
|
18 |
@{prop"~P"} is automatically turned into @{prop"P = False"}. The details
|
|
19 |
are explained in \S\ref{sec:SimpHow}.
|
|
20 |
|
|
21 |
The simplification attribute of theorems can be turned on and off as follows:
|
|
22 |
\begin{quote}
|
|
23 |
\isacommand{declare} \textit{theorem-name}@{text"[simp]"}\\
|
|
24 |
\isacommand{declare} \textit{theorem-name}@{text"[simp del]"}
|
|
25 |
\end{quote}
|
|
26 |
As a rule of thumb, equations that really simplify (like @{prop"rev(rev xs) =
|
|
27 |
xs"} and @{prop"xs @ [] = xs"}) should be made simplification
|
|
28 |
rules. Those of a more specific nature (e.g.\ distributivity laws, which
|
|
29 |
alter the structure of terms considerably) should only be used selectively,
|
|
30 |
i.e.\ they should not be default simplification rules. Conversely, it may
|
|
31 |
also happen that a simplification rule needs to be disabled in certain
|
|
32 |
proofs. Frequent changes in the simplification status of a theorem may
|
|
33 |
indicate a badly designed theory.
|
|
34 |
\begin{warn}
|
10795
|
35 |
Simplification may run forever, for example if both $f(x) = g(x)$ and
|
9932
|
36 |
$g(x) = f(x)$ are simplification rules. It is the user's responsibility not
|
|
37 |
to include simplification rules that can lead to nontermination, either on
|
|
38 |
their own or in combination with other simplification rules.
|
|
39 |
\end{warn}
|
|
40 |
*}
|
|
41 |
|
10885
|
42 |
subsubsection{*The Simplification Method*}
|
9932
|
43 |
|
|
44 |
text{*\index{*simp (method)|bold}
|
|
45 |
The general format of the simplification method is
|
|
46 |
\begin{quote}
|
|
47 |
@{text simp} \textit{list of modifiers}
|
|
48 |
\end{quote}
|
10795
|
49 |
where the list of \emph{modifiers} fine tunes the behaviour and may
|
9932
|
50 |
be empty. Most if not all of the proofs seen so far could have been performed
|
|
51 |
with @{text simp} instead of \isa{auto}, except that @{text simp} attacks
|
10971
|
52 |
only the first subgoal and may thus need to be repeated --- use
|
9932
|
53 |
\isaindex{simp_all} to simplify all subgoals.
|
|
54 |
Note that @{text simp} fails if nothing changes.
|
|
55 |
*}
|
|
56 |
|
10885
|
57 |
subsubsection{*Adding and Deleting Simplification Rules*}
|
9932
|
58 |
|
|
59 |
text{*
|
|
60 |
If a certain theorem is merely needed in a few proofs by simplification,
|
|
61 |
we do not need to make it a global simplification rule. Instead we can modify
|
|
62 |
the set of simplification rules used in a simplification step by adding rules
|
|
63 |
to it and/or deleting rules from it. The two modifiers for this are
|
|
64 |
\begin{quote}
|
|
65 |
@{text"add:"} \textit{list of theorem names}\\
|
|
66 |
@{text"del:"} \textit{list of theorem names}
|
|
67 |
\end{quote}
|
|
68 |
In case you want to use only a specific list of theorems and ignore all
|
|
69 |
others:
|
|
70 |
\begin{quote}
|
|
71 |
@{text"only:"} \textit{list of theorem names}
|
|
72 |
\end{quote}
|
|
73 |
*}
|
|
74 |
|
|
75 |
subsubsection{*Assumptions*}
|
|
76 |
|
|
77 |
text{*\index{simplification!with/of assumptions}
|
|
78 |
By default, assumptions are part of the simplification process: they are used
|
|
79 |
as simplification rules and are simplified themselves. For example:
|
|
80 |
*}
|
|
81 |
|
10171
|
82 |
lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs";
|
|
83 |
apply simp;
|
|
84 |
done
|
9932
|
85 |
|
|
86 |
text{*\noindent
|
|
87 |
The second assumption simplifies to @{term"xs = []"}, which in turn
|
|
88 |
simplifies the first assumption to @{term"zs = ys"}, thus reducing the
|
|
89 |
conclusion to @{term"ys = ys"} and hence to @{term"True"}.
|
|
90 |
|
|
91 |
In some cases this may be too much of a good thing and may lead to
|
|
92 |
nontermination:
|
|
93 |
*}
|
|
94 |
|
10171
|
95 |
lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []";
|
9932
|
96 |
|
|
97 |
txt{*\noindent
|
|
98 |
cannot be solved by an unmodified application of @{text"simp"} because the
|
|
99 |
simplification rule @{term"f x = g (f (g x))"} extracted from the assumption
|
|
100 |
does not terminate. Isabelle notices certain simple forms of
|
|
101 |
nontermination but not this one. The problem can be circumvented by
|
|
102 |
explicitly telling the simplifier to ignore the assumptions:
|
|
103 |
*}
|
|
104 |
|
10171
|
105 |
apply(simp (no_asm));
|
|
106 |
done
|
9932
|
107 |
|
|
108 |
text{*\noindent
|
10971
|
109 |
There are three modifiers that influence the treatment of assumptions:
|
9932
|
110 |
\begin{description}
|
|
111 |
\item[@{text"(no_asm)"}]\indexbold{*no_asm}
|
|
112 |
means that assumptions are completely ignored.
|
|
113 |
\item[@{text"(no_asm_simp)"}]\indexbold{*no_asm_simp}
|
|
114 |
means that the assumptions are not simplified but
|
|
115 |
are used in the simplification of the conclusion.
|
|
116 |
\item[@{text"(no_asm_use)"}]\indexbold{*no_asm_use}
|
|
117 |
means that the assumptions are simplified but are not
|
|
118 |
used in the simplification of each other or the conclusion.
|
|
119 |
\end{description}
|
10795
|
120 |
Both @{text"(no_asm_simp)"} and @{text"(no_asm_use)"} run forever on
|
|
121 |
the problematic subgoal above.
|
10971
|
122 |
Note that only one of the modifiers is allowed, and it must precede all
|
9932
|
123 |
other arguments.
|
11206
|
124 |
|
|
125 |
\begin{warn}
|
|
126 |
Assumptions are simplified in a left-to-right fashion. If an
|
|
127 |
assumption can help in simplifying one to the left of it, this may get
|
|
128 |
overlooked. In such cases you have to rotate the assumptions explicitly:
|
|
129 |
\isacommand{apply}@{text"(rotate_tac"}~$n$@{text")"}\indexbold{*rotate_tac}
|
|
130 |
causes a cyclic shift by $n$ positions from right to left, if $n$ is
|
|
131 |
positive, and from left to right, if $n$ is negative.
|
|
132 |
Beware that such rotations make proofs quite brittle.
|
|
133 |
\end{warn}
|
9932
|
134 |
*}
|
|
135 |
|
10885
|
136 |
subsubsection{*Rewriting with Definitions*}
|
9932
|
137 |
|
|
138 |
text{*\index{simplification!with definitions}
|
|
139 |
Constant definitions (\S\ref{sec:ConstDefinitions}) can
|
|
140 |
be used as simplification rules, but by default they are not. Hence the
|
|
141 |
simplifier does not expand them automatically, just as it should be:
|
|
142 |
definitions are introduced for the purpose of abbreviating complex
|
|
143 |
concepts. Of course we need to expand the definitions initially to derive
|
|
144 |
enough lemmas that characterize the concept sufficiently for us to forget the
|
|
145 |
original definition. For example, given
|
|
146 |
*}
|
|
147 |
|
10788
|
148 |
constdefs xor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
|
|
149 |
"xor A B \<equiv> (A \<and> \<not>B) \<or> (\<not>A \<and> B)";
|
9932
|
150 |
|
|
151 |
text{*\noindent
|
|
152 |
we may want to prove
|
|
153 |
*}
|
|
154 |
|
10788
|
155 |
lemma "xor A (\<not>A)";
|
9932
|
156 |
|
|
157 |
txt{*\noindent
|
|
158 |
Typically, the opening move consists in \emph{unfolding} the definition(s), which we need to
|
|
159 |
get started, but nothing else:\indexbold{*unfold}\indexbold{definition!unfolding}
|
|
160 |
*}
|
|
161 |
|
10788
|
162 |
apply(simp only:xor_def);
|
9932
|
163 |
|
|
164 |
txt{*\noindent
|
|
165 |
In this particular case, the resulting goal
|
10362
|
166 |
@{subgoals[display,indent=0]}
|
10171
|
167 |
can be proved by simplification. Thus we could have proved the lemma outright by
|
10788
|
168 |
*}(*<*)oops;lemma "xor A (\<not>A)";(*>*)
|
|
169 |
apply(simp add: xor_def)
|
10171
|
170 |
(*<*)done(*>*)
|
9932
|
171 |
text{*\noindent
|
|
172 |
Of course we can also unfold definitions in the middle of a proof.
|
|
173 |
|
|
174 |
You should normally not turn a definition permanently into a simplification
|
10983
|
175 |
rule because this defeats the whole purpose.
|
9932
|
176 |
|
|
177 |
\begin{warn}
|
10971
|
178 |
If you have defined $f\,x\,y~\isasymequiv~t$ then you can only unfold
|
|
179 |
occurrences of $f$ with at least two arguments. This may be helpful for unfolding
|
|
180 |
$f$ selectively, but it may also get in the way. Defining
|
|
181 |
$f$~\isasymequiv~\isasymlambda$x\,y.\;t$ allows to unfold all occurrences of $f$.
|
9932
|
182 |
\end{warn}
|
|
183 |
*}
|
|
184 |
|
10971
|
185 |
subsubsection{*Simplifying {\tt\slshape let}-Expressions*}
|
9932
|
186 |
|
|
187 |
text{*\index{simplification!of let-expressions}
|
|
188 |
Proving a goal containing \isaindex{let}-expressions almost invariably
|
|
189 |
requires the @{text"let"}-con\-structs to be expanded at some point. Since
|
10795
|
190 |
@{text"let"}\ldots\isa{=}\ldots@{text"in"}{\ldots} is just syntactic sugar for a predefined constant
|
9932
|
191 |
(called @{term"Let"}), expanding @{text"let"}-constructs means rewriting with
|
|
192 |
@{thm[source]Let_def}:
|
|
193 |
*}
|
|
194 |
|
|
195 |
lemma "(let xs = [] in xs@ys@xs) = ys";
|
10171
|
196 |
apply(simp add: Let_def);
|
|
197 |
done
|
9932
|
198 |
|
|
199 |
text{*
|
|
200 |
If, in a particular context, there is no danger of a combinatorial explosion
|
|
201 |
of nested @{text"let"}s one could even simlify with @{thm[source]Let_def} by
|
|
202 |
default:
|
|
203 |
*}
|
|
204 |
declare Let_def [simp]
|
|
205 |
|
10885
|
206 |
subsubsection{*Conditional Equations*}
|
9932
|
207 |
|
|
208 |
text{*
|
|
209 |
So far all examples of rewrite rules were equations. The simplifier also
|
|
210 |
accepts \emph{conditional} equations, for example
|
|
211 |
*}
|
|
212 |
|
10171
|
213 |
lemma hd_Cons_tl[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs";
|
|
214 |
apply(case_tac xs, simp, simp);
|
|
215 |
done
|
9932
|
216 |
|
|
217 |
text{*\noindent
|
|
218 |
Note the use of ``\ttindexboldpos{,}{$Isar}'' to string together a
|
|
219 |
sequence of methods. Assuming that the simplification rule
|
|
220 |
@{term"(rev xs = []) = (xs = [])"}
|
|
221 |
is present as well,
|
10795
|
222 |
the lemma below is proved by plain simplification:
|
9932
|
223 |
*}
|
|
224 |
|
10171
|
225 |
lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs";
|
9932
|
226 |
(*<*)
|
|
227 |
by(simp);
|
|
228 |
(*>*)
|
|
229 |
text{*\noindent
|
10795
|
230 |
The conditional equation @{thm[source]hd_Cons_tl} above
|
9932
|
231 |
can simplify @{term"hd(rev xs) # tl(rev xs)"} to @{term"rev xs"}
|
|
232 |
because the corresponding precondition @{term"rev xs ~= []"}
|
|
233 |
simplifies to @{term"xs ~= []"}, which is exactly the local
|
|
234 |
assumption of the subgoal.
|
|
235 |
*}
|
|
236 |
|
|
237 |
|
10885
|
238 |
subsubsection{*Automatic Case Splits*}
|
9932
|
239 |
|
10654
|
240 |
text{*\indexbold{case splits}\index{*split (method, attr.)|(}
|
9932
|
241 |
Goals containing @{text"if"}-expressions are usually proved by case
|
|
242 |
distinction on the condition of the @{text"if"}. For example the goal
|
|
243 |
*}
|
|
244 |
|
10171
|
245 |
lemma "\<forall>xs. if xs = [] then rev xs = [] else rev xs \<noteq> []";
|
9932
|
246 |
|
|
247 |
txt{*\noindent
|
10654
|
248 |
can be split by a special method @{text split}:
|
9932
|
249 |
*}
|
|
250 |
|
10654
|
251 |
apply(split split_if)
|
9932
|
252 |
|
10362
|
253 |
txt{*\noindent
|
|
254 |
@{subgoals[display,indent=0]}
|
10654
|
255 |
where \isaindexbold{split_if} is a theorem that expresses splitting of
|
|
256 |
@{text"if"}s. Because
|
9932
|
257 |
case-splitting on @{text"if"}s is almost always the right proof strategy, the
|
|
258 |
simplifier performs it automatically. Try \isacommand{apply}@{text"(simp)"}
|
|
259 |
on the initial goal above.
|
|
260 |
|
|
261 |
This splitting idea generalizes from @{text"if"} to \isaindex{case}:
|
10654
|
262 |
*}(*<*)by simp(*>*)
|
10171
|
263 |
lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs";
|
10654
|
264 |
apply(split list.split);
|
9932
|
265 |
|
10362
|
266 |
txt{*
|
|
267 |
@{subgoals[display,indent=0]}
|
9932
|
268 |
In contrast to @{text"if"}-expressions, the simplifier does not split
|
|
269 |
@{text"case"}-expressions by default because this can lead to nontermination
|
10654
|
270 |
in case of recursive datatypes. Therefore the simplifier has a modifier
|
|
271 |
@{text split} for adding further splitting rules explicitly. This means the
|
|
272 |
above lemma can be proved in one step by
|
9932
|
273 |
*}
|
10362
|
274 |
(*<*)oops;
|
10171
|
275 |
lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs";
|
9932
|
276 |
(*>*)
|
10171
|
277 |
apply(simp split: list.split);
|
|
278 |
(*<*)done(*>*)
|
10654
|
279 |
text{*\noindent
|
|
280 |
whereas \isacommand{apply}@{text"(simp)"} alone will not succeed.
|
9932
|
281 |
|
|
282 |
In general, every datatype $t$ comes with a theorem
|
|
283 |
$t$@{text".split"} which can be declared to be a \bfindex{split rule} either
|
|
284 |
locally as above, or by giving it the @{text"split"} attribute globally:
|
|
285 |
*}
|
|
286 |
|
|
287 |
declare list.split [split]
|
|
288 |
|
|
289 |
text{*\noindent
|
|
290 |
The @{text"split"} attribute can be removed with the @{text"del"} modifier,
|
|
291 |
either locally
|
|
292 |
*}
|
|
293 |
(*<*)
|
|
294 |
lemma "dummy=dummy";
|
|
295 |
(*>*)
|
|
296 |
apply(simp split del: split_if);
|
|
297 |
(*<*)
|
|
298 |
oops;
|
|
299 |
(*>*)
|
|
300 |
text{*\noindent
|
|
301 |
or globally:
|
|
302 |
*}
|
|
303 |
declare list.split [split del]
|
|
304 |
|
|
305 |
text{*
|
10654
|
306 |
In polished proofs the @{text split} method is rarely used on its own
|
|
307 |
but always as part of the simplifier. However, if a goal contains
|
|
308 |
multiple splittable constructs, the @{text split} method can be
|
|
309 |
helpful in selectively exploring the effects of splitting.
|
|
310 |
|
9932
|
311 |
The above split rules intentionally only affect the conclusion of a
|
|
312 |
subgoal. If you want to split an @{text"if"} or @{text"case"}-expression in
|
|
313 |
the assumptions, you have to apply @{thm[source]split_if_asm} or
|
|
314 |
$t$@{text".split_asm"}:
|
|
315 |
*}
|
|
316 |
|
10654
|
317 |
lemma "if xs = [] then ys \<noteq> [] else ys = [] \<Longrightarrow> xs @ ys \<noteq> []"
|
|
318 |
apply(split split_if_asm)
|
9932
|
319 |
|
10362
|
320 |
txt{*\noindent
|
9932
|
321 |
In contrast to splitting the conclusion, this actually creates two
|
|
322 |
separate subgoals (which are solved by @{text"simp_all"}):
|
10362
|
323 |
@{subgoals[display,indent=0]}
|
9932
|
324 |
If you need to split both in the assumptions and the conclusion,
|
|
325 |
use $t$@{text".splits"} which subsumes $t$@{text".split"} and
|
|
326 |
$t$@{text".split_asm"}. Analogously, there is @{thm[source]if_splits}.
|
|
327 |
|
|
328 |
\begin{warn}
|
|
329 |
The simplifier merely simplifies the condition of an \isa{if} but not the
|
|
330 |
\isa{then} or \isa{else} parts. The latter are simplified only after the
|
|
331 |
condition reduces to \isa{True} or \isa{False}, or after splitting. The
|
|
332 |
same is true for \isaindex{case}-expressions: only the selector is
|
|
333 |
simplified at first, until either the expression reduces to one of the
|
|
334 |
cases or it is split.
|
10654
|
335 |
\end{warn}\index{*split (method, attr.)|)}
|
9932
|
336 |
*}
|
10362
|
337 |
(*<*)
|
|
338 |
by(simp_all)
|
|
339 |
(*>*)
|
9932
|
340 |
|
|
341 |
subsubsection{*Arithmetic*}
|
|
342 |
|
|
343 |
text{*\index{arithmetic}
|
|
344 |
The simplifier routinely solves a small class of linear arithmetic formulae
|
|
345 |
(over type \isa{nat} and other numeric types): it only takes into account
|
10795
|
346 |
assumptions and conclusions that are relations
|
|
347 |
($=$, $\le$, $<$, possibly negated) and it only knows about addition. Thus
|
9932
|
348 |
*}
|
|
349 |
|
10171
|
350 |
lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
|
9932
|
351 |
(*<*)by(auto)(*>*)
|
|
352 |
|
|
353 |
text{*\noindent
|
|
354 |
is proved by simplification, whereas the only slightly more complex
|
|
355 |
*}
|
|
356 |
|
10171
|
357 |
lemma "\<not> m < n \<and> m < n+1 \<Longrightarrow> m = n";
|
9932
|
358 |
(*<*)by(arith)(*>*)
|
|
359 |
|
|
360 |
text{*\noindent
|
|
361 |
is not proved by simplification and requires @{text arith}.
|
|
362 |
*}
|
|
363 |
|
|
364 |
|
|
365 |
subsubsection{*Tracing*}
|
|
366 |
text{*\indexbold{tracing the simplifier}
|
|
367 |
Using the simplifier effectively may take a bit of experimentation. Set the
|
|
368 |
\isaindexbold{trace_simp} \rmindex{flag} to get a better idea of what is going
|
|
369 |
on:
|
|
370 |
*}
|
|
371 |
|
|
372 |
ML "set trace_simp";
|
|
373 |
lemma "rev [a] = []";
|
|
374 |
apply(simp);
|
|
375 |
(*<*)oops(*>*)
|
|
376 |
|
|
377 |
text{*\noindent
|
|
378 |
produces the trace
|
|
379 |
|
|
380 |
\begin{ttbox}\makeatother
|
|
381 |
Applying instance of rewrite rule:
|
|
382 |
rev (?x1 \# ?xs1) == rev ?xs1 @ [?x1]
|
|
383 |
Rewriting:
|
10971
|
384 |
rev [a] == rev [] @ [a]
|
9932
|
385 |
Applying instance of rewrite rule:
|
|
386 |
rev [] == []
|
|
387 |
Rewriting:
|
|
388 |
rev [] == []
|
|
389 |
Applying instance of rewrite rule:
|
|
390 |
[] @ ?y == ?y
|
|
391 |
Rewriting:
|
10971
|
392 |
[] @ [a] == [a]
|
9932
|
393 |
Applying instance of rewrite rule:
|
|
394 |
?x3 \# ?t3 = ?t3 == False
|
|
395 |
Rewriting:
|
10971
|
396 |
[a] = [] == False
|
9932
|
397 |
\end{ttbox}
|
|
398 |
|
|
399 |
In more complicated cases, the trace can be quite lenghty, especially since
|
|
400 |
invocations of the simplifier are often nested (e.g.\ when solving conditions
|
|
401 |
of rewrite rules). Thus it is advisable to reset it:
|
|
402 |
*}
|
|
403 |
|
|
404 |
ML "reset trace_simp";
|
|
405 |
|
|
406 |
(*<*)
|
9922
|
407 |
end
|
9932
|
408 |
(*>*)
|