src/Provers/clasimp.ML
author wenzelm
Wed Apr 04 00:11:03 2007 +0200 (2007-04-04)
changeset 22578 b0eb5652f210
parent 22095 07875394618e
child 26425 6561665c5cb1
permissions -rw-r--r--
removed obsolete sign_of/sign_of_thm;
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@18728
    59
  val attrib: (clasimpset * thm list -> clasimpset) -> attribute
wenzelm@9506
    60
  val get_local_clasimpset: Proof.context -> clasimpset
wenzelm@15032
    61
  val local_clasimpset_of: Proof.context -> clasimpset
wenzelm@18728
    62
  val iff_add: attribute
wenzelm@18728
    63
  val iff_add': attribute
wenzelm@18728
    64
  val iff_del: 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@22095
    84
fun change_clasimpset f = change_clasimpset_of (ML_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
wenzelm@9860
   117
(* iffs: addition of rules to simpsets and clasets simultaneously *)
wenzelm@9860
   118
oheimb@11344
   119
(*Takes (possibly conditional) 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
paulson@11462
   123
  Failing other cases, A is added as a Safe Intr rule*)
wenzelm@9860
   124
local
wenzelm@9860
   125
wenzelm@13026
   126
fun addIff decls1 decls2 simp ((cs, ss), th) =
wenzelm@13026
   127
  let
wenzelm@13026
   128
    val n = nprems_of th;
paulson@17084
   129
    val (elim, intro) = if n = 0 then decls1 else decls2;
wenzelm@13026
   130
    val zero_rotate = zero_var_indexes o rotate_prems n;
wenzelm@13026
   131
  in
paulson@21709
   132
    (elim (intro (cs, [zero_rotate (th RS iffD2)]),
paulson@21709
   133
           [Tactic.make_elim (zero_rotate (th RS iffD1))])
paulson@21709
   134
      handle THM _ => (elim (cs, [zero_rotate (th RS notE)])
wenzelm@11902
   135
        handle THM _ => intro (cs, [th])),
wenzelm@11902
   136
     simp (ss, [th]))
wenzelm@11902
   137
  end;
wenzelm@9860
   138
wenzelm@12375
   139
fun delIff delcs delss ((cs, ss), th) =
wenzelm@11902
   140
  let val zero_rotate = zero_var_indexes o rotate_prems (nprems_of th) in
wenzelm@18688
   141
    (delcs (cs, [zero_rotate (th RS iffD2),
wenzelm@18688
   142
        Tactic.make_elim (zero_rotate (th RS iffD1))])
wenzelm@18688
   143
      handle THM _ => (delcs (cs, [zero_rotate (th RS notE)])
wenzelm@12375
   144
        handle THM _ => delcs (cs, [th])),
wenzelm@12375
   145
     delss (ss, [th]))
wenzelm@11902
   146
  end;
wenzelm@9860
   147
wenzelm@18630
   148
fun modifier att (x, ths) =
wenzelm@18728
   149
  fst (foldl_map (Library.apply [att]) (x, rev ths));
wenzelm@18630
   150
wenzelm@18688
   151
val addXIs = modifier (ContextRules.intro_query NONE);
wenzelm@18688
   152
val addXEs = modifier (ContextRules.elim_query NONE);
wenzelm@18688
   153
val addXDs = modifier (ContextRules.dest_query NONE);
wenzelm@18688
   154
val delXs = modifier ContextRules.rule_del;
wenzelm@18630
   155
wenzelm@9860
   156
in
wenzelm@9860
   157
wenzelm@10033
   158
val op addIffs =
paulson@17084
   159
  Library.foldl 
paulson@17084
   160
      (addIff (Classical.addSEs, Classical.addSIs)
paulson@17084
   161
              (Classical.addEs, Classical.addIs) 
paulson@17084
   162
              Simplifier.addsimps);
skalberg@15570
   163
val op delIffs = Library.foldl (delIff Classical.delrules Simplifier.delsimps);
wenzelm@10033
   164
wenzelm@17879
   165
fun AddIffs thms = change_clasimpset (fn css => css addIffs thms);
wenzelm@17879
   166
fun DelIffs thms = change_clasimpset (fn css => css delIffs thms);
wenzelm@9860
   167
wenzelm@18688
   168
fun addIffs_generic (x, ths) =
wenzelm@18688
   169
  Library.foldl (addIff (addXEs, addXIs) (addXEs, addXIs) #1) ((x, ()), ths) |> #1;
wenzelm@12375
   170
wenzelm@18688
   171
fun delIffs_generic (x, ths) =
wenzelm@18688
   172
  Library.foldl (delIff delXs #1) ((x, ()), ths) |> #1;
wenzelm@12375
   173
wenzelm@9860
   174
end;
wenzelm@9860
   175
wenzelm@9860
   176
wenzelm@5219
   177
(* tacticals *)
wenzelm@5219
   178
wenzelm@5219
   179
fun CLASIMPSET tacf state =
wenzelm@5219
   180
  Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss))) state;
wenzelm@5219
   181
wenzelm@5219
   182
fun CLASIMPSET' tacf i state =
wenzelm@5219
   183
  Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss) i)) state;
wenzelm@4888
   184
wenzelm@4888
   185
wenzelm@12375
   186
fun clarsimp_tac (cs, ss) =
wenzelm@12375
   187
  safe_asm_full_simp_tac ss THEN_ALL_NEW
wenzelm@12375
   188
  Classical.clarify_tac (cs addSss ss);
wenzelm@12375
   189
wenzelm@10317
   190
fun Clarsimp_tac i = clarsimp_tac (clasimpset ()) i;
oheimb@5483
   191
oheimb@5483
   192
wenzelm@5219
   193
(* auto_tac *)
oheimb@4652
   194
wenzelm@5219
   195
fun blast_depth_tac cs m i thm =
wenzelm@9772
   196
    Blast.depth_tac cs m i thm
oheimb@5554
   197
      handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
wenzelm@9772
   198
wenzelm@9772
   199
(* a variant of depth_tac that avoids interference of the simplifier
wenzelm@5219
   200
   with dup_step_tac when they are combined by auto_tac *)
oheimb@5756
   201
local
wenzelm@9772
   202
fun slow_step_tac' cs = Classical.appWrappers cs
wenzelm@9772
   203
        (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
wenzelm@9772
   204
in fun nodup_depth_tac cs m i state = SELECT_GOAL
wenzelm@9772
   205
   (Classical.safe_steps_tac cs 1 THEN_ELSE
wenzelm@9772
   206
        (DEPTH_SOLVE (nodup_depth_tac cs m 1),
wenzelm@9772
   207
         Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
wenzelm@9772
   208
             (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
oheimb@5756
   209
        )) i state;
oheimb@5756
   210
end;
oheimb@4652
   211
oheimb@9402
   212
(*Designed to be idempotent, except if blast_depth_tac instantiates variables
oheimb@4652
   213
  in some of the subgoals*)
oheimb@4652
   214
fun mk_auto_tac (cs, ss) m n =
wenzelm@5219
   215
    let val cs' = cs addss ss
wenzelm@9772
   216
        val maintac =
wenzelm@9772
   217
          blast_depth_tac cs m               (* fast but can't use wrappers *)
oheimb@4652
   218
          ORELSE'
oheimb@5756
   219
          (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
wenzelm@5219
   220
    in  EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
wenzelm@9772
   221
               TRY (Classical.safe_tac cs),
wenzelm@9772
   222
               REPEAT (FIRSTGOAL maintac),
wenzelm@5219
   223
               TRY (Classical.safe_tac (cs addSss ss)),
wenzelm@9772
   224
               prune_params_tac]
oheimb@4652
   225
    end;
oheimb@4652
   226
wenzelm@9772
   227
fun auto_tac css = mk_auto_tac css 4 2;
wenzelm@10317
   228
fun Auto_tac st = auto_tac (clasimpset ()) st;
oheimb@4652
   229
fun auto () = by Auto_tac;
oheimb@4652
   230
wenzelm@9772
   231
wenzelm@5219
   232
(* force_tac *)
wenzelm@5219
   233
oheimb@4659
   234
(* aimed to solve the given subgoal totally, using whatever tools possible *)
oheimb@4717
   235
fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
wenzelm@9772
   236
        Classical.clarify_tac cs' 1,
wenzelm@9772
   237
        IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
wenzelm@9772
   238
        ALLGOALS (Classical.first_best_tac cs')]) end;
wenzelm@10317
   239
fun Force_tac i = force_tac (clasimpset ()) i;
oheimb@4717
   240
fun force i = by (Force_tac i);
oheimb@4659
   241
wenzelm@5219
   242
wenzelm@9805
   243
(* basic combinations *)
wenzelm@9805
   244
wenzelm@9805
   245
fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
wenzelm@9591
   246
wenzelm@9805
   247
val fast_simp_tac = ADDSS Classical.fast_tac;
wenzelm@9805
   248
val slow_simp_tac = ADDSS Classical.slow_tac;
wenzelm@9805
   249
val best_simp_tac = ADDSS Classical.best_tac;
wenzelm@9591
   250
wenzelm@9591
   251
wenzelm@9860
   252
(* access clasimpset *)
wenzelm@8639
   253
wenzelm@7153
   254
fun get_local_clasimpset ctxt =
wenzelm@7153
   255
  (Classical.get_local_claset ctxt, Simplifier.get_local_simpset ctxt);
wenzelm@5926
   256
wenzelm@15032
   257
fun local_clasimpset_of ctxt =
wenzelm@15032
   258
  (Classical.local_claset_of ctxt, Simplifier.local_simpset_of ctxt);
wenzelm@15032
   259
wenzelm@9860
   260
wenzelm@9860
   261
(* attributes *)
wenzelm@9860
   262
wenzelm@18728
   263
fun attrib f = Thm.declaration_attribute (fn th =>
wenzelm@18688
   264
 fn Context.Theory thy => (change_clasimpset_of thy (fn css => f (css, [th])); Context.Theory thy)
wenzelm@18688
   265
  | Context.Proof ctxt =>
wenzelm@18688
   266
      let
wenzelm@18688
   267
        val cs = Classical.get_local_claset ctxt;
wenzelm@18688
   268
        val ss = Simplifier.get_local_simpset ctxt;
wenzelm@18688
   269
        val (cs', ss') = f ((cs, ss), [th]);
wenzelm@18688
   270
        val ctxt' =
wenzelm@18688
   271
          ctxt
wenzelm@18688
   272
          |> Classical.put_local_claset cs'
wenzelm@18688
   273
          |> Simplifier.put_local_simpset ss';
wenzelm@18688
   274
      in Context.Proof ctxt' end);
wenzelm@9860
   275
wenzelm@18688
   276
fun attrib' f (x, th) = (f (x, [th]), th);
wenzelm@10033
   277
wenzelm@18688
   278
val iff_add = attrib (op addIffs);
wenzelm@18688
   279
val iff_add' = attrib' addIffs_generic;
wenzelm@18688
   280
val iff_del = attrib (op delIffs) o attrib' delIffs_generic;
wenzelm@18688
   281
wenzelm@18688
   282
val iff_att = Attrib.syntax (Scan.lift
wenzelm@18688
   283
 (Args.del >> K iff_del ||
wenzelm@18688
   284
  Scan.option Args.add -- Args.query >> K iff_add' ||
wenzelm@18688
   285
  Scan.option Args.add >> K iff_add));
wenzelm@9860
   286
wenzelm@9860
   287
wenzelm@9860
   288
(* method modifiers *)
wenzelm@9860
   289
wenzelm@9860
   290
val iffN = "iff";
wenzelm@9860
   291
wenzelm@9860
   292
val iff_modifiers =
wenzelm@18728
   293
 [Args.$$$ iffN -- Scan.option Args.add -- Args.colon >> K ((I, iff_add): Method.modifier),
wenzelm@18728
   294
  Args.$$$ iffN -- Scan.option Args.add -- Args.query_colon >> K (I, iff_add'),
wenzelm@18728
   295
  Args.$$$ iffN -- Args.del -- Args.colon >> K (I, iff_del)];
wenzelm@9860
   296
wenzelm@8469
   297
val clasimp_modifiers =
wenzelm@9860
   298
  Simplifier.simp_modifiers @ Splitter.split_modifiers @
wenzelm@9860
   299
  Classical.cla_modifiers @ iff_modifiers;
wenzelm@9860
   300
wenzelm@9860
   301
wenzelm@9860
   302
(* methods *)
wenzelm@5926
   303
wenzelm@7559
   304
fun clasimp_meth tac prems ctxt = Method.METHOD (fn facts =>
wenzelm@15032
   305
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (local_clasimpset_of ctxt));
wenzelm@7132
   306
wenzelm@7559
   307
fun clasimp_meth' tac prems ctxt = Method.METHOD (fn facts =>
wenzelm@15032
   308
  HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (local_clasimpset_of ctxt)));
wenzelm@5926
   309
wenzelm@7559
   310
val clasimp_method = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth;
wenzelm@7559
   311
val clasimp_method' = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth';
wenzelm@5926
   312
wenzelm@5926
   313
wenzelm@9772
   314
fun auto_args m =
wenzelm@9772
   315
  Method.bang_sectioned_args' clasimp_modifiers (Scan.lift (Scan.option (Args.nat -- Args.nat))) m;
wenzelm@9772
   316
skalberg@15531
   317
fun auto_meth NONE = clasimp_meth (CHANGED_PROP o auto_tac)
skalberg@15531
   318
  | auto_meth (SOME (m, n)) = clasimp_meth (CHANGED_PROP o (fn css => mk_auto_tac css m n));
wenzelm@9772
   319
wenzelm@9772
   320
wenzelm@9772
   321
(* theory setup *)
wenzelm@9772
   322
wenzelm@5926
   323
val setup =
wenzelm@18708
   324
 (Attrib.add_attributes
wenzelm@18728
   325
   [("iff", iff_att, "declaration of Simplifier / Classical rules")] #>
wenzelm@9860
   326
  Method.add_methods
wenzelm@9591
   327
   [("fastsimp", clasimp_method' fast_simp_tac, "combined fast and simp"),
wenzelm@9805
   328
    ("slowsimp", clasimp_method' slow_simp_tac, "combined slow and simp"),
wenzelm@9805
   329
    ("bestsimp", clasimp_method' best_simp_tac, "combined best and simp"),
wenzelm@9591
   330
    ("force", clasimp_method' force_tac, "force"),
wenzelm@9772
   331
    ("auto", auto_args auto_meth, "auto"),
wenzelm@18708
   332
    ("clarsimp", clasimp_method' (CHANGED_PROP oo clarsimp_tac), "clarify simplified goal")]);
wenzelm@5926
   333
oheimb@4652
   334
end;