src/Provers/simplifier.ML
author wenzelm
Tue Sep 21 23:06:50 1999 +0200 (1999-09-21 ago)
changeset 7571 44e9db3150f6
parent 7568 436c87ac2fac
child 7646 1ad3866b86cc
permissions -rw-r--r--
merged in lost update;
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@2645
   213
  make_ss (Thm.set_mk_rews (mss, map (Thm.strip_shyps o Drule.zero_var_indexes) o mk_simps),
nipkow@7568
   214
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2509
   215
nipkow@7568
   216
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   217
    setmkeqTrue mk_eq_True =
nipkow@4677
   218
  make_ss (Thm.set_mk_eq_True (mss, mk_eq_True),
nipkow@7568
   219
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
nipkow@4677
   220
nipkow@7568
   221
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   222
    setmksym mksym =
nipkow@4677
   223
  make_ss (Thm.set_mk_sym (mss, mksym),
nipkow@7568
   224
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
nipkow@4677
   225
nipkow@7568
   226
fun (Simpset {mss, subgoal_tac, loop_tacs,  unsafe_solvers, solvers})
wenzelm@3551
   227
    settermless termless =
nipkow@4668
   228
  make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tacs,
nipkow@7568
   229
    unsafe_solvers, solvers);
wenzelm@3551
   230
nipkow@7568
   231
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   232
    addsimps rews =
oheimb@7214
   233
  make_ss (Thm.add_simps (mss, rews), subgoal_tac, loop_tacs, 
nipkow@7568
   234
	   unsafe_solvers, solvers);
wenzelm@2503
   235
nipkow@7568
   236
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   237
    delsimps rews =
oheimb@7214
   238
  make_ss (Thm.del_simps (mss, rews), subgoal_tac, loop_tacs, 
nipkow@7568
   239
	   unsafe_solvers, solvers);
wenzelm@2503
   240
nipkow@7568
   241
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   242
    addeqcongs newcongs =
oheimb@7214
   243
  make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tacs, 
nipkow@7568
   244
	   unsafe_solvers, solvers);
wenzelm@2509
   245
nipkow@7568
   246
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   247
    deleqcongs oldcongs =
oheimb@7214
   248
  make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tacs, 
nipkow@7568
   249
	   unsafe_solvers, solvers);
oheimb@2629
   250
nipkow@7568
   251
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   252
    addsimprocs simprocs =
wenzelm@3551
   253
  make_ss
wenzelm@3551
   254
    (Thm.add_simprocs (mss, map rep_simproc simprocs),
nipkow@7568
   255
      subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2509
   256
nipkow@7568
   257
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   258
    delsimprocs simprocs =
wenzelm@3551
   259
  make_ss
wenzelm@3551
   260
    (Thm.del_simprocs (mss, map rep_simproc simprocs),
nipkow@7568
   261
      subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2503
   262
nipkow@7568
   263
fun clear_ss (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers}) =
nipkow@7568
   264
  make_ss (Thm.clear_mss mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@6911
   265
wenzelm@2503
   266
nipkow@7568
   267
(* merge simpsets *)
nipkow@7568
   268
(*NOTE: ignores subgoal_tac of 2nd simpset *)
wenzelm@2503
   269
wenzelm@3551
   270
fun merge_ss
nipkow@7568
   271
   (Simpset {mss = mss1, loop_tacs = loop_tacs1, subgoal_tac,
nipkow@7568
   272
             unsafe_solvers = unsafe_solvers1, solvers = solvers1},
nipkow@7568
   273
    Simpset {mss = mss2, loop_tacs = loop_tacs2,
nipkow@7568
   274
             unsafe_solvers = unsafe_solvers2, solvers = solvers2, ...}) =
nipkow@4668
   275
  make_ss (Thm.merge_mss (mss1, mss2), subgoal_tac,
nipkow@7568
   276
    merge_alists loop_tacs1 loop_tacs2,
nipkow@7568
   277
    merge_solvers unsafe_solvers1 unsafe_solvers2,
nipkow@7568
   278
    merge_solvers solvers1 solvers2);
wenzelm@2503
   279
wenzelm@3557
   280
wenzelm@5024
   281
(** global and local simpset data **)
wenzelm@4080
   282
wenzelm@5842
   283
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   284
wenzelm@5842
   285
structure GlobalSimpsetArgs =
wenzelm@5024
   286
struct
wenzelm@5842
   287
  val name = "Provers/simpset";
wenzelm@5024
   288
  type T = simpset ref;
clasohm@0
   289
wenzelm@5024
   290
  val empty = ref empty_ss;
wenzelm@6556
   291
  fun copy (ref ss) = (ref ss): T;            (*create new reference!*)
wenzelm@6556
   292
  val prep_ext = copy;
wenzelm@5024
   293
  fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@5024
   294
  fun print _ (ref ss) = print_ss ss;
wenzelm@5024
   295
end;
wenzelm@4080
   296
wenzelm@5842
   297
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   298
val print_simpset = GlobalSimpset.print;
wenzelm@5842
   299
val simpset_ref_of_sg = GlobalSimpset.get_sg;
wenzelm@5842
   300
val simpset_ref_of = GlobalSimpset.get;
wenzelm@4080
   301
wenzelm@4080
   302
wenzelm@4795
   303
(* access global simpset *)
clasohm@0
   304
wenzelm@4080
   305
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   306
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   307
wenzelm@6391
   308
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   309
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   310
wenzelm@5028
   311
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   312
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   313
wenzelm@4080
   314
wenzelm@4795
   315
(* change global simpset *)
wenzelm@4080
   316
wenzelm@7177
   317
fun change_simpset_of f x thy =
wenzelm@7177
   318
  let val r = simpset_ref_of thy
wenzelm@7177
   319
  in r := f (! r, x); thy end;
wenzelm@7177
   320
wenzelm@7177
   321
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   322
wenzelm@4080
   323
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   324
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   325
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   326
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@2509
   327
clasohm@0
   328
wenzelm@5842
   329
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   330
wenzelm@5842
   331
structure LocalSimpsetArgs =
wenzelm@5842
   332
struct
wenzelm@5842
   333
  val name = "Provers/simpset";
wenzelm@5842
   334
  type T = simpset;
wenzelm@5842
   335
  val init = simpset_of;
wenzelm@5842
   336
  fun print _ ss = print_ss ss;
wenzelm@5842
   337
end;
wenzelm@4795
   338
wenzelm@5842
   339
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   340
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   341
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   342
val put_local_simpset = LocalSimpset.put;
wenzelm@7273
   343
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@4795
   344
wenzelm@4795
   345
wenzelm@5886
   346
(* attributes *)
wenzelm@5886
   347
wenzelm@5886
   348
fun change_global_ss f (thy, th) =
wenzelm@5886
   349
  let val r = simpset_ref_of thy
wenzelm@6096
   350
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   351
wenzelm@5886
   352
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   353
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   354
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   355
wenzelm@5886
   356
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   357
val simp_del_global = change_global_ss (op delsimps);
wenzelm@5886
   358
val simp_add_local = change_local_ss (op addsimps);
wenzelm@5886
   359
val simp_del_local = change_local_ss (op delsimps);
wenzelm@5886
   360
wenzelm@5886
   361
wenzelm@3557
   362
wenzelm@2503
   363
(** simplification tactics **)
clasohm@0
   364
nipkow@7568
   365
fun solve_all_tac (subgoal_tac, loop_tacs, unsafe_solvers) mss =
wenzelm@3557
   366
  let
wenzelm@3557
   367
    val ss =
nipkow@7568
   368
      make_ss (mss, subgoal_tac, loop_tacs,unsafe_solvers,unsafe_solvers);
nipkow@4612
   369
    val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
wenzelm@3557
   370
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   371
nipkow@4668
   372
fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
wenzelm@3557
   373
oheimb@7214
   374
(*unsafe: may instantiate unknowns that appear also in other subgoals*)
nipkow@7568
   375
fun basic_gen_simp_tac mode solvers =
nipkow@7568
   376
  fn (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) =>
wenzelm@3551
   377
    let
nipkow@4612
   378
      fun simp_loop_tac i =
nipkow@4612
   379
        asm_rewrite_goal_tac mode
nipkow@7568
   380
          (solve_all_tac (subgoal_tac,loop_tacs,unsafe_solvers))
nipkow@4612
   381
          mss i
nipkow@7568
   382
        THEN (solvers (prems_of_mss mss) i ORELSE
nipkow@4668
   383
              TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
nipkow@4612
   384
    in simp_loop_tac end;
clasohm@0
   385
oheimb@7214
   386
fun gen_simp_tac mode 
nipkow@7568
   387
      (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) =
nipkow@7568
   388
  basic_gen_simp_tac mode (app_sols unsafe_solvers) ss;
wenzelm@3551
   389
nipkow@4713
   390
val          simp_tac = gen_simp_tac (false, false, false);
nipkow@4713
   391
val      asm_simp_tac = gen_simp_tac (false, true, false);
nipkow@4713
   392
val     full_simp_tac = gen_simp_tac (true,  false, false);
nipkow@4722
   393
val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
nipkow@4722
   394
val asm_full_simp_tac = gen_simp_tac (true,  true, true);
clasohm@0
   395
oheimb@2629
   396
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@7214
   397
fun safe_asm_full_simp_tac (ss as Simpset {mss, subgoal_tac, loop_tacs, 
nipkow@7568
   398
	unsafe_solvers, solvers}) = 
nipkow@7568
   399
  basic_gen_simp_tac (true, true, true) (app_sols solvers) ss;
oheimb@2629
   400
wenzelm@4795
   401
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   402
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   403
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   404
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   405
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   406
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   407
wenzelm@3557
   408
wenzelm@4795
   409
wenzelm@5082
   410
(** simplification rules and conversions **)
wenzelm@3557
   411
oheimb@7214
   412
fun simp rew mode 
nipkow@7568
   413
     (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
wenzelm@3557
   414
  let
nipkow@7568
   415
    val tacf = solve_all_tac (subgoal_tac, loop_tacs, unsafe_solvers);
wenzelm@4271
   416
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@5082
   417
  in rew mode prover mss thm end;
wenzelm@5082
   418
wenzelm@5082
   419
val simp_thm = simp Drule.rewrite_thm;
wenzelm@5082
   420
val simp_cterm = simp Drule.rewrite_cterm;
wenzelm@3557
   421
wenzelm@5082
   422
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   423
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   424
val     full_simplify = simp_thm (true, false, false);
wenzelm@5082
   425
val asm_full_simplify = simp_thm (true, true, false);
wenzelm@5082
   426
wenzelm@5082
   427
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   428
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   429
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@5082
   430
val asm_full_rewrite = simp_cterm (true, true, false);
wenzelm@3557
   431
wenzelm@3557
   432
wenzelm@4795
   433
wenzelm@5886
   434
(** concrete syntax of attributes **)
wenzelm@5842
   435
wenzelm@5886
   436
(* add / del *)
wenzelm@5842
   437
wenzelm@5928
   438
val simpN = "simp";
wenzelm@5928
   439
val addN = "add";
wenzelm@5928
   440
val delN = "del";
wenzelm@6911
   441
val onlyN = "only";
wenzelm@5928
   442
val otherN = "other";
wenzelm@5842
   443
wenzelm@5886
   444
fun simp_att change =
wenzelm@5928
   445
  (Args.$$$ addN >> K (op addsimps) ||
wenzelm@5928
   446
    Args.$$$ delN >> K (op delsimps) ||
wenzelm@5886
   447
    Scan.succeed (op addsimps))
wenzelm@5886
   448
  >> change
wenzelm@5886
   449
  |> Scan.lift
wenzelm@5886
   450
  |> Attrib.syntax;
wenzelm@5842
   451
wenzelm@5886
   452
val simp_attr = (simp_att change_global_ss, simp_att change_local_ss);
wenzelm@5842
   453
wenzelm@5842
   454
wenzelm@5842
   455
(* conversions *)
wenzelm@5842
   456
wenzelm@5842
   457
fun conv_attr f =
wenzelm@6096
   458
  (Attrib.no_args (Drule.rule_attribute (f o simpset_of)),
wenzelm@6096
   459
    Attrib.no_args (Drule.rule_attribute (f o get_local_simpset)));
wenzelm@5842
   460
wenzelm@5842
   461
wenzelm@5842
   462
(* setup_attrs *)
wenzelm@5842
   463
wenzelm@5842
   464
val setup_attrs = Attrib.add_attributes
wenzelm@5928
   465
 [(simpN,               simp_attr, "simplification rule"),
wenzelm@5842
   466
  ("simplify",          conv_attr simplify, "simplify rule"),
wenzelm@5842
   467
  ("asm_simplify",      conv_attr asm_simplify, "simplify rule, using assumptions"),
wenzelm@5842
   468
  ("full_simplify",     conv_attr full_simplify, "fully simplify rule"),
wenzelm@5842
   469
  ("asm_full_simplify", conv_attr asm_full_simplify, "fully simplify rule, using assumptions")];
wenzelm@5842
   470
wenzelm@5842
   471
wenzelm@5842
   472
wenzelm@5842
   473
(** proof methods **)
wenzelm@5842
   474
wenzelm@5842
   475
(* simplification *)
wenzelm@5842
   476
wenzelm@5928
   477
val simp_modifiers =
wenzelm@7273
   478
 [Args.$$$ simpN -- Args.$$$ addN >> K (I, simp_add_local),
wenzelm@7273
   479
  Args.$$$ simpN -- Args.$$$ delN >> K (I, simp_del_local),
wenzelm@7273
   480
  Args.$$$ simpN -- Args.$$$ onlyN >> K (map_local_simpset clear_ss, simp_add_local),
wenzelm@7273
   481
  Args.$$$ otherN >> K (I, I)];
wenzelm@5928
   482
wenzelm@5928
   483
val simp_modifiers' =
wenzelm@7273
   484
 [Args.$$$ addN >> K (I, simp_add_local),
wenzelm@7273
   485
  Args.$$$ delN >> K (I, simp_del_local),
wenzelm@7273
   486
  Args.$$$ onlyN >> K (map_local_simpset clear_ss, simp_add_local),
wenzelm@7273
   487
  Args.$$$ otherN >> K (I, I)];
wenzelm@5928
   488
wenzelm@7558
   489
fun simp_method tac =
wenzelm@7558
   490
  (fn prems => fn ctxt => Method.METHOD (fn facts =>
wenzelm@7558
   491
    FIRSTGOAL (Method.insert_tac (prems @ facts) THEN' tac (get_local_simpset ctxt))))
wenzelm@7558
   492
  |> Method.bang_sectioned_args simp_modifiers';
nipkow@7568
   493
  
wenzelm@5842
   494
wenzelm@5842
   495
(* setup_methods *)
wenzelm@5842
   496
wenzelm@5842
   497
val setup_methods = Method.add_methods
wenzelm@7571
   498
 [(simpN,               simp_method (CHANGED oo asm_full_simp_tac), "full simplification"),
wenzelm@7571
   499
  ("simp_tac",          simp_method simp_tac, "simp_tac (improper!)"),
wenzelm@7571
   500
  ("asm_simp_tac",      simp_method asm_simp_tac, "asm_simp_tac (improper!)"),
wenzelm@7571
   501
  ("full_simp_tac",     simp_method full_simp_tac, "full_simp_tac (improper!)"),
wenzelm@7571
   502
  ("asm_full_simp_tac", simp_method asm_full_simp_tac, "asm_full_simp_tac (improper!)"),
wenzelm@7571
   503
  ("asm_lr_simp_tac",   simp_method asm_lr_simp_tac, "asm_lr_simp_tac (improper!)")];
wenzelm@5842
   504
wenzelm@5842
   505
wenzelm@5842
   506
wenzelm@4795
   507
(** theory setup **)
wenzelm@4795
   508
wenzelm@5842
   509
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs, setup_methods];
wenzelm@4795
   510
wenzelm@4795
   511
clasohm@1243
   512
end;
wenzelm@4795
   513
wenzelm@4795
   514
wenzelm@4795
   515
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   516
open BasicSimplifier;