src/Pure/simplifier.ML
author wenzelm
Mon Oct 17 23:10:21 2005 +0200 (2005-10-17)
changeset 17883 efa1bc2bdcc6
parent 17723 ee5b42e3cbb4
child 17898 ff78ecd1e768
permissions -rw-r--r--
removed obsolete/experimental context components (superceded by Simplifier.the_context);
more abstract change_simpset(_of);
tuned;
wenzelm@16014
     1
(*  Title:      Pure/simplifier.ML
wenzelm@16014
     2
    ID:         $Id$
wenzelm@16014
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@16014
     4
wenzelm@16014
     5
Generic simplifier, suitable for most logics (see also
wenzelm@16014
     6
meta_simplifier.ML for the actual meta-level rewriting engine).
wenzelm@16014
     7
*)
wenzelm@16014
     8
wenzelm@16014
     9
signature BASIC_SIMPLIFIER =
wenzelm@16014
    10
sig
wenzelm@16014
    11
  include BASIC_META_SIMPLIFIER
wenzelm@16014
    12
  val print_simpset: theory -> unit
wenzelm@17883
    13
  val change_simpset_of: theory -> (simpset -> simpset) -> unit
wenzelm@17883
    14
  val change_simpset: (simpset -> simpset) -> unit
wenzelm@16014
    15
  val simpset_of: theory -> simpset
wenzelm@17883
    16
  val simpset: unit -> simpset
wenzelm@16014
    17
  val SIMPSET: (simpset -> tactic) -> tactic
wenzelm@16014
    18
  val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
wenzelm@16014
    19
  val Addsimps: thm list -> unit
wenzelm@16014
    20
  val Delsimps: thm list -> unit
wenzelm@16014
    21
  val Addsimprocs: simproc list -> unit
wenzelm@16014
    22
  val Delsimprocs: simproc list -> unit
wenzelm@16014
    23
  val Addcongs: thm list -> unit
wenzelm@16014
    24
  val Delcongs: thm list -> unit
wenzelm@16014
    25
  val local_simpset_of: Proof.context -> simpset
wenzelm@16014
    26
  val safe_asm_full_simp_tac: simpset -> int -> tactic
wenzelm@16014
    27
  val               simp_tac: simpset -> int -> tactic
wenzelm@16014
    28
  val           asm_simp_tac: simpset -> int -> tactic
wenzelm@16014
    29
  val          full_simp_tac: simpset -> int -> tactic
wenzelm@16014
    30
  val        asm_lr_simp_tac: simpset -> int -> tactic
wenzelm@16014
    31
  val      asm_full_simp_tac: simpset -> int -> tactic
wenzelm@16014
    32
  val               Simp_tac:            int -> tactic
wenzelm@16014
    33
  val           Asm_simp_tac:            int -> tactic
wenzelm@16014
    34
  val          Full_simp_tac:            int -> tactic
wenzelm@16014
    35
  val        Asm_lr_simp_tac:            int -> tactic
wenzelm@16014
    36
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@16014
    37
  val          simplify: simpset -> thm -> thm
wenzelm@16014
    38
  val      asm_simplify: simpset -> thm -> thm
wenzelm@16014
    39
  val     full_simplify: simpset -> thm -> thm
wenzelm@16014
    40
  val   asm_lr_simplify: simpset -> thm -> thm
wenzelm@16014
    41
  val asm_full_simplify: simpset -> thm -> thm
wenzelm@16014
    42
end;
wenzelm@16014
    43
wenzelm@16014
    44
signature SIMPLIFIER =
wenzelm@16014
    45
sig
wenzelm@16014
    46
  include BASIC_SIMPLIFIER
wenzelm@17004
    47
  val clear_ss: simpset -> simpset
wenzelm@17723
    48
  val debug_bounds: bool ref
wenzelm@17883
    49
  val inherit_context: simpset -> simpset -> simpset
wenzelm@17883
    50
  val the_context: simpset -> Context.proof
wenzelm@17883
    51
  val set_context: Context.proof -> simpset -> simpset
wenzelm@16458
    52
  val simproc_i: theory -> string -> term list
wenzelm@16458
    53
    -> (theory -> simpset -> term -> thm option) -> simproc
wenzelm@16458
    54
  val simproc: theory -> string -> string list
wenzelm@16458
    55
    -> (theory -> simpset -> term -> thm option) -> simproc
wenzelm@16014
    56
  val          rewrite: simpset -> cterm -> thm
wenzelm@16014
    57
  val      asm_rewrite: simpset -> cterm -> thm
wenzelm@16014
    58
  val     full_rewrite: simpset -> cterm -> thm
wenzelm@16014
    59
  val   asm_lr_rewrite: simpset -> cterm -> thm
wenzelm@16014
    60
  val asm_full_rewrite: simpset -> cterm -> thm
wenzelm@17883
    61
  val get_simpset: theory -> simpset
wenzelm@16014
    62
  val print_local_simpset: Proof.context -> unit
wenzelm@16014
    63
  val get_local_simpset: Proof.context -> simpset
wenzelm@16014
    64
  val put_local_simpset: simpset -> Proof.context -> Proof.context
wenzelm@16014
    65
  val change_global_ss: (simpset * thm list -> simpset) -> theory attribute
wenzelm@16014
    66
  val change_local_ss: (simpset * thm list -> simpset) -> Proof.context attribute
wenzelm@16014
    67
  val simp_add_global: theory attribute
wenzelm@16014
    68
  val simp_del_global: theory attribute
wenzelm@16014
    69
  val simp_add_local: Proof.context attribute
wenzelm@16014
    70
  val simp_del_local: Proof.context attribute
wenzelm@16014
    71
  val cong_add_global: theory attribute
wenzelm@16014
    72
  val cong_del_global: theory attribute
wenzelm@16014
    73
  val cong_add_local: Proof.context attribute
wenzelm@16014
    74
  val cong_del_local: Proof.context attribute
wenzelm@16014
    75
  val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@16014
    76
  val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
wenzelm@16014
    77
    -> (theory -> theory) list
wenzelm@16014
    78
  val easy_setup: thm -> thm list -> (theory -> theory) list
wenzelm@16014
    79
end;
wenzelm@16014
    80
wenzelm@16014
    81
structure Simplifier: SIMPLIFIER =
wenzelm@16014
    82
struct
wenzelm@16014
    83
wenzelm@17883
    84
(** simpset data **)
wenzelm@16014
    85
wenzelm@17883
    86
(* global simpsets *)
wenzelm@16014
    87
wenzelm@16458
    88
structure GlobalSimpset = TheoryDataFun
wenzelm@16458
    89
(struct
wenzelm@16014
    90
  val name = "Pure/simpset";
wenzelm@17883
    91
  type T = simpset ref;
wenzelm@16014
    92
wenzelm@17883
    93
  val empty = ref empty_ss;
wenzelm@17883
    94
  fun copy (ref ss) = ref ss: T;            (*create new reference!*)
wenzelm@16458
    95
  val extend = copy;
wenzelm@17883
    96
  fun merge _ (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
wenzelm@17883
    97
  fun print _ (ref ss) = print_ss ss;
wenzelm@16458
    98
end);
wenzelm@16014
    99
wenzelm@16014
   100
val _ = Context.add_setup [GlobalSimpset.init];
wenzelm@16014
   101
val print_simpset = GlobalSimpset.print;
wenzelm@17883
   102
val get_simpset = ! o GlobalSimpset.get;
wenzelm@16014
   103
wenzelm@17883
   104
val change_simpset_of = change o GlobalSimpset.get;
wenzelm@17883
   105
fun change_simpset f = change_simpset_of (Context.the_context ()) f;
wenzelm@16014
   106
wenzelm@17883
   107
fun simpset_of thy = MetaSimplifier.set_context (Context.init_proof thy) (get_simpset thy);
wenzelm@17883
   108
val simpset = simpset_of o Context.the_context;
wenzelm@16014
   109
wenzelm@16014
   110
wenzelm@17883
   111
fun SIMPSET tacf st = tacf (simpset_of (Thm.theory_of_thm st)) st;
wenzelm@17883
   112
fun SIMPSET' tacf i st = tacf (simpset_of (Thm.theory_of_thm st)) i st;
wenzelm@16014
   113
wenzelm@17883
   114
fun Addsimps args = change_simpset (fn ss => ss addsimps args);
wenzelm@17883
   115
fun Delsimps args = change_simpset (fn ss => ss delsimps args);
wenzelm@17883
   116
fun Addsimprocs args = change_simpset (fn ss => ss addsimprocs args);
wenzelm@17883
   117
fun Delsimprocs args = change_simpset (fn ss => ss delsimprocs args);
wenzelm@17883
   118
fun Addcongs args = change_simpset (fn ss => ss addcongs args);
wenzelm@17883
   119
fun Delcongs args = change_simpset (fn ss => ss delcongs args);
wenzelm@16014
   120
wenzelm@16014
   121
wenzelm@17883
   122
(* local simpsets *)
wenzelm@16014
   123
wenzelm@16458
   124
structure LocalSimpset = ProofDataFun
wenzelm@16458
   125
(struct
wenzelm@16014
   126
  val name = "Pure/simpset";
wenzelm@16014
   127
  type T = simpset;
wenzelm@17883
   128
  val init = get_simpset;
wenzelm@17883
   129
  fun print _ ss = print_ss ss;
wenzelm@16458
   130
end);
wenzelm@16014
   131
wenzelm@16014
   132
val _ = Context.add_setup [LocalSimpset.init];
wenzelm@16014
   133
val print_local_simpset = LocalSimpset.print;
wenzelm@16014
   134
val get_local_simpset = LocalSimpset.get;
wenzelm@16014
   135
val put_local_simpset = LocalSimpset.put;
wenzelm@16014
   136
wenzelm@17883
   137
fun local_simpset_of ctxt = MetaSimplifier.set_context ctxt (get_local_simpset ctxt);
wenzelm@16014
   138
wenzelm@16014
   139
wenzelm@16014
   140
(* attributes *)
wenzelm@16014
   141
wenzelm@17883
   142
fun change_global_ss f (thy, th) = (change_simpset_of thy (fn ss => f (ss, [th])); (thy, th));
wenzelm@17883
   143
fun change_local_ss f (ctxt, th) = (LocalSimpset.map (fn ss => f (ss, [th])) ctxt, th);
wenzelm@16014
   144
wenzelm@16014
   145
val simp_add_global = change_global_ss (op addsimps);
wenzelm@16014
   146
val simp_del_global = change_global_ss (op delsimps);
wenzelm@16806
   147
val simp_add_local = change_local_ss (op addsimps);
wenzelm@16014
   148
val simp_del_local = change_local_ss (op delsimps);
wenzelm@16014
   149
wenzelm@16014
   150
val cong_add_global = change_global_ss (op addcongs);
wenzelm@16014
   151
val cong_del_global = change_global_ss (op delcongs);
wenzelm@16014
   152
val cong_add_local = change_local_ss (op addcongs);
wenzelm@16014
   153
val cong_del_local = change_local_ss (op delcongs);
wenzelm@16014
   154
wenzelm@16014
   155
wenzelm@16806
   156
(* tactics *)
wenzelm@16806
   157
wenzelm@16014
   158
val simp_tac = generic_simp_tac false (false, false, false);
wenzelm@16014
   159
val asm_simp_tac = generic_simp_tac false (false, true, false);
wenzelm@16014
   160
val full_simp_tac = generic_simp_tac false (true, false, false);
wenzelm@16014
   161
val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
wenzelm@16014
   162
val asm_full_simp_tac = generic_simp_tac false (true, true, true);
wenzelm@16014
   163
val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
wenzelm@16014
   164
wenzelm@16014
   165
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@16014
   166
fun          Simp_tac i st =          simp_tac (simpset ()) i st;
wenzelm@16014
   167
fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
wenzelm@16014
   168
fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
wenzelm@16014
   169
fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
wenzelm@16014
   170
fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
wenzelm@16014
   171
wenzelm@16806
   172
wenzelm@16806
   173
(* conversions *)
wenzelm@16806
   174
wenzelm@17883
   175
val          simplify = MetaSimplifier.simp_thm (false, false, false);
wenzelm@17883
   176
val      asm_simplify = MetaSimplifier.simp_thm (false, true, false);
wenzelm@17883
   177
val     full_simplify = MetaSimplifier.simp_thm (true, false, false);
wenzelm@17883
   178
val   asm_lr_simplify = MetaSimplifier.simp_thm (true, true, false);
wenzelm@17883
   179
val asm_full_simplify = MetaSimplifier.simp_thm (true, true, true);
wenzelm@16014
   180
wenzelm@17883
   181
val          rewrite = MetaSimplifier.simp_cterm (false, false, false);
wenzelm@17883
   182
val      asm_rewrite = MetaSimplifier.simp_cterm (false, true, false);
wenzelm@17883
   183
val     full_rewrite = MetaSimplifier.simp_cterm (true, false, false);
wenzelm@17883
   184
val   asm_lr_rewrite = MetaSimplifier.simp_cterm (true, true, false);
wenzelm@17883
   185
val asm_full_rewrite = MetaSimplifier.simp_cterm (true, true, true);
wenzelm@16014
   186
wenzelm@16014
   187
wenzelm@16014
   188
wenzelm@16014
   189
(** concrete syntax of attributes **)
wenzelm@16014
   190
wenzelm@16014
   191
(* add / del *)
wenzelm@16014
   192
wenzelm@16014
   193
val simpN = "simp";
wenzelm@16014
   194
val congN = "cong";
wenzelm@16014
   195
val addN = "add";
wenzelm@16014
   196
val delN = "del";
wenzelm@16014
   197
val onlyN = "only";
wenzelm@16014
   198
val no_asmN = "no_asm";
wenzelm@16014
   199
val no_asm_useN = "no_asm_use";
wenzelm@16014
   200
val no_asm_simpN = "no_asm_simp";
wenzelm@16014
   201
val asm_lrN = "asm_lr";
wenzelm@16014
   202
wenzelm@16014
   203
val simp_attr =
wenzelm@16014
   204
 (Attrib.add_del_args simp_add_global simp_del_global,
wenzelm@16014
   205
  Attrib.add_del_args simp_add_local simp_del_local);
wenzelm@16014
   206
wenzelm@16014
   207
val cong_attr =
wenzelm@16014
   208
 (Attrib.add_del_args cong_add_global cong_del_global,
wenzelm@16014
   209
  Attrib.add_del_args cong_add_local cong_del_local);
wenzelm@16014
   210
wenzelm@16014
   211
wenzelm@16014
   212
(* conversions *)
wenzelm@16014
   213
wenzelm@16014
   214
local
wenzelm@16014
   215
wenzelm@16014
   216
fun conv_mode x =
wenzelm@16014
   217
  ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
wenzelm@16014
   218
    Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
wenzelm@16014
   219
    Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
wenzelm@16014
   220
    Scan.succeed asm_full_simplify) |> Scan.lift) x;
wenzelm@16014
   221
wenzelm@16014
   222
fun simplified_att get args =
wenzelm@16014
   223
  Attrib.syntax (conv_mode -- args >> (fn (f, ths) =>
wenzelm@17883
   224
    Drule.rule_attribute (fn x =>
wenzelm@17883
   225
      f ((if null ths then I else MetaSimplifier.clear_ss) (get x) addsimps ths))));
wenzelm@16014
   226
wenzelm@16014
   227
in
wenzelm@16014
   228
wenzelm@16014
   229
val simplified_attr =
wenzelm@16014
   230
 (simplified_att simpset_of Attrib.global_thmss,
wenzelm@16014
   231
  simplified_att local_simpset_of Attrib.local_thmss);
wenzelm@16014
   232
wenzelm@16014
   233
end;
wenzelm@16014
   234
wenzelm@16014
   235
wenzelm@16014
   236
(* setup attributes *)
wenzelm@16014
   237
wenzelm@16014
   238
val _ = Context.add_setup
wenzelm@16014
   239
 [Attrib.add_attributes
wenzelm@16014
   240
   [(simpN, simp_attr, "declaration of simplification rule"),
wenzelm@16014
   241
    (congN, cong_attr, "declaration of Simplifier congruence rule"),
wenzelm@16014
   242
    ("simplified", simplified_attr, "simplified rule")]];
wenzelm@16014
   243
wenzelm@16014
   244
wenzelm@16014
   245
wenzelm@16014
   246
(** proof methods **)
wenzelm@16014
   247
wenzelm@16014
   248
(* simplification *)
wenzelm@16014
   249
wenzelm@16014
   250
val simp_options =
wenzelm@16014
   251
 (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
wenzelm@16014
   252
  Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
wenzelm@16014
   253
  Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
wenzelm@16014
   254
  Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
wenzelm@16014
   255
  Scan.succeed asm_full_simp_tac);
wenzelm@16014
   256
wenzelm@16685
   257
fun simp_flags x = (Scan.repeat
wenzelm@16684
   258
  (Args.parens (Args.$$$ "depth_limit" -- Args.colon |-- Args.nat)
wenzelm@16684
   259
    >> setmp MetaSimplifier.simp_depth_limit)
wenzelm@16709
   260
  >> (curry (Library.foldl op o) I o rev)) x;
wenzelm@16684
   261
wenzelm@16014
   262
val cong_modifiers =
wenzelm@17883
   263
 [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local): Method.modifier),
wenzelm@16014
   264
  Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add_local),
wenzelm@16014
   265
  Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del_local)];
wenzelm@16014
   266
wenzelm@16014
   267
val simp_modifiers =
wenzelm@16014
   268
 [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
wenzelm@16014
   269
  Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@16014
   270
  Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@17883
   271
  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon
wenzelm@17883
   272
    >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add_local)]
wenzelm@16014
   273
   @ cong_modifiers;
wenzelm@16014
   274
wenzelm@16014
   275
val simp_modifiers' =
wenzelm@16014
   276
 [Args.add -- Args.colon >> K (I, simp_add_local),
wenzelm@16014
   277
  Args.del -- Args.colon >> K (I, simp_del_local),
wenzelm@17883
   278
  Args.$$$ onlyN -- Args.colon >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add_local)]
wenzelm@16014
   279
   @ cong_modifiers;
wenzelm@16014
   280
wenzelm@16014
   281
fun simp_args more_mods =
wenzelm@16684
   282
  Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options -- Scan.lift simp_flags)
wenzelm@16684
   283
    (more_mods @ simp_modifiers');
wenzelm@16014
   284
wenzelm@16684
   285
fun simp_method ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
wenzelm@16014
   286
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN
wenzelm@16684
   287
    (FLAGS o CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
wenzelm@16014
   288
wenzelm@16684
   289
fun simp_method' ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
wenzelm@16014
   290
  HEADGOAL (Method.insert_tac (prems @ facts) THEN'
wenzelm@16684
   291
      ((FLAGS o CHANGED_PROP) oo tac) (local_simpset_of ctxt)));
wenzelm@16014
   292
wenzelm@16014
   293
wenzelm@16014
   294
(* setup methods *)
wenzelm@16014
   295
wenzelm@16014
   296
fun setup_methods more_mods = Method.add_methods
wenzelm@16014
   297
 [(simpN, simp_args more_mods simp_method', "simplification"),
wenzelm@16014
   298
  ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
wenzelm@16014
   299
wenzelm@16014
   300
fun method_setup mods = [setup_methods mods];
wenzelm@16014
   301
wenzelm@16014
   302
wenzelm@16014
   303
(** easy_setup **)
wenzelm@16014
   304
wenzelm@16014
   305
fun easy_setup reflect trivs =
wenzelm@16014
   306
  let
wenzelm@16014
   307
    val trivialities = Drule.reflexive_thm :: trivs;
wenzelm@16014
   308
wenzelm@16014
   309
    fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
wenzelm@16014
   310
    val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
wenzelm@16014
   311
wenzelm@16014
   312
    (*no premature instantiation of variables during simplification*)
wenzelm@16014
   313
    fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
wenzelm@16014
   314
    val safe_solver = mk_solver "easy safe" safe_solver_tac;
wenzelm@16014
   315
wenzelm@16014
   316
    fun mk_eq thm =
wenzelm@16014
   317
      if Logic.is_equals (Thm.concl_of thm) then [thm]
wenzelm@16014
   318
      else [thm RS reflect] handle THM _ => [];
wenzelm@16014
   319
wenzelm@16014
   320
    fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
wenzelm@16014
   321
wenzelm@16014
   322
    fun init_ss thy =
wenzelm@17883
   323
      (GlobalSimpset.get thy :=
wenzelm@16014
   324
        empty_ss setsubgoaler asm_simp_tac
wenzelm@16014
   325
        setSSolver safe_solver
wenzelm@16014
   326
        setSolver unsafe_solver
wenzelm@16014
   327
        setmksimps mksimps; thy);
wenzelm@16014
   328
  in method_setup [] @ [init_ss] end;
wenzelm@16014
   329
wenzelm@17883
   330
wenzelm@17883
   331
open MetaSimplifier;
wenzelm@17883
   332
wenzelm@16014
   333
end;
wenzelm@16014
   334
wenzelm@16014
   335
structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
wenzelm@16014
   336
open BasicSimplifier;