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