doc-src/Ref/simplifier.tex
changeset 1860 71bfeecfa96c
parent 1387 9bcad9c22fd4
child 2020 586f3c075b05
equal deleted inserted replaced
1859:2ea3f7ebeccb 1860:71bfeecfa96c
     5 This chapter describes Isabelle's generic simplification package, which
     5 This chapter describes Isabelle's generic simplification package, which
     6 provides a suite of simplification tactics.  It performs conditional and
     6 provides a suite of simplification tactics.  It performs conditional and
     7 unconditional rewriting and uses contextual information (`local
     7 unconditional rewriting and uses contextual information (`local
     8 assumptions').  It provides a few general hooks, which can provide
     8 assumptions').  It provides a few general hooks, which can provide
     9 automatic case splits during rewriting, for example.  The simplifier is set
     9 automatic case splits during rewriting, for example.  The simplifier is set
    10 up for many of Isabelle's logics: {\tt FOL}, {\tt ZF}, {\tt LCF} and {\tt
    10 up for many of Isabelle's logics: \FOL, \ZF, \HOL\ and \HOLCF.
    11   HOL}.
    11 
       
    12 The next section is a quick introduction to the simplifier, the later
       
    13 sections explain advanced features.
       
    14 
       
    15 \section{Simplification for dummies}
       
    16 \label{sec:simp-for-dummies}
       
    17 
       
    18 In some logics (e.g.\ \HOL), the simplifier is particularly easy to
       
    19 use because it supports the concept of a {\em current set of simplification
       
    20   rules}, also called the {\em current simpset}\index{simpset!current}. All
       
    21 commands are interpreted relative to the current simpset. For example, in the
       
    22 theory of arithmetic the goal
       
    23 \begin{ttbox}
       
    24 {\out  1. 0 + (x + 0) = x + 0 + 0}
       
    25 \end{ttbox}
       
    26 can be solved by a single
       
    27 \begin{ttbox}
       
    28 by(Simp_tac 1);
       
    29 \end{ttbox}
       
    30 The simplifier uses the current simpset, which in the case of arithmetic
       
    31 contains the required theorems $\Var{n}+0 = \Var{n}$ and $0+\Var{n} =
       
    32 \Var{n}$.
       
    33 
       
    34 If assumptions of the subgoal are also needed in the simplification
       
    35 process, as in
       
    36 \begin{ttbox}
       
    37 {\out  1. x = 0 ==> x + x = 0}
       
    38 \end{ttbox}
       
    39 then there is the more powerful
       
    40 \begin{ttbox}
       
    41 by(Asm_simp_tac 1);
       
    42 \end{ttbox}
       
    43 which solves the above goal.
       
    44 
       
    45 There are four basic simplification tactics:
       
    46 \begin{ttdescription}
       
    47 \item[\ttindexbold{Simp_tac} $i$] simplifies subgoal~$i$ using the current
       
    48   simpset.  It may solve the subgoal completely if it has become trivial,
       
    49   using the solver.
       
    50   
       
    51 \item[\ttindexbold{Asm_simp_tac}]\index{assumptions!in simplification}
       
    52   is like \verb$Simp_tac$, but extracts additional rewrite rules from the
       
    53   assumptions.
       
    54 
       
    55 \item[\ttindexbold{Full_simp_tac}] is like \verb$Simp_tac$, but also
       
    56   simplifies the assumptions (but without using the assumptions to simplify
       
    57   each other or the actual goal.)
       
    58 
       
    59 \item[\ttindexbold{Asm_full_simp_tac}]
       
    60   is like \verb$Asm_simp_tac$, but also simplifies the assumptions one by
       
    61   one.  {\em Working from left to right, it uses each assumption in the
       
    62   simplification of those following.}
       
    63 \end{ttdescription}
       
    64 
       
    65 {\tt Asm_full_simp_tac} is the most powerful of this quartet but may also
       
    66 loop where some of the others terminate. For example,
       
    67 \begin{ttbox}
       
    68 {\out  1. ALL x. f(x) = g(f(g(x))) ==> f(0) = f(0)+0}
       
    69 \end{ttbox}
       
    70 is solved by {\tt Simp_tac}, but {\tt Asm_simp_tac} and {\tt Asm_simp_tac}
       
    71 loop because the rewrite rule $f(\Var{x}) = f(g(f(\Var{x})))$ extracted from
       
    72 the assumption does not terminate. Isabelle notices certain simple forms of
       
    73 nontermination, but not this one.
       
    74  
       
    75 \begin{warn}
       
    76   Since \verb$Asm_full_simp_tac$ works from left to right, it sometimes
       
    77 misses opportunities for simplification: given the subgoal
       
    78 \begin{ttbox}
       
    79 {\out [| P(f(a)); f(a) = t |] ==> ...}
       
    80 \end{ttbox}
       
    81 \verb$Asm_full_simp_tac$ will not simplify the first assumption using the
       
    82 second but will leave the assumptions unchanged. See
       
    83 \S\ref{sec:reordering-asms} for ways around this problem.
       
    84 \end{warn}
       
    85 
       
    86 Using the simplifier effectively may take a bit of experimentation.  The
       
    87 tactics can be traced with the ML command \verb$trace_simp := true$.
       
    88 
       
    89 There is not just one global current simpset, but one associated with each
       
    90 theory as well. How are these simpset built up?
       
    91 \begin{enumerate}
       
    92 \item When loading {\tt T.thy}, the current simpset is initialized with the
       
    93   union of the simpsets associated with all the ancestors of {\tt T}. In
       
    94   addition, certain constructs in {\tt T} may add new rules to the simpset,
       
    95   e.g.\ \ttindex{datatype} and \ttindex{primrec} in \HOL. Definitions are not
       
    96   added automatically!
       
    97 \item The script in {\tt T.ML} may manipulate the current simpset further by
       
    98   explicitly adding/deleting theorems to/from it (see below).
       
    99 \item After {\tt T.ML} has been read, the current simpset is associated with
       
   100   theory {\tt T}.
       
   101 \end{enumerate}
       
   102 The current simpset is manipulated by
       
   103 \begin{ttbox}
       
   104 Addsimps, Delsimps: thm list -> unit
       
   105 \end{ttbox}
       
   106 \begin{ttdescription}
       
   107 \item[\ttindexbold{Addsimps} $thms$] adds $thms$ to the current simpset
       
   108 \item[\ttindexbold{Delsimps} $thms$] deletes $thms$ from the current simpset
       
   109 \end{ttdescription}
       
   110 
       
   111 Generally useful simplification rules $\Var{n}+0 = \Var{n}$ should be added
       
   112 to the current simpset right after they have been proved. Those of a more
       
   113 specific nature (e.g.\ the laws of de~Morgan, which alter the structure of a
       
   114 formula) should only be added for specific proofs and deleted again
       
   115 afterwards. Conversely, it may also happen that a generally useful rule needs
       
   116 to be removed for a certain proof and is added again afterwards.  Well
       
   117 designed simpsets need few temporary additions or deletions.
       
   118 
       
   119 \begin{warn}
       
   120   If you execute proofs interactively, or load them from an ML file without
       
   121   associated {\tt .thy} file, you must set the current simpset by calling
       
   122   \ttindex{set_current_thy}~{\tt"}$T${\tt"}, where $T$ is the name of the
       
   123   theory you want to work in. If you have just loaded $T$, this is not
       
   124   necessary.
       
   125 \end{warn}
    12 
   126 
    13 
   127 
    14 \section{Simplification sets}\index{simplification sets} 
   128 \section{Simplification sets}\index{simplification sets} 
    15 The simplification tactics are controlled by {\bf simpsets}.  These consist
   129 The simplification tactics are controlled by {\bf simpsets}.  These consist
    16 of five components: rewrite rules, congruence rules, the subgoaler, the
   130 of five components: rewrite rules, congruence rules, the subgoaler, the
    17 solver and the looper.  The simplifier should be set up with sensible
   131 solver and the looper.  The simplifier should be set up with sensible
    18 defaults so that most simplifier calls specify only rewrite rules.
   132 defaults so that most simplifier calls specify only rewrite rules.
    19 Experienced users can exploit the other components to streamline proofs.
   133 Experienced users can exploit the other components to streamline proofs.
    20 
   134 
       
   135 Logics like \HOL, which support a current simpset\index{simpset!current},
       
   136 store its value in an ML reference variable usually called {\tt
       
   137   simpset}\index{simpset@{\tt simpset} ML value}. It can be accessed via
       
   138 {\tt!simpset} and updated via {\tt simpset := \dots}.
    21 
   139 
    22 \subsection{Rewrite rules}
   140 \subsection{Rewrite rules}
    23 \index{rewrite rules|(}
   141 \index{rewrite rules|(}
    24 Rewrite rules are theorems expressing some form of equality:
   142 Rewrite rules are theorems expressing some form of equality:
    25 \begin{eqnarray*}
   143 \begin{eqnarray*}
    26   Suc(\Var{m}) + \Var{n} &=&      \Var{m} + Suc(\Var{n}) \\
   144   Suc(\Var{m}) + \Var{n} &=&      \Var{m} + Suc(\Var{n}) \\
    27   \Var{P}\conj\Var{P}    &\bimp&  \Var{P} \\
   145   \Var{P}\conj\Var{P}    &\bimp&  \Var{P} \\
    28   \Var{A} \un \Var{B} &\equiv& \{x.x\in \Var{A} \disj x\in \Var{B}\}
   146   \Var{A} \un \Var{B} &\equiv& \{x.x\in \Var{A} \disj x\in \Var{B}\}
    29 \end{eqnarray*}
   147 \end{eqnarray*}
    30 {\bf Conditional} rewrites such as $\Var{m}<\Var{n} \Imp \Var{m}/\Var{n} =
   148 Conditional rewrites such as $\Var{m}<\Var{n} \Imp \Var{m}/\Var{n} =
    31 0$ are permitted; the conditions can be arbitrary terms.  The infix
   149 0$ are permitted; the conditions can be arbitrary terms.
    32 operation \ttindex{addsimps} adds new rewrite rules, while
       
    33 \ttindex{delsimps} deletes rewrite rules from a simpset.
       
    34 
   150 
    35 Internally, all rewrite rules are translated into meta-equalities, theorems
   151 Internally, all rewrite rules are translated into meta-equalities, theorems
    36 with conclusion $lhs \equiv rhs$.  Each simpset contains a function for
   152 with conclusion $lhs \equiv rhs$.  Each simpset contains a function for
    37 extracting equalities from arbitrary theorems.  For example,
   153 extracting equalities from arbitrary theorems.  For example,
    38 $\neg(\Var{x}\in \{\})$ could be turned into $\Var{x}\in \{\} \equiv
   154 $\neg(\Var{x}\in \{\})$ could be turned into $\Var{x}\in \{\} \equiv
   210 signature SIMPLIFIER =
   326 signature SIMPLIFIER =
   211 sig
   327 sig
   212   type simpset
   328   type simpset
   213   val simp_tac:          simpset -> int -> tactic
   329   val simp_tac:          simpset -> int -> tactic
   214   val asm_simp_tac:      simpset -> int -> tactic
   330   val asm_simp_tac:      simpset -> int -> tactic
       
   331   val full_simp_tac:     simpset -> int -> tactic
   215   val asm_full_simp_tac: simpset -> int -> tactic\smallskip
   332   val asm_full_simp_tac: simpset -> int -> tactic\smallskip
   216   val addeqcongs:   simpset * thm list -> simpset
   333   val addeqcongs:   simpset * thm list -> simpset
   217   val addsimps:     simpset * thm list -> simpset
   334   val addsimps:     simpset * thm list -> simpset
   218   val delsimps:     simpset * thm list -> simpset
   335   val delsimps:     simpset * thm list -> simpset
   219   val empty_ss:     simpset
   336   val empty_ss:     simpset
   228 \end{ttbox}
   345 \end{ttbox}
   229 \caption{The simplifier primitives} \label{SIMPLIFIER}
   346 \caption{The simplifier primitives} \label{SIMPLIFIER}
   230 \end{figure}
   347 \end{figure}
   231 
   348 
   232 
   349 
   233 \section{The simplification tactics} \label{simp-tactics}
   350 \section{The simplification tactics}
       
   351 \label{simp-tactics}
   234 \index{simplification!tactics}
   352 \index{simplification!tactics}
   235 \index{tactics!simplification}
   353 \index{tactics!simplification}
   236 
   354 
   237 The actual simplification work is performed by the following tactics.  The
   355 The actual simplification work is performed by the following basic tactics:
   238 rewriting strategy is strictly bottom up, except for congruence rules, which
   356 \ttindexbold{simp_tac}, \ttindexbold{asm_simp_tac},
   239 are applied while descending into a term.  Conditions in conditional rewrite
   357 \ttindexbold{full_simp_tac} and \ttindexbold{asm_full_simp_tac}. They work
   240 rules are solved recursively before the rewrite rule is applied.
   358 exactly like their namesakes in \S\ref{sec:simp-for-dummies}, except that
   241 
   359 they are explicitly supplied with a simpset. This is because the ones in
   242 There are three basic simplification tactics:
   360 \S\ref{sec:simp-for-dummies} are defined in terms of the ones above, e.g.
   243 \begin{ttdescription}
   361 \begin{ttbox}
   244 \item[\ttindexbold{simp_tac} $ss$ $i$] simplifies subgoal~$i$ using the rules
   362 fun Simp_tac i = simp_tac (!simpset) i;
   245   in~$ss$.  It may solve the subgoal completely if it has become trivial,
   363 \end{ttbox}
   246   using the solver.
   364 where \ttindex{!simpset} is the current simpset\index{simpset!current}.
   247   
   365 
   248 \item[\ttindexbold{asm_simp_tac}]\index{assumptions!in simplification}
   366 The rewriting strategy of all four tactics is strictly bottom up, except for
   249   is like \verb$simp_tac$, but extracts additional rewrite rules from the
   367 congruence rules, which are applied while descending into a term.  Conditions
   250   assumptions.
   368 in conditional rewrite rules are solved recursively before the rewrite rule
   251 
   369 is applied.
   252 \item[\ttindexbold{asm_full_simp_tac}] 
   370 
   253   is like \verb$asm_simp_tac$, but also simplifies the assumptions one by
   371 The infix operations \ttindex{addsimps}/\ttindex{delsimps} add/delete rewrite
   254   one.  Working from left to right, it uses each assumption in the
   372 rules to/from a simpset. They are used to implement \ttindex{Addsimps} and
   255   simplification of those following.
   373 \ttindex{Delsimps}, e.g.
   256 \end{ttdescription}
   374 \begin{ttbox}
   257 \begin{warn}
   375 fun Addsimps thms = (simpset := (!simpset addsimps thms));
   258   Since \verb$asm_full_simp_tac$ works from left to right, it sometimes
   376 \end{ttbox}
   259 misses opportunities for simplification: given the subgoal
   377 and can also be used directly, even in the presence of a current simpset. For
   260 \begin{ttbox}
   378 example
   261 {\out [| P(f(a)); f(a) = t |] ==> ...}
   379 \begin{ttbox}
   262 \end{ttbox}
   380 Addsimps \(thms\);
   263 \verb$asm_full_simp_tac$ will not simplify the first assumption using the
   381 by(Simp_tac \(i\));
   264 second but will leave the assumptions unchanged. See
   382 Delsimps \(thms\);
   265 \S\ref{sec:reordering-asms} for ways around this problem.
   383 \end{ttbox}
   266 \end{warn}
   384 can be compressed into
   267 Using the simplifier effectively may take a bit of experimentation.  The
   385 \begin{ttbox}
   268 tactics can be traced with the ML command \verb$trace_simp := true$.  To
   386 by(simp_tac (!simpset addsimps \(thms\)) \(i\));
   269 remind yourself of what is in a simpset, use the function \verb$rep_ss$ to
   387 \end{ttbox}
       
   388 
       
   389 The simpset associated with a particular theory can be retrieved via the name
       
   390 of the theory using the function
       
   391 \begin{ttbox}
       
   392 simpset_of: string -> simpset
       
   393 \end{ttbox}\index{*simpset_of}
       
   394 
       
   395 To remind yourself of what is in a simpset, use the function \verb$rep_ss$ to
   270 return its simplification and congruence rules.
   396 return its simplification and congruence rules.
   271 
   397 
   272 \section{Examples using the simplifier}
   398 \section{Examples using the simplifier}
   273 \index{examples!of simplification}
   399 \index{examples!of simplification}
   274 Assume we are working within {\tt FOL} and that
   400 Assume we are working within {\tt FOL} and that