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