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