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