src/Provers/clasimp.ML
author wenzelm
Sat, 02 Sep 2000 21:51:14 +0200
changeset 9805 10b617bdd028
parent 9772 c07777210a69
child 9860 5c5efed691b9
permissions -rw-r--r--
added "slowsimp", "bestsimp";
     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 simplifier.ML, splitter.ML classical.ML, blast.ML).
     7 *)
     8 
     9 infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2 addsimps2 delsimps2;
    10 infix 4 addSss addss;
    11 
    12 signature CLASIMP_DATA =
    13 sig
    14   structure Simplifier: SIMPLIFIER
    15   structure Splitter: SPLITTER
    16   structure Classical: CLASSICAL
    17   structure Blast: BLAST
    18   sharing type Classical.claset = Blast.claset
    19 end
    20 
    21 signature CLASIMP =
    22 sig
    23   include CLASIMP_DATA
    24   type claset
    25   type simpset
    26   type clasimpset
    27   val addSIs2   : clasimpset * thm list -> clasimpset
    28   val addSEs2   : clasimpset * thm list -> clasimpset
    29   val addSDs2   : clasimpset * thm list -> clasimpset
    30   val addIs2    : clasimpset * thm list -> clasimpset
    31   val addEs2    : clasimpset * thm list -> clasimpset
    32   val addDs2    : clasimpset * thm list -> clasimpset
    33   val addsimps2 : clasimpset * thm list -> clasimpset
    34   val delsimps2 : clasimpset * thm list -> clasimpset
    35   val addSss    : claset * simpset -> claset
    36   val addss     : claset * simpset -> claset
    37   val CLASIMPSET  :(clasimpset -> tactic) -> tactic
    38   val CLASIMPSET' :(clasimpset -> 'a -> tactic) -> 'a -> tactic
    39   val clarsimp_tac: clasimpset -> int -> tactic
    40   val Clarsimp_tac:               int -> tactic
    41   val mk_auto_tac : clasimpset -> int -> int -> tactic
    42   val auto_tac    : clasimpset -> tactic
    43   val Auto_tac    : tactic
    44   val auto        : unit -> unit
    45   val force_tac   : clasimpset  -> int -> tactic
    46   val Force_tac   : int -> tactic
    47   val force       : int -> unit
    48   val fast_simp_tac : clasimpset -> int -> tactic
    49   val slow_simp_tac : clasimpset -> int -> tactic
    50   val best_simp_tac : clasimpset -> int -> tactic
    51   val change_global_css: (clasimpset * thm list -> clasimpset) -> theory attribute
    52   val change_local_css: (clasimpset * thm list -> clasimpset) -> Proof.context attribute
    53   val get_local_clasimpset: Proof.context -> clasimpset
    54   val clasimp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    55   val setup       : (theory -> theory) list
    56 end;
    57 
    58 functor ClasimpFun(Data: CLASIMP_DATA): CLASIMP =
    59 struct
    60 
    61 open Data;
    62 
    63 type claset = Classical.claset;
    64 type simpset = Simplifier.simpset;
    65 type clasimpset = claset * simpset;
    66 
    67 
    68 (* clasimpset operations *)
    69 
    70 (*this interface for updating a clasimpset is rudimentary and just for
    71   convenience for the most common cases*)
    72 
    73 fun pair_upd1 f ((a,b),x) = (f(a,x), b);
    74 fun pair_upd2 f ((a,b),x) = (a, f(b,x));
    75 
    76 fun op addSIs2   arg = pair_upd1 Classical.addSIs arg;
    77 fun op addSEs2   arg = pair_upd1 Classical.addSEs arg;
    78 fun op addSDs2   arg = pair_upd1 Classical.addSDs arg;
    79 fun op addIs2    arg = pair_upd1 Classical.addIs arg;
    80 fun op addEs2    arg = pair_upd1 Classical.addEs arg;
    81 fun op addDs2    arg = pair_upd1 Classical.addDs arg;
    82 fun op addsimps2 arg = pair_upd2 Simplifier.addsimps arg;
    83 fun op delsimps2 arg = pair_upd2 Simplifier.delsimps arg;
    84 
    85 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
    86 val safe_asm_full_simp_tac = Simplifier.generic_simp_tac true (true,true,true);
    87 
    88 (*Add a simpset to a classical set!*)
    89 (*Caution: only one simpset added can be added by each of addSss and addss*)
    90 fun cs addSss ss = Classical.addSaltern (cs, ("safe_asm_full_simp_tac",
    91                             CHANGED o safe_asm_full_simp_tac ss));
    92 fun cs addss  ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
    93                             CHANGED o Simplifier.asm_full_simp_tac ss));
    94 
    95 (* tacticals *)
    96 
    97 fun CLASIMPSET tacf state =
    98   Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss))) state;
    99 
   100 fun CLASIMPSET' tacf i state =
   101   Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss) i)) state;
   102 
   103 
   104 fun clarsimp_tac (cs, ss) = safe_asm_full_simp_tac ss THEN_ALL_NEW
   105                             Classical.clarify_tac (cs addSss ss);
   106 fun Clarsimp_tac i = clarsimp_tac (Classical.claset(), Simplifier.simpset()) i;
   107 
   108 
   109 (* auto_tac *)
   110 
   111 fun blast_depth_tac cs m i thm =
   112     Blast.depth_tac cs m i thm
   113       handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
   114 
   115 (* a variant of depth_tac that avoids interference of the simplifier
   116    with dup_step_tac when they are combined by auto_tac *)
   117 local
   118 fun slow_step_tac' cs = Classical.appWrappers cs
   119         (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
   120 in fun nodup_depth_tac cs m i state = SELECT_GOAL
   121    (Classical.safe_steps_tac cs 1 THEN_ELSE
   122         (DEPTH_SOLVE (nodup_depth_tac cs m 1),
   123          Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
   124              (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
   125         )) i state;
   126 end;
   127 
   128 (*Designed to be idempotent, except if blast_depth_tac instantiates variables
   129   in some of the subgoals*)
   130 fun mk_auto_tac (cs, ss) m n =
   131     let val cs' = cs addss ss
   132         val maintac =
   133           blast_depth_tac cs m               (* fast but can't use wrappers *)
   134           ORELSE'
   135           (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
   136     in  EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
   137                TRY (Classical.safe_tac cs),
   138                REPEAT (FIRSTGOAL maintac),
   139                TRY (Classical.safe_tac (cs addSss ss)),
   140                prune_params_tac]
   141     end;
   142 
   143 fun auto_tac css = mk_auto_tac css 4 2;
   144 
   145 fun Auto_tac st = auto_tac (Classical.claset(), Simplifier.simpset()) st;
   146 
   147 fun auto () = by Auto_tac;
   148 
   149 
   150 (* force_tac *)
   151 
   152 (* aimed to solve the given subgoal totally, using whatever tools possible *)
   153 fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
   154         Classical.clarify_tac cs' 1,
   155         IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
   156         ALLGOALS (Classical.first_best_tac cs')]) end;
   157 fun Force_tac i = force_tac (Classical.claset(), Simplifier.simpset()) i;
   158 fun force i = by (Force_tac i);
   159 
   160 
   161 (* basic combinations *)
   162 
   163 fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
   164 
   165 val fast_simp_tac = ADDSS Classical.fast_tac;
   166 val slow_simp_tac = ADDSS Classical.slow_tac;
   167 val best_simp_tac = ADDSS Classical.best_tac;
   168 
   169 
   170 (* change clasimpset *)
   171 
   172 fun change_global_css f (thy, th) =
   173   let
   174     val cs_ref = Classical.claset_ref_of thy;
   175     val ss_ref = Simplifier.simpset_ref_of thy;
   176     val (cs', ss') = f ((! cs_ref, ! ss_ref), [th]);
   177   in cs_ref := cs'; ss_ref := ss'; (thy, th) end;
   178 
   179 fun change_local_css f (ctxt, th) =
   180   let
   181     val cs = Classical.get_local_claset ctxt;
   182     val ss = Simplifier.get_local_simpset ctxt;
   183     val (cs', ss') = f ((cs, ss), [th]);
   184     val ctxt' =
   185       ctxt
   186       |> Classical.put_local_claset cs'
   187       |> Simplifier.put_local_simpset ss';
   188   in (ctxt', th) end;
   189 
   190 
   191 (* methods *)
   192 
   193 fun get_local_clasimpset ctxt =
   194   (Classical.get_local_claset ctxt, Simplifier.get_local_simpset ctxt);
   195 
   196 val clasimp_modifiers =
   197   Simplifier.simp_modifiers @ Splitter.split_modifiers @ Classical.cla_modifiers;
   198 
   199 fun clasimp_meth tac prems ctxt = Method.METHOD (fn facts =>
   200   ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (get_local_clasimpset ctxt));
   201 
   202 fun clasimp_meth' tac prems ctxt = Method.METHOD (fn facts =>
   203   HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (get_local_clasimpset ctxt)));
   204 
   205 val clasimp_method = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth;
   206 val clasimp_method' = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth';
   207 
   208 
   209 fun auto_args m =
   210   Method.bang_sectioned_args' clasimp_modifiers (Scan.lift (Scan.option (Args.nat -- Args.nat))) m;
   211 
   212 fun auto_meth None = clasimp_meth (CHANGED o auto_tac)
   213   | auto_meth (Some (m, n)) = clasimp_meth (CHANGED o (fn css => mk_auto_tac css m n));
   214 
   215 
   216 (* theory setup *)
   217 
   218 val setup =
   219  [Method.add_methods
   220    [("fastsimp", clasimp_method' fast_simp_tac, "combined fast and simp"),
   221     ("slowsimp", clasimp_method' slow_simp_tac, "combined slow and simp"),
   222     ("bestsimp", clasimp_method' best_simp_tac, "combined best and simp"),
   223     ("force", clasimp_method' force_tac, "force"),
   224     ("auto", auto_args auto_meth, "auto"),
   225     ("clarsimp", clasimp_method' (CHANGED oo clarsimp_tac), "clarify simplified goal")]];
   226 
   227 
   228 end;