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