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