doc-src/Ref/simplifier.tex
changeset 5549 7e91d450fd6f
parent 5370 ba0470fe09fc
child 5574 620130d6b8e6
equal deleted inserted replaced
5548:5cd3396802f5 5549:7e91d450fd6f
   133 Delsimps    : thm list -> unit
   133 Delsimps    : thm list -> unit
   134 Addsimprocs : simproc list -> unit
   134 Addsimprocs : simproc list -> unit
   135 Delsimprocs : simproc list -> unit
   135 Delsimprocs : simproc list -> unit
   136 Addcongs    : thm list -> unit
   136 Addcongs    : thm list -> unit
   137 Delcongs    : thm list -> unit
   137 Delcongs    : thm list -> unit
       
   138 Addsplits   : thm list -> unit
       
   139 Delsplits   : thm list -> unit
   138 \end{ttbox}
   140 \end{ttbox}
   139 
   141 
   140 Depending on the theory context, the \texttt{Add} and \texttt{Del}
   142 Depending on the theory context, the \texttt{Add} and \texttt{Del}
   141 functions manipulate basic components of the associated current
   143 functions manipulate basic components of the associated current
   142 simpset.  Internally, all rewrite rules have to be expressed as
   144 simpset.  Internally, all rewrite rules have to be expressed as
   162   procedures $procs$ from the current simpset.
   164   procedures $procs$ from the current simpset.
   163   
   165   
   164 \item[\ttindexbold{Addcongs} $thms$;] adds congruence rules to the
   166 \item[\ttindexbold{Addcongs} $thms$;] adds congruence rules to the
   165   current simpset.
   167   current simpset.
   166   
   168   
   167 \item[\ttindexbold{Delcongs} $thms$;] deletes congruence rules to the
   169 \item[\ttindexbold{Delcongs} $thms$;] deletes congruence rules from the
       
   170   current simpset.
       
   171 
       
   172 \item[\ttindexbold{Addsplits} $thms$;] adds splitting rules to the
       
   173   current simpset.
       
   174   
       
   175 \item[\ttindexbold{Delsplits} $thms$;] deletes splitting rules from the
   168   current simpset.
   176   current simpset.
   169 
   177 
   170 \end{ttdescription}
   178 \end{ttdescription}
   171 
   179 
   172 When a new theory is built, its implicit simpset is initialized by the
   180 When a new theory is built, its implicit simpset is initialized by the
   595 \end{warn}
   603 \end{warn}
   596 
   604 
   597 
   605 
   598 \subsection{*The looper}\label{sec:simp-looper}
   606 \subsection{*The looper}\label{sec:simp-looper}
   599 \begin{ttbox}
   607 \begin{ttbox}
   600 setloop   : simpset * (int -> tactic) -> simpset \hfill{\bf infix 4}
   608 setloop   : simpset *           (int -> tactic)  -> simpset \hfill{\bf infix 4}
   601 addloop   : simpset * (int -> tactic) -> simpset \hfill{\bf infix 4}
   609 addloop   : simpset * (string * (int -> tactic)) -> simpset \hfill{\bf infix 4}
       
   610 delloop   : simpset *  string                    -> simpset \hfill{\bf infix 4}
   602 addsplits : simpset * thm list -> simpset \hfill{\bf infix 4}
   611 addsplits : simpset * thm list -> simpset \hfill{\bf infix 4}
   603 \end{ttbox}
   612 delsplits : simpset * thm list -> simpset \hfill{\bf infix 4}
   604 
   613 \end{ttbox}
   605 The looper is a tactic that is applied after simplification, in case
   614 
       
   615 The looper is a list of tactics that are applied after simplification, in case
   606 the solver failed to solve the simplified goal.  If the looper
   616 the solver failed to solve the simplified goal.  If the looper
   607 succeeds, the simplification process is started all over again.  Each
   617 succeeds, the simplification process is started all over again.  Each
   608 of the subgoals generated by the looper is attacked in turn, in
   618 of the subgoals generated by the looper is attacked in turn, in
   609 reverse order.
   619 reverse order.
   610 
   620 
   612 Another possibility is to apply an elimination rule on the
   622 Another possibility is to apply an elimination rule on the
   613 assumptions.  More adventurous loopers could start an induction.
   623 assumptions.  More adventurous loopers could start an induction.
   614 
   624 
   615 \begin{ttdescription}
   625 \begin{ttdescription}
   616   
   626   
   617 \item[$ss$ \ttindexbold{setloop} $tacf$] installs $tacf$ as the looper
   627 \item[$ss$ \ttindexbold{setloop} $tacf$] installs $tacf$ as the only looper
   618   of $ss$.
   628   tactic of $ss$.
   619   
   629   
   620 \item[$ss$ \ttindexbold{addloop} $tacf$] adds $tacf$ as an additional
   630 \item[$ss$ \ttindexbold{addloop} $(name,tacf)$] adds $tacf$ as an additional
   621   looper; it will be tried after the loopers which had already been
   631   looper tactic with name $name$; it will be tried after the looper tactics
   622   present in $ss$.
   632   that had already been present in $ss$.
       
   633   
       
   634 \item[$ss$ \ttindexbold{delloop} $name$] deletes the looper tactic $name$
       
   635   from $ss$.
   623   
   636   
   624 \item[$ss$ \ttindexbold{addsplits} $thms$] adds
   637 \item[$ss$ \ttindexbold{addsplits} $thms$] adds
   625   \texttt{(split_tac~$thms$)} as an additional looper.
   638   split tactics for $thms$ as additional looper tactics of $ss$.
       
   639 
       
   640 \item[$ss$ \ttindexbold{addsplits} $thms$] deletes the
       
   641   split tactics for $thms$ from the looper tactics of $ss$.
   626 
   642 
   627 \end{ttdescription}
   643 \end{ttdescription}
   628 
   644 
       
   645 The splitter replaces applications of a given function; the right-hand side
       
   646 of the replacement can be anything.  For example, here is a splitting rule
       
   647 for conditional expressions:
       
   648 \[ \Var{P}(if(\Var{Q},\Var{x},\Var{y})) \bimp (\Var{Q} \imp \Var{P}(\Var{x}))
       
   649 \conj (\lnot\Var{Q} \imp \Var{P}(\Var{y})) 
       
   650 \] 
       
   651 Another example is the elimination operator (which happens to be
       
   652 called~$split$) for Cartesian products:
       
   653 \[ \Var{P}(split(\Var{f},\Var{p})) \bimp (\forall a~b. \Var{p} =
       
   654 \langle a,b\rangle \imp \Var{P}(\Var{f}(a,b))) 
       
   655 \] 
       
   656 
       
   657 For technical reasons, there is a distinction between case splitting in the 
       
   658 conclusion and in the premises of a subgoal. The former is done by
       
   659 \texttt{split_tac} with rules like \texttt{split_if}, 
       
   660 which does not split the subgoal, while the latter is done by 
       
   661 \texttt{split_asm_tac} with rules like \texttt{split_if_asm}, 
       
   662 which splits the subgoal.
       
   663 The operator \texttt{addsplits} automatically takes care of which tactic to
       
   664 call, analyzing the form of the rules given as argument.
       
   665 \begin{warn}
       
   666 Due to \texttt{split_asm_tac}, the simplifier may split subgoals!
       
   667 \end{warn}
       
   668 
       
   669 Case splits should be allowed only when necessary; they are expensive
       
   670 and hard to control.  Here is an example of use, where \texttt{split_if}
       
   671 is the first rule above:
       
   672 \begin{ttbox}
       
   673 by (simp_tac (simpset() addloop ("split if",split_tac [split_if])) 1);
       
   674 \end{ttbox}
       
   675 Users would usually prefer the following shortcut using
       
   676 \ttindex{addsplits}:
       
   677 \begin{ttbox}
       
   678 by (simp_tac (simpset() addsplits [split_if]) 1);
       
   679 \end{ttbox}
   629 
   680 
   630 
   681 
   631 \section{The simplification tactics}\label{simp-tactics}
   682 \section{The simplification tactics}\label{simp-tactics}
   632 \index{simplification!tactics}\index{tactics!simplification}
   683 \index{simplification!tactics}\index{tactics!simplification}
   633 \begin{ttbox}
   684 \begin{ttbox}
  1264 quantifiers from the front of the given theorem.  Usually there are none
  1315 quantifiers from the front of the given theorem.  Usually there are none
  1265 anyway.
  1316 anyway.
  1266 \begin{ttbox}
  1317 \begin{ttbox}
  1267 fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
  1318 fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
  1268 \end{ttbox}
  1319 \end{ttbox}
       
  1320 
  1269 The function \texttt{atomize} analyses a theorem in order to extract
  1321 The function \texttt{atomize} analyses a theorem in order to extract
  1270 atomic rewrite rules.  The head of all the patterns, matched by the
  1322 atomic rewrite rules.  The head of all the patterns, matched by the
  1271 wildcard~\texttt{_}, is the coercion function \texttt{Trueprop}.
  1323 wildcard~\texttt{_}, is the coercion function \texttt{Trueprop}.
  1272 \begin{ttbox}
  1324 \begin{ttbox}
  1273 fun atomize th = case concl_of th of 
  1325 fun atomize th = case concl_of th of 
  1280   | _                               => [th];
  1332   | _                               => [th];
  1281 \end{ttbox}
  1333 \end{ttbox}
  1282 There are several cases, depending upon the form of the conclusion:
  1334 There are several cases, depending upon the form of the conclusion:
  1283 \begin{itemize}
  1335 \begin{itemize}
  1284 \item Conjunction: extract rewrites from both conjuncts.
  1336 \item Conjunction: extract rewrites from both conjuncts.
  1285 
       
  1286 \item Implication: convert $P\imp Q$ to the meta-implication $P\Imp Q$ and
  1337 \item Implication: convert $P\imp Q$ to the meta-implication $P\Imp Q$ and
  1287   extract rewrites from~$Q$; these will be conditional rewrites with the
  1338   extract rewrites from~$Q$; these will be conditional rewrites with the
  1288   condition~$P$.
  1339   condition~$P$.
  1289 
       
  1290 \item Universal quantification: remove the quantifier, replacing the bound
  1340 \item Universal quantification: remove the quantifier, replacing the bound
  1291   variable by a schematic variable, and extract rewrites from the body.
  1341   variable by a schematic variable, and extract rewrites from the body.
  1292 
       
  1293 \item \texttt{True} and \texttt{False} contain no useful rewrites.
  1342 \item \texttt{True} and \texttt{False} contain no useful rewrites.
  1294 
       
  1295 \item Anything else: return the theorem in a singleton list.
  1343 \item Anything else: return the theorem in a singleton list.
  1296 \end{itemize}
  1344 \end{itemize}
  1297 The resulting theorems are not literally atomic --- they could be
  1345 The resulting theorems are not literally atomic --- they could be
  1298 disjunctive, for example --- but are broken down as much as possible.  See
  1346 disjunctive, for example --- but are broken down as much as possible. 
  1299 the file \texttt{ZF/simpdata.ML} for a sophisticated translation of
  1347 See the file \texttt{ZF/simpdata.ML} for a sophisticated translation of
  1300 set-theoretic formulae into rewrite rules.
  1348 set-theoretic formulae into rewrite rules. 
       
  1349 
       
  1350 For standard situations like the above,
       
  1351 there is a generic auxiliary function \ttindexbold{mk_atomize} that takes a 
       
  1352 list of pairs $(name, thms)$, where $name$ is an operator name and
       
  1353 $thms$ is a list of theorems to resolve with in case the pattern matches, 
       
  1354 and returns a suitable \texttt{atomize} function.
       
  1355 
  1301 
  1356 
  1302 The simplified rewrites must now be converted into meta-equalities.  The
  1357 The simplified rewrites must now be converted into meta-equalities.  The
  1303 rule \texttt{eq_reflection} converts equality rewrites, while {\tt
  1358 rule \texttt{eq_reflection} converts equality rewrites, while {\tt
  1304   iff_reflection} converts if-and-only-if rewrites.  The latter possibility
  1359   iff_reflection} converts if-and-only-if rewrites.  The latter possibility
  1305 can arise in two other ways: the negative theorem~$\neg P$ is converted to
  1360 can arise in two other ways: the negative theorem~$\neg P$ is converted to
  1311 val iff_reflection_F = P_iff_F RS iff_reflection;
  1366 val iff_reflection_F = P_iff_F RS iff_reflection;
  1312 \ttbreak
  1367 \ttbreak
  1313 val P_iff_T = int_prove_fun "P ==> (P <-> True)";
  1368 val P_iff_T = int_prove_fun "P ==> (P <-> True)";
  1314 val iff_reflection_T = P_iff_T RS iff_reflection;
  1369 val iff_reflection_T = P_iff_T RS iff_reflection;
  1315 \end{ttbox}
  1370 \end{ttbox}
  1316 The function \texttt{mk_meta_eq} converts a theorem to a meta-equality
  1371 The function \texttt{mk_eq} converts a theorem to a meta-equality
  1317 using the case analysis described above.
  1372 using the case analysis described above.
  1318 \begin{ttbox}
  1373 \begin{ttbox}
  1319 fun mk_meta_eq th = case concl_of th of
  1374 fun mk_eq th = case concl_of th of
  1320     _ $ (Const("op =",_)$_$_)   => th RS eq_reflection
  1375     _ $ (Const("op =",_)$_$_)   => th RS eq_reflection
  1321   | _ $ (Const("op <->",_)$_$_) => th RS iff_reflection
  1376   | _ $ (Const("op <->",_)$_$_) => th RS iff_reflection
  1322   | _ $ (Const("Not",_)$_)      => th RS iff_reflection_F
  1377   | _ $ (Const("Not",_)$_)      => th RS iff_reflection_F
  1323   | _                           => th RS iff_reflection_T;
  1378   | _                           => th RS iff_reflection_T;
  1324 \end{ttbox}
  1379 \end{ttbox}
  1325 The three functions \texttt{gen_all}, \texttt{atomize} and \texttt{mk_meta_eq} will
  1380 The three functions \texttt{gen_all}, \texttt{atomize} and \texttt{mk_eq} 
  1326 be composed together and supplied below to \texttt{setmksimps}.
  1381 will be composed together and supplied below to \texttt{setmksimps}.
  1327 
  1382 
  1328 
  1383 
  1329 \subsection{Making the initial simpset}
  1384 \subsection{Making the initial simpset}
  1330 
  1385 
  1331 It is time to assemble these items.  We define the infix operator
  1386 It is time to assemble these items.  We define the infix operator
  1335 \begin{ttbox}
  1390 \begin{ttbox}
  1336 infix 4 addcongs;
  1391 infix 4 addcongs;
  1337 fun ss addcongs congs =
  1392 fun ss addcongs congs =
  1338     ss addeqcongs (congs RL [eq_reflection,iff_reflection]);
  1393     ss addeqcongs (congs RL [eq_reflection,iff_reflection]);
  1339 \end{ttbox}
  1394 \end{ttbox}
  1340 Furthermore, we define the infix operator \ttindex{addsplits}
       
  1341 providing a convenient interface for adding split tactics.
       
  1342 \begin{ttbox}
       
  1343 infix 4 addsplits;
       
  1344 fun ss addsplits splits = ss addloop (split_tac splits);
       
  1345 \end{ttbox}
       
  1346 
  1395 
  1347 The list \texttt{IFOL_simps} contains the default rewrite rules for
  1396 The list \texttt{IFOL_simps} contains the default rewrite rules for
  1348 intuitionistic first-order logic.  The first of these is the reflexive law
  1397 intuitionistic first-order logic.  The first of these is the reflexive law
  1349 expressed as the equivalence $(a=a)\bimp\texttt{True}$; the rewrite rule $a=a$ is
  1398 expressed as the equivalence $(a=a)\bimp\texttt{True}$; the rewrite rule $a=a$ is
  1350 clearly useless.
  1399 clearly useless.
  1360 val triv_rls = [TrueI,refl,iff_refl,notFalseI];
  1409 val triv_rls = [TrueI,refl,iff_refl,notFalseI];
  1361 \end{ttbox}
  1410 \end{ttbox}
  1362 %
  1411 %
  1363 The basic simpset for intuitionistic \FOL{} is
  1412 The basic simpset for intuitionistic \FOL{} is
  1364 \ttindexbold{FOL_basic_ss}.  It preprocess rewrites using {\tt
  1413 \ttindexbold{FOL_basic_ss}.  It preprocess rewrites using {\tt
  1365   gen_all}, \texttt{atomize} and \texttt{mk_meta_eq}.  It solves simplified
  1414   gen_all}, \texttt{atomize} and \texttt{mk_eq}.  It solves simplified
  1366 subgoals using \texttt{triv_rls} and assumptions, and by detecting
  1415 subgoals using \texttt{triv_rls} and assumptions, and by detecting
  1367 contradictions.  It uses \ttindex{asm_simp_tac} to tackle subgoals of
  1416 contradictions.  It uses \ttindex{asm_simp_tac} to tackle subgoals of
  1368 conditional rewrites.
  1417 conditional rewrites.
  1369 
  1418 
  1370 Other simpsets built from \texttt{FOL_basic_ss} will inherit these items.
  1419 Other simpsets built from \texttt{FOL_basic_ss} will inherit these items.
  1383 
  1432 
  1384 val FOL_basic_ss = empty_ss setsubgoaler asm_simp_tac
  1433 val FOL_basic_ss = empty_ss setsubgoaler asm_simp_tac
  1385                             addsimprocs [defALL_regroup, defEX_regroup]
  1434                             addsimprocs [defALL_regroup, defEX_regroup]
  1386                             setSSolver   safe_solver
  1435                             setSSolver   safe_solver
  1387                             setSolver  unsafe_solver
  1436                             setSolver  unsafe_solver
  1388                             setmksimps (map mk_meta_eq o 
  1437                             setmksimps (map mk_eq o atomize o gen_all);
  1389                                         atomize o gen_all);
       
  1390 
  1438 
  1391 val IFOL_ss = FOL_basic_ss addsimps (IFOL_simps {\at} 
  1439 val IFOL_ss = FOL_basic_ss addsimps (IFOL_simps {\at} 
  1392                                      int_ex_simps {\at} int_all_simps)
  1440                                      int_ex_simps {\at} int_all_simps)
  1393                            addcongs [imp_cong];
  1441                            addcongs [imp_cong];
  1394 \end{ttbox}
  1442 \end{ttbox}
  1399 \]
  1447 \]
  1400 By adding the congruence rule \texttt{conj_cong}, we could obtain a similar
  1448 By adding the congruence rule \texttt{conj_cong}, we could obtain a similar
  1401 effect for conjunctions.
  1449 effect for conjunctions.
  1402 
  1450 
  1403 
  1451 
  1404 \subsection{Case splitting}
  1452 \subsection{Splitter setup}\index{simplification!setting up the splitter}
  1405 
  1453 
  1406 To set up case splitting, we must prove the theorem \texttt{meta_iffD}
  1454 To set up case splitting, we have to call the \ML{} functor \ttindex{
  1407 below and pass it to \ttindexbold{mk_case_split_tac}.  The tactic
  1455 SplitterFun}, which takes the argument signature \texttt{SPLITTER_DATA}. 
  1408 \ttindexbold{split_tac} uses \texttt{mk_meta_eq}, defined above, to
  1456 So we prove the theorem \texttt{meta_eq_to_iff} below and store it, together
  1409 convert the splitting rules to meta-equalities.
  1457 with the \texttt{mk_eq} function described above and several standard
  1410 \begin{ttbox}
  1458 theorems, in the structure \texttt{SplitterData}. Calling the functor with
  1411 val meta_iffD = 
  1459 this data yields a new instantiation of the splitter for our logic.
  1412     prove_goal FOL.thy "[| P==Q; Q |] ==> P"
  1460 \begin{ttbox}
  1413         (fn [prem1,prem2] => [rewtac prem1, rtac prem2 1])
  1461 val meta_eq_to_iff = prove_goal IFOL.thy "x==y ==> x<->y"
       
  1462   (fn [prem] => [rewtac prem, rtac iffI 1, atac 1, atac 1]);
  1414 \ttbreak
  1463 \ttbreak
  1415 fun split_tac splits =
  1464 structure SplitterData =
  1416     mk_case_split_tac meta_iffD (map mk_meta_eq splits);
  1465   struct
  1417 \end{ttbox}
  1466   structure Simplifier = Simplifier
  1418 %
  1467   val mk_eq          = mk_eq
  1419 The splitter replaces applications of a given function; the right-hand side
  1468   val meta_eq_to_iff = meta_eq_to_iff
  1420 of the replacement can be anything.  For example, here is a splitting rule
  1469   val iffD           = iffD2
  1421 for conditional expressions:
  1470   val disjE          = disjE
  1422 \[ \Var{P}(if(\Var{Q},\Var{x},\Var{y})) \bimp (\Var{Q} \imp \Var{P}(\Var{x}))
  1471   val conjE          = conjE
  1423 \conj (\lnot\Var{Q} \imp \Var{P}(\Var{y})) 
  1472   val exE            = exE
  1424 \] 
  1473   val contrapos      = contrapos
  1425 Another example is the elimination operator (which happens to be
  1474   val contrapos2     = contrapos2
  1426 called~$split$) for Cartesian products:
  1475   val notnotD        = notnotD
  1427 \[ \Var{P}(split(\Var{f},\Var{p})) \bimp (\forall a~b. \Var{p} =
  1476   end;
  1428 \langle a,b\rangle \imp \Var{P}(\Var{f}(a,b))) 
  1477 \ttbreak
  1429 \] 
  1478 structure Splitter = SplitterFun(SplitterData);
  1430 Case splits should be allowed only when necessary; they are expensive
       
  1431 and hard to control.  Here is an example of use, where \texttt{expand_if}
       
  1432 is the first rule above:
       
  1433 \begin{ttbox}
       
  1434 by (simp_tac (simpset() addloop (split_tac [expand_if])) 1);
       
  1435 \end{ttbox}
       
  1436 Users would usually prefer the following shortcut using
       
  1437 \ttindex{addsplits}:
       
  1438 \begin{ttbox}
       
  1439 by (simp_tac (simpset() addsplits [expand_if]) 1);
       
  1440 \end{ttbox}
  1479 \end{ttbox}
  1441 
  1480 
  1442 
  1481 
  1443 \subsection{Theory setup}\index{simplification!setting up the theory}
  1482 \subsection{Theory setup}\index{simplification!setting up the theory}
  1444 \begin{ttbox}\indexbold{*Simplifier.setup}\index{*setup!simplifier}
  1483 \begin{ttbox}\indexbold{*Simplifier.setup}\index{*setup!simplifier}