src/Provers/simplifier.ML
author oheimb
Mon Aug 16 17:33:45 1999 +0200 (1999-08-16 ago)
changeset 7214 381d6987f68d
parent 7177 6bb7ad30f3da
child 7217 3af1e69b25b8
permissions -rw-r--r--
exchanged finish_tac and unsafe_finish_tac (the more important one) in simpset
basic_gen_simp_tac now takes a looper as _explicit_ argument
removed superfluous argument of solve_all_tac
corrected safe_asm_full_simp_tac: now also with mutual simplification of prems
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,
oheimb@7214
    25
    unsafe_finish_tac: thm list -> int -> tactic,
oheimb@7214
    26
           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@5082
    81
  val          rewrite: simpset -> cterm -> thm
wenzelm@5082
    82
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@5082
    83
  val     full_rewrite: simpset -> cterm -> thm
wenzelm@5082
    84
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@5842
    85
  val print_local_simpset: Proof.context -> unit
wenzelm@5842
    86
  val get_local_simpset: Proof.context -> simpset
wenzelm@5842
    87
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@4855
    88
  val simp_add_global: theory attribute
wenzelm@4855
    89
  val simp_del_global: theory attribute
wenzelm@6911
    90
  val simp_only_global: theory attribute
wenzelm@5842
    91
  val simp_add_local: Proof.context attribute
wenzelm@5842
    92
  val simp_del_local: Proof.context attribute
wenzelm@6911
    93
  val simp_only_local: Proof.context attribute
wenzelm@7177
    94
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@5928
    95
  val simp_modifiers: (Args.T list -> (Proof.context attribute * Args.T list)) list
wenzelm@5842
    96
  val setup: (theory -> theory) list
wenzelm@4795
    97
end;
wenzelm@2503
    98
wenzelm@2503
    99
structure Simplifier: SIMPLIFIER =
clasohm@0
   100
struct
clasohm@0
   101
wenzelm@2509
   102
wenzelm@2509
   103
(** simplification procedures **)
wenzelm@2509
   104
wenzelm@2509
   105
(* datatype simproc *)
wenzelm@2509
   106
wenzelm@2509
   107
datatype simproc =
wenzelm@3577
   108
  Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
wenzelm@2509
   109
wenzelm@3557
   110
fun mk_simproc name lhss proc =
wenzelm@3557
   111
  Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
wenzelm@3557
   112
wenzelm@3551
   113
fun rep_simproc (Simproc args) = args;
wenzelm@2509
   114
wenzelm@2509
   115
wenzelm@2509
   116
wenzelm@2503
   117
(** simplification sets **)
wenzelm@2503
   118
wenzelm@2503
   119
(* type simpset *)
wenzelm@2503
   120
clasohm@0
   121
datatype simpset =
wenzelm@2503
   122
  Simpset of {
wenzelm@2503
   123
    mss: meta_simpset,
oheimb@2629
   124
    subgoal_tac:        simpset -> int -> tactic,
nipkow@4668
   125
    loop_tacs:          (string * (int -> tactic))list,
oheimb@7214
   126
    unsafe_finish_tac: thm list -> int -> tactic,
oheimb@7214
   127
           finish_tac: thm list -> int -> tactic};
wenzelm@2503
   128
oheimb@7214
   129
fun make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac) =
nipkow@4668
   130
  Simpset {mss = mss, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
oheimb@7214
   131
    unsafe_finish_tac = unsafe_finish_tac, finish_tac = finish_tac};
clasohm@0
   132
clasohm@0
   133
val empty_ss =
wenzelm@6911
   134
  let val mss = Thm.set_mk_sym (Thm.empty_mss, Some o symmetric_fun)
nipkow@4677
   135
  in make_ss (mss, K (K no_tac), [], K (K no_tac), K (K no_tac)) end;
wenzelm@3551
   136
wenzelm@3551
   137
fun rep_ss (Simpset args) = args;
wenzelm@3551
   138
fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
wenzelm@3551
   139
wenzelm@3551
   140
wenzelm@3551
   141
(* print simpsets *)
wenzelm@2503
   142
wenzelm@3551
   143
fun print_ss ss =
wenzelm@3551
   144
  let
wenzelm@3551
   145
    val Simpset {mss, ...} = ss;
wenzelm@3551
   146
    val {simps, procs, congs} = Thm.dest_mss mss;
wenzelm@2503
   147
wenzelm@3551
   148
    val pretty_thms = map Display.pretty_thm;
wenzelm@3551
   149
    fun pretty_proc (name, lhss) =
wenzelm@3551
   150
      Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
wenzelm@3551
   151
  in
wenzelm@3551
   152
    Pretty.writeln (Pretty.big_list "simplification rules:" (pretty_thms simps));
wenzelm@3551
   153
    Pretty.writeln (Pretty.big_list "simplification procedures:" (map pretty_proc procs));
wenzelm@3551
   154
    Pretty.writeln (Pretty.big_list "congruences:" (pretty_thms congs))
wenzelm@3551
   155
  end;
wenzelm@2503
   156
wenzelm@2503
   157
wenzelm@2503
   158
(* extend simpsets *)
wenzelm@2503
   159
oheimb@7214
   160
fun (Simpset {mss, subgoal_tac = _, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   161
    setsubgoaler subgoal_tac =
oheimb@7214
   162
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
oheimb@2629
   163
oheimb@7214
   164
fun (Simpset {mss, subgoal_tac, loop_tacs = _, unsafe_finish_tac, finish_tac})
nipkow@4668
   165
    setloop tac =
oheimb@7214
   166
  make_ss (mss, subgoal_tac, [("",tac)], unsafe_finish_tac, finish_tac);
wenzelm@2503
   167
oheimb@7214
   168
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
oheimb@4741
   169
    addloop tac = make_ss (mss, subgoal_tac, 
wenzelm@5886
   170
        (case assoc_string (loop_tacs,(fst tac)) of None => () | Some x => 
wenzelm@5886
   171
         warning ("overwriting looper "^fst tac); overwrite(loop_tacs,tac)),
oheimb@7214
   172
           unsafe_finish_tac, finish_tac);
oheimb@2567
   173
oheimb@7214
   174
fun (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
oheimb@7214
   175
 delloop name =
wenzelm@6911
   176
  let val (del, rest) = partition (fn (n, _) => n = name) loop_tacs in
wenzelm@6911
   177
    if null del then (warning ("No such looper in simpset: " ^ name); ss)
oheimb@7214
   178
    else make_ss (mss, subgoal_tac, rest, unsafe_finish_tac, finish_tac)
nipkow@4682
   179
  end;
oheimb@7214
   180
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, ...})
oheimb@7214
   181
    setSSolver finish_tac =
oheimb@7214
   182
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
nipkow@4682
   183
oheimb@7214
   184
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   185
    addSSolver tac =
oheimb@7214
   186
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac,
oheimb@7214
   187
	fn hyps => finish_tac hyps ORELSE' tac hyps);
wenzelm@2503
   188
oheimb@7214
   189
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac = _, finish_tac})
wenzelm@3551
   190
    setSolver unsafe_finish_tac =
oheimb@7214
   191
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
wenzelm@2503
   192
oheimb@7214
   193
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   194
    addSolver tac =
oheimb@7214
   195
  make_ss (mss, subgoal_tac, loop_tacs, 
oheimb@7214
   196
    fn hyps => unsafe_finish_tac hyps ORELSE' tac hyps, finish_tac);
wenzelm@2503
   197
oheimb@7214
   198
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   199
    setmksimps mk_simps =
wenzelm@2645
   200
  make_ss (Thm.set_mk_rews (mss, map (Thm.strip_shyps o Drule.zero_var_indexes) o mk_simps),
oheimb@7214
   201
    subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
wenzelm@2509
   202
oheimb@7214
   203
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
nipkow@4677
   204
    setmkeqTrue mk_eq_True =
nipkow@4677
   205
  make_ss (Thm.set_mk_eq_True (mss, mk_eq_True),
oheimb@7214
   206
    subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
nipkow@4677
   207
oheimb@7214
   208
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
nipkow@4677
   209
    setmksym mksym =
nipkow@4677
   210
  make_ss (Thm.set_mk_sym (mss, mksym),
oheimb@7214
   211
    subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
nipkow@4677
   212
oheimb@7214
   213
fun (Simpset {mss, subgoal_tac, loop_tacs,  unsafe_finish_tac, finish_tac})
wenzelm@3551
   214
    settermless termless =
nipkow@4668
   215
  make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tacs,
oheimb@7214
   216
    unsafe_finish_tac, finish_tac);
wenzelm@3551
   217
oheimb@7214
   218
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   219
    addsimps rews =
oheimb@7214
   220
  make_ss (Thm.add_simps (mss, rews), subgoal_tac, loop_tacs, 
oheimb@7214
   221
	   unsafe_finish_tac, finish_tac);
wenzelm@2503
   222
oheimb@7214
   223
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   224
    delsimps rews =
oheimb@7214
   225
  make_ss (Thm.del_simps (mss, rews), subgoal_tac, loop_tacs, 
oheimb@7214
   226
	   unsafe_finish_tac, finish_tac);
wenzelm@2503
   227
oheimb@7214
   228
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   229
    addeqcongs newcongs =
oheimb@7214
   230
  make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tacs, 
oheimb@7214
   231
	   unsafe_finish_tac, finish_tac);
wenzelm@2509
   232
oheimb@7214
   233
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   234
    deleqcongs oldcongs =
oheimb@7214
   235
  make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tacs, 
oheimb@7214
   236
	   unsafe_finish_tac, finish_tac);
oheimb@2629
   237
oheimb@7214
   238
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   239
    addsimprocs simprocs =
wenzelm@3551
   240
  make_ss
wenzelm@3551
   241
    (Thm.add_simprocs (mss, map rep_simproc simprocs),
oheimb@7214
   242
      subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
wenzelm@2509
   243
oheimb@7214
   244
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac})
wenzelm@3551
   245
    delsimprocs simprocs =
wenzelm@3551
   246
  make_ss
wenzelm@3551
   247
    (Thm.del_simprocs (mss, map rep_simproc simprocs),
oheimb@7214
   248
      subgoal_tac, loop_tacs, unsafe_finish_tac, finish_tac);
wenzelm@2503
   249
wenzelm@6911
   250
fun onlysimps (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}, rews) =
wenzelm@6911
   251
  make_ss (Thm.clear_mss mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac)
wenzelm@6911
   252
    addsimps rews;
wenzelm@6911
   253
wenzelm@2503
   254
wenzelm@5886
   255
(* merge simpsets *)    (*NOTE: ignores tactics of 2nd simpset (except loopers)*)
wenzelm@2503
   256
wenzelm@3551
   257
fun merge_ss
oheimb@7214
   258
   (Simpset {mss = mss1, loop_tacs = loop_tacs1, subgoal_tac, unsafe_finish_tac, finish_tac},
nipkow@4668
   259
    Simpset {mss = mss2, loop_tacs = loop_tacs2, ...}) =
nipkow@4668
   260
  make_ss (Thm.merge_mss (mss1, mss2), subgoal_tac,
oheimb@7214
   261
    merge_alists loop_tacs1 loop_tacs2, unsafe_finish_tac, finish_tac);
wenzelm@2503
   262
wenzelm@2503
   263
wenzelm@3557
   264
wenzelm@5024
   265
(** global and local simpset data **)
wenzelm@4080
   266
wenzelm@5842
   267
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   268
wenzelm@5842
   269
structure GlobalSimpsetArgs =
wenzelm@5024
   270
struct
wenzelm@5842
   271
  val name = "Provers/simpset";
wenzelm@5024
   272
  type T = simpset ref;
clasohm@0
   273
wenzelm@5024
   274
  val empty = ref empty_ss;
wenzelm@6556
   275
  fun copy (ref ss) = (ref ss): T;            (*create new reference!*)
wenzelm@6556
   276
  val prep_ext = copy;
wenzelm@5024
   277
  fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@5024
   278
  fun print _ (ref ss) = print_ss ss;
wenzelm@5024
   279
end;
wenzelm@4080
   280
wenzelm@5842
   281
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   282
val print_simpset = GlobalSimpset.print;
wenzelm@5842
   283
val simpset_ref_of_sg = GlobalSimpset.get_sg;
wenzelm@5842
   284
val simpset_ref_of = GlobalSimpset.get;
wenzelm@4080
   285
wenzelm@4080
   286
wenzelm@4795
   287
(* access global simpset *)
clasohm@0
   288
wenzelm@4080
   289
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   290
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   291
wenzelm@6391
   292
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   293
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   294
wenzelm@5028
   295
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   296
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   297
wenzelm@4080
   298
wenzelm@4795
   299
(* change global simpset *)
wenzelm@4080
   300
wenzelm@7177
   301
fun change_simpset_of f x thy =
wenzelm@7177
   302
  let val r = simpset_ref_of thy
wenzelm@7177
   303
  in r := f (! r, x); thy end;
wenzelm@7177
   304
wenzelm@7177
   305
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   306
wenzelm@4080
   307
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   308
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   309
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   310
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@2509
   311
clasohm@0
   312
wenzelm@5842
   313
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   314
wenzelm@5842
   315
structure LocalSimpsetArgs =
wenzelm@5842
   316
struct
wenzelm@5842
   317
  val name = "Provers/simpset";
wenzelm@5842
   318
  type T = simpset;
wenzelm@5842
   319
  val init = simpset_of;
wenzelm@5842
   320
  fun print _ ss = print_ss ss;
wenzelm@5842
   321
end;
wenzelm@4795
   322
wenzelm@5842
   323
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   324
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   325
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   326
val put_local_simpset = LocalSimpset.put;
wenzelm@4795
   327
wenzelm@4795
   328
wenzelm@5886
   329
(* attributes *)
wenzelm@5886
   330
wenzelm@5886
   331
fun change_global_ss f (thy, th) =
wenzelm@5886
   332
  let val r = simpset_ref_of thy
wenzelm@6096
   333
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   334
wenzelm@5886
   335
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   336
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   337
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   338
wenzelm@5886
   339
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   340
val simp_del_global = change_global_ss (op delsimps);
wenzelm@6911
   341
val simp_only_global = change_global_ss onlysimps;
wenzelm@5886
   342
val simp_add_local = change_local_ss (op addsimps);
wenzelm@5886
   343
val simp_del_local = change_local_ss (op delsimps);
wenzelm@6911
   344
val simp_only_local = change_local_ss onlysimps;
wenzelm@5886
   345
wenzelm@5886
   346
wenzelm@3557
   347
wenzelm@2503
   348
(** simplification tactics **)
clasohm@0
   349
oheimb@7214
   350
fun solve_all_tac (subgoal_tac, loop_tacs, unsafe_finish_tac) mss =
wenzelm@3557
   351
  let
wenzelm@3557
   352
    val ss =
oheimb@7214
   353
      make_ss (mss, subgoal_tac, loop_tacs,unsafe_finish_tac,unsafe_finish_tac);
nipkow@4612
   354
    val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
wenzelm@3557
   355
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   356
nipkow@4668
   357
fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
wenzelm@3557
   358
oheimb@7214
   359
(*unsafe: may instantiate unknowns that appear also in other subgoals*)
oheimb@7214
   360
fun basic_gen_simp_tac mode finish_tac =
oheimb@7214
   361
  fn (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, ...}) =>
wenzelm@3551
   362
    let
nipkow@4612
   363
      fun simp_loop_tac i =
nipkow@4612
   364
        asm_rewrite_goal_tac mode
oheimb@7214
   365
          (solve_all_tac (subgoal_tac,loop_tacs,unsafe_finish_tac))
nipkow@4612
   366
          mss i
nipkow@4612
   367
        THEN (finish_tac (prems_of_mss mss) i ORELSE
nipkow@4668
   368
              TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
nipkow@4612
   369
    in simp_loop_tac end;
clasohm@0
   370
oheimb@7214
   371
fun gen_simp_tac mode 
oheimb@7214
   372
      (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, ...}) =
oheimb@7214
   373
  basic_gen_simp_tac mode unsafe_finish_tac ss;
wenzelm@3551
   374
nipkow@4713
   375
val          simp_tac = gen_simp_tac (false, false, false);
nipkow@4713
   376
val      asm_simp_tac = gen_simp_tac (false, true, false);
nipkow@4713
   377
val     full_simp_tac = gen_simp_tac (true,  false, false);
nipkow@4722
   378
val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
nipkow@4722
   379
val asm_full_simp_tac = gen_simp_tac (true,  true, true);
clasohm@0
   380
oheimb@2629
   381
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@7214
   382
fun safe_asm_full_simp_tac (ss as Simpset {mss, subgoal_tac, loop_tacs, 
oheimb@7214
   383
	unsafe_finish_tac, finish_tac}) = 
oheimb@7214
   384
  basic_gen_simp_tac (true, true, true) finish_tac ss;
oheimb@2629
   385
wenzelm@4795
   386
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   387
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   388
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   389
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   390
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   391
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   392
wenzelm@3557
   393
wenzelm@4795
   394
wenzelm@5082
   395
(** simplification rules and conversions **)
wenzelm@3557
   396
oheimb@7214
   397
fun simp rew mode 
oheimb@7214
   398
     (Simpset {mss, subgoal_tac, loop_tacs, unsafe_finish_tac, ...}) thm =
wenzelm@3557
   399
  let
oheimb@7214
   400
    val tacf = solve_all_tac (subgoal_tac, loop_tacs, unsafe_finish_tac);
wenzelm@4271
   401
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@5082
   402
  in rew mode prover mss thm end;
wenzelm@5082
   403
wenzelm@5082
   404
val simp_thm = simp Drule.rewrite_thm;
wenzelm@5082
   405
val simp_cterm = simp Drule.rewrite_cterm;
wenzelm@3557
   406
wenzelm@5082
   407
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   408
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   409
val     full_simplify = simp_thm (true, false, false);
wenzelm@5082
   410
val asm_full_simplify = simp_thm (true, true, false);
wenzelm@5082
   411
wenzelm@5082
   412
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   413
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   414
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@5082
   415
val asm_full_rewrite = simp_cterm (true, true, false);
wenzelm@3557
   416
wenzelm@3557
   417
wenzelm@4795
   418
wenzelm@5886
   419
(** concrete syntax of attributes **)
wenzelm@5842
   420
wenzelm@5886
   421
(* add / del *)
wenzelm@5842
   422
wenzelm@5928
   423
val simpN = "simp";
wenzelm@6911
   424
val asm_simpN = "asm_simp";
wenzelm@5928
   425
val addN = "add";
wenzelm@5928
   426
val delN = "del";
wenzelm@6911
   427
val onlyN = "only";
wenzelm@5928
   428
val otherN = "other";
wenzelm@5842
   429
wenzelm@5886
   430
fun simp_att change =
wenzelm@5928
   431
  (Args.$$$ addN >> K (op addsimps) ||
wenzelm@5928
   432
    Args.$$$ delN >> K (op delsimps) ||
wenzelm@6911
   433
    Args.$$$ onlyN >> K onlysimps ||
wenzelm@5886
   434
    Scan.succeed (op addsimps))
wenzelm@5886
   435
  >> change
wenzelm@5886
   436
  |> Scan.lift
wenzelm@5886
   437
  |> Attrib.syntax;
wenzelm@5842
   438
wenzelm@5886
   439
val simp_attr = (simp_att change_global_ss, simp_att change_local_ss);
wenzelm@5842
   440
wenzelm@5842
   441
wenzelm@5842
   442
(* conversions *)
wenzelm@5842
   443
wenzelm@5842
   444
fun conv_attr f =
wenzelm@6096
   445
  (Attrib.no_args (Drule.rule_attribute (f o simpset_of)),
wenzelm@6096
   446
    Attrib.no_args (Drule.rule_attribute (f o get_local_simpset)));
wenzelm@5842
   447
wenzelm@5842
   448
wenzelm@5842
   449
(* setup_attrs *)
wenzelm@5842
   450
wenzelm@5842
   451
val setup_attrs = Attrib.add_attributes
wenzelm@5928
   452
 [(simpN,               simp_attr, "simplification rule"),
wenzelm@5842
   453
  ("simplify",          conv_attr simplify, "simplify rule"),
wenzelm@5842
   454
  ("asm_simplify",      conv_attr asm_simplify, "simplify rule, using assumptions"),
wenzelm@5842
   455
  ("full_simplify",     conv_attr full_simplify, "fully simplify rule"),
wenzelm@5842
   456
  ("asm_full_simplify", conv_attr asm_full_simplify, "fully simplify rule, using assumptions")];
wenzelm@5842
   457
wenzelm@5842
   458
wenzelm@5842
   459
wenzelm@5842
   460
(** proof methods **)
wenzelm@5842
   461
wenzelm@5842
   462
(* simplification *)
wenzelm@5842
   463
wenzelm@5928
   464
val simp_modifiers =
wenzelm@5928
   465
 [Args.$$$ simpN -- Args.$$$ addN >> K simp_add_local,
wenzelm@5928
   466
  Args.$$$ simpN -- Args.$$$ delN >> K simp_del_local,
wenzelm@6911
   467
  Args.$$$ simpN -- Args.$$$ onlyN >> K simp_only_local,
wenzelm@6911
   468
  Args.$$$ otherN >> K I];
wenzelm@5928
   469
wenzelm@5928
   470
val simp_modifiers' =
wenzelm@5928
   471
 [Args.$$$ addN >> K simp_add_local,
wenzelm@5928
   472
  Args.$$$ delN >> K simp_del_local,
wenzelm@6911
   473
  Args.$$$ onlyN >> K simp_only_local,
wenzelm@5928
   474
  Args.$$$ otherN >> K I];
wenzelm@5928
   475
wenzelm@5928
   476
val simp_args = Method.only_sectioned_args simp_modifiers';
wenzelm@5842
   477
wenzelm@6911
   478
fun simp_meth thin cut tac ctxt = Method.METHOD (fn facts =>
wenzelm@6534
   479
  FIRSTGOAL
wenzelm@6911
   480
    ((if thin then REPEAT_DETERM o etac Drule.thin_rl else K all_tac)
wenzelm@6994
   481
      THEN' (if cut then Method.same_tac facts else K all_tac)
wenzelm@6534
   482
      THEN' tac (get_local_simpset ctxt)));
wenzelm@5842
   483
wenzelm@6911
   484
val simp_method = simp_args ooo simp_meth;
wenzelm@5842
   485
wenzelm@5842
   486
wenzelm@5842
   487
(* setup_methods *)
wenzelm@5842
   488
wenzelm@5842
   489
val setup_methods = Method.add_methods
wenzelm@6911
   490
 [(simpN,               simp_method true true asm_full_simp_tac, "simplification"),
wenzelm@6911
   491
  (asm_simpN,           simp_method false true asm_full_simp_tac,
wenzelm@6911
   492
    "simplification (including assumption)"),
wenzelm@7132
   493
  ("simp_tac",          simp_method false false simp_tac, "simp_tac (improper!)"),
wenzelm@7132
   494
  ("asm_simp_tac",      simp_method false false asm_simp_tac, "asm_simp_tac (improper!)"),
wenzelm@7132
   495
  ("full_simp_tac",     simp_method false false full_simp_tac, "full_simp_tac (improper!)"),
wenzelm@7132
   496
  ("asm_full_simp_tac", simp_method false false asm_full_simp_tac, "asm_full_simp_tac (improper!)"),
wenzelm@7132
   497
  ("asm_lr_simp_tac",   simp_method false false asm_lr_simp_tac, "asm_lr_simp_tac (improper!)")];
wenzelm@5842
   498
wenzelm@5842
   499
wenzelm@5842
   500
wenzelm@4795
   501
(** theory setup **)
wenzelm@4795
   502
wenzelm@5842
   503
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs, setup_methods];
wenzelm@4795
   504
wenzelm@4795
   505
clasohm@1243
   506
end;
wenzelm@4795
   507
wenzelm@4795
   508
wenzelm@4795
   509
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   510
open BasicSimplifier;