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