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