9932
|
1 |
(*<*)
|
9922
|
2 |
theory simp = Main:
|
9932
|
3 |
(*>*)
|
9922
|
4 |
|
9932
|
5 |
subsubsection{*Simplification rules*}
|
|
6 |
|
|
7 |
text{*\indexbold{simplification rule}
|
|
8 |
To facilitate simplification, theorems can be declared to be simplification
|
|
9 |
rules (with the help of the attribute @{text"[simp]"}\index{*simp
|
|
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}
|
|
35 |
Simplification may not terminate, for example if both $f(x) = g(x)$ and
|
|
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 |
|
|
42 |
subsubsection{*The simplification method*}
|
|
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}
|
|
49 |
where the list of \emph{modifiers} helps to fine tune the behaviour and may
|
|
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
|
|
52 |
only the first subgoal and may thus need to be repeated---use
|
|
53 |
\isaindex{simp_all} to simplify all subgoals.
|
|
54 |
Note that @{text simp} fails if nothing changes.
|
|
55 |
*}
|
|
56 |
|
|
57 |
subsubsection{*Adding and deleting simplification rules*}
|
|
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
|
|
109 |
There are three options that influence the treatment of assumptions:
|
|
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}
|
|
120 |
Neither @{text"(no_asm_simp)"} nor @{text"(no_asm_use)"} allow to simplify
|
|
121 |
the above problematic subgoal.
|
|
122 |
|
|
123 |
Note that only one of the above options is allowed, and it must precede all
|
|
124 |
other arguments.
|
|
125 |
*}
|
|
126 |
|
|
127 |
subsubsection{*Rewriting with definitions*}
|
|
128 |
|
|
129 |
text{*\index{simplification!with definitions}
|
|
130 |
Constant definitions (\S\ref{sec:ConstDefinitions}) can
|
|
131 |
be used as simplification rules, but by default they are not. Hence the
|
|
132 |
simplifier does not expand them automatically, just as it should be:
|
|
133 |
definitions are introduced for the purpose of abbreviating complex
|
|
134 |
concepts. Of course we need to expand the definitions initially to derive
|
|
135 |
enough lemmas that characterize the concept sufficiently for us to forget the
|
|
136 |
original definition. For example, given
|
|
137 |
*}
|
|
138 |
|
10171
|
139 |
constdefs exor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
|
|
140 |
"exor A B \<equiv> (A \<and> \<not>B) \<or> (\<not>A \<and> B)";
|
9932
|
141 |
|
|
142 |
text{*\noindent
|
|
143 |
we may want to prove
|
|
144 |
*}
|
|
145 |
|
10171
|
146 |
lemma "exor A (\<not>A)";
|
9932
|
147 |
|
|
148 |
txt{*\noindent
|
|
149 |
Typically, the opening move consists in \emph{unfolding} the definition(s), which we need to
|
|
150 |
get started, but nothing else:\indexbold{*unfold}\indexbold{definition!unfolding}
|
|
151 |
*}
|
|
152 |
|
|
153 |
apply(simp only:exor_def);
|
|
154 |
|
|
155 |
txt{*\noindent
|
|
156 |
In this particular case, the resulting goal
|
10362
|
157 |
@{subgoals[display,indent=0]}
|
10171
|
158 |
can be proved by simplification. Thus we could have proved the lemma outright by
|
|
159 |
*}(*<*)oops;lemma "exor A (\<not>A)";(*>*)
|
|
160 |
apply(simp add: exor_def)
|
|
161 |
(*<*)done(*>*)
|
9932
|
162 |
text{*\noindent
|
|
163 |
Of course we can also unfold definitions in the middle of a proof.
|
|
164 |
|
|
165 |
You should normally not turn a definition permanently into a simplification
|
|
166 |
rule because this defeats the whole purpose of an abbreviation.
|
|
167 |
|
|
168 |
\begin{warn}
|
|
169 |
If you have defined $f\,x\,y~\isasymequiv~t$ then you can only expand
|
|
170 |
occurrences of $f$ with at least two arguments. Thus it is safer to define
|
|
171 |
$f$~\isasymequiv~\isasymlambda$x\,y.\;t$.
|
|
172 |
\end{warn}
|
|
173 |
*}
|
|
174 |
|
|
175 |
subsubsection{*Simplifying let-expressions*}
|
|
176 |
|
|
177 |
text{*\index{simplification!of let-expressions}
|
|
178 |
Proving a goal containing \isaindex{let}-expressions almost invariably
|
|
179 |
requires the @{text"let"}-con\-structs to be expanded at some point. Since
|
|
180 |
@{text"let"}-@{text"in"} is just syntactic sugar for a predefined constant
|
|
181 |
(called @{term"Let"}), expanding @{text"let"}-constructs means rewriting with
|
|
182 |
@{thm[source]Let_def}:
|
|
183 |
*}
|
|
184 |
|
|
185 |
lemma "(let xs = [] in xs@ys@xs) = ys";
|
10171
|
186 |
apply(simp add: Let_def);
|
|
187 |
done
|
9932
|
188 |
|
|
189 |
text{*
|
|
190 |
If, in a particular context, there is no danger of a combinatorial explosion
|
|
191 |
of nested @{text"let"}s one could even simlify with @{thm[source]Let_def} by
|
|
192 |
default:
|
|
193 |
*}
|
|
194 |
declare Let_def [simp]
|
|
195 |
|
|
196 |
subsubsection{*Conditional equations*}
|
|
197 |
|
|
198 |
text{*
|
|
199 |
So far all examples of rewrite rules were equations. The simplifier also
|
|
200 |
accepts \emph{conditional} equations, for example
|
|
201 |
*}
|
|
202 |
|
10171
|
203 |
lemma hd_Cons_tl[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs";
|
|
204 |
apply(case_tac xs, simp, simp);
|
|
205 |
done
|
9932
|
206 |
|
|
207 |
text{*\noindent
|
|
208 |
Note the use of ``\ttindexboldpos{,}{$Isar}'' to string together a
|
|
209 |
sequence of methods. Assuming that the simplification rule
|
|
210 |
@{term"(rev xs = []) = (xs = [])"}
|
|
211 |
is present as well,
|
|
212 |
*}
|
|
213 |
|
10171
|
214 |
lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs";
|
9932
|
215 |
(*<*)
|
|
216 |
by(simp);
|
|
217 |
(*>*)
|
|
218 |
text{*\noindent
|
|
219 |
is proved by plain simplification:
|
|
220 |
the conditional equation @{thm[source]hd_Cons_tl} above
|
|
221 |
can simplify @{term"hd(rev xs) # tl(rev xs)"} to @{term"rev xs"}
|
|
222 |
because the corresponding precondition @{term"rev xs ~= []"}
|
|
223 |
simplifies to @{term"xs ~= []"}, which is exactly the local
|
|
224 |
assumption of the subgoal.
|
|
225 |
*}
|
|
226 |
|
|
227 |
|
|
228 |
subsubsection{*Automatic case splits*}
|
|
229 |
|
|
230 |
text{*\indexbold{case splits}\index{*split|(}
|
|
231 |
Goals containing @{text"if"}-expressions are usually proved by case
|
|
232 |
distinction on the condition of the @{text"if"}. For example the goal
|
|
233 |
*}
|
|
234 |
|
10171
|
235 |
lemma "\<forall>xs. if xs = [] then rev xs = [] else rev xs \<noteq> []";
|
9932
|
236 |
|
|
237 |
txt{*\noindent
|
10362
|
238 |
can be split by a degenerate form of simplification
|
9932
|
239 |
*}
|
|
240 |
|
|
241 |
apply(simp only: split: split_if);
|
|
242 |
|
10362
|
243 |
txt{*\noindent
|
|
244 |
@{subgoals[display,indent=0]}
|
9932
|
245 |
where no simplification rules are included (@{text"only:"} is followed by the
|
|
246 |
empty list of theorems) but the rule \isaindexbold{split_if} for
|
|
247 |
splitting @{text"if"}s is added (via the modifier @{text"split:"}). Because
|
|
248 |
case-splitting on @{text"if"}s is almost always the right proof strategy, the
|
|
249 |
simplifier performs it automatically. Try \isacommand{apply}@{text"(simp)"}
|
|
250 |
on the initial goal above.
|
|
251 |
|
|
252 |
This splitting idea generalizes from @{text"if"} to \isaindex{case}:
|
10362
|
253 |
*}(*<*)oops;(*>*)
|
9932
|
254 |
|
10171
|
255 |
lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs";
|
10362
|
256 |
apply(simp only: split: list.split);
|
9932
|
257 |
|
10362
|
258 |
txt{*
|
|
259 |
@{subgoals[display,indent=0]}
|
9932
|
260 |
In contrast to @{text"if"}-expressions, the simplifier does not split
|
|
261 |
@{text"case"}-expressions by default because this can lead to nontermination
|
|
262 |
in case of recursive datatypes. Again, if the @{text"only:"} modifier is
|
|
263 |
dropped, the above goal is solved,
|
|
264 |
*}
|
10362
|
265 |
(*<*)oops;
|
10171
|
266 |
lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs";
|
9932
|
267 |
(*>*)
|
10171
|
268 |
apply(simp split: list.split);
|
|
269 |
(*<*)done(*>*)
|
9932
|
270 |
text{*\noindent%
|
|
271 |
which \isacommand{apply}@{text"(simp)"} alone will not do.
|
|
272 |
|
|
273 |
In general, every datatype $t$ comes with a theorem
|
|
274 |
$t$@{text".split"} which can be declared to be a \bfindex{split rule} either
|
|
275 |
locally as above, or by giving it the @{text"split"} attribute globally:
|
|
276 |
*}
|
|
277 |
|
|
278 |
declare list.split [split]
|
|
279 |
|
|
280 |
text{*\noindent
|
|
281 |
The @{text"split"} attribute can be removed with the @{text"del"} modifier,
|
|
282 |
either locally
|
|
283 |
*}
|
|
284 |
(*<*)
|
|
285 |
lemma "dummy=dummy";
|
|
286 |
(*>*)
|
|
287 |
apply(simp split del: split_if);
|
|
288 |
(*<*)
|
|
289 |
oops;
|
|
290 |
(*>*)
|
|
291 |
text{*\noindent
|
|
292 |
or globally:
|
|
293 |
*}
|
|
294 |
declare list.split [split del]
|
|
295 |
|
|
296 |
text{*
|
|
297 |
The above split rules intentionally only affect the conclusion of a
|
|
298 |
subgoal. If you want to split an @{text"if"} or @{text"case"}-expression in
|
|
299 |
the assumptions, you have to apply @{thm[source]split_if_asm} or
|
|
300 |
$t$@{text".split_asm"}:
|
|
301 |
*}
|
|
302 |
|
|
303 |
lemma "if xs = [] then ys ~= [] else ys = [] ==> xs @ ys ~= []"
|
|
304 |
apply(simp only: split: split_if_asm);
|
|
305 |
|
10362
|
306 |
txt{*\noindent
|
9932
|
307 |
In contrast to splitting the conclusion, this actually creates two
|
|
308 |
separate subgoals (which are solved by @{text"simp_all"}):
|
10362
|
309 |
@{subgoals[display,indent=0]}
|
9932
|
310 |
If you need to split both in the assumptions and the conclusion,
|
|
311 |
use $t$@{text".splits"} which subsumes $t$@{text".split"} and
|
|
312 |
$t$@{text".split_asm"}. Analogously, there is @{thm[source]if_splits}.
|
|
313 |
|
|
314 |
\begin{warn}
|
|
315 |
The simplifier merely simplifies the condition of an \isa{if} but not the
|
|
316 |
\isa{then} or \isa{else} parts. The latter are simplified only after the
|
|
317 |
condition reduces to \isa{True} or \isa{False}, or after splitting. The
|
|
318 |
same is true for \isaindex{case}-expressions: only the selector is
|
|
319 |
simplified at first, until either the expression reduces to one of the
|
|
320 |
cases or it is split.
|
|
321 |
\end{warn}
|
|
322 |
|
|
323 |
\index{*split|)}
|
|
324 |
*}
|
10362
|
325 |
(*<*)
|
|
326 |
by(simp_all)
|
|
327 |
(*>*)
|
9932
|
328 |
|
|
329 |
|
|
330 |
subsubsection{*Arithmetic*}
|
|
331 |
|
|
332 |
text{*\index{arithmetic}
|
|
333 |
The simplifier routinely solves a small class of linear arithmetic formulae
|
|
334 |
(over type \isa{nat} and other numeric types): it only takes into account
|
|
335 |
assumptions and conclusions that are (possibly negated) (in)equalities
|
|
336 |
(@{text"="}, \isasymle, @{text"<"}) and it only knows about addition. Thus
|
|
337 |
*}
|
|
338 |
|
10171
|
339 |
lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
|
9932
|
340 |
(*<*)by(auto)(*>*)
|
|
341 |
|
|
342 |
text{*\noindent
|
|
343 |
is proved by simplification, whereas the only slightly more complex
|
|
344 |
*}
|
|
345 |
|
10171
|
346 |
lemma "\<not> m < n \<and> m < n+1 \<Longrightarrow> m = n";
|
9932
|
347 |
(*<*)by(arith)(*>*)
|
|
348 |
|
|
349 |
text{*\noindent
|
|
350 |
is not proved by simplification and requires @{text arith}.
|
|
351 |
*}
|
|
352 |
|
|
353 |
|
|
354 |
subsubsection{*Tracing*}
|
|
355 |
text{*\indexbold{tracing the simplifier}
|
|
356 |
Using the simplifier effectively may take a bit of experimentation. Set the
|
|
357 |
\isaindexbold{trace_simp} \rmindex{flag} to get a better idea of what is going
|
|
358 |
on:
|
|
359 |
*}
|
|
360 |
|
|
361 |
ML "set trace_simp";
|
|
362 |
lemma "rev [a] = []";
|
|
363 |
apply(simp);
|
|
364 |
(*<*)oops(*>*)
|
|
365 |
|
|
366 |
text{*\noindent
|
|
367 |
produces the trace
|
|
368 |
|
|
369 |
\begin{ttbox}\makeatother
|
|
370 |
Applying instance of rewrite rule:
|
|
371 |
rev (?x1 \# ?xs1) == rev ?xs1 @ [?x1]
|
|
372 |
Rewriting:
|
|
373 |
rev [x] == rev [] @ [x]
|
|
374 |
Applying instance of rewrite rule:
|
|
375 |
rev [] == []
|
|
376 |
Rewriting:
|
|
377 |
rev [] == []
|
|
378 |
Applying instance of rewrite rule:
|
|
379 |
[] @ ?y == ?y
|
|
380 |
Rewriting:
|
|
381 |
[] @ [x] == [x]
|
|
382 |
Applying instance of rewrite rule:
|
|
383 |
?x3 \# ?t3 = ?t3 == False
|
|
384 |
Rewriting:
|
|
385 |
[x] = [] == False
|
|
386 |
\end{ttbox}
|
|
387 |
|
|
388 |
In more complicated cases, the trace can be quite lenghty, especially since
|
|
389 |
invocations of the simplifier are often nested (e.g.\ when solving conditions
|
|
390 |
of rewrite rules). Thus it is advisable to reset it:
|
|
391 |
*}
|
|
392 |
|
|
393 |
ML "reset trace_simp";
|
|
394 |
|
|
395 |
(*<*)
|
9922
|
396 |
end
|
9932
|
397 |
(*>*)
|