doc-src/TutorialI/Misc/simp.thy
changeset 10795 9e888d60d3e5
parent 10788 ea48dd8b0232
child 10885 90695f46440b
equal deleted inserted replaced
10794:65d18005d802 10795:9e888d60d3e5
     4 
     4 
     5 subsubsection{*Simplification rules*}
     5 subsubsection{*Simplification rules*}
     6 
     6 
     7 text{*\indexbold{simplification rule}
     7 text{*\indexbold{simplification rule}
     8 To facilitate simplification, theorems can be declared to be simplification
     8 To facilitate simplification, theorems can be declared to be simplification
     9 rules (with the help of the attribute @{text"[simp]"}\index{*simp
     9 rules (by the attribute @{text"[simp]"}\index{*simp
    10   (attribute)}), in which case proofs by simplification make use of these
    10   (attribute)}), in which case proofs by simplification make use of these
    11 rules automatically. In addition the constructs \isacommand{datatype} and
    11 rules automatically. In addition the constructs \isacommand{datatype} and
    12 \isacommand{primrec} (and a few others) invisibly declare useful
    12 \isacommand{primrec} (and a few others) invisibly declare useful
    13 simplification rules. Explicit definitions are \emph{not} declared
    13 simplification rules. Explicit definitions are \emph{not} declared
    14 simplification rules automatically!
    14 simplification rules automatically!
    30 i.e.\ they should not be default simplification rules.  Conversely, it may
    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
    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
    32 proofs.  Frequent changes in the simplification status of a theorem may
    33 indicate a badly designed theory.
    33 indicate a badly designed theory.
    34 \begin{warn}
    34 \begin{warn}
    35   Simplification may not terminate, for example if both $f(x) = g(x)$ and
    35   Simplification may run forever, for example if both $f(x) = g(x)$ and
    36   $g(x) = f(x)$ are simplification rules. It is the user's responsibility not
    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
    37   to include simplification rules that can lead to nontermination, either on
    38   their own or in combination with other simplification rules.
    38   their own or in combination with other simplification rules.
    39 \end{warn}
    39 \end{warn}
    40 *}
    40 *}
    44 text{*\index{*simp (method)|bold}
    44 text{*\index{*simp (method)|bold}
    45 The general format of the simplification method is
    45 The general format of the simplification method is
    46 \begin{quote}
    46 \begin{quote}
    47 @{text simp} \textit{list of modifiers}
    47 @{text simp} \textit{list of modifiers}
    48 \end{quote}
    48 \end{quote}
    49 where the list of \emph{modifiers} helps to fine tune the behaviour and may
    49 where the list of \emph{modifiers} fine tunes the behaviour and may
    50 be empty. Most if not all of the proofs seen so far could have been performed
    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
    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
    52 only the first subgoal and may thus need to be repeated---use
    53 \isaindex{simp_all} to simplify all subgoals.
    53 \isaindex{simp_all} to simplify all subgoals.
    54 Note that @{text simp} fails if nothing changes.
    54 Note that @{text simp} fails if nothing changes.
   115   are used in the simplification of the conclusion.
   115   are used in the simplification of the conclusion.
   116 \item[@{text"(no_asm_use)"}]\indexbold{*no_asm_use}
   116 \item[@{text"(no_asm_use)"}]\indexbold{*no_asm_use}
   117  means that the assumptions are simplified but are not
   117  means that the assumptions are simplified but are not
   118   used in the simplification of each other or the conclusion.
   118   used in the simplification of each other or the conclusion.
   119 \end{description}
   119 \end{description}
   120 Neither @{text"(no_asm_simp)"} nor @{text"(no_asm_use)"} allow to simplify
   120 Both @{text"(no_asm_simp)"} and @{text"(no_asm_use)"} run forever on
   121 the above problematic subgoal.
   121 the problematic subgoal above.
   122 
   122 Note that only one of the options is allowed, and it must precede all
   123 Note that only one of the above options is allowed, and it must precede all
       
   124 other arguments.
   123 other arguments.
   125 *}
   124 *}
   126 
   125 
   127 subsubsection{*Rewriting with definitions*}
   126 subsubsection{*Rewriting with definitions*}
   128 
   127 
   175 subsubsection{*Simplifying let-expressions*}
   174 subsubsection{*Simplifying let-expressions*}
   176 
   175 
   177 text{*\index{simplification!of let-expressions}
   176 text{*\index{simplification!of let-expressions}
   178 Proving a goal containing \isaindex{let}-expressions almost invariably
   177 Proving a goal containing \isaindex{let}-expressions almost invariably
   179 requires the @{text"let"}-con\-structs to be expanded at some point. Since
   178 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
   179 @{text"let"}\ldots\isa{=}\ldots@{text"in"}{\ldots} is just syntactic sugar for a predefined constant
   181 (called @{term"Let"}), expanding @{text"let"}-constructs means rewriting with
   180 (called @{term"Let"}), expanding @{text"let"}-constructs means rewriting with
   182 @{thm[source]Let_def}:
   181 @{thm[source]Let_def}:
   183 *}
   182 *}
   184 
   183 
   185 lemma "(let xs = [] in xs@ys@xs) = ys";
   184 lemma "(let xs = [] in xs@ys@xs) = ys";
   207 text{*\noindent
   206 text{*\noindent
   208 Note the use of ``\ttindexboldpos{,}{$Isar}'' to string together a
   207 Note the use of ``\ttindexboldpos{,}{$Isar}'' to string together a
   209 sequence of methods. Assuming that the simplification rule
   208 sequence of methods. Assuming that the simplification rule
   210 @{term"(rev xs = []) = (xs = [])"}
   209 @{term"(rev xs = []) = (xs = [])"}
   211 is present as well,
   210 is present as well,
       
   211 the lemma below is proved by plain simplification:
   212 *}
   212 *}
   213 
   213 
   214 lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs";
   214 lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs";
   215 (*<*)
   215 (*<*)
   216 by(simp);
   216 by(simp);
   217 (*>*)
   217 (*>*)
   218 text{*\noindent
   218 text{*\noindent
   219 is proved by plain simplification:
   219 The conditional equation @{thm[source]hd_Cons_tl} above
   220 the conditional equation @{thm[source]hd_Cons_tl} above
       
   221 can simplify @{term"hd(rev xs) # tl(rev xs)"} to @{term"rev xs"}
   220 can simplify @{term"hd(rev xs) # tl(rev xs)"} to @{term"rev xs"}
   222 because the corresponding precondition @{term"rev xs ~= []"}
   221 because the corresponding precondition @{term"rev xs ~= []"}
   223 simplifies to @{term"xs ~= []"}, which is exactly the local
   222 simplifies to @{term"xs ~= []"}, which is exactly the local
   224 assumption of the subgoal.
   223 assumption of the subgoal.
   225 *}
   224 *}
   331 subsubsection{*Arithmetic*}
   330 subsubsection{*Arithmetic*}
   332 
   331 
   333 text{*\index{arithmetic}
   332 text{*\index{arithmetic}
   334 The simplifier routinely solves a small class of linear arithmetic formulae
   333 The simplifier routinely solves a small class of linear arithmetic formulae
   335 (over type \isa{nat} and other numeric types): it only takes into account
   334 (over type \isa{nat} and other numeric types): it only takes into account
   336 assumptions and conclusions that are (possibly negated) (in)equalities
   335 assumptions and conclusions that are relations
   337 (@{text"="}, \isasymle, @{text"<"}) and it only knows about addition. Thus
   336 ($=$, $\le$, $<$, possibly negated) and it only knows about addition. Thus
   338 *}
   337 *}
   339 
   338 
   340 lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
   339 lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
   341 (*<*)by(auto)(*>*)
   340 (*<*)by(auto)(*>*)
   342 
   341