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