src/Provers/simplifier.ML
author wenzelm
Tue Apr 04 22:16:11 2000 +0200 (2000-04-04 ago)
changeset 8667 4230d17073ea
parent 8634 3f34637cb9c0
child 8700 628ffca977b8
permissions -rw-r--r--
print_simpset / print_claset command;
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@8467
    90
  val change_global_ss: (simpset * thm list -> simpset) -> theory attribute
wenzelm@8467
    91
  val change_local_ss: (simpset * thm list -> simpset) -> Proof.context attribute
wenzelm@4855
    92
  val simp_add_global: theory attribute
wenzelm@4855
    93
  val simp_del_global: theory attribute
wenzelm@5842
    94
  val simp_add_local: Proof.context attribute
wenzelm@5842
    95
  val simp_del_local: Proof.context attribute
wenzelm@7177
    96
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@7273
    97
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@5842
    98
  val setup: (theory -> theory) list
wenzelm@8467
    99
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@8467
   100
    -> (theory -> theory) list
wenzelm@8228
   101
  val easy_setup: thm -> thm list -> (theory -> theory) list
wenzelm@4795
   102
end;
wenzelm@2503
   103
wenzelm@2503
   104
structure Simplifier: SIMPLIFIER =
clasohm@0
   105
struct
clasohm@0
   106
wenzelm@2509
   107
wenzelm@2509
   108
(** simplification procedures **)
wenzelm@2509
   109
wenzelm@2509
   110
(* datatype simproc *)
wenzelm@2509
   111
wenzelm@2509
   112
datatype simproc =
wenzelm@3577
   113
  Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
wenzelm@2509
   114
wenzelm@3557
   115
fun mk_simproc name lhss proc =
wenzelm@3557
   116
  Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
wenzelm@3557
   117
wenzelm@3551
   118
fun rep_simproc (Simproc args) = args;
wenzelm@2509
   119
wenzelm@8467
   120
wenzelm@8467
   121
nipkow@7568
   122
(** solvers **)
wenzelm@2509
   123
nipkow@7568
   124
datatype solver = Solver of string * (thm list -> int -> tactic) * stamp;
nipkow@7568
   125
nipkow@7568
   126
fun mk_solver name solver = Solver(name, solver, stamp());
nipkow@7568
   127
nipkow@7568
   128
fun eq_solver (Solver(_,_,s1),Solver(_,_,s2)) = s1=s2;
nipkow@7568
   129
nipkow@7568
   130
val merge_solvers = generic_merge eq_solver I I;
nipkow@7568
   131
nipkow@7568
   132
fun app_sols [] _ _ = no_tac
nipkow@7568
   133
  | app_sols (Solver(_,solver,_)::sols) thms i =
nipkow@7568
   134
       solver thms i ORELSE app_sols sols thms i;
wenzelm@2509
   135
wenzelm@8467
   136
wenzelm@8467
   137
wenzelm@2503
   138
(** simplification sets **)
wenzelm@2503
   139
wenzelm@2503
   140
(* type simpset *)
wenzelm@2503
   141
clasohm@0
   142
datatype simpset =
wenzelm@2503
   143
  Simpset of {
wenzelm@2503
   144
    mss: meta_simpset,
oheimb@2629
   145
    subgoal_tac:        simpset -> int -> tactic,
nipkow@4668
   146
    loop_tacs:          (string * (int -> tactic))list,
nipkow@7568
   147
    unsafe_solvers: solver list,
nipkow@7568
   148
           solvers: solver list};
wenzelm@2503
   149
nipkow@7568
   150
fun make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =
nipkow@4668
   151
  Simpset {mss = mss, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
nipkow@7568
   152
    unsafe_solvers = unsafe_solvers, solvers = solvers};
clasohm@0
   153
clasohm@0
   154
val empty_ss =
wenzelm@6911
   155
  let val mss = Thm.set_mk_sym (Thm.empty_mss, Some o symmetric_fun)
nipkow@7568
   156
  in make_ss (mss, K (K no_tac), [], [], []) end;
wenzelm@3551
   157
wenzelm@3551
   158
fun rep_ss (Simpset args) = args;
wenzelm@3551
   159
fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
wenzelm@3551
   160
wenzelm@3551
   161
wenzelm@3551
   162
(* print simpsets *)
wenzelm@2503
   163
wenzelm@3551
   164
fun print_ss ss =
wenzelm@3551
   165
  let
wenzelm@3551
   166
    val Simpset {mss, ...} = ss;
wenzelm@3551
   167
    val {simps, procs, congs} = Thm.dest_mss mss;
wenzelm@2503
   168
wenzelm@3551
   169
    val pretty_thms = map Display.pretty_thm;
wenzelm@3551
   170
    fun pretty_proc (name, lhss) =
wenzelm@3551
   171
      Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
wenzelm@3551
   172
  in
wenzelm@3551
   173
    Pretty.writeln (Pretty.big_list "simplification rules:" (pretty_thms simps));
wenzelm@3551
   174
    Pretty.writeln (Pretty.big_list "simplification procedures:" (map pretty_proc procs));
wenzelm@3551
   175
    Pretty.writeln (Pretty.big_list "congruences:" (pretty_thms congs))
wenzelm@3551
   176
  end;
wenzelm@2503
   177
wenzelm@2503
   178
wenzelm@2503
   179
(* extend simpsets *)
wenzelm@2503
   180
nipkow@7568
   181
fun (Simpset {mss, subgoal_tac = _, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   182
    setsubgoaler subgoal_tac =
nipkow@7568
   183
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers);
oheimb@2629
   184
nipkow@7568
   185
fun (Simpset {mss, subgoal_tac, loop_tacs = _, unsafe_solvers, solvers})
nipkow@4668
   186
    setloop tac =
nipkow@7568
   187
  make_ss (mss, subgoal_tac, [("",tac)], unsafe_solvers, solvers);
wenzelm@2503
   188
nipkow@7568
   189
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
oheimb@4741
   190
    addloop tac = make_ss (mss, subgoal_tac, 
wenzelm@5886
   191
        (case assoc_string (loop_tacs,(fst tac)) of None => () | Some x => 
wenzelm@5886
   192
         warning ("overwriting looper "^fst tac); overwrite(loop_tacs,tac)),
nipkow@7568
   193
           unsafe_solvers, solvers);
oheimb@2567
   194
nipkow@7568
   195
fun (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
oheimb@7214
   196
 delloop name =
wenzelm@6911
   197
  let val (del, rest) = partition (fn (n, _) => n = name) loop_tacs in
wenzelm@6911
   198
    if null del then (warning ("No such looper in simpset: " ^ name); ss)
nipkow@7568
   199
    else make_ss (mss, subgoal_tac, rest, unsafe_solvers, solvers)
nipkow@4682
   200
  end;
nipkow@4682
   201
nipkow@7568
   202
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...})
nipkow@7568
   203
    setSSolver solver =
nipkow@7568
   204
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers, [solver]);
wenzelm@2503
   205
nipkow@7568
   206
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@7568
   207
    addSSolver sol =
nipkow@7568
   208
  make_ss (mss, subgoal_tac, loop_tacs, unsafe_solvers,
nipkow@7568
   209
           merge_solvers solvers [sol]);
wenzelm@2503
   210
nipkow@7568
   211
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers = _, solvers})
nipkow@7568
   212
    setSolver unsafe_solver =
nipkow@7568
   213
  make_ss (mss, subgoal_tac, loop_tacs, [unsafe_solver], solvers);
nipkow@7568
   214
nipkow@7568
   215
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@7568
   216
    addSolver sol =
oheimb@7214
   217
  make_ss (mss, subgoal_tac, loop_tacs, 
nipkow@7568
   218
    merge_solvers unsafe_solvers [sol], solvers);
wenzelm@2503
   219
nipkow@7568
   220
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   221
    setmksimps mk_simps =
wenzelm@7646
   222
  make_ss (Thm.set_mk_rews (mss, mk_simps), subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2509
   223
nipkow@7568
   224
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   225
    setmkeqTrue mk_eq_True =
nipkow@4677
   226
  make_ss (Thm.set_mk_eq_True (mss, mk_eq_True),
nipkow@7568
   227
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
nipkow@4677
   228
nipkow@7568
   229
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   230
    setmksym mksym =
nipkow@4677
   231
  make_ss (Thm.set_mk_sym (mss, mksym),
nipkow@7568
   232
    subgoal_tac, loop_tacs, unsafe_solvers, solvers);
nipkow@4677
   233
nipkow@7568
   234
fun (Simpset {mss, subgoal_tac, loop_tacs,  unsafe_solvers, solvers})
wenzelm@3551
   235
    settermless termless =
nipkow@4668
   236
  make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tacs,
nipkow@7568
   237
    unsafe_solvers, solvers);
wenzelm@3551
   238
nipkow@7568
   239
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   240
    addsimps rews =
oheimb@7214
   241
  make_ss (Thm.add_simps (mss, rews), subgoal_tac, loop_tacs, 
nipkow@7568
   242
	   unsafe_solvers, solvers);
wenzelm@2503
   243
nipkow@7568
   244
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   245
    delsimps rews =
oheimb@7214
   246
  make_ss (Thm.del_simps (mss, rews), subgoal_tac, loop_tacs, 
nipkow@7568
   247
	   unsafe_solvers, solvers);
wenzelm@2503
   248
nipkow@7568
   249
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   250
    addeqcongs newcongs =
oheimb@7214
   251
  make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tacs, 
nipkow@7568
   252
	   unsafe_solvers, solvers);
wenzelm@2509
   253
nipkow@7568
   254
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   255
    deleqcongs oldcongs =
oheimb@7214
   256
  make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tacs, 
nipkow@7568
   257
	   unsafe_solvers, solvers);
oheimb@2629
   258
nipkow@7568
   259
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   260
    addsimprocs simprocs =
wenzelm@3551
   261
  make_ss
wenzelm@3551
   262
    (Thm.add_simprocs (mss, map rep_simproc simprocs),
nipkow@7568
   263
      subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2509
   264
nipkow@7568
   265
fun (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   266
    delsimprocs simprocs =
wenzelm@3551
   267
  make_ss
wenzelm@3551
   268
    (Thm.del_simprocs (mss, map rep_simproc simprocs),
nipkow@7568
   269
      subgoal_tac, loop_tacs, unsafe_solvers, solvers);
wenzelm@2503
   270
wenzelm@8467
   271
fun clear_ss (Simpset {mss, subgoal_tac, loop_tacs = _, unsafe_solvers, solvers}) =
wenzelm@8467
   272
  make_ss (Thm.clear_mss mss, subgoal_tac, [], unsafe_solvers, solvers);
wenzelm@6911
   273
wenzelm@2503
   274
nipkow@7568
   275
(* merge simpsets *)
wenzelm@8467
   276
wenzelm@8467
   277
(*ignores subgoal_tac of 2nd simpset!*)
wenzelm@2503
   278
wenzelm@3551
   279
fun merge_ss
nipkow@7568
   280
   (Simpset {mss = mss1, loop_tacs = loop_tacs1, subgoal_tac,
nipkow@7568
   281
             unsafe_solvers = unsafe_solvers1, solvers = solvers1},
nipkow@7568
   282
    Simpset {mss = mss2, loop_tacs = loop_tacs2,
nipkow@7568
   283
             unsafe_solvers = unsafe_solvers2, solvers = solvers2, ...}) =
nipkow@4668
   284
  make_ss (Thm.merge_mss (mss1, mss2), subgoal_tac,
nipkow@7568
   285
    merge_alists loop_tacs1 loop_tacs2,
nipkow@7568
   286
    merge_solvers unsafe_solvers1 unsafe_solvers2,
nipkow@7568
   287
    merge_solvers solvers1 solvers2);
wenzelm@2503
   288
wenzelm@3557
   289
wenzelm@8467
   290
wenzelm@5024
   291
(** global and local simpset data **)
wenzelm@4080
   292
wenzelm@5842
   293
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   294
wenzelm@5842
   295
structure GlobalSimpsetArgs =
wenzelm@5024
   296
struct
wenzelm@5842
   297
  val name = "Provers/simpset";
wenzelm@5024
   298
  type T = simpset ref;
clasohm@0
   299
wenzelm@5024
   300
  val empty = ref empty_ss;
wenzelm@6556
   301
  fun copy (ref ss) = (ref ss): T;            (*create new reference!*)
wenzelm@6556
   302
  val prep_ext = copy;
wenzelm@5024
   303
  fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@5024
   304
  fun print _ (ref ss) = print_ss ss;
wenzelm@5024
   305
end;
wenzelm@4080
   306
wenzelm@5842
   307
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   308
val print_simpset = GlobalSimpset.print;
wenzelm@5842
   309
val simpset_ref_of_sg = GlobalSimpset.get_sg;
wenzelm@5842
   310
val simpset_ref_of = GlobalSimpset.get;
wenzelm@4080
   311
wenzelm@4080
   312
wenzelm@4795
   313
(* access global simpset *)
clasohm@0
   314
wenzelm@4080
   315
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   316
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   317
wenzelm@6391
   318
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   319
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   320
wenzelm@5028
   321
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   322
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   323
wenzelm@4080
   324
wenzelm@4795
   325
(* change global simpset *)
wenzelm@4080
   326
wenzelm@7177
   327
fun change_simpset_of f x thy =
wenzelm@7177
   328
  let val r = simpset_ref_of thy
wenzelm@7177
   329
  in r := f (! r, x); thy end;
wenzelm@7177
   330
wenzelm@7177
   331
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   332
wenzelm@4080
   333
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   334
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   335
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   336
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@2509
   337
clasohm@0
   338
wenzelm@5842
   339
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   340
wenzelm@5842
   341
structure LocalSimpsetArgs =
wenzelm@5842
   342
struct
wenzelm@5842
   343
  val name = "Provers/simpset";
wenzelm@5842
   344
  type T = simpset;
wenzelm@5842
   345
  val init = simpset_of;
wenzelm@5842
   346
  fun print _ ss = print_ss ss;
wenzelm@5842
   347
end;
wenzelm@4795
   348
wenzelm@5842
   349
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   350
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   351
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   352
val put_local_simpset = LocalSimpset.put;
wenzelm@7273
   353
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@4795
   354
wenzelm@4795
   355
wenzelm@5886
   356
(* attributes *)
wenzelm@5886
   357
wenzelm@5886
   358
fun change_global_ss f (thy, th) =
wenzelm@5886
   359
  let val r = simpset_ref_of thy
wenzelm@6096
   360
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   361
wenzelm@5886
   362
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   363
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   364
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   365
wenzelm@5886
   366
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   367
val simp_del_global = change_global_ss (op delsimps);
wenzelm@5886
   368
val simp_add_local = change_local_ss (op addsimps);
wenzelm@5886
   369
val simp_del_local = change_local_ss (op delsimps);
wenzelm@5886
   370
wenzelm@5886
   371
wenzelm@3557
   372
wenzelm@2503
   373
(** simplification tactics **)
clasohm@0
   374
nipkow@7568
   375
fun solve_all_tac (subgoal_tac, loop_tacs, unsafe_solvers) mss =
wenzelm@3557
   376
  let
wenzelm@3557
   377
    val ss =
nipkow@7568
   378
      make_ss (mss, subgoal_tac, loop_tacs,unsafe_solvers,unsafe_solvers);
nipkow@4612
   379
    val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
wenzelm@3557
   380
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   381
nipkow@4668
   382
fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
wenzelm@3557
   383
oheimb@7214
   384
(*unsafe: may instantiate unknowns that appear also in other subgoals*)
nipkow@7568
   385
fun basic_gen_simp_tac mode solvers =
nipkow@7568
   386
  fn (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) =>
wenzelm@3551
   387
    let
nipkow@4612
   388
      fun simp_loop_tac i =
nipkow@4612
   389
        asm_rewrite_goal_tac mode
nipkow@7568
   390
          (solve_all_tac (subgoal_tac,loop_tacs,unsafe_solvers))
nipkow@4612
   391
          mss i
nipkow@7568
   392
        THEN (solvers (prems_of_mss mss) i ORELSE
nipkow@4668
   393
              TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
nipkow@4612
   394
    in simp_loop_tac end;
clasohm@0
   395
oheimb@7214
   396
fun gen_simp_tac mode 
nipkow@7568
   397
      (ss as Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) =
nipkow@7568
   398
  basic_gen_simp_tac mode (app_sols unsafe_solvers) ss;
wenzelm@3551
   399
nipkow@4713
   400
val          simp_tac = gen_simp_tac (false, false, false);
nipkow@4713
   401
val      asm_simp_tac = gen_simp_tac (false, true, false);
nipkow@4713
   402
val     full_simp_tac = gen_simp_tac (true,  false, false);
nipkow@4722
   403
val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
nipkow@4722
   404
val asm_full_simp_tac = gen_simp_tac (true,  true, true);
clasohm@0
   405
oheimb@2629
   406
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@7214
   407
fun safe_asm_full_simp_tac (ss as Simpset {mss, subgoal_tac, loop_tacs, 
nipkow@7568
   408
	unsafe_solvers, solvers}) = 
nipkow@7568
   409
  basic_gen_simp_tac (true, true, true) (app_sols solvers) ss;
oheimb@2629
   410
wenzelm@4795
   411
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   412
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   413
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   414
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   415
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   416
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   417
wenzelm@3557
   418
wenzelm@4795
   419
wenzelm@5082
   420
(** simplification rules and conversions **)
wenzelm@3557
   421
oheimb@7214
   422
fun simp rew mode 
nipkow@7568
   423
     (Simpset {mss, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
wenzelm@3557
   424
  let
nipkow@7568
   425
    val tacf = solve_all_tac (subgoal_tac, loop_tacs, unsafe_solvers);
wenzelm@4271
   426
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@5082
   427
  in rew mode prover mss thm end;
wenzelm@5082
   428
wenzelm@5082
   429
val simp_thm = simp Drule.rewrite_thm;
wenzelm@5082
   430
val simp_cterm = simp Drule.rewrite_cterm;
wenzelm@3557
   431
wenzelm@5082
   432
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   433
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   434
val     full_simplify = simp_thm (true, false, false);
wenzelm@5082
   435
val asm_full_simplify = simp_thm (true, true, false);
wenzelm@5082
   436
wenzelm@5082
   437
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   438
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   439
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@5082
   440
val asm_full_rewrite = simp_cterm (true, true, false);
wenzelm@3557
   441
wenzelm@3557
   442
wenzelm@4795
   443
wenzelm@5886
   444
(** concrete syntax of attributes **)
wenzelm@5842
   445
wenzelm@5886
   446
(* add / del *)
wenzelm@5842
   447
wenzelm@5928
   448
val simpN = "simp";
wenzelm@5928
   449
val addN = "add";
wenzelm@5928
   450
val delN = "del";
wenzelm@6911
   451
val onlyN = "only";
wenzelm@5928
   452
val otherN = "other";
wenzelm@5842
   453
wenzelm@8634
   454
val simp_attr =
wenzelm@8634
   455
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@8634
   456
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@5842
   457
wenzelm@5842
   458
wenzelm@5842
   459
(* conversions *)
wenzelm@5842
   460
wenzelm@5842
   461
fun conv_attr f =
wenzelm@6096
   462
  (Attrib.no_args (Drule.rule_attribute (f o simpset_of)),
wenzelm@6096
   463
    Attrib.no_args (Drule.rule_attribute (f o get_local_simpset)));
wenzelm@5842
   464
wenzelm@5842
   465
wenzelm@5842
   466
(* setup_attrs *)
wenzelm@5842
   467
wenzelm@5842
   468
val setup_attrs = Attrib.add_attributes
wenzelm@8467
   469
 [(simpN,               simp_attr, "declare simplification rule"),
wenzelm@5842
   470
  ("simplify",          conv_attr simplify, "simplify rule"),
wenzelm@5842
   471
  ("asm_simplify",      conv_attr asm_simplify, "simplify rule, using assumptions"),
wenzelm@5842
   472
  ("full_simplify",     conv_attr full_simplify, "fully simplify rule"),
wenzelm@5842
   473
  ("asm_full_simplify", conv_attr asm_full_simplify, "fully simplify rule, using assumptions")];
wenzelm@5842
   474
wenzelm@5842
   475
wenzelm@5842
   476
wenzelm@5842
   477
(** proof methods **)
wenzelm@5842
   478
wenzelm@5842
   479
(* simplification *)
wenzelm@5842
   480
wenzelm@8382
   481
val colon = Args.$$$ ":";
wenzelm@8382
   482
wenzelm@5928
   483
val simp_modifiers =
wenzelm@8467
   484
 [Args.$$$ simpN -- colon >> K (I, simp_add_local),
wenzelm@8467
   485
  Args.$$$ simpN -- Args.$$$ addN -- colon >> K (I, simp_add_local),
wenzelm@8382
   486
  Args.$$$ simpN -- Args.$$$ delN -- colon >> K (I, simp_del_local),
wenzelm@8382
   487
  Args.$$$ simpN -- Args.$$$ onlyN -- colon >> K (map_local_simpset clear_ss, simp_add_local),
wenzelm@8382
   488
  Args.$$$ otherN -- colon >> K (I, I)];
wenzelm@5928
   489
wenzelm@5928
   490
val simp_modifiers' =
wenzelm@8382
   491
 [Args.$$$ addN -- colon >> K (I, simp_add_local),
wenzelm@8382
   492
  Args.$$$ delN -- colon >> K (I, simp_del_local),
wenzelm@8382
   493
  Args.$$$ onlyN -- colon >> K (map_local_simpset clear_ss, simp_add_local),
wenzelm@8382
   494
  Args.$$$ otherN -- colon >> K (I, I)];
wenzelm@5928
   495
wenzelm@8467
   496
fun simp_method more_mods tac =
wenzelm@7558
   497
  (fn prems => fn ctxt => Method.METHOD (fn facts =>
wenzelm@8170
   498
    ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (get_local_simpset ctxt)))
wenzelm@8467
   499
  |> Method.bang_sectioned_args (more_mods @ simp_modifiers');
wenzelm@8170
   500
wenzelm@8467
   501
fun simp_method' more_mods tac =
wenzelm@8170
   502
  (fn prems => fn ctxt => Method.METHOD (fn facts =>
wenzelm@8168
   503
    HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (get_local_simpset ctxt))))
wenzelm@8467
   504
  |> Method.bang_sectioned_args (more_mods @ simp_modifiers');
nipkow@7568
   505
  
wenzelm@5842
   506
wenzelm@5842
   507
(* setup_methods *)
wenzelm@5842
   508
wenzelm@8467
   509
fun setup_methods more_mods = Method.add_methods
wenzelm@8467
   510
 [(simpN,               simp_method' more_mods
wenzelm@8467
   511
    (CHANGED oo asm_full_simp_tac), "full simplification"),
wenzelm@8467
   512
  ("simp_all",          simp_method  more_mods (CHANGED o ALLGOALS o asm_full_simp_tac),
wenzelm@8170
   513
    "full simplification (all goals)"),
wenzelm@8467
   514
  ("simp_tac",          simp_method' more_mods simp_tac, "simp_tac (improper!)"),
wenzelm@8467
   515
  ("asm_simp_tac",      simp_method' more_mods asm_simp_tac, "asm_simp_tac (improper!)"),
wenzelm@8467
   516
  ("full_simp_tac",     simp_method' more_mods full_simp_tac, "full_simp_tac (improper!)"),
wenzelm@8467
   517
  ("asm_full_simp_tac", simp_method' more_mods asm_full_simp_tac, "asm_full_simp_tac (improper!)"),
wenzelm@8467
   518
  ("asm_lr_simp_tac",   simp_method' more_mods asm_lr_simp_tac, "asm_lr_simp_tac (improper!)")];
wenzelm@5842
   519
wenzelm@5842
   520
wenzelm@5842
   521
wenzelm@4795
   522
(** theory setup **)
wenzelm@4795
   523
wenzelm@8467
   524
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs];
wenzelm@8467
   525
fun method_setup mods = [setup_methods mods];
wenzelm@4795
   526
wenzelm@8228
   527
fun easy_setup reflect trivs =
wenzelm@8228
   528
  let
wenzelm@8228
   529
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@8228
   530
wenzelm@8228
   531
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@8228
   532
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@8228
   533
wenzelm@8228
   534
    (*no premature instantiation of variables during simplification*)
wenzelm@8228
   535
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@8228
   536
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@8228
   537
wenzelm@8243
   538
    fun mk_eq thm =
wenzelm@8228
   539
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@8228
   540
      else [thm RS reflect] handle THM _ => [];
wenzelm@8228
   541
wenzelm@8243
   542
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@8243
   543
wenzelm@8228
   544
    fun init_ss thy =
wenzelm@8228
   545
      (simpset_ref_of thy :=
wenzelm@8228
   546
        empty_ss setsubgoaler asm_simp_tac
wenzelm@8228
   547
        setSSolver safe_solver
wenzelm@8228
   548
        setSolver unsafe_solver
wenzelm@8228
   549
        setmksimps mksimps; thy);
wenzelm@8467
   550
  in setup @ method_setup [] @ [init_ss] end;
wenzelm@8228
   551
wenzelm@4795
   552
wenzelm@8667
   553
wenzelm@8667
   554
(** outer syntax **)
wenzelm@8667
   555
wenzelm@8667
   556
val print_simpsetP =
wenzelm@8667
   557
  OuterSyntax.improper_command "print_simpset" "print context of Simplifier"
wenzelm@8667
   558
    OuterSyntax.Keyword.diag
wenzelm@8667
   559
    (Scan.succeed (Toplevel.keep
wenzelm@8667
   560
      (Toplevel.node_case print_simpset (print_local_simpset o Proof.context_of))));
wenzelm@8667
   561
wenzelm@8667
   562
val _ = OuterSyntax.add_parsers [print_simpsetP];
wenzelm@8667
   563
clasohm@1243
   564
end;
wenzelm@4795
   565
wenzelm@4795
   566
wenzelm@4795
   567
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   568
open BasicSimplifier;