src/Provers/simplifier.ML
author wenzelm
Sat Jan 29 14:22:16 2000 +0100 (2000-01-29 ago)
changeset 8170 4b9451fae406
parent 8168 9d2ac5439089
child 8228 8283e416b680
permissions -rw-r--r--
simp_all 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
nipkow@7568
    19
  type solver
nipkow@7568
    20
  val mk_solver: string -> (thm list -> int -> tactic) -> solver
clasohm@0
    21
  type simpset
wenzelm@2503
    22
  val empty_ss: simpset
wenzelm@3551
    23
  val rep_ss: simpset ->
wenzelm@3551
    24
   {mss: meta_simpset,
wenzelm@3551
    25
    subgoal_tac:        simpset -> int -> tactic,
nipkow@4668
    26
    loop_tacs:          (string * (int -> tactic))list,
nipkow@7568
    27
    unsafe_solvers: solver list,
nipkow@7568
    28
           solvers: solver list};
wenzelm@3551
    29
  val print_ss: simpset -> unit
wenzelm@4366
    30
  val print_simpset: theory -> unit
oheimb@2629
    31
  val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
oheimb@2629
    32
  val setloop:      simpset *             (int -> tactic) -> simpset
nipkow@4668
    33
  val addloop:      simpset *  (string * (int -> tactic)) -> simpset
nipkow@4682
    34
  val delloop:      simpset *   string                    -> simpset
nipkow@7568
    35
  val setSSolver:   simpset * solver -> simpset
nipkow@7568
    36
  val addSSolver:   simpset * solver -> simpset
nipkow@7568
    37
  val setSolver:    simpset * solver -> simpset
nipkow@7568
    38
  val addSolver:    simpset * solver -> simpset
wenzelm@3577
    39
  val setmksimps:   simpset * (thm -> thm list) -> simpset
nipkow@4677
    40
  val setmkeqTrue:  simpset * (thm -> thm option) -> simpset
nipkow@4677
    41
  val setmksym:     simpset * (thm -> thm option) -> simpset
wenzelm@3577
    42
  val settermless:  simpset * (term * term -> bool) -> simpset
wenzelm@3577
    43
  val addsimps:     simpset * thm list -> simpset
wenzelm@3577
    44
  val delsimps:     simpset * thm list -> simpset
wenzelm@3577
    45
  val addeqcongs:   simpset * thm list -> simpset
wenzelm@3577
    46
  val deleqcongs:   simpset * thm list -> simpset
wenzelm@3577
    47
  val addsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    48
  val delsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    49
  val merge_ss:     simpset * simpset -> simpset
wenzelm@3577
    50
  val prems_of_ss:  simpset -> thm list
wenzelm@4080
    51
  val simpset_ref_of_sg: Sign.sg -> simpset ref
wenzelm@4080
    52
  val simpset_ref_of: theory -> simpset ref
wenzelm@4080
    53
  val simpset_of_sg: Sign.sg -> simpset
wenzelm@4080
    54
  val simpset_of: theory -> simpset
wenzelm@4080
    55
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@4080
    56
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4080
    57
  val simpset: unit -> simpset
wenzelm@4080
    58
  val simpset_ref: unit -> simpset ref
clasohm@1243
    59
  val Addsimps: thm list -> unit
clasohm@1243
    60
  val Delsimps: thm list -> unit
wenzelm@2509
    61
  val Addsimprocs: simproc list -> unit
wenzelm@2509
    62
  val Delsimprocs: simproc list -> unit
oheimb@2629
    63
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    64
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    65
  val          full_simp_tac: simpset -> int -> tactic
nipkow@4722
    66
  val        asm_lr_simp_tac: simpset -> int -> tactic
oheimb@2629
    67
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    68
  val safe_asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    69
  val               Simp_tac:            int -> tactic
oheimb@2629
    70
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    71
  val          Full_simp_tac:            int -> tactic
nipkow@4722
    72
  val        Asm_lr_simp_tac:            int -> tactic
oheimb@2629
    73
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    74
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    75
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    76
  val     full_simplify: simpset -> thm -> thm
wenzelm@3557
    77
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    78
end;
clasohm@0
    79
wenzelm@4795
    80
signature SIMPLIFIER =
wenzelm@4795
    81
sig
wenzelm@4795
    82
  include BASIC_SIMPLIFIER
wenzelm@5082
    83
  val          rewrite: simpset -> cterm -> thm
wenzelm@5082
    84
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@5082
    85
  val     full_rewrite: simpset -> cterm -> thm
wenzelm@5082
    86
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@5842
    87
  val print_local_simpset: Proof.context -> unit
wenzelm@5842
    88
  val get_local_simpset: Proof.context -> simpset
wenzelm@5842
    89
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@4855
    90
  val simp_add_global: theory attribute
wenzelm@4855
    91
  val simp_del_global: theory attribute
wenzelm@5842
    92
  val simp_add_local: Proof.context attribute
wenzelm@5842
    93
  val simp_del_local: Proof.context attribute
wenzelm@7177
    94
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@7273
    95
  val simp_modifiers: (Args.T list -> (Method.modifier * 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
nipkow@7568
   115
(** solvers **)
wenzelm@2509
   116
nipkow@7568
   117
datatype solver = Solver of string * (thm list -> int -> tactic) * stamp;
nipkow@7568
   118
nipkow@7568
   119
fun mk_solver name solver = Solver(name, solver, stamp());
nipkow@7568
   120
nipkow@7568
   121
fun eq_solver (Solver(_,_,s1),Solver(_,_,s2)) = s1=s2;
nipkow@7568
   122
nipkow@7568
   123
val merge_solvers = generic_merge eq_solver I I;
nipkow@7568
   124
nipkow@7568
   125
fun app_sols [] _ _ = no_tac
nipkow@7568
   126
  | app_sols (Solver(_,solver,_)::sols) thms i =
nipkow@7568
   127
       solver thms i ORELSE app_sols sols thms i;
wenzelm@2509
   128
wenzelm@2503
   129
(** simplification sets **)
wenzelm@2503
   130
wenzelm@2503
   131
(* type simpset *)
wenzelm@2503
   132
clasohm@0
   133
datatype simpset =
wenzelm@2503
   134
  Simpset of {
wenzelm@2503
   135
    mss: meta_simpset,
oheimb@2629
   136
    subgoal_tac:        simpset -> int -> tactic,
nipkow@4668
   137
    loop_tacs:          (string * (int -> tactic))list,
nipkow@7568
   138
    unsafe_solvers: solver list,
nipkow@7568
   139
           solvers: solver list};
wenzelm@2503
   140
nipkow@7568
   141
fun make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =
nipkow@4668
   142
  Simpset {mss = mss, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
nipkow@7568
   143
    unsafe_solvers = unsafe_solvers, solvers = solvers};
clasohm@0
   144
clasohm@0
   145
val empty_ss =
wenzelm@6911
   146
  let val mss = Thm.set_mk_sym (Thm.empty_mss, Some o symmetric_fun)
nipkow@7568
   147
  in make_ss (mss, K (K no_tac), [], [], []) end;
wenzelm@3551
   148
wenzelm@3551
   149
fun rep_ss (Simpset args) = args;
wenzelm@3551
   150
fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
wenzelm@3551
   151
wenzelm@3551
   152
wenzelm@3551
   153
(* print simpsets *)
wenzelm@2503
   154
wenzelm@3551
   155
fun print_ss ss =
wenzelm@3551
   156
  let
wenzelm@3551
   157
    val Simpset {mss, ...} = ss;
wenzelm@3551
   158
    val {simps, procs, congs} = Thm.dest_mss mss;
wenzelm@2503
   159
wenzelm@3551
   160
    val pretty_thms = map Display.pretty_thm;
wenzelm@3551
   161
    fun pretty_proc (name, lhss) =
wenzelm@3551
   162
      Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
wenzelm@3551
   163
  in
wenzelm@3551
   164
    Pretty.writeln (Pretty.big_list "simplification rules:" (pretty_thms simps));
wenzelm@3551
   165
    Pretty.writeln (Pretty.big_list "simplification procedures:" (map pretty_proc procs));
wenzelm@3551
   166
    Pretty.writeln (Pretty.big_list "congruences:" (pretty_thms congs))
wenzelm@3551
   167
  end;
wenzelm@2503
   168
wenzelm@2503
   169
wenzelm@2503
   170
(* extend simpsets *)
wenzelm@2503
   171
nipkow@7568
   172
fun (Simpset {mss, subgoal_tac = _, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   173
    setsubgoaler subgoal_tac =
nipkow@7568
   174
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers);
oheimb@2629
   175
nipkow@7568
   176
fun (Simpset {mss, subgoal_tac, loop_tacs = _, unsafe_solvers, solvers})
nipkow@4668
   177
    setloop tac =
nipkow@7568
   178
  make_ss (mss, subgoal_tac, [("",tac)], unsafe_solvers, solvers);
wenzelm@2503
   179
nipkow@7568
   180
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
oheimb@4741
   181
    addloop tac = make_ss (mss, subgoal_tac, 
wenzelm@5886
   182
        (case assoc_string (loop_tacs,(fst tac)) of None => () | Some x => 
wenzelm@5886
   183
         warning ("overwriting looper "^fst tac); overwrite(loop_tacs,tac)),
nipkow@7568
   184
           unsafe_solvers, solvers);
oheimb@2567
   185
nipkow@7568
   186
fun (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
oheimb@7214
   187
 delloop name =
wenzelm@6911
   188
  let val (del, rest) = partition (fn (n, _) => n = name) loop_tacs in
wenzelm@6911
   189
    if null del then (warning ("No such looper in simpset: " ^ name); ss)
nipkow@7568
   190
    else make_ss (mss, subgoal_tac, rest, unsafe_solvers, solvers)
nipkow@4682
   191
  end;
nipkow@4682
   192
nipkow@7568
   193
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...})
nipkow@7568
   194
    setSSolver solver =
nipkow@7568
   195
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers, [solver]);
wenzelm@2503
   196
nipkow@7568
   197
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@7568
   198
    addSSolver sol =
nipkow@7568
   199
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers,
nipkow@7568
   200
           merge_solvers solvers [sol]);
wenzelm@2503
   201
nipkow@7568
   202
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers = _, solvers})
nipkow@7568
   203
    setSolver unsafe_solver =
nipkow@7568
   204
  make_ss (mss, subgoal_tac, loop_tacs, [unsafe_solver], solvers);
nipkow@7568
   205
nipkow@7568
   206
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@7568
   207
    addSolver sol =
oheimb@7214
   208
  make_ss (mss, subgoal_tac, loop_tacs, 
nipkow@7568
   209
    merge_solvers unsafe_solvers [sol], solvers);
wenzelm@2503
   210
nipkow@7568
   211
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   212
    setmksimps mk_simps =
wenzelm@7646
   213
  make_ss (Thm.set_mk_rews (mss, mk_simps), subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2509
   214
nipkow@7568
   215
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   216
    setmkeqTrue mk_eq_True =
nipkow@4677
   217
  make_ss (Thm.set_mk_eq_True (mss, mk_eq_True),
nipkow@7568
   218
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
nipkow@4677
   219
nipkow@7568
   220
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   221
    setmksym mksym =
nipkow@4677
   222
  make_ss (Thm.set_mk_sym (mss, mksym),
nipkow@7568
   223
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
nipkow@4677
   224
nipkow@7568
   225
fun (Simpset {mss, subgoal_tac, loop_tacs,  unsafe_solvers, solvers})
wenzelm@3551
   226
    settermless termless =
nipkow@4668
   227
  make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tacs,
nipkow@7568
   228
    unsafe_solvers, solvers);
wenzelm@3551
   229
nipkow@7568
   230
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   231
    addsimps rews =
oheimb@7214
   232
  make_ss (Thm.add_simps (mss, rews), subgoal_tac, loop_tacs, 
nipkow@7568
   233
	   unsafe_solvers, solvers);
wenzelm@2503
   234
nipkow@7568
   235
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   236
    delsimps rews =
oheimb@7214
   237
  make_ss (Thm.del_simps (mss, rews), subgoal_tac, loop_tacs, 
nipkow@7568
   238
	   unsafe_solvers, solvers);
wenzelm@2503
   239
nipkow@7568
   240
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   241
    addeqcongs newcongs =
oheimb@7214
   242
  make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tacs, 
nipkow@7568
   243
	   unsafe_solvers, solvers);
wenzelm@2509
   244
nipkow@7568
   245
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   246
    deleqcongs oldcongs =
oheimb@7214
   247
  make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tacs, 
nipkow@7568
   248
	   unsafe_solvers, solvers);
oheimb@2629
   249
nipkow@7568
   250
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   251
    addsimprocs simprocs =
wenzelm@3551
   252
  make_ss
wenzelm@3551
   253
    (Thm.add_simprocs (mss, map rep_simproc simprocs),
nipkow@7568
   254
      subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2509
   255
nipkow@7568
   256
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   257
    delsimprocs simprocs =
wenzelm@3551
   258
  make_ss
wenzelm@3551
   259
    (Thm.del_simprocs (mss, map rep_simproc simprocs),
nipkow@7568
   260
      subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2503
   261
nipkow@7568
   262
fun clear_ss (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers}) =
nipkow@7568
   263
  make_ss (Thm.clear_mss mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@6911
   264
wenzelm@2503
   265
nipkow@7568
   266
(* merge simpsets *)
nipkow@7568
   267
(*NOTE: ignores subgoal_tac of 2nd simpset *)
wenzelm@2503
   268
wenzelm@3551
   269
fun merge_ss
nipkow@7568
   270
   (Simpset {mss = mss1, loop_tacs = loop_tacs1, subgoal_tac,
nipkow@7568
   271
             unsafe_solvers = unsafe_solvers1, solvers = solvers1},
nipkow@7568
   272
    Simpset {mss = mss2, loop_tacs = loop_tacs2,
nipkow@7568
   273
             unsafe_solvers = unsafe_solvers2, solvers = solvers2, ...}) =
nipkow@4668
   274
  make_ss (Thm.merge_mss (mss1, mss2), subgoal_tac,
nipkow@7568
   275
    merge_alists loop_tacs1 loop_tacs2,
nipkow@7568
   276
    merge_solvers unsafe_solvers1 unsafe_solvers2,
nipkow@7568
   277
    merge_solvers solvers1 solvers2);
wenzelm@2503
   278
wenzelm@3557
   279
wenzelm@5024
   280
(** global and local simpset data **)
wenzelm@4080
   281
wenzelm@5842
   282
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   283
wenzelm@5842
   284
structure GlobalSimpsetArgs =
wenzelm@5024
   285
struct
wenzelm@5842
   286
  val name = "Provers/simpset";
wenzelm@5024
   287
  type T = simpset ref;
clasohm@0
   288
wenzelm@5024
   289
  val empty = ref empty_ss;
wenzelm@6556
   290
  fun copy (ref ss) = (ref ss): T;            (*create new reference!*)
wenzelm@6556
   291
  val prep_ext = copy;
wenzelm@5024
   292
  fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@5024
   293
  fun print _ (ref ss) = print_ss ss;
wenzelm@5024
   294
end;
wenzelm@4080
   295
wenzelm@5842
   296
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   297
val print_simpset = GlobalSimpset.print;
wenzelm@5842
   298
val simpset_ref_of_sg = GlobalSimpset.get_sg;
wenzelm@5842
   299
val simpset_ref_of = GlobalSimpset.get;
wenzelm@4080
   300
wenzelm@4080
   301
wenzelm@4795
   302
(* access global simpset *)
clasohm@0
   303
wenzelm@4080
   304
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   305
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   306
wenzelm@6391
   307
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   308
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   309
wenzelm@5028
   310
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   311
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   312
wenzelm@4080
   313
wenzelm@4795
   314
(* change global simpset *)
wenzelm@4080
   315
wenzelm@7177
   316
fun change_simpset_of f x thy =
wenzelm@7177
   317
  let val r = simpset_ref_of thy
wenzelm@7177
   318
  in r := f (! r, x); thy end;
wenzelm@7177
   319
wenzelm@7177
   320
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   321
wenzelm@4080
   322
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   323
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   324
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   325
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@2509
   326
clasohm@0
   327
wenzelm@5842
   328
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   329
wenzelm@5842
   330
structure LocalSimpsetArgs =
wenzelm@5842
   331
struct
wenzelm@5842
   332
  val name = "Provers/simpset";
wenzelm@5842
   333
  type T = simpset;
wenzelm@5842
   334
  val init = simpset_of;
wenzelm@5842
   335
  fun print _ ss = print_ss ss;
wenzelm@5842
   336
end;
wenzelm@4795
   337
wenzelm@5842
   338
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   339
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   340
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   341
val put_local_simpset = LocalSimpset.put;
wenzelm@7273
   342
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@4795
   343
wenzelm@4795
   344
wenzelm@5886
   345
(* attributes *)
wenzelm@5886
   346
wenzelm@5886
   347
fun change_global_ss f (thy, th) =
wenzelm@5886
   348
  let val r = simpset_ref_of thy
wenzelm@6096
   349
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   350
wenzelm@5886
   351
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   352
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   353
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   354
wenzelm@5886
   355
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   356
val simp_del_global = change_global_ss (op delsimps);
wenzelm@5886
   357
val simp_add_local = change_local_ss (op addsimps);
wenzelm@5886
   358
val simp_del_local = change_local_ss (op delsimps);
wenzelm@5886
   359
wenzelm@5886
   360
wenzelm@3557
   361
wenzelm@2503
   362
(** simplification tactics **)
clasohm@0
   363
nipkow@7568
   364
fun solve_all_tac (subgoal_tac, loop_tacs, unsafe_solvers) mss =
wenzelm@3557
   365
  let
wenzelm@3557
   366
    val ss =
nipkow@7568
   367
      make_ss (mss, subgoal_tac, loop_tacs,unsafe_solvers,unsafe_solvers);
nipkow@4612
   368
    val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
wenzelm@3557
   369
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   370
nipkow@4668
   371
fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
wenzelm@3557
   372
oheimb@7214
   373
(*unsafe: may instantiate unknowns that appear also in other subgoals*)
nipkow@7568
   374
fun basic_gen_simp_tac mode solvers =
nipkow@7568
   375
  fn (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) =>
wenzelm@3551
   376
    let
nipkow@4612
   377
      fun simp_loop_tac i =
nipkow@4612
   378
        asm_rewrite_goal_tac mode
nipkow@7568
   379
          (solve_all_tac (subgoal_tac,loop_tacs,unsafe_solvers))
nipkow@4612
   380
          mss i
nipkow@7568
   381
        THEN (solvers (prems_of_mss mss) i ORELSE
nipkow@4668
   382
              TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
nipkow@4612
   383
    in simp_loop_tac end;
clasohm@0
   384
oheimb@7214
   385
fun gen_simp_tac mode 
nipkow@7568
   386
      (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) =
nipkow@7568
   387
  basic_gen_simp_tac mode (app_sols unsafe_solvers) ss;
wenzelm@3551
   388
nipkow@4713
   389
val          simp_tac = gen_simp_tac (false, false, false);
nipkow@4713
   390
val      asm_simp_tac = gen_simp_tac (false, true, false);
nipkow@4713
   391
val     full_simp_tac = gen_simp_tac (true,  false, false);
nipkow@4722
   392
val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
nipkow@4722
   393
val asm_full_simp_tac = gen_simp_tac (true,  true, true);
clasohm@0
   394
oheimb@2629
   395
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@7214
   396
fun safe_asm_full_simp_tac (ss as Simpset {mss, subgoal_tac, loop_tacs, 
nipkow@7568
   397
	unsafe_solvers, solvers}) = 
nipkow@7568
   398
  basic_gen_simp_tac (true, true, true) (app_sols solvers) ss;
oheimb@2629
   399
wenzelm@4795
   400
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   401
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   402
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   403
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   404
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   405
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   406
wenzelm@3557
   407
wenzelm@4795
   408
wenzelm@5082
   409
(** simplification rules and conversions **)
wenzelm@3557
   410
oheimb@7214
   411
fun simp rew mode 
nipkow@7568
   412
     (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
wenzelm@3557
   413
  let
nipkow@7568
   414
    val tacf = solve_all_tac (subgoal_tac, loop_tacs, unsafe_solvers);
wenzelm@4271
   415
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@5082
   416
  in rew mode prover mss thm end;
wenzelm@5082
   417
wenzelm@5082
   418
val simp_thm = simp Drule.rewrite_thm;
wenzelm@5082
   419
val simp_cterm = simp Drule.rewrite_cterm;
wenzelm@3557
   420
wenzelm@5082
   421
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   422
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   423
val     full_simplify = simp_thm (true, false, false);
wenzelm@5082
   424
val asm_full_simplify = simp_thm (true, true, false);
wenzelm@5082
   425
wenzelm@5082
   426
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   427
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   428
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@5082
   429
val asm_full_rewrite = simp_cterm (true, true, false);
wenzelm@3557
   430
wenzelm@3557
   431
wenzelm@4795
   432
wenzelm@5886
   433
(** concrete syntax of attributes **)
wenzelm@5842
   434
wenzelm@5886
   435
(* add / del *)
wenzelm@5842
   436
wenzelm@5928
   437
val simpN = "simp";
wenzelm@5928
   438
val addN = "add";
wenzelm@5928
   439
val delN = "del";
wenzelm@6911
   440
val onlyN = "only";
wenzelm@5928
   441
val otherN = "other";
wenzelm@5842
   442
wenzelm@5886
   443
fun simp_att change =
wenzelm@5928
   444
  (Args.$$$ addN >> K (op addsimps) ||
wenzelm@5928
   445
    Args.$$$ delN >> K (op delsimps) ||
wenzelm@5886
   446
    Scan.succeed (op addsimps))
wenzelm@5886
   447
  >> change
wenzelm@5886
   448
  |> Scan.lift
wenzelm@5886
   449
  |> Attrib.syntax;
wenzelm@5842
   450
wenzelm@5886
   451
val simp_attr = (simp_att change_global_ss, simp_att change_local_ss);
wenzelm@5842
   452
wenzelm@5842
   453
wenzelm@5842
   454
(* conversions *)
wenzelm@5842
   455
wenzelm@5842
   456
fun conv_attr f =
wenzelm@6096
   457
  (Attrib.no_args (Drule.rule_attribute (f o simpset_of)),
wenzelm@6096
   458
    Attrib.no_args (Drule.rule_attribute (f o get_local_simpset)));
wenzelm@5842
   459
wenzelm@5842
   460
wenzelm@5842
   461
(* setup_attrs *)
wenzelm@5842
   462
wenzelm@5842
   463
val setup_attrs = Attrib.add_attributes
wenzelm@5928
   464
 [(simpN,               simp_attr, "simplification rule"),
wenzelm@5842
   465
  ("simplify",          conv_attr simplify, "simplify rule"),
wenzelm@5842
   466
  ("asm_simplify",      conv_attr asm_simplify, "simplify rule, using assumptions"),
wenzelm@5842
   467
  ("full_simplify",     conv_attr full_simplify, "fully simplify rule"),
wenzelm@5842
   468
  ("asm_full_simplify", conv_attr asm_full_simplify, "fully simplify rule, using assumptions")];
wenzelm@5842
   469
wenzelm@5842
   470
wenzelm@5842
   471
wenzelm@5842
   472
(** proof methods **)
wenzelm@5842
   473
wenzelm@5842
   474
(* simplification *)
wenzelm@5842
   475
wenzelm@5928
   476
val simp_modifiers =
wenzelm@7273
   477
 [Args.$$$ simpN -- Args.$$$ addN >> K (I, simp_add_local),
wenzelm@7273
   478
  Args.$$$ simpN -- Args.$$$ delN >> K (I, simp_del_local),
wenzelm@7273
   479
  Args.$$$ simpN -- Args.$$$ onlyN >> K (map_local_simpset clear_ss, simp_add_local),
wenzelm@7273
   480
  Args.$$$ otherN >> K (I, I)];
wenzelm@5928
   481
wenzelm@5928
   482
val simp_modifiers' =
wenzelm@7273
   483
 [Args.$$$ addN >> K (I, simp_add_local),
wenzelm@7273
   484
  Args.$$$ delN >> K (I, simp_del_local),
wenzelm@7273
   485
  Args.$$$ onlyN >> K (map_local_simpset clear_ss, simp_add_local),
wenzelm@7273
   486
  Args.$$$ otherN >> K (I, I)];
wenzelm@5928
   487
wenzelm@7558
   488
fun simp_method tac =
wenzelm@7558
   489
  (fn prems => fn ctxt => Method.METHOD (fn facts =>
wenzelm@8170
   490
    ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (get_local_simpset ctxt)))
wenzelm@8170
   491
  |> Method.bang_sectioned_args simp_modifiers';
wenzelm@8170
   492
wenzelm@8170
   493
fun simp_method' tac =
wenzelm@8170
   494
  (fn prems => fn ctxt => Method.METHOD (fn facts =>
wenzelm@8168
   495
    HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (get_local_simpset ctxt))))
wenzelm@7558
   496
  |> Method.bang_sectioned_args simp_modifiers';
nipkow@7568
   497
  
wenzelm@5842
   498
wenzelm@5842
   499
(* setup_methods *)
wenzelm@5842
   500
wenzelm@5842
   501
val setup_methods = Method.add_methods
wenzelm@8170
   502
 [(simpN,               simp_method' (CHANGED oo asm_full_simp_tac), "full simplification"),
wenzelm@8170
   503
  ("simp_all",          simp_method  (CHANGED o ALLGOALS o asm_full_simp_tac),
wenzelm@8170
   504
    "full simplification (all goals)"),
wenzelm@8170
   505
  ("simp_tac",          simp_method' simp_tac, "simp_tac (improper!)"),
wenzelm@8170
   506
  ("asm_simp_tac",      simp_method' asm_simp_tac, "asm_simp_tac (improper!)"),
wenzelm@8170
   507
  ("full_simp_tac",     simp_method' full_simp_tac, "full_simp_tac (improper!)"),
wenzelm@8170
   508
  ("asm_full_simp_tac", simp_method' asm_full_simp_tac, "asm_full_simp_tac (improper!)"),
wenzelm@8170
   509
  ("asm_lr_simp_tac",   simp_method' asm_lr_simp_tac, "asm_lr_simp_tac (improper!)")];
wenzelm@5842
   510
wenzelm@5842
   511
wenzelm@5842
   512
wenzelm@4795
   513
(** theory setup **)
wenzelm@4795
   514
wenzelm@5842
   515
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs, setup_methods];
wenzelm@4795
   516
wenzelm@4795
   517
clasohm@1243
   518
end;
wenzelm@4795
   519
wenzelm@4795
   520
wenzelm@4795
   521
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   522
open BasicSimplifier;