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