ProgTutorial/Tactical.thy
changeset 362 a5e7ab090abf
parent 361 8ba963a3e039
child 363 f7f1d8a98098
equal deleted inserted replaced
361:8ba963a3e039 362:a5e7ab090abf
    11 (*>*)
    11 (*>*)
    12 
    12 
    13 chapter {* Tactical Reasoning\label{chp:tactical} *}
    13 chapter {* Tactical Reasoning\label{chp:tactical} *}
    14 
    14 
    15 text {*
    15 text {*
    16   One of the main reason for descending to the ML-level of Isabelle is to be able to
    16   One of the main reason for descending to the ML-level of Isabelle is to be
    17   implement automatic proof procedures. Such proof procedures usually lessen
    17   able to implement automatic proof procedures. Such proof procedures can
    18   considerably the burden of manual reasoning, for example, when introducing
    18   considerably lessen the burden of manual reasoning. They are centred around
    19   new definitions. These proof procedures are centred around refining a goal
    19   the idea of refining a goal state using tactics. This is similar to the
    20   state using tactics. This is similar to the \isacommand{apply}-style
    20   \isacommand{apply}-style reasoning at the user-level, where goals are
    21   reasoning at the user-level, where goals are modified in a sequence of proof
    21   modified in a sequence of proof steps until all of them are discharged.
    22   steps until all of them are solved. However, there are also more structured
    22   In this chapter we will explain simple tactics and how to combine them using
    23   operations available on the ML-level that help with the handling of
    23   tactic combinators. We also explain briefly the simplifier and conversions.
    24   variables and assumptions.
       
    25 *}
    24 *}
    26 
    25 
    27 
    26 
    28 section {* Basics of Reasoning with Tactics*}
    27 section {* Basics of Reasoning with Tactics*}
    29 
    28 
    30 text {*
    29 text {*
    31   To see how tactics work, let us first transcribe a simple \isacommand{apply}-style proof 
    30   To see how tactics work, let us first transcribe a simple \isacommand{apply}-style proof 
    32   into ML. Suppose the following proof.
    31   into ML. Suppose the following proof.
    33 *}
    32 *}
    34 
    33 
    35 lemma disj_swap: "P \<or> Q \<Longrightarrow> Q \<or> P"
    34 lemma disj_swap: 
       
    35   shows "P \<or> Q \<Longrightarrow> Q \<or> P"
    36 apply(erule disjE)
    36 apply(erule disjE)
    37 apply(rule disjI2)
    37 apply(rule disjI2)
    38 apply(assumption)
    38 apply(assumption)
    39 apply(rule disjI1)
    39 apply(rule disjI1)
    40 apply(assumption)
    40 apply(assumption)
    55       THEN atac 1
    55       THEN atac 1
    56       THEN rtac @{thm disjI1} 1
    56       THEN rtac @{thm disjI1} 1
    57       THEN atac 1)
    57       THEN atac 1)
    58 end" "?P \<or> ?Q \<Longrightarrow> ?Q \<or> ?P"}
    58 end" "?P \<or> ?Q \<Longrightarrow> ?Q \<or> ?P"}
    59   
    59   
    60   To start the proof, the function @{ML_ind "Goal.prove"}~@{text "ctxt xs As C
    60   To start the proof, the function @{ML_ind prove in Goal} sets up a goal
    61   tac"} sets up a goal state for proving the goal @{text C} (that is @{prop "P
    61   state for proving the goal @{prop "P \<or> Q \<Longrightarrow> Q \<or> P"}. We can give this
    62   \<or> Q \<Longrightarrow> Q \<or> P"} in the proof at hand) under the assumptions @{text As}
    62   function some assumptions in the third argument (there are no assumption in
    63   (happens to be empty) with the variables @{text xs} that will be generalised
    63   the proof at hand). The second argument stands for a list of variables
    64   once the goal is proved (in our case @{text P} and @{text
    64   (given as strings). This list contains the variables that will be turned
    65   Q}).\footnote{FIXME: explain prove earlier} The @{text "tac"} is the tactic
    65   into schematic variables once the goal is proved (in our case @{text P} and
    66   that proves the goal; it can make use of the local assumptions (there are
    66   @{text Q}). The last argument is the tactic that proves the goal. This
    67   none in this example). The tactics @{ML_ind etac}, @{ML_ind rtac} and
    67   tactic can make use of the local assumptions (there are none in this
    68   @{ML_ind atac} in the code above correspond roughly to @{text erule}, @{text
    68   example). The tactics @{ML_ind etac}, @{ML_ind rtac} and @{ML_ind atac} in
    69   rule} and @{text assumption}, respectively. The operator @{ML_ind THEN}
    69   the code above correspond roughly to @{text erule}, @{text rule} and @{text
    70   strings the tactics together.
    70   assumption}, respectively. The combinator @{ML_ind THEN} strings the tactics
       
    71   together.
    71 
    72 
    72   \begin{readmore}
    73   \begin{readmore}
    73   To learn more about the function @{ML_ind prove in Goal} see
    74   To learn more about the function @{ML_ind prove in Goal} see
    74   \isccite{sec:results} and the file @{ML_file "Pure/goal.ML"}.  See @{ML_file
    75   \isccite{sec:results} and the file @{ML_file "Pure/goal.ML"}.  See @{ML_file
    75   "Pure/tactic.ML"} and @{ML_file "Pure/tactical.ML"} for the code of basic
    76   "Pure/tactic.ML"} and @{ML_file "Pure/tactical.ML"} for the code of basic
    77   Isabelle Reference Manual, and Chapter 3 in the Isabelle/Isar Implementation
    78   Isabelle Reference Manual, and Chapter 3 in the Isabelle/Isar Implementation
    78   Manual.
    79   Manual.
    79   \end{readmore}
    80   \end{readmore}
    80 
    81 
    81   Note that in the code above we use antiquotations for referencing the
    82   Note that in the code above we use antiquotations for referencing the
    82   theorems. Many theorems also have ML-bindings with the same name. Therefore,
    83   theorems. We  could also just have written @{ML "etac disjE 1"}. The reason 
    83   we could also just have written @{ML "etac disjE 1"}, or in case where there
    84   is that many of the basic theorems have a corresponding ML-binding:
    84   is no ML-binding obtain the theorem dynamically using the function @{ML
    85 
    85   thm}; for example \mbox{@{ML "etac (thm \"disjE\") 1"}}. Both ways however
    86   @{ML_response_fake [gray,display]
    86   are considered bad style! The reason is that the binding for @{ML disjE} can
    87   "disjE"
    87   be re-assigned by the user and thus one does not have complete control over
    88   "\<lbrakk>?P \<or> ?Q; ?P \<Longrightarrow> ?R; ?Q \<Longrightarrow> ?R\<rbrakk> \<Longrightarrow> ?R"}
    88   which theorem is actually applied. This problem is nicely prevented by using
    89 
    89   antiquotations, because then the theorems are fixed statically at
    90   In case where there is no ML-binding theorems can also be obtained dynamically using 
    90   compile-time.
    91   the function @{ML thm} and the (string) name of the theorem; for example: 
       
    92 
       
    93   @{ML_response_fake [gray,display]
       
    94   "thm \"disjE\""
       
    95   "\<lbrakk>?P \<or> ?Q; ?P \<Longrightarrow> ?R; ?Q \<Longrightarrow> ?R\<rbrakk> \<Longrightarrow> ?R"}
       
    96 
       
    97   Both ways however are considered bad style! The reason is that the binding
       
    98   for @{ML disjE} can be re-assigned by the user and thus one does not have
       
    99   complete control over which theorem is actually applied. Similarly with the
       
   100   string @{text [quotes] "disjE"}. Although theorems in the theorem database
       
   101   must have a unique name, the string can stand for a dynamically updated
       
   102   theorem list. Also in this case we cannot be sure which theorem is applied.
       
   103   These problems can be nicely prevented by using antiquotations, because then
       
   104   the theorems are fixed statically at compile-time.
       
   105 
    91 
   106 
    92   During the development of automatic proof procedures, you will often find it
   107   During the development of automatic proof procedures, you will often find it
    93   necessary to test a tactic on examples. This can be conveniently done with
   108   necessary to test a tactic on examples. This can be conveniently done with
    94   the command \isacommand{apply}@{text "(tactic \<verbopen> \<dots> \<verbclose>)"}. 
   109   the command \isacommand{apply}@{text "(tactic \<verbopen> \<dots> \<verbclose>)"}. 
    95   Consider the following sequence of tactics
   110   Consider the following sequence of tactics
   103        THEN rtac @{thm disjI1} 1
   118        THEN rtac @{thm disjI1} 1
   104        THEN atac 1)*}
   119        THEN atac 1)*}
   105 
   120 
   106 text {* and the Isabelle proof: *}
   121 text {* and the Isabelle proof: *}
   107 
   122 
   108 lemma "P \<or> Q \<Longrightarrow> Q \<or> P"
   123 lemma 
       
   124   shows "P \<or> Q \<Longrightarrow> Q \<or> P"
   109 apply(tactic {* foo_tac *})
   125 apply(tactic {* foo_tac *})
   110 done
   126 done
   111 
   127 
   112 text {*
   128 text {*
   113   By using @{text "tactic \<verbopen> \<dots> \<verbclose>"} you can call from the 
   129   By using @{text "tactic \<verbopen> \<dots> \<verbclose>"} you can call from the 
   128        THEN' atac 
   144        THEN' atac 
   129        THEN' rtac @{thm disjI1} 
   145        THEN' rtac @{thm disjI1} 
   130        THEN' atac)*}text_raw{*\label{tac:footacprime}*}
   146        THEN' atac)*}text_raw{*\label{tac:footacprime}*}
   131 
   147 
   132 text {* 
   148 text {* 
   133   where @{ML_ind  THEN'} is used instead of @{ML THEN}. With @{ML foo_tac'} you can give 
   149   where @{ML_ind THEN'} is used instead of @{ML THEN}. (For most combinators
   134   the number for the subgoal explicitly when the tactic is
   150   that combine tactics---@{ML THEN} is only one such combinator---a ``primed''
   135   called. So in the next proof you can first discharge the second subgoal, and
   151   version exists.)  With @{ML foo_tac'} you can give the number for the
   136   subsequently the first.
   152   subgoal explicitly when the tactic is called. So in the next proof you can
   137 *}
   153   first discharge the second subgoal, and subsequently the first.
   138 
   154 *}
   139 lemma "P1 \<or> Q1 \<Longrightarrow> Q1 \<or> P1"
   155 
   140    and "P2 \<or> Q2 \<Longrightarrow> Q2 \<or> P2"
   156 lemma 
       
   157   shows "P1 \<or> Q1 \<Longrightarrow> Q1 \<or> P1"
       
   158   and   "P2 \<or> Q2 \<Longrightarrow> Q2 \<or> P2"
   141 apply(tactic {* foo_tac' 2 *})
   159 apply(tactic {* foo_tac' 2 *})
   142 apply(tactic {* foo_tac' 1 *})
   160 apply(tactic {* foo_tac' 1 *})
   143 done
   161 done
   144 
   162 
   145 text {*
   163 text {*
   146   This kind of addressing is more difficult to achieve when the goal is 
   164   This kind of addressing is more difficult to achieve when the goal is 
   147   hard-coded inside the tactic. For most operators that combine tactics 
   165   hard-coded inside the tactic. 
   148   (@{ML THEN} is only one such operator) a ``primed'' version exists.
   166 
   149 
   167   The tactics @{ML foo_tac} and @{ML foo_tac'} are very specific for analysing
   150   The tactics @{ML foo_tac} and @{ML foo_tac'} are very specific for
   168   goals being only of the form @{prop "P \<or> Q \<Longrightarrow> Q \<or> P"}. If the goal is not of
   151   analysing goals being only of the form @{prop "P \<or> Q \<Longrightarrow> Q \<or> P"}. If the goal is not
   169   this form, then these tactics return the error message:\footnote{To be
   152   of this form, then these tactics return the error message:
   170   precise, tactics do not produce this error message, it originates from the
       
   171   \isacommand{apply} wrapper.}
       
   172 
   153 
   173 
   154   \begin{isabelle}
   174   \begin{isabelle}
   155   @{text "*** empty result sequence -- proof command failed"}\\
   175   @{text "*** empty result sequence -- proof command failed"}\\
   156   @{text "*** At command \"apply\"."}
   176   @{text "*** At command \"apply\"."}
   157   \end{isabelle}
   177   \end{isabelle}
   158 
   178 
   159   This means they failed.\footnote{To be precise, tactics do not produce this error
   179   This means they failed. The reason for this error message is that tactics
   160   message, it originates from the \isacommand{apply} wrapper.} The reason for this 
   180   are functions mapping a goal state to a (lazy) sequence of successor
   161   error message is that tactics 
   181   states. Hence the type of a tactic is:
   162   are functions mapping a goal state to a (lazy) sequence of successor states. 
       
   163   Hence the type of a tactic is:
       
   164 *}
   182 *}
   165   
   183   
   166 ML{*type tactic = thm -> thm Seq.seq*}
   184 ML{*type tactic = thm -> thm Seq.seq*}
   167 
   185 
   168 text {*
   186 text {*
   192   of Isabelle when using the command \isacommand{back}. For instance in the 
   210   of Isabelle when using the command \isacommand{back}. For instance in the 
   193   following proof there are two possibilities for how to apply 
   211   following proof there are two possibilities for how to apply 
   194   @{ML foo_tac'}: either using the first assumption or the second.
   212   @{ML foo_tac'}: either using the first assumption or the second.
   195 *}
   213 *}
   196 
   214 
   197 lemma "\<lbrakk>P \<or> Q; P \<or> Q\<rbrakk> \<Longrightarrow> Q \<or> P"
   215 lemma 
       
   216   shows "\<lbrakk>P \<or> Q; P \<or> Q\<rbrakk> \<Longrightarrow> Q \<or> P"
   198 apply(tactic {* foo_tac' 1 *})
   217 apply(tactic {* foo_tac' 1 *})
   199 back
   218 back
   200 done
   219 done
   201 
   220 
   202 
   221 
   233   \begin{boxedminipage}{\textwidth}
   252   \begin{boxedminipage}{\textwidth}
   234   \begin{isabelle}
   253   \begin{isabelle}
   235 *}
   254 *}
   236 notation (output) "prop"  ("#_" [1000] 1000)
   255 notation (output) "prop"  ("#_" [1000] 1000)
   237 
   256 
   238 lemma shows "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B" 
   257 lemma 
       
   258   shows "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B" 
   239 apply(tactic {* my_print_tac @{context} *})
   259 apply(tactic {* my_print_tac @{context} *})
   240 
   260 
   241 txt{* \begin{minipage}{\textwidth}
   261 txt{* \begin{minipage}{\textwidth}
   242       @{subgoals [display]} 
   262       @{subgoals [display]} 
   243       \end{minipage}\medskip      
   263       \end{minipage}\medskip      
   300    *}
   320    *}
   301 (*<*)oops(*>*)
   321 (*<*)oops(*>*)
   302 text_raw {*  
   322 text_raw {*  
   303   \end{isabelle}
   323   \end{isabelle}
   304   \end{boxedminipage}
   324   \end{boxedminipage}
   305   \caption{The figure shows a proof where each intermediate goal state is
   325   \caption{The figure shows an Isabelle snippet of a proof where each
   306   printed by the Isabelle system and by @{ML my_print_tac}. The latter shows
   326   intermediate goal state is printed by the Isabelle system and by @{ML
   307   the goal state as represented internally (highlighted boxes). This
   327   my_print_tac}. The latter shows the goal state as represented internally
   308   tactic shows that every goal state in Isabelle is represented by a theorem:
   328   (highlighted boxes). This tactic shows that every goal state in Isabelle is
   309   when you start the proof of \mbox{@{text "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B"}} the theorem is
   329   represented by a theorem: when you start the proof of \mbox{@{text "\<lbrakk>A; B\<rbrakk> \<Longrightarrow>
   310   @{text "(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B) \<Longrightarrow> #(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B)"}; when you finish the proof the
   330   A \<and> B"}} the theorem is @{text "(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B) \<Longrightarrow> #(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B)"}; when
   311   theorem is @{text "#(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B)"}.\label{fig:goalstates}}
   331   you finish the proof the theorem is @{text "#(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and>
       
   332   B)"}.\label{fig:goalstates}}
   312   \end{figure}
   333   \end{figure}
   313 *}
   334 *}
   314 
   335 
   315 text {*
   336 text {*
   316   which prints out the given theorem (using the string-function defined in
   337   which prints out the given theorem (using the string-function defined in
   317   Section~\ref{sec:printing}) and then behaves like @{ML all_tac}. With this
   338   Section~\ref{sec:printing}) and then behaves like @{ML all_tac}. With this
   318   tactic we are in the position to inspect every goal state in a
   339   tactic we are in the position to inspect every goal state in a proof. For
   319   proof. Consider now the proof in Figure~\ref{fig:goalstates}: as can be seen, 
   340   this consider the proof in Figure~\ref{fig:goalstates}: as can be seen,
   320   internally every goal state is an implication of the form
   341   internally every goal state is an implication of the form
   321 
   342 
   322   @{text[display] "A\<^isub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^isub>n \<Longrightarrow> #C"}
   343   @{text[display] "A\<^isub>1 \<Longrightarrow> \<dots> \<Longrightarrow> A\<^isub>n \<Longrightarrow> #C"}
   323 
   344 
   324   where @{term C} is the goal to be proved and the @{term "A\<^isub>i"} are
   345   where @{term C} is the goal to be proved and the @{term "A\<^isub>i"} are
   329   @{text "Const (\"prop\", bool \<Rightarrow> bool)"}; in the figure we make it visible
   350   @{text "Const (\"prop\", bool \<Rightarrow> bool)"}; in the figure we make it visible
   330   as an @{text #}). This wrapper prevents that premises of @{text C} are
   351   as an @{text #}). This wrapper prevents that premises of @{text C} are
   331   misinterpreted as open subgoals. While tactics can operate on the subgoals
   352   misinterpreted as open subgoals. While tactics can operate on the subgoals
   332   (the @{text "A\<^isub>i"} above), they are expected to leave the conclusion
   353   (the @{text "A\<^isub>i"} above), they are expected to leave the conclusion
   333   @{term C} intact, with the exception of possibly instantiating schematic
   354   @{term C} intact, with the exception of possibly instantiating schematic
   334   variables. The instantiations of schematic variables can even be observed 
   355   variables. This instantiations of schematic variables can be observed 
   335   on the user level. For this consider the following definition and proof.
   356   on the user level. Have a look at the following definition and proof.
   336 *}
   357 *}
   337 
   358 
   338 definition 
   359 definition 
   339   EQ_TRUE 
   360   EQ_TRUE 
   340 where
   361 where
   347 
   368 
   348 text {*
   369 text {*
   349   Although Isabelle issues a warning message when stating goals involving 
   370   Although Isabelle issues a warning message when stating goals involving 
   350   meta-variables, it is possible to prove this theorem. The reason for the warning 
   371   meta-variables, it is possible to prove this theorem. The reason for the warning 
   351   message is that the proved theorem is not @{text "EQ_TRUE ?X"}, as one might 
   372   message is that the proved theorem is not @{text "EQ_TRUE ?X"}, as one might 
   352   expect, but @{thm test}:
   373   expect, but @{thm test}. We can test this with:
   353 
   374 
   354   \begin{isabelle}
   375   \begin{isabelle}
   355   \isacommand{thm}~@{thm [source] test}\\
   376   \isacommand{thm}~@{thm [source] test}\\
   356   @{text ">"}~@{thm test}
   377   @{text ">"}~@{thm test}
   357   \end{isabelle}
   378   \end{isabelle}
   358  
   379  
   359   As can be seen, the schematic variable @{text "?X"} has been instantiated inside the proof.
   380   The reason for this result is that the schematic variable @{text "?X"} 
       
   381   is instantiated inside the proof and then the instantiation propagated 
       
   382   to the ``outside''.
   360 
   383 
   361   \begin{readmore}
   384   \begin{readmore}
   362   For more information about the internals of goals see \isccite{sec:tactical-goals}.
   385   For more information about the internals of goals see \isccite{sec:tactical-goals}.
   363   \end{readmore}
   386   \end{readmore}
   364 
   387 
   365 *}
   388 *}
   366 
   389 
   367 section {* Simple Tactics\label{sec:simpletacs} *}
   390 section {* Simple Tactics\label{sec:simpletacs} *}
   368 
   391 
   369 text {*
   392 text {*
   370   Let us start with explaining the simple tactic @{ML_ind  print_tac}, which is quite useful 
   393   In this section we will describe more of the simple tactics in Isabelle. The 
       
   394   first one is @{ML_ind  print_tac}, which is quite useful 
   371   for low-level debugging of tactics. It just prints out a message and the current 
   395   for low-level debugging of tactics. It just prints out a message and the current 
   372   goal state. Unlike @{ML my_print_tac} shown earlier, it prints the goal state 
   396   goal state. Unlike @{ML my_print_tac} shown earlier, it prints the goal state 
   373   as the user would see it.  For example, processing the proof
   397   as the user would see it.  For example, processing the proof
   374 *}
   398 *}
   375  
   399  
   376 lemma shows "False \<Longrightarrow> True"
   400 lemma 
       
   401   shows "False \<Longrightarrow> True"
   377 apply(tactic {* print_tac "foo message" *})
   402 apply(tactic {* print_tac "foo message" *})
   378 txt{*gives:\medskip
   403 txt{*gives:\medskip
   379 
   404 
   380      \begin{minipage}{\textwidth}\small
   405      \begin{minipage}{\textwidth}\small
   381      @{text "foo message"}\\[3mm] 
   406      @{text "foo message"}\\[3mm] 
   385    *}
   410    *}
   386 (*<*)oops(*>*)
   411 (*<*)oops(*>*)
   387 
   412 
   388 text {*
   413 text {*
   389   A simple tactic for easy discharge of any proof obligations is 
   414   A simple tactic for easy discharge of any proof obligations is 
   390   @{ML_ind  cheat_tac in Skip_Proof}. This tactic corresponds to
   415   @{ML_ind  cheat_tac in Skip_Proof} in the structure @{ML_struct Skip_Proof}. 
   391   the Isabelle command \isacommand{sorry} and is sometimes useful  
   416   This tactic corresponds to the Isabelle command \isacommand{sorry} and is 
   392   during the development of tactics.
   417   sometimes useful during the development of tactics.
   393 *}
   418 *}
   394 
   419 
   395 lemma shows "False" and "Goldbach_conjecture"  
   420 lemma 
       
   421   shows "False" and "Goldbach_conjecture"  
   396 apply(tactic {* Skip_Proof.cheat_tac @{theory} *})
   422 apply(tactic {* Skip_Proof.cheat_tac @{theory} *})
   397 txt{*\begin{minipage}{\textwidth}
   423 txt{*\begin{minipage}{\textwidth}
   398      @{subgoals [display]}
   424      @{subgoals [display]}
   399      \end{minipage}*}
   425      \end{minipage}*}
   400 (*<*)oops(*>*)
   426 (*<*)oops(*>*)
   401 
   427 
   402 text {*
   428 text {*
   403   This tactic works however only if the quick-and-dirty mode of Isabelle 
   429   This tactic works however only if the quick-and-dirty mode of Isabelle 
   404   is switched on.
   430   is switched on. This is done automatically in the ``interactive
   405 
   431   mode'' of Isabelle, but must ne done manually in the ``batch mode''.
   406   Another simple tactic is the function @{ML_ind  atac}, which, as shown in the previous
   432 
   407   section, corresponds to the assumption command.
   433   Another simple tactic is the function @{ML_ind  atac}, which, as shown 
   408 *}
   434   earlier, corresponds to the assumption method.
   409 
   435 *}
   410 lemma shows "P \<Longrightarrow> P"
   436 
       
   437 lemma 
       
   438   shows "P \<Longrightarrow> P"
   411 apply(tactic {* atac 1 *})
   439 apply(tactic {* atac 1 *})
   412 txt{*\begin{minipage}{\textwidth}
   440 txt{*\begin{minipage}{\textwidth}
   413      @{subgoals [display]}
   441      @{subgoals [display]}
   414      \end{minipage}*}
   442      \end{minipage}*}
   415 (*<*)oops(*>*)
   443 (*<*)oops(*>*)
   420   to @{text rule}, @{text drule}, @{text erule} and @{text frule}, 
   448   to @{text rule}, @{text drule}, @{text erule} and @{text frule}, 
   421   respectively. Each of them takes a theorem as argument and attempts to 
   449   respectively. Each of them takes a theorem as argument and attempts to 
   422   apply it to a goal. Below are three self-explanatory examples.
   450   apply it to a goal. Below are three self-explanatory examples.
   423 *}
   451 *}
   424 
   452 
   425 lemma shows "P \<and> Q"
   453 lemma 
       
   454   shows "P \<and> Q"
   426 apply(tactic {* rtac @{thm conjI} 1 *})
   455 apply(tactic {* rtac @{thm conjI} 1 *})
   427 txt{*\begin{minipage}{\textwidth}
   456 txt{*\begin{minipage}{\textwidth}
   428      @{subgoals [display]}
   457      @{subgoals [display]}
   429      \end{minipage}*}
   458      \end{minipage}*}
   430 (*<*)oops(*>*)
   459 (*<*)oops(*>*)
   431 
   460 
   432 lemma shows "P \<and> Q \<Longrightarrow> False"
   461 lemma 
       
   462   shows "P \<and> Q \<Longrightarrow> False"
   433 apply(tactic {* etac @{thm conjE} 1 *})
   463 apply(tactic {* etac @{thm conjE} 1 *})
   434 txt{*\begin{minipage}{\textwidth}
   464 txt{*\begin{minipage}{\textwidth}
   435      @{subgoals [display]}
   465      @{subgoals [display]}
   436      \end{minipage}*}
   466      \end{minipage}*}
   437 (*<*)oops(*>*)
   467 (*<*)oops(*>*)
   438 
   468 
   439 lemma shows "False \<and> True \<Longrightarrow> False"
   469 lemma 
       
   470   shows "False \<and> True \<Longrightarrow> False"
   440 apply(tactic {* dtac @{thm conjunct2} 1 *})
   471 apply(tactic {* dtac @{thm conjunct2} 1 *})
   441 txt{*\begin{minipage}{\textwidth}
   472 txt{*\begin{minipage}{\textwidth}
   442      @{subgoals [display]}
   473      @{subgoals [display]}
   443      \end{minipage}*}
   474      \end{minipage}*}
   444 (*<*)oops(*>*)
   475 (*<*)oops(*>*)
   455 text {*
   486 text {*
   456   an example for @{ML resolve_tac} is the following proof where first an outermost 
   487   an example for @{ML resolve_tac} is the following proof where first an outermost 
   457   implication is analysed and then an outermost conjunction.
   488   implication is analysed and then an outermost conjunction.
   458 *}
   489 *}
   459 
   490 
   460 lemma shows "C \<longrightarrow> (A \<and> B)" and "(A \<longrightarrow> B) \<and> C"
   491 lemma 
       
   492   shows "C \<longrightarrow> (A \<and> B)" 
       
   493   and   "(A \<longrightarrow> B) \<and> C"
   461 apply(tactic {* resolve_xmp_tac 1 *})
   494 apply(tactic {* resolve_xmp_tac 1 *})
   462 apply(tactic {* resolve_xmp_tac 2 *})
   495 apply(tactic {* resolve_xmp_tac 2 *})
   463 txt{*\begin{minipage}{\textwidth}
   496 txt{*\begin{minipage}{\textwidth}
   464      @{subgoals [display]} 
   497      @{subgoals [display]} 
   465      \end{minipage}*}
   498      \end{minipage}*}
   470   @{ML dtac} (@{ML_ind  dresolve_tac}), @{ML etac} 
   503   @{ML dtac} (@{ML_ind  dresolve_tac}), @{ML etac} 
   471   (@{ML_ind  eresolve_tac}) and so on.
   504   (@{ML_ind  eresolve_tac}) and so on.
   472 
   505 
   473 
   506 
   474   Another simple tactic is @{ML_ind  cut_facts_tac}. It inserts a list of theorems
   507   Another simple tactic is @{ML_ind  cut_facts_tac}. It inserts a list of theorems
   475   into the assumptions of the current goal state. For example
   508   into the assumptions of the current goal state. Below we will insert the definitions
   476 *}
   509   for the constants @{term True} and @{term False}. So
   477 
   510 *}
   478 lemma shows "True \<noteq> False"
   511 
       
   512 lemma 
       
   513   shows "True \<noteq> False"
   479 apply(tactic {* cut_facts_tac [@{thm True_def}, @{thm False_def}] 1 *})
   514 apply(tactic {* cut_facts_tac [@{thm True_def}, @{thm False_def}] 1 *})
   480 txt{*produces the goal state\medskip
   515 txt{*produces the goal state\medskip
   481 
   516 
   482      \begin{minipage}{\textwidth}
   517      \begin{minipage}{\textwidth}
   483      @{subgoals [display]} 
   518      @{subgoals [display]} 
   484      \end{minipage}*}
   519      \end{minipage}*}
   485 (*<*)oops(*>*)
   520 (*<*)oops(*>*)
   486 
   521 
   487 text {*
   522 text {*
   488   Since rules are applied using higher-order unification, an automatic proof
       
   489   procedure might become too fragile, if it just applies inference rules as 
       
   490   shown above. The reason is that a number of rules introduce meta-variables 
       
   491   into the goal state. Consider for example the proof
       
   492 *}
       
   493 
       
   494 lemma shows "\<forall>x \<in> A. P x \<Longrightarrow> Q x"
       
   495 apply(tactic {* dtac @{thm bspec} 1 *})
       
   496 txt{*\begin{minipage}{\textwidth}
       
   497      @{subgoals [display]} 
       
   498      \end{minipage}*}
       
   499 (*<*)oops(*>*)
       
   500 
       
   501 text {*
       
   502   where the application of rule @{text bspec} generates two subgoals involving the
       
   503   meta-variable @{text "?x"}. Now, if you are not careful, tactics 
       
   504   applied to the first subgoal might instantiate this meta-variable in such a 
       
   505   way that the second subgoal becomes unprovable. If it is clear what the @{text "?x"}
       
   506   should be, then this situation can be avoided by introducing a more
       
   507   constrained version of the @{text bspec}-rule. Such constraints can be given by
       
   508   pre-instantiating theorems with other theorems. One function to do this is
       
   509   @{ML_ind "RS"}
       
   510   
       
   511   @{ML_response_fake [display,gray]
       
   512   "@{thm disjI1} RS @{thm conjI}" "\<lbrakk>?P1; ?Q\<rbrakk> \<Longrightarrow> (?P1 \<or> ?Q1) \<and> ?Q"}
       
   513 
       
   514   which in the example instantiates the first premise of the @{text conjI}-rule 
       
   515   with the rule @{text disjI1}. If the instantiation is impossible, as in the 
       
   516   case of
       
   517 
       
   518   @{ML_response_fake_both [display,gray]
       
   519   "@{thm conjI} RS @{thm mp}" 
       
   520 "*** Exception- THM (\"RSN: no unifiers\", 1, 
       
   521 [\"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q\", \"\<lbrakk>?P \<longrightarrow> ?Q; ?P\<rbrakk> \<Longrightarrow> ?Q\"]) raised"}
       
   522 
       
   523   then the function raises an exception. The function @{ML_ind  RSN} is similar to @{ML RS}, but 
       
   524   takes an additional number as argument that makes explicit which premise 
       
   525   should be instantiated. 
       
   526 
       
   527   To improve readability of the theorems we shall produce below, we will use the
       
   528   function @{ML no_vars} from Section~\ref{sec:printing}, which transforms
       
   529   schematic variables into free ones.  Using this function for the first @{ML
       
   530   RS}-expression above produces the more readable result:
       
   531 
       
   532   @{ML_response_fake [display,gray]
       
   533   "no_vars @{context} (@{thm disjI1} RS @{thm conjI})" "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> (P \<or> Qa) \<and> Q"}
       
   534 
       
   535   If you want to instantiate more than one premise of a theorem, you can use 
       
   536   the function @{ML_ind  MRS}:
       
   537 
       
   538   @{ML_response_fake [display,gray]
       
   539   "no_vars @{context} ([@{thm disjI1}, @{thm disjI2}] MRS @{thm conjI})" 
       
   540   "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> (P \<or> Qa) \<and> (Pa \<or> Q)"}
       
   541 
       
   542   If you need to instantiate lists of theorems, you can use the
       
   543   functions @{ML RL} and @{ML_ind  MRL}. For example in the code below,
       
   544   every theorem in the second list is instantiated with every 
       
   545   theorem in the first.
       
   546 
       
   547   @{ML_response_fake [display,gray]
       
   548 "map (no_vars @{context}) 
       
   549    ([@{thm impI}, @{thm disjI2}] RL [@{thm conjI}, @{thm disjI1}])" 
       
   550 "[\<lbrakk>P \<Longrightarrow> Q; Qa\<rbrakk> \<Longrightarrow> (P \<longrightarrow> Q) \<and> Qa,
       
   551  \<lbrakk>Q; Qa\<rbrakk> \<Longrightarrow> (P \<or> Q) \<and> Qa,
       
   552  (P \<Longrightarrow> Q) \<Longrightarrow> (P \<longrightarrow> Q) \<or> Qa,
       
   553  Q \<Longrightarrow> (P \<or> Q) \<or> Qa]"}
       
   554 
       
   555   \begin{readmore}
       
   556   The combinators for instantiating theorems are defined in @{ML_file "Pure/drule.ML"}.
       
   557   \end{readmore}
       
   558 
       
   559   Often proofs on the ML-level involve elaborate operations on assumptions and 
   523   Often proofs on the ML-level involve elaborate operations on assumptions and 
   560   @{text "\<And>"}-quantified variables. To do such operations using the basic tactics 
   524   @{text "\<And>"}-quantified variables. To do such operations using the basic tactics 
   561   shown so far is very unwieldy and brittle. Some convenience and
   525   shown so far is very unwieldy and brittle. Some convenience and
   562   safety is provided by the functions @{ML_ind  FOCUS in Subgoal} and 
   526   safety is provided by the functions @{ML_ind  FOCUS in Subgoal} and 
   563   @{ML_ind  SUBPROOF}. These tactics fix the parameters 
   527   @{ML_ind  SUBPROOF}. These tactics fix the parameters 
   564   and bind the various components of a goal state to a record. 
   528   and bind the various components of a goal state to a record. 
   565   To see what happens, use the function defined in Figure~\ref{fig:sptac}, which
   529   To see what happens, suppose the function defined in Figure~\ref{fig:sptac}, which
   566   takes a record and just prints out the contents of this record. Consider
   530   takes a record and just prints out the contents of this record. Consider
   567   now the proof:
   531   now the proof:
   568 *}
   532 *}
       
   533 
       
   534 ML {* Subgoal.FOCUS *}
   569 
   535 
   570 text_raw{*
   536 text_raw{*
   571 \begin{figure}[t]
   537 \begin{figure}[t]
   572 \begin{minipage}{\textwidth}
   538 \begin{minipage}{\textwidth}
   573 \begin{isabelle}
   539 \begin{isabelle}
   574 *}
   540 *}
   575 
   541 
   576 
       
   577 ML{*fun foc_tac {prems, params, asms, concl, context, schematics} = 
   542 ML{*fun foc_tac {prems, params, asms, concl, context, schematics} = 
   578 let 
   543 let 
   579   val string_of_params = string_of_cterms context (map snd params)
   544   fun pairs1 f1 f2 xs =
       
   545     commas (map (fn (x, y) => f1 x ^ ":=" ^ f2 y) xs) 
       
   546 
       
   547   fun pairs2 f xs = pairs1 f f xs 
       
   548 
       
   549   val string_of_params = pairs1 I (string_of_cterm context) params
   580   val string_of_asms = string_of_cterms context asms
   550   val string_of_asms = string_of_cterms context asms
   581   val string_of_concl = string_of_cterm context concl
   551   val string_of_concl = string_of_cterm context concl
   582   val string_of_prems = string_of_thms_no_vars context prems   
   552   val string_of_prems = string_of_thms_no_vars context prems   
   583   val string_of_schms = string_of_cterms context (map fst (snd schematics))    
   553   val string_of_schms = pairs2 (string_of_cterm context) (snd schematics)
   584  
   554 
   585   val strs = ["params: " ^ string_of_params,
   555   val strs = ["params: " ^ string_of_params,
   586               "schematics: " ^ string_of_schms,
   556               "schematics: " ^ string_of_schms,
   587               "assumptions: " ^ string_of_asms,
   557               "assumptions: " ^ string_of_asms,
   588               "conclusion: " ^ string_of_concl,
   558               "conclusion: " ^ string_of_concl,
   589               "premises: " ^ string_of_prems]
   559               "premises: " ^ string_of_prems]
   599   in Section~\ref{sec:printing} for extracting strings from @{ML_type cterm}s 
   569   in Section~\ref{sec:printing} for extracting strings from @{ML_type cterm}s 
   600   and @{ML_type thm}s.\label{fig:sptac}}
   570   and @{ML_type thm}s.\label{fig:sptac}}
   601 \end{figure}
   571 \end{figure}
   602 *}
   572 *}
   603 
   573 
   604 lemma shows "\<And>x y. A x y \<Longrightarrow> B y x \<longrightarrow> C (?z y) x"
   574 lemma 
       
   575   shows "\<And>x y. A x y \<Longrightarrow> B y x \<longrightarrow> C (?z y) x"
   605 apply(tactic {* Subgoal.FOCUS foc_tac @{context} 1 *})
   576 apply(tactic {* Subgoal.FOCUS foc_tac @{context} 1 *})
   606 
   577 
   607 txt {*
   578 txt {*
   608   The tactic produces the following printout:
   579   The tactic produces the following printout:
   609 
   580 
   610   \begin{quote}\small
   581   \begin{quote}\small
   611   \begin{tabular}{ll}
   582   \begin{tabular}{ll}
   612   params:      & @{term x}, @{term y}\\
   583   params:      & @{text "x:=x"}, @{text "y:=y"}\\
   613   schematics:  & @{text ?z}\\
   584   schematics:  & @{text "?z:=z"}\\
   614   assumptions: & @{term "A x y"}\\
   585   assumptions: & @{term "A x y"}\\
   615   conclusion:  & @{term "B y x \<longrightarrow> C (z y) x"}\\
   586   conclusion:  & @{term "B y x \<longrightarrow> C (z y) x"}\\
   616   premises:    & @{term "A x y"}
   587   premises:    & @{term "A x y"}
   617   \end{tabular}
   588   \end{tabular}
   618   \end{quote}
   589   \end{quote}
   619 
   590 
   620   (FIXME: Find out how nowadays the schematics are handled)
   591   The @{text params} and @{text schematics} stand or list of pairs where the left-hand
   621 
   592   side of @{text ":="} is replaced by the right-hand side inside the tactic.
   622   Notice in the actual output the brown colour of the variables @{term x}, 
   593   Notice that in the actual output the brown colour of the variables @{term x}, 
   623   and @{term y}. Although they are parameters in the original goal, they are fixed inside
   594   and @{term y}. Although they are parameters in the original goal, they are fixed inside
   624   the tactic. By convention these fixed variables are printed in brown colour.
   595   the tactic. By convention these fixed variables are printed in brown colour.
   625   Similarly the schematic variable @{text ?z}. The assumption, or premise, 
   596   Similarly the schematic variable @{text ?z}. The assumption, or premise, 
   626   @{prop "A x y"} is bound as @{ML_type cterm} to the record-variable 
   597   @{prop "A x y"} is bound as @{ML_type cterm} to the record-variable 
   627   @{text asms}, but also as @{ML_type thm} to @{text prems}.
   598   @{text asms}, but also as @{ML_type thm} to @{text prems}.
   635 txt {*
   606 txt {*
   636   then the tactic prints out: 
   607   then the tactic prints out: 
   637 
   608 
   638   \begin{quote}\small
   609   \begin{quote}\small
   639   \begin{tabular}{ll}
   610   \begin{tabular}{ll}
   640   params:      & @{term x}, @{term y}\\
   611   params:      & @{text "x:=x"}, @{text "y:=y"}\\
   641   schematics:  & @{text ?z}\\
   612   schematics:  & @{text "?z:=z"}\\
   642   assumptions: & @{term "A x y"}, @{term "B y x"}\\
   613   assumptions: & @{term "A x y"}, @{term "B y x"}\\
   643   conclusion:  & @{term "C (z y) x"}\\
   614   conclusion:  & @{term "C (z y) x"}\\
   644   premises:    & @{term "A x y"}, @{term "B y x"}
   615   premises:    & @{term "A x y"}, @{term "B y x"}
   645   \end{tabular}
   616   \end{tabular}
   646   \end{quote}
   617   \end{quote}
   648 (*<*)oops(*>*)
   619 (*<*)oops(*>*)
   649 
   620 
   650 text {*
   621 text {*
   651   Now also @{term "B y x"} is an assumption bound to @{text asms} and @{text prems}.
   622   Now also @{term "B y x"} is an assumption bound to @{text asms} and @{text prems}.
   652 
   623 
   653   The difference between the tactics @{ML SUBPROOF} and @{ML FOCUS in Subgoal}
   624   One difference between the tactics @{ML SUBPROOF} and @{ML FOCUS in Subgoal}
   654   is that the former expects that the goal is solved completely, which the
   625   is that the former expects that the goal is solved completely, which the
   655   latter does not. @{ML SUBPROOF} can also not instantiate an schematic
   626   latter does not. Another is that @{ML SUBPROOF} can not instantiate any schematic
   656   variables.
   627   variables.
   657 
   628 
   658   One convenience of both @{ML FOCUS in Subgoal} and @{ML SUBPROOF} is that we
   629   One convenience of both @{ML FOCUS in Subgoal} and @{ML SUBPROOF} is that we
   659   can apply the assumptions using the usual tactics, because the parameter
   630   can apply the assumptions using the usual tactics, because the parameter
   660   @{text prems} contains them as theorems. With this you can easily implement
   631   @{text prems} contains them as theorems. With this you can easily implement
   665 
   636 
   666 text {*
   637 text {*
   667   If you apply @{ML atac'} to the next lemma
   638   If you apply @{ML atac'} to the next lemma
   668 *}
   639 *}
   669 
   640 
   670 lemma shows "\<lbrakk>B x y; A x y; C x y\<rbrakk> \<Longrightarrow> A x y"
   641 lemma 
       
   642   shows "\<lbrakk>B x y; A x y; C x y\<rbrakk> \<Longrightarrow> A x y"
   671 apply(tactic {* atac' @{context} 1 *})
   643 apply(tactic {* atac' @{context} 1 *})
   672 txt{* it will produce
   644 txt{* it will produce
   673       @{subgoals [display]} *}
   645       @{subgoals [display]} *}
   674 (*<*)oops(*>*)
   646 (*<*)oops(*>*)
   675 
   647 
   682   subproof. It is therefore possible to also apply @{ML atac'} to the second
   654   subproof. It is therefore possible to also apply @{ML atac'} to the second
   683   goal by just writing:
   655   goal by just writing:
   684 
   656 
   685 *}
   657 *}
   686 
   658 
   687 lemma shows "True" and "\<lbrakk>B x y; A x y; C x y\<rbrakk> \<Longrightarrow> A x y"
   659 lemma 
       
   660   shows "True" 
       
   661   and   "\<lbrakk>B x y; A x y; C x y\<rbrakk> \<Longrightarrow> A x y"
   688 apply(tactic {* atac' @{context} 2 *})
   662 apply(tactic {* atac' @{context} 2 *})
   689 apply(rule TrueI)
   663 apply(rule TrueI)
   690 done
   664 done
   691 
   665 
   692 
   666 
   737 
   711 
   738   Let us now see how to apply this tactic. Consider the four goals:
   712   Let us now see how to apply this tactic. Consider the four goals:
   739 *}
   713 *}
   740 
   714 
   741 
   715 
   742 lemma shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
   716 lemma 
       
   717   shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
   743 apply(tactic {* SUBGOAL select_tac 4 *})
   718 apply(tactic {* SUBGOAL select_tac 4 *})
   744 apply(tactic {* SUBGOAL select_tac 3 *})
   719 apply(tactic {* SUBGOAL select_tac 3 *})
   745 apply(tactic {* SUBGOAL select_tac 2 *})
   720 apply(tactic {* SUBGOAL select_tac 2 *})
   746 apply(tactic {* SUBGOAL select_tac 1 *})
   721 apply(tactic {* SUBGOAL select_tac 1 *})
   747 txt{* \begin{minipage}{\textwidth}
   722 txt{* \begin{minipage}{\textwidth}
   754   Note that we applied the tactic to the goals in ``reverse'' order. 
   729   Note that we applied the tactic to the goals in ``reverse'' order. 
   755   This is a trick in order to be independent from the subgoals that are 
   730   This is a trick in order to be independent from the subgoals that are 
   756   produced by the rule. If we had applied it in the other order 
   731   produced by the rule. If we had applied it in the other order 
   757 *}
   732 *}
   758 
   733 
   759 lemma shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
   734 lemma 
       
   735   shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
   760 apply(tactic {* SUBGOAL select_tac 1 *})
   736 apply(tactic {* SUBGOAL select_tac 1 *})
   761 apply(tactic {* SUBGOAL select_tac 3 *})
   737 apply(tactic {* SUBGOAL select_tac 3 *})
   762 apply(tactic {* SUBGOAL select_tac 4 *})
   738 apply(tactic {* SUBGOAL select_tac 4 *})
   763 apply(tactic {* SUBGOAL select_tac 5 *})
   739 apply(tactic {* SUBGOAL select_tac 5 *})
   764 (*<*)oops(*>*)
   740 (*<*)oops(*>*)
   770 
   746 
   771   Of course, this example is
   747   Of course, this example is
   772   contrived: there are much simpler methods available in Isabelle for
   748   contrived: there are much simpler methods available in Isabelle for
   773   implementing a tactic analysing a goal according to its topmost
   749   implementing a tactic analysing a goal according to its topmost
   774   connective. These simpler methods use tactic combinators, which we will
   750   connective. These simpler methods use tactic combinators, which we will
   775   explain in the next section.
   751   explain in the next section, but before that we will show how
   776 
   752   tactic application can be constraint.
       
   753 
       
   754   Since rules are applied using higher-order unification, an automatic proof
       
   755   procedure might become too fragile, if it just applies inference rules as 
       
   756   shown above. The reason is that a number of rules introduce meta-variables 
       
   757   into the goal state. Consider for example the proof
       
   758 *}
       
   759 
       
   760 lemma 
       
   761   shows "\<forall>x \<in> A. P x \<Longrightarrow> Q x"
       
   762 apply(tactic {* dtac @{thm bspec} 1 *})
       
   763 txt{*\begin{minipage}{\textwidth}
       
   764      @{subgoals [display]} 
       
   765      \end{minipage}*}
       
   766 (*<*)oops(*>*)
       
   767 
       
   768 text {*
       
   769   where the application of rule @{text bspec} generates two subgoals involving the
       
   770   meta-variable @{text "?x"}. Now, if you are not careful, tactics 
       
   771   applied to the first subgoal might instantiate this meta-variable in such a 
       
   772   way that the second subgoal becomes unprovable. If it is clear what the @{text "?x"}
       
   773   should be, then this situation can be avoided by introducing a more
       
   774   constrained version of the @{text bspec}-rule. One way to give such 
       
   775   constraints is by pre-instantiating theorems with other theorems. 
       
   776   The function @{ML_ind "RS"}, for example, does this.
       
   777   
       
   778   @{ML_response_fake [display,gray]
       
   779   "@{thm disjI1} RS @{thm conjI}" "\<lbrakk>?P1; ?Q\<rbrakk> \<Longrightarrow> (?P1 \<or> ?Q1) \<and> ?Q"}
       
   780 
       
   781   In this example it instantiates the first premise of the @{text conjI}-rule 
       
   782   with the rule @{text disjI1}. If the instantiation is impossible, as in the 
       
   783   case of
       
   784 
       
   785   @{ML_response_fake_both [display,gray]
       
   786   "@{thm conjI} RS @{thm mp}" 
       
   787 "*** Exception- THM (\"RSN: no unifiers\", 1, 
       
   788 [\"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q\", \"\<lbrakk>?P \<longrightarrow> ?Q; ?P\<rbrakk> \<Longrightarrow> ?Q\"]) raised"}
       
   789 
       
   790   then the function raises an exception. The function @{ML_ind  RSN} is similar to @{ML RS}, but 
       
   791   takes an additional number as argument that makes explicit which premise 
       
   792   should be instantiated. 
       
   793 
       
   794   To improve readability of the theorems we shall produce below, we will use the
       
   795   function @{ML no_vars} from Section~\ref{sec:printing}, which transforms
       
   796   schematic variables into free ones.  Using this function for the first @{ML
       
   797   RS}-expression above produces the more readable result:
       
   798 
       
   799   @{ML_response_fake [display,gray]
       
   800   "no_vars @{context} (@{thm disjI1} RS @{thm conjI})" "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> (P \<or> Qa) \<and> Q"}
       
   801 
       
   802   If you want to instantiate more than one premise of a theorem, you can use 
       
   803   the function @{ML_ind  MRS}:
       
   804 
       
   805   @{ML_response_fake [display,gray]
       
   806   "no_vars @{context} ([@{thm disjI1}, @{thm disjI2}] MRS @{thm conjI})" 
       
   807   "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> (P \<or> Qa) \<and> (Pa \<or> Q)"}
       
   808 
       
   809   If you need to instantiate lists of theorems, you can use the
       
   810   functions @{ML RL} and @{ML_ind  MRL}. For example in the code below,
       
   811   every theorem in the second list is instantiated with every 
       
   812   theorem in the first.
       
   813 
       
   814   @{ML_response_fake [display,gray]
       
   815 "map (no_vars @{context}) 
       
   816    ([@{thm impI}, @{thm disjI2}] RL [@{thm conjI}, @{thm disjI1}])" 
       
   817 "[\<lbrakk>P \<Longrightarrow> Q; Qa\<rbrakk> \<Longrightarrow> (P \<longrightarrow> Q) \<and> Qa,
       
   818  \<lbrakk>Q; Qa\<rbrakk> \<Longrightarrow> (P \<or> Q) \<and> Qa,
       
   819  (P \<Longrightarrow> Q) \<Longrightarrow> (P \<longrightarrow> Q) \<or> Qa,
       
   820  Q \<Longrightarrow> (P \<or> Q) \<or> Qa]"}
       
   821 
       
   822   \begin{readmore}
       
   823   The combinators for instantiating theorems are defined in @{ML_file "Pure/drule.ML"}.
       
   824   \end{readmore}
       
   825 
       
   826   Higher-order unification is also often in the way when applying certain 
       
   827   congruence theorems. For example we would expect that the theorem 
       
   828   @{thm [source] cong}
       
   829 
       
   830   \begin{isabelle}
       
   831   \isacommand{thm}~@{thm [source] cong}\\
       
   832   @{text "> "}~@{thm cong}
       
   833   \end{isabelle}
       
   834 
       
   835   is applicable in the following proof producing the subgoals
       
   836   @{term "t x = s u"} and @{term "y = w"}. 
       
   837 *}
       
   838 
       
   839 lemma 
       
   840   fixes x y u w::"'a"
       
   841   shows "t x y = s u w"
       
   842 apply(rule cong)
       
   843 txt{*\begin{minipage}{\textwidth}
       
   844      @{subgoals [display]}
       
   845      \end{minipage}*}
       
   846 (*<*)oops(*>*)
       
   847 
       
   848 text {*
       
   849   As you can see this is unfortunately \emph{not} the case. The problem is
       
   850   that higher-order unification produces an instantiation that is not the
       
   851   intended one. While we can use \isacommand{back} to interactively find the
       
   852   intended instantiation, this is not an option for an automatic proof
       
   853   procedure. Fortunately, the tactic @{ML_ind cong_tac in Cong_Tac} helps 
       
   854   with applying congruence rules and finding the intended instantiation.
       
   855 *}
       
   856 
       
   857 lemma 
       
   858   fixes x y u w::"'a"
       
   859   shows "t x y = s u w"
       
   860 apply(tactic {* Cong_Tac.cong_tac @{thm cong} 1 *})
       
   861 txt{*\begin{minipage}{\textwidth}
       
   862      @{subgoals [display]}
       
   863      \end{minipage}*}
       
   864 (*<*)oops(*>*)
       
   865 
       
   866 text {*
       
   867   However, sometimes it is necessary to expicitly match a theroem against a proof
       
   868   state and in doing so finding manually an appropriate instantiation. Imagine 
       
   869   you have the theorem
       
   870 *}
       
   871 
       
   872 lemma rule:
       
   873   shows "\<lbrakk>f = g; x = y\<rbrakk> \<Longrightarrow> R (f x) (g y)"
       
   874 sorry
       
   875 
       
   876 text {* 
       
   877   and you want to apply it to the goal @{term "(t\<^isub>1 t\<^isub>2) \<le>
       
   878   (s\<^isub>1 s\<^isub>2)"}. Since in this theorem, all variables are
       
   879   schematic, we have a nasty higher-order unification problem and @{text rtac}
       
   880   will not be able to apply this rule as we want. However, in the proof below
       
   881   we are only interested where @{term R} is instantiated to a constant and
       
   882   similarly the instantiation for the other variables is ``obvious'' from the
       
   883   proof state.  To use this rule we essentially match the conclusion of 
       
   884   @{thm [source] rule} against the goal state reading of an instantiation for
       
   885   @{thm [source] rule}. For this the function @{ML_ind first_order_match in Thm} 
       
   886   matches two @{ML_type cterm}s and produces, in the sucessful case, a matcher 
       
   887   that can be used to instantiate the @{thm [source] rule}. The instantiation 
       
   888   can be done with the function @{ML_ind instantiate in Drule}. To automate 
       
   889   this we implement the following function.
       
   890 *}
       
   891 
       
   892 ML{*fun fo_rtac thm = 
       
   893   Subgoal.FOCUS (fn {concl, ...} =>
       
   894   let 
       
   895     val concl_pat = Drule.strip_imp_concl (cprop_of thm)
       
   896     val insts = Thm.first_order_match (concl_pat, concl)
       
   897   in
       
   898     rtac (Drule.instantiate insts thm) 1
       
   899   end)*}
       
   900 
       
   901 text {*
       
   902   Note that we use @{ML FOCUS in Subgoal} because we have directly access
       
   903   to the conclusion of the goal state and also because this function also
       
   904   takes care about correctly handling parameters that might be present
       
   905   in the goal state. An example for @{ML fo_rtac} is as follows.
       
   906 *}
       
   907 
       
   908 lemma
       
   909   shows "\<And>t\<^isub>1 s\<^isub>1 (t\<^isub>2::'a) (s\<^isub>2::'a). (t\<^isub>1 t\<^isub>2) \<le> (s\<^isub>1 s\<^isub>2)"
       
   910 apply(tactic {* fo_rtac @{thm rule} @{context} 1 *})
       
   911 txt{*\begin{minipage}{\textwidth}
       
   912      @{subgoals [display]}
       
   913      \end{minipage}*}
       
   914 (*<*)oops(*>*)
       
   915 
       
   916 text {*
       
   917   We obtain the intended subgoals and also the parameters are correctly
       
   918   introduced in both of them. Such manual instantiations are quite frequently
       
   919   necessary in order to contrain the application of inference rules. Otherwise
       
   920   one ends up with ``wild'' higher-order unification problems that make
       
   921   automatic proofs fails.
   777 *}
   922 *}
   778 
   923 
   779 section {* Tactic Combinators *}
   924 section {* Tactic Combinators *}
   780 
   925 
   781 text {* 
   926 text {* 
   782   The purpose of tactic combinators is to build compound tactics out of
   927   The purpose of tactic combinators is to build compound tactics out of
   783   smaller tactics. In the previous section we already used @{ML THEN}, which
   928   smaller tactics. In the previous section we already used @{ML THEN}, which
   784   just strings together two tactics in a sequence. For example:
   929   just strings together two tactics in a sequence. For example:
   785 *}
   930 *}
   786 
   931 
   787 lemma shows "(Foo \<and> Bar) \<and> False"
   932 lemma 
       
   933   shows "(Foo \<and> Bar) \<and> False"
   788 apply(tactic {* rtac @{thm conjI} 1 THEN rtac @{thm conjI} 1 *})
   934 apply(tactic {* rtac @{thm conjI} 1 THEN rtac @{thm conjI} 1 *})
   789 txt {* \begin{minipage}{\textwidth}
   935 txt {* \begin{minipage}{\textwidth}
   790        @{subgoals [display]} 
   936        @{subgoals [display]} 
   791        \end{minipage} *}
   937        \end{minipage} *}
   792 (*<*)oops(*>*)
   938 (*<*)oops(*>*)
   795   If you want to avoid the hard-coded subgoal addressing, then, as 
   941   If you want to avoid the hard-coded subgoal addressing, then, as 
   796   seen earlier, you can use
   942   seen earlier, you can use
   797   the ``primed'' version of @{ML THEN}. For example:
   943   the ``primed'' version of @{ML THEN}. For example:
   798 *}
   944 *}
   799 
   945 
   800 lemma shows "(Foo \<and> Bar) \<and> False"
   946 lemma 
       
   947   shows "(Foo \<and> Bar) \<and> False"
   801 apply(tactic {* (rtac @{thm conjI} THEN' rtac @{thm conjI}) 1 *})
   948 apply(tactic {* (rtac @{thm conjI} THEN' rtac @{thm conjI}) 1 *})
   802 txt {* \begin{minipage}{\textwidth}
   949 txt {* \begin{minipage}{\textwidth}
   803        @{subgoals [display]} 
   950        @{subgoals [display]} 
   804        \end{minipage} *}
   951        \end{minipage} *}
   805 (*<*)oops(*>*)
   952 (*<*)oops(*>*)
   846 text {*
   993 text {*
   847   will first try out whether rule @{text disjI} applies and in case of failure 
   994   will first try out whether rule @{text disjI} applies and in case of failure 
   848   will try @{text conjI}. To see this consider the proof
   995   will try @{text conjI}. To see this consider the proof
   849 *}
   996 *}
   850 
   997 
   851 lemma shows "True \<and> False" and "Foo \<or> Bar"
   998 lemma 
       
   999   shows "True \<and> False" and "Foo \<or> Bar"
   852 apply(tactic {* orelse_xmp_tac 2 *})
  1000 apply(tactic {* orelse_xmp_tac 2 *})
   853 apply(tactic {* orelse_xmp_tac 1 *})
  1001 apply(tactic {* orelse_xmp_tac 1 *})
   854 txt {* which results in the goal state
  1002 txt {* which results in the goal state
   855 
  1003 
   856        \begin{minipage}{\textwidth}
  1004        \begin{minipage}{\textwidth}
   874   fail if no rule applies (we also have to wrap @{ML all_tac} using the 
  1022   fail if no rule applies (we also have to wrap @{ML all_tac} using the 
   875   @{ML K}-combinator, because it does not take a subgoal number as argument). You can 
  1023   @{ML K}-combinator, because it does not take a subgoal number as argument). You can 
   876   test the tactic on the same goals:
  1024   test the tactic on the same goals:
   877 *}
  1025 *}
   878 
  1026 
   879 lemma shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
  1027 lemma 
       
  1028   shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
   880 apply(tactic {* select_tac' 4 *})
  1029 apply(tactic {* select_tac' 4 *})
   881 apply(tactic {* select_tac' 3 *})
  1030 apply(tactic {* select_tac' 3 *})
   882 apply(tactic {* select_tac' 2 *})
  1031 apply(tactic {* select_tac' 2 *})
   883 apply(tactic {* select_tac' 1 *})
  1032 apply(tactic {* select_tac' 1 *})
   884 txt{* \begin{minipage}{\textwidth}
  1033 txt{* \begin{minipage}{\textwidth}
   890   Since such repeated applications of a tactic to the reverse order of 
  1039   Since such repeated applications of a tactic to the reverse order of 
   891   \emph{all} subgoals is quite common, there is the tactic combinator 
  1040   \emph{all} subgoals is quite common, there is the tactic combinator 
   892   @{ML_ind  ALLGOALS} that simplifies this. Using this combinator you can simply 
  1041   @{ML_ind  ALLGOALS} that simplifies this. Using this combinator you can simply 
   893   write: *}
  1042   write: *}
   894 
  1043 
   895 lemma shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
  1044 lemma 
       
  1045   shows "A \<and> B" and "A \<longrightarrow> B \<longrightarrow>C" and "\<forall>x. D x" and "E \<Longrightarrow> F"
   896 apply(tactic {* ALLGOALS select_tac' *})
  1046 apply(tactic {* ALLGOALS select_tac' *})
   897 txt{* \begin{minipage}{\textwidth}
  1047 txt{* \begin{minipage}{\textwidth}
   898       @{subgoals [display]}
  1048       @{subgoals [display]}
   899       \end{minipage} *}
  1049       \end{minipage} *}
   900 (*<*)oops(*>*)
  1050 (*<*)oops(*>*)
   916   unchanged. This, however, can be potentially very confusing when visible to 
  1066   unchanged. This, however, can be potentially very confusing when visible to 
   917   the user, for example,  in cases where the goal is the form
  1067   the user, for example,  in cases where the goal is the form
   918 
  1068 
   919 *}
  1069 *}
   920 
  1070 
   921 lemma shows "E \<Longrightarrow> F"
  1071 lemma 
       
  1072   shows "E \<Longrightarrow> F"
   922 apply(tactic {* select_tac' 1 *})
  1073 apply(tactic {* select_tac' 1 *})
   923 txt{* \begin{minipage}{\textwidth}
  1074 txt{* \begin{minipage}{\textwidth}
   924       @{subgoals [display]}
  1075       @{subgoals [display]}
   925       \end{minipage} *}
  1076       \end{minipage} *}
   926 (*<*)oops(*>*)
  1077 (*<*)oops(*>*)
   938   use the combinator @{ML_ind  CHANGED} to make sure the subgoal has been changed
  1089   use the combinator @{ML_ind  CHANGED} to make sure the subgoal has been changed
   939   by the tactic. Because now
  1090   by the tactic. Because now
   940 
  1091 
   941 *}
  1092 *}
   942 
  1093 
   943 lemma shows "E \<Longrightarrow> F"
  1094 lemma 
       
  1095   shows "E \<Longrightarrow> F"
   944 apply(tactic {* CHANGED (select_tac' 1) *})(*<*)?(*>*)
  1096 apply(tactic {* CHANGED (select_tac' 1) *})(*<*)?(*>*)
   945 txt{* gives the error message:
  1097 txt{* gives the error message:
   946   \begin{isabelle}
  1098   \begin{isabelle}
   947   @{text "*** empty result sequence -- proof command failed"}\\
  1099   @{text "*** empty result sequence -- proof command failed"}\\
   948   @{text "*** At command \"apply\"."}
  1100   @{text "*** At command \"apply\"."}
   959 
  1111 
   960 ML{*val repeat_xmp_tac = REPEAT (CHANGED (select_tac' 1)) *}
  1112 ML{*val repeat_xmp_tac = REPEAT (CHANGED (select_tac' 1)) *}
   961 
  1113 
   962 text {* which applied to the proof *}
  1114 text {* which applied to the proof *}
   963 
  1115 
   964 lemma shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1116 lemma 
       
  1117   shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
   965 apply(tactic {* repeat_xmp_tac *})
  1118 apply(tactic {* repeat_xmp_tac *})
   966 txt{* produces
  1119 txt{* produces
   967 
  1120 
   968       \begin{minipage}{\textwidth}
  1121       \begin{minipage}{\textwidth}
   969       @{subgoals [display]}
  1122       @{subgoals [display]}
   998 
  1151 
   999 text {* 
  1152 text {* 
  1000   you see that the following goal
  1153   you see that the following goal
  1001 *}
  1154 *}
  1002 
  1155 
  1003 lemma shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1156 lemma 
       
  1157   shows "((\<not>A) \<and> (\<forall>x. B x)) \<and> (C \<longrightarrow> D)"
  1004 apply(tactic {* repeat_all_new_xmp_tac 1 *})
  1158 apply(tactic {* repeat_all_new_xmp_tac 1 *})
  1005 txt{* \begin{minipage}{\textwidth}
  1159 txt{* \begin{minipage}{\textwidth}
  1006       @{subgoals [display]}
  1160       @{subgoals [display]}
  1007       \end{minipage} *}
  1161       \end{minipage} *}
  1008 (*<*)oops(*>*)
  1162 (*<*)oops(*>*)
  1014   Recall that tactics produce a lazy sequence of successor goal states. These
  1168   Recall that tactics produce a lazy sequence of successor goal states. These
  1015   states can be explored using the command \isacommand{back}. For example
  1169   states can be explored using the command \isacommand{back}. For example
  1016 
  1170 
  1017 *}
  1171 *}
  1018 
  1172 
  1019 lemma "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1173 lemma 
       
  1174   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1020 apply(tactic {* etac @{thm disjE} 1 *})
  1175 apply(tactic {* etac @{thm disjE} 1 *})
  1021 txt{* applies the rule to the first assumption yielding the goal state:\smallskip
  1176 txt{* applies the rule to the first assumption yielding the goal state:\smallskip
  1022       
  1177       
  1023       \begin{minipage}{\textwidth}
  1178       \begin{minipage}{\textwidth}
  1024       @{subgoals [display]}
  1179       @{subgoals [display]}
  1026 
  1181 
  1027       After typing
  1182       After typing
  1028   *}
  1183   *}
  1029 (*<*)
  1184 (*<*)
  1030 oops
  1185 oops
  1031 lemma "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1186 lemma 
       
  1187   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1032 apply(tactic {* etac @{thm disjE} 1 *})
  1188 apply(tactic {* etac @{thm disjE} 1 *})
  1033 (*>*)
  1189 (*>*)
  1034 back
  1190 back
  1035 txt{* the rule now applies to the second assumption.\smallskip
  1191 txt{* the rule now applies to the second assumption.\smallskip
  1036 
  1192 
  1044   the potential to explode the search space for tactics.
  1200   the potential to explode the search space for tactics.
  1045   These problems can be avoided by prefixing the tactic with the tactic 
  1201   These problems can be avoided by prefixing the tactic with the tactic 
  1046   combinator @{ML_ind  DETERM}.
  1202   combinator @{ML_ind  DETERM}.
  1047 *}
  1203 *}
  1048 
  1204 
  1049 lemma "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1205 lemma 
       
  1206   shows "\<lbrakk>P1 \<or> Q1; P2 \<or> Q2\<rbrakk> \<Longrightarrow> R"
  1050 apply(tactic {* DETERM (etac @{thm disjE} 1) *})
  1207 apply(tactic {* DETERM (etac @{thm disjE} 1) *})
  1051 txt {*\begin{minipage}{\textwidth}
  1208 txt {*\begin{minipage}{\textwidth}
  1052       @{subgoals [display]}
  1209       @{subgoals [display]}
  1053       \end{minipage} *}
  1210       \end{minipage} *}
  1054 (*<*)oops(*>*)
  1211 (*<*)oops(*>*)
  1186 
  1343 
  1187   All of the tactics take a simpset and an integer as argument (the latter as usual 
  1344   All of the tactics take a simpset and an integer as argument (the latter as usual 
  1188   to specify  the goal to be analysed). So the proof
  1345   to specify  the goal to be analysed). So the proof
  1189 *}
  1346 *}
  1190 
  1347 
  1191 lemma "Suc (1 + 2) < 3 + 2"
  1348 lemma 
       
  1349   shows "Suc (1 + 2) < 3 + 2"
  1192 apply(simp)
  1350 apply(simp)
  1193 done
  1351 done
  1194 
  1352 
  1195 text {*
  1353 text {*
  1196   corresponds on the ML-level to the tactic
  1354   corresponds on the ML-level to the tactic
  1197 *}
  1355 *}
  1198 
  1356 
  1199 lemma "Suc (1 + 2) < 3 + 2"
  1357 lemma 
       
  1358   shows "Suc (1 + 2) < 3 + 2"
  1200 apply(tactic {* asm_full_simp_tac @{simpset} 1 *})
  1359 apply(tactic {* asm_full_simp_tac @{simpset} 1 *})
  1201 done
  1360 done
  1202 
  1361 
  1203 text {*
  1362 text {*
  1204   If the simplifier cannot make any progress, then it leaves the goal unchanged,
  1363   If the simplifier cannot make any progress, then it leaves the goal unchanged,
  1409 
  1568 
  1410 text {*
  1569 text {*
  1411   then we can use this tactic to unfold the definition of the constant.
  1570   then we can use this tactic to unfold the definition of the constant.
  1412 *}
  1571 *}
  1413 
  1572 
  1414 lemma shows "MyTrue \<Longrightarrow> True"
  1573 lemma 
       
  1574   shows "MyTrue \<Longrightarrow> True"
  1415 apply(tactic {* rewrite_goal_tac @{thms MyTrue_def} 1 *})
  1575 apply(tactic {* rewrite_goal_tac @{thms MyTrue_def} 1 *})
  1416 txt{* producing the goal state
  1576 txt{* producing the goal state
  1417 
  1577 
  1418       \begin{minipage}{\textwidth}
  1578       \begin{minipage}{\textwidth}
  1419       @{subgoals [display]}
  1579       @{subgoals [display]}
  1458 | "perm_list pi (x#xs) = (pi\<bullet>x)#(perm_list pi xs)"
  1618 | "perm_list pi (x#xs) = (pi\<bullet>x)#(perm_list pi xs)"
  1459 
  1619 
  1460 end
  1620 end
  1461 
  1621 
  1462 lemma perm_append[simp]:
  1622 lemma perm_append[simp]:
  1463 fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1623   fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1464 shows "((pi\<^isub>1@pi\<^isub>2)\<bullet>c) = (pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c))"
  1624   shows "((pi\<^isub>1@pi\<^isub>2)\<bullet>c) = (pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c))"
  1465 by (induct pi\<^isub>1) (auto) 
  1625 by (induct pi\<^isub>1) (auto) 
  1466 
  1626 
  1467 lemma perm_bij[simp]:
  1627 lemma perm_bij[simp]:
  1468 fixes c d::"nat" and pi::"prm"
  1628   fixes c d::"nat" and pi::"prm"
  1469 shows "(pi\<bullet>c = pi\<bullet>d) = (c = d)" 
  1629   shows "(pi\<bullet>c = pi\<bullet>d) = (c = d)" 
  1470 by (induct pi) (auto)
  1630 by (induct pi) (auto)
  1471 
  1631 
  1472 lemma perm_rev[simp]:
  1632 lemma perm_rev[simp]:
  1473 fixes c::"nat" and pi::"prm"
  1633   fixes c::"nat" and pi::"prm"
  1474 shows "pi\<bullet>((rev pi)\<bullet>c) = c"
  1634   shows "pi\<bullet>((rev pi)\<bullet>c) = c"
  1475 by (induct pi arbitrary: c) (auto)
  1635 by (induct pi arbitrary: c) (auto)
  1476 
  1636 
  1477 lemma perm_compose:
  1637 lemma perm_compose:
  1478 fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1638 fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1479 shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c) = (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>(pi\<^isub>1\<bullet>c)" 
  1639 shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c) = (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>(pi\<^isub>1\<bullet>c)" 
  1504   this rule and so one ends up with clunky proofs like:
  1664   this rule and so one ends up with clunky proofs like:
  1505 *}
  1665 *}
  1506 
  1666 
  1507 lemma 
  1667 lemma 
  1508 fixes c d::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1668 fixes c d::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1509 shows "pi\<^isub>1\<bullet>(c, pi\<^isub>2\<bullet>((rev pi\<^isub>1)\<bullet>d)) = (pi\<^isub>1\<bullet>c, (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>d)"
  1669   shows "pi\<^isub>1\<bullet>(c, pi\<^isub>2\<bullet>((rev pi\<^isub>1)\<bullet>d)) = (pi\<^isub>1\<bullet>c, (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>d)"
  1510 apply(simp)
  1670 apply(simp)
  1511 apply(rule trans)
  1671 apply(rule trans)
  1512 apply(rule perm_compose)
  1672 apply(rule perm_compose)
  1513 apply(simp)
  1673 apply(simp)
  1514 done 
  1674 done 
  1526   "pi \<bullet>aux c \<equiv> pi \<bullet> c"
  1686   "pi \<bullet>aux c \<equiv> pi \<bullet> c"
  1527 
  1687 
  1528 text {* Now the two lemmas *}
  1688 text {* Now the two lemmas *}
  1529 
  1689 
  1530 lemma perm_aux_expand:
  1690 lemma perm_aux_expand:
  1531 fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1691   fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1532 shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c) = pi\<^isub>1 \<bullet>aux (pi\<^isub>2\<bullet>c)" 
  1692   shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c) = pi\<^isub>1 \<bullet>aux (pi\<^isub>2\<bullet>c)" 
  1533 unfolding perm_aux_def by (rule refl)
  1693 unfolding perm_aux_def by (rule refl)
  1534 
  1694 
  1535 lemma perm_compose_aux:
  1695 lemma perm_compose_aux:
  1536 fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1696   fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1537 shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>aux c) = (pi\<^isub>1\<bullet>pi\<^isub>2) \<bullet>aux (pi\<^isub>1\<bullet>c)" 
  1697   shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>aux c) = (pi\<^isub>1\<bullet>pi\<^isub>2) \<bullet>aux (pi\<^isub>1\<bullet>c)" 
  1538 unfolding perm_aux_def by (rule perm_compose)
  1698 unfolding perm_aux_def by (rule perm_compose)
  1539 
  1699 
  1540 text {* 
  1700 text {* 
  1541   are simple consequence of the definition and @{thm [source] perm_compose}. 
  1701   are simple consequence of the definition and @{thm [source] perm_compose}. 
  1542   More importantly, the lemma @{thm [source] perm_compose_aux} can be safely 
  1702   More importantly, the lemma @{thm [source] perm_compose_aux} can be safely 
  1570   for our purposes here, we can add these lemmas to @{ML HOL_basic_ss} in order to obtain
  1730   for our purposes here, we can add these lemmas to @{ML HOL_basic_ss} in order to obtain
  1571   the desired results. Now we can solve the following lemma
  1731   the desired results. Now we can solve the following lemma
  1572 *}
  1732 *}
  1573 
  1733 
  1574 lemma 
  1734 lemma 
  1575 fixes c d::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1735   fixes c d::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1576 shows "pi\<^isub>1\<bullet>(c, pi\<^isub>2\<bullet>((rev pi\<^isub>1)\<bullet>d)) = (pi\<^isub>1\<bullet>c, (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>d)"
  1736   shows "pi\<^isub>1\<bullet>(c, pi\<^isub>2\<bullet>((rev pi\<^isub>1)\<bullet>d)) = (pi\<^isub>1\<bullet>c, (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>d)"
  1577 apply(tactic {* perm_simp_tac 1 *})
  1737 apply(tactic {* perm_simp_tac 1 *})
  1578 done
  1738 done
  1579 
  1739 
  1580 
  1740 
  1581 text {*
  1741 text {*
  1638   an argument about morphisms. 
  1798   an argument about morphisms. 
  1639   After this, the simplifier is aware of the simproc and you can test whether 
  1799   After this, the simplifier is aware of the simproc and you can test whether 
  1640   it fires on the lemma:
  1800   it fires on the lemma:
  1641 *}
  1801 *}
  1642 
  1802 
  1643 lemma shows "Suc 0 = 1"
  1803 lemma 
       
  1804   shows "Suc 0 = 1"
  1644 apply(simp)
  1805 apply(simp)
  1645 (*<*)oops(*>*)
  1806 (*<*)oops(*>*)
  1646 
  1807 
  1647 text {*
  1808 text {*
  1648   \begin{isabelle}
  1809   \begin{isabelle}
  1666   If you want to see what happens with just \emph{this} simproc, without any 
  1827   If you want to see what happens with just \emph{this} simproc, without any 
  1667   interference from other rewrite rules, you can call @{text fail} 
  1828   interference from other rewrite rules, you can call @{text fail} 
  1668   as follows:
  1829   as follows:
  1669 *}
  1830 *}
  1670 
  1831 
  1671 lemma shows "Suc 0 = 1"
  1832 lemma 
       
  1833   shows "Suc 0 = 1"
  1672 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [@{simproc fail}]) 1*})
  1834 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [@{simproc fail}]) 1*})
  1673 (*<*)oops(*>*)
  1835 (*<*)oops(*>*)
  1674 
  1836 
  1675 text {*
  1837 text {*
  1676   Now the message shows up only once since the term @{term "1::nat"} is 
  1838   Now the message shows up only once since the term @{term "1::nat"} is 
  1716 
  1878 
  1717   Simprocs are applied from inside to outside and from left to right. You can
  1879   Simprocs are applied from inside to outside and from left to right. You can
  1718   see this in the proof
  1880   see this in the proof
  1719 *}
  1881 *}
  1720 
  1882 
  1721 lemma shows "Suc (Suc 0) = (Suc 1)"
  1883 lemma 
       
  1884   shows "Suc (Suc 0) = (Suc 1)"
  1722 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [fail']) 1*})
  1885 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [fail']) 1*})
  1723 (*<*)oops(*>*)
  1886 (*<*)oops(*>*)
  1724 
  1887 
  1725 text {*
  1888 text {*
  1726   The simproc @{ML fail'} prints out the sequence 
  1889   The simproc @{ML fail'} prints out the sequence 
  1768   of the form @{term "Suc n"}, but inside the simproc we only produce
  1931   of the form @{term "Suc n"}, but inside the simproc we only produce
  1769   a theorem if the term is not @{term "Suc 0"}. The result you can see
  1932   a theorem if the term is not @{term "Suc 0"}. The result you can see
  1770   in the following proof
  1933   in the following proof
  1771 *}
  1934 *}
  1772 
  1935 
  1773 lemma shows "P (Suc (Suc (Suc 0))) (Suc 0)"
  1936 lemma 
       
  1937   shows "P (Suc (Suc (Suc 0))) (Suc 0)"
  1774 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [plus_one]) 1*})
  1938 apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [plus_one]) 1*})
  1775 txt{*
  1939 txt{*
  1776   where the simproc produces the goal state
  1940   where the simproc produces the goal state
  1777   
  1941   
  1778   \begin{minipage}{\textwidth}
  1942   \begin{minipage}{\textwidth}
  1873 
  2037 
  1874 text {* 
  2038 text {* 
  1875   Now in the lemma
  2039   Now in the lemma
  1876   *}
  2040   *}
  1877 
  2041 
  1878 lemma "P (Suc (Suc 2)) (Suc 99) (0::nat) (Suc 4 + Suc 0) (Suc (0 + 0))"
  2042 lemma 
       
  2043   shows "P (Suc (Suc 2)) (Suc 99) (0::nat) (Suc 4 + Suc 0) (Suc (0 + 0))"
  1879 apply(tactic {* simp_tac (HOL_ss addsimprocs [nat_number]) 1*})
  2044 apply(tactic {* simp_tac (HOL_ss addsimprocs [nat_number]) 1*})
  1880 txt {* 
  2045 txt {* 
  1881   you obtain the more legible goal state
  2046   you obtain the more legible goal state
  1882   
  2047   
  1883   \begin{minipage}{\textwidth}
  2048   \begin{minipage}{\textwidth}
  1975   @{ML_ind  rewr_conv in Conv}, which expects a meta-equation as an 
  2140   @{ML_ind  rewr_conv in Conv}, which expects a meta-equation as an 
  1976   argument. Suppose the following meta-equation.
  2141   argument. Suppose the following meta-equation.
  1977   
  2142   
  1978 *}
  2143 *}
  1979 
  2144 
  1980 lemma true_conj1: "True \<and> P \<equiv> P" by simp
  2145 lemma true_conj1: 
       
  2146   shows "True \<and> P \<equiv> P" by simp
  1981 
  2147 
  1982 text {* 
  2148 text {* 
  1983   It can be used for example to rewrite @{term "True \<and> (Foo \<longrightarrow> Bar)"} 
  2149   It can be used for example to rewrite @{term "True \<and> (Foo \<longrightarrow> Bar)"} 
  1984   to @{term "Foo \<longrightarrow> Bar"}. The code is as follows.
  2150   to @{term "Foo \<longrightarrow> Bar"}. The code is as follows.
  1985 
  2151 
  2156   So far we only applied conversions to @{ML_type cterm}s. Conversions can, however, 
  2322   So far we only applied conversions to @{ML_type cterm}s. Conversions can, however, 
  2157   also work on theorems using the function @{ML_ind  fconv_rule in Conv}. As an example, 
  2323   also work on theorems using the function @{ML_ind  fconv_rule in Conv}. As an example, 
  2158   consider the conversion @{ML all_true1_conv} and the lemma:
  2324   consider the conversion @{ML all_true1_conv} and the lemma:
  2159 *}
  2325 *}
  2160 
  2326 
  2161 lemma foo_test: "P \<or> (True \<and> \<not>P)" by simp
  2327 lemma foo_test: 
       
  2328   shows "P \<or> (True \<and> \<not>P)" by simp
  2162 
  2329 
  2163 text {*
  2330 text {*
  2164   Using the conversion @{ML all_true1_conv} you can transform this theorem into a 
  2331   Using the conversion @{ML all_true1_conv} you can transform this theorem into a 
  2165   new theorem as follows
  2332   new theorem as follows
  2166 
  2333