src/Provers/simplifier.ML
author wenzelm
Thu Sep 07 20:56:04 2000 +0200 (2000-09-07 ago)
changeset 9899 5a9081c3b869
parent 9861 b2a6d854d6da
child 9952 24914e42b857
permissions -rw-r--r--
tuned att names / msgs;
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
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@3551
    24
   {mss: 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
oheimb@2629
    70
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    71
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    72
  val          full_simp_tac: simpset -> int -> tactic
nipkow@4722
    73
  val        asm_lr_simp_tac: simpset -> int -> tactic
oheimb@2629
    74
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    75
  val               Simp_tac:            int -> tactic
oheimb@2629
    76
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    77
  val          Full_simp_tac:            int -> tactic
nipkow@4722
    78
  val        Asm_lr_simp_tac:            int -> tactic
oheimb@2629
    79
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    80
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    81
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    82
  val     full_simplify: simpset -> thm -> thm
wenzelm@3557
    83
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    84
end;
clasohm@0
    85
wenzelm@4795
    86
signature SIMPLIFIER =
wenzelm@4795
    87
sig
wenzelm@4795
    88
  include BASIC_SIMPLIFIER
wenzelm@5082
    89
  val          rewrite: simpset -> cterm -> thm
wenzelm@5082
    90
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@5082
    91
  val     full_rewrite: simpset -> cterm -> thm
wenzelm@5082
    92
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@5842
    93
  val print_local_simpset: Proof.context -> unit
wenzelm@5842
    94
  val get_local_simpset: Proof.context -> simpset
wenzelm@5842
    95
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@8467
    96
  val change_global_ss: (simpset * thm list -> simpset) -> theory attribute
wenzelm@8467
    97
  val change_local_ss: (simpset * thm list -> simpset) -> Proof.context attribute
wenzelm@4855
    98
  val simp_add_global: theory attribute
wenzelm@4855
    99
  val simp_del_global: theory attribute
wenzelm@5842
   100
  val simp_add_local: Proof.context attribute
wenzelm@5842
   101
  val simp_del_local: Proof.context attribute
wenzelm@9715
   102
  val cong_add_global: theory attribute
wenzelm@9715
   103
  val cong_del_global: theory attribute
wenzelm@9715
   104
  val cong_add_local: Proof.context attribute
wenzelm@9715
   105
  val cong_del_local: Proof.context attribute
wenzelm@7177
   106
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@7273
   107
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@5842
   108
  val setup: (theory -> theory) list
wenzelm@8467
   109
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@8467
   110
    -> (theory -> theory) list
wenzelm@8228
   111
  val easy_setup: thm -> thm list -> (theory -> theory) list
wenzelm@4795
   112
end;
wenzelm@2503
   113
wenzelm@2503
   114
structure Simplifier: SIMPLIFIER =
clasohm@0
   115
struct
clasohm@0
   116
wenzelm@2509
   117
wenzelm@2509
   118
(** simplification procedures **)
wenzelm@2509
   119
wenzelm@2509
   120
(* datatype simproc *)
wenzelm@2509
   121
wenzelm@2509
   122
datatype simproc =
wenzelm@3577
   123
  Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
wenzelm@2509
   124
wenzelm@3557
   125
fun mk_simproc name lhss proc =
wenzelm@3557
   126
  Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
wenzelm@3557
   127
wenzelm@3551
   128
fun rep_simproc (Simproc args) = args;
wenzelm@2509
   129
wenzelm@8467
   130
wenzelm@8467
   131
nipkow@7568
   132
(** solvers **)
wenzelm@2509
   133
nipkow@7568
   134
datatype solver = Solver of string * (thm list -> int -> tactic) * stamp;
nipkow@7568
   135
nipkow@7568
   136
fun mk_solver name solver = Solver(name, solver, stamp());
nipkow@7568
   137
nipkow@7568
   138
fun eq_solver (Solver(_,_,s1),Solver(_,_,s2)) = s1=s2;
nipkow@7568
   139
nipkow@7568
   140
val merge_solvers = generic_merge eq_solver I I;
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@2503
   154
    mss: 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 =
wenzelm@6911
   166
  let val mss = Thm.set_mk_sym (Thm.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;
wenzelm@3551
   170
fun prems_of_ss (Simpset {mss, ...}) = Thm.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;
wenzelm@3551
   178
    val {simps, procs, congs} = Thm.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 =
wenzelm@9715
   232
  make_ss (Thm.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 =
wenzelm@9715
   236
  make_ss (Thm.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 =
wenzelm@9715
   245
  make_ss (Thm.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 =
wenzelm@9715
   249
  make_ss (Thm.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 =
wenzelm@9715
   254
  make_ss (Thm.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 =
wenzelm@9715
   258
  make_ss (Thm.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 =
wenzelm@9715
   262
  make_ss (Thm.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 =
wenzelm@9715
   266
  make_ss (Thm.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 =
wenzelm@9715
   276
  make_ss (Thm.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 =
wenzelm@9715
   281
  make_ss (Thm.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}) =
wenzelm@9715
   285
  make_ss (Thm.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}) =
wenzelm@9715
   297
  make_ss (Thm.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@8879
   413
        THEN (solvs (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@8879
   417
val          simp_tac = generic_simp_tac false (false, false, false);
wenzelm@8879
   418
val      asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@8879
   419
val     full_simp_tac = generic_simp_tac false (true,  false, false);
wenzelm@8879
   420
val   asm_lr_simp_tac = generic_simp_tac false (true,  true, false);
wenzelm@8879
   421
val asm_full_simp_tac = generic_simp_tac false (true,  true, true);
clasohm@0
   422
oheimb@2629
   423
wenzelm@4795
   424
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   425
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   426
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   427
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   428
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   429
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   430
wenzelm@3557
   431
wenzelm@4795
   432
wenzelm@5082
   433
(** simplification rules and conversions **)
wenzelm@3557
   434
wenzelm@9715
   435
fun simp rew mode
wenzelm@9715
   436
     (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
wenzelm@3557
   437
  let
wenzelm@9715
   438
    val tacf = solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers);
wenzelm@4271
   439
    fun prover m th = apsome fst (Seq.pull (tacf m th));
wenzelm@5082
   440
  in rew mode prover mss thm end;
wenzelm@5082
   441
wenzelm@5082
   442
val simp_thm = simp Drule.rewrite_thm;
wenzelm@5082
   443
val simp_cterm = simp Drule.rewrite_cterm;
wenzelm@3557
   444
wenzelm@5082
   445
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   446
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   447
val     full_simplify = simp_thm (true, false, false);
wenzelm@5082
   448
val asm_full_simplify = simp_thm (true, true, false);
wenzelm@5082
   449
wenzelm@5082
   450
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   451
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   452
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@5082
   453
val asm_full_rewrite = simp_cterm (true, true, false);
wenzelm@3557
   454
wenzelm@3557
   455
wenzelm@4795
   456
wenzelm@5886
   457
(** concrete syntax of attributes **)
wenzelm@5842
   458
wenzelm@5886
   459
(* add / del *)
wenzelm@5842
   460
wenzelm@5928
   461
val simpN = "simp";
wenzelm@9715
   462
val congN = "cong";
wenzelm@5928
   463
val addN = "add";
wenzelm@5928
   464
val delN = "del";
wenzelm@6911
   465
val onlyN = "only";
wenzelm@9899
   466
val no_asmN = "no_asm";
wenzelm@9899
   467
val no_asm_useN = "no_asm_use";
wenzelm@9899
   468
val no_asm_simpN = "no_asm_simp";
wenzelm@5842
   469
wenzelm@8634
   470
val simp_attr =
wenzelm@8634
   471
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@8634
   472
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@5842
   473
wenzelm@9715
   474
val cong_attr =
wenzelm@9715
   475
 (Attrib.add_del_args cong_add_global cong_del_global,
wenzelm@9715
   476
  Attrib.add_del_args cong_add_local cong_del_local);
wenzelm@9715
   477
wenzelm@5842
   478
wenzelm@5842
   479
(* conversions *)
wenzelm@5842
   480
wenzelm@9899
   481
local
wenzelm@9899
   482
wenzelm@9899
   483
fun conv_mode x =
wenzelm@9899
   484
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@9899
   485
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@9899
   486
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@9899
   487
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@9899
   488
wenzelm@9899
   489
fun simplified_att get =
wenzelm@9899
   490
  Attrib.syntax (conv_mode >> (fn f => Drule.rule_attribute (f o get)));
wenzelm@9899
   491
wenzelm@9899
   492
in
wenzelm@9899
   493
wenzelm@9899
   494
val simplified_attr =
wenzelm@9899
   495
  (simplified_att simpset_of, simplified_att get_local_simpset);
wenzelm@9899
   496
wenzelm@9899
   497
end;
wenzelm@5842
   498
wenzelm@5842
   499
wenzelm@5842
   500
(* setup_attrs *)
wenzelm@5842
   501
wenzelm@5842
   502
val setup_attrs = Attrib.add_attributes
wenzelm@9899
   503
 [(simpN, simp_attr, "declaration of simplification rule"),
wenzelm@9899
   504
  (congN, cong_attr, "declaration of Simplifier congruence rule"),
wenzelm@9899
   505
  ("simplified", simplified_attr, "simplified rule")];
wenzelm@5842
   506
wenzelm@5842
   507
wenzelm@5842
   508
wenzelm@5842
   509
(** proof methods **)
wenzelm@5842
   510
wenzelm@5842
   511
(* simplification *)
wenzelm@5842
   512
wenzelm@8700
   513
val simp_options =
wenzelm@9899
   514
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@9899
   515
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@9899
   516
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
wenzelm@8700
   517
  Scan.succeed asm_full_simp_tac);
wenzelm@8700
   518
wenzelm@9715
   519
val cong_modifiers =
wenzelm@9720
   520
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local):Method.modifier),
wenzelm@9715
   521
  Args.$$$ congN -- Args.$$$ addN -- Args.colon >> K (I, cong_add_local),
wenzelm@9715
   522
  Args.$$$ congN -- Args.$$$ delN -- Args.colon >> K (I, cong_del_local)];
wenzelm@9715
   523
wenzelm@5928
   524
val simp_modifiers =
wenzelm@8815
   525
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
wenzelm@8815
   526
  Args.$$$ simpN -- Args.$$$ addN -- Args.colon >> K (I, simp_add_local),
wenzelm@8815
   527
  Args.$$$ simpN -- Args.$$$ delN -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   528
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   529
   @ cong_modifiers;
wenzelm@5928
   530
wenzelm@5928
   531
val simp_modifiers' =
wenzelm@8815
   532
 [Args.$$$ addN -- Args.colon >> K (I, simp_add_local),
wenzelm@8815
   533
  Args.$$$ delN -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   534
  Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   535
   @ cong_modifiers;
wenzelm@5928
   536
wenzelm@8700
   537
fun simp_args more_mods =
wenzelm@8700
   538
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options) (more_mods @ simp_modifiers');
wenzelm@8700
   539
wenzelm@8170
   540
wenzelm@8700
   541
fun simp_method (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@8700
   542
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@8700
   543
    (CHANGED o ALLGOALS o tac) (get_local_simpset ctxt));
wenzelm@8700
   544
wenzelm@8700
   545
fun simp_method' (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@8700
   546
  HEADGOAL (Method.insert_tac (prems @ facts) THEN' (CHANGED oo tac) (get_local_simpset ctxt)));
wenzelm@9715
   547
wenzelm@5842
   548
wenzelm@5842
   549
(* setup_methods *)
wenzelm@5842
   550
wenzelm@8467
   551
fun setup_methods more_mods = Method.add_methods
wenzelm@8700
   552
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@8700
   553
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@5842
   554
wenzelm@5842
   555
wenzelm@5842
   556
wenzelm@4795
   557
(** theory setup **)
wenzelm@4795
   558
wenzelm@8467
   559
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs];
wenzelm@8467
   560
fun method_setup mods = [setup_methods mods];
wenzelm@4795
   561
wenzelm@8228
   562
fun easy_setup reflect trivs =
wenzelm@8228
   563
  let
wenzelm@8228
   564
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@8228
   565
wenzelm@8228
   566
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@8228
   567
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@8228
   568
wenzelm@8228
   569
    (*no premature instantiation of variables during simplification*)
wenzelm@8228
   570
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@8228
   571
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@8228
   572
wenzelm@8243
   573
    fun mk_eq thm =
wenzelm@8228
   574
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@8228
   575
      else [thm RS reflect] handle THM _ => [];
wenzelm@8228
   576
wenzelm@8243
   577
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@8243
   578
wenzelm@8228
   579
    fun init_ss thy =
wenzelm@8228
   580
      (simpset_ref_of thy :=
wenzelm@8228
   581
        empty_ss setsubgoaler asm_simp_tac
wenzelm@8228
   582
        setSSolver safe_solver
wenzelm@8228
   583
        setSolver unsafe_solver
wenzelm@8228
   584
        setmksimps mksimps; thy);
wenzelm@8467
   585
  in setup @ method_setup [] @ [init_ss] end;
wenzelm@8228
   586
wenzelm@4795
   587
wenzelm@8667
   588
wenzelm@8667
   589
(** outer syntax **)
wenzelm@8667
   590
wenzelm@8667
   591
val print_simpsetP =
wenzelm@8667
   592
  OuterSyntax.improper_command "print_simpset" "print context of Simplifier"
wenzelm@8667
   593
    OuterSyntax.Keyword.diag
wenzelm@9513
   594
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o (Toplevel.keep
wenzelm@9010
   595
      (Toplevel.node_case print_simpset (print_local_simpset o Proof.context_of)))));
wenzelm@8667
   596
wenzelm@8667
   597
val _ = OuterSyntax.add_parsers [print_simpsetP];
wenzelm@8667
   598
clasohm@1243
   599
end;
wenzelm@4795
   600
wenzelm@4795
   601
wenzelm@4795
   602
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   603
open BasicSimplifier;