src/Pure/simplifier.ML
author wenzelm
Sun Jan 28 23:29:15 2007 +0100 (2007-01-28)
changeset 22201 6fe46a7259ec
parent 22132 0f26cd597193
child 22204 33da3a55c00e
permissions -rw-r--r--
added def_simproc(_i) -- define named simprocs;
tuned;
     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 def_simproc: string -> string list -> (morphism -> simpset -> cterm -> thm option) ->
    73     local_theory -> local_theory
    74   val def_simproc_i: string -> term list -> (morphism -> simpset -> cterm -> thm option) ->
    75     local_theory -> local_theory
    76   val cong_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    77   val simp_modifiers': (Args.T list -> (Method.modifier * Args.T list)) list
    78   val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    79   val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
    80     -> theory -> theory
    81   val easy_setup: thm -> thm list -> theory -> theory
    82 end;
    83 
    84 structure Simplifier: SIMPLIFIER =
    85 struct
    86 
    87 open MetaSimplifier;
    88 
    89 
    90 (** simpset data **)
    91 
    92 (* global simpsets *)
    93 
    94 structure GlobalSimpset = TheoryDataFun
    95 (struct
    96   val name = "Pure/simpset";
    97   type T = simpset ref;
    98 
    99   val empty = ref empty_ss;
   100   fun copy (ref ss) = ref ss: T;            (*create new reference!*)
   101   fun extend (ref ss) = ref (MetaSimplifier.inherit_context empty_ss ss);
   102   fun merge _ (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
   103   fun print _ (ref ss) = print_ss ss;
   104 end);
   105 
   106 val _ = Context.add_setup GlobalSimpset.init;
   107 val print_simpset = GlobalSimpset.print;
   108 val get_simpset = ! o GlobalSimpset.get;
   109 
   110 val change_simpset_of = change o GlobalSimpset.get;
   111 fun change_simpset f = change_simpset_of (ML_Context.the_context ()) f;
   112 
   113 fun simpset_of thy = MetaSimplifier.context (ProofContext.init thy) (get_simpset thy);
   114 val simpset = simpset_of o ML_Context.the_context;
   115 
   116 
   117 fun SIMPSET tacf st = tacf (simpset_of (Thm.theory_of_thm st)) st;
   118 fun SIMPSET' tacf i st = tacf (simpset_of (Thm.theory_of_thm st)) i st;
   119 
   120 fun Addsimps args = change_simpset (fn ss => ss addsimps args);
   121 fun Delsimps args = change_simpset (fn ss => ss delsimps args);
   122 fun Addsimprocs args = change_simpset (fn ss => ss addsimprocs args);
   123 fun Delsimprocs args = change_simpset (fn ss => ss delsimprocs args);
   124 fun Addcongs args = change_simpset (fn ss => ss addcongs args);
   125 fun Delcongs args = change_simpset (fn ss => ss delcongs args);
   126 
   127 
   128 (* local simpsets *)
   129 
   130 structure LocalSimpset = ProofDataFun
   131 (struct
   132   val name = "Pure/simpset";
   133   type T = simpset;
   134   val init = get_simpset;
   135   fun print _ ss = print_ss ss;
   136 end);
   137 
   138 val _ = Context.add_setup LocalSimpset.init;
   139 val print_local_simpset = LocalSimpset.print;
   140 val get_local_simpset = LocalSimpset.get;
   141 val put_local_simpset = LocalSimpset.put;
   142 
   143 fun local_simpset_of ctxt = MetaSimplifier.context ctxt (get_local_simpset ctxt);
   144 
   145 val _ = ML_Context.value_antiq "simpset"
   146   (Scan.succeed ("simpset", "Simplifier.local_simpset_of (ML_Context.the_local_context ())"));
   147 
   148 
   149 (* generic simpsets *)
   150 
   151 fun get_ss (Context.Theory thy) = simpset_of thy
   152   | get_ss (Context.Proof ctxt) = local_simpset_of ctxt;
   153 
   154 fun map_ss f (Context.Theory thy) = (change_simpset_of thy f; Context.Theory thy)
   155   | map_ss f (Context.Proof ctxt) = Context.Proof (LocalSimpset.map f ctxt);
   156 
   157 
   158 (* attributes *)
   159 
   160 fun attrib f = Thm.declaration_attribute (fn th => map_ss (fn ss => f (ss, [th])));
   161 
   162 val simp_add = attrib (op addsimps);
   163 val simp_del = attrib (op delsimps);
   164 val cong_add = attrib (op addcongs);
   165 val cong_del = attrib (op delcongs);
   166 
   167 
   168 
   169 (** named simprocs **)
   170 
   171 fun err_dup_simprocs ds =
   172   error ("Duplicate simproc(s): " ^ commas_quote ds);
   173 
   174 structure Simprocs = GenericDataFun
   175 (
   176   val name = "Pure/simprocs";
   177   type T = (simproc * stamp) NameSpace.table;
   178   val empty = NameSpace.empty_table;
   179   val extend = I;
   180   fun merge _ simprocs = NameSpace.merge_tables (eq_snd (op =)) simprocs
   181     handle Symtab.DUPS ds => err_dup_simprocs ds;
   182   fun print _ _ = ();
   183 );
   184 
   185 val _ = Context.add_setup Simprocs.init;
   186 
   187 local
   188 
   189 (* FIXME *)
   190 fun read_terms ctxt ts =
   191   #1 (ProofContext.read_termTs ctxt (K false) (K NONE) (K NONE) []
   192     (map (rpair TypeInfer.logicT) ts));
   193 
   194 (* FIXME *)
   195 fun cert_terms ctxt ts = map (ProofContext.cert_term ctxt) ts;
   196 
   197 fun prep_pats prep ctxt raw_pats =
   198   let
   199     val pats = prep ctxt raw_pats;
   200     val ctxt' = ctxt
   201       |> fold Variable.declare_term pats
   202       |> fold Variable.auto_fixes pats;
   203   in Variable.export_terms ctxt' ctxt pats end;
   204 
   205 fun gen_simproc prep name raw_pats proc lthy =
   206   let
   207     val pats =
   208       prep_pats prep lthy raw_pats
   209       |> map (Morphism.term (LocalTheory.target_morphism lthy));
   210     val naming = LocalTheory.target_naming lthy;
   211   in
   212     lthy |> LocalTheory.declaration (fn phi => fn context =>
   213       let
   214         val cert = Thm.cterm_of (Context.theory_of context);
   215         val pats' = map (cert o Morphism.term phi) pats;
   216         val name' = Morphism.name phi name;
   217         val proc' = proc phi;
   218         val simproc = mk_simproc' (NameSpace.full naming name') pats' proc';
   219       in
   220         context
   221         |> Simprocs.map (fn simprocs =>
   222             NameSpace.extend_table naming (simprocs, [(name', (simproc, stamp ()))])
   223               handle Symtab.DUPS ds => err_dup_simprocs ds)
   224         |> map_ss (fn ss => ss addsimprocs [simproc])
   225       end)
   226   end;
   227 
   228 in
   229 
   230 val def_simproc = gen_simproc read_terms;
   231 val def_simproc_i = gen_simproc cert_terms;
   232 
   233 end;
   234 
   235 
   236 
   237 (** simplification tactics and rules **)
   238 
   239 fun solve_all_tac solvers ss =
   240   let
   241     val (_, {subgoal_tac, ...}) = MetaSimplifier.rep_ss ss;
   242     val solve_tac = subgoal_tac (MetaSimplifier.set_solvers solvers ss) THEN_ALL_NEW (K no_tac);
   243   in DEPTH_SOLVE (solve_tac 1) end;
   244 
   245 (*NOTE: may instantiate unknowns that appear also in other subgoals*)
   246 fun generic_simp_tac safe mode ss =
   247   let
   248     val (_, {loop_tacs, solvers = (unsafe_solvers, solvers), ...}) = MetaSimplifier.rep_ss ss;
   249     val loop_tac = FIRST' (map (fn (_, tac) => tac ss) (rev loop_tacs));
   250     val solve_tac = FIRST' (map (MetaSimplifier.solver ss)
   251       (if safe then solvers else unsafe_solvers));
   252 
   253     fun simp_loop_tac i =
   254       Goal.asm_rewrite_goal_tac mode (solve_all_tac unsafe_solvers) ss i THEN
   255       (solve_tac i ORELSE TRY ((loop_tac THEN_ALL_NEW simp_loop_tac) i));
   256   in simp_loop_tac end;
   257 
   258 local
   259 
   260 fun simp rew mode ss thm =
   261   let
   262     val (_, {solvers = (unsafe_solvers, _), ...}) = MetaSimplifier.rep_ss ss;
   263     val tacf = solve_all_tac unsafe_solvers;
   264     fun prover s th = Option.map #1 (Seq.pull (tacf s th));
   265   in rew mode prover ss thm end;
   266 
   267 in
   268 
   269 val simp_thm = simp MetaSimplifier.rewrite_thm;
   270 val simp_cterm = simp MetaSimplifier.rewrite_cterm;
   271 
   272 end;
   273 
   274 
   275 (* tactics *)
   276 
   277 val simp_tac = generic_simp_tac false (false, false, false);
   278 val asm_simp_tac = generic_simp_tac false (false, true, false);
   279 val full_simp_tac = generic_simp_tac false (true, false, false);
   280 val asm_lr_simp_tac = generic_simp_tac false (true, true, false);
   281 val asm_full_simp_tac = generic_simp_tac false (true, true, true);
   282 val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
   283 
   284 (*the abstraction over the proof state delays the dereferencing*)
   285 fun          Simp_tac i st =          simp_tac (simpset ()) i st;
   286 fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
   287 fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
   288 fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
   289 fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
   290 
   291 
   292 (* conversions *)
   293 
   294 val          simplify = simp_thm (false, false, false);
   295 val      asm_simplify = simp_thm (false, true, false);
   296 val     full_simplify = simp_thm (true, false, false);
   297 val   asm_lr_simplify = simp_thm (true, true, false);
   298 val asm_full_simplify = simp_thm (true, true, true);
   299 
   300 val          rewrite = simp_cterm (false, false, false);
   301 val      asm_rewrite = simp_cterm (false, true, false);
   302 val     full_rewrite = simp_cterm (true, false, false);
   303 val   asm_lr_rewrite = simp_cterm (true, true, false);
   304 val asm_full_rewrite = simp_cterm (true, true, true);
   305 
   306 
   307 
   308 (** concrete syntax of attributes **)
   309 
   310 (* add / del *)
   311 
   312 val simpN = "simp";
   313 val congN = "cong";
   314 val addN = "add";
   315 val delN = "del";
   316 val onlyN = "only";
   317 val no_asmN = "no_asm";
   318 val no_asm_useN = "no_asm_use";
   319 val no_asm_simpN = "no_asm_simp";
   320 val asm_lrN = "asm_lr";
   321 
   322 
   323 (* conversions *)
   324 
   325 local
   326 
   327 fun conv_mode x =
   328   ((Args.parens (Args.$$$ no_asmN) >> K simplify ||
   329     Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
   330     Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
   331     Scan.succeed asm_full_simplify) |> Scan.lift) x;
   332 
   333 in
   334 
   335 val simplified =
   336   Attrib.syntax (conv_mode -- Attrib.thms >> (fn (f, ths) => Thm.rule_attribute (fn x =>
   337     f ((if null ths then I else MetaSimplifier.clear_ss) (get_ss x) addsimps ths))));
   338 
   339 end;
   340 
   341 
   342 (* setup attributes *)
   343 
   344 val _ = Context.add_setup
   345  (Attrib.add_attributes
   346    [(simpN, Attrib.add_del_args simp_add simp_del, "declaration of Simplifier rule"),
   347     (congN, Attrib.add_del_args cong_add cong_del, "declaration of Simplifier congruence rule"),
   348     ("simplified", simplified, "simplified rule")]);
   349 
   350 
   351 
   352 (** proof methods **)
   353 
   354 (* simplification *)
   355 
   356 val simp_options =
   357  (Args.parens (Args.$$$ no_asmN) >> K simp_tac ||
   358   Args.parens (Args.$$$ no_asm_simpN) >> K asm_simp_tac ||
   359   Args.parens (Args.$$$ no_asm_useN) >> K full_simp_tac ||
   360   Args.parens (Args.$$$ asm_lrN) >> K asm_lr_simp_tac ||
   361   Scan.succeed asm_full_simp_tac);
   362 
   363 fun simp_flags x = (Scan.repeat
   364   (Args.parens (Args.$$$ "depth_limit" -- Args.colon |-- Args.nat)
   365     >> setmp MetaSimplifier.simp_depth_limit)
   366   >> (curry (Library.foldl op o) I o rev)) x;
   367 
   368 val cong_modifiers =
   369  [Args.$$$ congN -- Args.colon >> K ((I, cong_add): Method.modifier),
   370   Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add),
   371   Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del)];
   372 
   373 val simp_modifiers =
   374  [Args.$$$ simpN -- Args.colon >> K (I, simp_add),
   375   Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add),
   376   Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del),
   377   Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon
   378     >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add)]
   379    @ cong_modifiers;
   380 
   381 val simp_modifiers' =
   382  [Args.add -- Args.colon >> K (I, simp_add),
   383   Args.del -- Args.colon >> K (I, simp_del),
   384   Args.$$$ onlyN -- Args.colon
   385     >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add)]
   386    @ cong_modifiers;
   387 
   388 fun simp_args more_mods =
   389   Method.sectioned_args (Args.bang_facts -- Scan.lift simp_options -- Scan.lift simp_flags)
   390     (more_mods @ simp_modifiers');
   391 
   392 fun simp_method ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
   393   ALLGOALS (Method.insert_tac (prems @ facts)) THEN
   394     (FLAGS o CHANGED_PROP o ALLGOALS o tac) (local_simpset_of ctxt));
   395 
   396 fun simp_method' ((prems, tac), FLAGS) ctxt = Method.METHOD (fn facts =>
   397   HEADGOAL (Method.insert_tac (prems @ facts) THEN'
   398       ((FLAGS o CHANGED_PROP) oo tac) (local_simpset_of ctxt)));
   399 
   400 
   401 
   402 (** setup **)
   403 
   404 fun method_setup more_mods = Method.add_methods
   405  [(simpN, simp_args more_mods simp_method', "simplification"),
   406   ("simp_all", simp_args more_mods simp_method, "simplification (all goals)")];
   407 
   408 fun easy_setup reflect trivs = method_setup [] #> (fn thy =>
   409   let
   410     val trivialities = Drule.reflexive_thm :: trivs;
   411 
   412     fun unsafe_solver_tac prems = FIRST' [resolve_tac (trivialities @ prems), assume_tac];
   413     val unsafe_solver = mk_solver "easy unsafe" unsafe_solver_tac;
   414 
   415     (*no premature instantiation of variables during simplification*)
   416     fun safe_solver_tac prems = FIRST' [match_tac (trivialities @ prems), eq_assume_tac];
   417     val safe_solver = mk_solver "easy safe" safe_solver_tac;
   418 
   419     fun mk_eq thm =
   420       if can Logic.dest_equals (Thm.concl_of thm) then [thm]
   421       else [thm RS reflect] handle THM _ => [];
   422 
   423     fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
   424   in
   425     GlobalSimpset.get thy :=
   426       empty_ss setsubgoaler asm_simp_tac
   427       setSSolver safe_solver
   428       setSolver unsafe_solver
   429       setmksimps mksimps;
   430     thy
   431   end);
   432 
   433 end;
   434 
   435 structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
   436 open BasicSimplifier;