src/Pure/simplifier.ML
author wenzelm
Fri Mar 29 22:14:27 2013 +0100 (2013-03-29)
changeset 51580 64ef8260dc60
parent 50107 289181e3e524
child 51584 98029ceda8ce
permissions -rw-r--r--
Pretty.item markup for improved readability of lists of items;
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
haftmann@41386
     5
raw_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@41228
    10
  include BASIC_RAW_SIMPLIFIER
wenzelm@42795
    11
  val map_simpset: (simpset -> simpset) -> Proof.context -> Proof.context
wenzelm@42795
    12
  val simpset_of: Proof.context -> simpset
wenzelm@32148
    13
  val global_simpset_of: theory -> simpset
wenzelm@16014
    14
  val Addsimprocs: simproc list -> unit
wenzelm@16014
    15
  val Delsimprocs: simproc list -> unit
wenzelm@35613
    16
  val simp_tac: simpset -> int -> tactic
wenzelm@35613
    17
  val asm_simp_tac: simpset -> int -> tactic
wenzelm@35613
    18
  val full_simp_tac: simpset -> int -> tactic
wenzelm@35613
    19
  val asm_lr_simp_tac: simpset -> int -> tactic
wenzelm@35613
    20
  val asm_full_simp_tac: simpset -> int -> tactic
wenzelm@50107
    21
  val safe_simp_tac: simpset -> int -> tactic
wenzelm@50107
    22
  val safe_asm_simp_tac: simpset -> int -> tactic
wenzelm@50107
    23
  val safe_full_simp_tac: simpset -> int -> tactic
wenzelm@50107
    24
  val safe_asm_lr_simp_tac: simpset -> int -> tactic
wenzelm@50107
    25
  val safe_asm_full_simp_tac: simpset -> int -> tactic
wenzelm@35613
    26
  val simplify: simpset -> thm -> thm
wenzelm@35613
    27
  val asm_simplify: simpset -> thm -> thm
wenzelm@35613
    28
  val full_simplify: simpset -> thm -> thm
wenzelm@35613
    29
  val asm_lr_simplify: simpset -> thm -> thm
wenzelm@16014
    30
  val asm_full_simplify: simpset -> thm -> thm
wenzelm@16014
    31
end;
wenzelm@16014
    32
wenzelm@16014
    33
signature SIMPLIFIER =
wenzelm@16014
    34
sig
wenzelm@16014
    35
  include BASIC_SIMPLIFIER
wenzelm@42795
    36
  val map_simpset_global: (simpset -> simpset) -> theory -> theory
wenzelm@30356
    37
  val pretty_ss: Proof.context -> simpset -> Pretty.T
wenzelm@17004
    38
  val clear_ss: simpset -> simpset
wenzelm@47239
    39
  val default_mk_sym: simpset -> thm -> thm option
wenzelm@32738
    40
  val debug_bounds: bool Unsynchronized.ref
wenzelm@43597
    41
  val prems_of: simpset -> thm list
wenzelm@41226
    42
  val add_simp: thm -> simpset -> simpset
wenzelm@41226
    43
  val del_simp: thm -> simpset -> simpset
wenzelm@45620
    44
  val add_eqcong: thm -> simpset -> simpset
wenzelm@45620
    45
  val del_eqcong: thm -> simpset -> simpset
wenzelm@45620
    46
  val add_cong: thm -> simpset -> simpset
wenzelm@45620
    47
  val del_cong: thm -> simpset -> simpset
wenzelm@41226
    48
  val add_prems: thm list -> simpset -> simpset
wenzelm@45625
    49
  val mksimps: simpset -> thm -> thm list
wenzelm@45625
    50
  val set_mksimps: (simpset -> thm -> thm list) -> simpset -> simpset
wenzelm@45625
    51
  val set_mkcong: (simpset -> thm -> thm) -> simpset -> simpset
wenzelm@45625
    52
  val set_mksym: (simpset -> thm -> thm option) -> simpset -> simpset
wenzelm@45625
    53
  val set_mkeqTrue: (simpset -> thm -> thm option) -> simpset -> simpset
wenzelm@45625
    54
  val set_termless: (term * term -> bool) -> simpset -> simpset
wenzelm@45625
    55
  val set_subgoaler: (simpset -> int -> tactic) -> simpset -> simpset
wenzelm@17883
    56
  val inherit_context: simpset -> simpset -> simpset
wenzelm@17898
    57
  val the_context: simpset -> Proof.context
wenzelm@17898
    58
  val context: Proof.context -> simpset -> simpset
haftmann@37441
    59
  val global_context: theory -> simpset -> simpset
wenzelm@36600
    60
  val with_context: Proof.context -> (simpset -> simpset) -> simpset -> simpset
wenzelm@42795
    61
  val simproc_global_i: theory -> string -> term list ->
wenzelm@42795
    62
    (theory -> simpset -> term -> thm option) -> simproc
wenzelm@42795
    63
  val simproc_global: theory -> string -> string list ->
wenzelm@42795
    64
    (theory -> simpset -> term -> thm option) -> simproc
wenzelm@35613
    65
  val rewrite: simpset -> conv
wenzelm@35613
    66
  val asm_rewrite: simpset -> conv
wenzelm@35613
    67
  val full_rewrite: simpset -> conv
wenzelm@35613
    68
  val asm_lr_rewrite: simpset -> conv
wenzelm@23598
    69
  val asm_full_rewrite: simpset -> conv
wenzelm@22379
    70
  val get_ss: Context.generic -> simpset
wenzelm@22379
    71
  val map_ss: (simpset -> simpset) -> Context.generic -> Context.generic
wenzelm@45620
    72
  val attrib: (thm -> simpset -> simpset) -> attribute
wenzelm@18728
    73
  val simp_add: attribute
wenzelm@18728
    74
  val simp_del: attribute
wenzelm@18728
    75
  val cong_add: attribute
wenzelm@18728
    76
  val cong_del: attribute
wenzelm@42465
    77
  val check_simproc: Proof.context -> xstring * Position.T -> string
wenzelm@42465
    78
  val the_simproc: Proof.context -> string -> simproc
wenzelm@42464
    79
  val def_simproc: {name: binding, lhss: term list,
wenzelm@22236
    80
    proc: morphism -> simpset -> cterm -> thm option, identifier: thm list} ->
wenzelm@22201
    81
    local_theory -> local_theory
wenzelm@42464
    82
  val def_simproc_cmd: {name: binding, lhss: string list,
wenzelm@22236
    83
    proc: morphism -> simpset -> cterm -> thm option, identifier: thm list} ->
wenzelm@22201
    84
    local_theory -> local_theory
wenzelm@30513
    85
  val cong_modifiers: Method.modifier parser list
wenzelm@30513
    86
  val simp_modifiers': Method.modifier parser list
wenzelm@30513
    87
  val simp_modifiers: Method.modifier parser list
wenzelm@30513
    88
  val method_setup: Method.modifier parser list -> theory -> theory
wenzelm@18708
    89
  val easy_setup: thm -> thm list -> theory -> theory
wenzelm@16014
    90
end;
wenzelm@16014
    91
wenzelm@16014
    92
structure Simplifier: SIMPLIFIER =
wenzelm@16014
    93
struct
wenzelm@16014
    94
wenzelm@41228
    95
open Raw_Simplifier;
wenzelm@21708
    96
wenzelm@21708
    97
wenzelm@42465
    98
(** data **)
wenzelm@42465
    99
wenzelm@42465
   100
structure Data = Generic_Data
wenzelm@42465
   101
(
wenzelm@42465
   102
  type T = simpset * simproc Name_Space.table;
wenzelm@42465
   103
  val empty : T = (empty_ss, Name_Space.empty_table "simproc");
wenzelm@42465
   104
  fun extend (ss, tab) = (Raw_Simplifier.inherit_context empty_ss ss, tab);
wenzelm@42465
   105
  fun merge ((ss1, tab1), (ss2, tab2)) =
wenzelm@42465
   106
    (merge_ss (ss1, ss2), Name_Space.merge_tables (tab1, tab2));
wenzelm@42465
   107
);
wenzelm@42465
   108
wenzelm@42465
   109
val get_ss = fst o Data.get;
wenzelm@42465
   110
wenzelm@42465
   111
fun map_ss f context =
wenzelm@42465
   112
  Data.map (apfst ((Raw_Simplifier.with_context (Context.proof_of context) f))) context;
wenzelm@42465
   113
wenzelm@42465
   114
val get_simprocs = snd o Data.get o Context.Proof;
wenzelm@42465
   115
wenzelm@42465
   116
wenzelm@42465
   117
wenzelm@30356
   118
(** pretty printing **)
wenzelm@30356
   119
wenzelm@30356
   120
fun pretty_ss ctxt ss =
wenzelm@30356
   121
  let
wenzelm@30356
   122
    val pretty_cterm = Syntax.pretty_term ctxt o Thm.term_of;
wenzelm@32091
   123
    val pretty_thm = Display.pretty_thm ctxt;
wenzelm@51580
   124
    fun pretty_proc (name, lhss) =
wenzelm@51580
   125
      Pretty.big_list (name ^ ":") (map (Pretty.item o single o pretty_cterm) lhss);
wenzelm@30356
   126
    fun pretty_cong (name, thm) =
wenzelm@30356
   127
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, pretty_thm thm];
wenzelm@30356
   128
wenzelm@30356
   129
    val {simps, procs, congs, loopers, unsafe_solvers, safe_solvers, ...} = dest_ss ss;
wenzelm@30356
   130
  in
wenzelm@51580
   131
    [Pretty.big_list "simplification rules:" (map (Pretty.item o single o pretty_thm o #2) simps),
wenzelm@30356
   132
      Pretty.big_list "simplification procedures:" (map pretty_proc (sort_wrt #1 procs)),
wenzelm@30356
   133
      Pretty.big_list "congruences:" (map pretty_cong congs),
wenzelm@30356
   134
      Pretty.strs ("loopers:" :: map quote loopers),
wenzelm@30356
   135
      Pretty.strs ("unsafe solvers:" :: map quote unsafe_solvers),
wenzelm@30356
   136
      Pretty.strs ("safe solvers:" :: map quote safe_solvers)]
wenzelm@30356
   137
    |> Pretty.chunks
wenzelm@30356
   138
  end;
wenzelm@30356
   139
wenzelm@30356
   140
wenzelm@30356
   141
wenzelm@17883
   142
(** simpset data **)
wenzelm@16014
   143
wenzelm@26497
   144
(* attributes *)
wenzelm@26497
   145
wenzelm@45620
   146
fun attrib f = Thm.declaration_attribute (map_ss o f);
wenzelm@16014
   147
wenzelm@45620
   148
val simp_add = attrib add_simp;
wenzelm@45620
   149
val simp_del = attrib del_simp;
wenzelm@45620
   150
val cong_add = attrib add_cong;
wenzelm@45620
   151
val cong_del = attrib del_cong;
wenzelm@26497
   152
wenzelm@16014
   153
wenzelm@26497
   154
(* local simpset *)
wenzelm@16014
   155
wenzelm@42795
   156
fun map_simpset f = Context.proof_map (map_ss f);
wenzelm@41228
   157
fun simpset_of ctxt = Raw_Simplifier.context ctxt (get_ss (Context.Proof ctxt));
wenzelm@16014
   158
wenzelm@43560
   159
val _ = Context.>> (Context.map_theory
wenzelm@43560
   160
  (ML_Antiquote.value (Binding.name "simpset")
wenzelm@48776
   161
    (Scan.succeed "Simplifier.simpset_of ML_context")));
wenzelm@22132
   162
wenzelm@16014
   163
wenzelm@42795
   164
(* global simpset *)
wenzelm@42795
   165
wenzelm@42795
   166
fun map_simpset_global f = Context.theory_map (map_ss f);
wenzelm@42795
   167
fun global_simpset_of thy =
wenzelm@42795
   168
  Raw_Simplifier.context (Proof_Context.init_global thy) (get_ss (Context.Theory thy));
wenzelm@42795
   169
wenzelm@42795
   170
fun Addsimprocs args = Context.>> (map_ss (fn ss => ss addsimprocs args));
wenzelm@42795
   171
fun Delsimprocs args = Context.>> (map_ss (fn ss => ss delsimprocs args));
wenzelm@42795
   172
wenzelm@42795
   173
wenzelm@17967
   174
wenzelm@22201
   175
(** named simprocs **)
wenzelm@22201
   176
wenzelm@22204
   177
(* get simprocs *)
wenzelm@22204
   178
wenzelm@47005
   179
fun check_simproc ctxt = Name_Space.check (Context.Proof ctxt) (get_simprocs ctxt) #> #1;
wenzelm@42466
   180
val the_simproc = Name_Space.get o get_simprocs;
wenzelm@42465
   181
wenzelm@42464
   182
val _ =
wenzelm@43560
   183
  Context.>> (Context.map_theory
wenzelm@43560
   184
   (ML_Antiquote.value (Binding.name "simproc")
wenzelm@43560
   185
      (Args.context -- Scan.lift (Parse.position Args.name)
wenzelm@43560
   186
        >> (fn (ctxt, name) =>
wenzelm@48776
   187
          "Simplifier.the_simproc ML_context " ^
wenzelm@43560
   188
            ML_Syntax.print_string (check_simproc ctxt name)))));
wenzelm@22204
   189
wenzelm@22204
   190
wenzelm@22204
   191
(* define simprocs *)
wenzelm@22201
   192
wenzelm@22201
   193
local
wenzelm@22201
   194
wenzelm@42464
   195
fun gen_simproc prep {name = b, lhss, proc, identifier} lthy =
wenzelm@22201
   196
  let
wenzelm@22236
   197
    val simproc = make_simproc
wenzelm@47001
   198
      {name = Local_Theory.full_name lthy b,
wenzelm@22236
   199
       lhss =
wenzelm@22236
   200
        let
wenzelm@22236
   201
          val lhss' = prep lthy lhss;
wenzelm@45326
   202
          val ctxt' = fold Variable.auto_fixes lhss' lthy;
wenzelm@22236
   203
        in Variable.export_terms ctxt' lthy lhss' end
wenzelm@42360
   204
        |> map (Thm.cterm_of (Proof_Context.theory_of lthy)),
wenzelm@22236
   205
       proc = proc,
wenzelm@33551
   206
       identifier = identifier};
wenzelm@22201
   207
  in
wenzelm@47001
   208
    lthy |> Local_Theory.declaration {syntax = false, pervasive = true} (fn phi => fn context =>
wenzelm@22201
   209
      let
haftmann@28991
   210
        val b' = Morphism.binding phi b;
wenzelm@45290
   211
        val simproc' = transform_simproc phi simproc;
wenzelm@22201
   212
      in
wenzelm@42375
   213
        context
wenzelm@42465
   214
        |> Data.map (fn (ss, tab) =>
wenzelm@47005
   215
          (ss addsimprocs [simproc'], #2 (Name_Space.define context true (b', simproc') tab)))
wenzelm@22201
   216
      end)
wenzelm@22201
   217
  end;
wenzelm@22201
   218
wenzelm@22201
   219
in
wenzelm@22201
   220
wenzelm@42464
   221
val def_simproc = gen_simproc Syntax.check_terms;
wenzelm@42464
   222
val def_simproc_cmd = gen_simproc Syntax.read_terms;
wenzelm@22201
   223
wenzelm@22201
   224
end;
wenzelm@22201
   225
wenzelm@22201
   226
wenzelm@22201
   227
wenzelm@17967
   228
(** simplification tactics and rules **)
wenzelm@17967
   229
wenzelm@17967
   230
fun solve_all_tac solvers ss =
wenzelm@17967
   231
  let
wenzelm@41228
   232
    val (_, {subgoal_tac, ...}) = Raw_Simplifier.internal_ss ss;
wenzelm@41228
   233
    val solve_tac = subgoal_tac (Raw_Simplifier.set_solvers solvers ss) THEN_ALL_NEW (K no_tac);
wenzelm@17967
   234
  in DEPTH_SOLVE (solve_tac 1) end;
wenzelm@17967
   235
wenzelm@17967
   236
(*NOTE: may instantiate unknowns that appear also in other subgoals*)
wenzelm@17967
   237
fun generic_simp_tac safe mode ss =
wenzelm@17967
   238
  let
wenzelm@41228
   239
    val (_, {loop_tacs, solvers = (unsafe_solvers, solvers), ...}) = Raw_Simplifier.internal_ss ss;
haftmann@21286
   240
    val loop_tac = FIRST' (map (fn (_, tac) => tac ss) (rev loop_tacs));
wenzelm@41228
   241
    val solve_tac = FIRST' (map (Raw_Simplifier.solver ss)
haftmann@22717
   242
      (rev (if safe then solvers else unsafe_solvers)));
wenzelm@17967
   243
wenzelm@17967
   244
    fun simp_loop_tac i =
wenzelm@46465
   245
      Raw_Simplifier.generic_rewrite_goal_tac mode (solve_all_tac unsafe_solvers) ss i THEN
wenzelm@17967
   246
      (solve_tac i ORELSE TRY ((loop_tac THEN_ALL_NEW simp_loop_tac) i));
wenzelm@47468
   247
  in SELECT_GOAL (simp_loop_tac 1) end;
wenzelm@17967
   248
wenzelm@17967
   249
local
wenzelm@17967
   250
wenzelm@17967
   251
fun simp rew mode ss thm =
wenzelm@17967
   252
  let
wenzelm@41228
   253
    val (_, {solvers = (unsafe_solvers, _), ...}) = Raw_Simplifier.internal_ss ss;
haftmann@22717
   254
    val tacf = solve_all_tac (rev unsafe_solvers);
wenzelm@17967
   255
    fun prover s th = Option.map #1 (Seq.pull (tacf s th));
wenzelm@17967
   256
  in rew mode prover ss thm end;
wenzelm@17967
   257
wenzelm@17967
   258
in
wenzelm@17967
   259
wenzelm@41228
   260
val simp_thm = simp Raw_Simplifier.rewrite_thm;
wenzelm@41228
   261
val simp_cterm = simp Raw_Simplifier.rewrite_cterm;
wenzelm@17967
   262
wenzelm@17967
   263
end;
wenzelm@17967
   264
wenzelm@17967
   265
wenzelm@16806
   266
(* tactics *)
wenzelm@16806
   267
wenzelm@16014
   268
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@16014
   269
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@16014
   270
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@16014
   271
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@16014
   272
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@50107
   273
wenzelm@50107
   274
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
wenzelm@50107
   275
val safe_simp_tac = generic_simp_tac true (false, false, false);
wenzelm@50107
   276
val safe_asm_simp_tac = generic_simp_tac true (false, true, false);
wenzelm@50107
   277
val safe_full_simp_tac = generic_simp_tac true (true, false, false);
wenzelm@50107
   278
val safe_asm_lr_simp_tac = generic_simp_tac true (true, true, false);
wenzelm@16014
   279
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
wenzelm@16014
   280
wenzelm@16806
   281
wenzelm@16806
   282
(* conversions *)
wenzelm@16806
   283
wenzelm@17967
   284
val          simplify = simp_thm (false, false, false);
wenzelm@17967
   285
val      asm_simplify = simp_thm (false, true, false);
wenzelm@17967
   286
val     full_simplify = simp_thm (true, false, false);
wenzelm@17967
   287
val   asm_lr_simplify = simp_thm (true, true, false);
wenzelm@17967
   288
val asm_full_simplify = simp_thm (true, true, true);
wenzelm@16014
   289
wenzelm@17967
   290
val          rewrite = simp_cterm (false, false, false);
wenzelm@17967
   291
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@17967
   292
val     full_rewrite = simp_cterm (true, false, false);
wenzelm@17967
   293
val   asm_lr_rewrite = simp_cterm (true, true, false);
wenzelm@17967
   294
val asm_full_rewrite = simp_cterm (true, true, true);
wenzelm@16014
   295
wenzelm@16014
   296
wenzelm@16014
   297
wenzelm@16014
   298
(** concrete syntax of attributes **)
wenzelm@16014
   299
wenzelm@16014
   300
(* add / del *)
wenzelm@16014
   301
wenzelm@16014
   302
val simpN = "simp";
wenzelm@16014
   303
val congN = "cong";
wenzelm@16014
   304
val onlyN = "only";
wenzelm@16014
   305
val no_asmN = "no_asm";
wenzelm@16014
   306
val no_asm_useN = "no_asm_use";
wenzelm@16014
   307
val no_asm_simpN = "no_asm_simp";
wenzelm@16014
   308
val asm_lrN = "asm_lr";
wenzelm@16014
   309
wenzelm@16014
   310
wenzelm@24024
   311
(* simprocs *)
wenzelm@24024
   312
wenzelm@24024
   313
local
wenzelm@24024
   314
wenzelm@24024
   315
val add_del =
wenzelm@24024
   316
  (Args.del -- Args.colon >> K (op delsimprocs) ||
wenzelm@24024
   317
    Scan.option (Args.add -- Args.colon) >> K (op addsimprocs))
wenzelm@24024
   318
  >> (fn f => fn simproc => fn phi => Thm.declaration_attribute
wenzelm@45290
   319
      (K (map_ss (fn ss => f (ss, [transform_simproc phi simproc])))));
wenzelm@24024
   320
wenzelm@24024
   321
in
wenzelm@24024
   322
wenzelm@30528
   323
val simproc_att =
wenzelm@42465
   324
  (Args.context -- Scan.lift add_del) :|-- (fn (ctxt, decl) =>
wenzelm@42465
   325
    Scan.repeat1 (Scan.lift (Args.named_attribute (decl o the_simproc ctxt o check_simproc ctxt))))
wenzelm@45375
   326
  >> (fn atts => Thm.declaration_attribute (fn th =>
wenzelm@46776
   327
        fold (fn att => Thm.attribute_declaration (Morphism.form att) th) atts));
wenzelm@24024
   328
wenzelm@24024
   329
end;
wenzelm@24124
   330
wenzelm@24024
   331
wenzelm@16014
   332
(* conversions *)
wenzelm@16014
   333
wenzelm@16014
   334
local
wenzelm@16014
   335
wenzelm@16014
   336
fun conv_mode x =
wenzelm@16014
   337
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@16014
   338
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@16014
   339
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@16014
   340
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@16014
   341
wenzelm@16014
   342
in
wenzelm@16014
   343
wenzelm@30528
   344
val simplified = conv_mode -- Attrib.thms >>
wenzelm@30528
   345
  (fn (f, ths) => Thm.rule_attribute (fn context =>
wenzelm@41228
   346
    f ((if null ths then I else Raw_Simplifier.clear_ss)
wenzelm@32148
   347
        (simpset_of (Context.proof_of context)) addsimps ths)));
wenzelm@16014
   348
wenzelm@16014
   349
end;
wenzelm@16014
   350
wenzelm@16014
   351
wenzelm@16014
   352
(* setup attributes *)
wenzelm@16014
   353
wenzelm@26463
   354
val _ = Context.>> (Context.map_theory
wenzelm@30528
   355
 (Attrib.setup (Binding.name simpN) (Attrib.add_del simp_add simp_del)
wenzelm@30528
   356
    "declaration of Simplifier rewrite rule" #>
wenzelm@30528
   357
  Attrib.setup (Binding.name congN) (Attrib.add_del cong_add cong_del)
wenzelm@30528
   358
    "declaration of Simplifier congruence rule" #>
wenzelm@33671
   359
  Attrib.setup (Binding.name "simproc") simproc_att
wenzelm@33671
   360
    "declaration of simplification procedures" #>
wenzelm@30528
   361
  Attrib.setup (Binding.name "simplified") simplified "simplified rule"));
wenzelm@16014
   362
wenzelm@16014
   363
wenzelm@16014
   364
wenzelm@31300
   365
(** method syntax **)
wenzelm@16014
   366
wenzelm@16014
   367
val cong_modifiers =
wenzelm@18728
   368
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add): Method.modifier),
wenzelm@18728
   369
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add),
wenzelm@18728
   370
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del)];
wenzelm@16014
   371
wenzelm@16014
   372
val simp_modifiers =
wenzelm@18728
   373
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add),
wenzelm@18728
   374
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   375
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del),
wenzelm@17883
   376
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon
wenzelm@41228
   377
    >> K (Context.proof_map (map_ss Raw_Simplifier.clear_ss), simp_add)]
wenzelm@16014
   378
   @ cong_modifiers;
wenzelm@16014
   379
wenzelm@16014
   380
val simp_modifiers' =
wenzelm@18728
   381
 [Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   382
  Args.del -- Args.colon >> K (I, simp_del),
wenzelm@18688
   383
  Args.$$$ onlyN -- Args.colon
wenzelm@41228
   384
    >> K (Context.proof_map (map_ss Raw_Simplifier.clear_ss), simp_add)]
wenzelm@16014
   385
   @ cong_modifiers;
wenzelm@16014
   386
wenzelm@31300
   387
val simp_options =
wenzelm@31300
   388
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@31300
   389
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@31300
   390
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
wenzelm@31300
   391
  Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
wenzelm@31300
   392
  Scan.succeed asm_full_simp_tac);
wenzelm@16014
   393
wenzelm@31300
   394
fun simp_method more_mods meth =
wenzelm@35613
   395
  Scan.lift simp_options --|
wenzelm@31300
   396
    Method.sections (more_mods @ simp_modifiers') >>
wenzelm@35613
   397
    (fn tac => fn ctxt => METHOD (fn facts => meth ctxt tac facts));
wenzelm@16014
   398
wenzelm@16014
   399
wenzelm@16014
   400
wenzelm@18708
   401
(** setup **)
wenzelm@18708
   402
wenzelm@31300
   403
fun method_setup more_mods =
wenzelm@31300
   404
  Method.setup (Binding.name simpN)
wenzelm@31300
   405
    (simp_method more_mods (fn ctxt => fn tac => fn facts =>
wenzelm@31300
   406
      HEADGOAL (Method.insert_tac facts THEN'
wenzelm@32148
   407
        (CHANGED_PROP oo tac) (simpset_of ctxt))))
wenzelm@31300
   408
    "simplification" #>
wenzelm@31300
   409
  Method.setup (Binding.name "simp_all")
wenzelm@31300
   410
    (simp_method more_mods (fn ctxt => fn tac => fn facts =>
wenzelm@31300
   411
      ALLGOALS (Method.insert_tac facts) THEN
wenzelm@42372
   412
        (CHANGED_PROP o PARALLEL_GOALS o ALLGOALS o tac) (simpset_of ctxt)))
wenzelm@31300
   413
    "simplification (all goals)";
wenzelm@16014
   414
wenzelm@26497
   415
fun easy_setup reflect trivs = method_setup [] #> Context.theory_map (map_ss (fn _ =>
wenzelm@16014
   416
  let
wenzelm@16014
   417
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@16014
   418
wenzelm@43597
   419
    fun unsafe_solver_tac ss =
wenzelm@43597
   420
      FIRST' [resolve_tac (trivialities @ Raw_Simplifier.prems_of ss), assume_tac];
wenzelm@16014
   421
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@16014
   422
wenzelm@16014
   423
    (*no premature instantiation of variables during simplification*)
wenzelm@43597
   424
    fun safe_solver_tac ss =
wenzelm@43597
   425
      FIRST' [match_tac (trivialities @ Raw_Simplifier.prems_of ss), eq_assume_tac];
wenzelm@16014
   426
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@16014
   427
wenzelm@16014
   428
    fun mk_eq thm =
wenzelm@20872
   429
      if can Logic.dest_equals (Thm.concl_of thm) then [thm]
wenzelm@16014
   430
      else [thm RS reflect] handle THM _ => [];
wenzelm@16014
   431
wenzelm@44058
   432
    fun mksimps thm = mk_eq (Thm.forall_elim_vars (Thm.maxidx_of thm + 1) thm);
wenzelm@26497
   433
  in
wenzelm@45625
   434
    empty_ss
wenzelm@26497
   435
    setSSolver safe_solver
wenzelm@26497
   436
    setSolver unsafe_solver
wenzelm@45625
   437
    |> set_subgoaler asm_simp_tac
wenzelm@45625
   438
    |> set_mksimps (K mksimps)
wenzelm@26497
   439
  end));
wenzelm@16014
   440
wenzelm@16014
   441
end;
wenzelm@16014
   442
wenzelm@32738
   443
structure Basic_Simplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@32738
   444
open Basic_Simplifier;