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