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