| author | wenzelm | 
| Tue, 18 May 2021 21:09:51 +0200 | |
| changeset 73734 | f7f0d516df0c | 
| parent 69597 | ff784d5a5bfb | 
| permissions | -rw-r--r-- | 
| 9932 | 1 | (*<*) | 
| 16417 | 2 | theory simp imports Main begin | 
| 9932 | 3 | (*>*) | 
| 9922 | 4 | |
| 67406 | 5 | subsection\<open>Simplification Rules\<close> | 
| 9932 | 6 | |
| 67406 | 7 | text\<open>\index{simplification rules}
 | 
| 11458 | 8 | To facilitate simplification, | 
| 69505 | 9 | the attribute \<open>[simp]\<close>\index{*simp (attribute)}
 | 
| 11458 | 10 | declares theorems to be simplification rules, which the simplifier | 
| 11 | will use automatically.  In addition, \isacommand{datatype} and
 | |
| 12 | \isacommand{primrec} declarations (and a few others) 
 | |
| 13 | implicitly declare some simplification rules. | |
| 14 | Explicit definitions are \emph{not} declared as 
 | |
| 9932 | 15 | simplification rules automatically! | 
| 16 | ||
| 11458 | 17 | Nearly any theorem can become a simplification | 
| 18 | rule. The simplifier will try to transform it into an equation. | |
| 19 | For example, the theorem | |
| 69597 | 20 | \<^prop>\<open>~P\<close> is turned into \<^prop>\<open>P = False\<close>. The details | 
| 9932 | 21 | are explained in \S\ref{sec:SimpHow}.
 | 
| 22 | ||
| 11458 | 23 | The simplification attribute of theorems can be turned on and off:% | 
| 12489 | 24 | \index{*simp del (attribute)}
 | 
| 9932 | 25 | \begin{quote}
 | 
| 69505 | 26 | \isacommand{declare} \textit{theorem-name}\<open>[simp]\<close>\\
 | 
| 27 | \isacommand{declare} \textit{theorem-name}\<open>[simp del]\<close>
 | |
| 9932 | 28 | \end{quote}
 | 
| 11309 | 29 | Only equations that really simplify, like \isa{rev\
 | 
| 30 | {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ xs} and
 | |
| 31 | \isa{xs\ {\isacharat}\ {\isacharbrackleft}{\isacharbrackright}\
 | |
| 32 | {\isacharequal}\ xs}, should be declared as default simplification rules. 
 | |
| 33 | More specific ones should only be used selectively and should | |
| 34 | not be made default. Distributivity laws, for example, alter | |
| 35 | the structure of terms and can produce an exponential blow-up instead of | |
| 36 | simplification. A default simplification rule may | |
| 37 | need to be disabled in certain proofs. Frequent changes in the simplification | |
| 38 | status of a theorem may indicate an unwise use of defaults. | |
| 9932 | 39 | \begin{warn}
 | 
| 11458 | 40 | Simplification can run forever, for example if both $f(x) = g(x)$ and | 
| 9932 | 41 | $g(x) = f(x)$ are simplification rules. It is the user's responsibility not | 
| 42 | to include simplification rules that can lead to nontermination, either on | |
| 43 | their own or in combination with other simplification rules. | |
| 44 | \end{warn}
 | |
| 12332 | 45 | \begin{warn}
 | 
| 46 | It is inadvisable to toggle the simplification attribute of a | |
| 47 | theorem from a parent theory $A$ in a child theory $B$ for good. | |
| 48 | The reason is that if some theory $C$ is based both on $B$ and (via a | |
| 13814 | 49 | different path) on $A$, it is not defined what the simplification attribute | 
| 12332 | 50 | of that theorem will be in $C$: it could be either. | 
| 51 | \end{warn}
 | |
| 67406 | 52 | \<close> | 
| 9932 | 53 | |
| 67406 | 54 | subsection\<open>The {\tt\slshape simp}  Method\<close>
 | 
| 9932 | 55 | |
| 67406 | 56 | text\<open>\index{*simp (method)|bold}
 | 
| 9932 | 57 | The general format of the simplification method is | 
| 58 | \begin{quote}
 | |
| 69505 | 59 | \<open>simp\<close> \textit{list of modifiers}
 | 
| 9932 | 60 | \end{quote}
 | 
| 10795 | 61 | where the list of \emph{modifiers} fine tunes the behaviour and may
 | 
| 11309 | 62 | be empty. Specific modifiers are discussed below. Most if not all of the | 
| 63 | proofs seen so far could have been performed | |
| 69505 | 64 | with \<open>simp\<close> instead of \isa{auto}, except that \<open>simp\<close> attacks
 | 
| 10971 | 65 | only the first subgoal and may thus need to be repeated --- use | 
| 11428 | 66 | \methdx{simp_all} to simplify all subgoals.
 | 
| 69505 | 67 | If nothing changes, \<open>simp\<close> fails. | 
| 67406 | 68 | \<close> | 
| 9932 | 69 | |
| 67406 | 70 | subsection\<open>Adding and Deleting Simplification Rules\<close> | 
| 9932 | 71 | |
| 67406 | 72 | text\<open> | 
| 11458 | 73 | \index{simplification rules!adding and deleting}%
 | 
| 9932 | 74 | If a certain theorem is merely needed in a few proofs by simplification, | 
| 75 | we do not need to make it a global simplification rule. Instead we can modify | |
| 76 | the set of simplification rules used in a simplification step by adding rules | |
| 77 | to it and/or deleting rules from it. The two modifiers for this are | |
| 78 | \begin{quote}
 | |
| 69505 | 79 | \<open>add:\<close> \textit{list of theorem names}\index{*add (modifier)}\\
 | 
| 80 | \<open>del:\<close> \textit{list of theorem names}\index{*del (modifier)}
 | |
| 9932 | 81 | \end{quote}
 | 
| 11458 | 82 | Or you can use a specific list of theorems and omit all others: | 
| 9932 | 83 | \begin{quote}
 | 
| 69505 | 84 | \<open>only:\<close> \textit{list of theorem names}\index{*only (modifier)}
 | 
| 9932 | 85 | \end{quote}
 | 
| 11309 | 86 | In this example, we invoke the simplifier, adding two distributive | 
| 87 | laws: | |
| 88 | \begin{quote}
 | |
| 69505 | 89 | \isacommand{apply}\<open>(simp add: mod_mult_distrib add_mult_distrib)\<close>
 | 
| 11309 | 90 | \end{quote}
 | 
| 67406 | 91 | \<close> | 
| 9932 | 92 | |
| 67406 | 93 | subsection\<open>Assumptions\<close> | 
| 9932 | 94 | |
| 67406 | 95 | text\<open>\index{simplification!with/of assumptions}
 | 
| 9932 | 96 | By default, assumptions are part of the simplification process: they are used | 
| 97 | as simplification rules and are simplified themselves. For example: | |
| 67406 | 98 | \<close> | 
| 9932 | 99 | |
| 12631 | 100 | lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs" | 
| 101 | apply simp | |
| 10171 | 102 | done | 
| 9932 | 103 | |
| 67406 | 104 | text\<open>\noindent | 
| 69597 | 105 | The second assumption simplifies to \<^term>\<open>xs = []\<close>, which in turn | 
| 106 | simplifies the first assumption to \<^term>\<open>zs = ys\<close>, thus reducing the | |
| 107 | conclusion to \<^term>\<open>ys = ys\<close> and hence to \<^term>\<open>True\<close>. | |
| 9932 | 108 | |
| 11458 | 109 | In some cases, using the assumptions can lead to nontermination: | 
| 67406 | 110 | \<close> | 
| 9932 | 111 | |
| 12631 | 112 | lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []" | 
| 9932 | 113 | |
| 67406 | 114 | txt\<open>\noindent | 
| 69505 | 115 | An unmodified application of \<open>simp\<close> loops. The culprit is the | 
| 69597 | 116 | simplification rule \<^term>\<open>f x = g (f (g x))\<close>, which is extracted from | 
| 11458 | 117 | the assumption. (Isabelle notices certain simple forms of | 
| 118 | nontermination but not this one.) The problem can be circumvented by | |
| 119 | telling the simplifier to ignore the assumptions: | |
| 67406 | 120 | \<close> | 
| 9932 | 121 | |
| 12631 | 122 | apply(simp (no_asm)) | 
| 10171 | 123 | done | 
| 9932 | 124 | |
| 67406 | 125 | text\<open>\noindent | 
| 11458 | 126 | Three modifiers influence the treatment of assumptions: | 
| 9932 | 127 | \begin{description}
 | 
| 69505 | 128 | \item[\<open>(no_asm)\<close>]\index{*no_asm (modifier)}
 | 
| 9932 | 129 | means that assumptions are completely ignored. | 
| 69505 | 130 | \item[\<open>(no_asm_simp)\<close>]\index{*no_asm_simp (modifier)}
 | 
| 9932 | 131 | means that the assumptions are not simplified but | 
| 132 | are used in the simplification of the conclusion. | |
| 69505 | 133 | \item[\<open>(no_asm_use)\<close>]\index{*no_asm_use (modifier)}
 | 
| 9932 | 134 | means that the assumptions are simplified but are not | 
| 135 | used in the simplification of each other or the conclusion. | |
| 136 | \end{description}
 | |
| 11458 | 137 | Only one of the modifiers is allowed, and it must precede all | 
| 11309 | 138 | other modifiers. | 
| 13623 | 139 | %\begin{warn}
 | 
| 140 | %Assumptions are simplified in a left-to-right fashion. If an | |
| 141 | %assumption can help in simplifying one to the left of it, this may get | |
| 142 | %overlooked. In such cases you have to rotate the assumptions explicitly: | |
| 143 | %\isacommand{apply}@ {text"("}\methdx{rotate_tac}~$n$@ {text")"}
 | |
| 144 | %causes a cyclic shift by $n$ positions from right to left, if $n$ is | |
| 145 | %positive, and from left to right, if $n$ is negative. | |
| 146 | %Beware that such rotations make proofs quite brittle. | |
| 147 | %\end{warn}
 | |
| 67406 | 148 | \<close> | 
| 9932 | 149 | |
| 67406 | 150 | subsection\<open>Rewriting with Definitions\<close> | 
| 9932 | 151 | |
| 67406 | 152 | text\<open>\label{sec:Simp-with-Defs}\index{simplification!with definitions}
 | 
| 11458 | 153 | Constant definitions (\S\ref{sec:ConstDefinitions}) can be used as
 | 
| 154 | simplification rules, but by default they are not: the simplifier does not | |
| 155 | expand them automatically. Definitions are intended for introducing abstract | |
| 12582 
b85acd66f715
removed Misc/Translations (text covered by Documents.thy);
 wenzelm parents: 
12533diff
changeset | 156 | concepts and not merely as abbreviations. Of course, we need to expand | 
| 11458 | 157 | the definition initially, but once we have proved enough abstract properties | 
| 158 | of the new constant, we can forget its original definition. This style makes | |
| 159 | proofs more robust: if the definition has to be changed, | |
| 160 | only the proofs of the abstract properties will be affected. | |
| 161 | ||
| 67406 | 162 | For example, given\<close> | 
| 9932 | 163 | |
| 27027 | 164 | definition xor :: "bool \<Rightarrow> bool \<Rightarrow> bool" where | 
| 165 | "xor A B \<equiv> (A \<and> \<not>B) \<or> (\<not>A \<and> B)" | |
| 9932 | 166 | |
| 67406 | 167 | text\<open>\noindent | 
| 9932 | 168 | we may want to prove | 
| 67406 | 169 | \<close> | 
| 9932 | 170 | |
| 12631 | 171 | lemma "xor A (\<not>A)" | 
| 9932 | 172 | |
| 67406 | 173 | txt\<open>\noindent | 
| 11428 | 174 | Typically, we begin by unfolding some definitions: | 
| 175 | \indexbold{definitions!unfolding}
 | |
| 67406 | 176 | \<close> | 
| 9932 | 177 | |
| 12631 | 178 | apply(simp only: xor_def) | 
| 9932 | 179 | |
| 67406 | 180 | txt\<open>\noindent | 
| 9932 | 181 | In this particular case, the resulting goal | 
| 10362 | 182 | @{subgoals[display,indent=0]}
 | 
| 10171 | 183 | can be proved by simplification. Thus we could have proved the lemma outright by | 
| 67406 | 184 | \<close>(*<*)oops lemma "xor A (\<not>A)"(*>*) | 
| 10788 | 185 | apply(simp add: xor_def) | 
| 10171 | 186 | (*<*)done(*>*) | 
| 67406 | 187 | text\<open>\noindent | 
| 9932 | 188 | Of course we can also unfold definitions in the middle of a proof. | 
| 189 | ||
| 190 | \begin{warn}
 | |
| 10971 | 191 | If you have defined $f\,x\,y~\isasymequiv~t$ then you can only unfold | 
| 192 | occurrences of $f$ with at least two arguments. This may be helpful for unfolding | |
| 193 | $f$ selectively, but it may also get in the way. Defining | |
| 194 | $f$~\isasymequiv~\isasymlambda$x\,y.\;t$ allows to unfold all occurrences of $f$. | |
| 9932 | 195 | \end{warn}
 | 
| 12473 | 196 | |
| 197 | There is also the special method \isa{unfold}\index{*unfold (method)|bold}
 | |
| 198 | which merely unfolds | |
| 199 | one or several definitions, as in \isacommand{apply}\isa{(unfold xor_def)}.
 | |
| 200 | This is can be useful in situations where \isa{simp} does too much.
 | |
| 12533 | 201 | Warning: \isa{unfold} acts on all subgoals!
 | 
| 67406 | 202 | \<close> | 
| 9932 | 203 | |
| 67406 | 204 | subsection\<open>Simplifying {\tt\slshape let}-Expressions\<close>
 | 
| 9932 | 205 | |
| 67406 | 206 | text\<open>\index{simplification!of \isa{let}-expressions}\index{*let expressions}%
 | 
| 11458 | 207 | Proving a goal containing \isa{let}-expressions almost invariably requires the
 | 
| 69505 | 208 | \<open>let\<close>-con\-structs to be expanded at some point. Since | 
| 209 | \<open>let\<close>\ldots\isa{=}\ldots\<open>in\<close>{\ldots} is just syntactic sugar for
 | |
| 69597 | 210 | the predefined constant \<^term>\<open>Let\<close>, expanding \<open>let\<close>-constructs | 
| 67406 | 211 | means rewriting with \tdx{Let_def}:\<close>
 | 
| 9932 | 212 | |
| 12631 | 213 | lemma "(let xs = [] in xs@ys@xs) = ys" | 
| 214 | apply(simp add: Let_def) | |
| 10171 | 215 | done | 
| 9932 | 216 | |
| 67406 | 217 | text\<open> | 
| 9932 | 218 | If, in a particular context, there is no danger of a combinatorial explosion | 
| 69505 | 219 | of nested \<open>let\<close>s, you could even simplify with @{thm[source]Let_def} by
 | 
| 9932 | 220 | default: | 
| 67406 | 221 | \<close> | 
| 9932 | 222 | declare Let_def [simp] | 
| 223 | ||
| 67406 | 224 | subsection\<open>Conditional Simplification Rules\<close> | 
| 9932 | 225 | |
| 67406 | 226 | text\<open> | 
| 11458 | 227 | \index{conditional simplification rules}%
 | 
| 9932 | 228 | So far all examples of rewrite rules were equations. The simplifier also | 
| 229 | accepts \emph{conditional} equations, for example
 | |
| 67406 | 230 | \<close> | 
| 9932 | 231 | |
| 12631 | 232 | lemma hd_Cons_tl[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs" | 
| 233 | apply(case_tac xs, simp, simp) | |
| 10171 | 234 | done | 
| 9932 | 235 | |
| 67406 | 236 | text\<open>\noindent | 
| 9932 | 237 | Note the use of ``\ttindexboldpos{,}{$Isar}'' to string together a
 | 
| 238 | sequence of methods. Assuming that the simplification rule | |
| 69597 | 239 | \<^term>\<open>(rev xs = []) = (xs = [])\<close> | 
| 9932 | 240 | is present as well, | 
| 10795 | 241 | the lemma below is proved by plain simplification: | 
| 67406 | 242 | \<close> | 
| 9932 | 243 | |
| 12631 | 244 | lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs" | 
| 9932 | 245 | (*<*) | 
| 12631 | 246 | by(simp) | 
| 9932 | 247 | (*>*) | 
| 67406 | 248 | text\<open>\noindent | 
| 10795 | 249 | The conditional equation @{thm[source]hd_Cons_tl} above
 | 
| 69597 | 250 | can simplify \<^term>\<open>hd(rev xs) # tl(rev xs)\<close> to \<^term>\<open>rev xs\<close> | 
| 251 | because the corresponding precondition \<^term>\<open>rev xs ~= []\<close> | |
| 252 | simplifies to \<^term>\<open>xs ~= []\<close>, which is exactly the local | |
| 9932 | 253 | assumption of the subgoal. | 
| 67406 | 254 | \<close> | 
| 9932 | 255 | |
| 256 | ||
| 67406 | 257 | subsection\<open>Automatic Case Splits\<close> | 
| 9932 | 258 | |
| 67406 | 259 | text\<open>\label{sec:AutoCaseSplits}\indexbold{case splits}%
 | 
| 69505 | 260 | Goals containing \<open>if\<close>-expressions\index{*if expressions!splitting of}
 | 
| 11458 | 261 | are usually proved by case | 
| 262 | distinction on the boolean condition. Here is an example: | |
| 67406 | 263 | \<close> | 
| 9932 | 264 | |
| 12631 | 265 | lemma "\<forall>xs. if xs = [] then rev xs = [] else rev xs \<noteq> []" | 
| 9932 | 266 | |
| 67406 | 267 | txt\<open>\noindent | 
| 11458 | 268 | The goal can be split by a special method, \methdx{split}:
 | 
| 67406 | 269 | \<close> | 
| 9932 | 270 | |
| 62392 | 271 | apply(split if_split) | 
| 9932 | 272 | |
| 67406 | 273 | txt\<open>\noindent | 
| 10362 | 274 | @{subgoals[display,indent=0]}
 | 
| 62392 | 275 | where \tdx{if_split} is a theorem that expresses splitting of
 | 
| 69505 | 276 | \<open>if\<close>s. Because | 
| 277 | splitting the \<open>if\<close>s is usually the right proof strategy, the | |
| 278 | simplifier does it automatically.  Try \isacommand{apply}\<open>(simp)\<close>
 | |
| 9932 | 279 | on the initial goal above. | 
| 280 | ||
| 69505 | 281 | This splitting idea generalizes from \<open>if\<close> to \sdx{case}.
 | 
| 11458 | 282 | Let us simplify a case analysis over lists:\index{*list.split (theorem)}
 | 
| 67406 | 283 | \<close>(*<*)by simp(*>*) | 
| 12631 | 284 | lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs" | 
| 285 | apply(split list.split) | |
| 11309 | 286 | |
| 67406 | 287 | txt\<open> | 
| 10362 | 288 | @{subgoals[display,indent=0]}
 | 
| 11458 | 289 | The simplifier does not split | 
| 69505 | 290 | \<open>case\<close>-expressions, as it does \<open>if\<close>-expressions, | 
| 11458 | 291 | because with recursive datatypes it could lead to nontermination. | 
| 292 | Instead, the simplifier has a modifier | |
| 69505 | 293 | \<open>split\<close>\index{*split (modifier)} 
 | 
| 11458 | 294 | for adding splitting rules explicitly. The | 
| 295 | lemma above can be proved in one step by | |
| 67406 | 296 | \<close> | 
| 12631 | 297 | (*<*)oops | 
| 298 | lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs" | |
| 9932 | 299 | (*>*) | 
| 12631 | 300 | apply(simp split: list.split) | 
| 10171 | 301 | (*<*)done(*>*) | 
| 67406 | 302 | text\<open>\noindent | 
| 69505 | 303 | whereas \isacommand{apply}\<open>(simp)\<close> alone will not succeed.
 | 
| 9932 | 304 | |
| 11458 | 305 | Every datatype $t$ comes with a theorem | 
| 69505 | 306 | $t$\<open>.split\<close> which can be declared to be a \bfindex{split rule} either
 | 
| 11458 | 307 | locally as above, or by giving it the \attrdx{split} attribute globally:
 | 
| 67406 | 308 | \<close> | 
| 9932 | 309 | |
| 310 | declare list.split [split] | |
| 311 | ||
| 67406 | 312 | text\<open>\noindent | 
| 69505 | 313 | The \<open>split\<close> attribute can be removed with the \<open>del\<close> modifier, | 
| 9932 | 314 | either locally | 
| 67406 | 315 | \<close> | 
| 9932 | 316 | (*<*) | 
| 12631 | 317 | lemma "dummy=dummy" | 
| 9932 | 318 | (*>*) | 
| 62392 | 319 | apply(simp split del: if_split) | 
| 9932 | 320 | (*<*) | 
| 12631 | 321 | oops | 
| 9932 | 322 | (*>*) | 
| 67406 | 323 | text\<open>\noindent | 
| 9932 | 324 | or globally: | 
| 67406 | 325 | \<close> | 
| 9932 | 326 | declare list.split [split del] | 
| 327 | ||
| 67406 | 328 | text\<open> | 
| 69505 | 329 | Polished proofs typically perform splitting within \<open>simp\<close> rather than | 
| 330 | invoking the \<open>split\<close> method. However, if a goal contains | |
| 331 | several \<open>if\<close> and \<open>case\<close> expressions, | |
| 332 | the \<open>split\<close> method can be | |
| 10654 | 333 | helpful in selectively exploring the effects of splitting. | 
| 334 | ||
| 11458 | 335 | The split rules shown above are intended to affect only the subgoal's | 
| 69505 | 336 | conclusion. If you want to split an \<open>if\<close> or \<open>case\<close>-expression | 
| 62392 | 337 | in the assumptions, you have to apply \tdx{if_split_asm} or
 | 
| 69505 | 338 | $t$\<open>.split_asm\<close>:\<close> | 
| 9932 | 339 | |
| 10654 | 340 | lemma "if xs = [] then ys \<noteq> [] else ys = [] \<Longrightarrow> xs @ ys \<noteq> []" | 
| 62392 | 341 | apply(split if_split_asm) | 
| 9932 | 342 | |
| 67406 | 343 | txt\<open>\noindent | 
| 11458 | 344 | Unlike splitting the conclusion, this step creates two | 
| 69505 | 345 | separate subgoals, which here can be solved by \<open>simp_all\<close>: | 
| 10362 | 346 | @{subgoals[display,indent=0]}
 | 
| 9932 | 347 | If you need to split both in the assumptions and the conclusion, | 
| 69505 | 348 | use $t$\<open>.splits\<close> which subsumes $t$\<open>.split\<close> and | 
| 349 | $t$\<open>.split_asm\<close>. Analogously, there is @{thm[source]if_splits}.
 | |
| 9932 | 350 | |
| 351 | \begin{warn}
 | |
| 11458 | 352 | The simplifier merely simplifies the condition of an | 
| 353 |   \isa{if}\index{*if expressions!simplification of} but not the
 | |
| 9932 | 354 |   \isa{then} or \isa{else} parts. The latter are simplified only after the
 | 
| 355 |   condition reduces to \isa{True} or \isa{False}, or after splitting. The
 | |
| 11428 | 356 |   same is true for \sdx{case}-expressions: only the selector is
 | 
| 9932 | 357 | simplified at first, until either the expression reduces to one of the | 
| 358 | cases or it is split. | |
| 11458 | 359 | \end{warn}
 | 
| 67406 | 360 | \<close> | 
| 10362 | 361 | (*<*) | 
| 362 | by(simp_all) | |
| 363 | (*>*) | |
| 9932 | 364 | |
| 67406 | 365 | subsection\<open>Tracing\<close> | 
| 366 | text\<open>\indexbold{tracing the simplifier}
 | |
| 9932 | 367 | Using the simplifier effectively may take a bit of experimentation. Set the | 
| 16523 | 368 | Proof General flag \pgmenu{Isabelle} $>$ \pgmenu{Settings} $>$ \pgmenu{Trace Simplifier} to get a better idea of what is going on:
 | 
| 67406 | 369 | \<close> | 
| 9932 | 370 | |
| 12631 | 371 | lemma "rev [a] = []" | 
| 372 | apply(simp) | |
| 9932 | 373 | (*<*)oops(*>*) | 
| 374 | ||
| 67406 | 375 | text\<open>\noindent | 
| 16523 | 376 | produces the following trace in Proof General's \pgmenu{Trace} buffer:
 | 
| 9932 | 377 | |
| 378 | \begin{ttbox}\makeatother
 | |
| 16518 | 379 | [1]Applying instance of rewrite rule "List.rev.simps_2": | 
| 16359 | 380 | rev (?x1 # ?xs1) \(\equiv\) rev ?xs1 @ [?x1] | 
| 381 | ||
| 16518 | 382 | [1]Rewriting: | 
| 16359 | 383 | rev [a] \(\equiv\) rev [] @ [a] | 
| 384 | ||
| 16518 | 385 | [1]Applying instance of rewrite rule "List.rev.simps_1": | 
| 16359 | 386 | rev [] \(\equiv\) [] | 
| 387 | ||
| 16518 | 388 | [1]Rewriting: | 
| 16359 | 389 | rev [] \(\equiv\) [] | 
| 390 | ||
| 67350 | 391 | [1]Applying instance of rewrite rule "List.append.append_Nil": | 
| 16359 | 392 | [] @ ?y \(\equiv\) ?y | 
| 393 | ||
| 16518 | 394 | [1]Rewriting: | 
| 16359 | 395 | [] @ [a] \(\equiv\) [a] | 
| 396 | ||
| 16518 | 397 | [1]Applying instance of rewrite rule | 
| 16359 | 398 | ?x2 # ?t1 = ?t1 \(\equiv\) False | 
| 399 | ||
| 16518 | 400 | [1]Rewriting: | 
| 16359 | 401 | [a] = [] \(\equiv\) False | 
| 9932 | 402 | \end{ttbox}
 | 
| 16359 | 403 | The trace lists each rule being applied, both in its general form and | 
| 404 | the instance being used. The \texttt{[}$i$\texttt{]} in front (where
 | |
| 16518 | 405 | above $i$ is always \texttt{1}) indicates that we are inside the $i$th
 | 
| 406 | invocation of the simplifier. Each attempt to apply a | |
| 16359 | 407 | conditional rule shows the rule followed by the trace of the | 
| 408 | (recursive!) simplification of the conditions, the latter prefixed by | |
| 409 | \texttt{[}$i+1$\texttt{]} instead of \texttt{[}$i$\texttt{]}.
 | |
| 410 | Another source of recursive invocations of the simplifier are | |
| 35992 | 411 | proofs of arithmetic formulae. By default, recursive invocations are not shown, | 
| 412 | you must increase the trace depth via \pgmenu{Isabelle} $>$ \pgmenu{Settings} $>$ \pgmenu{Trace Simplifier Depth}.
 | |
| 11309 | 413 | |
| 16359 | 414 | Many other hints about the simplifier's actions may appear. | 
| 9932 | 415 | |
| 16359 | 416 | In more complicated cases, the trace can be very lengthy. Thus it is | 
| 16523 | 417 | advisable to reset the \pgmenu{Trace Simplifier} flag after having
 | 
| 35992 | 418 | obtained the desired trace. | 
| 36069 | 419 | Since this is easily forgotten (and may have the unpleasant effect of | 
| 420 | swamping the interface with trace information), here is how you can switch | |
| 67406 | 421 | the trace on locally in a proof:\<close> | 
| 36069 | 422 | |
| 423 | (*<*)lemma "x=x" | |
| 424 | (*>*) | |
| 40878 
7695e4de4d86
renamed trace_simp to simp_trace, and debug_simp to simp_debug;
 wenzelm parents: 
36069diff
changeset | 425 | using [[simp_trace=true]] | 
| 36069 | 426 | apply simp | 
| 427 | (*<*)oops(*>*) | |
| 428 | ||
| 67406 | 429 | text\<open>\noindent | 
| 36069 | 430 | Within the current proof, all simplifications in subsequent proof steps | 
| 431 | will be traced, but the text reminds you to remove the \isa{using} clause
 | |
| 67406 | 432 | after it has done its job.\<close> | 
| 9932 | 433 | |
| 67406 | 434 | subsection\<open>Finding Theorems\label{sec:find}\<close>
 | 
| 16518 | 435 | |
| 67406 | 436 | text\<open>\indexbold{finding theorems}\indexbold{searching theorems}
 | 
| 16560 | 437 | Isabelle's large database of proved theorems | 
| 438 | offers a powerful search engine. Its chief limitation is | |
| 16518 | 439 | its restriction to the theories currently loaded. | 
| 440 | ||
| 441 | \begin{pgnote}
 | |
| 16523 | 442 | The search engine is started by clicking on Proof General's \pgmenu{Find} icon.
 | 
| 16518 | 443 | You specify your search textually in the input buffer at the bottom | 
| 444 | of the window. | |
| 445 | \end{pgnote}
 | |
| 446 | ||
| 16560 | 447 | The simplest form of search finds theorems containing specified | 
| 448 | patterns. A pattern can be any term (even | |
| 449 | a single identifier).  It may contain ``\texttt{\_}'', a wildcard standing
 | |
| 450 | for any term. Here are some | |
| 16518 | 451 | examples: | 
| 452 | \begin{ttbox}
 | |
| 453 | length | |
| 454 | "_ # _ = _ # _" | |
| 455 | "_ + _" | |
| 456 | "_ * (_ - (_::nat))" | |
| 457 | \end{ttbox}
 | |
| 16560 | 458 | Specifying types, as shown in the last example, | 
| 459 | constrains searches involving overloaded operators. | |
| 16518 | 460 | |
| 461 | \begin{warn}
 | |
| 462 | Always use ``\texttt{\_}'' rather than variable names: searching for
 | |
| 463 | \texttt{"x + y"} will usually not find any matching theorems
 | |
| 16560 | 464 | because they would need to contain \texttt{x} and~\texttt{y} literally.
 | 
| 465 | When searching for infix operators, do not just type in the symbol, | |
| 466 | such as~\texttt{+}, but a proper term such as \texttt{"_ + _"}.
 | |
| 467 | This remark applies to more complicated syntaxes, too. | |
| 16518 | 468 | \end{warn}
 | 
| 469 | ||
| 16560 | 470 | If you are looking for rewrite rules (possibly conditional) that could | 
| 471 | simplify some term, prefix the pattern with \texttt{simp:}.
 | |
| 16518 | 472 | \begin{ttbox}
 | 
| 473 | simp: "_ * (_ + _)" | |
| 474 | \end{ttbox}
 | |
| 16560 | 475 | This finds \emph{all} equations---not just those with a \isa{simp} attribute---whose conclusion has the form
 | 
| 16518 | 476 | @{text[display]"_ * (_ + _) = \<dots>"}
 | 
| 16560 | 477 | It only finds equations that can simplify the given pattern | 
| 478 | at the root, not somewhere inside: for example, equations of the form | |
| 69505 | 479 | \<open>_ + _ = \<dots>\<close> do not match. | 
| 16518 | 480 | |
| 16560 | 481 | You may also search for theorems by name---you merely | 
| 482 | need to specify a substring. For example, you could search for all | |
| 16518 | 483 | commutativity theorems like this: | 
| 484 | \begin{ttbox}
 | |
| 485 | name: comm | |
| 486 | \end{ttbox}
 | |
| 487 | This retrieves all theorems whose name contains \texttt{comm}.
 | |
| 488 | ||
| 16560 | 489 | Search criteria can also be negated by prefixing them with ``\texttt{-}''.
 | 
| 490 | For example, | |
| 16518 | 491 | \begin{ttbox}
 | 
| 16523 | 492 | -name: List | 
| 16518 | 493 | \end{ttbox}
 | 
| 16560 | 494 | finds theorems whose name does not contain \texttt{List}. You can use this
 | 
| 495 | to exclude particular theories from the search: the long name of | |
| 16518 | 496 | a theorem contains the name of the theory it comes from. | 
| 497 | ||
| 16560 | 498 | Finallly, different search criteria can be combined arbitrarily. | 
| 20143 | 499 | The effect is conjuctive: Find returns the theorems that satisfy all of | 
| 16560 | 500 | the criteria. For example, | 
| 16518 | 501 | \begin{ttbox}
 | 
| 502 | "_ + _" -"_ - _" -simp: "_ * (_ + _)" name: assoc | |
| 503 | \end{ttbox}
 | |
| 16560 | 504 | looks for theorems containing plus but not minus, and which do not simplify | 
| 69505 | 505 | \mbox{\<open>_ * (_ + _)\<close>} at the root, and whose name contains \texttt{assoc}.
 | 
| 16518 | 506 | |
| 16544 | 507 | Further search criteria are explained in \S\ref{sec:find2}.
 | 
| 16523 | 508 | |
| 509 | \begin{pgnote}
 | |
| 510 | Proof General keeps a history of all your search expressions. | |
| 511 | If you click on \pgmenu{Find}, you can use the arrow keys to scroll
 | |
| 512 | through previous searches and just modify them. This saves you having | |
| 513 | to type in lengthy expressions again and again. | |
| 514 | \end{pgnote}
 | |
| 67406 | 515 | \<close> | 
| 9932 | 516 | (*<*) | 
| 9922 | 517 | end | 
| 9932 | 518 | (*>*) |