ProgTutorial/Tactical.thy
changeset 368 b1a458a03a8e
parent 363 f7f1d8a98098
child 369 74ba778b09c9
equal deleted inserted replaced
367:643b1e1d7d29 368:b1a458a03a8e
    20   \isacommand{apply}-style reasoning at the user-level, where goals are
    20   \isacommand{apply}-style reasoning at the user-level, where goals are
    21   modified in a sequence of proof steps until all of them are discharged.
    21   modified in a sequence of proof steps until all of them are discharged.
    22   In this chapter we will explain simple tactics and how to combine them using
    22   In this chapter we will explain simple tactics and how to combine them using
    23   tactic combinators. We also describe the simplifier, simprocs and conversions.
    23   tactic combinators. We also describe the simplifier, simprocs and conversions.
    24 *}
    24 *}
    25 
       
    26 
    25 
    27 section {* Basics of Reasoning with Tactics*}
    26 section {* Basics of Reasoning with Tactics*}
    28 
    27 
    29 text {*
    28 text {*
    30   To see how tactics work, let us first transcribe a simple \isacommand{apply}-style proof 
    29   To see how tactics work, let us first transcribe a simple \isacommand{apply}-style proof 
    97   for @{ML disjE} can be re-assigned and thus one does not have
    96   for @{ML disjE} can be re-assigned and thus one does not have
    98   complete control over which theorem is actually applied. Similarly with the
    97   complete control over which theorem is actually applied. Similarly with the
    99   lookup of @{text [quotes] "disjE"}. Although theorems must have a unique name
    98   lookup of @{text [quotes] "disjE"}. Although theorems must have a unique name
   100   in the theorem database, the string can stand for a dynamically updatable
    99   in the theorem database, the string can stand for a dynamically updatable
   101   theorem list. Also in this case we cannot be sure which theorem is applied.
   100   theorem list. Also in this case we cannot be sure which theorem is applied.
   102   These problems can be nicely prevented by using antiquotations, because then
   101   These problems can be nicely prevented by using antiquotations
   103   the theorems are fixed statically at compile-time.
   102 
   104 
   103   @{ML_response_fake [gray,display]
       
   104   "@{thm \"disjE\"}"
       
   105   "\<lbrakk>?P \<or> ?Q; ?P \<Longrightarrow> ?R; ?Q \<Longrightarrow> ?R\<rbrakk> \<Longrightarrow> ?R"}
       
   106 
       
   107   because then the theorems are fixed statically at compile-time.
   105 
   108 
   106   During the development of automatic proof procedures, you will often find it
   109   During the development of automatic proof procedures, you will often find it
   107   necessary to test a tactic on examples. This can be conveniently done with
   110   necessary to test a tactic on examples. This can be conveniently done with
   108   the command \isacommand{apply}@{text "(tactic \<verbopen> \<dots> \<verbclose>)"}. 
   111   the command \isacommand{apply}@{text "(tactic \<verbopen> \<dots> \<verbclose>)"}. 
   109   Consider the following sequence of tactics
   112   Consider the following sequence of tactics
   389 *}
   392 *}
   390 
   393 
   391 section {* Simple Tactics\label{sec:simpletacs} *}
   394 section {* Simple Tactics\label{sec:simpletacs} *}
   392 
   395 
   393 text {*
   396 text {*
   394   In this section we will introduce more simple tactics. The 
   397   In this section we will introduce more of the simple tactics in Isabelle. The 
   395   first one is @{ML_ind print_tac in Tactical}, which is quite useful 
   398   first one is @{ML_ind print_tac in Tactical}, which is quite useful 
   396   for low-level debugging of tactics. It just prints out a message and the current 
   399   for low-level debugging of tactics. It just prints out a message and the current 
   397   goal state. Unlike @{ML my_print_tac} shown earlier, it prints the goal state 
   400   goal state. Unlike @{ML my_print_tac} shown earlier, it prints the goal state 
   398   as the user would see it.  For example, processing the proof
   401   as the user would see it.  For example, processing the proof
   399 *}
   402 *}
   400  
   403  
   401 lemma 
   404 lemma 
   402   shows "False \<Longrightarrow> True"
   405   shows "False \<Longrightarrow> True"
   403 apply(tactic {* print_tac "foo message" *})
   406 apply(tactic {* print_tac "foo message" *})
   404 txt{*gives:\medskip
   407 txt{*gives:
   405 
   408      \begin{isabelle}
   406      \begin{minipage}{\textwidth}\small
       
   407      @{text "foo message"}\\[3mm] 
   409      @{text "foo message"}\\[3mm] 
   408      @{prop "False \<Longrightarrow> True"}\\
   410      @{prop "False \<Longrightarrow> True"}\\
   409      @{text " 1. False \<Longrightarrow> True"}\\
   411      @{text " 1. False \<Longrightarrow> True"}\\
   410      \end{minipage}
   412      \end{isabelle}
   411    *}
   413    *}
   412 (*<*)oops(*>*)
   414 (*<*)oops(*>*)
   413 
   415 
   414 text {*
   416 text {*
   415   A simple tactic for easy discharge of any proof obligations, even difficult ones, is 
   417   A simple tactic for easy discharge of any proof obligations, even difficult ones, is 
   428 
   430 
   429 text {*
   431 text {*
   430   This tactic works however only if the quick-and-dirty mode of Isabelle 
   432   This tactic works however only if the quick-and-dirty mode of Isabelle 
   431   is switched on. This is done automatically in the ``interactive
   433   is switched on. This is done automatically in the ``interactive
   432   mode'' of Isabelle, but must be done manually in the ``batch mode''
   434   mode'' of Isabelle, but must be done manually in the ``batch mode''
   433   with for example the assignment
   435   with the assignment
   434 *}
   436 *}
   435 
   437 
   436 ML{*quick_and_dirty := true*}
   438 ML{*quick_and_dirty := true*}
   437 
   439 
   438 text {*
   440 text {*
   516 *}
   518 *}
   517 
   519 
   518 lemma 
   520 lemma 
   519   shows "True \<noteq> False"
   521   shows "True \<noteq> False"
   520 apply(tactic {* cut_facts_tac [@{thm True_def}, @{thm False_def}] 1 *})
   522 apply(tactic {* cut_facts_tac [@{thm True_def}, @{thm False_def}] 1 *})
   521 txt{*produces the goal state\medskip
   523 txt{*produces the goal state
   522 
   524      \begin{isabelle}
   523      \begin{minipage}{\textwidth}
       
   524      @{subgoals [display]} 
   525      @{subgoals [display]} 
   525      \end{minipage}*}
   526      \end{isabelle}*}
   526 (*<*)oops(*>*)
   527 (*<*)oops(*>*)
   527 
   528 
   528 text {*
   529 text {*
   529   Often proofs on the ML-level involve elaborate operations on assumptions and 
   530   Often proofs on the ML-level involve elaborate operations on assumptions and 
   530   @{text "\<And>"}-quantified variables. To do such operations using the basic tactics 
   531   @{text "\<And>"}-quantified variables. To do such operations using the basic tactics 
   591   conclusion:  & @{term "B y x \<longrightarrow> C (z y) x"}\\
   592   conclusion:  & @{term "B y x \<longrightarrow> C (z y) x"}\\
   592   premises:    & @{term "A x y"}
   593   premises:    & @{term "A x y"}
   593   \end{tabular}
   594   \end{tabular}
   594   \end{quote}
   595   \end{quote}
   595 
   596 
   596   The @{text params} and @{text schematics} stand or list of pairs where the
   597   The @{text params} and @{text schematics} stand for list of pairs where the
   597   left-hand side of @{text ":="} is replaced by the right-hand side inside the
   598   left-hand side of @{text ":="} is replaced by the right-hand side inside the
   598   tactic.  Notice that in the actual output the variables @{term x} and @{term
   599   tactic.  Notice that in the actual output the variables @{term x} and @{term
   599   y} have a brown colour. Although they are parameters in the original goal,
   600   y} have a brown colour. Although they are parameters in the original goal,
   600   they are fixed inside the tactic. By convention these fixed variables are
   601   they are fixed inside the tactic. By convention these fixed variables are
   601   printed in brown colour.  Similarly the schematic variable @{text ?z}. The
   602   printed in brown colour.  Similarly the schematic variable @{text ?z}. The
   754   implementing a tactic analysing a goal according to its topmost
   755   implementing a tactic analysing a goal according to its topmost
   755   connective. These simpler methods use tactic combinators, which we will
   756   connective. These simpler methods use tactic combinators, which we will
   756   explain in the next section. But before that we will show how
   757   explain in the next section. But before that we will show how
   757   tactic application can be constrained.
   758   tactic application can be constrained.
   758 
   759 
   759   Since rules are applied using higher-order unification, an automatic proof
   760   \begin{readmore}
   760   procedure might become too fragile, if it just applies inference rules as 
   761   The functions @{ML SUBGOAL} and @{ML CSUBGOAL} are defined in @{ML_file "Pure/tactical.ML"}.
   761   shown above. The reason is that a number of rules introduce schematic variables 
   762   \end{readmore}
   762   into the goal state. Consider for example the proof
   763 
       
   764 
       
   765   Since @{ML_ind rtac in Tactic} and the like use higher-order unification, an
       
   766   automatic proof procedure based on them might become too fragile, if it just
       
   767   applies theorems as shown above. The reason is that a number of theorems
       
   768   introduce schematic variables into the goal state. Consider for example the
       
   769   proof
   763 *}
   770 *}
   764 
   771 
   765 lemma 
   772 lemma 
   766   shows "\<forall>x \<in> A. P x \<Longrightarrow> Q x"
   773   shows "\<forall>x \<in> A. P x \<Longrightarrow> Q x"
   767 apply(tactic {* dtac @{thm bspec} 1 *})
   774 apply(tactic {* dtac @{thm bspec} 1 *})
   769      @{subgoals [display]} 
   776      @{subgoals [display]} 
   770      \end{minipage}*}
   777      \end{minipage}*}
   771 (*<*)oops(*>*)
   778 (*<*)oops(*>*)
   772 
   779 
   773 text {*
   780 text {*
   774   where the application of rule @{text bspec} generates two subgoals involving the
   781   where the application of theorem @{text bspec} generates two subgoals involving the
   775   schematic variable @{text "?x"}. Now, if you are not careful, tactics 
   782   new schematic variable @{text "?x"}. Now, if you are not careful, tactics 
   776   applied to the first subgoal might instantiate this schematic variable in such a 
   783   applied to the first subgoal might instantiate this schematic variable in such a 
   777   way that the second subgoal becomes unprovable. If it is clear what the @{text "?x"}
   784   way that the second subgoal becomes unprovable. If it is clear what the @{text "?x"}
   778   should be, then this situation can be avoided by introducing a more
   785   should be, then this situation can be avoided by introducing a more
   779   constrained version of the @{text bspec}-rule. One way to give such 
   786   constrained version of the @{text bspec}-theorem. One way to give such 
   780   constraints is by pre-instantiating theorems with other theorems. 
   787   constraints is by pre-instantiating theorems with other theorems. 
   781   The function @{ML_ind RS in Drule}, for example, does this.
   788   The function @{ML_ind RS in Drule}, for example, does this.
   782   
   789   
   783   @{ML_response_fake [display,gray]
   790   @{ML_response_fake [display,gray]
   784   "@{thm disjI1} RS @{thm conjI}" "\<lbrakk>?P1; ?Q\<rbrakk> \<Longrightarrow> (?P1 \<or> ?Q1) \<and> ?Q"}
   791   "@{thm disjI1} RS @{thm conjI}" "\<lbrakk>?P1; ?Q\<rbrakk> \<Longrightarrow> (?P1 \<or> ?Q1) \<and> ?Q"}
   785 
   792 
   786   In this example it instantiates the first premise of the @{text conjI}-rule 
   793   In this example it instantiates the first premise of the @{text conjI}-theorem 
   787   with the rule @{text disjI1}. If the instantiation is impossible, as in the 
   794   with the theorem @{text disjI1}. If the instantiation is impossible, as in the 
   788   case of
   795   case of
   789 
   796 
   790   @{ML_response_fake_both [display,gray]
   797   @{ML_response_fake_both [display,gray]
   791   "@{thm conjI} RS @{thm mp}" 
   798   "@{thm conjI} RS @{thm mp}" 
   792 "*** Exception- THM (\"RSN: no unifiers\", 1, 
   799 "*** Exception- THM (\"RSN: no unifiers\", 1, 
   827  \<lbrakk>Q; Qa\<rbrakk> \<Longrightarrow> (P \<or> Q) \<and> Qa,
   834  \<lbrakk>Q; Qa\<rbrakk> \<Longrightarrow> (P \<or> Q) \<and> Qa,
   828  (P \<Longrightarrow> Q) \<Longrightarrow> (P \<longrightarrow> Q) \<or> Qa,
   835  (P \<Longrightarrow> Q) \<Longrightarrow> (P \<longrightarrow> Q) \<or> Qa,
   829  Q \<Longrightarrow> (P \<or> Q) \<or> Qa]"}
   836  Q \<Longrightarrow> (P \<or> Q) \<or> Qa]"}
   830 
   837 
   831   \begin{readmore}
   838   \begin{readmore}
   832   The combinators for instantiating theorems are defined in @{ML_file "Pure/drule.ML"}.
   839   The combinators for instantiating theorems with other theorems are 
       
   840   defined in @{ML_file "Pure/drule.ML"}.
   833   \end{readmore}
   841   \end{readmore}
   834 
   842 
   835   Higher-order unification is also often in the way when applying certain 
   843   Higher-order unification is also often in the way when applying certain 
   836   congruence theorems. For example we would expect that the theorem 
   844   congruence theorems. For example we would expect that the theorem 
   837   @{thm [source] cong}
   845   @{thm [source] cong}
   859   cong} with the method @{text rule}. The problem is
   867   cong} with the method @{text rule}. The problem is
   860   that higher-order unification produces an instantiation that is not the
   868   that higher-order unification produces an instantiation that is not the
   861   intended one. While we can use \isacommand{back} to interactively find the
   869   intended one. While we can use \isacommand{back} to interactively find the
   862   intended instantiation, this is not an option for an automatic proof
   870   intended instantiation, this is not an option for an automatic proof
   863   procedure. Fortunately, the tactic @{ML_ind cong_tac in Cong_Tac} helps 
   871   procedure. Fortunately, the tactic @{ML_ind cong_tac in Cong_Tac} helps 
   864   with applying congruence rules and finding the intended instantiation.
   872   with applying congruence theorems and finding the intended instantiation.
   865   For example
   873   For example
   866 *}
   874 *}
   867 
   875 
   868 lemma 
   876 lemma 
   869   fixes x y u w::"'a"
   877   fixes x y u w::"'a"
   873      @{subgoals [display]}
   881      @{subgoals [display]}
   874      \end{minipage}*}
   882      \end{minipage}*}
   875 (*<*)oops(*>*)
   883 (*<*)oops(*>*)
   876 
   884 
   877 text {*
   885 text {*
   878   However, frequently it is necessary to explicitly match a theorem against a proof
   886   However, frequently it is necessary to explicitly match a theorem against a goal
   879   state and in doing so construct manually an appropriate instantiation. Imagine 
   887   state and in doing so construct manually an appropriate instantiation. Imagine 
   880   you have the theorem
   888   you have the theorem
   881 *}
   889 *}
   882 
   890 
   883 lemma rule:
   891 lemma rule:
   926 (*<*)oops(*>*)
   934 (*<*)oops(*>*)
   927 
   935 
   928 text {*
   936 text {*
   929   We obtain the intended subgoals and also the parameters are correctly
   937   We obtain the intended subgoals and also the parameters are correctly
   930   introduced in both of them. Such manual instantiations are quite frequently
   938   introduced in both of them. Such manual instantiations are quite frequently
   931   necessary in order to appropriately constrain the application of inference 
   939   necessary in order to appropriately constrain the application of theorems. 
   932   rules. Otherwise one would end up with ``wild'' higher-order unification 
   940   Otherwise one can end up with ``wild'' higher-order unification  problems 
   933   problems that make automatic proofs fail.
   941   that make automatic proofs fail.
       
   942 
       
   943   \begin{readmore}
       
   944   Functions for matching @{ML_type cterm}s are defined in @{ML_file "Pure/thm.ML"}.
       
   945   Functions for instantiating schematic variables in theorems are defined
       
   946   in @{ML_file "Pure/drule.ML"}.
       
   947   \end{readmore}
   934 *}
   948 *}
   935 
   949 
   936 section {* Tactic Combinators *}
   950 section {* Tactic Combinators *}
   937 
   951 
   938 text {* 
   952 text {* 
   948        @{subgoals [display]} 
   962        @{subgoals [display]} 
   949        \end{minipage} *}
   963        \end{minipage} *}
   950 (*<*)oops(*>*)
   964 (*<*)oops(*>*)
   951 
   965 
   952 text {*
   966 text {*
   953   If you want to avoid the hard-coded subgoal addressing, then, as 
   967   If you want to avoid the hard-coded subgoal addressing in each component, 
   954   seen earlier, you can use
   968   then, as seen earlier, you can use the ``primed'' version of @{ML THEN}. 
   955   the ``primed'' version of @{ML THEN}. For example:
   969   For example:
   956 *}
   970 *}
   957 
   971 
   958 lemma 
   972 lemma 
   959   shows "(Foo \<and> Bar) \<and> False"
   973   shows "(Foo \<and> Bar) \<and> False"
   960 apply(tactic {* (rtac @{thm conjI} THEN' rtac @{thm conjI}) 1 *})
   974 apply(tactic {* (rtac @{thm conjI} THEN' rtac @{thm conjI}) 1 *})
   963        \end{minipage} *}
   977        \end{minipage} *}
   964 (*<*)oops(*>*)
   978 (*<*)oops(*>*)
   965 
   979 
   966 text {* 
   980 text {* 
   967   Here you have to specify the subgoal of interest only once and
   981   Here you have to specify the subgoal of interest only once and
   968   it is consistently applied to the component tactics.
   982   it is consistently applied to the component.
   969   For most tactic combinators such a ``primed'' version exists and
   983   For most tactic combinators such a ``primed'' version exists and
   970   in what follows we will usually prefer it over the ``unprimed'' one. 
   984   in what follows we will usually prefer it over the ``unprimed'' one. 
   971 
   985 
   972   If there is a list of tactics that should all be tried out in 
   986   If there is a list of tactics that should all be tried out in 
   973   sequence, you can use the combinator @{ML_ind  EVERY' in Tactical}. For example
   987   sequence, you can use the combinator @{ML_ind  EVERY' in Tactical}. For example
  1001 *}
  1015 *}
  1002 
  1016 
  1003 ML{*val orelse_xmp_tac = rtac @{thm disjI1} ORELSE' rtac @{thm conjI}*}
  1017 ML{*val orelse_xmp_tac = rtac @{thm disjI1} ORELSE' rtac @{thm conjI}*}
  1004 
  1018 
  1005 text {*
  1019 text {*
  1006   will first try out whether rule @{text disjI} applies and in case of failure 
  1020   will first try out whether theorem @{text disjI} applies and in case of failure 
  1007   will try @{text conjI}. To see this consider the proof
  1021   will try @{text conjI}. To see this consider the proof
  1008 *}
  1022 *}
  1009 
  1023 
  1010 lemma 
  1024 lemma 
  1011   shows "True \<and> False" and "Foo \<or> Bar"
  1025   shows "True \<and> False" and "Foo \<or> Bar"
  1012 apply(tactic {* orelse_xmp_tac 2 *})
  1026 apply(tactic {* orelse_xmp_tac 2 *})
  1013 apply(tactic {* orelse_xmp_tac 1 *})
  1027 apply(tactic {* orelse_xmp_tac 1 *})
  1014 txt {* which results in the goal state
  1028 txt {* which results in the goal state
  1015 
  1029        \begin{isabelle}
  1016        \begin{minipage}{\textwidth}
       
  1017        @{subgoals [display]} 
  1030        @{subgoals [display]} 
  1018        \end{minipage}
  1031        \end{isabelle}
  1019 *}
  1032 *}
  1020 (*<*)oops(*>*)
  1033 (*<*)oops(*>*)
  1021 
  1034 
  1022 
  1035 
  1023 text {*
  1036 text {*
  1029                           rtac @{thm notI}, rtac @{thm allI}, K all_tac]*}text_raw{*\label{tac:selectprime}*}
  1042                           rtac @{thm notI}, rtac @{thm allI}, K all_tac]*}text_raw{*\label{tac:selectprime}*}
  1030 
  1043 
  1031 text {*
  1044 text {*
  1032   Since we like to mimic the behaviour of @{ML select_tac} as closely as possible, 
  1045   Since we like to mimic the behaviour of @{ML select_tac} as closely as possible, 
  1033   we must include @{ML all_tac} at the end of the list, otherwise the tactic will
  1046   we must include @{ML all_tac} at the end of the list, otherwise the tactic will
  1034   fail if no rule applies (we also have to wrap @{ML all_tac} using the 
  1047   fail if no theorem applies (we also have to wrap @{ML all_tac} using the 
  1035   @{ML K}-combinator, because it does not take a subgoal number as argument). You can 
  1048   @{ML K}-combinator, because it does not take a subgoal number as argument). You can 
  1036   test the tactic on the same goals:
  1049   test the tactic on the same goals:
  1037 *}
  1050 *}
  1038 
  1051 
  1039 lemma 
  1052 lemma 
  1061       \end{minipage} *}
  1074       \end{minipage} *}
  1062 (*<*)oops(*>*)
  1075 (*<*)oops(*>*)
  1063 
  1076 
  1064 text {*
  1077 text {*
  1065   Remember that we chose to implement @{ML select_tac'} so that it 
  1078   Remember that we chose to implement @{ML select_tac'} so that it 
  1066   always  succeeds by adding @{ML all_tac} at the end of the tactic
  1079   always  succeeds by fact of having @{ML all_tac} at the end of the tactic
  1067   list. The same can be achieved with the tactic combinator @{ML_ind  TRY in Tactical}.
  1080   list. The same can be achieved with the tactic combinator @{ML_ind  TRY in Tactical}.
  1068   For example:
  1081   For example:
  1069 *}
  1082 *}
  1070 
  1083 
  1071 ML{*val select_tac'' = TRY o FIRST' [rtac @{thm conjI}, rtac @{thm impI}, 
  1084 ML{*val select_tac'' = TRY o FIRST' [rtac @{thm conjI}, rtac @{thm impI}, 
  1087       @{subgoals [display]}
  1100       @{subgoals [display]}
  1088       \end{minipage} *}
  1101       \end{minipage} *}
  1089 (*<*)oops(*>*)
  1102 (*<*)oops(*>*)
  1090 
  1103 
  1091 text {*
  1104 text {*
  1092   In this case no rule applies, but because of @{ML TRY} or the inclusion of @{ML all_tac}
  1105   In this case no theorem applies. But because we wrapped the tactic in a @{ML
  1093   the tactics do not fail. The problem with this is that for the user there is little 
  1106   TRY}, it does not fail anymore. The problem is that for the user there is
  1094   chance to see whether or not progress in the proof has been made. By convention
  1107   little chance to see whether progress in the proof has been made, or not. By
  1095   therefore, tactics visible to the user should either change something or fail.
  1108   convention therefore, tactics visible to the user should either change
  1096   
  1109   something or fail.
       
  1110 
  1097   To comply with this convention, we could simply delete the @{ML "K all_tac"}
  1111   To comply with this convention, we could simply delete the @{ML "K all_tac"}
  1098   from the end of the theorem list. As a result @{ML select_tac'} would only
  1112   in @{ML select_tac'} or delete @{ML TRY} from @{ML select_tac''}. But for
  1099   succeed on goals where it can make progress. But for the sake of argument,
  1113   the sake of argument, let us suppose that this deletion is \emph{not} an
  1100   let us suppose that this deletion is \emph{not} an option. In such cases, you can
  1114   option. In such cases, you can use the combinator @{ML_ind CHANGED in
  1101   use the combinator @{ML_ind  CHANGED in Tactical} to make sure the subgoal has been changed
  1115   Tactical} to make sure the subgoal has been changed by the tactic. Because
  1102   by the tactic. Because now
  1116   now
  1103 
       
  1104 *}
  1117 *}
  1105 
  1118 
  1106 lemma 
  1119 lemma 
  1107   shows "E \<Longrightarrow> F"
  1120   shows "E \<Longrightarrow> F"
  1108 apply(tactic {* CHANGED (select_tac' 1) *})(*<*)?(*>*)
  1121 apply(tactic {* CHANGED (select_tac' 1) *})(*<*)?(*>*)
  1113   \end{isabelle} 
  1126   \end{isabelle} 
  1114 *}(*<*)oops(*>*)
  1127 *}(*<*)oops(*>*)
  1115 
  1128 
  1116 
  1129 
  1117 text {*
  1130 text {*
  1118   We can further extend @{ML select_tac'} so that it not just applies to the topmost
  1131   We can further extend the @{ML select_tac}s so that they not just apply to the topmost
  1119   connective, but also to the ones immediately ``underneath'', i.e.~analyse the goal 
  1132   connective, but also to the ones immediately ``underneath'', i.e.~analyse the goal 
  1120   completely. For this you can use the tactic combinator @{ML_ind  REPEAT in Tactical}. As an example 
  1133   completely. For this you can use the tactic combinator @{ML_ind REPEAT in Tactical}. As an example 
  1121   suppose the following tactic
  1134   suppose the following tactic
  1122 *}
  1135 *}
  1123 
  1136 
  1124 ML{*val repeat_xmp_tac = REPEAT (CHANGED (select_tac' 1)) *}
  1137 ML{*val repeat_xmp_tac = REPEAT (CHANGED (select_tac' 1)) *}
  1125 
  1138 
  1127 
  1140 
  1128 lemma 
  1141 lemma 
  1129   shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1142   shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1130 apply(tactic {* repeat_xmp_tac *})
  1143 apply(tactic {* repeat_xmp_tac *})
  1131 txt{* produces
  1144 txt{* produces
  1132 
  1145       \begin{isabelle}
  1133       \begin{minipage}{\textwidth}
       
  1134       @{subgoals [display]}
  1146       @{subgoals [display]}
  1135       \end{minipage} *}
  1147       \end{isabelle} *}
  1136 (*<*)oops(*>*)
  1148 (*<*)oops(*>*)
  1137 
  1149 
  1138 text {*
  1150 text {*
  1139   Here it is crucial that @{ML select_tac'} is prefixed with @{ML CHANGED}, 
  1151   Here it is crucial that @{ML select_tac'} is prefixed with @{ML CHANGED},
  1140   because otherwise @{ML REPEAT} runs into an infinite loop (it applies the
  1152   because otherwise @{ML REPEAT} runs into an infinite loop (it applies the
  1141   tactic as long as it succeeds). The function
  1153   tactic as long as it succeeds). The function @{ML_ind REPEAT1 in Tactical}
  1142   @{ML_ind  REPEAT1 in Tactical} is similar, but runs the tactic at least once (failing if 
  1154   is similar, but runs the tactic at least once (failing if this is not
  1143   this is not possible).
  1155   possible).
  1144 
  1156 
  1145   If you are after the ``primed'' version of @{ML repeat_xmp_tac}, then you 
  1157   If you are after the ``primed'' version of @{ML repeat_xmp_tac}, then you 
  1146   can implement it as
  1158   can implement it as
  1147 *}
  1159 *}
  1148 
  1160 
  1154   If you look closely at the goal state above, then you see the tactics @{ML repeat_xmp_tac}
  1166   If you look closely at the goal state above, then you see the tactics @{ML repeat_xmp_tac}
  1155   and @{ML repeat_xmp_tac'} are not yet quite what we are after: the problem is
  1167   and @{ML repeat_xmp_tac'} are not yet quite what we are after: the problem is
  1156   that goals 2 and 3 are not analysed. This is because the tactic
  1168   that goals 2 and 3 are not analysed. This is because the tactic
  1157   is applied repeatedly only to the first subgoal. To analyse also all
  1169   is applied repeatedly only to the first subgoal. To analyse also all
  1158   resulting subgoals, you can use the tactic combinator @{ML_ind  REPEAT_ALL_NEW in Tactical}. 
  1170   resulting subgoals, you can use the tactic combinator @{ML_ind  REPEAT_ALL_NEW in Tactical}. 
  1159   Suppose the tactic
  1171   Supposing the tactic
  1160 *}
  1172 *}
  1161 
  1173 
  1162 ML{*val repeat_all_new_xmp_tac = REPEAT_ALL_NEW (CHANGED o select_tac')*}
  1174 ML{*val repeat_all_new_xmp_tac = REPEAT_ALL_NEW (CHANGED o select_tac')*}
  1163 
  1175 
  1164 text {* 
  1176 text {* 
  1165   you see that the following goal
  1177   you can see that the following goal
  1166 *}
  1178 *}
  1167 
  1179 
  1168 lemma 
  1180 lemma 
  1169   shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1181   shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1170 apply(tactic {* repeat_all_new_xmp_tac 1 *})
  1182 apply(tactic {* repeat_all_new_xmp_tac 1 *})
  1183 *}
  1195 *}
  1184 
  1196 
  1185 lemma 
  1197 lemma 
  1186   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1198   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1187 apply(tactic {* etac @{thm disjE} 1 *})
  1199 apply(tactic {* etac @{thm disjE} 1 *})
  1188 txt{* applies the rule to the first assumption yielding the goal state:\smallskip
  1200 txt{* applies the rule to the first assumption yielding the goal state:
  1189       
  1201       \begin{isabelle}
  1190       \begin{minipage}{\textwidth}
       
  1191       @{subgoals [display]}
  1202       @{subgoals [display]}
  1192       \end{minipage}\smallskip 
  1203       \end{isabelle}
  1193 
  1204 
  1194       After typing
  1205       After typing
  1195   *}
  1206   *}
  1196 (*<*)
  1207 (*<*)
  1197 oops
  1208 oops
  1198 lemma 
  1209 lemma 
  1199   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1210   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1200 apply(tactic {* etac @{thm disjE} 1 *})
  1211 apply(tactic {* etac @{thm disjE} 1 *})
  1201 (*>*)
  1212 (*>*)
  1202 back
  1213 back
  1203 txt{* the rule now applies to the second assumption.\smallskip
  1214 txt{* the rule now applies to the second assumption.
  1204 
  1215       \begin{isabelle}
  1205       \begin{minipage}{\textwidth}
       
  1206       @{subgoals [display]}
  1216       @{subgoals [display]}
  1207       \end{minipage} *}
  1217       \end{isabelle} *}
  1208 (*<*)oops(*>*)
  1218 (*<*)oops(*>*)
  1209 
  1219 
  1210 text {*
  1220 text {*
  1211   Sometimes this leads to confusing behaviour of tactics and also has
  1221   Sometimes this leads to confusing behaviour of tactics and also has
  1212   the potential to explode the search space for tactics.
  1222   the potential to explode the search space for tactics.
  1229   \begin{isabelle}
  1239   \begin{isabelle}
  1230   @{text "*** back: no alternatives"}\\
  1240   @{text "*** back: no alternatives"}\\
  1231   @{text "*** At command \"back\"."}
  1241   @{text "*** At command \"back\"."}
  1232   \end{isabelle}
  1242   \end{isabelle}
  1233 
  1243 
  1234   Recall that we implemented @{ML select_tac'} on Page~\pageref{tac:selectprime} specifically 
  1244 
  1235   so that it always succeeds. We achieved this by adding at the end the tactic @{ML all_tac}.
  1245   \footnote{\bf FIXME: say something about @{ML_ind  COND} and COND'}
  1236   We can achieve this also by using the combinator @{ML TRY}. Suppose, for example the 
  1246   \footnote{\bf FIXME: PARALLEL-CHOICE PARALLEL-GOALS}
  1237   tactic
       
  1238 *}
       
  1239 
       
  1240 ML{*val select_tac'' = FIRST' [rtac @{thm conjI}, rtac @{thm impI}, 
       
  1241                            rtac @{thm notI}, rtac @{thm allI}]*}
       
  1242 
       
  1243 text {*
       
  1244   which will fail if none of the rules applies. However, if you prefix it as follows
       
  1245 *}
       
  1246 
       
  1247 ML{*val select_tac''' = TRY o select_tac''*}
       
  1248 
       
  1249 text {*
       
  1250   then the tactic @{ML select_tac''} will be tried out and any failure is harnessed. 
       
  1251   We again have to use the construction with \mbox{@{text "TRY o ..."}} since there is 
       
  1252   no primed version of @{ML_ind  TRY}. The tactic combinator @{ML_ind  TRYALL} will try out
       
  1253   a tactic on all subgoals. For example the tactic 
       
  1254 *}
       
  1255 
       
  1256 ML{*val triv_tac = TRYALL (rtac @{thm TrueI} ORELSE' etac @{thm FalseE})*}
       
  1257 
       
  1258 text {*
       
  1259   will solve all trivial subgoals involving @{term True} or @{term "False"}.
       
  1260 
       
  1261   (FIXME: say something about @{ML_ind  COND} and COND')
       
  1262 
       
  1263   (FIXME: PARALLEL-CHOICE PARALLEL-GOALS)
       
  1264   
  1247   
  1265   \begin{readmore}
  1248   \begin{readmore}
  1266   Most tactic combinators described in this section are defined in @{ML_file "Pure/tactical.ML"}.
  1249   Most tactic combinators described in this section are defined in @{ML_file "Pure/tactical.ML"}.
  1267   Some combinators for the purpose of proof search are implemented in @{ML_file "Pure/search.ML"}.
  1250   Some combinators for the purpose of proof search are implemented in @{ML_file "Pure/search.ML"}.
  1268   \end{readmore}
  1251   \end{readmore}
  1269 *}
  1252 *}
  1270 
  1253 
  1271 text {*
  1254 text {*
  1272   \begin{exercise}\label{ex:dyckhoff}
  1255   \begin{exercise}\label{ex:dyckhoff}
  1273   Dyckhoff presents in \cite{Dyckhoff92} inference rules for a sequent
  1256   Dyckhoff presents in \cite{Dyckhoff92} inference rules for a sequent
  1274   calculus, called G4ip, in which no contraction rule is needed in order to be
  1257   calculus, called G4ip, for intuitionistic propositional logic. The
  1275   complete. As a result the rules applied in any order give a simple decision
  1258   interesting feature of this calculus is that no contraction rule is needed
  1276   procedure for propositional intuitionistic logic. His rules are
  1259   in order to be complete. As a result applying the rules exhaustively leads
       
  1260   to simple decision procedure for propositional intuitionistic logic. The task
       
  1261   is to implement this decision procedure as a tactic. His rules are
  1277 
  1262 
  1278   \begin{center}
  1263   \begin{center}
  1279   \def\arraystretch{2.3}
  1264   \def\arraystretch{2.3}
  1280   \begin{tabular}{cc}
  1265   \begin{tabular}{cc}
  1281   \infer[Ax]{A,\varGamma \Rightarrow A}{} &
  1266   \infer[Ax]{A,\varGamma \Rightarrow A}{} &
  1311   {D\longrightarrow B, \varGamma \Rightarrow C \longrightarrow D &
  1296   {D\longrightarrow B, \varGamma \Rightarrow C \longrightarrow D &
  1312   B, \varGamma \Rightarrow G}}\\
  1297   B, \varGamma \Rightarrow G}}\\
  1313   \end{tabular}
  1298   \end{tabular}
  1314   \end{center}
  1299   \end{center}
  1315 
  1300 
  1316   Implement a tactic that explores all possibilites of applying these rules to
  1301   Note that in Isabelle the left-rules need to be implemented as elimination
  1317   a propositional formula until a goal state is reached in which all subgoals
  1302   rules. You need to prove separate lemmas corresponding to
  1318   are discharged.  Note that in Isabelle the left-rules need to be implemented
  1303   $\longrightarrow_{L_{1..4}}$. The tactic should explore all possibilites of
  1319   as elimination rules. You need to prove separate lemmas corresponding to
  1304   applying these rules to a propositional formula until a goal state is
  1320   $\longrightarrow_{L_{1..4}}$.  In order to explore all possibilities of applying 
  1305   reached in which all subgoals are discharged. For this you can use the tactic 
  1321   the rules, use the tactic combinator @{ML_ind  DEPTH_SOLVE in Search}, which searches 
  1306   combinator @{ML_ind DEPTH_SOLVE in Search}. 
  1322   for a state in which all subgoals are solved. Add also rules for equality and
       
  1323   run your tactic on the de Bruijn formulae discussed in Exercise~\ref{ex:debruijn}. 
       
  1324   \end{exercise}
  1307   \end{exercise}
       
  1308 
       
  1309   \begin{exercise}
       
  1310   Add to the previous exercise also rules for equality and run your tactic on 
       
  1311   the de Bruijn formulae discussed in Exercise~\ref{ex:debruijn}.
       
  1312   \end{exercise}
  1325 *}
  1313 *}
  1326 
  1314 
  1327 section {* Simplifier Tactics *}
  1315 section {* Simplifier Tactics *}
  1328 
  1316 
  1329 text {*
  1317 text {*
  1330   A lot of convenience in the reasoning with Isabelle derives from its
  1318   A lot of convenience in reasoning with Isabelle derives from its
  1331   powerful simplifier. The power of the simplifier is a strength and a weakness at
  1319   powerful simplifier. We will describe it in this section, whereby 
  1332   the same time, because you can easily make the simplifier run into a loop and
  1320   we can, however, only scratch the surface due to its complexity. 
  1333   in general its
  1321 
  1334   behaviour can be difficult to predict. There is also a multitude
  1322   The power of the simplifier is a strength and a weakness at the same time,
  1335   of options that you can configure to control the behaviour of the simplifier. 
  1323   because you can easily make the simplifier run into a loop and in general
  1336   We describe some of them in this and the next section.
  1324   its behaviour can be difficult to predict. There is also a multitude of
  1337 
  1325   options that you can configure to change the behaviour of the
  1338   There are the following five main tactics behind 
  1326   simplifier. There are the following five main tactics behind the simplifier
  1339   the simplifier (in parentheses is their user-level counterpart):
  1327   (in parentheses is their user-level counterpart):
  1340 
  1328 
  1341   \begin{isabelle}
  1329   \begin{isabelle}
  1342   \begin{center}
  1330   \begin{center}
  1343   \begin{tabular}{l@ {\hspace{2cm}}l}
  1331   \begin{tabular}{l@ {\hspace{2cm}}l}
  1344    @{ML_ind  simp_tac}            & @{text "(simp (no_asm))"} \\
  1332    @{ML_ind simp_tac in Simplifier}            & @{text "(simp (no_asm))"} \\
  1345    @{ML_ind  asm_simp_tac}        & @{text "(simp (no_asm_simp))"} \\
  1333    @{ML_ind asm_simp_tac in Simplifier}        & @{text "(simp (no_asm_simp))"} \\
  1346    @{ML_ind  full_simp_tac}       & @{text "(simp (no_asm_use))"} \\
  1334    @{ML_ind full_simp_tac in Simplifier}       & @{text "(simp (no_asm_use))"} \\
  1347    @{ML_ind  asm_lr_simp_tac}     & @{text "(simp (asm_lr))"} \\
  1335    @{ML_ind asm_lr_simp_tac in Simplifier}     & @{text "(simp (asm_lr))"} \\
  1348    @{ML_ind  asm_full_simp_tac}   & @{text "(simp)"}
  1336    @{ML_ind asm_full_simp_tac in Simplifier}   & @{text "(simp)"}
  1349   \end{tabular}
  1337   \end{tabular}
  1350   \end{center}
  1338   \end{center}
  1351   \end{isabelle}
  1339   \end{isabelle}
  1352 
  1340 
  1353   All of the tactics take a simpset and an integer as argument (the latter as usual 
  1341   All of the tactics take a simpset and an integer as argument (the latter as usual 
  1372   If the simplifier cannot make any progress, then it leaves the goal unchanged,
  1360   If the simplifier cannot make any progress, then it leaves the goal unchanged,
  1373   i.e., does not raise any error message. That means if you use it to unfold a 
  1361   i.e., does not raise any error message. That means if you use it to unfold a 
  1374   definition for a constant and this constant is not present in the goal state, 
  1362   definition for a constant and this constant is not present in the goal state, 
  1375   you can still safely apply the simplifier.
  1363   you can still safely apply the simplifier.
  1376 
  1364 
  1377   (FIXME: show rewriting of cterms)
  1365   \footnote{\bf FIXME: show rewriting of cterms}
  1378 
       
  1379 
  1366 
  1380   When using the simplifier, the crucial information you have to provide is
  1367   When using the simplifier, the crucial information you have to provide is
  1381   the simpset. If this information is not handled with care, then the
  1368   the simpset. If this information is not handled with care, then, as
  1382   simplifier can easily run into a loop. Therefore a good rule of thumb is to
  1369   mentioned above, the simplifier can easily run into a loop. Therefore a good
  1383   use simpsets that are as minimal as possible. It might be surprising that a
  1370   rule of thumb is to use simpsets that are as minimal as possible. It might
  1384   simpset is more complex than just a simple collection of theorems used as
  1371   be surprising that a simpset is more complex than just a simple collection
  1385   simplification rules. One reason for the complexity is that the simplifier
  1372   of theorems. One reason for the complexity is that the simplifier must be
  1386   must be able to rewrite inside terms and should also be able to rewrite
  1373   able to rewrite inside terms and should also be able to rewrite according to
  1387   according to rules that have preconditions.
  1374   theorems that have premises.
  1388 
  1375 
  1389 
  1376   The rewriting inside terms requires congruence theorems, which
  1390   The rewriting inside terms requires congruence rules, which
  1377   are typically meta-equalities of the form
  1391   are meta-equalities typical of the form
       
  1392 
  1378 
  1393   \begin{isabelle}
  1379   \begin{isabelle}
  1394   \begin{center}
  1380   \begin{center}
  1395   \mbox{\inferrule{@{text "t\<^isub>1 \<equiv> s\<^isub>1 \<dots> t\<^isub>n \<equiv> s\<^isub>n"}}
  1381   \mbox{\inferrule{@{text "t\<^isub>1 \<equiv> s\<^isub>1 \<dots> t\<^isub>n \<equiv> s\<^isub>n"}}
  1396                   {@{text "constr t\<^isub>1\<dots>t\<^isub>n \<equiv> constr s\<^isub>1\<dots>s\<^isub>n"}}}
  1382                   {@{text "constr t\<^isub>1\<dots>t\<^isub>n \<equiv> constr s\<^isub>1\<dots>s\<^isub>n"}}}
  1397   \end{center}
  1383   \end{center}
  1398   \end{isabelle}
  1384   \end{isabelle}
  1399 
  1385 
  1400   with @{text "constr"} being a constant, like @{const "If"} or @{const "Let"}. 
  1386   with @{text "constr"} being a constant, like @{const "If"}, @{const "Let"}
  1401   Every simpset contains only
  1387   and so on. Every simpset contains only one congruence rule for each
  1402   one congruence rule for each term-constructor, which however can be
  1388   term-constructor, which however can be overwritten. The user can declare
  1403   overwritten. The user can declare lemmas to be congruence rules using the
  1389   lemmas to be congruence rules using the attribute @{text "[cong]"}. In HOL,
  1404   attribute @{text "[cong]"}. In HOL, the user usually states these lemmas as
  1390   the user usually states these lemmas as equations, which are then internally
  1405   equations, which are then internally transformed into meta-equations.
  1391   transformed into meta-equations.
  1406 
  1392 
  1407 
  1393   The rewriting with theorems involving premises requires what is in Isabelle
  1408   The rewriting with rules involving preconditions requires what is in
  1394   called a subgoaler, a solver and a looper. These can be arbitrary tactics
  1409   Isabelle called a subgoaler, a solver and a looper. These can be arbitrary
  1395   that can be installed in a simpset and which are executed at various stages
  1410   tactics that can be installed in a simpset and which are called at
  1396   during simplification. However, simpsets also include simprocs, which can
  1411   various stages during simplification. However, simpsets also include
  1397   produce rewrite rules on demand (see next section). Another component are
  1412   simprocs, which can produce rewrite rules on demand (see next
  1398   split-rules, which can simplify for example the ``then'' and ``else''
  1413   section). Another component are split-rules, which can simplify for example
  1399   branches of if-statements under the corresponding preconditions.
  1414   the ``then'' and ``else'' branches of if-statements under the corresponding
       
  1415   preconditions.
       
  1416 
       
  1417 
  1400 
  1418   \begin{readmore}
  1401   \begin{readmore}
  1419   For more information about the simplifier see @{ML_file "Pure/meta_simplifier.ML"}
  1402   For more information about the simplifier see @{ML_file "Pure/meta_simplifier.ML"}
  1420   and @{ML_file "Pure/simplifier.ML"}. The simplifier for HOL is set up in 
  1403   and @{ML_file "Pure/simplifier.ML"}. The generic splitter is implemented in 
  1421   @{ML_file "HOL/Tools/simpdata.ML"}. The generic splitter is implemented in 
  1404   @{ML_file "Provers/splitter.ML"}.
  1422   @{ML_file  "Provers/splitter.ML"}.
       
  1423   \end{readmore}
  1405   \end{readmore}
  1424 
  1406 
  1425   \begin{readmore}
  1407   
  1426   FIXME: Find the right place: Discrimination nets are implemented
  1408   \footnote{\bf FIXME: Find the right place to mention this: Discrimination 
  1427   in @{ML_file "Pure/net.ML"}.
  1409   nets are implemented in @{ML_file "Pure/net.ML"}.}
  1428   \end{readmore}
       
  1429 
  1410 
  1430   The most common combinators to modify simpsets are:
  1411   The most common combinators to modify simpsets are:
  1431 
  1412 
  1432   \begin{isabelle}
  1413   \begin{isabelle}
  1433   \begin{tabular}{ll}
  1414   \begin{tabular}{ll}
  1434   @{ML_ind  addsimps}    & @{ML_ind  delsimps}\\
  1415   @{ML_ind addsimps in MetaSimplifier}    & @{ML_ind delsimps in MetaSimplifier}\\
  1435   @{ML_ind  addcongs}    & @{ML_ind  delcongs}\\
  1416   @{ML_ind addcongs in MetaSimplifier}    & @{ML_ind delcongs in MetaSimplifier}\\
  1436   @{ML_ind  addsimprocs} & @{ML_ind  delsimprocs}\\
  1417   @{ML_ind addsimprocs in MetaSimplifier} & @{ML_ind delsimprocs in MetaSimplifier}\\
  1437   \end{tabular}
  1418   \end{tabular}
  1438   \end{isabelle}
  1419   \end{isabelle}
  1439 
  1420 
  1440   (FIXME: What about splitters? @{ML addsplits}, @{ML delsplits})
  1421   \footnote{\bf FIXME: What about splitters? @{ML addsplits}, @{ML delsplits}}
  1441 *}
  1422 *}
  1442 
  1423 
  1443 text_raw {*
  1424 text_raw {*
  1444 \begin{figure}[t]
  1425 \begin{figure}[t]
  1445 \begin{minipage}{\textwidth}
  1426 \begin{minipage}{\textwidth}
  1470 
  1451 
  1471 
  1452 
  1472 text {* 
  1453 text {* 
  1473   To see how they work, consider the function in Figure~\ref{fig:printss}, which
  1454   To see how they work, consider the function in Figure~\ref{fig:printss}, which
  1474   prints out some parts of a simpset. If you use it to print out the components of the
  1455   prints out some parts of a simpset. If you use it to print out the components of the
  1475   empty simpset, i.e., @{ML_ind  empty_ss}
  1456   empty simpset, i.e., @{ML_ind empty_ss in MetaSimplifier}
  1476   
  1457   
  1477   @{ML_response_fake [display,gray]
  1458   @{ML_response_fake [display,gray]
  1478   "print_ss @{context} empty_ss"
  1459   "print_ss @{context} empty_ss"
  1479 "Simplification rules:
  1460 "Simplification rules:
  1480 Congruences rules:
  1461 Congruences rules:
  1495 "Simplification rules:
  1476 "Simplification rules:
  1496   ??.unknown: A - B \<inter> C \<equiv> A - B \<union> (A - C)
  1477   ??.unknown: A - B \<inter> C \<equiv> A - B \<union> (A - C)
  1497 Congruences rules:
  1478 Congruences rules:
  1498 Simproc patterns:"}
  1479 Simproc patterns:"}
  1499 
  1480 
  1500   (FIXME: Why does it print out ??.unknown)
  1481   \footnote{\bf FIXME: Why does it print out ??.unknown}
  1501 
  1482 
  1502   Adding also the congruence rule @{thm [source] UN_cong} 
  1483   Adding also the congruence rule @{thm [source] UN_cong} 
  1503 *}
  1484 *}
  1504 
  1485 
  1505 ML{*val ss2 = ss1 addcongs [@{thm UN_cong} RS @{thm eq_reflection}] *}
  1486 ML{*val ss2 = ss1 addcongs [@{thm UN_cong} RS @{thm eq_reflection}] *}
  1517 
  1498 
  1518   Notice that we had to add these lemmas as meta-equations. The @{ML empty_ss} 
  1499   Notice that we had to add these lemmas as meta-equations. The @{ML empty_ss} 
  1519   expects this form of the simplification and congruence rules. However, even 
  1500   expects this form of the simplification and congruence rules. However, even 
  1520   when adding these lemmas to @{ML empty_ss} we do not end up with anything useful yet.
  1501   when adding these lemmas to @{ML empty_ss} we do not end up with anything useful yet.
  1521 
  1502 
  1522   In the context of HOL, the first really useful simpset is @{ML_ind  HOL_basic_ss}. While
  1503   In the context of HOL, the first really useful simpset is @{ML_ind
  1523   printing out the components of this simpset
  1504   HOL_basic_ss in Simpdata}. While printing out the components of this simpset
  1524 
  1505 
  1525   @{ML_response_fake [display,gray]
  1506   @{ML_response_fake [display,gray]
  1526   "print_ss @{context} HOL_basic_ss"
  1507   "print_ss @{context} HOL_basic_ss"
  1527 "Simplification rules:
  1508 "Simplification rules:
  1528 Congruences rules:
  1509 Congruences rules:
  1538 
  1519 
  1539   and also resolve with assumptions. For example:
  1520   and also resolve with assumptions. For example:
  1540 *}
  1521 *}
  1541 
  1522 
  1542 lemma 
  1523 lemma 
  1543  "True" and "t = t" and "t \<equiv> t" and "False \<Longrightarrow> Foo" and "\<lbrakk>A; B; C\<rbrakk> \<Longrightarrow> A"
  1524  shows "True" 
       
  1525   and  "t = t" 
       
  1526   and  "t \<equiv> t" 
       
  1527   and  "False \<Longrightarrow> Foo" 
       
  1528   and  "\<lbrakk>A; B; C\<rbrakk> \<Longrightarrow> A"
  1544 apply(tactic {* ALLGOALS (simp_tac HOL_basic_ss) *})
  1529 apply(tactic {* ALLGOALS (simp_tac HOL_basic_ss) *})
  1545 done
  1530 done
  1546 
  1531 
  1547 text {*
  1532 text {*
  1548   This behaviour is not because of simplification rules, but how the subgoaler, solver 
  1533   This behaviour is not because of simplification rules, but how the subgoaler, solver 
  1549   and looper are set up in @{ML_ind  HOL_basic_ss}.
  1534   and looper are set up in @{ML_ind HOL_basic_ss}.
  1550 
  1535 
  1551   The simpset @{ML_ind  HOL_ss} is an extension of @{ML HOL_basic_ss} containing 
  1536   The simpset @{ML_ind HOL_ss} is an extension of @{ML HOL_basic_ss} containing 
  1552   already many useful simplification and congruence rules for the logical 
  1537   already many useful simplification and congruence rules for the logical 
  1553   connectives in HOL. 
  1538   connectives in HOL. 
  1554 
  1539 
  1555   @{ML_response_fake [display,gray]
  1540   @{ML_response_fake [display,gray]
  1556   "print_ss @{context} HOL_ss"
  1541   "print_ss @{context} HOL_ss"
  1564     \<Longrightarrow> (PROP P =simp=> PROP Q) \<equiv> (PROP P' =simp=> PROP Q')
  1549     \<Longrightarrow> (PROP P =simp=> PROP Q) \<equiv> (PROP P' =simp=> PROP Q')
  1565   op -->: \<lbrakk>P \<equiv> P'; P' \<Longrightarrow> Q \<equiv> Q'\<rbrakk> \<Longrightarrow> P \<longrightarrow> Q \<equiv> P' \<longrightarrow> Q'
  1550   op -->: \<lbrakk>P \<equiv> P'; P' \<Longrightarrow> Q \<equiv> Q'\<rbrakk> \<Longrightarrow> P \<longrightarrow> Q \<equiv> P' \<longrightarrow> Q'
  1566 Simproc patterns:
  1551 Simproc patterns:
  1567   \<dots>"}
  1552   \<dots>"}
  1568 
  1553 
  1569   
  1554   \begin{readmore}
       
  1555   The simplifier for HOL is set up in @{ML_file "HOL/Tools/simpdata.ML"}.
       
  1556   The simpset @{ML HOL_ss} is implemented in @{ML_file "HOL/HOL.thy"}.
       
  1557   \end{readmore}
       
  1558 
  1570   The simplifier is often used to unfold definitions in a proof. For this the
  1559   The simplifier is often used to unfold definitions in a proof. For this the
  1571   simplifier implements the tactic @{ML_ind  rewrite_goal_tac}.\footnote{\bf FIXME: 
  1560   simplifier implements the tactic @{ML_ind rewrite_goal_tac in MetaSimplifier}.\footnote{\bf FIXME: 
  1572   see LocalDefs infrastructure.} Suppose for example the
  1561   see LocalDefs infrastructure.} Suppose for example the
  1573   definition
  1562   definition
  1574 *}
  1563 *}
  1575 
  1564 
  1576 definition "MyTrue \<equiv> True"
  1565 definition "MyTrue \<equiv> True"
  1581 
  1570 
  1582 lemma 
  1571 lemma 
  1583   shows "MyTrue \<Longrightarrow> True"
  1572   shows "MyTrue \<Longrightarrow> True"
  1584 apply(tactic {* rewrite_goal_tac @{thms MyTrue_def} 1 *})
  1573 apply(tactic {* rewrite_goal_tac @{thms MyTrue_def} 1 *})
  1585 txt{* producing the goal state
  1574 txt{* producing the goal state
  1586 
  1575       \begin{isabelle}
  1587       \begin{minipage}{\textwidth}
       
  1588       @{subgoals [display]}
  1576       @{subgoals [display]}
  1589       \end{minipage} *}
  1577       \end{isabelle} *}
  1590 (*<*)oops(*>*)
  1578 (*<*)oops(*>*)
  1591 
  1579 
  1592 text {*
  1580 text {*
  1593   If you want to unfold definitions in all subgoals, then use the 
  1581   If you want to unfold definitions in \emph{all} subgoals not just one, 
  1594   the tactic @{ML_ind rewrite_goals_tac}.
  1582   then use the the tactic @{ML_ind rewrite_goals_tac in MetaSimplifier}.
  1595 *}
  1583 *}
  1596 
  1584 
  1597 
  1585 
  1598 text_raw {*
  1586 text_raw {*
  1599 \begin{figure}[p]
  1587 \begin{figure}[p]
  1945 lemma 
  1933 lemma 
  1946   shows "P (Suc (Suc (Suc 0))) (Suc 0)"
  1934   shows "P (Suc (Suc (Suc 0))) (Suc 0)"
  1947 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [plus_one]) 1*})
  1935 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [plus_one]) 1*})
  1948 txt{*
  1936 txt{*
  1949   where the simproc produces the goal state
  1937   where the simproc produces the goal state
  1950   
  1938   \begin{isabelle}
  1951   \begin{minipage}{\textwidth}
       
  1952   @{subgoals[display]}
  1939   @{subgoals[display]}
  1953   \end{minipage}
  1940   \end{isabelle}
  1954 *}  
  1941 *}  
  1955 (*<*)oops(*>*)
  1942 (*<*)oops(*>*)
  1956 
  1943 
  1957 text {*
  1944 text {*
  1958   As usual with rewriting you have to worry about looping: you already have
  1945   As usual with rewriting you have to worry about looping: you already have
  2051 lemma 
  2038 lemma 
  2052   shows "P (Suc (Suc 2)) (Suc 99) (0::nat) (Suc 4 + Suc 0) (Suc (0 + 0))"
  2039   shows "P (Suc (Suc 2)) (Suc 99) (0::nat) (Suc 4 + Suc 0) (Suc (0 + 0))"
  2053 apply(tactic {* simp_tac (HOL_ss addsimprocs [nat_number]) 1*})
  2040 apply(tactic {* simp_tac (HOL_ss addsimprocs [nat_number]) 1*})
  2054 txt {* 
  2041 txt {* 
  2055   you obtain the more legible goal state
  2042   you obtain the more legible goal state
  2056   
  2043   \begin{isabelle}
  2057   \begin{minipage}{\textwidth}
       
  2058   @{subgoals [display]}
  2044   @{subgoals [display]}
  2059   \end{minipage}
  2045   \end{isabelle}*}
  2060   *}
       
  2061 (*<*)oops(*>*)
  2046 (*<*)oops(*>*)
  2062 
  2047 
  2063 text {*
  2048 text {*
  2064   where the simproc rewrites all @{term "Suc"}s except in the last argument. There it cannot 
  2049   where the simproc rewrites all @{term "Suc"}s except in the last argument. There it cannot 
  2065   rewrite anything, because it does not know how to transform the term @{term "Suc (0 + 0)"}
  2050   rewrite anything, because it does not know how to transform the term @{term "Suc (0 + 0)"}
  2387 lemma
  2372 lemma
  2388   "if True \<and> P then P else True \<and> False \<Longrightarrow>
  2373   "if True \<and> P then P else True \<and> False \<Longrightarrow>
  2389   (if True \<and> Q then True \<and> Q else P) \<longrightarrow> True \<and> (True \<and> Q)"
  2374   (if True \<and> Q then True \<and> Q else P) \<longrightarrow> True \<and> (True \<and> Q)"
  2390 apply(tactic {* true1_tac @{context} 1 *})
  2375 apply(tactic {* true1_tac @{context} 1 *})
  2391 txt {* where the tactic yields the goal state
  2376 txt {* where the tactic yields the goal state
  2392 
  2377   \begin{isabelle}
  2393   \begin{minipage}{\textwidth}
       
  2394   @{subgoals [display]}
  2378   @{subgoals [display]}
  2395   \end{minipage}*}
  2379   \end{isabelle}*}
  2396 (*<*)oops(*>*)
  2380 (*<*)oops(*>*)
  2397 
  2381 
  2398 text {*
  2382 text {*
  2399   As you can see, the premises are rewritten according to @{ML if_true1_conv}, while
  2383   As you can see, the premises are rewritten according to @{ML if_true1_conv}, while
  2400   the conclusion according to @{ML all_true1_conv}.
  2384   the conclusion according to @{ML all_true1_conv}.
  2541   val this = r OF this;
  2525   val this = r OF this;
  2542   val this = Assumption.export false ctxt ctxt0 this 
  2526   val this = Assumption.export false ctxt ctxt0 this 
  2543   val this = Variable.export ctxt ctxt0 [this] 
  2527   val this = Variable.export ctxt ctxt0 [this] 
  2544 *}
  2528 *}
  2545 
  2529 
  2546 text {*
  2530 section {* Summary *}
  2547   If a tactic should fail, return the empty sequence.
  2531 
       
  2532 text {*
       
  2533 
       
  2534   \begin{conventions}
       
  2535   \begin{itemize}
       
  2536   \item Reference theorems with the antiquotation @{text "@{thm \<dots>}"}.
       
  2537   \item If a tactic is supposed to fail, return the empty sequence.
       
  2538   \item If you apply a tactic to a sequence of subgoals, apply it 
       
  2539   in reverse order (i.e.~start with the last subgoal). 
       
  2540   \item Use simpsets that are as small as possible.
       
  2541   \end{itemize}
       
  2542   \end{conventions}
       
  2543 
  2548 *}
  2544 *}
  2549 
  2545 
  2550 end
  2546 end