CookBook/Tactical.thy
changeset 173 d820cb5873ea
parent 172 ec47352e99c2
child 174 a29b81d4fa88
equal deleted inserted replaced
172:ec47352e99c2 173:d820cb5873ea
   163 
   163 
   164 ML{*fun no_tac thm = Seq.empty*}
   164 ML{*fun no_tac thm = Seq.empty*}
   165 
   165 
   166 text {*
   166 text {*
   167   which means @{ML no_tac} always fails. The second returns the given theorem wrapped 
   167   which means @{ML no_tac} always fails. The second returns the given theorem wrapped 
   168   up in a single member sequence; it is defined as
   168   in a single member sequence; it is defined as
   169 *}
   169 *}
   170 
   170 
   171 ML{*fun all_tac thm = Seq.single thm*}
   171 ML{*fun all_tac thm = Seq.single thm*}
   172 
   172 
   173 text {*
   173 text {*
   214   Seq.single thm
   214   Seq.single thm
   215 end*}
   215 end*}
   216 
   216 
   217 text_raw {*
   217 text_raw {*
   218   \begin{figure}[p]
   218   \begin{figure}[p]
       
   219   \begin{boxedminipage}{\textwidth}
   219   \begin{isabelle}
   220   \begin{isabelle}
   220 *}
   221 *}
   221 lemma shows "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B" 
   222 lemma shows "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B" 
   222 apply(tactic {* my_print_tac @{context} *})
   223 apply(tactic {* my_print_tac @{context} *})
   223 
   224 
   282       \end{minipage}\medskip   
   283       \end{minipage}\medskip   
   283    *}
   284    *}
   284 done
   285 done
   285 text_raw {*  
   286 text_raw {*  
   286   \end{isabelle}
   287   \end{isabelle}
       
   288   \end{boxedminipage}
   287   \caption{The figure shows a proof where each intermediate goal state is
   289   \caption{The figure shows a proof where each intermediate goal state is
   288   printed by the Isabelle system and by @{ML my_print_tac}. The latter shows
   290   printed by the Isabelle system and by @{ML my_print_tac}. The latter shows
   289   the goal state as represented internally (highlighted boxes). This
   291   the goal state as represented internally (highlighted boxes). This
   290   illustrates that every goal state in Isabelle is represented by a theorem:
   292   tactic shows that every goal state in Isabelle is represented by a theorem:
   291   when you start the proof of \mbox{@{text "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B"}} the theorem is
   293   when you start the proof of \mbox{@{text "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B"}} the theorem is
   292   @{text "(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B) \<Longrightarrow> (\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B)"}; when you finish the proof the
   294   @{text "(\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B) \<Longrightarrow> (\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B)"}; when you finish the proof the
   293   theorem is @{text "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B"}.\label{fig:goalstates}}
   295   theorem is @{text "\<lbrakk>A; B\<rbrakk> \<Longrightarrow> A \<and> B"}.\label{fig:goalstates}}
   294   \end{figure}
   296   \end{figure}
   295 *}
   297 *}
   324 *}
   326 *}
   325 
   327 
   326 section {* Simple Tactics *}
   328 section {* Simple Tactics *}
   327 
   329 
   328 text {*
   330 text {*
   329   Let us start with the tactic @{ML print_tac}, which is quite useful for low-level 
   331   Let us start with explaining the simple tactic @{ML print_tac}, which is quite useful 
   330   debugging of tactics. It just prints out a message and the current goal state 
   332   for low-level debugging of tactics. It just prints out a message and the current 
   331   (unlike @{ML my_print_tac}, it prints the goal state as the user would see it). 
   333   goal state. Unlike @{ML my_print_tac} shown earlier, it prints the goal state 
   332   For example, processing the proof
   334   as the user would see it.  For example, processing the proof
   333 *}
   335 *}
   334  
   336  
   335 lemma shows "False \<Longrightarrow> True"
   337 lemma shows "False \<Longrightarrow> True"
   336 apply(tactic {* print_tac "foo message" *})
   338 apply(tactic {* print_tac "foo message" *})
   337 txt{*gives:\medskip
   339 txt{*gives:\medskip
   384      \end{minipage}*}
   386      \end{minipage}*}
   385 (*<*)oops(*>*)
   387 (*<*)oops(*>*)
   386 
   388 
   387 text {*
   389 text {*
   388   Note the number in each tactic call. Also as mentioned in the previous section, most 
   390   Note the number in each tactic call. Also as mentioned in the previous section, most 
   389   basic tactics take such a number as argument; it addresses the subgoal they are analysing. 
   391   basic tactics take such a number as argument: this argument addresses the subgoal 
   390   In the proof below, we first split up the conjunction in  the second subgoal by 
   392   the tacics are analysing. In the proof below, we first split up the conjunction in  
   391   focusing on this subgoal first.
   393   the second subgoal by focusing on this subgoal first.
   392 *}
   394 *}
   393 
   395 
   394 lemma shows "Foo" and "P \<and> Q"
   396 lemma shows "Foo" and "P \<and> Q"
   395 apply(tactic {* rtac @{thm conjI} 2 *})
   397 apply(tactic {* rtac @{thm conjI} 2 *})
   396 txt {*\begin{minipage}{\textwidth}
   398 txt {*\begin{minipage}{\textwidth}
   419      @{subgoals [display]} 
   421      @{subgoals [display]} 
   420      \end{minipage}*}
   422      \end{minipage}*}
   421 (*<*)oops(*>*)
   423 (*<*)oops(*>*)
   422 
   424 
   423 text {* 
   425 text {* 
   424   Similarly versions taking a list of theorems exist for the tactics 
   426   Similarl versions taking a list of theorems exist for the tactics 
   425   @{ML dtac} (@{ML dresolve_tac}), @{ML etac} (@{ML eresolve_tac}) and so on.
   427   @{ML dtac} (@{ML dresolve_tac}), @{ML etac} (@{ML eresolve_tac}) and so on.
   426 
   428 
   427 
   429 
   428   Another simple tactic is @{ML cut_facts_tac}. It inserts a list of theorems
   430   Another simple tactic is @{ML cut_facts_tac}. It inserts a list of theorems
   429   into the assumptions of the current goal state. For example
   431   into the assumptions of the current goal state. For example
   519   string transformation functions from in Section~\ref{sec:printing}). Consider
   521   string transformation functions from in Section~\ref{sec:printing}). Consider
   520   now the proof:
   522   now the proof:
   521 *}
   523 *}
   522 
   524 
   523 text_raw{*
   525 text_raw{*
   524 \begin{figure}
   526 \begin{figure}[t]
   525 \begin{isabelle}
   527 \begin{isabelle}
   526 *}
   528 *}
   527 ML{*fun sp_tac {prems, params, asms, concl, context, schematics} = 
   529 ML{*fun sp_tac {prems, params, asms, concl, context, schematics} = 
   528 let 
   530 let 
   529   val str_of_params = str_of_cterms context params
   531   val str_of_params = str_of_cterms context params
  1115 
  1117 
  1116   (FIXME: What about splitters? @{ML addsplits}, @{ML delsplits})
  1118   (FIXME: What about splitters? @{ML addsplits}, @{ML delsplits})
  1117 *}
  1119 *}
  1118 
  1120 
  1119 text_raw {*
  1121 text_raw {*
  1120 \begin{figure}[tp]
  1122 \begin{figure}[t]
  1121 \begin{isabelle}*}
  1123 \begin{isabelle}*}
  1122 ML{*fun print_ss ctxt ss =
  1124 ML{*fun print_ss ctxt ss =
  1123 let
  1125 let
  1124   val {simps, congs, procs, ...} = MetaSimplifier.dest_ss ss
  1126   val {simps, congs, procs, ...} = MetaSimplifier.dest_ss ss
  1125 
  1127 
  1263   As you can see, the tactic unfolds the definitions in all subgoals.
  1265   As you can see, the tactic unfolds the definitions in all subgoals.
  1264 *}
  1266 *}
  1265 
  1267 
  1266 
  1268 
  1267 text_raw {*
  1269 text_raw {*
  1268 \begin{figure}[tp]
  1270 \begin{figure}[p]
       
  1271 \begin{boxedminipage}{\textwidth}
  1269 \begin{isabelle} *}
  1272 \begin{isabelle} *}
  1270 types  prm = "(nat \<times> nat) list"
  1273 types  prm = "(nat \<times> nat) list"
  1271 consts perm :: "prm \<Rightarrow> 'a \<Rightarrow> 'a"  ("_ \<bullet> _" [80,80] 80)
  1274 consts perm :: "prm \<Rightarrow> 'a \<Rightarrow> 'a"  ("_ \<bullet> _" [80,80] 80)
  1272 
  1275 
  1273 primrec (perm_nat)
  1276 primrec (perm_nat)
  1300 lemma perm_compose:
  1303 lemma perm_compose:
  1301   fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1304   fixes c::"nat" and pi\<^isub>1 pi\<^isub>2::"prm"
  1302   shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c) = (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>(pi\<^isub>1\<bullet>c)" 
  1305   shows "pi\<^isub>1\<bullet>(pi\<^isub>2\<bullet>c) = (pi\<^isub>1\<bullet>pi\<^isub>2)\<bullet>(pi\<^isub>1\<bullet>c)" 
  1303 by (induct pi\<^isub>2) (auto)
  1306 by (induct pi\<^isub>2) (auto)
  1304 text_raw {*
  1307 text_raw {*
  1305 \end{isabelle}\medskip
  1308 \end{isabelle}
       
  1309 \end{boxedminipage}
  1306 \caption{A simple theory about permutations over @{typ nat}. The point is that the
  1310 \caption{A simple theory about permutations over @{typ nat}. The point is that the
  1307   lemma @{thm [source] perm_compose} cannot be directly added to the simplifier, as
  1311   lemma @{thm [source] perm_compose} cannot be directly added to the simplifier, as
  1308   it would cause the simplifier to loop. It can still be used as a simplification 
  1312   it would cause the simplifier to loop. It can still be used as a simplification 
  1309   rule if the permutation is sufficiently protected.\label{fig:perms}
  1313   rule if the permutation is sufficiently protected.\label{fig:perms}
  1310   (FIXME: Uses old primrec.)}
  1314   (FIXME: Uses old primrec.)}