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