src/Pure/simplifier.ML
author wenzelm
Mon Jul 04 20:20:50 2005 +0200 (2005-07-04)
changeset 16685 4ffc943c9c75
parent 16684 7b58002668c0
child 16709 a4679ac06502
permissions -rw-r--r--
made smlnj happy;
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@16458
    25
  val simpset_ref_of_sg: theory -> simpset ref    (*obsolete*)
wenzelm@16014
    26
  val simpset_ref_of: theory -> simpset ref
wenzelm@16458
    27
  val simpset_of_sg: theory -> simpset            (*obsolete*)
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@16458
    61
  val simproc_i: theory -> string -> term list
wenzelm@16458
    62
    -> (theory -> simpset -> term -> thm option) -> simproc
wenzelm@16458
    63
  val simproc: theory -> string -> string list
wenzelm@16458
    64
    -> (theory -> simpset -> term -> thm option) -> simproc
wenzelm@16458
    65
  val context_simproc_i: theory -> string -> term list
wenzelm@16014
    66
    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@16458
    67
  val context_simproc: theory -> 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@16458
   133
fun context_simproc_i thy name =
wenzelm@16458
   134
  mk_context_simproc name o map (Thm.cterm_of thy o Logic.varify);
wenzelm@16014
   135
wenzelm@16458
   136
fun context_simproc thy name =
wenzelm@16458
   137
  context_simproc_i thy name o map (Sign.simple_read_term thy 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@16458
   191
structure GlobalSimpset = TheoryDataFun
wenzelm@16458
   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@16458
   198
  val extend = copy;
wenzelm@16458
   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@16458
   202
end);
wenzelm@16014
   203
wenzelm@16014
   204
val _ = Context.add_setup [GlobalSimpset.init];
wenzelm@16014
   205
val print_simpset = GlobalSimpset.print;
wenzelm@16014
   206
wenzelm@16014
   207
val simpset_ref_of = #1 o GlobalSimpset.get;
wenzelm@16458
   208
val simpset_ref_of_sg = simpset_ref_of;
wenzelm@16014
   209
val get_context_ss = #2 o GlobalSimpset.get o ProofContext.theory_of;
wenzelm@16014
   210
wenzelm@16014
   211
fun map_context_ss f = GlobalSimpset.map (apsnd
wenzelm@16014
   212
  (fn ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} =>
wenzelm@16014
   213
    make_context_ss (f (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers))));
wenzelm@16014
   214
wenzelm@16014
   215
wenzelm@16014
   216
(* access global simpset *)
wenzelm@16014
   217
wenzelm@16458
   218
val simpset_of = ! o simpset_ref_of;
wenzelm@16458
   219
val simpset_of_sg = simpset_of;
wenzelm@16014
   220
wenzelm@16458
   221
fun SIMPSET tacf state = tacf (simpset_of (Thm.theory_of_thm state)) state;
wenzelm@16458
   222
fun SIMPSET' tacf i state = tacf (simpset_of (Thm.theory_of_thm state)) i state;
wenzelm@16014
   223
wenzelm@16014
   224
val simpset = simpset_of o Context.the_context;
wenzelm@16458
   225
val simpset_ref = simpset_ref_of o Context.the_context;
wenzelm@16014
   226
wenzelm@16014
   227
wenzelm@16014
   228
(* change global simpset *)
wenzelm@16014
   229
wenzelm@16014
   230
fun change_simpset_of f x thy =
wenzelm@16014
   231
  let val r = simpset_ref_of thy
wenzelm@16014
   232
  in r := f (! r, x); thy end;
wenzelm@16014
   233
wenzelm@16014
   234
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@16014
   235
wenzelm@16014
   236
val Addsimps = change_simpset (op addsimps);
wenzelm@16014
   237
val Delsimps = change_simpset (op delsimps);
wenzelm@16014
   238
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@16014
   239
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@16014
   240
val Addcongs = change_simpset (op addcongs);
wenzelm@16014
   241
val Delcongs = change_simpset (op delcongs);
wenzelm@16014
   242
wenzelm@16014
   243
wenzelm@16014
   244
(* change context dependent components *)
wenzelm@16014
   245
wenzelm@16014
   246
fun add_context_simprocs procs =
wenzelm@16014
   247
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   248
    (merge_context_simprocs procs simprocs, subgoal_tac, loop_tacs,
wenzelm@16014
   249
      unsafe_solvers, solvers));
wenzelm@16014
   250
wenzelm@16014
   251
fun del_context_simprocs procs =
wenzelm@16014
   252
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   253
    (gen_rems eq_context_simproc (simprocs, procs), subgoal_tac, loop_tacs,
wenzelm@16014
   254
      unsafe_solvers, solvers));
wenzelm@16014
   255
wenzelm@16014
   256
fun set_context_subgoaler tac =
wenzelm@16014
   257
  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   258
    (simprocs, SOME tac, loop_tacs, unsafe_solvers, solvers));
wenzelm@16014
   259
wenzelm@16014
   260
val reset_context_subgoaler =
wenzelm@16014
   261
  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   262
    (simprocs, NONE, loop_tacs, unsafe_solvers, solvers));
wenzelm@16014
   263
wenzelm@16014
   264
fun add_context_looper (name, tac) =
wenzelm@16014
   265
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   266
    (simprocs, subgoal_tac, merge_alists [(name, tac)] loop_tacs,
wenzelm@16014
   267
      unsafe_solvers, solvers));
wenzelm@16014
   268
wenzelm@16014
   269
fun del_context_looper name =
wenzelm@16014
   270
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   271
    (simprocs, subgoal_tac, filter_out (equal name o #1) loop_tacs,
wenzelm@16014
   272
      unsafe_solvers, solvers));
wenzelm@16014
   273
wenzelm@16014
   274
fun add_context_unsafe_solver solver =
wenzelm@16014
   275
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   276
    (simprocs, subgoal_tac, loop_tacs,
wenzelm@16014
   277
      merge_context_solvers [solver] unsafe_solvers, solvers));
wenzelm@16014
   278
wenzelm@16014
   279
fun add_context_safe_solver solver =
wenzelm@16014
   280
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@16014
   281
    (simprocs, subgoal_tac, loop_tacs, unsafe_solvers,
wenzelm@16014
   282
      merge_context_solvers [solver] solvers));
wenzelm@16014
   283
wenzelm@16014
   284
wenzelm@16014
   285
(* proof data kind 'Pure/simpset' *)
wenzelm@16014
   286
wenzelm@16458
   287
structure LocalSimpset = ProofDataFun
wenzelm@16458
   288
(struct
wenzelm@16014
   289
  val name = "Pure/simpset";
wenzelm@16014
   290
  type T = simpset;
wenzelm@16014
   291
  val init = simpset_of;
wenzelm@16014
   292
  fun print ctxt ss = print_ss (context_ss ctxt ss (get_context_ss ctxt));
wenzelm@16458
   293
end);
wenzelm@16014
   294
wenzelm@16014
   295
val _ = Context.add_setup [LocalSimpset.init];
wenzelm@16014
   296
val print_local_simpset = LocalSimpset.print;
wenzelm@16014
   297
wenzelm@16014
   298
val get_local_simpset = LocalSimpset.get;
wenzelm@16014
   299
val put_local_simpset = LocalSimpset.put;
wenzelm@16014
   300
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@16014
   301
wenzelm@16014
   302
fun local_simpset_of ctxt =
wenzelm@16014
   303
  context_ss ctxt (get_local_simpset ctxt) (get_context_ss ctxt);
wenzelm@16014
   304
wenzelm@16014
   305
wenzelm@16014
   306
(* Jia: added DeltaSimpsetArgs and DeltaSimpset for delta_simpset
wenzelm@16014
   307
	also added methods to retrieve them. *)
wenzelm@16014
   308
(* CB: changed "delta simpsets" to context data,
wenzelm@16014
   309
       moved counter to here, it is no longer a ref *)
wenzelm@16014
   310
wenzelm@16014
   311
structure DeltaSimpsetArgs =
wenzelm@16014
   312
struct
wenzelm@16014
   313
  val name = "Pure/delta_simpset";
wenzelm@16014
   314
  type T = Thm.thm list * int;  (*the type is thm list * int*)
wenzelm@16014
   315
  fun init _ = ([], 0);
wenzelm@16014
   316
  fun print ctxt thms = ();
wenzelm@16014
   317
end;
wenzelm@16014
   318
wenzelm@16014
   319
structure DeltaSimpsetData = ProofDataFun(DeltaSimpsetArgs);
wenzelm@16014
   320
val _ = Context.add_setup [DeltaSimpsetData.init];
wenzelm@16014
   321
wenzelm@16014
   322
val get_delta_simpset = #1 o DeltaSimpsetData.get;
wenzelm@16014
   323
fun put_delta_simpset ss = DeltaSimpsetData.map (fn (_, i) => (ss, i));
wenzelm@16014
   324
fun delta_increment ctxt =
wenzelm@16014
   325
  let val ctxt' = DeltaSimpsetData.map (fn (ss, i) => (ss, i+1)) ctxt
wenzelm@16014
   326
  in (ctxt', #2 (DeltaSimpsetData.get ctxt'))
wenzelm@16014
   327
  end;
wenzelm@16014
   328
wenzelm@16014
   329
(* Jia: added to rename a local thm if necessary *) 
wenzelm@16014
   330
local 
wenzelm@16014
   331
fun rename_thm' (ctxt,thm) =
wenzelm@16014
   332
  let val (new_ctxt, new_id) = delta_increment ctxt
wenzelm@16014
   333
      val new_name = "anon_simp_" ^ (string_of_int new_id)
wenzelm@16014
   334
  in
wenzelm@16014
   335
    (new_ctxt, Thm.name_thm(new_name,thm))
wenzelm@16014
   336
end;
wenzelm@16014
   337
wenzelm@16014
   338
in
wenzelm@16014
   339
wenzelm@16014
   340
fun rename_thm (ctxt,thm) = if (!Proof.call_atp) then rename_thm' (ctxt,thm) else (ctxt, thm);
wenzelm@16014
   341
wenzelm@16014
   342
end
wenzelm@16014
   343
wenzelm@16014
   344
(* attributes *)
wenzelm@16014
   345
wenzelm@16014
   346
fun change_global_ss f (thy, th) =
wenzelm@16014
   347
  let val r = simpset_ref_of thy
wenzelm@16014
   348
  in r := f (! r, [th]); (thy, th) end;
wenzelm@16014
   349
wenzelm@16014
   350
fun change_local_ss f (ctxt, th) =
wenzelm@16014
   351
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@16014
   352
  in (put_local_simpset ss ctxt, th) end;
wenzelm@16014
   353
wenzelm@16014
   354
val simp_add_global = change_global_ss (op addsimps);
wenzelm@16014
   355
val simp_del_global = change_global_ss (op delsimps);
wenzelm@16014
   356
wenzelm@16014
   357
wenzelm@16014
   358
wenzelm@16014
   359
wenzelm@16014
   360
wenzelm@16014
   361
(* 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
   362
fun simp_add_local (ctxt,th) = 
wenzelm@16014
   363
    let val delta_ss = get_delta_simpset ctxt
wenzelm@16014
   364
	val thm_name = Thm.name_of_thm th
wenzelm@16014
   365
        val (ctxt', th') =
wenzelm@16014
   366
          if (thm_name = "") then rename_thm (ctxt,th)  else (ctxt, th)
wenzelm@16014
   367
	val new_dss = th'::delta_ss
wenzelm@16014
   368
	val ctxt'' = put_delta_simpset new_dss ctxt' 
wenzelm@16014
   369
    in
wenzelm@16014
   370
	change_local_ss (op addsimps) (ctxt'',th)
wenzelm@16014
   371
    end;
wenzelm@16014
   372
wenzelm@16014
   373
val simp_del_local = change_local_ss (op delsimps);
wenzelm@16014
   374
wenzelm@16014
   375
val cong_add_global = change_global_ss (op addcongs);
wenzelm@16014
   376
val cong_del_global = change_global_ss (op delcongs);
wenzelm@16014
   377
val cong_add_local = change_local_ss (op addcongs);
wenzelm@16014
   378
val cong_del_local = change_local_ss (op delcongs);
wenzelm@16014
   379
wenzelm@16014
   380
wenzelm@16014
   381
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@16014
   382
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@16014
   383
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@16014
   384
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@16014
   385
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@16014
   386
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
wenzelm@16014
   387
wenzelm@16014
   388
wenzelm@16014
   389
wenzelm@16014
   390
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@16014
   391
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@16014
   392
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@16014
   393
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
wenzelm@16014
   394
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@16014
   395
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
wenzelm@16014
   396
wenzelm@16014
   397
val          simplify = simp_thm (false, false, false);
wenzelm@16014
   398
val      asm_simplify = simp_thm (false, true, false);
wenzelm@16014
   399
val     full_simplify = simp_thm (true, false, false);
wenzelm@16014
   400
val   asm_lr_simplify = simp_thm (true, true, false);
wenzelm@16014
   401
val asm_full_simplify = simp_thm (true, true, true);
wenzelm@16014
   402
wenzelm@16014
   403
val          rewrite = simp_cterm (false, false, false);
wenzelm@16014
   404
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@16014
   405
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@16014
   406
val   asm_lr_rewrite = simp_cterm (true, true, false);
wenzelm@16014
   407
val asm_full_rewrite = simp_cterm (true, true, true);
wenzelm@16014
   408
wenzelm@16014
   409
wenzelm@16014
   410
wenzelm@16014
   411
(** concrete syntax of attributes **)
wenzelm@16014
   412
wenzelm@16014
   413
(* add / del *)
wenzelm@16014
   414
wenzelm@16014
   415
val simpN = "simp";
wenzelm@16014
   416
val congN = "cong";
wenzelm@16014
   417
val addN = "add";
wenzelm@16014
   418
val delN = "del";
wenzelm@16014
   419
val onlyN = "only";
wenzelm@16014
   420
val no_asmN = "no_asm";
wenzelm@16014
   421
val no_asm_useN = "no_asm_use";
wenzelm@16014
   422
val no_asm_simpN = "no_asm_simp";
wenzelm@16014
   423
val asm_lrN = "asm_lr";
wenzelm@16014
   424
wenzelm@16014
   425
val simp_attr =
wenzelm@16014
   426
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@16014
   427
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@16014
   428
wenzelm@16014
   429
val cong_attr =
wenzelm@16014
   430
 (Attrib.add_del_args cong_add_global cong_del_global,
wenzelm@16014
   431
  Attrib.add_del_args cong_add_local cong_del_local);
wenzelm@16014
   432
wenzelm@16014
   433
wenzelm@16014
   434
(* conversions *)
wenzelm@16014
   435
wenzelm@16014
   436
local
wenzelm@16014
   437
wenzelm@16014
   438
fun conv_mode x =
wenzelm@16014
   439
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@16014
   440
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@16014
   441
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@16014
   442
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@16014
   443
wenzelm@16014
   444
fun simplified_att get args =
wenzelm@16014
   445
  Attrib.syntax (conv_mode -- args >> (fn (f, ths) =>
wenzelm@16014
   446
    Drule.rule_attribute (fn x => f ((if null ths then I else clear_ss) (get x) addsimps ths))));
wenzelm@16014
   447
wenzelm@16014
   448
in
wenzelm@16014
   449
wenzelm@16014
   450
val simplified_attr =
wenzelm@16014
   451
 (simplified_att simpset_of Attrib.global_thmss,
wenzelm@16014
   452
  simplified_att local_simpset_of Attrib.local_thmss);
wenzelm@16014
   453
wenzelm@16014
   454
end;
wenzelm@16014
   455
wenzelm@16014
   456
wenzelm@16014
   457
(* setup attributes *)
wenzelm@16014
   458
wenzelm@16014
   459
val _ = Context.add_setup
wenzelm@16014
   460
 [Attrib.add_attributes
wenzelm@16014
   461
   [(simpN, simp_attr, "declaration of simplification rule"),
wenzelm@16014
   462
    (congN, cong_attr, "declaration of Simplifier congruence rule"),
wenzelm@16014
   463
    ("simplified", simplified_attr, "simplified rule")]];
wenzelm@16014
   464
wenzelm@16014
   465
wenzelm@16014
   466
wenzelm@16014
   467
(** proof methods **)
wenzelm@16014
   468
wenzelm@16014
   469
(* simplification *)
wenzelm@16014
   470
wenzelm@16014
   471
val simp_options =
wenzelm@16014
   472
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@16014
   473
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@16014
   474
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
wenzelm@16014
   475
  Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
wenzelm@16014
   476
  Scan.succeed asm_full_simp_tac);
wenzelm@16014
   477
wenzelm@16685
   478
fun simp_flags x = (Scan.repeat
wenzelm@16684
   479
  (Args.parens (Args.$$$ "depth_limit" -- Args.colon |-- Args.nat)
wenzelm@16684
   480
    >> setmp MetaSimplifier.simp_depth_limit)
wenzelm@16685
   481
  >> curry (Library.foldl op o) I) x;
wenzelm@16684
   482
wenzelm@16014
   483
val cong_modifiers =
wenzelm@16014
   484
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local):Method.modifier),
wenzelm@16014
   485
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add_local),
wenzelm@16014
   486
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del_local)];
wenzelm@16014
   487
wenzelm@16014
   488
val simp_modifiers =
wenzelm@16014
   489
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
wenzelm@16014
   490
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@16014
   491
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@16014
   492
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@16014
   493
   @ cong_modifiers;
wenzelm@16014
   494
wenzelm@16014
   495
val simp_modifiers' =
wenzelm@16014
   496
 [Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@16014
   497
  Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@16014
   498
  Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@16014
   499
   @ cong_modifiers;
wenzelm@16014
   500
wenzelm@16014
   501
fun simp_args more_mods =
wenzelm@16684
   502
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options -- Scan.lift simp_flags)
wenzelm@16684
   503
    (more_mods @ simp_modifiers');
wenzelm@16014
   504
wenzelm@16684
   505
fun simp_method ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
wenzelm@16014
   506
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@16684
   507
    (FLAGS o CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
wenzelm@16014
   508
wenzelm@16684
   509
fun simp_method' ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
wenzelm@16014
   510
  HEADGOAL (Method.insert_tac (prems @ facts) THEN'
wenzelm@16684
   511
      ((FLAGS o CHANGED_PROP) oo tac) (local_simpset_of ctxt)));
wenzelm@16014
   512
wenzelm@16014
   513
wenzelm@16014
   514
(* setup methods *)
wenzelm@16014
   515
wenzelm@16014
   516
fun setup_methods more_mods = Method.add_methods
wenzelm@16014
   517
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@16014
   518
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@16014
   519
wenzelm@16014
   520
fun method_setup mods = [setup_methods mods];
wenzelm@16014
   521
wenzelm@16014
   522
wenzelm@16014
   523
(** easy_setup **)
wenzelm@16014
   524
wenzelm@16014
   525
fun easy_setup reflect trivs =
wenzelm@16014
   526
  let
wenzelm@16014
   527
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@16014
   528
wenzelm@16014
   529
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@16014
   530
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@16014
   531
wenzelm@16014
   532
    (*no premature instantiation of variables during simplification*)
wenzelm@16014
   533
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@16014
   534
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@16014
   535
wenzelm@16014
   536
    fun mk_eq thm =
wenzelm@16014
   537
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@16014
   538
      else [thm RS reflect] handle THM _ => [];
wenzelm@16014
   539
wenzelm@16014
   540
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@16014
   541
wenzelm@16014
   542
    fun init_ss thy =
wenzelm@16014
   543
      (simpset_ref_of thy :=
wenzelm@16014
   544
        empty_ss setsubgoaler asm_simp_tac
wenzelm@16014
   545
        setSSolver safe_solver
wenzelm@16014
   546
        setSolver unsafe_solver
wenzelm@16014
   547
        setmksimps mksimps; thy);
wenzelm@16014
   548
  in method_setup [] @ [init_ss] end;
wenzelm@16014
   549
wenzelm@16014
   550
end;
wenzelm@16014
   551
wenzelm@16014
   552
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@16014
   553
open BasicSimplifier;