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