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