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