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