src/Provers/clasimp.ML
author nipkow
Fri May 17 11:25:07 2002 +0200 (2002-05-17 ago)
changeset 13157 4a4599f78f18
parent 13026 e45ebbb2e18e
child 13603 57f364d1d3b2
permissions -rw-r--r--
allowed more general split rules to cope with div/mod 2
     1 (*  Title:      Provers/clasimp.ML
     2     ID:         $Id$
     3     Author:     David von Oheimb, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Combination of classical reasoner and simplifier (depends on
     7 simplifier.ML, splitter.ML classical.ML, blast.ML).
     8 *)
     9 
    10 infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2 addsimps2 delsimps2
    11   addSss addss addIffs delIffs;
    12 
    13 signature CLASIMP_DATA =
    14 sig
    15   structure Simplifier: SIMPLIFIER
    16   structure Splitter: SPLITTER
    17   structure Classical: CLASSICAL
    18   structure Blast: BLAST
    19   sharing type Classical.claset = Blast.claset
    20   val notE: thm
    21   val iffD1: thm
    22   val iffD2: thm
    23   val cla_make_elim: thm -> thm
    24 end
    25 
    26 signature CLASIMP =
    27 sig
    28   include CLASIMP_DATA
    29   type claset
    30   type simpset
    31   type clasimpset
    32   val clasimpset: unit -> clasimpset
    33   val addSIs2: clasimpset * thm list -> clasimpset
    34   val addSEs2: clasimpset * thm list -> clasimpset
    35   val addSDs2: clasimpset * thm list -> clasimpset
    36   val addIs2: clasimpset * thm list -> clasimpset
    37   val addEs2: clasimpset * thm list -> clasimpset
    38   val addDs2: clasimpset * thm list -> clasimpset
    39   val addsimps2: clasimpset * thm list -> clasimpset
    40   val delsimps2: clasimpset * thm list -> clasimpset
    41   val addSss: claset * simpset -> claset
    42   val addss: claset * simpset -> claset
    43   val addIffs: clasimpset * thm list -> clasimpset
    44   val delIffs: clasimpset * thm list -> clasimpset
    45   val AddIffs: thm list -> unit
    46   val DelIffs: thm list -> unit
    47   val CLASIMPSET: (clasimpset -> tactic) -> tactic
    48   val CLASIMPSET': (clasimpset -> 'a -> tactic) -> 'a -> tactic
    49   val clarsimp_tac: clasimpset -> int -> tactic
    50   val Clarsimp_tac: int -> tactic
    51   val mk_auto_tac: clasimpset -> int -> int -> tactic
    52   val auto_tac: clasimpset -> tactic
    53   val Auto_tac: tactic
    54   val auto: unit -> unit
    55   val force_tac: clasimpset  -> int -> tactic
    56   val Force_tac: int -> tactic
    57   val force: int -> unit
    58   val fast_simp_tac: clasimpset -> int -> tactic
    59   val slow_simp_tac: clasimpset -> int -> tactic
    60   val best_simp_tac: clasimpset -> int -> tactic
    61   val change_global_css: (clasimpset * thm list -> clasimpset) -> theory attribute
    62   val change_local_css: (clasimpset * thm list -> clasimpset) -> Proof.context attribute
    63   val get_local_clasimpset: Proof.context -> clasimpset
    64   val iff_add_global: theory attribute
    65   val iff_add_global': theory attribute
    66   val iff_del_global: theory attribute
    67   val iff_add_local: Proof.context attribute
    68   val iff_add_local': Proof.context attribute
    69   val iff_del_local: Proof.context attribute
    70   val iff_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    71   val clasimp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    72   val setup: (theory -> theory) list
    73 end;
    74 
    75 functor ClasimpFun(Data: CLASIMP_DATA): CLASIMP =
    76 struct
    77 
    78 open Data;
    79 
    80 type claset = Classical.claset;
    81 type simpset = Simplifier.simpset;
    82 type clasimpset = claset * simpset;
    83 
    84 fun clasimpset () = (Classical.claset (), Simplifier.simpset ());
    85 
    86 
    87 (* clasimpset operations *)
    88 
    89 (*this interface for updating a clasimpset is rudimentary and just for
    90   convenience for the most common cases*)
    91 
    92 fun pair_upd1 f ((a,b),x) = (f(a,x), b);
    93 fun pair_upd2 f ((a,b),x) = (a, f(b,x));
    94 
    95 fun op addSIs2   arg = pair_upd1 Classical.addSIs arg;
    96 fun op addSEs2   arg = pair_upd1 Classical.addSEs arg;
    97 fun op addSDs2   arg = pair_upd1 Classical.addSDs arg;
    98 fun op addIs2    arg = pair_upd1 Classical.addIs arg;
    99 fun op addEs2    arg = pair_upd1 Classical.addEs arg;
   100 fun op addDs2    arg = pair_upd1 Classical.addDs arg;
   101 fun op addsimps2 arg = pair_upd2 Simplifier.addsimps arg;
   102 fun op delsimps2 arg = pair_upd2 Simplifier.delsimps arg;
   103 
   104 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
   105 val safe_asm_full_simp_tac = Simplifier.generic_simp_tac true (true,true,true);
   106 
   107 (*Add a simpset to a classical set!*)
   108 (*Caution: only one simpset added can be added by each of addSss and addss*)
   109 fun cs addSss ss = Classical.addSafter (cs, ("safe_asm_full_simp_tac",
   110                             CHANGED o safe_asm_full_simp_tac ss));
   111 fun cs addss  ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
   112                             CHANGED o Simplifier.asm_full_simp_tac ss));
   113 
   114 
   115 (* iffs: addition of rules to simpsets and clasets simultaneously *)
   116 
   117 (*Takes (possibly conditional) theorems of the form A<->B to
   118         the Safe Intr     rule B==>A and
   119         the Safe Destruct rule A==>B.
   120   Also ~A goes to the Safe Elim rule A ==> ?R
   121   Failing other cases, A is added as a Safe Intr rule*)
   122 local
   123 
   124 fun addIff decls1 decls2 simp ((cs, ss), th) =
   125   let
   126     val n = nprems_of th;
   127     val (dest, elim, intro) = if n = 0 then decls1 else decls2;
   128     val zero_rotate = zero_var_indexes o rotate_prems n;
   129   in
   130     (dest (intro (cs, [zero_rotate (th RS Data.iffD2)]), [zero_rotate (th RS Data.iffD1)])
   131       handle THM _ => (elim (cs, [zero_rotate (th RS Data.notE)])
   132         handle THM _ => intro (cs, [th])),
   133      simp (ss, [th]))
   134   end;
   135 
   136 fun delIff delcs delss ((cs, ss), th) =
   137   let val zero_rotate = zero_var_indexes o rotate_prems (nprems_of th) in
   138     (delcs (cs, [zero_rotate (th RS Data.iffD2),
   139         Data.cla_make_elim (zero_rotate (th RS Data.iffD1))])
   140       handle THM _ => (delcs (cs, [zero_rotate (th RS Data.notE)])
   141         handle THM _ => delcs (cs, [th])),
   142      delss (ss, [th]))
   143   end;
   144 
   145 fun store_clasimp (cs, ss) =
   146   (Classical.claset_ref () := cs; Simplifier.simpset_ref () := ss);
   147 
   148 in
   149 
   150 val op addIffs =
   151   foldl (addIff (Classical.addSDs, Classical.addSEs, Classical.addSIs)
   152     (Classical.addDs, Classical.addEs, Classical.addIs) Simplifier.addsimps);
   153 val op delIffs = foldl (delIff Classical.delrules Simplifier.delsimps);
   154 
   155 fun AddIffs thms = store_clasimp (clasimpset () addIffs thms);
   156 fun DelIffs thms = store_clasimp (clasimpset () delIffs thms);
   157 
   158 fun addIffs_global (thy, ths) =
   159   foldl (addIff
   160     (ContextRules.addXDs_global, ContextRules.addXEs_global, ContextRules.addXIs_global)
   161     (ContextRules.addXDs_global, ContextRules.addXEs_global, ContextRules.addXIs_global) #1)
   162   ((thy, ()), ths) |> #1;
   163 
   164 fun addIffs_local (ctxt, ths) =
   165   foldl (addIff
   166     (ContextRules.addXDs_local, ContextRules.addXEs_local, ContextRules.addXIs_local)
   167     (ContextRules.addXDs_local, ContextRules.addXEs_local, ContextRules.addXIs_local) #1)
   168   ((ctxt, ()), ths) |> #1;
   169 
   170 fun delIffs_global (thy, ths) =
   171   foldl (delIff ContextRules.delrules_global #1) ((thy, ()), ths) |> #1;
   172 
   173 fun delIffs_local (ctxt, ths) =
   174   foldl (delIff ContextRules.delrules_local #1) ((ctxt, ()), ths) |> #1;
   175 
   176 end;
   177 
   178 
   179 (* tacticals *)
   180 
   181 fun CLASIMPSET tacf state =
   182   Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss))) state;
   183 
   184 fun CLASIMPSET' tacf i state =
   185   Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss) i)) state;
   186 
   187 
   188 fun clarsimp_tac (cs, ss) =
   189   safe_asm_full_simp_tac ss THEN_ALL_NEW
   190   Classical.clarify_tac (cs addSss ss);
   191 
   192 fun Clarsimp_tac i = clarsimp_tac (clasimpset ()) i;
   193 
   194 
   195 (* auto_tac *)
   196 
   197 fun blast_depth_tac cs m i thm =
   198     Blast.depth_tac cs m i thm
   199       handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
   200 
   201 (* a variant of depth_tac that avoids interference of the simplifier
   202    with dup_step_tac when they are combined by auto_tac *)
   203 local
   204 fun slow_step_tac' cs = Classical.appWrappers cs
   205         (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
   206 in fun nodup_depth_tac cs m i state = SELECT_GOAL
   207    (Classical.safe_steps_tac cs 1 THEN_ELSE
   208         (DEPTH_SOLVE (nodup_depth_tac cs m 1),
   209          Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
   210              (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
   211         )) i state;
   212 end;
   213 
   214 (*Designed to be idempotent, except if blast_depth_tac instantiates variables
   215   in some of the subgoals*)
   216 fun mk_auto_tac (cs, ss) m n =
   217     let val cs' = cs addss ss
   218         val maintac =
   219           blast_depth_tac cs m               (* fast but can't use wrappers *)
   220           ORELSE'
   221           (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
   222     in  EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
   223                TRY (Classical.safe_tac cs),
   224                REPEAT (FIRSTGOAL maintac),
   225                TRY (Classical.safe_tac (cs addSss ss)),
   226                prune_params_tac]
   227     end;
   228 
   229 fun auto_tac css = mk_auto_tac css 4 2;
   230 fun Auto_tac st = auto_tac (clasimpset ()) st;
   231 fun auto () = by Auto_tac;
   232 
   233 
   234 (* force_tac *)
   235 
   236 (* aimed to solve the given subgoal totally, using whatever tools possible *)
   237 fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
   238         Classical.clarify_tac cs' 1,
   239         IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
   240         ALLGOALS (Classical.first_best_tac cs')]) end;
   241 fun Force_tac i = force_tac (clasimpset ()) i;
   242 fun force i = by (Force_tac i);
   243 
   244 
   245 (* basic combinations *)
   246 
   247 fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
   248 
   249 val fast_simp_tac = ADDSS Classical.fast_tac;
   250 val slow_simp_tac = ADDSS Classical.slow_tac;
   251 val best_simp_tac = ADDSS Classical.best_tac;
   252 
   253 
   254 (* access clasimpset *)
   255 
   256 fun change_global_css f (thy, th) =
   257   let
   258     val cs_ref = Classical.claset_ref_of thy;
   259     val ss_ref = Simplifier.simpset_ref_of thy;
   260     val (cs', ss') = f ((! cs_ref, ! ss_ref), [th]);
   261   in cs_ref := cs'; ss_ref := ss'; (thy, th) end;
   262 
   263 fun change_local_css f (ctxt, th) =
   264   let
   265     val cs = Classical.get_local_claset ctxt;
   266     val ss = Simplifier.get_local_simpset ctxt;
   267     val (cs', ss') = f ((cs, ss), [th]);
   268     val ctxt' =
   269       ctxt
   270       |> Classical.put_local_claset cs'
   271       |> Simplifier.put_local_simpset ss';
   272   in (ctxt', th) end;
   273 
   274 fun get_local_clasimpset ctxt =
   275   (Classical.get_local_claset ctxt, Simplifier.get_local_simpset ctxt);
   276 
   277 
   278 (* attributes *)
   279 
   280 fun change_rules f (x, th) = (f (x, [th]), th);
   281 
   282 val iff_add_global = change_global_css (op addIffs);
   283 val iff_add_global' = change_rules addIffs_global;
   284 val iff_del_global = change_global_css (op delIffs) o change_rules delIffs_global;
   285 val iff_add_local = change_local_css (op addIffs);
   286 val iff_add_local' = change_rules addIffs_local;
   287 val iff_del_local = change_local_css (op delIffs) o change_rules delIffs_local;
   288 
   289 fun iff_att add add' del = Attrib.syntax (Scan.lift
   290  (Args.del >> K del ||
   291   Scan.option Args.add -- Args.query >> K add' ||
   292   Scan.option Args.add >> K add));
   293 
   294 val iff_attr =
   295  (iff_att iff_add_global iff_add_global' iff_del_global,
   296   iff_att iff_add_local iff_add_local' iff_del_local);
   297 
   298 
   299 (* method modifiers *)
   300 
   301 val iffN = "iff";
   302 
   303 val iff_modifiers =
   304  [Args.$$$ iffN -- Scan.option Args.add -- Args.colon >> K ((I, iff_add_local): Method.modifier),
   305   Args.$$$ iffN -- Scan.option Args.add -- Args.query_colon >> K (I, iff_add_local'),
   306   Args.$$$ iffN -- Args.del -- Args.colon >> K (I, iff_del_local)];
   307 
   308 val clasimp_modifiers =
   309   Simplifier.simp_modifiers @ Splitter.split_modifiers @
   310   Classical.cla_modifiers @ iff_modifiers;
   311 
   312 
   313 (* methods *)
   314 
   315 fun clasimp_meth tac prems ctxt = Method.METHOD (fn facts =>
   316   ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (get_local_clasimpset ctxt));
   317 
   318 fun clasimp_meth' tac prems ctxt = Method.METHOD (fn facts =>
   319   HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (get_local_clasimpset ctxt)));
   320 
   321 val clasimp_method = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth;
   322 val clasimp_method' = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth';
   323 
   324 
   325 fun auto_args m =
   326   Method.bang_sectioned_args' clasimp_modifiers (Scan.lift (Scan.option (Args.nat -- Args.nat))) m;
   327 
   328 fun auto_meth None = clasimp_meth (CHANGED_PROP o auto_tac)
   329   | auto_meth (Some (m, n)) = clasimp_meth (CHANGED_PROP o (fn css => mk_auto_tac css m n));
   330 
   331 
   332 (* theory setup *)
   333 
   334 val setup =
   335  [Attrib.add_attributes
   336   [("iff", iff_attr, "declaration of Simplifier / Classical rules")],
   337   Method.add_methods
   338    [("fastsimp", clasimp_method' fast_simp_tac, "combined fast and simp"),
   339     ("slowsimp", clasimp_method' slow_simp_tac, "combined slow and simp"),
   340     ("bestsimp", clasimp_method' best_simp_tac, "combined best and simp"),
   341     ("force", clasimp_method' force_tac, "force"),
   342     ("auto", auto_args auto_meth, "auto"),
   343     ("clarsimp", clasimp_method' (CHANGED_PROP oo clarsimp_tac), "clarify simplified goal")]];
   344 
   345 end;