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