3 \def\isabellecontext{simp}%
5 \isamarkupsubsubsection{Simplification rules}
8 \indexbold{simplification rule}
9 To facilitate simplification, theorems can be declared to be simplification
10 rules (with the help of the attribute \isa{{\isacharbrackleft}simp{\isacharbrackright}}\index{*simp
11 (attribute)}), in which case proofs by simplification make use of these
12 rules automatically. In addition the constructs \isacommand{datatype} and
13 \isacommand{primrec} (and a few others) invisibly declare useful
14 simplification rules. Explicit definitions are \emph{not} declared
15 simplification rules automatically!
17 Not merely equations but pretty much any theorem can become a simplification
18 rule. The simplifier will try to make sense of it. For example, a theorem
19 \isa{{\isasymnot}\ P} is automatically turned into \isa{P\ {\isacharequal}\ False}. The details
20 are explained in \S\ref{sec:SimpHow}.
22 The simplification attribute of theorems can be turned on and off as follows:
24 \isacommand{declare} \textit{theorem-name}\isa{{\isacharbrackleft}simp{\isacharbrackright}}\\
25 \isacommand{declare} \textit{theorem-name}\isa{{\isacharbrackleft}simp\ del{\isacharbrackright}}
27 As a rule of thumb, equations that really simplify (like \isa{rev\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs} and \isa{xs\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ 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.
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.
42 \isamarkupsubsubsection{The simplification method}
44 \begin{isamarkuptext}%
45 \index{*simp (method)|bold}
46 The general format of the simplification method is
48 \isa{simp} \textit{list of modifiers}
50 where the list of \emph{modifiers} helps to fine tune the behaviour and may
51 be empty. Most if not all of the proofs seen so far could have been performed
52 with \isa{simp} instead of \isa{auto}, except that \isa{simp} attacks
53 only the first subgoal and may thus need to be repeated---use
54 \isaindex{simp_all} to simplify all subgoals.
55 Note that \isa{simp} fails if nothing changes.%
58 \isamarkupsubsubsection{Adding and deleting simplification rules}
60 \begin{isamarkuptext}%
61 If a certain theorem is merely needed in a few proofs by simplification,
62 we do not need to make it a global simplification rule. Instead we can modify
63 the set of simplification rules used in a simplification step by adding rules
64 to it and/or deleting rules from it. The two modifiers for this are
66 \isa{add{\isacharcolon}} \textit{list of theorem names}\\
67 \isa{del{\isacharcolon}} \textit{list of theorem names}
69 In case you want to use only a specific list of theorems and ignore all
72 \isa{only{\isacharcolon}} \textit{list of theorem names}
76 \isamarkupsubsubsection{Assumptions}
78 \begin{isamarkuptext}%
79 \index{simplification!with/of assumptions}
80 By default, assumptions are part of the simplification process: they are used
81 as simplification rules and are simplified themselves. For example:%
83 \isacommand{lemma}\ {\isachardoublequote}{\isasymlbrakk}\ xs\ {\isacharat}\ zs\ {\isacharequal}\ ys\ {\isacharat}\ xs{\isacharsemicolon}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ ys\ {\isacharequal}\ zs{\isachardoublequote}\isanewline
84 \isacommand{apply}\ simp\isanewline
86 \begin{isamarkuptext}%
88 The second assumption simplifies to \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}}, which in turn
89 simplifies the first assumption to \isa{zs\ {\isacharequal}\ ys}, thus reducing the
90 conclusion to \isa{ys\ {\isacharequal}\ ys} and hence to \isa{True}.
92 In some cases this may be too much of a good thing and may lead to
95 \isacommand{lemma}\ {\isachardoublequote}{\isasymforall}x{\isachardot}\ f\ x\ {\isacharequal}\ g\ {\isacharparenleft}f\ {\isacharparenleft}g\ x{\isacharparenright}{\isacharparenright}\ {\isasymLongrightarrow}\ f\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ f\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}%
98 cannot be solved by an unmodified application of \isa{simp} because the
99 simplification rule \isa{f\ x\ {\isacharequal}\ g\ {\isacharparenleft}f\ {\isacharparenleft}g\ x{\isacharparenright}{\isacharparenright}} 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:%
104 \isacommand{apply}{\isacharparenleft}simp\ {\isacharparenleft}no{\isacharunderscore}asm{\isacharparenright}{\isacharparenright}\isanewline
106 \begin{isamarkuptext}%
108 There are three options that influence the treatment of assumptions:
110 \item[\isa{{\isacharparenleft}no{\isacharunderscore}asm{\isacharparenright}}]\indexbold{*no_asm}
111 means that assumptions are completely ignored.
112 \item[\isa{{\isacharparenleft}no{\isacharunderscore}asm{\isacharunderscore}simp{\isacharparenright}}]\indexbold{*no_asm_simp}
113 means that the assumptions are not simplified but
114 are used in the simplification of the conclusion.
115 \item[\isa{{\isacharparenleft}no{\isacharunderscore}asm{\isacharunderscore}use{\isacharparenright}}]\indexbold{*no_asm_use}
116 means that the assumptions are simplified but are not
117 used in the simplification of each other or the conclusion.
119 Neither \isa{{\isacharparenleft}no{\isacharunderscore}asm{\isacharunderscore}simp{\isacharparenright}} nor \isa{{\isacharparenleft}no{\isacharunderscore}asm{\isacharunderscore}use{\isacharparenright}} allow to simplify
120 the above problematic subgoal.
122 Note that only one of the above options is allowed, and it must precede all
126 \isamarkupsubsubsection{Rewriting with definitions}
128 \begin{isamarkuptext}%
129 \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%
138 \isacommand{constdefs}\ exor\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}bool\ {\isasymRightarrow}\ bool\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
139 \ \ \ \ \ \ \ \ \ {\isachardoublequote}exor\ A\ B\ {\isasymequiv}\ {\isacharparenleft}A\ {\isasymand}\ {\isasymnot}B{\isacharparenright}\ {\isasymor}\ {\isacharparenleft}{\isasymnot}A\ {\isasymand}\ B{\isacharparenright}{\isachardoublequote}%
140 \begin{isamarkuptext}%
142 we may want to prove%
144 \isacommand{lemma}\ {\isachardoublequote}exor\ A\ {\isacharparenleft}{\isasymnot}A{\isacharparenright}{\isachardoublequote}%
145 \begin{isamarkuptxt}%
147 Typically, the opening move consists in \emph{unfolding} the definition(s), which we need to
148 get started, but nothing else:\indexbold{*unfold}\indexbold{definition!unfolding}%
150 \isacommand{apply}{\isacharparenleft}simp\ only{\isacharcolon}exor{\isacharunderscore}def{\isacharparenright}%
151 \begin{isamarkuptxt}%
153 In this particular case, the resulting goal
155 ~1.~A~{\isasymand}~{\isasymnot}~{\isasymnot}~A~{\isasymor}~{\isasymnot}~A~{\isasymand}~{\isasymnot}~A%
157 can be proved by simplification. Thus we could have proved the lemma outright by%
159 \isacommand{apply}{\isacharparenleft}simp\ add{\isacharcolon}\ exor{\isacharunderscore}def{\isacharparenright}%
160 \begin{isamarkuptext}%
162 Of course we can also unfold definitions in the middle of a proof.
164 You should normally not turn a definition permanently into a simplification
165 rule because this defeats the whole purpose of an abbreviation.
168 If you have defined $f\,x\,y~\isasymequiv~t$ then you can only expand
169 occurrences of $f$ with at least two arguments. Thus it is safer to define
170 $f$~\isasymequiv~\isasymlambda$x\,y.\;t$.
174 \isamarkupsubsubsection{Simplifying let-expressions}
176 \begin{isamarkuptext}%
177 \index{simplification!of let-expressions}
178 Proving a goal containing \isaindex{let}-expressions almost invariably
179 requires the \isa{let}-con\-structs to be expanded at some point. Since
180 \isa{let}-\isa{in} is just syntactic sugar for a predefined constant
181 (called \isa{Let}), expanding \isa{let}-constructs means rewriting with
182 \isa{Let{\isacharunderscore}def}:%
184 \isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}let\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ in\ xs{\isacharat}ys{\isacharat}xs{\isacharparenright}\ {\isacharequal}\ ys{\isachardoublequote}\isanewline
185 \isacommand{apply}{\isacharparenleft}simp\ add{\isacharcolon}\ Let{\isacharunderscore}def{\isacharparenright}\isanewline
187 \begin{isamarkuptext}%
188 If, in a particular context, there is no danger of a combinatorial explosion
189 of nested \isa{let}s one could even simlify with \isa{Let{\isacharunderscore}def} by
192 \isacommand{declare}\ Let{\isacharunderscore}def\ {\isacharbrackleft}simp{\isacharbrackright}%
193 \isamarkupsubsubsection{Conditional equations}
195 \begin{isamarkuptext}%
196 So far all examples of rewrite rules were equations. The simplifier also
197 accepts \emph{conditional} equations, for example%
199 \isacommand{lemma}\ hd{\isacharunderscore}Cons{\isacharunderscore}tl{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}xs\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}\ \ {\isasymLongrightarrow}\ \ hd\ xs\ {\isacharhash}\ tl\ xs\ {\isacharequal}\ xs{\isachardoublequote}\isanewline
200 \isacommand{apply}{\isacharparenleft}case{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharcomma}\ simp{\isacharparenright}\isanewline
202 \begin{isamarkuptext}%
204 Note the use of ``\ttindexboldpos{,}{$Isar}'' to string together a
205 sequence of methods. Assuming that the simplification rule
206 \isa{{\isacharparenleft}rev\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}}
209 \isacommand{lemma}\ {\isachardoublequote}xs\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymLongrightarrow}\ hd{\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharhash}\ tl{\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ rev\ xs{\isachardoublequote}%
210 \begin{isamarkuptext}%
212 is proved by plain simplification:
213 the conditional equation \isa{hd{\isacharunderscore}Cons{\isacharunderscore}tl} above
214 can simplify \isa{hd\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharhash}\ tl\ {\isacharparenleft}rev\ xs{\isacharparenright}} to \isa{rev\ xs}
215 because the corresponding precondition \isa{rev\ xs\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}}
216 simplifies to \isa{xs\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}}, which is exactly the local
217 assumption of the subgoal.%
220 \isamarkupsubsubsection{Automatic case splits}
222 \begin{isamarkuptext}%
223 \indexbold{case splits}\index{*split|(}
224 Goals containing \isa{if}-expressions are usually proved by case
225 distinction on the condition of the \isa{if}. For example the goal%
227 \isacommand{lemma}\ {\isachardoublequote}{\isasymforall}xs{\isachardot}\ if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ rev\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ else\ rev\ xs\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}%
228 \begin{isamarkuptxt}%
232 ~1.~{\isasymforall}xs.~(xs~=~[]~{\isasymlongrightarrow}~rev~xs~=~[])~{\isasymand}~(xs~{\isasymnoteq}~[]~{\isasymlongrightarrow}~rev~xs~{\isasymnoteq}~[])
234 by a degenerate form of simplification%
236 \isacommand{apply}{\isacharparenleft}simp\ only{\isacharcolon}\ split{\isacharcolon}\ split{\isacharunderscore}if{\isacharparenright}%
237 \begin{isamarkuptext}%
239 where no simplification rules are included (\isa{only{\isacharcolon}} is followed by the
240 empty list of theorems) but the rule \isaindexbold{split_if} for
241 splitting \isa{if}s is added (via the modifier \isa{split{\isacharcolon}}). Because
242 case-splitting on \isa{if}s is almost always the right proof strategy, the
243 simplifier performs it automatically. Try \isacommand{apply}\isa{{\isacharparenleft}simp{\isacharparenright}}
244 on the initial goal above.
246 This splitting idea generalizes from \isa{if} to \isaindex{case}:%
248 \isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}case\ xs\ of\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymRightarrow}\ zs\ {\isacharbar}\ y{\isacharhash}ys\ {\isasymRightarrow}\ y{\isacharhash}{\isacharparenleft}ys{\isacharat}zs{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ xs{\isacharat}zs{\isachardoublequote}%
249 \begin{isamarkuptxt}%
252 \begin{isabelle}\makeatother
253 ~1.~(xs~=~[]~{\isasymlongrightarrow}~zs~=~xs~@~zs)~{\isasymand}\isanewline
254 ~~~~({\isasymforall}a~list.~xs~=~a~\#~list~{\isasymlongrightarrow}~a~\#~list~@~zs~=~xs~@~zs)
258 \isacommand{apply}{\isacharparenleft}simp\ only{\isacharcolon}\ split{\isacharcolon}\ list{\isachardot}split{\isacharparenright}%
259 \begin{isamarkuptext}%
261 In contrast to \isa{if}-expressions, the simplifier does not split
262 \isa{case}-expressions by default because this can lead to nontermination
263 in case of recursive datatypes. Again, if the \isa{only{\isacharcolon}} modifier is
264 dropped, the above goal is solved,%
266 \isacommand{apply}{\isacharparenleft}simp\ split{\isacharcolon}\ list{\isachardot}split{\isacharparenright}%
267 \begin{isamarkuptext}%
269 which \isacommand{apply}\isa{{\isacharparenleft}simp{\isacharparenright}} alone will not do.
271 In general, every datatype $t$ comes with a theorem
272 $t$\isa{{\isachardot}split} which can be declared to be a \bfindex{split rule} either
273 locally as above, or by giving it the \isa{split} attribute globally:%
275 \isacommand{declare}\ list{\isachardot}split\ {\isacharbrackleft}split{\isacharbrackright}%
276 \begin{isamarkuptext}%
278 The \isa{split} attribute can be removed with the \isa{del} modifier,
281 \isacommand{apply}{\isacharparenleft}simp\ split\ del{\isacharcolon}\ split{\isacharunderscore}if{\isacharparenright}%
282 \begin{isamarkuptext}%
286 \isacommand{declare}\ list{\isachardot}split\ {\isacharbrackleft}split\ del{\isacharbrackright}%
287 \begin{isamarkuptext}%
288 The above split rules intentionally only affect the conclusion of a
289 subgoal. If you want to split an \isa{if} or \isa{case}-expression in
290 the assumptions, you have to apply \isa{split{\isacharunderscore}if{\isacharunderscore}asm} or
291 $t$\isa{{\isachardot}split{\isacharunderscore}asm}:%
293 \isacommand{lemma}\ {\isachardoublequote}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ ys\ {\isachartilde}{\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ else\ ys\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}{\isacharequal}{\isachargreater}\ xs\ {\isacharat}\ ys\ {\isachartilde}{\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}\isanewline
294 \isacommand{apply}{\isacharparenleft}simp\ only{\isacharcolon}\ split{\isacharcolon}\ split{\isacharunderscore}if{\isacharunderscore}asm{\isacharparenright}%
295 \begin{isamarkuptext}%
297 In contrast to splitting the conclusion, this actually creates two
298 separate subgoals (which are solved by \isa{simp{\isacharunderscore}all}):
300 \ \isadigit{1}{\isachardot}\ {\isasymlbrakk}\mbox{xs}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\ \mbox{ys}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharat}\ \mbox{ys}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}\isanewline
301 \ \isadigit{2}{\isachardot}\ {\isasymlbrakk}\mbox{xs}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}{\isacharsemicolon}\ \mbox{ys}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isasymrbrakk}\ {\isasymLongrightarrow}\ \mbox{xs}\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymnoteq}\ {\isacharbrackleft}{\isacharbrackright}
303 If you need to split both in the assumptions and the conclusion,
304 use $t$\isa{{\isachardot}splits} which subsumes $t$\isa{{\isachardot}split} and
305 $t$\isa{{\isachardot}split{\isacharunderscore}asm}. Analogously, there is \isa{if{\isacharunderscore}splits}.
308 The simplifier merely simplifies the condition of an \isa{if} but not the
309 \isa{then} or \isa{else} parts. The latter are simplified only after the
310 condition reduces to \isa{True} or \isa{False}, or after splitting. The
311 same is true for \isaindex{case}-expressions: only the selector is
312 simplified at first, until either the expression reduces to one of the
313 cases or it is split.
319 \isamarkupsubsubsection{Arithmetic}
321 \begin{isamarkuptext}%
323 The simplifier routinely solves a small class of linear arithmetic formulae
324 (over type \isa{nat} and other numeric types): it only takes into account
325 assumptions and conclusions that are (possibly negated) (in)equalities
326 (\isa{{\isacharequal}}, \isasymle, \isa{{\isacharless}}) and it only knows about addition. Thus%
328 \isacommand{lemma}\ {\isachardoublequote}{\isasymlbrakk}\ {\isasymnot}\ m\ {\isacharless}\ n{\isacharsemicolon}\ m\ {\isacharless}\ n{\isacharplus}\isadigit{1}\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ m\ {\isacharequal}\ n{\isachardoublequote}%
329 \begin{isamarkuptext}%
331 is proved by simplification, whereas the only slightly more complex%
333 \isacommand{lemma}\ {\isachardoublequote}{\isasymnot}\ m\ {\isacharless}\ n\ {\isasymand}\ m\ {\isacharless}\ n{\isacharplus}\isadigit{1}\ {\isasymLongrightarrow}\ m\ {\isacharequal}\ n{\isachardoublequote}%
334 \begin{isamarkuptext}%
336 is not proved by simplification and requires \isa{arith}.%
339 \isamarkupsubsubsection{Tracing}
341 \begin{isamarkuptext}%
342 \indexbold{tracing the simplifier}
343 Using the simplifier effectively may take a bit of experimentation. Set the
344 \isaindexbold{trace_simp} \rmindex{flag} to get a better idea of what is going
347 \isacommand{ML}\ {\isachardoublequote}set\ trace{\isacharunderscore}simp{\isachardoublequote}\isanewline
348 \isacommand{lemma}\ {\isachardoublequote}rev\ {\isacharbrackleft}a{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}\isanewline
349 \isacommand{apply}{\isacharparenleft}simp{\isacharparenright}%
350 \begin{isamarkuptext}%
354 \begin{ttbox}\makeatother
355 Applying instance of rewrite rule:
356 rev (?x1 \# ?xs1) == rev ?xs1 @ [?x1]
358 rev [x] == rev [] @ [x]
359 Applying instance of rewrite rule:
363 Applying instance of rewrite rule:
367 Applying instance of rewrite rule:
368 ?x3 \# ?t3 = ?t3 == False
373 In more complicated cases, the trace can be quite lenghty, especially since
374 invocations of the simplifier are often nested (e.g.\ when solving conditions
375 of rewrite rules). Thus it is advisable to reset it:%
377 \isacommand{ML}\ {\isachardoublequote}reset\ trace{\isacharunderscore}simp{\isachardoublequote}\isanewline
381 %%% TeX-master: "root"