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