src/Provers/clasimp.ML
author wenzelm
Tue Oct 24 17:35:22 2000 +0200 (2000-10-24)
changeset 10317 3205fe2f4ef5
parent 10036 ca83cc2973f9
child 10821 dcb75538f542
permissions -rw-r--r--
added clasimpset: unit -> clasimpset;
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
wenzelm@9860
     9
infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2 addsimps2 delsimps2
wenzelm@9860
    10
  addSss addss addIffs delIffs;
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@9860
    19
  val dest_Trueprop: term -> term
wenzelm@9860
    20
  val not_const: term
wenzelm@9860
    21
  val iff_const: term
wenzelm@9860
    22
  val notE: thm
wenzelm@9860
    23
  val iffD1: thm
wenzelm@9860
    24
  val iffD2: thm
wenzelm@9860
    25
  val cla_make_elim: thm -> thm
wenzelm@5219
    26
end
wenzelm@5219
    27
oheimb@4652
    28
signature CLASIMP =
oheimb@4652
    29
sig
wenzelm@5219
    30
  include CLASIMP_DATA
wenzelm@5219
    31
  type claset
wenzelm@5219
    32
  type simpset
wenzelm@5219
    33
  type clasimpset
wenzelm@10317
    34
  val clasimpset: unit -> clasimpset
wenzelm@9860
    35
  val addSIs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    36
  val addSEs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    37
  val addSDs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    38
  val addIs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    39
  val addEs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    40
  val addDs2: clasimpset * thm list -> clasimpset
wenzelm@9860
    41
  val addsimps2: clasimpset * thm list -> clasimpset
wenzelm@9860
    42
  val delsimps2: clasimpset * thm list -> clasimpset
wenzelm@9860
    43
  val addSss: claset * simpset -> claset
wenzelm@9860
    44
  val addss: claset * simpset -> claset
wenzelm@9860
    45
  val addIffs: clasimpset * thm list -> clasimpset
wenzelm@9860
    46
  val delIffs: clasimpset * thm list -> clasimpset
wenzelm@9860
    47
  val AddIffs: thm list -> unit
wenzelm@9860
    48
  val DelIffs: thm list -> unit
wenzelm@9860
    49
  val CLASIMPSET: (clasimpset -> tactic) -> tactic
wenzelm@9860
    50
  val CLASIMPSET': (clasimpset -> 'a -> tactic) -> 'a -> tactic
oheimb@5483
    51
  val clarsimp_tac: clasimpset -> int -> tactic
wenzelm@9860
    52
  val Clarsimp_tac: int -> tactic
wenzelm@9860
    53
  val mk_auto_tac: clasimpset -> int -> int -> tactic
wenzelm@9860
    54
  val auto_tac: clasimpset -> tactic
wenzelm@9860
    55
  val Auto_tac: tactic
wenzelm@9860
    56
  val auto: unit -> unit
wenzelm@9860
    57
  val force_tac: clasimpset  -> int -> tactic
wenzelm@9860
    58
  val Force_tac: int -> tactic
wenzelm@9860
    59
  val force: int -> unit
wenzelm@9860
    60
  val fast_simp_tac: clasimpset -> int -> tactic
wenzelm@9860
    61
  val slow_simp_tac: clasimpset -> int -> tactic
wenzelm@9860
    62
  val best_simp_tac: clasimpset -> int -> tactic
wenzelm@8639
    63
  val change_global_css: (clasimpset * thm list -> clasimpset) -> theory attribute
wenzelm@8639
    64
  val change_local_css: (clasimpset * thm list -> clasimpset) -> Proof.context attribute
wenzelm@9506
    65
  val get_local_clasimpset: Proof.context -> clasimpset
wenzelm@9860
    66
  val iff_add_global: theory attribute
wenzelm@10033
    67
  val iff_add_global': theory attribute
wenzelm@9860
    68
  val iff_del_global: theory attribute
wenzelm@9860
    69
  val iff_add_local: Proof.context attribute
wenzelm@10033
    70
  val iff_add_local': Proof.context attribute
wenzelm@9860
    71
  val iff_del_local: Proof.context attribute
wenzelm@9860
    72
  val iff_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@9506
    73
  val clasimp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@9860
    74
  val setup: (theory -> theory) list
oheimb@4652
    75
end;
oheimb@4652
    76
wenzelm@5219
    77
functor ClasimpFun(Data: CLASIMP_DATA): CLASIMP =
oheimb@4652
    78
struct
oheimb@4652
    79
wenzelm@5219
    80
open Data;
wenzelm@5219
    81
wenzelm@5219
    82
type claset = Classical.claset;
wenzelm@5219
    83
type simpset = Simplifier.simpset;
wenzelm@5219
    84
type clasimpset = claset * simpset;
wenzelm@5219
    85
wenzelm@10317
    86
fun clasimpset () = (Classical.claset (), Simplifier.simpset ());
wenzelm@10317
    87
wenzelm@5219
    88
wenzelm@5219
    89
(* clasimpset operations *)
wenzelm@5219
    90
wenzelm@5219
    91
(*this interface for updating a clasimpset is rudimentary and just for
wenzelm@5219
    92
  convenience for the most common cases*)
wenzelm@5219
    93
wenzelm@5219
    94
fun pair_upd1 f ((a,b),x) = (f(a,x), b);
wenzelm@5219
    95
fun pair_upd2 f ((a,b),x) = (a, f(b,x));
wenzelm@5219
    96
wenzelm@5219
    97
fun op addSIs2   arg = pair_upd1 Classical.addSIs arg;
wenzelm@5219
    98
fun op addSEs2   arg = pair_upd1 Classical.addSEs arg;
wenzelm@5219
    99
fun op addSDs2   arg = pair_upd1 Classical.addSDs arg;
wenzelm@5219
   100
fun op addIs2    arg = pair_upd1 Classical.addIs arg;
wenzelm@5219
   101
fun op addEs2    arg = pair_upd1 Classical.addEs arg;
wenzelm@5219
   102
fun op addDs2    arg = pair_upd1 Classical.addDs arg;
wenzelm@5219
   103
fun op addsimps2 arg = pair_upd2 Simplifier.addsimps arg;
wenzelm@5219
   104
fun op delsimps2 arg = pair_upd2 Simplifier.delsimps arg;
oheimb@4652
   105
oheimb@9402
   106
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@9402
   107
val safe_asm_full_simp_tac = Simplifier.generic_simp_tac true (true,true,true);
oheimb@9402
   108
oheimb@4652
   109
(*Add a simpset to a classical set!*)
oheimb@4652
   110
(*Caution: only one simpset added can be added by each of addSss and addss*)
paulson@5567
   111
fun cs addSss ss = Classical.addSaltern (cs, ("safe_asm_full_simp_tac",
wenzelm@9772
   112
                            CHANGED o safe_asm_full_simp_tac ss));
wenzelm@9772
   113
fun cs addss  ss = Classical.addbefore  (cs, ("asm_full_simp_tac",
wenzelm@9772
   114
                            CHANGED o Simplifier.asm_full_simp_tac ss));
oheimb@4652
   115
wenzelm@9860
   116
wenzelm@9860
   117
(* iffs: addition of rules to simpsets and clasets simultaneously *)
wenzelm@9860
   118
wenzelm@9860
   119
(*Takes UNCONDITIONAL theorems of the form A<->B to
wenzelm@9860
   120
        the Safe Intr     rule B==>A and
wenzelm@9860
   121
        the Safe Destruct rule A==>B.
wenzelm@9860
   122
  Also ~A goes to the Safe Elim rule A ==> ?R
wenzelm@9860
   123
  Failing other cases, A is added as a Safe Intr rule*)
wenzelm@9860
   124
local
wenzelm@9860
   125
wenzelm@10033
   126
fun addIff dest elim intro simp ((cs, ss), th) =
wenzelm@10033
   127
  (case dest_Trueprop (#prop (Thm.rep_thm th)) of
wenzelm@9860
   128
    con $ _ $ _ =>
wenzelm@9860
   129
      if con = Data.iff_const then
wenzelm@10033
   130
        dest (intro (cs, [zero_var_indexes (th RS Data.iffD2)]),
wenzelm@9860
   131
          [zero_var_indexes (th RS Data.iffD1)])
wenzelm@10033
   132
      else intro (cs, [th])
wenzelm@9860
   133
  | con $ A =>
wenzelm@10033
   134
      if con = Data.not_const then elim (cs, [zero_var_indexes (th RS Data.notE)])
wenzelm@10033
   135
      else intro (cs, [th])
wenzelm@10033
   136
  | _ => intro (cs, [th]), simp (ss, [th]))
wenzelm@9860
   137
  handle TERM _ => error ("iff add: theorem must be unconditional\n" ^ Display.string_of_thm th);
wenzelm@9860
   138
wenzelm@10033
   139
fun delIff ((cs, ss), th) =
wenzelm@10033
   140
  (case dest_Trueprop (#prop (Thm.rep_thm th)) of
wenzelm@9860
   141
    con $ _ $ _ =>
wenzelm@9860
   142
      if con = Data.iff_const then
wenzelm@10033
   143
        Classical.delrules (cs, [zero_var_indexes (th RS Data.iffD2),
wenzelm@9860
   144
          Data.cla_make_elim (zero_var_indexes (th RS Data.iffD1))])
wenzelm@10033
   145
      else Classical.delrules (cs, [th])
wenzelm@9860
   146
  | con $ A =>
wenzelm@10033
   147
      if con = Data.not_const then Classical.delrules (cs, [zero_var_indexes (th RS Data.notE)])
wenzelm@10033
   148
      else Classical.delrules (cs, [th])
wenzelm@10033
   149
  | _ => Classical.delrules (cs, [th]), Simplifier.delsimps (ss, [th]))
wenzelm@9860
   150
  handle TERM _ =>
wenzelm@10033
   151
    (warning ("iff del: ignoring conditional theorem\n" ^ string_of_thm th); (cs, ss));
wenzelm@9860
   152
wenzelm@10033
   153
fun store_clasimp (cs, ss) =
wenzelm@10033
   154
  (Classical.claset_ref () := cs; Simplifier.simpset_ref () := ss);
wenzelm@9860
   155
wenzelm@9860
   156
in
wenzelm@9860
   157
wenzelm@10033
   158
val op addIffs =
wenzelm@10033
   159
  foldl (addIff Classical.addSDs Classical.addSEs Classical.addSIs Simplifier.addsimps);
wenzelm@10033
   160
val addIffs' =
wenzelm@10036
   161
  foldl (addIff Classical.addXDs Classical.addXEs Classical.addXIs
wenzelm@10036
   162
    ((fn (ss, _) => ss): Simplifier.simpset * thm list -> Simplifier.simpset));
wenzelm@9860
   163
val op delIffs = foldl delIff;
wenzelm@10033
   164
wenzelm@10317
   165
fun AddIffs thms = store_clasimp (clasimpset () addIffs thms);
wenzelm@10317
   166
fun DelIffs thms = store_clasimp (clasimpset () delIffs thms);
wenzelm@9860
   167
wenzelm@9860
   168
end;
wenzelm@9860
   169
wenzelm@9860
   170
wenzelm@5219
   171
(* tacticals *)
wenzelm@5219
   172
wenzelm@5219
   173
fun CLASIMPSET tacf state =
wenzelm@5219
   174
  Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss))) state;
wenzelm@5219
   175
wenzelm@5219
   176
fun CLASIMPSET' tacf i state =
wenzelm@5219
   177
  Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss) i)) state;
wenzelm@4888
   178
wenzelm@4888
   179
oheimb@9402
   180
fun clarsimp_tac (cs, ss) = safe_asm_full_simp_tac ss THEN_ALL_NEW
wenzelm@9772
   181
                            Classical.clarify_tac (cs addSss ss);
wenzelm@10317
   182
fun Clarsimp_tac i = clarsimp_tac (clasimpset ()) i;
oheimb@5483
   183
oheimb@5483
   184
wenzelm@5219
   185
(* auto_tac *)
oheimb@4652
   186
wenzelm@5219
   187
fun blast_depth_tac cs m i thm =
wenzelm@9772
   188
    Blast.depth_tac cs m i thm
oheimb@5554
   189
      handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
wenzelm@9772
   190
wenzelm@9772
   191
(* a variant of depth_tac that avoids interference of the simplifier
wenzelm@5219
   192
   with dup_step_tac when they are combined by auto_tac *)
oheimb@5756
   193
local
wenzelm@9772
   194
fun slow_step_tac' cs = Classical.appWrappers cs
wenzelm@9772
   195
        (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
wenzelm@9772
   196
in fun nodup_depth_tac cs m i state = SELECT_GOAL
wenzelm@9772
   197
   (Classical.safe_steps_tac cs 1 THEN_ELSE
wenzelm@9772
   198
        (DEPTH_SOLVE (nodup_depth_tac cs m 1),
wenzelm@9772
   199
         Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
wenzelm@9772
   200
             (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
oheimb@5756
   201
        )) i state;
oheimb@5756
   202
end;
oheimb@4652
   203
oheimb@9402
   204
(*Designed to be idempotent, except if blast_depth_tac instantiates variables
oheimb@4652
   205
  in some of the subgoals*)
oheimb@4652
   206
fun mk_auto_tac (cs, ss) m n =
wenzelm@5219
   207
    let val cs' = cs addss ss
wenzelm@9772
   208
        val maintac =
wenzelm@9772
   209
          blast_depth_tac cs m               (* fast but can't use wrappers *)
oheimb@4652
   210
          ORELSE'
oheimb@5756
   211
          (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
wenzelm@5219
   212
    in  EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
wenzelm@9772
   213
               TRY (Classical.safe_tac cs),
wenzelm@9772
   214
               REPEAT (FIRSTGOAL maintac),
wenzelm@5219
   215
               TRY (Classical.safe_tac (cs addSss ss)),
wenzelm@9772
   216
               prune_params_tac]
oheimb@4652
   217
    end;
oheimb@4652
   218
wenzelm@9772
   219
fun auto_tac css = mk_auto_tac css 4 2;
oheimb@4652
   220
wenzelm@10317
   221
fun Auto_tac st = auto_tac (clasimpset ()) st;
oheimb@4652
   222
oheimb@4652
   223
fun auto () = by Auto_tac;
oheimb@4652
   224
wenzelm@9772
   225
wenzelm@5219
   226
(* force_tac *)
wenzelm@5219
   227
oheimb@4659
   228
(* aimed to solve the given subgoal totally, using whatever tools possible *)
oheimb@4717
   229
fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
wenzelm@9772
   230
        Classical.clarify_tac cs' 1,
wenzelm@9772
   231
        IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
wenzelm@9772
   232
        ALLGOALS (Classical.first_best_tac cs')]) end;
wenzelm@10317
   233
fun Force_tac i = force_tac (clasimpset ()) i;
oheimb@4717
   234
fun force i = by (Force_tac i);
oheimb@4659
   235
wenzelm@5219
   236
wenzelm@9805
   237
(* basic combinations *)
wenzelm@9805
   238
wenzelm@9805
   239
fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
wenzelm@9591
   240
wenzelm@9805
   241
val fast_simp_tac = ADDSS Classical.fast_tac;
wenzelm@9805
   242
val slow_simp_tac = ADDSS Classical.slow_tac;
wenzelm@9805
   243
val best_simp_tac = ADDSS Classical.best_tac;
wenzelm@9591
   244
wenzelm@9591
   245
wenzelm@9860
   246
(* access clasimpset *)
wenzelm@8639
   247
wenzelm@8639
   248
fun change_global_css f (thy, th) =
wenzelm@8639
   249
  let
wenzelm@8639
   250
    val cs_ref = Classical.claset_ref_of thy;
wenzelm@8639
   251
    val ss_ref = Simplifier.simpset_ref_of thy;
wenzelm@8639
   252
    val (cs', ss') = f ((! cs_ref, ! ss_ref), [th]);
wenzelm@8639
   253
  in cs_ref := cs'; ss_ref := ss'; (thy, th) end;
wenzelm@8639
   254
wenzelm@8639
   255
fun change_local_css f (ctxt, th) =
wenzelm@8639
   256
  let
wenzelm@8639
   257
    val cs = Classical.get_local_claset ctxt;
wenzelm@8639
   258
    val ss = Simplifier.get_local_simpset ctxt;
wenzelm@8639
   259
    val (cs', ss') = f ((cs, ss), [th]);
wenzelm@8639
   260
    val ctxt' =
wenzelm@8639
   261
      ctxt
wenzelm@8639
   262
      |> Classical.put_local_claset cs'
wenzelm@8639
   263
      |> Simplifier.put_local_simpset ss';
wenzelm@8639
   264
  in (ctxt', th) end;
wenzelm@8639
   265
wenzelm@7153
   266
fun get_local_clasimpset ctxt =
wenzelm@7153
   267
  (Classical.get_local_claset ctxt, Simplifier.get_local_simpset ctxt);
wenzelm@5926
   268
wenzelm@9860
   269
wenzelm@9860
   270
(* attributes *)
wenzelm@9860
   271
wenzelm@9860
   272
val iff_add_global = change_global_css (op addIffs);
wenzelm@10033
   273
val iff_add_global' = change_global_css (op addIffs');
wenzelm@9860
   274
val iff_del_global = change_global_css (op delIffs);
wenzelm@9860
   275
val iff_add_local = change_local_css (op addIffs);
wenzelm@10033
   276
val iff_add_local' = change_local_css (op addIffs');
wenzelm@9860
   277
val iff_del_local = change_local_css (op delIffs);
wenzelm@9860
   278
wenzelm@10033
   279
fun iff_att add add' del = Attrib.syntax (Scan.lift
wenzelm@10033
   280
 (Args.del >> K del ||
wenzelm@10033
   281
  Scan.option Args.add -- Args.query >> K add' ||
wenzelm@10033
   282
  Scan.option Args.add >> K add));
wenzelm@10033
   283
wenzelm@9860
   284
val iff_attr =
wenzelm@10033
   285
 (iff_att iff_add_global iff_add_global' iff_del_global,
wenzelm@10033
   286
  iff_att iff_add_local iff_add_local' iff_del_local);
wenzelm@9860
   287
wenzelm@9860
   288
wenzelm@9860
   289
(* method modifiers *)
wenzelm@9860
   290
wenzelm@9860
   291
val iffN = "iff";
wenzelm@9860
   292
wenzelm@9860
   293
val iff_modifiers =
wenzelm@10033
   294
 [Args.$$$ iffN -- Scan.option Args.add -- Args.colon >> K ((I, iff_add_local): Method.modifier),
wenzelm@10033
   295
  Args.$$$ iffN -- Scan.option Args.add -- Args.query_colon >> K (I, iff_add_local'),
wenzelm@10033
   296
  Args.$$$ iffN -- Args.del -- Args.colon >> K (I, iff_del_local)];
wenzelm@9860
   297
wenzelm@8469
   298
val clasimp_modifiers =
wenzelm@9860
   299
  Simplifier.simp_modifiers @ Splitter.split_modifiers @
wenzelm@9860
   300
  Classical.cla_modifiers @ iff_modifiers;
wenzelm@9860
   301
wenzelm@9860
   302
wenzelm@9860
   303
(* methods *)
wenzelm@5926
   304
wenzelm@7559
   305
fun clasimp_meth tac prems ctxt = Method.METHOD (fn facts =>
wenzelm@7559
   306
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (get_local_clasimpset ctxt));
wenzelm@7132
   307
wenzelm@7559
   308
fun clasimp_meth' tac prems ctxt = Method.METHOD (fn facts =>
wenzelm@8168
   309
  HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (get_local_clasimpset ctxt)));
wenzelm@5926
   310
wenzelm@7559
   311
val clasimp_method = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth;
wenzelm@7559
   312
val clasimp_method' = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth';
wenzelm@5926
   313
wenzelm@5926
   314
wenzelm@9772
   315
fun auto_args m =
wenzelm@9772
   316
  Method.bang_sectioned_args' clasimp_modifiers (Scan.lift (Scan.option (Args.nat -- Args.nat))) m;
wenzelm@9772
   317
wenzelm@9772
   318
fun auto_meth None = clasimp_meth (CHANGED o auto_tac)
wenzelm@9772
   319
  | auto_meth (Some (m, n)) = clasimp_meth (CHANGED o (fn css => mk_auto_tac css m n));
wenzelm@9772
   320
wenzelm@9772
   321
wenzelm@9772
   322
(* theory setup *)
wenzelm@9772
   323
wenzelm@5926
   324
val setup =
wenzelm@9860
   325
 [Attrib.add_attributes
wenzelm@9893
   326
  [("iff", iff_attr, "declaration of Simplifier / Classical rules")],
wenzelm@9860
   327
  Method.add_methods
wenzelm@9591
   328
   [("fastsimp", clasimp_method' fast_simp_tac, "combined fast and simp"),
wenzelm@9805
   329
    ("slowsimp", clasimp_method' slow_simp_tac, "combined slow and simp"),
wenzelm@9805
   330
    ("bestsimp", clasimp_method' best_simp_tac, "combined best and simp"),
wenzelm@9591
   331
    ("force", clasimp_method' force_tac, "force"),
wenzelm@9772
   332
    ("auto", auto_args auto_meth, "auto"),
wenzelm@9591
   333
    ("clarsimp", clasimp_method' (CHANGED oo clarsimp_tac), "clarify simplified goal")]];
wenzelm@5926
   334
oheimb@4652
   335
end;