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