src/Provers/simplifier.ML
author wenzelm
Sun Jul 11 20:35:50 2004 +0200 (2004-07-11 ago)
changeset 15036 cab1c1fc1851
parent 15024 341cd221c3e1
child 15452 e2a721567f67
permissions -rw-r--r--
context dependent components;
clasohm@1243
     1
(*  Title:      Provers/simplifier.ML
nipkow@1
     2
    ID:         $Id$
wenzelm@3557
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
nipkow@1
     4
berghofe@10412
     5
Generic simplifier, suitable for most logics.  See Pure/meta_simplifier.ML
berghofe@10412
     6
for the actual meta-level rewriting engine.
nipkow@1
     7
*)
clasohm@1260
     8
wenzelm@4795
     9
signature BASIC_SIMPLIFIER =
clasohm@0
    10
sig
wenzelm@15024
    11
  include BASIC_META_SIMPLIFIER
wenzelm@15036
    12
  type context_solver
wenzelm@15036
    13
  val mk_context_solver: string -> (Proof.context -> thm list -> int -> tactic)
wenzelm@15036
    14
    -> context_solver
wenzelm@15036
    15
  type context_simproc
wenzelm@15036
    16
  val mk_context_simproc: string -> cterm list ->
wenzelm@15036
    17
    (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@4366
    18
  val print_simpset: theory -> unit
wenzelm@4080
    19
  val simpset_ref_of_sg: Sign.sg -> simpset ref
wenzelm@4080
    20
  val simpset_ref_of: theory -> simpset ref
wenzelm@4080
    21
  val simpset_of_sg: Sign.sg -> simpset
wenzelm@4080
    22
  val simpset_of: theory -> simpset
wenzelm@4080
    23
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@4080
    24
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4080
    25
  val simpset: unit -> simpset
wenzelm@4080
    26
  val simpset_ref: unit -> simpset ref
clasohm@1243
    27
  val Addsimps: thm list -> unit
clasohm@1243
    28
  val Delsimps: thm list -> unit
wenzelm@2509
    29
  val Addsimprocs: simproc list -> unit
wenzelm@2509
    30
  val Delsimprocs: simproc list -> unit
wenzelm@9715
    31
  val Addcongs: thm list -> unit
wenzelm@9715
    32
  val Delcongs: thm list -> unit
wenzelm@15036
    33
  val local_simpset_of: Proof.context -> simpset
wenzelm@10004
    34
  val safe_asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    35
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    36
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    37
  val          full_simp_tac: simpset -> int -> tactic
nipkow@4722
    38
  val        asm_lr_simp_tac: simpset -> int -> tactic
oheimb@2629
    39
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    40
  val               Simp_tac:            int -> tactic
oheimb@2629
    41
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    42
  val          Full_simp_tac:            int -> tactic
nipkow@4722
    43
  val        Asm_lr_simp_tac:            int -> tactic
oheimb@2629
    44
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    45
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    46
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    47
  val     full_simplify: simpset -> thm -> thm
schirmer@14814
    48
  val   asm_lr_simplify: simpset -> thm -> thm
wenzelm@3557
    49
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    50
end;
clasohm@0
    51
wenzelm@4795
    52
signature SIMPLIFIER =
wenzelm@4795
    53
sig
wenzelm@4795
    54
  include BASIC_SIMPLIFIER
wenzelm@15036
    55
  val simproc_i: Sign.sg -> string -> term list
wenzelm@15036
    56
    -> (Sign.sg -> simpset -> term -> thm option) -> simproc
wenzelm@15036
    57
  val simproc: Sign.sg -> string -> string list
wenzelm@15036
    58
    -> (Sign.sg -> simpset -> term -> thm option) -> simproc
wenzelm@15036
    59
  val context_simproc_i: Sign.sg -> string -> term list
wenzelm@15036
    60
    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@15036
    61
  val context_simproc: Sign.sg -> string -> string list
wenzelm@15036
    62
    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
wenzelm@5082
    63
  val          rewrite: simpset -> cterm -> thm
wenzelm@5082
    64
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@5082
    65
  val     full_rewrite: simpset -> cterm -> thm
schirmer@14814
    66
  val   asm_lr_rewrite: simpset -> cterm -> thm
wenzelm@5082
    67
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@15036
    68
  val add_context_simprocs: context_simproc list -> theory -> theory
wenzelm@15036
    69
  val del_context_simprocs: context_simproc list -> theory -> theory
wenzelm@15036
    70
  val set_context_subgoaler: (Proof.context -> simpset -> int -> tactic) -> theory -> theory
wenzelm@15036
    71
  val reset_context_subgoaler: theory -> theory
wenzelm@15036
    72
  val add_context_looper: string * (Proof.context -> int -> Tactical.tactic) ->
wenzelm@15036
    73
    theory -> theory
wenzelm@15036
    74
  val del_context_looper: string -> theory -> theory
wenzelm@15036
    75
  val add_context_unsafe_solver: context_solver -> theory -> theory
wenzelm@15036
    76
  val add_context_safe_solver: context_solver -> theory -> theory
wenzelm@5842
    77
  val print_local_simpset: Proof.context -> unit
wenzelm@5842
    78
  val get_local_simpset: Proof.context -> simpset
wenzelm@5842
    79
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@8467
    80
  val change_global_ss: (simpset * thm list -> simpset) -> theory attribute
wenzelm@8467
    81
  val change_local_ss: (simpset * thm list -> simpset) -> Proof.context attribute
wenzelm@4855
    82
  val simp_add_global: theory attribute
wenzelm@4855
    83
  val simp_del_global: theory attribute
wenzelm@5842
    84
  val simp_add_local: Proof.context attribute
wenzelm@5842
    85
  val simp_del_local: Proof.context attribute
wenzelm@9715
    86
  val cong_add_global: theory attribute
wenzelm@9715
    87
  val cong_del_global: theory attribute
wenzelm@9715
    88
  val cong_add_local: Proof.context attribute
wenzelm@9715
    89
  val cong_del_local: Proof.context attribute
wenzelm@7177
    90
  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
wenzelm@7273
    91
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@5842
    92
  val setup: (theory -> theory) list
wenzelm@8467
    93
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@8467
    94
    -> (theory -> theory) list
wenzelm@8228
    95
  val easy_setup: thm -> thm list -> (theory -> theory) list
wenzelm@4795
    96
end;
wenzelm@2503
    97
wenzelm@2503
    98
structure Simplifier: SIMPLIFIER =
clasohm@0
    99
struct
clasohm@0
   100
skalberg@15006
   101
open MetaSimplifier;
wenzelm@8467
   102
wenzelm@15024
   103
wenzelm@15036
   104
(** context dependent simpset components **)
wenzelm@15036
   105
wenzelm@15036
   106
(* datatype context_solver *)
wenzelm@15036
   107
wenzelm@15036
   108
datatype context_solver =
wenzelm@15036
   109
  ContextSolver of (string * (Proof.context -> thm list -> int -> tactic)) * stamp;
wenzelm@15036
   110
wenzelm@15036
   111
fun mk_context_solver name f = ContextSolver ((name, f), stamp ());
wenzelm@15036
   112
fun eq_context_solver (ContextSolver (_, id1), ContextSolver (_, id2)) = (id1 = id2);
wenzelm@15036
   113
val merge_context_solvers = gen_merge_lists eq_context_solver;
wenzelm@15036
   114
wenzelm@15036
   115
wenzelm@15036
   116
(* datatype context_simproc *)
wenzelm@15036
   117
wenzelm@15036
   118
datatype context_simproc = ContextSimproc of
wenzelm@15036
   119
  (string * cterm list * (Proof.context -> simpset -> term -> thm option)) * stamp;
wenzelm@15036
   120
wenzelm@15036
   121
fun mk_context_simproc name lhss f = ContextSimproc ((name, lhss, f), stamp ());
wenzelm@15036
   122
fun eq_context_simproc (ContextSimproc (_, id1), ContextSimproc (_, id2)) = (id1 = id2);
wenzelm@15036
   123
val merge_context_simprocs = gen_merge_lists eq_context_simproc;
wenzelm@15036
   124
wenzelm@15036
   125
fun context_simproc_i sg name =
wenzelm@15036
   126
  mk_context_simproc name o map (Thm.cterm_of sg o Logic.varify);
wenzelm@15036
   127
wenzelm@15036
   128
fun context_simproc sg name =
wenzelm@15036
   129
  context_simproc_i sg name o map (Sign.simple_read_term sg TypeInfer.logicT);
wenzelm@15036
   130
wenzelm@15036
   131
wenzelm@15036
   132
(* datatype context_ss *)
wenzelm@15036
   133
wenzelm@15036
   134
datatype context_ss = ContextSS of
wenzelm@15036
   135
 {simprocs: context_simproc list,
wenzelm@15036
   136
  subgoal_tac: (Proof.context -> simpset -> int -> tactic) option,
wenzelm@15036
   137
  loop_tacs: (string * (Proof.context -> int -> tactic)) list,
wenzelm@15036
   138
  unsafe_solvers: context_solver list,
wenzelm@15036
   139
  solvers: context_solver list};
wenzelm@15036
   140
wenzelm@15036
   141
fun context_ss ctxt ss ctxt_ss =
wenzelm@15036
   142
  let
wenzelm@15036
   143
    val ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} = ctxt_ss;
wenzelm@15036
   144
    fun prep_simproc (ContextSimproc ((name, lhss, f), _)) =
wenzelm@15036
   145
      mk_simproc name lhss (K (f ctxt));
wenzelm@15036
   146
    fun add_loop (name, f) simpset = simpset addloop (name, f ctxt);
wenzelm@15036
   147
    fun add_solver add (ContextSolver ((name, f), _)) simpset =
wenzelm@15036
   148
      add (simpset, mk_solver name (f ctxt));
wenzelm@15036
   149
  in
wenzelm@15036
   150
    ((case subgoal_tac of None => ss | Some tac => ss setsubgoaler tac ctxt)
wenzelm@15036
   151
      addsimprocs map prep_simproc simprocs)
wenzelm@15036
   152
    |> fold_rev add_loop loop_tacs
wenzelm@15036
   153
    |> fold_rev (add_solver (op addSolver)) unsafe_solvers
wenzelm@15036
   154
    |> fold_rev (add_solver (op addSSolver)) solvers
wenzelm@15036
   155
  end;
wenzelm@15036
   156
wenzelm@15036
   157
fun make_context_ss (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =
wenzelm@15036
   158
  ContextSS {simprocs = simprocs, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
wenzelm@15036
   159
    unsafe_solvers = unsafe_solvers, solvers = solvers};
wenzelm@15036
   160
wenzelm@15036
   161
val empty_context_ss = make_context_ss ([], None, [], [], []);
wenzelm@15036
   162
wenzelm@15036
   163
fun merge_context_ss (ctxt_ss1, ctxt_ss2) =
wenzelm@15036
   164
  let
wenzelm@15036
   165
    val ContextSS {simprocs = simprocs1, subgoal_tac = subgoal_tac1, loop_tacs = loop_tacs1,
wenzelm@15036
   166
      unsafe_solvers = unsafe_solvers1, solvers = solvers1} = ctxt_ss1;
wenzelm@15036
   167
    val ContextSS {simprocs = simprocs2, subgoal_tac = subgoal_tac2, loop_tacs = loop_tacs2,
wenzelm@15036
   168
      unsafe_solvers = unsafe_solvers2, solvers = solvers2} = ctxt_ss2;
wenzelm@15036
   169
wenzelm@15036
   170
    val simprocs' = merge_context_simprocs simprocs1 simprocs2;
wenzelm@15036
   171
    val subgoal_tac' = (case subgoal_tac1 of None => subgoal_tac2 | some => some);
wenzelm@15036
   172
    val loop_tacs' = merge_alists loop_tacs1 loop_tacs2;
wenzelm@15036
   173
    val unsafe_solvers' = merge_context_solvers unsafe_solvers1 unsafe_solvers2;
wenzelm@15036
   174
    val solvers' = merge_context_solvers solvers1 solvers2;
wenzelm@15036
   175
  in make_context_ss (simprocs', subgoal_tac', loop_tacs', unsafe_solvers', solvers') end;
wenzelm@15036
   176
wenzelm@15036
   177
wenzelm@15036
   178
wenzelm@5024
   179
(** global and local simpset data **)
wenzelm@4080
   180
wenzelm@5842
   181
(* theory data kind 'Provers/simpset' *)
wenzelm@4795
   182
wenzelm@5842
   183
structure GlobalSimpsetArgs =
wenzelm@5024
   184
struct
wenzelm@5842
   185
  val name = "Provers/simpset";
wenzelm@15036
   186
  type T = simpset ref * context_ss;
clasohm@0
   187
wenzelm@15036
   188
  val empty = (ref empty_ss, empty_context_ss);
wenzelm@15036
   189
  fun copy (ref ss, ctxt_ss) = (ref ss, ctxt_ss): T;            (*create new reference!*)
wenzelm@6556
   190
  val prep_ext = copy;
wenzelm@15036
   191
  fun merge ((ref ss1, ctxt_ss1), (ref ss2, ctxt_ss2)) =
wenzelm@15036
   192
    (ref (merge_ss (ss1, ss2)), merge_context_ss (ctxt_ss1, ctxt_ss2));
wenzelm@15036
   193
  fun print _ (ref ss, _) = print_ss ss;
wenzelm@5024
   194
end;
wenzelm@4080
   195
wenzelm@5842
   196
structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
wenzelm@5842
   197
val print_simpset = GlobalSimpset.print;
wenzelm@15036
   198
val simpset_ref_of_sg = #1 o GlobalSimpset.get_sg;
wenzelm@15036
   199
val simpset_ref_of = #1 o GlobalSimpset.get;
wenzelm@15036
   200
val get_context_ss = #2 o GlobalSimpset.get o ProofContext.theory_of;
wenzelm@15036
   201
wenzelm@15036
   202
fun map_context_ss f = GlobalSimpset.map (apsnd
wenzelm@15036
   203
  (fn ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} =>
wenzelm@15036
   204
    make_context_ss (f (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers))));
wenzelm@4080
   205
wenzelm@4080
   206
wenzelm@4795
   207
(* access global simpset *)
clasohm@0
   208
wenzelm@4080
   209
val simpset_of_sg = ! o simpset_ref_of_sg;
wenzelm@6391
   210
val simpset_of = simpset_of_sg o Theory.sign_of;
wenzelm@4080
   211
wenzelm@6391
   212
fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   213
fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
clasohm@0
   214
wenzelm@5028
   215
val simpset = simpset_of o Context.the_context;
wenzelm@6391
   216
val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4080
   217
wenzelm@4080
   218
wenzelm@4795
   219
(* change global simpset *)
wenzelm@4080
   220
wenzelm@7177
   221
fun change_simpset_of f x thy =
wenzelm@7177
   222
  let val r = simpset_ref_of thy
wenzelm@7177
   223
  in r := f (! r, x); thy end;
wenzelm@7177
   224
wenzelm@7177
   225
fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
wenzelm@4080
   226
wenzelm@4080
   227
val Addsimps = change_simpset (op addsimps);
wenzelm@4080
   228
val Delsimps = change_simpset (op delsimps);
wenzelm@4080
   229
val Addsimprocs = change_simpset (op addsimprocs);
wenzelm@4080
   230
val Delsimprocs = change_simpset (op delsimprocs);
wenzelm@9715
   231
val Addcongs = change_simpset (op addcongs);
wenzelm@9715
   232
val Delcongs = change_simpset (op delcongs);
wenzelm@2509
   233
clasohm@0
   234
wenzelm@15036
   235
(* change context dependent components *)
wenzelm@15036
   236
wenzelm@15036
   237
fun add_context_simprocs procs =
wenzelm@15036
   238
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   239
    (merge_context_simprocs procs simprocs, subgoal_tac, loop_tacs,
wenzelm@15036
   240
      unsafe_solvers, solvers));
wenzelm@15036
   241
wenzelm@15036
   242
fun del_context_simprocs procs =
wenzelm@15036
   243
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   244
    (gen_rems eq_context_simproc (simprocs, procs), subgoal_tac, loop_tacs,
wenzelm@15036
   245
      unsafe_solvers, solvers));
wenzelm@15036
   246
wenzelm@15036
   247
fun set_context_subgoaler tac =
wenzelm@15036
   248
  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   249
    (simprocs, Some tac, loop_tacs, unsafe_solvers, solvers));
wenzelm@15036
   250
wenzelm@15036
   251
val reset_context_subgoaler =
wenzelm@15036
   252
  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   253
    (simprocs, None, loop_tacs, unsafe_solvers, solvers));
wenzelm@15036
   254
wenzelm@15036
   255
fun add_context_looper (name, tac) =
wenzelm@15036
   256
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   257
    (simprocs, subgoal_tac, merge_alists [(name, tac)] loop_tacs,
wenzelm@15036
   258
      unsafe_solvers, solvers));
wenzelm@15036
   259
wenzelm@15036
   260
fun del_context_looper name =
wenzelm@15036
   261
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   262
    (simprocs, subgoal_tac, filter_out (equal name o #1) loop_tacs,
wenzelm@15036
   263
      unsafe_solvers, solvers));
wenzelm@15036
   264
wenzelm@15036
   265
fun add_context_unsafe_solver solver =
wenzelm@15036
   266
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   267
    (simprocs, subgoal_tac, loop_tacs,
wenzelm@15036
   268
      merge_context_solvers [solver] unsafe_solvers, solvers));
wenzelm@15036
   269
wenzelm@15036
   270
fun add_context_safe_solver solver =
wenzelm@15036
   271
  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
wenzelm@15036
   272
    (simprocs, subgoal_tac, loop_tacs, unsafe_solvers,
wenzelm@15036
   273
      merge_context_solvers [solver] solvers));
wenzelm@15036
   274
wenzelm@15036
   275
wenzelm@5842
   276
(* proof data kind 'Provers/simpset' *)
wenzelm@4795
   277
wenzelm@5842
   278
structure LocalSimpsetArgs =
wenzelm@5842
   279
struct
wenzelm@5842
   280
  val name = "Provers/simpset";
wenzelm@5842
   281
  type T = simpset;
wenzelm@5842
   282
  val init = simpset_of;
wenzelm@15036
   283
  fun print ctxt ss = print_ss (context_ss ctxt ss (get_context_ss ctxt));
wenzelm@5842
   284
end;
wenzelm@4795
   285
wenzelm@5842
   286
structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
wenzelm@5842
   287
val print_local_simpset = LocalSimpset.print;
wenzelm@5842
   288
val get_local_simpset = LocalSimpset.get;
wenzelm@5842
   289
val put_local_simpset = LocalSimpset.put;
wenzelm@7273
   290
fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
wenzelm@4795
   291
wenzelm@15036
   292
fun local_simpset_of ctxt =
wenzelm@15036
   293
  context_ss ctxt (get_local_simpset ctxt) (get_context_ss ctxt);
wenzelm@15036
   294
wenzelm@4795
   295
wenzelm@5886
   296
(* attributes *)
wenzelm@5886
   297
wenzelm@5886
   298
fun change_global_ss f (thy, th) =
wenzelm@5886
   299
  let val r = simpset_ref_of thy
wenzelm@6096
   300
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5886
   301
wenzelm@5886
   302
fun change_local_ss f (ctxt, th) =
wenzelm@6096
   303
  let val ss = f (get_local_simpset ctxt, [th])
wenzelm@5886
   304
  in (put_local_simpset ss ctxt, th) end;
wenzelm@5886
   305
wenzelm@5886
   306
val simp_add_global = change_global_ss (op addsimps);
wenzelm@5886
   307
val simp_del_global = change_global_ss (op delsimps);
wenzelm@5886
   308
val simp_add_local = change_local_ss (op addsimps);
wenzelm@5886
   309
val simp_del_local = change_local_ss (op delsimps);
wenzelm@5886
   310
wenzelm@9715
   311
val cong_add_global = change_global_ss (op addcongs);
wenzelm@9715
   312
val cong_del_global = change_global_ss (op delcongs);
wenzelm@9715
   313
val cong_add_local = change_local_ss (op addcongs);
wenzelm@9715
   314
val cong_del_local = change_local_ss (op delcongs);
wenzelm@9715
   315
wenzelm@5886
   316
wenzelm@10004
   317
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@10004
   318
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@10004
   319
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@10004
   320
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@10004
   321
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@10004
   322
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
clasohm@0
   323
oheimb@2629
   324
skalberg@15006
   325
wenzelm@4795
   326
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4080
   327
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@4080
   328
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@4080
   329
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
nipkow@4722
   330
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@4080
   331
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
nipkow@406
   332
wenzelm@5082
   333
val          simplify = simp_thm (false, false, false);
wenzelm@5082
   334
val      asm_simplify = simp_thm (false, true, false);
wenzelm@5082
   335
val     full_simplify = simp_thm (true, false, false);
schirmer@14814
   336
val   asm_lr_simplify = simp_thm (true, true, false);
schirmer@14814
   337
val asm_full_simplify = simp_thm (true, true, true);
wenzelm@5082
   338
wenzelm@5082
   339
val          rewrite = simp_cterm (false, false, false);
wenzelm@5082
   340
val      asm_rewrite = simp_cterm (false, true, false);
wenzelm@5082
   341
val     full_rewrite = simp_cterm (true, false, false);
schirmer@14814
   342
val   asm_lr_rewrite = simp_cterm (true, true, false);
schirmer@14814
   343
val asm_full_rewrite = simp_cterm (true, true, true);
wenzelm@3557
   344
wenzelm@3557
   345
wenzelm@4795
   346
wenzelm@5886
   347
(** concrete syntax of attributes **)
wenzelm@5842
   348
wenzelm@5886
   349
(* add / del *)
wenzelm@5842
   350
wenzelm@5928
   351
val simpN = "simp";
wenzelm@9715
   352
val congN = "cong";
berghofe@10412
   353
val addN = "add";
berghofe@10412
   354
val delN = "del";
wenzelm@6911
   355
val onlyN = "only";
wenzelm@9899
   356
val no_asmN = "no_asm";
wenzelm@9899
   357
val no_asm_useN = "no_asm_use";
wenzelm@9899
   358
val no_asm_simpN = "no_asm_simp";
berghofe@13605
   359
val asm_lrN = "asm_lr";
wenzelm@5842
   360
wenzelm@8634
   361
val simp_attr =
wenzelm@8634
   362
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@8634
   363
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@5842
   364
wenzelm@9715
   365
val cong_attr =
wenzelm@9715
   366
 (Attrib.add_del_args cong_add_global cong_del_global,
wenzelm@9715
   367
  Attrib.add_del_args cong_add_local cong_del_local);
wenzelm@9715
   368
wenzelm@5842
   369
wenzelm@5842
   370
(* conversions *)
wenzelm@5842
   371
wenzelm@9899
   372
local
wenzelm@9899
   373
wenzelm@9899
   374
fun conv_mode x =
wenzelm@9899
   375
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@9899
   376
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@9899
   377
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@9899
   378
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@9899
   379
wenzelm@11938
   380
fun simplified_att get args =
wenzelm@11938
   381
  Attrib.syntax (conv_mode -- args >> (fn (f, ths) =>
wenzelm@11938
   382
    Drule.rule_attribute (fn x => f ((if null ths then I else clear_ss) (get x) addsimps ths))));
wenzelm@9899
   383
wenzelm@9899
   384
in
wenzelm@9899
   385
wenzelm@9899
   386
val simplified_attr =
wenzelm@11938
   387
 (simplified_att simpset_of Attrib.global_thmss,
wenzelm@15036
   388
  simplified_att local_simpset_of Attrib.local_thmss);
wenzelm@9899
   389
wenzelm@9899
   390
end;
wenzelm@5842
   391
wenzelm@5842
   392
wenzelm@5842
   393
(* setup_attrs *)
wenzelm@5842
   394
wenzelm@5842
   395
val setup_attrs = Attrib.add_attributes
wenzelm@9899
   396
 [(simpN, simp_attr, "declaration of simplification rule"),
wenzelm@9899
   397
  (congN, cong_attr, "declaration of Simplifier congruence rule"),
wenzelm@9899
   398
  ("simplified", simplified_attr, "simplified rule")];
wenzelm@5842
   399
wenzelm@5842
   400
wenzelm@5842
   401
wenzelm@5842
   402
(** proof methods **)
wenzelm@5842
   403
wenzelm@5842
   404
(* simplification *)
wenzelm@5842
   405
wenzelm@8700
   406
val simp_options =
wenzelm@9899
   407
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@9899
   408
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@9899
   409
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
berghofe@13605
   410
  Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
wenzelm@8700
   411
  Scan.succeed asm_full_simp_tac);
wenzelm@8700
   412
wenzelm@9715
   413
val cong_modifiers =
wenzelm@9720
   414
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local):Method.modifier),
wenzelm@10034
   415
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add_local),
wenzelm@10034
   416
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del_local)];
wenzelm@9715
   417
wenzelm@5928
   418
val simp_modifiers =
wenzelm@8815
   419
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   420
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   421
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   422
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   423
   @ cong_modifiers;
wenzelm@5928
   424
wenzelm@5928
   425
val simp_modifiers' =
wenzelm@10034
   426
 [Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@10034
   427
  Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@9861
   428
  Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
wenzelm@9861
   429
   @ cong_modifiers;
wenzelm@5928
   430
wenzelm@8700
   431
fun simp_args more_mods =
wenzelm@8700
   432
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options) (more_mods @ simp_modifiers');
wenzelm@8700
   433
wenzelm@8170
   434
wenzelm@8700
   435
fun simp_method (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@8700
   436
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@15036
   437
    (CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
wenzelm@8700
   438
wenzelm@8700
   439
fun simp_method' (prems, tac) ctxt = Method.METHOD (fn facts =>
wenzelm@10821
   440
  HEADGOAL (Method.insert_tac (prems @ facts) THEN'
wenzelm@15036
   441
      (CHANGED_PROP oo tac) (local_simpset_of ctxt)));
wenzelm@9715
   442
wenzelm@5842
   443
wenzelm@5842
   444
(* setup_methods *)
wenzelm@5842
   445
wenzelm@8467
   446
fun setup_methods more_mods = Method.add_methods
wenzelm@8700
   447
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@8700
   448
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@5842
   449
wenzelm@5842
   450
wenzelm@5842
   451
wenzelm@4795
   452
(** theory setup **)
wenzelm@4795
   453
wenzelm@8467
   454
val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs];
wenzelm@8467
   455
fun method_setup mods = [setup_methods mods];
wenzelm@4795
   456
wenzelm@8228
   457
fun easy_setup reflect trivs =
wenzelm@8228
   458
  let
wenzelm@8228
   459
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@8228
   460
wenzelm@8228
   461
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@8228
   462
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@8228
   463
wenzelm@8228
   464
    (*no premature instantiation of variables during simplification*)
wenzelm@8228
   465
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@8228
   466
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@8228
   467
wenzelm@8243
   468
    fun mk_eq thm =
wenzelm@8228
   469
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@8228
   470
      else [thm RS reflect] handle THM _ => [];
wenzelm@8228
   471
wenzelm@8243
   472
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@8243
   473
wenzelm@8228
   474
    fun init_ss thy =
wenzelm@8228
   475
      (simpset_ref_of thy :=
wenzelm@8228
   476
        empty_ss setsubgoaler asm_simp_tac
wenzelm@8228
   477
        setSSolver safe_solver
wenzelm@8228
   478
        setSolver unsafe_solver
wenzelm@8228
   479
        setmksimps mksimps; thy);
wenzelm@8467
   480
  in setup @ method_setup [] @ [init_ss] end;
wenzelm@8228
   481
wenzelm@4795
   482
wenzelm@8667
   483
wenzelm@8667
   484
(** outer syntax **)
wenzelm@8667
   485
wenzelm@8667
   486
val print_simpsetP =
wenzelm@8667
   487
  OuterSyntax.improper_command "print_simpset" "print context of Simplifier"
wenzelm@8667
   488
    OuterSyntax.Keyword.diag
wenzelm@9513
   489
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o (Toplevel.keep
wenzelm@9010
   490
      (Toplevel.node_case print_simpset (print_local_simpset o Proof.context_of)))));
wenzelm@8667
   491
wenzelm@8667
   492
val _ = OuterSyntax.add_parsers [print_simpsetP];
wenzelm@8667
   493
clasohm@1243
   494
end;
wenzelm@4795
   495
wenzelm@4795
   496
wenzelm@4795
   497
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@4795
   498
open BasicSimplifier;