src/Provers/simplifier.ML
author nipkow
Fri May 17 11:25:07 2002 +0200 (2002-05-17 ago)
changeset 13157 4a4599f78f18
parent 12311 ce5f9e61c037
child 13462 56610e2ba220
permissions -rw-r--r--
allowed more general split rules to cope with div/mod 2
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
berghofe@10412
     5
Generic simplifier, suitable for most logics.  See Pure/meta_simplifier.ML
berghofe@10412
     6
for the 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
wenzelm@9715
    11
  addSolver addsimps delsimps addeqcongs deleqcongs addcongs delcongs
wenzelm@9715
    12
  setmksimps setmkeqTrue setmkcong 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@11669
    24
   {mss: MetaSimplifier.meta_simpset,
wenzelm@9715
    25
    mk_cong: thm -> thm,
wenzelm@9715
    26
    subgoal_tac: simpset -> int -> tactic,
wenzelm@9715
    27
    loop_tacs: (string * (int -> tactic)) list,
nipkow@7568
    28
    unsafe_solvers: solver list,
wenzelm@9715
    29
    solvers: solver list};
wenzelm@3551
    30
  val print_ss: simpset -> unit
wenzelm@4366
    31
  val print_simpset: theory -> unit
oheimb@2629
    32
  val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
oheimb@2629
    33
  val setloop:      simpset *             (int -> tactic) -> simpset
nipkow@4668
    34
  val addloop:      simpset *  (string * (int -> tactic)) -> simpset
nipkow@4682
    35
  val delloop:      simpset *   string                    -> simpset
nipkow@7568
    36
  val setSSolver:   simpset * solver -> simpset
nipkow@7568
    37
  val addSSolver:   simpset * solver -> simpset
nipkow@7568
    38
  val setSolver:    simpset * solver -> simpset
nipkow@7568
    39
  val addSolver:    simpset * solver -> simpset
wenzelm@3577
    40
  val setmksimps:   simpset * (thm -> thm list) -> simpset
nipkow@4677
    41
  val setmkeqTrue:  simpset * (thm -> thm option) -> simpset
wenzelm@9715
    42
  val setmkcong:    simpset * (thm -> thm) -> simpset
nipkow@4677
    43
  val setmksym:     simpset * (thm -> thm option) -> simpset
wenzelm@3577
    44
  val settermless:  simpset * (term * term -> bool) -> simpset
wenzelm@3577
    45
  val addsimps:     simpset * thm list -> simpset
wenzelm@3577
    46
  val delsimps:     simpset * thm list -> simpset
wenzelm@3577
    47
  val addeqcongs:   simpset * thm list -> simpset
wenzelm@3577
    48
  val deleqcongs:   simpset * thm list -> simpset
wenzelm@9715
    49
  val addcongs:     simpset * thm list -> simpset
wenzelm@9715
    50
  val delcongs:     simpset * thm list -> simpset
wenzelm@3577
    51
  val addsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    52
  val delsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    53
  val merge_ss:     simpset * simpset -> simpset
wenzelm@3577
    54
  val prems_of_ss:  simpset -> thm list
wenzelm@4080
    55
  val simpset_ref_of_sg: Sign.sg -> simpset ref
wenzelm@4080
    56
  val simpset_ref_of: theory -> simpset ref
wenzelm@4080
    57
  val simpset_of_sg: Sign.sg -> simpset
wenzelm@4080
    58
  val simpset_of: theory -> simpset
wenzelm@4080
    59
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@4080
    60
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4080
    61
  val simpset: unit -> simpset
wenzelm@4080
    62
  val simpset_ref: unit -> simpset ref
clasohm@1243
    63
  val Addsimps: thm list -> unit
clasohm@1243
    64
  val Delsimps: thm list -> unit
wenzelm@2509
    65
  val Addsimprocs: simproc list -> unit
wenzelm@2509
    66
  val Delsimprocs: simproc list -> unit
wenzelm@9715
    67
  val Addcongs: thm list -> unit
wenzelm@9715
    68
  val Delcongs: thm list -> unit
wenzelm@8879
    69
  val generic_simp_tac: bool -> bool * bool * bool -> simpset -> int -> tactic
wenzelm@10004
    70
  val safe_asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    71
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    72
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    73
  val          full_simp_tac: simpset -> int -> tactic
nipkow@4722
    74
  val        asm_lr_simp_tac: simpset -> int -> tactic
oheimb@2629
    75
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    76
  val               Simp_tac:            int -> tactic
oheimb@2629
    77
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    78
  val          Full_simp_tac:            int -> tactic
nipkow@4722
    79
  val        Asm_lr_simp_tac:            int -> tactic
oheimb@2629
    80
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    81
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    82
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    83
  val     full_simplify: simpset -> thm -> thm
wenzelm@3557
    84
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    85
end;
clasohm@0
    86
wenzelm@4795
    87
signature SIMPLIFIER =
wenzelm@4795
    88
sig
wenzelm@4795
    89
  include BASIC_SIMPLIFIER
wenzelm@5082
    90
  val          rewrite: simpset -> cterm -> thm
wenzelm@5082
    91
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@5082
    92
  val     full_rewrite: simpset -> cterm -> thm
wenzelm@5082
    93
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@5842
    94
  val print_local_simpset: Proof.context -> unit
wenzelm@5842
    95
  val get_local_simpset: Proof.context -> simpset
wenzelm@5842
    96
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@8467
    97
  val change_global_ss: (simpset * thm list -> simpset) -> theory attribute
wenzelm@8467
    98
  val change_local_ss: (simpset * thm list -> simpset) -> Proof.context attribute
wenzelm@4855
    99
  val simp_add_global: theory attribute
wenzelm@4855
   100
  val simp_del_global: theory attribute
wenzelm@5842
   101
  val simp_add_local: Proof.context attribute
wenzelm@5842
   102
  val simp_del_local: Proof.context attribute
wenzelm@9715
   103
  val cong_add_global: theory attribute
wenzelm@9715
   104
  val cong_del_global: theory attribute
wenzelm@9715
   105
  val cong_add_local: Proof.context attribute
wenzelm@9715
   106
  val cong_del_local: Proof.context attribute
wenzelm@7177
   107
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@7273
   108
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@5842
   109
  val setup: (theory -> theory) list
wenzelm@8467
   110
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@8467
   111
    -> (theory -> theory) list
wenzelm@8228
   112
  val easy_setup: thm -> thm list -> (theory -> theory) list
wenzelm@4795
   113
end;
wenzelm@2503
   114
wenzelm@2503
   115
structure Simplifier: SIMPLIFIER =
clasohm@0
   116
struct
clasohm@0
   117
wenzelm@2509
   118
wenzelm@2509
   119
(** simplification procedures **)
wenzelm@2509
   120
wenzelm@2509
   121
(* datatype simproc *)
wenzelm@2509
   122
wenzelm@2509
   123
datatype simproc =
wenzelm@3577
   124
  Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
wenzelm@2509
   125
wenzelm@3557
   126
fun mk_simproc name lhss proc =
wenzelm@3557
   127
  Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
wenzelm@3557
   128
wenzelm@3551
   129
fun rep_simproc (Simproc args) = args;
wenzelm@2509
   130
wenzelm@8467
   131
wenzelm@8467
   132
nipkow@7568
   133
(** solvers **)
wenzelm@2509
   134
nipkow@7568
   135
datatype solver = Solver of string * (thm list -> int -> tactic) * stamp;
nipkow@7568
   136
wenzelm@12282
   137
fun mk_solver name solver = Solver (name, solver, stamp());
wenzelm@12282
   138
fun eq_solver (Solver (_, _, s1), Solver(_, _, s2)) = s1 = s2;
nipkow@7568
   139
wenzelm@12282
   140
val merge_solvers = gen_merge_lists eq_solver;
nipkow@7568
   141
nipkow@7568
   142
fun app_sols [] _ _ = no_tac
nipkow@7568
   143
  | app_sols (Solver(_,solver,_)::sols) thms i =
nipkow@7568
   144
       solver thms i ORELSE app_sols sols thms i;
wenzelm@2509
   145
wenzelm@8467
   146
wenzelm@8467
   147
wenzelm@2503
   148
(** simplification sets **)
wenzelm@2503
   149
wenzelm@2503
   150
(* type simpset *)
wenzelm@2503
   151
clasohm@0
   152
datatype simpset =
wenzelm@2503
   153
  Simpset of {
wenzelm@11669
   154
    mss: MetaSimplifier.meta_simpset,
wenzelm@9715
   155
    mk_cong: thm -> thm,
wenzelm@9715
   156
    subgoal_tac: simpset -> int -> tactic,
wenzelm@9715
   157
    loop_tacs: (string * (int -> tactic)) list,
nipkow@7568
   158
    unsafe_solvers: solver list,
wenzelm@9715
   159
    solvers: solver list};
wenzelm@2503
   160
wenzelm@9715
   161
fun make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers solvers =
wenzelm@9715
   162
  Simpset {mss = mss, mk_cong = mk_cong, subgoal_tac = subgoal_tac,
wenzelm@9715
   163
    loop_tacs = loop_tacs, unsafe_solvers = unsafe_solvers, solvers = solvers};
clasohm@0
   164
clasohm@0
   165
val empty_ss =
berghofe@10412
   166
  let val mss = MetaSimplifier.set_mk_sym (MetaSimplifier.empty_mss, Some o symmetric_fun)
wenzelm@9715
   167
  in make_ss mss I (K (K no_tac)) [] [] [] end;
wenzelm@3551
   168
wenzelm@3551
   169
fun rep_ss (Simpset args) = args;
berghofe@10412
   170
fun prems_of_ss (Simpset {mss, ...}) = MetaSimplifier.prems_of_mss mss;
wenzelm@3551
   171
wenzelm@3551
   172
wenzelm@3551
   173
(* print simpsets *)
wenzelm@2503
   174
wenzelm@3551
   175
fun print_ss ss =
wenzelm@3551
   176
  let
wenzelm@3551
   177
    val Simpset {mss, ...} = ss;
berghofe@10412
   178
    val {simps, procs, congs} = MetaSimplifier.dest_mss mss;
wenzelm@2503
   179
wenzelm@3551
   180
    val pretty_thms = map Display.pretty_thm;
wenzelm@3551
   181
    fun pretty_proc (name, lhss) =
wenzelm@3551
   182
      Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
wenzelm@3551
   183
  in
wenzelm@8727
   184
    [Pretty.big_list "simplification rules:" (pretty_thms simps),
wenzelm@8727
   185
      Pretty.big_list "simplification procedures:" (map pretty_proc procs),
wenzelm@8727
   186
      Pretty.big_list "congruences:" (pretty_thms congs)]
wenzelm@8727
   187
    |> Pretty.chunks |> Pretty.writeln
wenzelm@3551
   188
  end;
wenzelm@2503
   189
wenzelm@2503
   190
wenzelm@2503
   191
(* extend simpsets *)
wenzelm@2503
   192
wenzelm@9715
   193
fun (Simpset {mss, mk_cong, subgoal_tac = _, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   194
    setsubgoaler subgoal_tac =
wenzelm@9715
   195
  make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
oheimb@2629
   196
wenzelm@9715
   197
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs = _, unsafe_solvers, solvers})
nipkow@4668
   198
    setloop tac =
wenzelm@9715
   199
  make_ss mss mk_cong subgoal_tac [("", tac)] unsafe_solvers solvers;
wenzelm@2503
   200
wenzelm@9715
   201
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@9715
   202
    addloop tac = make_ss mss mk_cong subgoal_tac
wenzelm@9715
   203
      (case assoc_string (loop_tacs, (#1 tac)) of None => () | Some x =>
wenzelm@9715
   204
        warning ("overwriting looper " ^ quote (#1 tac)); overwrite (loop_tacs, tac))
wenzelm@9715
   205
      unsafe_solvers solvers;
oheimb@2567
   206
wenzelm@9715
   207
fun (ss as Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
oheimb@7214
   208
 delloop name =
wenzelm@6911
   209
  let val (del, rest) = partition (fn (n, _) => n = name) loop_tacs in
wenzelm@6911
   210
    if null del then (warning ("No such looper in simpset: " ^ name); ss)
wenzelm@9715
   211
    else make_ss mss mk_cong subgoal_tac rest unsafe_solvers solvers
nipkow@4682
   212
  end;
nipkow@4682
   213
wenzelm@9715
   214
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, ...})
nipkow@7568
   215
    setSSolver solver =
wenzelm@9715
   216
  make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers [solver];
wenzelm@2503
   217
wenzelm@9715
   218
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@7568
   219
    addSSolver sol =
wenzelm@9715
   220
  make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers (merge_solvers solvers [sol]);
wenzelm@2503
   221
wenzelm@9715
   222
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers = _, solvers})
nipkow@7568
   223
    setSolver unsafe_solver =
wenzelm@9715
   224
  make_ss mss mk_cong subgoal_tac loop_tacs [unsafe_solver] solvers;
nipkow@7568
   225
wenzelm@9715
   226
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@7568
   227
    addSolver sol =
wenzelm@9715
   228
  make_ss mss mk_cong subgoal_tac loop_tacs (merge_solvers unsafe_solvers [sol]) solvers;
wenzelm@2503
   229
wenzelm@9715
   230
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   231
    setmksimps mk_simps =
berghofe@10412
   232
  make_ss (MetaSimplifier.set_mk_rews (mss, mk_simps)) mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
wenzelm@2509
   233
wenzelm@9715
   234
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   235
    setmkeqTrue mk_eq_True =
berghofe@10412
   236
  make_ss (MetaSimplifier.set_mk_eq_True (mss, mk_eq_True)) mk_cong subgoal_tac loop_tacs
wenzelm@9715
   237
    unsafe_solvers solvers;
nipkow@4677
   238
wenzelm@9715
   239
fun (Simpset {mss, mk_cong = _, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@9715
   240
    setmkcong mk_cong =
wenzelm@9715
   241
  make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
wenzelm@9715
   242
wenzelm@9715
   243
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
nipkow@4677
   244
    setmksym mksym =
berghofe@10412
   245
  make_ss (MetaSimplifier.set_mk_sym (mss, mksym)) mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
nipkow@4677
   246
wenzelm@9715
   247
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs,  unsafe_solvers, solvers})
wenzelm@3551
   248
    settermless termless =
berghofe@10412
   249
  make_ss (MetaSimplifier.set_termless (mss, termless)) mk_cong subgoal_tac loop_tacs
wenzelm@9715
   250
    unsafe_solvers solvers;
wenzelm@3551
   251
wenzelm@9715
   252
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   253
    addsimps rews =
berghofe@10412
   254
  make_ss (MetaSimplifier.add_simps (mss, rews)) mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
wenzelm@2503
   255
wenzelm@9715
   256
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   257
    delsimps rews =
berghofe@10412
   258
  make_ss (MetaSimplifier.del_simps (mss, rews)) mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
wenzelm@2503
   259
wenzelm@9715
   260
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   261
    addeqcongs newcongs =
berghofe@10412
   262
  make_ss (MetaSimplifier.add_congs (mss, newcongs)) mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
wenzelm@9715
   263
wenzelm@9715
   264
fun (Simpset {mss, subgoal_tac, mk_cong, loop_tacs, unsafe_solvers, solvers})
wenzelm@9715
   265
    deleqcongs oldcongs =
berghofe@10412
   266
  make_ss (MetaSimplifier.del_congs (mss, oldcongs)) mk_cong subgoal_tac loop_tacs unsafe_solvers solvers;
wenzelm@2509
   267
wenzelm@9715
   268
fun (ss as Simpset {mk_cong, ...}) addcongs newcongs =
wenzelm@9715
   269
  ss addeqcongs map mk_cong newcongs;
oheimb@2629
   270
wenzelm@9715
   271
fun (ss as Simpset {mk_cong, ...}) delcongs oldcongs =
wenzelm@9715
   272
  ss deleqcongs map mk_cong oldcongs;
wenzelm@9715
   273
wenzelm@9715
   274
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   275
    addsimprocs simprocs =
berghofe@10412
   276
  make_ss (MetaSimplifier.add_simprocs (mss, map rep_simproc simprocs)) mk_cong subgoal_tac loop_tacs
wenzelm@9715
   277
    unsafe_solvers solvers;
wenzelm@2509
   278
wenzelm@9715
   279
fun (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers})
wenzelm@3551
   280
    delsimprocs simprocs =
berghofe@10412
   281
  make_ss (MetaSimplifier.del_simprocs (mss, map rep_simproc simprocs)) mk_cong subgoal_tac
wenzelm@9715
   282
    loop_tacs unsafe_solvers solvers;
wenzelm@2503
   283
wenzelm@9715
   284
fun clear_ss (Simpset {mss, mk_cong, subgoal_tac, loop_tacs = _, unsafe_solvers, solvers}) =
berghofe@10412
   285
  make_ss (MetaSimplifier.clear_mss mss) mk_cong subgoal_tac [] unsafe_solvers solvers;
wenzelm@6911
   286
wenzelm@2503
   287
nipkow@7568
   288
(* merge simpsets *)
wenzelm@8467
   289
wenzelm@8467
   290
(*ignores subgoal_tac of 2nd simpset!*)
wenzelm@2503
   291
wenzelm@3551
   292
fun merge_ss
wenzelm@9715
   293
   (Simpset {mss = mss1, mk_cong, loop_tacs = loop_tacs1, subgoal_tac,
nipkow@7568
   294
             unsafe_solvers = unsafe_solvers1, solvers = solvers1},
wenzelm@9715
   295
    Simpset {mss = mss2, mk_cong = _, loop_tacs = loop_tacs2, subgoal_tac = _,
wenzelm@9715
   296
             unsafe_solvers = unsafe_solvers2, solvers = solvers2}) =
berghofe@10412
   297
  make_ss (MetaSimplifier.merge_mss (mss1, mss2)) mk_cong subgoal_tac
wenzelm@9715
   298
    (merge_alists loop_tacs1 loop_tacs2)
wenzelm@9715
   299
    (merge_solvers unsafe_solvers1 unsafe_solvers2)
wenzelm@9715
   300
    (merge_solvers solvers1 solvers2);
wenzelm@2503
   301
wenzelm@3557
   302
wenzelm@8467
   303
wenzelm@5024
   304
(** global and local simpset data **)
wenzelm@4080
   305
wenzelm@5842
   306
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   307
wenzelm@5842
   308
structure GlobalSimpsetArgs =
wenzelm@5024
   309
struct
wenzelm@5842
   310
  val name = "Provers/simpset";
wenzelm@5024
   311
  type T = simpset ref;
clasohm@0
   312
wenzelm@5024
   313
  val empty = ref empty_ss;
wenzelm@6556
   314
  fun copy (ref ss) = (ref ss): T;            (*create new reference!*)
wenzelm@6556
   315
  val prep_ext = copy;
wenzelm@5024
   316
  fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@5024
   317
  fun print _ (ref ss) = print_ss ss;
wenzelm@5024
   318
end;
wenzelm@4080
   319
wenzelm@5842
   320
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   321
val print_simpset = GlobalSimpset.print;
wenzelm@5842
   322
val simpset_ref_of_sg = GlobalSimpset.get_sg;
wenzelm@5842
   323
val simpset_ref_of = GlobalSimpset.get;
wenzelm@4080
   324
wenzelm@4080
   325
wenzelm@4795
   326
(* access global simpset *)
clasohm@0
   327
wenzelm@4080
   328
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   329
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   330
wenzelm@6391
   331
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   332
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   333
wenzelm@5028
   334
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   335
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   336
wenzelm@4080
   337
wenzelm@4795
   338
(* change global simpset *)
wenzelm@4080
   339
wenzelm@7177
   340
fun change_simpset_of f x thy =
wenzelm@7177
   341
  let val r = simpset_ref_of thy
wenzelm@7177
   342
  in r := f (! r, x); thy end;
wenzelm@7177
   343
wenzelm@7177
   344
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   345
wenzelm@4080
   346
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   347
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   348
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   349
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@9715
   350
val Addcongs = change_simpset (op addcongs);
wenzelm@9715
   351
val Delcongs = change_simpset (op delcongs);
wenzelm@2509
   352
clasohm@0
   353
wenzelm@5842
   354
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   355
wenzelm@5842
   356
structure LocalSimpsetArgs =
wenzelm@5842
   357
struct
wenzelm@5842
   358
  val name = "Provers/simpset";
wenzelm@5842
   359
  type T = simpset;
wenzelm@5842
   360
  val init = simpset_of;
wenzelm@5842
   361
  fun print _ ss = print_ss ss;
wenzelm@5842
   362
end;
wenzelm@4795
   363
wenzelm@5842
   364
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   365
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   366
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   367
val put_local_simpset = LocalSimpset.put;
wenzelm@7273
   368
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@4795
   369
wenzelm@4795
   370
wenzelm@5886
   371
(* attributes *)
wenzelm@5886
   372
wenzelm@5886
   373
fun change_global_ss f (thy, th) =
wenzelm@5886
   374
  let val r = simpset_ref_of thy
wenzelm@6096
   375
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   376
wenzelm@5886
   377
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   378
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   379
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   380
wenzelm@5886
   381
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   382
val simp_del_global = change_global_ss (op delsimps);
wenzelm@5886
   383
val simp_add_local = change_local_ss (op addsimps);
wenzelm@5886
   384
val simp_del_local = change_local_ss (op delsimps);
wenzelm@5886
   385
wenzelm@9715
   386
val cong_add_global = change_global_ss (op addcongs);
wenzelm@9715
   387
val cong_del_global = change_global_ss (op delcongs);
wenzelm@9715
   388
val cong_add_local = change_local_ss (op addcongs);
wenzelm@9715
   389
val cong_del_local = change_local_ss (op delcongs);
wenzelm@9715
   390
wenzelm@5886
   391
wenzelm@3557
   392
wenzelm@2503
   393
(** simplification tactics **)
clasohm@0
   394
wenzelm@9715
   395
fun solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers) mss =
wenzelm@3557
   396
  let
wenzelm@3557
   397
    val ss =
wenzelm@9715
   398
      make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers unsafe_solvers;
nipkow@4612
   399
    val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
wenzelm@3557
   400
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   401
nipkow@4668
   402
fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
wenzelm@3557
   403
wenzelm@8879
   404
(*note: may instantiate unknowns that appear also in other subgoals*)
wenzelm@8879
   405
fun generic_simp_tac safe mode =
wenzelm@9715
   406
  fn (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers, ...}) =>
wenzelm@3551
   407
    let
wenzelm@8879
   408
      val solvs = app_sols (if safe then solvers else unsafe_solvers);
nipkow@4612
   409
      fun simp_loop_tac i =
nipkow@4612
   410
        asm_rewrite_goal_tac mode
wenzelm@9715
   411
          (solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers))
nipkow@4612
   412
          mss i
wenzelm@11669
   413
        THEN (solvs (MetaSimplifier.prems_of_mss mss) i ORELSE
nipkow@4668
   414
              TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
nipkow@4612
   415
    in simp_loop_tac end;
clasohm@0
   416
wenzelm@10004
   417
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@10004
   418
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@10004
   419
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@10004
   420
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@10004
   421
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@10004
   422
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
clasohm@0
   423
oheimb@2629
   424
wenzelm@4795
   425
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   426
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   427
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   428
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   429
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   430
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   431
wenzelm@3557
   432
wenzelm@4795
   433
wenzelm@5082
   434
(** simplification rules and conversions **)
wenzelm@3557
   435
wenzelm@9715
   436
fun simp rew mode
wenzelm@9715
   437
     (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
wenzelm@3557
   438
  let
wenzelm@9715
   439
    val tacf = solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers);
wenzelm@4271
   440
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@5082
   441
  in rew mode prover mss thm end;
wenzelm@5082
   442
berghofe@10412
   443
val simp_thm = simp MetaSimplifier.rewrite_thm;
berghofe@10412
   444
val simp_cterm = simp MetaSimplifier.rewrite_cterm;
wenzelm@3557
   445
wenzelm@5082
   446
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   447
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   448
val     full_simplify = simp_thm (true, false, false);
wenzelm@5082
   449
val asm_full_simplify = simp_thm (true, true, false);
wenzelm@5082
   450
wenzelm@5082
   451
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   452
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   453
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@5082
   454
val asm_full_rewrite = simp_cterm (true, true, false);
wenzelm@3557
   455
wenzelm@3557
   456
wenzelm@4795
   457
wenzelm@5886
   458
(** concrete syntax of attributes **)
wenzelm@5842
   459
wenzelm@5886
   460
(* add / del *)
wenzelm@5842
   461
wenzelm@5928
   462
val simpN = "simp";
wenzelm@9715
   463
val congN = "cong";
berghofe@10412
   464
val addN = "add";
berghofe@10412
   465
val delN = "del";
wenzelm@6911
   466
val onlyN = "only";
wenzelm@9899
   467
val no_asmN = "no_asm";
wenzelm@9899
   468
val no_asm_useN = "no_asm_use";
wenzelm@9899
   469
val no_asm_simpN = "no_asm_simp";
wenzelm@5842
   470
wenzelm@8634
   471
val simp_attr =
wenzelm@8634
   472
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@8634
   473
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@5842
   474
wenzelm@9715
   475
val cong_attr =
wenzelm@9715
   476
 (Attrib.add_del_args cong_add_global cong_del_global,
wenzelm@9715
   477
  Attrib.add_del_args cong_add_local cong_del_local);
wenzelm@9715
   478
wenzelm@5842
   479
wenzelm@5842
   480
(* conversions *)
wenzelm@5842
   481
wenzelm@9899
   482
local
wenzelm@9899
   483
wenzelm@9899
   484
fun conv_mode x =
wenzelm@9899
   485
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@9899
   486
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@9899
   487
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@9899
   488
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@9899
   489
wenzelm@11938
   490
fun simplified_att get args =
wenzelm@11938
   491
  Attrib.syntax (conv_mode -- args >> (fn (f, ths) =>
wenzelm@11938
   492
    Drule.rule_attribute (fn x => f ((if null ths then I else clear_ss) (get x) addsimps ths))));
wenzelm@9899
   493
wenzelm@9899
   494
in
wenzelm@9899
   495
wenzelm@9899
   496
val simplified_attr =
wenzelm@11938
   497
 (simplified_att simpset_of Attrib.global_thmss,
wenzelm@11938
   498
  simplified_att get_local_simpset Attrib.local_thmss);
wenzelm@9899
   499
wenzelm@9899
   500
end;
wenzelm@5842
   501
wenzelm@5842
   502
wenzelm@5842
   503
(* setup_attrs *)
wenzelm@5842
   504
wenzelm@5842
   505
val setup_attrs = Attrib.add_attributes
wenzelm@9899
   506
 [(simpN, simp_attr, "declaration of simplification rule"),
wenzelm@9899
   507
  (congN, cong_attr, "declaration of Simplifier congruence rule"),
wenzelm@9899
   508
  ("simplified", simplified_attr, "simplified rule")];
wenzelm@5842
   509
wenzelm@5842
   510
wenzelm@5842
   511
wenzelm@5842
   512
(** proof methods **)
wenzelm@5842
   513
wenzelm@5842
   514
(* simplification *)
wenzelm@5842
   515
wenzelm@8700
   516
val simp_options =
wenzelm@9899
   517
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@9899
   518
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@9899
   519
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
wenzelm@8700
   520
  Scan.succeed asm_full_simp_tac);
wenzelm@8700
   521
wenzelm@9715
   522
val cong_modifiers =
wenzelm@9720
   523
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local):Method.modifier),
wenzelm@10034
   524
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add_local),
wenzelm@10034
   525
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del_local)];
wenzelm@9715
   526
wenzelm@5928
   527
val simp_modifiers =
wenzelm@8815
   528
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   529
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   530
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   531
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   532
   @ cong_modifiers;
wenzelm@5928
   533
wenzelm@5928
   534
val simp_modifiers' =
wenzelm@10034
   535
 [Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   536
  Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   537
  Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   538
   @ cong_modifiers;
wenzelm@5928
   539
wenzelm@8700
   540
fun simp_args more_mods =
wenzelm@8700
   541
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options) (more_mods @ simp_modifiers');
wenzelm@8700
   542
wenzelm@8170
   543
wenzelm@8700
   544
fun simp_method (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@8700
   545
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@10821
   546
    (CHANGED_PROP o ALLGOALS o tac) (get_local_simpset ctxt));
wenzelm@8700
   547
wenzelm@8700
   548
fun simp_method' (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@10821
   549
  HEADGOAL (Method.insert_tac (prems @ facts) THEN'
wenzelm@10821
   550
      (CHANGED_PROP oo tac) (get_local_simpset ctxt)));
wenzelm@9715
   551
wenzelm@5842
   552
wenzelm@5842
   553
(* setup_methods *)
wenzelm@5842
   554
wenzelm@8467
   555
fun setup_methods more_mods = Method.add_methods
wenzelm@8700
   556
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@8700
   557
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@5842
   558
wenzelm@5842
   559
wenzelm@5842
   560
wenzelm@4795
   561
(** theory setup **)
wenzelm@4795
   562
wenzelm@8467
   563
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs];
wenzelm@8467
   564
fun method_setup mods = [setup_methods mods];
wenzelm@4795
   565
wenzelm@8228
   566
fun easy_setup reflect trivs =
wenzelm@8228
   567
  let
wenzelm@8228
   568
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@8228
   569
wenzelm@8228
   570
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@8228
   571
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@8228
   572
wenzelm@8228
   573
    (*no premature instantiation of variables during simplification*)
wenzelm@8228
   574
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@8228
   575
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@8228
   576
wenzelm@8243
   577
    fun mk_eq thm =
wenzelm@8228
   578
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@8228
   579
      else [thm RS reflect] handle THM _ => [];
wenzelm@8228
   580
wenzelm@8243
   581
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@8243
   582
wenzelm@8228
   583
    fun init_ss thy =
wenzelm@8228
   584
      (simpset_ref_of thy :=
wenzelm@8228
   585
        empty_ss setsubgoaler asm_simp_tac
wenzelm@8228
   586
        setSSolver safe_solver
wenzelm@8228
   587
        setSolver unsafe_solver
wenzelm@8228
   588
        setmksimps mksimps; thy);
wenzelm@8467
   589
  in setup @ method_setup [] @ [init_ss] end;
wenzelm@8228
   590
wenzelm@4795
   591
wenzelm@8667
   592
wenzelm@8667
   593
(** outer syntax **)
wenzelm@8667
   594
wenzelm@8667
   595
val print_simpsetP =
wenzelm@8667
   596
  OuterSyntax.improper_command "print_simpset" "print context of Simplifier"
wenzelm@8667
   597
    OuterSyntax.Keyword.diag
wenzelm@9513
   598
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o (Toplevel.keep
wenzelm@9010
   599
      (Toplevel.node_case print_simpset (print_local_simpset o Proof.context_of)))));
wenzelm@8667
   600
wenzelm@8667
   601
val _ = OuterSyntax.add_parsers [print_simpsetP];
wenzelm@8667
   602
clasohm@1243
   603
end;
wenzelm@4795
   604
wenzelm@4795
   605
wenzelm@4795
   606
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   607
open BasicSimplifier;