src/Provers/clasimp.ML
author wenzelm
Thu Sep 02 00:48:07 2010 +0200 (2010-09-02)
changeset 38980 af73cf0dc31f
parent 37720 50a9e2fa4f6b
child 42373 fb539d6d1ac2
permissions -rw-r--r--
turned show_question_marks into proper configuration option;
show_question_marks only affects regular type/term pretty printing, not raw Term.string_of_vname;
tuned;
     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
    74     val (cs, ss) = get_css context;
    75     val (cs', ss') = f (cs, Simplifier.context (Context.proof_of context) ss);
    76   in
    77     context
    78     |> Classical.map_cs (K cs')
    79     |> Simplifier.map_ss (K (Simplifier.inherit_context ss ss'))
    80   end;
    81 
    82 fun clasimpset_of ctxt = (Classical.claset_of ctxt, Simplifier.simpset_of ctxt);
    83 
    84 
    85 (* clasimpset operations *)
    86 
    87 (*this interface for updating a clasimpset is rudimentary and just for
    88   convenience for the most common cases*)
    89 
    90 fun pair_upd1 f ((a, b), x) = (f (a, x), b);
    91 fun pair_upd2 f ((a, b), x) = (a, f (b, x));
    92 
    93 fun op addSIs2   arg = pair_upd1 Classical.addSIs arg;
    94 fun op addSEs2   arg = pair_upd1 Classical.addSEs arg;
    95 fun op addSDs2   arg = pair_upd1 Classical.addSDs arg;
    96 fun op addIs2    arg = pair_upd1 Classical.addIs arg;
    97 fun op addEs2    arg = pair_upd1 Classical.addEs arg;
    98 fun op addDs2    arg = pair_upd1 Classical.addDs arg;
    99 fun op addsimps2 arg = pair_upd2 Simplifier.addsimps arg;
   100 fun op delsimps2 arg = pair_upd2 Simplifier.delsimps arg;
   101 
   102 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
   103 val safe_asm_full_simp_tac = Simplifier.generic_simp_tac true (true,true,true);
   104 
   105 (*Add a simpset to a classical set!*)
   106 (*Caution: only one simpset added can be added by each of addSss and addss*)
   107 fun cs addSss ss = Classical.addSafter (cs, ("safe_asm_full_simp_tac",
   108                             CHANGED o safe_asm_full_simp_tac ss));
   109 fun cs addss  ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
   110                             CHANGED o Simplifier.asm_full_simp_tac ss));
   111 fun cs addss' ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
   112                             CHANGED o Simplifier.asm_lr_simp_tac ss));
   113 
   114 (* iffs: addition of rules to simpsets and clasets simultaneously *)
   115 
   116 (*Takes (possibly conditional) theorems of the form A<->B to
   117         the Safe Intr     rule B==>A and
   118         the Safe Destruct rule A==>B.
   119   Also ~A goes to the Safe Elim rule A ==> ?R
   120   Failing other cases, A is added as a Safe Intr rule*)
   121 local
   122 
   123 fun addIff decls1 decls2 simp ((cs, ss), th) =
   124   let
   125     val n = nprems_of th;
   126     val (elim, intro) = if n = 0 then decls1 else decls2;
   127     val zero_rotate = zero_var_indexes o rotate_prems n;
   128   in
   129     (elim (intro (cs, [zero_rotate (th RS iffD2)]),
   130            [Tactic.make_elim (zero_rotate (th RS iffD1))])
   131       handle THM _ => (elim (cs, [zero_rotate (th RS 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 iffD2),
   139         Tactic.make_elim (zero_rotate (th RS iffD1))])
   140       handle THM _ => (delcs (cs, [zero_rotate (th RS notE)])
   141         handle THM _ => delcs (cs, [th])),
   142      delss (ss, [th]))
   143   end;
   144 
   145 fun modifier att (x, ths) =
   146   fst (Library.foldl_map (Library.apply [att]) (x, rev ths));
   147 
   148 val addXIs = modifier (Context_Rules.intro_query NONE);
   149 val addXEs = modifier (Context_Rules.elim_query NONE);
   150 val delXs = modifier Context_Rules.rule_del;
   151 
   152 in
   153 
   154 val op addIffs =
   155   Library.foldl
   156       (addIff (Classical.addSEs, Classical.addSIs)
   157               (Classical.addEs, Classical.addIs)
   158               Simplifier.addsimps);
   159 val op delIffs = Library.foldl (delIff Classical.delrules Simplifier.delsimps);
   160 
   161 fun addIffs_generic (x, ths) =
   162   Library.foldl (addIff (addXEs, addXIs) (addXEs, addXIs) #1) ((x, ()), ths) |> #1;
   163 
   164 fun delIffs_generic (x, ths) =
   165   Library.foldl (delIff delXs #1) ((x, ()), ths) |> #1;
   166 
   167 end;
   168 
   169 
   170 (* tacticals *)
   171 
   172 fun clarsimp_tac (cs, ss) =
   173   safe_asm_full_simp_tac ss THEN_ALL_NEW
   174   Classical.clarify_tac (cs addSss ss);
   175 
   176 
   177 (* auto_tac *)
   178 
   179 fun blast_depth_tac cs m i thm =
   180     Blast.depth_tac cs m i thm
   181       handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
   182 
   183 (* a variant of depth_tac that avoids interference of the simplifier
   184    with dup_step_tac when they are combined by auto_tac *)
   185 local
   186 fun slow_step_tac' cs = Classical.appWrappers cs
   187         (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
   188 in fun nodup_depth_tac cs m i state = SELECT_GOAL
   189    (Classical.safe_steps_tac cs 1 THEN_ELSE
   190         (DEPTH_SOLVE (nodup_depth_tac cs m 1),
   191          Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
   192              (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
   193         )) i state;
   194 end;
   195 
   196 (*Designed to be idempotent, except if blast_depth_tac instantiates variables
   197   in some of the subgoals*)
   198 fun mk_auto_tac (cs, ss) m n =
   199     let val cs' = cs addss ss
   200         val maintac =
   201           blast_depth_tac cs m               (* fast but can't use wrappers *)
   202           ORELSE'
   203           (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
   204     in  EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
   205                TRY (Classical.safe_tac cs),
   206                REPEAT_DETERM (FIRSTGOAL maintac),
   207                TRY (Classical.safe_tac (cs addSss ss)),
   208                prune_params_tac]
   209     end;
   210 
   211 fun auto_tac css = mk_auto_tac css 4 2;
   212 
   213 
   214 (* force_tac *)
   215 
   216 (* aimed to solve the given subgoal totally, using whatever tools possible *)
   217 fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
   218         Classical.clarify_tac cs' 1,
   219         IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
   220         ALLGOALS (Classical.first_best_tac cs')]) end;
   221 
   222 
   223 (* basic combinations *)
   224 
   225 fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
   226 
   227 val fast_simp_tac = ADDSS Classical.fast_tac;
   228 val slow_simp_tac = ADDSS Classical.slow_tac;
   229 val best_simp_tac = ADDSS Classical.best_tac;
   230 
   231 
   232 (* access clasimpset *)
   233 
   234 
   235 
   236 (* attributes *)
   237 
   238 fun attrib f = Thm.declaration_attribute (fn th => map_css (fn css => f (css, [th])));
   239 fun attrib' f (x, th) = (f (x, [th]), th);
   240 
   241 val iff_add = attrib (op addIffs);
   242 val iff_add' = attrib' addIffs_generic;
   243 val iff_del = attrib (op delIffs) o attrib' delIffs_generic;
   244 
   245 fun iff_att x = (Scan.lift
   246  (Args.del >> K iff_del ||
   247   Scan.option Args.add -- Args.query >> K iff_add' ||
   248   Scan.option Args.add >> K iff_add)) x;
   249 
   250 
   251 (* method modifiers *)
   252 
   253 val iffN = "iff";
   254 
   255 val iff_modifiers =
   256  [Args.$$$ iffN -- Scan.option Args.add -- Args.colon >> K ((I, iff_add): Method.modifier),
   257   Args.$$$ iffN -- Scan.option Args.add -- Args.query_colon >> K (I, iff_add'),
   258   Args.$$$ iffN -- Args.del -- Args.colon >> K (I, iff_del)];
   259 
   260 val clasimp_modifiers =
   261   Simplifier.simp_modifiers @ Splitter.split_modifiers @
   262   Classical.cla_modifiers @ iff_modifiers;
   263 
   264 
   265 (* methods *)
   266 
   267 fun clasimp_meth tac ctxt = METHOD (fn facts =>
   268   ALLGOALS (Method.insert_tac facts) THEN tac (clasimpset_of ctxt));
   269 
   270 fun clasimp_meth' tac ctxt = METHOD (fn facts =>
   271   HEADGOAL (Method.insert_tac facts THEN' tac (clasimpset_of ctxt)));
   272 
   273 
   274 fun clasimp_method' tac =
   275   Method.sections clasimp_modifiers >> K (clasimp_meth' tac);
   276 
   277 val auto_method =
   278   Scan.lift (Scan.option (Parse.nat -- Parse.nat)) --|
   279     Method.sections clasimp_modifiers >>
   280   (fn NONE => clasimp_meth (CHANGED_PROP o auto_tac)
   281     | SOME (m, n) => clasimp_meth (CHANGED_PROP o (fn css => mk_auto_tac css m n)));
   282 
   283 
   284 (* theory setup *)
   285 
   286 val clasimp_setup =
   287   Attrib.setup @{binding iff} iff_att "declaration of Simplifier / Classical rules" #>
   288   Method.setup @{binding fastsimp} (clasimp_method' fast_simp_tac) "combined fast and simp" #>
   289   Method.setup @{binding slowsimp} (clasimp_method' slow_simp_tac) "combined slow and simp" #>
   290   Method.setup @{binding bestsimp} (clasimp_method' best_simp_tac) "combined best and simp" #>
   291   Method.setup @{binding force} (clasimp_method' force_tac) "force" #>
   292   Method.setup @{binding auto} auto_method "auto" #>
   293   Method.setup @{binding clarsimp} (clasimp_method' (CHANGED_PROP oo clarsimp_tac))
   294     "clarify simplified goal";
   295 
   296 end;