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