src/Provers/simplifier.ML
author wenzelm
Wed Apr 29 11:30:55 1998 +0200 (1998-04-29 ago)
changeset 4855 62bc389d6168
parent 4795 721b532ada7a
child 5001 9de7fda0a6df
permissions -rw-r--r--
tuned setup;
simp_add_global, simp_del_global, simp_add_local, simp_del_local attributes;
clasohm@1243
     1
(*  Title:      Provers/simplifier.ML
nipkow@1
     2
    ID:         $Id$
wenzelm@3557
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
nipkow@1
     4
wenzelm@4795
     5
Generic simplifier, suitable for most logics.  See Pure/thm.ML for the
wenzelm@4795
     6
actual meta-level rewriting engine.
nipkow@1
     7
*)
clasohm@1260
     8
wenzelm@3551
     9
infix 4
nipkow@4682
    10
  setsubgoaler setloop addloop delloop setSSolver addSSolver setSolver
nipkow@4677
    11
  addSolver addsimps delsimps addeqcongs deleqcongs
nipkow@4677
    12
  setmksimps setmkeqTrue setmksym settermless addsimprocs delsimprocs;
oheimb@2567
    13
wenzelm@4795
    14
signature BASIC_SIMPLIFIER =
clasohm@0
    15
sig
wenzelm@2509
    16
  type simproc
wenzelm@3577
    17
  val mk_simproc: string -> cterm list
wenzelm@3577
    18
    -> (Sign.sg -> thm list -> term -> thm option) -> simproc
clasohm@0
    19
  type simpset
wenzelm@2503
    20
  val empty_ss: simpset
wenzelm@3551
    21
  val rep_ss: simpset ->
wenzelm@3551
    22
   {mss: meta_simpset,
wenzelm@3551
    23
    subgoal_tac:        simpset -> int -> tactic,
nipkow@4668
    24
    loop_tacs:          (string * (int -> tactic))list,
wenzelm@3551
    25
           finish_tac: thm list -> int -> tactic,
wenzelm@3551
    26
    unsafe_finish_tac: thm list -> int -> tactic};
wenzelm@3551
    27
  val print_ss: simpset -> unit
wenzelm@4366
    28
  val print_simpset: theory -> unit
oheimb@2629
    29
  val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
oheimb@2629
    30
  val setloop:      simpset *             (int -> tactic) -> simpset
nipkow@4668
    31
  val addloop:      simpset *  (string * (int -> tactic)) -> simpset
nipkow@4682
    32
  val delloop:      simpset *   string                    -> simpset
oheimb@2629
    33
  val setSSolver:   simpset * (thm list -> int -> tactic) -> simpset
oheimb@2629
    34
  val addSSolver:   simpset * (thm list -> int -> tactic) -> simpset
oheimb@2629
    35
  val setSolver:    simpset * (thm list -> int -> tactic) -> simpset
oheimb@2629
    36
  val addSolver:    simpset * (thm list -> int -> tactic) -> simpset
wenzelm@3577
    37
  val setmksimps:   simpset * (thm -> thm list) -> simpset
nipkow@4677
    38
  val setmkeqTrue:  simpset * (thm -> thm option) -> simpset
nipkow@4677
    39
  val setmksym:     simpset * (thm -> thm option) -> simpset
wenzelm@3577
    40
  val settermless:  simpset * (term * term -> bool) -> simpset
wenzelm@3577
    41
  val addsimps:     simpset * thm list -> simpset
wenzelm@3577
    42
  val delsimps:     simpset * thm list -> simpset
wenzelm@3577
    43
  val addeqcongs:   simpset * thm list -> simpset
wenzelm@3577
    44
  val deleqcongs:   simpset * thm list -> simpset
wenzelm@3577
    45
  val addsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    46
  val delsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    47
  val merge_ss:     simpset * simpset -> simpset
wenzelm@3577
    48
  val prems_of_ss:  simpset -> thm list
wenzelm@4080
    49
  val simpset_ref_of_sg: Sign.sg -> simpset ref
wenzelm@4080
    50
  val simpset_ref_of: theory -> simpset ref
wenzelm@4080
    51
  val simpset_of_sg: Sign.sg -> simpset
wenzelm@4080
    52
  val simpset_of: theory -> simpset
wenzelm@4080
    53
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@4080
    54
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4080
    55
  val simpset: unit -> simpset
wenzelm@4080
    56
  val simpset_ref: unit -> simpset ref
clasohm@1243
    57
  val Addsimps: thm list -> unit
clasohm@1243
    58
  val Delsimps: thm list -> unit
wenzelm@2509
    59
  val Addsimprocs: simproc list -> unit
wenzelm@2509
    60
  val Delsimprocs: simproc list -> unit
oheimb@2629
    61
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    62
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    63
  val          full_simp_tac: simpset -> int -> tactic
nipkow@4722
    64
  val        asm_lr_simp_tac: simpset -> int -> tactic
oheimb@2629
    65
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    66
  val safe_asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    67
  val               Simp_tac:            int -> tactic
oheimb@2629
    68
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    69
  val          Full_simp_tac:            int -> tactic
nipkow@4722
    70
  val        Asm_lr_simp_tac:            int -> tactic
oheimb@2629
    71
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    72
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    73
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    74
  val     full_simplify: simpset -> thm -> thm
wenzelm@3557
    75
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    76
end;
clasohm@0
    77
wenzelm@4795
    78
signature SIMPLIFIER =
wenzelm@4795
    79
sig
wenzelm@4795
    80
  include BASIC_SIMPLIFIER
wenzelm@4855
    81
  val setup: (theory -> theory) list
wenzelm@4795
    82
  val get_local_simpset: local_theory -> simpset
wenzelm@4795
    83
  val put_local_simpset: simpset -> local_theory -> local_theory
wenzelm@4795
    84
  val simp_add: tag
wenzelm@4795
    85
  val simp_del: tag
wenzelm@4855
    86
  val simp_add_global: theory attribute
wenzelm@4855
    87
  val simp_del_global: theory attribute
wenzelm@4855
    88
  val simp_add_local: local_theory attribute
wenzelm@4855
    89
  val simp_del_local: local_theory attribute
wenzelm@4795
    90
end;
wenzelm@2503
    91
wenzelm@2503
    92
structure Simplifier: SIMPLIFIER =
clasohm@0
    93
struct
clasohm@0
    94
wenzelm@2509
    95
wenzelm@2509
    96
(** simplification procedures **)
wenzelm@2509
    97
wenzelm@2509
    98
(* datatype simproc *)
wenzelm@2509
    99
wenzelm@2509
   100
datatype simproc =
wenzelm@3577
   101
  Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
wenzelm@2509
   102
wenzelm@3557
   103
fun mk_simproc name lhss proc =
wenzelm@3557
   104
  Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
wenzelm@3557
   105
wenzelm@3551
   106
fun rep_simproc (Simproc args) = args;
wenzelm@2509
   107
wenzelm@2509
   108
wenzelm@2509
   109
wenzelm@2503
   110
(** simplification sets **)
wenzelm@2503
   111
wenzelm@2503
   112
(* type simpset *)
wenzelm@2503
   113
clasohm@0
   114
datatype simpset =
wenzelm@2503
   115
  Simpset of {
wenzelm@2503
   116
    mss: meta_simpset,
oheimb@2629
   117
    subgoal_tac:        simpset -> int -> tactic,
nipkow@4668
   118
    loop_tacs:          (string * (int -> tactic))list,
oheimb@2629
   119
           finish_tac: thm list -> int -> tactic,
oheimb@2629
   120
    unsafe_finish_tac: thm list -> int -> tactic};
wenzelm@2503
   121
nipkow@4668
   122
fun make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac) =
nipkow@4668
   123
  Simpset {mss = mss, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
oheimb@2629
   124
    finish_tac = finish_tac, unsafe_finish_tac = unsafe_finish_tac};
clasohm@0
   125
clasohm@0
   126
val empty_ss =
nipkow@4677
   127
  let val mss = Thm.set_mk_sym(Thm.empty_mss, Some o symmetric_fun)
nipkow@4677
   128
  in make_ss (mss, K (K no_tac), [], K (K no_tac), K (K no_tac)) end;
wenzelm@3551
   129
wenzelm@3551
   130
fun rep_ss (Simpset args) = args;
wenzelm@3551
   131
fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
wenzelm@3551
   132
wenzelm@3551
   133
wenzelm@3551
   134
(* print simpsets *)
wenzelm@2503
   135
wenzelm@3551
   136
fun print_ss ss =
wenzelm@3551
   137
  let
wenzelm@3551
   138
    val Simpset {mss, ...} = ss;
wenzelm@3551
   139
    val {simps, procs, congs} = Thm.dest_mss mss;
wenzelm@2503
   140
wenzelm@3551
   141
    val pretty_thms = map Display.pretty_thm;
wenzelm@3551
   142
    fun pretty_proc (name, lhss) =
wenzelm@3551
   143
      Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
wenzelm@3551
   144
  in
wenzelm@3551
   145
    Pretty.writeln (Pretty.big_list "simplification rules:" (pretty_thms simps));
wenzelm@3551
   146
    Pretty.writeln (Pretty.big_list "simplification procedures:" (map pretty_proc procs));
wenzelm@3551
   147
    Pretty.writeln (Pretty.big_list "congruences:" (pretty_thms congs))
wenzelm@3551
   148
  end;
wenzelm@2503
   149
wenzelm@2503
   150
wenzelm@2503
   151
(* extend simpsets *)
wenzelm@2503
   152
nipkow@4668
   153
fun (Simpset {mss, subgoal_tac = _, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   154
    setsubgoaler subgoal_tac =
nipkow@4668
   155
  make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
oheimb@2629
   156
nipkow@4668
   157
fun (Simpset {mss, subgoal_tac, loop_tacs = _, finish_tac, unsafe_finish_tac})
nipkow@4668
   158
    setloop tac =
nipkow@4668
   159
  make_ss (mss, subgoal_tac, [("",tac)], finish_tac, unsafe_finish_tac);
wenzelm@2503
   160
nipkow@4668
   161
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
oheimb@4741
   162
    addloop tac = make_ss (mss, subgoal_tac, 
oheimb@4741
   163
	(case assoc_string (loop_tacs,(fst tac)) of None => () | Some x => 
oheimb@4741
   164
	 warning ("overwriting looper "^fst tac); overwrite(loop_tacs,tac)),
nipkow@4668
   165
           finish_tac, unsafe_finish_tac);
oheimb@2567
   166
nipkow@4682
   167
fun (ss as Simpset {mss,subgoal_tac,loop_tacs,finish_tac,unsafe_finish_tac})
nipkow@4682
   168
    delloop name =
nipkow@4682
   169
  let val (del,rest) = partition (fn (n,_) => n=name) loop_tacs
nipkow@4682
   170
  in if null del then (warning ("No such looper in simpset: " ^ name); ss)
nipkow@4682
   171
     else make_ss (mss, subgoal_tac, rest, finish_tac, unsafe_finish_tac)
nipkow@4682
   172
  end;
nipkow@4682
   173
nipkow@4668
   174
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac = _, unsafe_finish_tac})
wenzelm@3551
   175
    setSSolver finish_tac =
nipkow@4668
   176
  make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
wenzelm@2503
   177
nipkow@4668
   178
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   179
    addSSolver tac =
nipkow@4668
   180
  make_ss (mss, subgoal_tac, loop_tacs, fn hyps => finish_tac hyps ORELSE' tac hyps,
wenzelm@3551
   181
    unsafe_finish_tac);
wenzelm@2503
   182
nipkow@4668
   183
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac = _})
wenzelm@3551
   184
    setSolver unsafe_finish_tac =
nipkow@4668
   185
  make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
wenzelm@2503
   186
nipkow@4668
   187
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   188
    addSolver tac =
nipkow@4668
   189
  make_ss (mss, subgoal_tac, loop_tacs, finish_tac,
wenzelm@3551
   190
    fn hyps => unsafe_finish_tac hyps ORELSE' tac hyps);
wenzelm@2503
   191
nipkow@4668
   192
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   193
    setmksimps mk_simps =
wenzelm@2645
   194
  make_ss (Thm.set_mk_rews (mss, map (Thm.strip_shyps o Drule.zero_var_indexes) o mk_simps),
nipkow@4668
   195
    subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
wenzelm@2509
   196
nipkow@4677
   197
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
nipkow@4677
   198
    setmkeqTrue mk_eq_True =
nipkow@4677
   199
  make_ss (Thm.set_mk_eq_True (mss, mk_eq_True),
nipkow@4677
   200
    subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
nipkow@4677
   201
nipkow@4677
   202
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
nipkow@4677
   203
    setmksym mksym =
nipkow@4677
   204
  make_ss (Thm.set_mk_sym (mss, mksym),
nipkow@4677
   205
    subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
nipkow@4677
   206
nipkow@4668
   207
fun (Simpset {mss, subgoal_tac, loop_tacs,  finish_tac, unsafe_finish_tac})
wenzelm@3551
   208
    settermless termless =
nipkow@4668
   209
  make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tacs,
wenzelm@3551
   210
    finish_tac, unsafe_finish_tac);
wenzelm@3551
   211
nipkow@4668
   212
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   213
    addsimps rews =
nipkow@4668
   214
  make_ss (Thm.add_simps (mss, rews), subgoal_tac, loop_tacs,
nipkow@4668
   215
           finish_tac, unsafe_finish_tac);
wenzelm@2503
   216
nipkow@4668
   217
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   218
    delsimps rews =
nipkow@4668
   219
  make_ss (Thm.del_simps (mss, rews), subgoal_tac, loop_tacs,
nipkow@4668
   220
           finish_tac, unsafe_finish_tac);
wenzelm@2503
   221
nipkow@4668
   222
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   223
    addeqcongs newcongs =
nipkow@4668
   224
  make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tacs,
wenzelm@3551
   225
    finish_tac, unsafe_finish_tac);
wenzelm@2509
   226
nipkow@4668
   227
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   228
    deleqcongs oldcongs =
nipkow@4668
   229
  make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tacs,
wenzelm@3551
   230
    finish_tac, unsafe_finish_tac);
oheimb@2629
   231
nipkow@4668
   232
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   233
    addsimprocs simprocs =
wenzelm@3551
   234
  make_ss
wenzelm@3551
   235
    (Thm.add_simprocs (mss, map rep_simproc simprocs),
nipkow@4668
   236
      subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
wenzelm@2509
   237
nipkow@4668
   238
fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
wenzelm@3551
   239
    delsimprocs simprocs =
wenzelm@3551
   240
  make_ss
wenzelm@3551
   241
    (Thm.del_simprocs (mss, map rep_simproc simprocs),
nipkow@4668
   242
      subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
wenzelm@2503
   243
wenzelm@2503
   244
wenzelm@4795
   245
(* merge simpsets *)	(*NOTE: ignores tactics of 2nd simpset (except loopers)*)
wenzelm@2503
   246
wenzelm@3551
   247
fun merge_ss
wenzelm@4795
   248
   (Simpset {mss = mss1, loop_tacs = loop_tacs1, subgoal_tac, finish_tac, unsafe_finish_tac},
nipkow@4668
   249
    Simpset {mss = mss2, loop_tacs = loop_tacs2, ...}) =
nipkow@4668
   250
  make_ss (Thm.merge_mss (mss1, mss2), subgoal_tac,
wenzelm@4795
   251
    merge_alists loop_tacs1 loop_tacs2, finish_tac, unsafe_finish_tac);
wenzelm@2503
   252
wenzelm@2503
   253
wenzelm@3557
   254
wenzelm@4795
   255
(** simpset data **)
wenzelm@4080
   256
wenzelm@4784
   257
val simpsetK = "Provers/simpset";
wenzelm@4795
   258
wenzelm@4795
   259
wenzelm@4795
   260
(* global simpset ref *)
wenzelm@4795
   261
wenzelm@4795
   262
exception SimpsetGlobal of simpset ref;
wenzelm@4080
   263
wenzelm@4080
   264
local
wenzelm@4795
   265
  val empty = SimpsetGlobal (ref empty_ss);
clasohm@0
   266
wenzelm@4080
   267
  (*create new reference*)
wenzelm@4795
   268
  fun prep_ext (SimpsetGlobal (ref ss)) = SimpsetGlobal (ref ss);
wenzelm@4080
   269
wenzelm@4795
   270
  fun merge (SimpsetGlobal (ref ss1), SimpsetGlobal (ref ss2)) =
wenzelm@4795
   271
    SimpsetGlobal (ref (merge_ss (ss1, ss2)));
wenzelm@4080
   272
wenzelm@4795
   273
  fun print _ (SimpsetGlobal (ref ss)) = print_ss ss;
wenzelm@4080
   274
in
wenzelm@4795
   275
  val setup_thy_data = Theory.init_data [(simpsetK, (empty, prep_ext, merge, print))];
wenzelm@4080
   276
end;
wenzelm@4080
   277
wenzelm@4080
   278
wenzelm@4795
   279
(* access global simpset *)
clasohm@0
   280
wenzelm@4366
   281
fun print_simpset thy = Display.print_data thy simpsetK;
wenzelm@4366
   282
wenzelm@4080
   283
fun simpset_ref_of_sg sg =
wenzelm@4080
   284
  (case Sign.get_data sg simpsetK of
wenzelm@4795
   285
    SimpsetGlobal r => r
wenzelm@4795
   286
  | _ => type_error simpsetK);
wenzelm@4080
   287
wenzelm@4080
   288
val simpset_ref_of = simpset_ref_of_sg o sign_of;
wenzelm@4080
   289
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@4080
   290
val simpset_of = simpset_of_sg o sign_of;
wenzelm@4080
   291
wenzelm@4080
   292
fun SIMPSET tacf state = tacf (simpset_of_sg (sign_of_thm state)) state;
wenzelm@4080
   293
fun SIMPSET' tacf i state = tacf (simpset_of_sg (sign_of_thm state)) i state;
clasohm@0
   294
wenzelm@4080
   295
val simpset = simpset_of o Context.get_context;
wenzelm@4080
   296
val simpset_ref = simpset_ref_of_sg o sign_of o Context.get_context;
wenzelm@4080
   297
wenzelm@4080
   298
wenzelm@4795
   299
(* change global simpset *)
wenzelm@4080
   300
wenzelm@4080
   301
fun change_simpset f x = simpset_ref () := (f (simpset (), x));
wenzelm@4080
   302
wenzelm@4080
   303
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   304
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   305
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   306
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@2509
   307
clasohm@0
   308
wenzelm@4795
   309
(* local simpset *)
wenzelm@4795
   310
wenzelm@4795
   311
exception SimpsetLocal of simpset;
wenzelm@4795
   312
wenzelm@4795
   313
fun get_local_simpset (thy, data) =
wenzelm@4795
   314
  (case Symtab.lookup (data, simpsetK) of
wenzelm@4795
   315
    Some (SimpsetLocal ss) => ss
wenzelm@4795
   316
  | None => simpset_of thy
wenzelm@4795
   317
  | _ => type_error simpsetK);
wenzelm@4795
   318
wenzelm@4795
   319
fun put_local_simpset ss (thy, data) =
wenzelm@4795
   320
  (thy, Symtab.update ((simpsetK, SimpsetLocal ss), data));
wenzelm@4795
   321
wenzelm@4795
   322
wenzelm@4795
   323
wenzelm@4795
   324
(** simplifier attributes **)
wenzelm@4795
   325
wenzelm@4795
   326
(* tags *)
wenzelm@4795
   327
wenzelm@4855
   328
val simpN = "simp";
wenzelm@4855
   329
val simp_addN = "add";
wenzelm@4855
   330
val simp_delN = "del";
wenzelm@4855
   331
wenzelm@4855
   332
val simp_tag = (simpN, []);
wenzelm@4855
   333
val simp_add = (simpN, [simp_addN]);
wenzelm@4855
   334
val simp_del = (simpN, [simp_delN]);
wenzelm@4795
   335
wenzelm@4795
   336
wenzelm@4855
   337
(* attributes *)
wenzelm@4795
   338
wenzelm@4855
   339
local
wenzelm@4855
   340
  fun simp_attr change args (x, tth) =
wenzelm@4855
   341
    if null args orelse args = [simp_addN] then change (op addsimps) (x, tth)
wenzelm@4855
   342
    else if args = [simp_delN] then change (op delsimps) (x, tth)
wenzelm@4855
   343
    else Attribute.fail simpN ("bad argument(s) " ^ commas_quote args);
wenzelm@4855
   344
wenzelm@4855
   345
  fun change_global_ss f (thy, tth) =
wenzelm@4855
   346
    let val r = simpset_ref_of thy
wenzelm@4855
   347
    in r := f (! r, [Attribute.thm_of tth]); (thy, tth) end;
wenzelm@4795
   348
wenzelm@4855
   349
  fun change_local_ss f (lthy, tth) =
wenzelm@4855
   350
    let val ss = f (get_local_simpset lthy, [Attribute.thm_of tth])
wenzelm@4855
   351
    in (put_local_simpset ss lthy, tth) end;
wenzelm@4795
   352
wenzelm@4855
   353
  val simp_attr_global = simp_attr change_global_ss;
wenzelm@4855
   354
  val simp_attr_local = simp_attr change_local_ss;
wenzelm@4855
   355
in
wenzelm@4855
   356
  val setup_attrs = Attribute.add_attrs [(simpN, (simp_attr_global, simp_attr_local))];
wenzelm@4795
   357
wenzelm@4855
   358
  val simp_add_global = simp_attr_global [simp_addN];
wenzelm@4855
   359
  val simp_del_global = simp_attr_global [simp_delN];
wenzelm@4855
   360
  val simp_add_local = simp_attr_local [simp_addN];
wenzelm@4855
   361
  val simp_del_local = simp_attr_local [simp_delN];
wenzelm@4855
   362
end;
wenzelm@4795
   363
wenzelm@4795
   364
wenzelm@3557
   365
wenzelm@2503
   366
(** simplification tactics **)
clasohm@0
   367
nipkow@4668
   368
fun solve_all_tac (subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac) mss =
wenzelm@3557
   369
  let
wenzelm@3557
   370
    val ss =
nipkow@4668
   371
      make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, unsafe_finish_tac);
nipkow@4612
   372
    val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
wenzelm@3557
   373
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   374
nipkow@4668
   375
fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
wenzelm@3557
   376
oheimb@2629
   377
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@2629
   378
fun basic_gen_simp_tac mode =
nipkow@4668
   379
  fn (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}) =>
wenzelm@3551
   380
    let
nipkow@4612
   381
      fun simp_loop_tac i =
nipkow@4612
   382
        asm_rewrite_goal_tac mode
nipkow@4668
   383
          (solve_all_tac (subgoal_tac,loop_tacs,finish_tac,unsafe_finish_tac))
nipkow@4612
   384
          mss i
nipkow@4612
   385
        THEN (finish_tac (prems_of_mss mss) i ORELSE
nipkow@4668
   386
              TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
nipkow@4612
   387
    in simp_loop_tac end;
clasohm@0
   388
wenzelm@3551
   389
fun gen_simp_tac mode (ss as Simpset {unsafe_finish_tac, ...}) =
wenzelm@3551
   390
  basic_gen_simp_tac mode (ss setSSolver unsafe_finish_tac);
wenzelm@3551
   391
nipkow@4713
   392
val          simp_tac = gen_simp_tac (false, false, false);
nipkow@4713
   393
val      asm_simp_tac = gen_simp_tac (false, true, false);
nipkow@4713
   394
val     full_simp_tac = gen_simp_tac (true,  false, false);
nipkow@4722
   395
val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
nipkow@4722
   396
val asm_full_simp_tac = gen_simp_tac (true,  true, true);
clasohm@0
   397
oheimb@2629
   398
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
nipkow@4713
   399
val safe_asm_full_simp_tac = basic_gen_simp_tac (true, true, false);
oheimb@2629
   400
wenzelm@4795
   401
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   402
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   403
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   404
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   405
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   406
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   407
wenzelm@3557
   408
wenzelm@4795
   409
wenzelm@3557
   410
(** simplification meta rules **)
wenzelm@3557
   411
nipkow@4668
   412
fun simp mode (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}) thm =
wenzelm@3557
   413
  let
nipkow@4668
   414
    val tacf = solve_all_tac (subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
wenzelm@4271
   415
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@3557
   416
  in
wenzelm@3557
   417
    Drule.rewrite_thm mode prover mss thm
wenzelm@3557
   418
  end;
wenzelm@3557
   419
nipkow@4713
   420
val          simplify = simp (false, false, false);
nipkow@4713
   421
val      asm_simplify = simp (false, true, false);
nipkow@4713
   422
val     full_simplify = simp (true, false, false);
nipkow@4713
   423
val asm_full_simplify = simp (true, true, false);
wenzelm@3557
   424
wenzelm@3557
   425
wenzelm@4795
   426
wenzelm@4795
   427
(** theory setup **)
wenzelm@4795
   428
wenzelm@4855
   429
val setup = [setup_thy_data, setup_attrs];
wenzelm@4795
   430
wenzelm@4795
   431
clasohm@1243
   432
end;
wenzelm@4795
   433
wenzelm@4795
   434
wenzelm@4795
   435
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   436
open BasicSimplifier;