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