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