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