src/Provers/simplifier.ML
author paulson
Fri Jan 21 18:00:18 2005 +0100 (2005-01-21 ago)
changeset 15452 e2a721567f67
parent 15036 cab1c1fc1851
child 15496 3263daa96167
permissions -rw-r--r--
Jia Meng: delta simpsets and clasets
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
paulson@15452
     9
(* added: put_delta_simpset, get_delta_simpset
paulson@15452
    10
   changed: simp_add_local
paulson@15452
    11
   07/01/05
paulson@15452
    12
*)
paulson@15452
    13
paulson@15452
    14
wenzelm@4795
    15
signature BASIC_SIMPLIFIER =
clasohm@0
    16
sig
wenzelm@15024
    17
  include BASIC_META_SIMPLIFIER
wenzelm@15036
    18
  type context_solver
wenzelm@15036
    19
  val mk_context_solver: string -> (Proof.context -> thm list -> int -> tactic)
wenzelm@15036
    20
    -> context_solver
wenzelm@15036
    21
  type context_simproc
wenzelm@15036
    22
  val mk_context_simproc: string -> cterm list ->
wenzelm@15036
    23
    (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@4366
    24
  val print_simpset: theory -> unit
wenzelm@4080
    25
  val simpset_ref_of_sg: Sign.sg -> simpset ref
wenzelm@4080
    26
  val simpset_ref_of: theory -> simpset ref
wenzelm@4080
    27
  val simpset_of_sg: Sign.sg -> simpset
wenzelm@4080
    28
  val simpset_of: theory -> simpset
wenzelm@4080
    29
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@4080
    30
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4080
    31
  val simpset: unit -> simpset
wenzelm@4080
    32
  val simpset_ref: unit -> simpset ref
clasohm@1243
    33
  val Addsimps: thm list -> unit
clasohm@1243
    34
  val Delsimps: thm list -> unit
wenzelm@2509
    35
  val Addsimprocs: simproc list -> unit
wenzelm@2509
    36
  val Delsimprocs: simproc list -> unit
wenzelm@9715
    37
  val Addcongs: thm list -> unit
wenzelm@9715
    38
  val Delcongs: thm list -> unit
wenzelm@15036
    39
  val local_simpset_of: Proof.context -> simpset
wenzelm@10004
    40
  val safe_asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    41
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    42
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    43
  val          full_simp_tac: simpset -> int -> tactic
nipkow@4722
    44
  val        asm_lr_simp_tac: simpset -> int -> tactic
oheimb@2629
    45
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    46
  val               Simp_tac:            int -> tactic
oheimb@2629
    47
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    48
  val          Full_simp_tac:            int -> tactic
nipkow@4722
    49
  val        Asm_lr_simp_tac:            int -> tactic
oheimb@2629
    50
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    51
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    52
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    53
  val     full_simplify: simpset -> thm -> thm
schirmer@14814
    54
  val   asm_lr_simplify: simpset -> thm -> thm
wenzelm@3557
    55
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    56
end;
clasohm@0
    57
wenzelm@4795
    58
signature SIMPLIFIER =
wenzelm@4795
    59
sig
wenzelm@4795
    60
  include BASIC_SIMPLIFIER
wenzelm@15036
    61
  val simproc_i: Sign.sg -> string -> term list
wenzelm@15036
    62
    -> (Sign.sg -> simpset -> term -> thm option) -> simproc
wenzelm@15036
    63
  val simproc: Sign.sg -> string -> string list
wenzelm@15036
    64
    -> (Sign.sg -> simpset -> term -> thm option) -> simproc
wenzelm@15036
    65
  val context_simproc_i: Sign.sg -> string -> term list
wenzelm@15036
    66
    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@15036
    67
  val context_simproc: Sign.sg -> string -> string list
wenzelm@15036
    68
    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@5082
    69
  val          rewrite: simpset -> cterm -> thm
wenzelm@5082
    70
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@5082
    71
  val     full_rewrite: simpset -> cterm -> thm
schirmer@14814
    72
  val   asm_lr_rewrite: simpset -> cterm -> thm
wenzelm@5082
    73
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@15036
    74
  val add_context_simprocs: context_simproc list -> theory -> theory
wenzelm@15036
    75
  val del_context_simprocs: context_simproc list -> theory -> theory
wenzelm@15036
    76
  val set_context_subgoaler: (Proof.context -> simpset -> int -> tactic) -> theory -> theory
wenzelm@15036
    77
  val reset_context_subgoaler: theory -> theory
wenzelm@15036
    78
  val add_context_looper: string * (Proof.context -> int -> Tactical.tactic) ->
wenzelm@15036
    79
    theory -> theory
wenzelm@15036
    80
  val del_context_looper: string -> theory -> theory
wenzelm@15036
    81
  val add_context_unsafe_solver: context_solver -> theory -> theory
wenzelm@15036
    82
  val add_context_safe_solver: context_solver -> theory -> theory
wenzelm@5842
    83
  val print_local_simpset: Proof.context -> unit
wenzelm@5842
    84
  val get_local_simpset: Proof.context -> simpset
wenzelm@5842
    85
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@8467
    86
  val change_global_ss: (simpset * thm list -> simpset) -> theory attribute
wenzelm@8467
    87
  val change_local_ss: (simpset * thm list -> simpset) -> Proof.context attribute
wenzelm@4855
    88
  val simp_add_global: theory attribute
wenzelm@4855
    89
  val simp_del_global: theory attribute
wenzelm@5842
    90
  val simp_add_local: Proof.context attribute
wenzelm@5842
    91
  val simp_del_local: Proof.context attribute
wenzelm@9715
    92
  val cong_add_global: theory attribute
wenzelm@9715
    93
  val cong_del_global: theory attribute
wenzelm@9715
    94
  val cong_add_local: Proof.context attribute
wenzelm@9715
    95
  val cong_del_local: Proof.context attribute
wenzelm@7177
    96
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@7273
    97
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@5842
    98
  val setup: (theory -> theory) list
wenzelm@8467
    99
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@8467
   100
    -> (theory -> theory) list
wenzelm@8228
   101
  val easy_setup: thm -> thm list -> (theory -> theory) list
paulson@15452
   102
paulson@15452
   103
  val get_delta_simpset: ProofContext.context -> Thm.thm list
paulson@15452
   104
  val put_delta_simpset: Thm.thm list -> ProofContext.context -> ProofContext.context
paulson@15452
   105
wenzelm@4795
   106
end;
wenzelm@2503
   107
wenzelm@2503
   108
structure Simplifier: SIMPLIFIER =
clasohm@0
   109
struct
clasohm@0
   110
skalberg@15006
   111
open MetaSimplifier;
wenzelm@8467
   112
wenzelm@15024
   113
wenzelm@15036
   114
(** context dependent simpset components **)
wenzelm@15036
   115
wenzelm@15036
   116
(* datatype context_solver *)
wenzelm@15036
   117
wenzelm@15036
   118
datatype context_solver =
wenzelm@15036
   119
  ContextSolver of (string * (Proof.context -> thm list -> int -> tactic)) * stamp;
wenzelm@15036
   120
wenzelm@15036
   121
fun mk_context_solver name f = ContextSolver ((name, f), stamp ());
wenzelm@15036
   122
fun eq_context_solver (ContextSolver (_, id1), ContextSolver (_, id2)) = (id1 = id2);
wenzelm@15036
   123
val merge_context_solvers = gen_merge_lists eq_context_solver;
wenzelm@15036
   124
wenzelm@15036
   125
wenzelm@15036
   126
(* datatype context_simproc *)
wenzelm@15036
   127
wenzelm@15036
   128
datatype context_simproc = ContextSimproc of
wenzelm@15036
   129
  (string * cterm list * (Proof.context -> simpset -> term -> thm option)) * stamp;
wenzelm@15036
   130
wenzelm@15036
   131
fun mk_context_simproc name lhss f = ContextSimproc ((name, lhss, f), stamp ());
wenzelm@15036
   132
fun eq_context_simproc (ContextSimproc (_, id1), ContextSimproc (_, id2)) = (id1 = id2);
wenzelm@15036
   133
val merge_context_simprocs = gen_merge_lists eq_context_simproc;
wenzelm@15036
   134
wenzelm@15036
   135
fun context_simproc_i sg name =
wenzelm@15036
   136
  mk_context_simproc name o map (Thm.cterm_of sg o Logic.varify);
wenzelm@15036
   137
wenzelm@15036
   138
fun context_simproc sg name =
wenzelm@15036
   139
  context_simproc_i sg name o map (Sign.simple_read_term sg TypeInfer.logicT);
wenzelm@15036
   140
wenzelm@15036
   141
wenzelm@15036
   142
(* datatype context_ss *)
wenzelm@15036
   143
wenzelm@15036
   144
datatype context_ss = ContextSS of
wenzelm@15036
   145
 {simprocs: context_simproc list,
wenzelm@15036
   146
  subgoal_tac: (Proof.context -> simpset -> int -> tactic) option,
wenzelm@15036
   147
  loop_tacs: (string * (Proof.context -> int -> tactic)) list,
wenzelm@15036
   148
  unsafe_solvers: context_solver list,
wenzelm@15036
   149
  solvers: context_solver list};
wenzelm@15036
   150
wenzelm@15036
   151
fun context_ss ctxt ss ctxt_ss =
wenzelm@15036
   152
  let
wenzelm@15036
   153
    val ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} = ctxt_ss;
wenzelm@15036
   154
    fun prep_simproc (ContextSimproc ((name, lhss, f), _)) =
wenzelm@15036
   155
      mk_simproc name lhss (K (f ctxt));
wenzelm@15036
   156
    fun add_loop (name, f) simpset = simpset addloop (name, f ctxt);
wenzelm@15036
   157
    fun add_solver add (ContextSolver ((name, f), _)) simpset =
wenzelm@15036
   158
      add (simpset, mk_solver name (f ctxt));
wenzelm@15036
   159
  in
wenzelm@15036
   160
    ((case subgoal_tac of None => ss | Some tac => ss setsubgoaler tac ctxt)
wenzelm@15036
   161
      addsimprocs map prep_simproc simprocs)
wenzelm@15036
   162
    |> fold_rev add_loop loop_tacs
wenzelm@15036
   163
    |> fold_rev (add_solver (op addSolver)) unsafe_solvers
wenzelm@15036
   164
    |> fold_rev (add_solver (op addSSolver)) solvers
wenzelm@15036
   165
  end;
wenzelm@15036
   166
wenzelm@15036
   167
fun make_context_ss (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =
wenzelm@15036
   168
  ContextSS {simprocs = simprocs, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
wenzelm@15036
   169
    unsafe_solvers = unsafe_solvers, solvers = solvers};
wenzelm@15036
   170
wenzelm@15036
   171
val empty_context_ss = make_context_ss ([], None, [], [], []);
wenzelm@15036
   172
wenzelm@15036
   173
fun merge_context_ss (ctxt_ss1, ctxt_ss2) =
wenzelm@15036
   174
  let
wenzelm@15036
   175
    val ContextSS {simprocs = simprocs1, subgoal_tac = subgoal_tac1, loop_tacs = loop_tacs1,
wenzelm@15036
   176
      unsafe_solvers = unsafe_solvers1, solvers = solvers1} = ctxt_ss1;
wenzelm@15036
   177
    val ContextSS {simprocs = simprocs2, subgoal_tac = subgoal_tac2, loop_tacs = loop_tacs2,
wenzelm@15036
   178
      unsafe_solvers = unsafe_solvers2, solvers = solvers2} = ctxt_ss2;
wenzelm@15036
   179
wenzelm@15036
   180
    val simprocs' = merge_context_simprocs simprocs1 simprocs2;
wenzelm@15036
   181
    val subgoal_tac' = (case subgoal_tac1 of None => subgoal_tac2 | some => some);
wenzelm@15036
   182
    val loop_tacs' = merge_alists loop_tacs1 loop_tacs2;
wenzelm@15036
   183
    val unsafe_solvers' = merge_context_solvers unsafe_solvers1 unsafe_solvers2;
wenzelm@15036
   184
    val solvers' = merge_context_solvers solvers1 solvers2;
wenzelm@15036
   185
  in make_context_ss (simprocs', subgoal_tac', loop_tacs', unsafe_solvers', solvers') end;
wenzelm@15036
   186
wenzelm@15036
   187
wenzelm@15036
   188
wenzelm@5024
   189
(** global and local simpset data **)
wenzelm@4080
   190
wenzelm@5842
   191
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   192
wenzelm@5842
   193
structure GlobalSimpsetArgs =
wenzelm@5024
   194
struct
wenzelm@5842
   195
  val name = "Provers/simpset";
wenzelm@15036
   196
  type T = simpset ref * context_ss;
clasohm@0
   197
wenzelm@15036
   198
  val empty = (ref empty_ss, empty_context_ss);
wenzelm@15036
   199
  fun copy (ref ss, ctxt_ss) = (ref ss, ctxt_ss): T;            (*create new reference!*)
wenzelm@6556
   200
  val prep_ext = copy;
wenzelm@15036
   201
  fun merge ((ref ss1, ctxt_ss1), (ref ss2, ctxt_ss2)) =
wenzelm@15036
   202
    (ref (merge_ss (ss1, ss2)), merge_context_ss (ctxt_ss1, ctxt_ss2));
wenzelm@15036
   203
  fun print _ (ref ss, _) = print_ss ss;
wenzelm@5024
   204
end;
wenzelm@4080
   205
wenzelm@5842
   206
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   207
val print_simpset = GlobalSimpset.print;
wenzelm@15036
   208
val simpset_ref_of_sg = #1 o GlobalSimpset.get_sg;
wenzelm@15036
   209
val simpset_ref_of = #1 o GlobalSimpset.get;
wenzelm@15036
   210
val get_context_ss = #2 o GlobalSimpset.get o ProofContext.theory_of;
wenzelm@15036
   211
wenzelm@15036
   212
fun map_context_ss f = GlobalSimpset.map (apsnd
wenzelm@15036
   213
  (fn ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} =>
wenzelm@15036
   214
    make_context_ss (f (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers))));
wenzelm@4080
   215
wenzelm@4080
   216
wenzelm@4795
   217
(* access global simpset *)
clasohm@0
   218
wenzelm@4080
   219
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   220
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   221
wenzelm@6391
   222
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   223
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   224
wenzelm@5028
   225
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   226
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   227
wenzelm@4080
   228
wenzelm@4795
   229
(* change global simpset *)
wenzelm@4080
   230
wenzelm@7177
   231
fun change_simpset_of f x thy =
wenzelm@7177
   232
  let val r = simpset_ref_of thy
wenzelm@7177
   233
  in r := f (! r, x); thy end;
wenzelm@7177
   234
wenzelm@7177
   235
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   236
wenzelm@4080
   237
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   238
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   239
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   240
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@9715
   241
val Addcongs = change_simpset (op addcongs);
wenzelm@9715
   242
val Delcongs = change_simpset (op delcongs);
wenzelm@2509
   243
clasohm@0
   244
wenzelm@15036
   245
(* change context dependent components *)
wenzelm@15036
   246
wenzelm@15036
   247
fun add_context_simprocs procs =
wenzelm@15036
   248
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   249
    (merge_context_simprocs procs simprocs, subgoal_tac, loop_tacs,
wenzelm@15036
   250
      unsafe_solvers, solvers));
wenzelm@15036
   251
wenzelm@15036
   252
fun del_context_simprocs procs =
wenzelm@15036
   253
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   254
    (gen_rems eq_context_simproc (simprocs, procs), subgoal_tac, loop_tacs,
wenzelm@15036
   255
      unsafe_solvers, solvers));
wenzelm@15036
   256
wenzelm@15036
   257
fun set_context_subgoaler tac =
wenzelm@15036
   258
  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   259
    (simprocs, Some tac, loop_tacs, unsafe_solvers, solvers));
wenzelm@15036
   260
wenzelm@15036
   261
val reset_context_subgoaler =
wenzelm@15036
   262
  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   263
    (simprocs, None, loop_tacs, unsafe_solvers, solvers));
wenzelm@15036
   264
wenzelm@15036
   265
fun add_context_looper (name, tac) =
wenzelm@15036
   266
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   267
    (simprocs, subgoal_tac, merge_alists [(name, tac)] loop_tacs,
wenzelm@15036
   268
      unsafe_solvers, solvers));
wenzelm@15036
   269
wenzelm@15036
   270
fun del_context_looper name =
wenzelm@15036
   271
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   272
    (simprocs, subgoal_tac, filter_out (equal name o #1) loop_tacs,
wenzelm@15036
   273
      unsafe_solvers, solvers));
wenzelm@15036
   274
wenzelm@15036
   275
fun add_context_unsafe_solver solver =
wenzelm@15036
   276
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   277
    (simprocs, subgoal_tac, loop_tacs,
wenzelm@15036
   278
      merge_context_solvers [solver] unsafe_solvers, solvers));
wenzelm@15036
   279
wenzelm@15036
   280
fun add_context_safe_solver solver =
wenzelm@15036
   281
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   282
    (simprocs, subgoal_tac, loop_tacs, unsafe_solvers,
wenzelm@15036
   283
      merge_context_solvers [solver] solvers));
wenzelm@15036
   284
wenzelm@15036
   285
wenzelm@5842
   286
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   287
wenzelm@5842
   288
structure LocalSimpsetArgs =
wenzelm@5842
   289
struct
wenzelm@5842
   290
  val name = "Provers/simpset";
wenzelm@5842
   291
  type T = simpset;
wenzelm@5842
   292
  val init = simpset_of;
wenzelm@15036
   293
  fun print ctxt ss = print_ss (context_ss ctxt ss (get_context_ss ctxt));
wenzelm@5842
   294
end;
wenzelm@4795
   295
wenzelm@5842
   296
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   297
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   298
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   299
val put_local_simpset = LocalSimpset.put;
wenzelm@7273
   300
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@4795
   301
wenzelm@15036
   302
fun local_simpset_of ctxt =
wenzelm@15036
   303
  context_ss ctxt (get_local_simpset ctxt) (get_context_ss ctxt);
wenzelm@15036
   304
wenzelm@4795
   305
paulson@15452
   306
(* Jia: added DeltaSimpsetArgs and DeltaSimpset for delta_simpset
paulson@15452
   307
	also added methods to retrieve them. *)
paulson@15452
   308
paulson@15452
   309
structure DeltaSimpsetArgs =
paulson@15452
   310
struct
paulson@15452
   311
  val name = "delta_simpset";
paulson@15452
   312
  type T = Thm.thm list; (*the type is thm list*)
paulson@15452
   313
  val empty = [];
paulson@15452
   314
end;
paulson@15452
   315
paulson@15452
   316
structure DeltaSimpset = DeltaDataFun(DeltaSimpsetArgs);
paulson@15452
   317
val get_delta_simpset = DeltaSimpset.get;
paulson@15452
   318
val put_delta_simpset = DeltaSimpset.put;
paulson@15452
   319
paulson@15452
   320
val get_new_thm_id = ProofContext.get_delta_count_incr;
paulson@15452
   321
paulson@15452
   322
(* Jia: added to rename a local thm if necessary *) 
paulson@15452
   323
local 
paulson@15452
   324
fun rename_thm' (ctxt,thm) =
paulson@15452
   325
  let val new_id = get_new_thm_id ctxt
paulson@15452
   326
      val new_name = "anon_" ^ (string_of_int new_id)
paulson@15452
   327
  in
paulson@15452
   328
  Thm.name_thm(new_name,thm)
paulson@15452
   329
end;
paulson@15452
   330
paulson@15452
   331
in
paulson@15452
   332
paulson@15452
   333
fun rename_thm (ctxt,thm) = if (!Proof.call_atp) then rename_thm' (ctxt,thm) else thm;
paulson@15452
   334
paulson@15452
   335
end
paulson@15452
   336
wenzelm@5886
   337
(* attributes *)
wenzelm@5886
   338
wenzelm@5886
   339
fun change_global_ss f (thy, th) =
wenzelm@5886
   340
  let val r = simpset_ref_of thy
wenzelm@6096
   341
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   342
wenzelm@5886
   343
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   344
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   345
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   346
wenzelm@5886
   347
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   348
val simp_del_global = change_global_ss (op delsimps);
paulson@15452
   349
paulson@15452
   350
paulson@15452
   351
paulson@15452
   352
paulson@15452
   353
paulson@15452
   354
(* Jia: note: when simplifier rules are added to local_simpset, they are also added to delta_simpset in ProofContext.context, but not removed if simp_del_local is called *)
paulson@15452
   355
fun simp_add_local (ctxt,th) = 
paulson@15452
   356
    let val delta_ss = get_delta_simpset ctxt
paulson@15452
   357
	val thm_name = Thm.name_of_thm th
paulson@15452
   358
        val th' = if (thm_name = "") then rename_thm (ctxt,th)  else th
paulson@15452
   359
	val new_dss = th'::delta_ss
paulson@15452
   360
	val ctxt' = put_delta_simpset new_dss ctxt 
paulson@15452
   361
    in
paulson@15452
   362
	change_local_ss (op addsimps) (ctxt',th)
paulson@15452
   363
    end;
paulson@15452
   364
wenzelm@5886
   365
val simp_del_local = change_local_ss (op delsimps);
wenzelm@5886
   366
wenzelm@9715
   367
val cong_add_global = change_global_ss (op addcongs);
wenzelm@9715
   368
val cong_del_global = change_global_ss (op delcongs);
wenzelm@9715
   369
val cong_add_local = change_local_ss (op addcongs);
wenzelm@9715
   370
val cong_del_local = change_local_ss (op delcongs);
wenzelm@9715
   371
wenzelm@5886
   372
wenzelm@10004
   373
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@10004
   374
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@10004
   375
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@10004
   376
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@10004
   377
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@10004
   378
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
clasohm@0
   379
oheimb@2629
   380
skalberg@15006
   381
wenzelm@4795
   382
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   383
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   384
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   385
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   386
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   387
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   388
wenzelm@5082
   389
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   390
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   391
val     full_simplify = simp_thm (true, false, false);
schirmer@14814
   392
val   asm_lr_simplify = simp_thm (true, true, false);
schirmer@14814
   393
val asm_full_simplify = simp_thm (true, true, true);
wenzelm@5082
   394
wenzelm@5082
   395
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   396
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   397
val     full_rewrite = simp_cterm (true, false, false);
schirmer@14814
   398
val   asm_lr_rewrite = simp_cterm (true, true, false);
schirmer@14814
   399
val asm_full_rewrite = simp_cterm (true, true, true);
wenzelm@3557
   400
wenzelm@3557
   401
wenzelm@4795
   402
wenzelm@5886
   403
(** concrete syntax of attributes **)
wenzelm@5842
   404
wenzelm@5886
   405
(* add / del *)
wenzelm@5842
   406
wenzelm@5928
   407
val simpN = "simp";
wenzelm@9715
   408
val congN = "cong";
berghofe@10412
   409
val addN = "add";
berghofe@10412
   410
val delN = "del";
wenzelm@6911
   411
val onlyN = "only";
wenzelm@9899
   412
val no_asmN = "no_asm";
wenzelm@9899
   413
val no_asm_useN = "no_asm_use";
wenzelm@9899
   414
val no_asm_simpN = "no_asm_simp";
berghofe@13605
   415
val asm_lrN = "asm_lr";
wenzelm@5842
   416
wenzelm@8634
   417
val simp_attr =
wenzelm@8634
   418
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@8634
   419
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@5842
   420
wenzelm@9715
   421
val cong_attr =
wenzelm@9715
   422
 (Attrib.add_del_args cong_add_global cong_del_global,
wenzelm@9715
   423
  Attrib.add_del_args cong_add_local cong_del_local);
wenzelm@9715
   424
wenzelm@5842
   425
wenzelm@5842
   426
(* conversions *)
wenzelm@5842
   427
wenzelm@9899
   428
local
wenzelm@9899
   429
wenzelm@9899
   430
fun conv_mode x =
wenzelm@9899
   431
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@9899
   432
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@9899
   433
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@9899
   434
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@9899
   435
wenzelm@11938
   436
fun simplified_att get args =
wenzelm@11938
   437
  Attrib.syntax (conv_mode -- args >> (fn (f, ths) =>
wenzelm@11938
   438
    Drule.rule_attribute (fn x => f ((if null ths then I else clear_ss) (get x) addsimps ths))));
wenzelm@9899
   439
wenzelm@9899
   440
in
wenzelm@9899
   441
wenzelm@9899
   442
val simplified_attr =
wenzelm@11938
   443
 (simplified_att simpset_of Attrib.global_thmss,
wenzelm@15036
   444
  simplified_att local_simpset_of Attrib.local_thmss);
wenzelm@9899
   445
wenzelm@9899
   446
end;
wenzelm@5842
   447
wenzelm@5842
   448
wenzelm@5842
   449
(* setup_attrs *)
wenzelm@5842
   450
wenzelm@5842
   451
val setup_attrs = Attrib.add_attributes
wenzelm@9899
   452
 [(simpN, simp_attr, "declaration of simplification rule"),
wenzelm@9899
   453
  (congN, cong_attr, "declaration of Simplifier congruence rule"),
wenzelm@9899
   454
  ("simplified", simplified_attr, "simplified rule")];
wenzelm@5842
   455
wenzelm@5842
   456
wenzelm@5842
   457
wenzelm@5842
   458
(** proof methods **)
wenzelm@5842
   459
wenzelm@5842
   460
(* simplification *)
wenzelm@5842
   461
wenzelm@8700
   462
val simp_options =
wenzelm@9899
   463
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@9899
   464
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@9899
   465
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
berghofe@13605
   466
  Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
wenzelm@8700
   467
  Scan.succeed asm_full_simp_tac);
wenzelm@8700
   468
wenzelm@9715
   469
val cong_modifiers =
wenzelm@9720
   470
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local):Method.modifier),
wenzelm@10034
   471
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add_local),
wenzelm@10034
   472
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del_local)];
wenzelm@9715
   473
wenzelm@5928
   474
val simp_modifiers =
wenzelm@8815
   475
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   476
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   477
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   478
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   479
   @ cong_modifiers;
wenzelm@5928
   480
wenzelm@5928
   481
val simp_modifiers' =
wenzelm@10034
   482
 [Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   483
  Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   484
  Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   485
   @ cong_modifiers;
wenzelm@5928
   486
wenzelm@8700
   487
fun simp_args more_mods =
wenzelm@8700
   488
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options) (more_mods @ simp_modifiers');
wenzelm@8700
   489
wenzelm@8170
   490
wenzelm@8700
   491
fun simp_method (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@8700
   492
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@15036
   493
    (CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
wenzelm@8700
   494
wenzelm@8700
   495
fun simp_method' (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@10821
   496
  HEADGOAL (Method.insert_tac (prems @ facts) THEN'
wenzelm@15036
   497
      (CHANGED_PROP oo tac) (local_simpset_of ctxt)));
wenzelm@9715
   498
wenzelm@5842
   499
wenzelm@5842
   500
(* setup_methods *)
wenzelm@5842
   501
wenzelm@8467
   502
fun setup_methods more_mods = Method.add_methods
wenzelm@8700
   503
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@8700
   504
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@5842
   505
wenzelm@5842
   506
wenzelm@5842
   507
wenzelm@4795
   508
(** theory setup **)
wenzelm@4795
   509
wenzelm@8467
   510
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs];
wenzelm@8467
   511
fun method_setup mods = [setup_methods mods];
wenzelm@4795
   512
wenzelm@8228
   513
fun easy_setup reflect trivs =
wenzelm@8228
   514
  let
wenzelm@8228
   515
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@8228
   516
wenzelm@8228
   517
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@8228
   518
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@8228
   519
wenzelm@8228
   520
    (*no premature instantiation of variables during simplification*)
wenzelm@8228
   521
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@8228
   522
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@8228
   523
wenzelm@8243
   524
    fun mk_eq thm =
wenzelm@8228
   525
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@8228
   526
      else [thm RS reflect] handle THM _ => [];
wenzelm@8228
   527
wenzelm@8243
   528
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@8243
   529
wenzelm@8228
   530
    fun init_ss thy =
wenzelm@8228
   531
      (simpset_ref_of thy :=
wenzelm@8228
   532
        empty_ss setsubgoaler asm_simp_tac
wenzelm@8228
   533
        setSSolver safe_solver
wenzelm@8228
   534
        setSolver unsafe_solver
wenzelm@8228
   535
        setmksimps mksimps; thy);
wenzelm@8467
   536
  in setup @ method_setup [] @ [init_ss] end;
wenzelm@8228
   537
wenzelm@4795
   538
wenzelm@8667
   539
wenzelm@8667
   540
(** outer syntax **)
wenzelm@8667
   541
wenzelm@8667
   542
val print_simpsetP =
wenzelm@8667
   543
  OuterSyntax.improper_command "print_simpset" "print context of Simplifier"
wenzelm@8667
   544
    OuterSyntax.Keyword.diag
wenzelm@9513
   545
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o (Toplevel.keep
wenzelm@9010
   546
      (Toplevel.node_case print_simpset (print_local_simpset o Proof.context_of)))));
wenzelm@8667
   547
wenzelm@8667
   548
val _ = OuterSyntax.add_parsers [print_simpsetP];
wenzelm@8667
   549
clasohm@1243
   550
end;
wenzelm@4795
   551
wenzelm@4795
   552
wenzelm@4795
   553
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   554
open BasicSimplifier;