src/Pure/simplifier.ML
author haftmann
Mon Apr 16 16:11:03 2007 +0200 (2007-04-16)
changeset 22717 74dbc7696083
parent 22709 9ab51bac6287
child 22770 615e19792c92
permissions -rw-r--r--
canonical merge 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
  val print_simpset: theory -> unit
wenzelm@17883
    13
  val change_simpset_of: theory -> (simpset -> simpset) -> unit
wenzelm@17883
    14
  val change_simpset: (simpset -> simpset) -> unit
wenzelm@16014
    15
  val simpset_of: theory -> simpset
wenzelm@17883
    16
  val simpset: unit -> simpset
wenzelm@16014
    17
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@16014
    18
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@16014
    19
  val Addsimps: thm list -> unit
wenzelm@16014
    20
  val Delsimps: thm list -> unit
wenzelm@16014
    21
  val Addsimprocs: simproc list -> unit
wenzelm@16014
    22
  val Delsimprocs: simproc list -> unit
wenzelm@16014
    23
  val Addcongs: thm list -> unit
wenzelm@16014
    24
  val Delcongs: thm list -> unit
wenzelm@16014
    25
  val local_simpset_of: Proof.context -> simpset
wenzelm@17967
    26
  val generic_simp_tac: bool -> bool * bool * bool -> simpset -> int -> tactic
wenzelm@16014
    27
  val safe_asm_full_simp_tac: simpset -> int -> tactic
wenzelm@16014
    28
  val               simp_tac: simpset -> int -> tactic
wenzelm@16014
    29
  val           asm_simp_tac: simpset -> int -> tactic
wenzelm@16014
    30
  val          full_simp_tac: simpset -> int -> tactic
wenzelm@16014
    31
  val        asm_lr_simp_tac: simpset -> int -> tactic
wenzelm@16014
    32
  val      asm_full_simp_tac: simpset -> int -> tactic
wenzelm@16014
    33
  val               Simp_tac:            int -> tactic
wenzelm@16014
    34
  val           Asm_simp_tac:            int -> tactic
wenzelm@16014
    35
  val          Full_simp_tac:            int -> tactic
wenzelm@16014
    36
  val        Asm_lr_simp_tac:            int -> tactic
wenzelm@16014
    37
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@16014
    38
  val          simplify: simpset -> thm -> thm
wenzelm@16014
    39
  val      asm_simplify: simpset -> thm -> thm
wenzelm@16014
    40
  val     full_simplify: simpset -> thm -> thm
wenzelm@16014
    41
  val   asm_lr_simplify: simpset -> thm -> thm
wenzelm@16014
    42
  val asm_full_simplify: simpset -> thm -> thm
wenzelm@16014
    43
end;
wenzelm@16014
    44
wenzelm@16014
    45
signature SIMPLIFIER =
wenzelm@16014
    46
sig
wenzelm@16014
    47
  include BASIC_SIMPLIFIER
wenzelm@17004
    48
  val clear_ss: simpset -> simpset
wenzelm@17723
    49
  val debug_bounds: bool ref
wenzelm@17883
    50
  val inherit_context: simpset -> simpset -> simpset
wenzelm@17898
    51
  val the_context: simpset -> Proof.context
wenzelm@17898
    52
  val context: Proof.context -> simpset -> simpset
wenzelm@17898
    53
  val theory_context: theory  -> simpset -> simpset
wenzelm@16458
    54
  val simproc_i: theory -> string -> term list
wenzelm@16458
    55
    -> (theory -> simpset -> term -> thm option) -> simproc
wenzelm@16458
    56
  val simproc: theory -> string -> string list
wenzelm@16458
    57
    -> (theory -> simpset -> term -> thm option) -> simproc
wenzelm@16014
    58
  val          rewrite: simpset -> cterm -> thm
wenzelm@16014
    59
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@16014
    60
  val     full_rewrite: simpset -> cterm -> thm
wenzelm@16014
    61
  val   asm_lr_rewrite: simpset -> cterm -> thm
wenzelm@16014
    62
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@17883
    63
  val get_simpset: theory -> simpset
wenzelm@16014
    64
  val print_local_simpset: Proof.context -> unit
wenzelm@16014
    65
  val get_local_simpset: Proof.context -> simpset
wenzelm@16014
    66
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@22379
    67
  val get_ss: Context.generic -> simpset
wenzelm@22379
    68
  val map_ss: (simpset -> simpset) -> Context.generic -> Context.generic
wenzelm@18728
    69
  val attrib: (simpset * thm list -> simpset) -> attribute
wenzelm@18728
    70
  val simp_add: attribute
wenzelm@18728
    71
  val simp_del: attribute
wenzelm@18728
    72
  val cong_add: attribute
wenzelm@18728
    73
  val cong_del: attribute
wenzelm@22204
    74
  val get_simproc: Proof.context -> xstring -> simproc
wenzelm@22236
    75
  val def_simproc: {name: string, lhss: string list,
wenzelm@22236
    76
    proc: morphism -> simpset -> cterm -> thm option, identifier: thm list} ->
wenzelm@22201
    77
    local_theory -> local_theory
wenzelm@22236
    78
  val def_simproc_i: {name: string, lhss: term list,
wenzelm@22236
    79
    proc: morphism -> simpset -> cterm -> thm option, identifier: thm list} ->
wenzelm@22201
    80
    local_theory -> local_theory
wenzelm@18033
    81
  val cong_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@18033
    82
  val simp_modifiers': (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@16014
    83
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@16014
    84
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@18708
    85
    -> theory -> theory
wenzelm@18708
    86
  val easy_setup: thm -> thm list -> theory -> theory
wenzelm@16014
    87
end;
wenzelm@16014
    88
wenzelm@16014
    89
structure Simplifier: SIMPLIFIER =
wenzelm@16014
    90
struct
wenzelm@16014
    91
wenzelm@21708
    92
open MetaSimplifier;
wenzelm@21708
    93
wenzelm@21708
    94
wenzelm@17883
    95
(** simpset data **)
wenzelm@16014
    96
wenzelm@17883
    97
(* global simpsets *)
wenzelm@16014
    98
wenzelm@16458
    99
structure GlobalSimpset = TheoryDataFun
wenzelm@16458
   100
(struct
wenzelm@16014
   101
  val name = "Pure/simpset";
wenzelm@17883
   102
  type T = simpset ref;
wenzelm@16014
   103
wenzelm@17883
   104
  val empty = ref empty_ss;
wenzelm@17883
   105
  fun copy (ref ss) = ref ss: T;            (*create new reference!*)
wenzelm@17898
   106
  fun extend (ref ss) = ref (MetaSimplifier.inherit_context empty_ss ss);
wenzelm@17883
   107
  fun merge _ (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@17883
   108
  fun print _ (ref ss) = print_ss ss;
wenzelm@16458
   109
end);
wenzelm@16014
   110
wenzelm@18708
   111
val _ = Context.add_setup GlobalSimpset.init;
wenzelm@16014
   112
val print_simpset = GlobalSimpset.print;
wenzelm@17883
   113
val get_simpset = ! o GlobalSimpset.get;
wenzelm@16014
   114
wenzelm@17883
   115
val change_simpset_of = change o GlobalSimpset.get;
wenzelm@22095
   116
fun change_simpset f = change_simpset_of (ML_Context.the_context ()) f;
wenzelm@16014
   117
wenzelm@17898
   118
fun simpset_of thy = MetaSimplifier.context (ProofContext.init thy) (get_simpset thy);
wenzelm@22095
   119
val simpset = simpset_of o ML_Context.the_context;
wenzelm@16014
   120
wenzelm@16014
   121
wenzelm@17883
   122
fun SIMPSET tacf st = tacf (simpset_of (Thm.theory_of_thm st)) st;
wenzelm@17883
   123
fun SIMPSET' tacf i st = tacf (simpset_of (Thm.theory_of_thm st)) i st;
wenzelm@16014
   124
wenzelm@17883
   125
fun Addsimps args = change_simpset (fn ss => ss addsimps args);
wenzelm@17883
   126
fun Delsimps args = change_simpset (fn ss => ss delsimps args);
wenzelm@17883
   127
fun Addsimprocs args = change_simpset (fn ss => ss addsimprocs args);
wenzelm@17883
   128
fun Delsimprocs args = change_simpset (fn ss => ss delsimprocs args);
wenzelm@17883
   129
fun Addcongs args = change_simpset (fn ss => ss addcongs args);
wenzelm@17883
   130
fun Delcongs args = change_simpset (fn ss => ss delcongs args);
wenzelm@16014
   131
wenzelm@16014
   132
wenzelm@17883
   133
(* local simpsets *)
wenzelm@16014
   134
wenzelm@16458
   135
structure LocalSimpset = ProofDataFun
wenzelm@16458
   136
(struct
wenzelm@16014
   137
  val name = "Pure/simpset";
wenzelm@16014
   138
  type T = simpset;
wenzelm@17883
   139
  val init = get_simpset;
wenzelm@17883
   140
  fun print _ ss = print_ss ss;
wenzelm@16458
   141
end);
wenzelm@16014
   142
wenzelm@18708
   143
val _ = Context.add_setup LocalSimpset.init;
wenzelm@16014
   144
val print_local_simpset = LocalSimpset.print;
wenzelm@16014
   145
val get_local_simpset = LocalSimpset.get;
wenzelm@16014
   146
val put_local_simpset = LocalSimpset.put;
wenzelm@16014
   147
wenzelm@17898
   148
fun local_simpset_of ctxt = MetaSimplifier.context ctxt (get_local_simpset ctxt);
wenzelm@16014
   149
wenzelm@22132
   150
val _ = ML_Context.value_antiq "simpset"
wenzelm@22132
   151
  (Scan.succeed ("simpset", "Simplifier.local_simpset_of (ML_Context.the_local_context ())"));
wenzelm@22132
   152
wenzelm@16014
   153
wenzelm@22201
   154
(* generic simpsets *)
wenzelm@22201
   155
wenzelm@22201
   156
fun get_ss (Context.Theory thy) = simpset_of thy
wenzelm@22201
   157
  | get_ss (Context.Proof ctxt) = local_simpset_of ctxt;
wenzelm@22201
   158
wenzelm@22201
   159
fun map_ss f (Context.Theory thy) = (change_simpset_of thy f; Context.Theory thy)
wenzelm@22201
   160
  | map_ss f (Context.Proof ctxt) = Context.Proof (LocalSimpset.map f ctxt);
wenzelm@22201
   161
wenzelm@22201
   162
wenzelm@16014
   163
(* attributes *)
wenzelm@16014
   164
wenzelm@22201
   165
fun attrib f = Thm.declaration_attribute (fn th => map_ss (fn ss => f (ss, [th])));
wenzelm@16014
   166
wenzelm@18688
   167
val simp_add = attrib (op addsimps);
wenzelm@18688
   168
val simp_del = attrib (op delsimps);
wenzelm@18688
   169
val cong_add = attrib (op addcongs);
wenzelm@18688
   170
val cong_del = attrib (op delcongs);
wenzelm@16014
   171
wenzelm@16014
   172
wenzelm@17967
   173
wenzelm@22201
   174
(** named simprocs **)
wenzelm@22201
   175
wenzelm@22201
   176
fun err_dup_simprocs ds =
wenzelm@22201
   177
  error ("Duplicate simproc(s): " ^ commas_quote ds);
wenzelm@22201
   178
wenzelm@22236
   179
wenzelm@22204
   180
(* data *)
wenzelm@22204
   181
wenzelm@22201
   182
structure Simprocs = GenericDataFun
wenzelm@22201
   183
(
wenzelm@22201
   184
  val name = "Pure/simprocs";
wenzelm@22236
   185
  type T = simproc NameSpace.table;
wenzelm@22201
   186
  val empty = NameSpace.empty_table;
wenzelm@22201
   187
  val extend = I;
wenzelm@22236
   188
  fun merge _ simprocs = NameSpace.merge_tables eq_simproc simprocs
wenzelm@22201
   189
    handle Symtab.DUPS ds => err_dup_simprocs ds;
wenzelm@22201
   190
  fun print _ _ = ();
wenzelm@22201
   191
);
wenzelm@22204
   192
val _ = Context.add_setup Simprocs.init;
wenzelm@22201
   193
wenzelm@22204
   194
wenzelm@22204
   195
(* get simprocs *)
wenzelm@22204
   196
wenzelm@22204
   197
fun get_simproc ctxt xname =
wenzelm@22204
   198
  let
wenzelm@22204
   199
    val (space, tab) = Simprocs.get (Context.Proof ctxt);
wenzelm@22204
   200
    val name = NameSpace.intern space xname;
wenzelm@22204
   201
  in
wenzelm@22204
   202
    (case Symtab.lookup tab name of
wenzelm@22236
   203
      SOME proc => proc
wenzelm@22204
   204
    | NONE => error ("Undefined simplification procedure: " ^ quote name))
wenzelm@22204
   205
  end;
wenzelm@22204
   206
wenzelm@22204
   207
val _ = ML_Context.value_antiq "simproc" (Scan.lift Args.name >> (fn name =>
wenzelm@22204
   208
  ("simproc",
wenzelm@22204
   209
    "Simplifier.get_simproc (ML_Context.the_local_context ()) " ^ ML_Syntax.print_string name)));
wenzelm@22204
   210
wenzelm@22204
   211
wenzelm@22204
   212
(* define simprocs *)
wenzelm@22201
   213
wenzelm@22201
   214
local
wenzelm@22201
   215
wenzelm@22201
   216
(* FIXME *)
wenzelm@22201
   217
fun read_terms ctxt ts =
wenzelm@22201
   218
  #1 (ProofContext.read_termTs ctxt (K false) (K NONE) (K NONE) []
wenzelm@22709
   219
    (map (rpair dummyT) ts));
wenzelm@22201
   220
wenzelm@22201
   221
(* FIXME *)
wenzelm@22201
   222
fun cert_terms ctxt ts = map (ProofContext.cert_term ctxt) ts;
wenzelm@22201
   223
wenzelm@22236
   224
fun gen_simproc prep {name, lhss, proc, identifier} lthy =
wenzelm@22201
   225
  let
wenzelm@22236
   226
    val naming = LocalTheory.full_naming lthy;
wenzelm@22236
   227
    val simproc = make_simproc
wenzelm@22236
   228
      {name = LocalTheory.full_name lthy name,
wenzelm@22236
   229
       lhss =
wenzelm@22236
   230
        let
wenzelm@22236
   231
          val lhss' = prep lthy lhss;
wenzelm@22236
   232
          val ctxt' = lthy
wenzelm@22236
   233
            |> fold Variable.declare_term lhss'
wenzelm@22236
   234
            |> fold Variable.auto_fixes lhss';
wenzelm@22236
   235
        in Variable.export_terms ctxt' lthy lhss' end
wenzelm@22236
   236
        |> map (Thm.cterm_of (ProofContext.theory_of lthy)),
wenzelm@22236
   237
       proc = proc,
wenzelm@22236
   238
       identifier = identifier}
wenzelm@22236
   239
      |> morph_simproc (LocalTheory.target_morphism lthy);
wenzelm@22201
   240
  in
wenzelm@22201
   241
    lthy |> LocalTheory.declaration (fn phi => fn context =>
wenzelm@22201
   242
      let
wenzelm@22201
   243
        val name' = Morphism.name phi name;
wenzelm@22236
   244
        val simproc' = morph_simproc phi simproc;
wenzelm@22201
   245
      in
wenzelm@22201
   246
        context
wenzelm@22201
   247
        |> Simprocs.map (fn simprocs =>
wenzelm@22236
   248
            NameSpace.extend_table naming (simprocs, [(name', simproc')])
wenzelm@22201
   249
              handle Symtab.DUPS ds => err_dup_simprocs ds)
wenzelm@22236
   250
        |> map_ss (fn ss => ss addsimprocs [simproc'])
wenzelm@22201
   251
      end)
wenzelm@22201
   252
  end;
wenzelm@22201
   253
wenzelm@22201
   254
in
wenzelm@22201
   255
wenzelm@22201
   256
val def_simproc = gen_simproc read_terms;
wenzelm@22201
   257
val def_simproc_i = gen_simproc cert_terms;
wenzelm@22201
   258
wenzelm@22201
   259
end;
wenzelm@22201
   260
wenzelm@22201
   261
wenzelm@22201
   262
wenzelm@17967
   263
(** simplification tactics and rules **)
wenzelm@17967
   264
wenzelm@17967
   265
fun solve_all_tac solvers ss =
wenzelm@17967
   266
  let
wenzelm@17967
   267
    val (_, {subgoal_tac, ...}) = MetaSimplifier.rep_ss ss;
wenzelm@17967
   268
    val solve_tac = subgoal_tac (MetaSimplifier.set_solvers solvers ss) THEN_ALL_NEW (K no_tac);
wenzelm@17967
   269
  in DEPTH_SOLVE (solve_tac 1) end;
wenzelm@17967
   270
wenzelm@17967
   271
(*NOTE: may instantiate unknowns that appear also in other subgoals*)
wenzelm@17967
   272
fun generic_simp_tac safe mode ss =
wenzelm@17967
   273
  let
wenzelm@17967
   274
    val (_, {loop_tacs, solvers = (unsafe_solvers, solvers), ...}) = MetaSimplifier.rep_ss ss;
haftmann@21286
   275
    val loop_tac = FIRST' (map (fn (_, tac) => tac ss) (rev loop_tacs));
wenzelm@17967
   276
    val solve_tac = FIRST' (map (MetaSimplifier.solver ss)
haftmann@22717
   277
      (rev (if safe then solvers else unsafe_solvers)));
wenzelm@17967
   278
wenzelm@17967
   279
    fun simp_loop_tac i =
wenzelm@21687
   280
      Goal.asm_rewrite_goal_tac mode (solve_all_tac unsafe_solvers) ss i THEN
wenzelm@17967
   281
      (solve_tac i ORELSE TRY ((loop_tac THEN_ALL_NEW simp_loop_tac) i));
wenzelm@17967
   282
  in simp_loop_tac end;
wenzelm@17967
   283
wenzelm@17967
   284
local
wenzelm@17967
   285
wenzelm@17967
   286
fun simp rew mode ss thm =
wenzelm@17967
   287
  let
wenzelm@17967
   288
    val (_, {solvers = (unsafe_solvers, _), ...}) = MetaSimplifier.rep_ss ss;
haftmann@22717
   289
    val tacf = solve_all_tac (rev unsafe_solvers);
wenzelm@17967
   290
    fun prover s th = Option.map #1 (Seq.pull (tacf s th));
wenzelm@17967
   291
  in rew mode prover ss thm end;
wenzelm@17967
   292
wenzelm@17967
   293
in
wenzelm@17967
   294
wenzelm@17967
   295
val simp_thm = simp MetaSimplifier.rewrite_thm;
wenzelm@17967
   296
val simp_cterm = simp MetaSimplifier.rewrite_cterm;
wenzelm@17967
   297
wenzelm@17967
   298
end;
wenzelm@17967
   299
wenzelm@17967
   300
wenzelm@16806
   301
(* tactics *)
wenzelm@16806
   302
wenzelm@16014
   303
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@16014
   304
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@16014
   305
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@16014
   306
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@16014
   307
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@16014
   308
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
wenzelm@16014
   309
wenzelm@16014
   310
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@16014
   311
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@16014
   312
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@16014
   313
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
wenzelm@16014
   314
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@16014
   315
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
wenzelm@16014
   316
wenzelm@16806
   317
wenzelm@16806
   318
(* conversions *)
wenzelm@16806
   319
wenzelm@17967
   320
val          simplify = simp_thm (false, false, false);
wenzelm@17967
   321
val      asm_simplify = simp_thm (false, true, false);
wenzelm@17967
   322
val     full_simplify = simp_thm (true, false, false);
wenzelm@17967
   323
val   asm_lr_simplify = simp_thm (true, true, false);
wenzelm@17967
   324
val asm_full_simplify = simp_thm (true, true, true);
wenzelm@16014
   325
wenzelm@17967
   326
val          rewrite = simp_cterm (false, false, false);
wenzelm@17967
   327
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@17967
   328
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@17967
   329
val   asm_lr_rewrite = simp_cterm (true, true, false);
wenzelm@17967
   330
val asm_full_rewrite = simp_cterm (true, true, true);
wenzelm@16014
   331
wenzelm@16014
   332
wenzelm@16014
   333
wenzelm@16014
   334
(** concrete syntax of attributes **)
wenzelm@16014
   335
wenzelm@16014
   336
(* add / del *)
wenzelm@16014
   337
wenzelm@16014
   338
val simpN = "simp";
wenzelm@16014
   339
val congN = "cong";
wenzelm@16014
   340
val addN = "add";
wenzelm@16014
   341
val delN = "del";
wenzelm@16014
   342
val onlyN = "only";
wenzelm@16014
   343
val no_asmN = "no_asm";
wenzelm@16014
   344
val no_asm_useN = "no_asm_use";
wenzelm@16014
   345
val no_asm_simpN = "no_asm_simp";
wenzelm@16014
   346
val asm_lrN = "asm_lr";
wenzelm@16014
   347
wenzelm@16014
   348
wenzelm@16014
   349
(* conversions *)
wenzelm@16014
   350
wenzelm@16014
   351
local
wenzelm@16014
   352
wenzelm@16014
   353
fun conv_mode x =
wenzelm@16014
   354
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@16014
   355
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@16014
   356
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@16014
   357
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@16014
   358
wenzelm@16014
   359
in
wenzelm@16014
   360
wenzelm@18688
   361
val simplified =
wenzelm@18988
   362
  Attrib.syntax (conv_mode -- Attrib.thms >> (fn (f, ths) => Thm.rule_attribute (fn x =>
wenzelm@18688
   363
    f ((if null ths then I else MetaSimplifier.clear_ss) (get_ss x) addsimps ths))));
wenzelm@16014
   364
wenzelm@16014
   365
end;
wenzelm@16014
   366
wenzelm@16014
   367
wenzelm@16014
   368
(* setup attributes *)
wenzelm@16014
   369
wenzelm@16014
   370
val _ = Context.add_setup
wenzelm@18708
   371
 (Attrib.add_attributes
wenzelm@18728
   372
   [(simpN, Attrib.add_del_args simp_add simp_del, "declaration of Simplifier rule"),
wenzelm@18728
   373
    (congN, Attrib.add_del_args cong_add cong_del, "declaration of Simplifier congruence rule"),
wenzelm@18728
   374
    ("simplified", simplified, "simplified rule")]);
wenzelm@16014
   375
wenzelm@16014
   376
wenzelm@16014
   377
wenzelm@16014
   378
(** proof methods **)
wenzelm@16014
   379
wenzelm@16014
   380
(* simplification *)
wenzelm@16014
   381
wenzelm@16014
   382
val simp_options =
wenzelm@16014
   383
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@16014
   384
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@16014
   385
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
wenzelm@16014
   386
  Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
wenzelm@16014
   387
  Scan.succeed asm_full_simp_tac);
wenzelm@16014
   388
wenzelm@16685
   389
fun simp_flags x = (Scan.repeat
wenzelm@16684
   390
  (Args.parens (Args.$$$ "depth_limit" -- Args.colon |-- Args.nat)
wenzelm@16684
   391
    >> setmp MetaSimplifier.simp_depth_limit)
wenzelm@16709
   392
  >> (curry (Library.foldl op o) I o rev)) x;
wenzelm@16684
   393
wenzelm@16014
   394
val cong_modifiers =
wenzelm@18728
   395
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add): Method.modifier),
wenzelm@18728
   396
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add),
wenzelm@18728
   397
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del)];
wenzelm@16014
   398
wenzelm@16014
   399
val simp_modifiers =
wenzelm@18728
   400
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add),
wenzelm@18728
   401
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   402
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del),
wenzelm@17883
   403
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon
wenzelm@18728
   404
    >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add)]
wenzelm@16014
   405
   @ cong_modifiers;
wenzelm@16014
   406
wenzelm@16014
   407
val simp_modifiers' =
wenzelm@18728
   408
 [Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   409
  Args.del -- Args.colon >> K (I, simp_del),
wenzelm@18688
   410
  Args.$$$ onlyN -- Args.colon
wenzelm@18728
   411
    >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add)]
wenzelm@16014
   412
   @ cong_modifiers;
wenzelm@16014
   413
wenzelm@16014
   414
fun simp_args more_mods =
wenzelm@16684
   415
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options -- Scan.lift simp_flags)
wenzelm@16684
   416
    (more_mods @ simp_modifiers');
wenzelm@16014
   417
wenzelm@16684
   418
fun simp_method ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
wenzelm@16014
   419
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@16684
   420
    (FLAGS o CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
wenzelm@16014
   421
wenzelm@16684
   422
fun simp_method' ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
wenzelm@16014
   423
  HEADGOAL (Method.insert_tac (prems @ facts) THEN'
wenzelm@16684
   424
      ((FLAGS o CHANGED_PROP) oo tac) (local_simpset_of ctxt)));
wenzelm@16014
   425
wenzelm@16014
   426
wenzelm@16014
   427
wenzelm@18708
   428
(** setup **)
wenzelm@18708
   429
wenzelm@18708
   430
fun method_setup more_mods = Method.add_methods
wenzelm@16014
   431
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@16014
   432
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@16014
   433
wenzelm@18708
   434
fun easy_setup reflect trivs = method_setup [] #> (fn thy =>
wenzelm@16014
   435
  let
wenzelm@16014
   436
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@16014
   437
wenzelm@16014
   438
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@16014
   439
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@16014
   440
wenzelm@16014
   441
    (*no premature instantiation of variables during simplification*)
wenzelm@16014
   442
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@16014
   443
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@16014
   444
wenzelm@16014
   445
    fun mk_eq thm =
wenzelm@20872
   446
      if can Logic.dest_equals (Thm.concl_of thm) then [thm]
wenzelm@16014
   447
      else [thm RS reflect] handle THM _ => [];
wenzelm@16014
   448
wenzelm@16014
   449
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@18708
   450
  in
wenzelm@18708
   451
    GlobalSimpset.get thy :=
wenzelm@18708
   452
      empty_ss setsubgoaler asm_simp_tac
wenzelm@18708
   453
      setSSolver safe_solver
wenzelm@18708
   454
      setSolver unsafe_solver
wenzelm@18708
   455
      setmksimps mksimps;
wenzelm@18708
   456
    thy
wenzelm@18708
   457
  end);
wenzelm@16014
   458
wenzelm@16014
   459
end;
wenzelm@16014
   460
wenzelm@16014
   461
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@16014
   462
open BasicSimplifier;