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