src/Pure/simplifier.ML
author wenzelm
Fri Jan 19 22:08:02 2007 +0100 (2007-01-19)
changeset 22095 07875394618e
parent 21708 45e7491bea47
child 22132 0f26cd597193
permissions -rw-r--r--
moved ML context stuff to from Context to ML_Context;
     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 attrib: (simpset * thm list -> simpset) -> attribute
    68   val simp_add: attribute
    69   val simp_del: attribute
    70   val cong_add: attribute
    71   val cong_del: attribute
    72   val cong_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    73   val simp_modifiers': (Args.T list -> (Method.modifier * Args.T list)) list
    74   val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    75   val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
    76     -> theory -> theory
    77   val easy_setup: thm -> thm list -> theory -> theory
    78 end;
    79 
    80 structure Simplifier: SIMPLIFIER =
    81 struct
    82 
    83 open MetaSimplifier;
    84 
    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 (ML_Context.the_context ()) f;
   108 
   109 fun simpset_of thy = MetaSimplifier.context (ProofContext.init thy) (get_simpset thy);
   110 val simpset = simpset_of o ML_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 attrib f = Thm.declaration_attribute (fn th =>
   145    fn Context.Theory thy => (change_simpset_of thy (fn ss => f (ss, [th])); Context.Theory thy)
   146     | Context.Proof ctxt => Context.Proof (LocalSimpset.map (fn ss => f (ss, [th])) ctxt));
   147 
   148 val simp_add = attrib (op addsimps);
   149 val simp_del = attrib (op delsimps);
   150 val cong_add = attrib (op addcongs);
   151 val cong_del = attrib (op delcongs);
   152 
   153 
   154 
   155 (** simplification tactics and rules **)
   156 
   157 fun solve_all_tac solvers ss =
   158   let
   159     val (_, {subgoal_tac, ...}) = MetaSimplifier.rep_ss ss;
   160     val solve_tac = subgoal_tac (MetaSimplifier.set_solvers solvers ss) THEN_ALL_NEW (K no_tac);
   161   in DEPTH_SOLVE (solve_tac 1) end;
   162 
   163 (*NOTE: may instantiate unknowns that appear also in other subgoals*)
   164 fun generic_simp_tac safe mode ss =
   165   let
   166     val (_, {loop_tacs, solvers = (unsafe_solvers, solvers), ...}) = MetaSimplifier.rep_ss ss;
   167     val loop_tac = FIRST' (map (fn (_, tac) => tac ss) (rev loop_tacs));
   168     val solve_tac = FIRST' (map (MetaSimplifier.solver ss)
   169       (if safe then solvers else unsafe_solvers));
   170 
   171     fun simp_loop_tac i =
   172       Goal.asm_rewrite_goal_tac mode (solve_all_tac unsafe_solvers) ss i THEN
   173       (solve_tac i ORELSE TRY ((loop_tac THEN_ALL_NEW simp_loop_tac) i));
   174   in simp_loop_tac end;
   175 
   176 local
   177 
   178 fun simp rew mode ss thm =
   179   let
   180     val (_, {solvers = (unsafe_solvers, _), ...}) = MetaSimplifier.rep_ss ss;
   181     val tacf = solve_all_tac unsafe_solvers;
   182     fun prover s th = Option.map #1 (Seq.pull (tacf s th));
   183   in rew mode prover ss thm end;
   184 
   185 in
   186 
   187 val simp_thm = simp MetaSimplifier.rewrite_thm;
   188 val simp_cterm = simp MetaSimplifier.rewrite_cterm;
   189 
   190 end;
   191 
   192 
   193 (* tactics *)
   194 
   195 val simp_tac = generic_simp_tac false (false, false, false);
   196 val asm_simp_tac = generic_simp_tac false (false, true, false);
   197 val full_simp_tac = generic_simp_tac false (true, false, false);
   198 val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
   199 val asm_full_simp_tac = generic_simp_tac false (true, true, true);
   200 val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
   201 
   202 (*the abstraction over the proof state delays the dereferencing*)
   203 fun          Simp_tac i st =          simp_tac (simpset ()) i st;
   204 fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
   205 fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
   206 fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
   207 fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
   208 
   209 
   210 (* conversions *)
   211 
   212 val          simplify = simp_thm (false, false, false);
   213 val      asm_simplify = simp_thm (false, true, false);
   214 val     full_simplify = simp_thm (true, false, false);
   215 val   asm_lr_simplify = simp_thm (true, true, false);
   216 val asm_full_simplify = simp_thm (true, true, true);
   217 
   218 val          rewrite = simp_cterm (false, false, false);
   219 val      asm_rewrite = simp_cterm (false, true, false);
   220 val     full_rewrite = simp_cterm (true, false, false);
   221 val   asm_lr_rewrite = simp_cterm (true, true, false);
   222 val asm_full_rewrite = simp_cterm (true, true, true);
   223 
   224 
   225 
   226 (** concrete syntax of attributes **)
   227 
   228 (* add / del *)
   229 
   230 val simpN = "simp";
   231 val congN = "cong";
   232 val addN = "add";
   233 val delN = "del";
   234 val onlyN = "only";
   235 val no_asmN = "no_asm";
   236 val no_asm_useN = "no_asm_use";
   237 val no_asm_simpN = "no_asm_simp";
   238 val asm_lrN = "asm_lr";
   239 
   240 
   241 (* conversions *)
   242 
   243 local
   244 
   245 fun conv_mode x =
   246   ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
   247     Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
   248     Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
   249     Scan.succeed asm_full_simplify) |> Scan.lift) x;
   250 
   251 fun get_ss (Context.Theory thy) = simpset_of thy
   252   | get_ss (Context.Proof ctxt) = local_simpset_of ctxt;
   253 
   254 in
   255 
   256 val simplified =
   257   Attrib.syntax (conv_mode -- Attrib.thms >> (fn (f, ths) => Thm.rule_attribute (fn x =>
   258     f ((if null ths then I else MetaSimplifier.clear_ss) (get_ss x) addsimps ths))));
   259 
   260 end;
   261 
   262 
   263 (* setup attributes *)
   264 
   265 val _ = Context.add_setup
   266  (Attrib.add_attributes
   267    [(simpN, Attrib.add_del_args simp_add simp_del, "declaration of Simplifier rule"),
   268     (congN, Attrib.add_del_args cong_add cong_del, "declaration of Simplifier congruence rule"),
   269     ("simplified", simplified, "simplified rule")]);
   270 
   271 
   272 
   273 (** proof methods **)
   274 
   275 (* simplification *)
   276 
   277 val simp_options =
   278  (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
   279   Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
   280   Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
   281   Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
   282   Scan.succeed asm_full_simp_tac);
   283 
   284 fun simp_flags x = (Scan.repeat
   285   (Args.parens (Args.$$$ "depth_limit" -- Args.colon |-- Args.nat)
   286     >> setmp MetaSimplifier.simp_depth_limit)
   287   >> (curry (Library.foldl op o) I o rev)) x;
   288 
   289 val cong_modifiers =
   290  [Args.$$$ congN -- Args.colon >> K ((I, cong_add): Method.modifier),
   291   Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add),
   292   Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del)];
   293 
   294 val simp_modifiers =
   295  [Args.$$$ simpN -- Args.colon >> K (I, simp_add),
   296   Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add),
   297   Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del),
   298   Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon
   299     >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add)]
   300    @ cong_modifiers;
   301 
   302 val simp_modifiers' =
   303  [Args.add -- Args.colon >> K (I, simp_add),
   304   Args.del -- Args.colon >> K (I, simp_del),
   305   Args.$$$ onlyN -- Args.colon
   306     >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add)]
   307    @ cong_modifiers;
   308 
   309 fun simp_args more_mods =
   310   Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options -- Scan.lift simp_flags)
   311     (more_mods @ simp_modifiers');
   312 
   313 fun simp_method ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
   314   ALLGOALS (Method.insert_tac (prems @ facts)) THEN
   315     (FLAGS o CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
   316 
   317 fun simp_method' ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
   318   HEADGOAL (Method.insert_tac (prems @ facts) THEN'
   319       ((FLAGS o CHANGED_PROP) oo tac) (local_simpset_of ctxt)));
   320 
   321 
   322 
   323 (** setup **)
   324 
   325 fun method_setup more_mods = Method.add_methods
   326  [(simpN, simp_args more_mods simp_method', "simplification"),
   327   ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
   328 
   329 fun easy_setup reflect trivs = method_setup [] #> (fn thy =>
   330   let
   331     val trivialities = Drule.reflexive_thm :: trivs;
   332 
   333     fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
   334     val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
   335 
   336     (*no premature instantiation of variables during simplification*)
   337     fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
   338     val safe_solver = mk_solver "easy safe" safe_solver_tac;
   339 
   340     fun mk_eq thm =
   341       if can Logic.dest_equals (Thm.concl_of thm) then [thm]
   342       else [thm RS reflect] handle THM _ => [];
   343 
   344     fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
   345   in
   346     GlobalSimpset.get thy :=
   347       empty_ss setsubgoaler asm_simp_tac
   348       setSSolver safe_solver
   349       setSolver unsafe_solver
   350       setmksimps mksimps;
   351     thy
   352   end);
   353 
   354 end;
   355 
   356 structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
   357 open BasicSimplifier;