src/Provers/simplifier.ML
author wenzelm
Wed Nov 18 11:02:42 1998 +0100 (1998-11-18 ago)
changeset 5928 6e00a206a948
parent 5886 0373323180f5
child 6096 3451f9e88528
permissions -rw-r--r--
export simp_modifiers;
     1 (*  Title:      Provers/simplifier.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Generic simplifier, suitable for most logics.  See Pure/thm.ML for the
     6 actual meta-level rewriting engine.
     7 *)
     8 
     9 infix 4
    10   setsubgoaler setloop addloop delloop setSSolver addSSolver setSolver
    11   addSolver addsimps delsimps addeqcongs deleqcongs
    12   setmksimps setmkeqTrue setmksym settermless addsimprocs delsimprocs;
    13 
    14 signature BASIC_SIMPLIFIER =
    15 sig
    16   type simproc
    17   val mk_simproc: string -> cterm list
    18     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
    19   type simpset
    20   val empty_ss: simpset
    21   val rep_ss: simpset ->
    22    {mss: meta_simpset,
    23     subgoal_tac:        simpset -> int -> tactic,
    24     loop_tacs:          (string * (int -> tactic))list,
    25            finish_tac: thm list -> int -> tactic,
    26     unsafe_finish_tac: thm list -> int -> tactic};
    27   val print_ss: simpset -> unit
    28   val print_simpset: theory -> unit
    29   val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
    30   val setloop:      simpset *             (int -> tactic) -> simpset
    31   val addloop:      simpset *  (string * (int -> tactic)) -> simpset
    32   val delloop:      simpset *   string                    -> simpset
    33   val setSSolver:   simpset * (thm list -> int -> tactic) -> simpset
    34   val addSSolver:   simpset * (thm list -> int -> tactic) -> simpset
    35   val setSolver:    simpset * (thm list -> int -> tactic) -> simpset
    36   val addSolver:    simpset * (thm list -> int -> tactic) -> simpset
    37   val setmksimps:   simpset * (thm -> thm list) -> simpset
    38   val setmkeqTrue:  simpset * (thm -> thm option) -> simpset
    39   val setmksym:     simpset * (thm -> thm option) -> simpset
    40   val settermless:  simpset * (term * term -> bool) -> simpset
    41   val addsimps:     simpset * thm list -> simpset
    42   val delsimps:     simpset * thm list -> simpset
    43   val addeqcongs:   simpset * thm list -> simpset
    44   val deleqcongs:   simpset * thm list -> simpset
    45   val addsimprocs:  simpset * simproc list -> simpset
    46   val delsimprocs:  simpset * simproc list -> simpset
    47   val merge_ss:     simpset * simpset -> simpset
    48   val prems_of_ss:  simpset -> thm list
    49   val simpset_ref_of_sg: Sign.sg -> simpset ref
    50   val simpset_ref_of: theory -> simpset ref
    51   val simpset_of_sg: Sign.sg -> simpset
    52   val simpset_of: theory -> simpset
    53   val SIMPSET: (simpset -> tactic) -> tactic
    54   val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
    55   val simpset: unit -> simpset
    56   val simpset_ref: unit -> simpset ref
    57   val Addsimps: thm list -> unit
    58   val Delsimps: thm list -> unit
    59   val Addsimprocs: simproc list -> unit
    60   val Delsimprocs: simproc list -> unit
    61   val               simp_tac: simpset -> int -> tactic
    62   val           asm_simp_tac: simpset -> int -> tactic
    63   val          full_simp_tac: simpset -> int -> tactic
    64   val        asm_lr_simp_tac: simpset -> int -> tactic
    65   val      asm_full_simp_tac: simpset -> int -> tactic
    66   val safe_asm_full_simp_tac: simpset -> int -> tactic
    67   val               Simp_tac:            int -> tactic
    68   val           Asm_simp_tac:            int -> tactic
    69   val          Full_simp_tac:            int -> tactic
    70   val        Asm_lr_simp_tac:            int -> tactic
    71   val      Asm_full_simp_tac:            int -> tactic
    72   val          simplify: simpset -> thm -> thm
    73   val      asm_simplify: simpset -> thm -> thm
    74   val     full_simplify: simpset -> thm -> thm
    75   val asm_full_simplify: simpset -> thm -> thm
    76 end;
    77 
    78 signature SIMPLIFIER =
    79 sig
    80   include BASIC_SIMPLIFIER
    81   val          rewrite: simpset -> cterm -> thm
    82   val      asm_rewrite: simpset -> cterm -> thm
    83   val     full_rewrite: simpset -> cterm -> thm
    84   val asm_full_rewrite: simpset -> cterm -> thm
    85   val print_local_simpset: Proof.context -> unit
    86   val get_local_simpset: Proof.context -> simpset
    87   val put_local_simpset: simpset -> Proof.context -> Proof.context
    88   val simp_add_global: theory attribute
    89   val simp_del_global: theory attribute
    90   val simp_add_local: Proof.context attribute
    91   val simp_del_local: Proof.context attribute
    92   val simp_modifiers: (Args.T list -> (Proof.context attribute * Args.T list)) list
    93   val setup: (theory -> theory) list
    94 end;
    95 
    96 structure Simplifier: SIMPLIFIER =
    97 struct
    98 
    99 
   100 (** simplification procedures **)
   101 
   102 (* datatype simproc *)
   103 
   104 datatype simproc =
   105   Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
   106 
   107 fun mk_simproc name lhss proc =
   108   Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
   109 
   110 fun rep_simproc (Simproc args) = args;
   111 
   112 
   113 
   114 (** simplification sets **)
   115 
   116 (* type simpset *)
   117 
   118 datatype simpset =
   119   Simpset of {
   120     mss: meta_simpset,
   121     subgoal_tac:        simpset -> int -> tactic,
   122     loop_tacs:          (string * (int -> tactic))list,
   123            finish_tac: thm list -> int -> tactic,
   124     unsafe_finish_tac: thm list -> int -> tactic};
   125 
   126 fun make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac) =
   127   Simpset {mss = mss, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
   128     finish_tac = finish_tac, unsafe_finish_tac = unsafe_finish_tac};
   129 
   130 val empty_ss =
   131   let val mss = Thm.set_mk_sym(Thm.empty_mss, Some o symmetric_fun)
   132   in make_ss (mss, K (K no_tac), [], K (K no_tac), K (K no_tac)) end;
   133 
   134 fun rep_ss (Simpset args) = args;
   135 fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
   136 
   137 
   138 (* print simpsets *)
   139 
   140 fun print_ss ss =
   141   let
   142     val Simpset {mss, ...} = ss;
   143     val {simps, procs, congs} = Thm.dest_mss mss;
   144 
   145     val pretty_thms = map Display.pretty_thm;
   146     fun pretty_proc (name, lhss) =
   147       Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
   148   in
   149     Pretty.writeln (Pretty.big_list "simplification rules:" (pretty_thms simps));
   150     Pretty.writeln (Pretty.big_list "simplification procedures:" (map pretty_proc procs));
   151     Pretty.writeln (Pretty.big_list "congruences:" (pretty_thms congs))
   152   end;
   153 
   154 
   155 (* extend simpsets *)
   156 
   157 fun (Simpset {mss, subgoal_tac = _, loop_tacs, finish_tac, unsafe_finish_tac})
   158     setsubgoaler subgoal_tac =
   159   make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   160 
   161 fun (Simpset {mss, subgoal_tac, loop_tacs = _, finish_tac, unsafe_finish_tac})
   162     setloop tac =
   163   make_ss (mss, subgoal_tac, [("",tac)], finish_tac, unsafe_finish_tac);
   164 
   165 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   166     addloop tac = make_ss (mss, subgoal_tac, 
   167         (case assoc_string (loop_tacs,(fst tac)) of None => () | Some x => 
   168          warning ("overwriting looper "^fst tac); overwrite(loop_tacs,tac)),
   169            finish_tac, unsafe_finish_tac);
   170 
   171 fun (ss as Simpset {mss,subgoal_tac,loop_tacs,finish_tac,unsafe_finish_tac})
   172     delloop name =
   173   let val (del,rest) = partition (fn (n,_) => n=name) loop_tacs
   174   in if null del then (warning ("No such looper in simpset: " ^ name); ss)
   175      else make_ss (mss, subgoal_tac, rest, finish_tac, unsafe_finish_tac)
   176   end;
   177 
   178 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac = _, unsafe_finish_tac})
   179     setSSolver finish_tac =
   180   make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   181 
   182 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   183     addSSolver tac =
   184   make_ss (mss, subgoal_tac, loop_tacs, fn hyps => finish_tac hyps ORELSE' tac hyps,
   185     unsafe_finish_tac);
   186 
   187 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac = _})
   188     setSolver unsafe_finish_tac =
   189   make_ss (mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   190 
   191 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   192     addSolver tac =
   193   make_ss (mss, subgoal_tac, loop_tacs, finish_tac,
   194     fn hyps => unsafe_finish_tac hyps ORELSE' tac hyps);
   195 
   196 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   197     setmksimps mk_simps =
   198   make_ss (Thm.set_mk_rews (mss, map (Thm.strip_shyps o Drule.zero_var_indexes) o mk_simps),
   199     subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   200 
   201 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   202     setmkeqTrue mk_eq_True =
   203   make_ss (Thm.set_mk_eq_True (mss, mk_eq_True),
   204     subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   205 
   206 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   207     setmksym mksym =
   208   make_ss (Thm.set_mk_sym (mss, mksym),
   209     subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   210 
   211 fun (Simpset {mss, subgoal_tac, loop_tacs,  finish_tac, unsafe_finish_tac})
   212     settermless termless =
   213   make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tacs,
   214     finish_tac, unsafe_finish_tac);
   215 
   216 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   217     addsimps rews =
   218   make_ss (Thm.add_simps (mss, rews), subgoal_tac, loop_tacs,
   219            finish_tac, unsafe_finish_tac);
   220 
   221 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   222     delsimps rews =
   223   make_ss (Thm.del_simps (mss, rews), subgoal_tac, loop_tacs,
   224            finish_tac, unsafe_finish_tac);
   225 
   226 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   227     addeqcongs newcongs =
   228   make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tacs,
   229     finish_tac, unsafe_finish_tac);
   230 
   231 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   232     deleqcongs oldcongs =
   233   make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tacs,
   234     finish_tac, unsafe_finish_tac);
   235 
   236 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   237     addsimprocs simprocs =
   238   make_ss
   239     (Thm.add_simprocs (mss, map rep_simproc simprocs),
   240       subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   241 
   242 fun (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac})
   243     delsimprocs simprocs =
   244   make_ss
   245     (Thm.del_simprocs (mss, map rep_simproc simprocs),
   246       subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   247 
   248 
   249 (* merge simpsets *)    (*NOTE: ignores tactics of 2nd simpset (except loopers)*)
   250 
   251 fun merge_ss
   252    (Simpset {mss = mss1, loop_tacs = loop_tacs1, subgoal_tac, finish_tac, unsafe_finish_tac},
   253     Simpset {mss = mss2, loop_tacs = loop_tacs2, ...}) =
   254   make_ss (Thm.merge_mss (mss1, mss2), subgoal_tac,
   255     merge_alists loop_tacs1 loop_tacs2, finish_tac, unsafe_finish_tac);
   256 
   257 
   258 
   259 (** global and local simpset data **)
   260 
   261 (* theory data kind 'Provers/simpset' *)
   262 
   263 structure GlobalSimpsetArgs =
   264 struct
   265   val name = "Provers/simpset";
   266   type T = simpset ref;
   267 
   268   val empty = ref empty_ss;
   269   fun prep_ext (ref ss) = (ref ss): T;            (*create new reference!*)
   270   fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
   271   fun print _ (ref ss) = print_ss ss;
   272 end;
   273 
   274 structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
   275 val print_simpset = GlobalSimpset.print;
   276 val simpset_ref_of_sg = GlobalSimpset.get_sg;
   277 val simpset_ref_of = GlobalSimpset.get;
   278 
   279 
   280 (* access global simpset *)
   281 
   282 val simpset_of_sg = ! o simpset_ref_of_sg;
   283 val simpset_of = simpset_of_sg o sign_of;
   284 
   285 fun SIMPSET tacf state = tacf (simpset_of_sg (sign_of_thm state)) state;
   286 fun SIMPSET' tacf i state = tacf (simpset_of_sg (sign_of_thm state)) i state;
   287 
   288 val simpset = simpset_of o Context.the_context;
   289 val simpset_ref = simpset_ref_of_sg o sign_of o Context.the_context;
   290 
   291 
   292 (* change global simpset *)
   293 
   294 fun change_simpset f x = simpset_ref () := (f (simpset (), x));
   295 
   296 val Addsimps = change_simpset (op addsimps);
   297 val Delsimps = change_simpset (op delsimps);
   298 val Addsimprocs = change_simpset (op addsimprocs);
   299 val Delsimprocs = change_simpset (op delsimprocs);
   300 
   301 
   302 (* proof data kind 'Provers/simpset' *)
   303 
   304 structure LocalSimpsetArgs =
   305 struct
   306   val name = "Provers/simpset";
   307   type T = simpset;
   308   val init = simpset_of;
   309   fun print _ ss = print_ss ss;
   310 end;
   311 
   312 structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
   313 val print_local_simpset = LocalSimpset.print;
   314 val get_local_simpset = LocalSimpset.get;
   315 val put_local_simpset = LocalSimpset.put;
   316 
   317 
   318 (* attributes *)
   319 
   320 fun change_global_ss f (thy, th) =
   321   let val r = simpset_ref_of thy
   322   in r := f (! r, [Attribute.thm_of th]); (thy, th) end;
   323 
   324 fun change_local_ss f (ctxt, th) =
   325   let val ss = f (get_local_simpset ctxt, [Attribute.thm_of th])
   326   in (put_local_simpset ss ctxt, th) end;
   327 
   328 val simp_add_global = change_global_ss (op addsimps);
   329 val simp_del_global = change_global_ss (op delsimps);
   330 val simp_add_local = change_local_ss (op addsimps);
   331 val simp_del_local = change_local_ss (op delsimps);
   332 
   333 
   334 
   335 (** simplification tactics **)
   336 
   337 fun solve_all_tac (subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac) mss =
   338   let
   339     val ss =
   340       make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, unsafe_finish_tac);
   341     val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
   342   in DEPTH_SOLVE solve1_tac end;
   343 
   344 fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
   345 
   346 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
   347 fun basic_gen_simp_tac mode =
   348   fn (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}) =>
   349     let
   350       fun simp_loop_tac i =
   351         asm_rewrite_goal_tac mode
   352           (solve_all_tac (subgoal_tac,loop_tacs,finish_tac,unsafe_finish_tac))
   353           mss i
   354         THEN (finish_tac (prems_of_mss mss) i ORELSE
   355               TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
   356     in simp_loop_tac end;
   357 
   358 fun gen_simp_tac mode (ss as Simpset {unsafe_finish_tac, ...}) =
   359   basic_gen_simp_tac mode (ss setSSolver unsafe_finish_tac);
   360 
   361 val          simp_tac = gen_simp_tac (false, false, false);
   362 val      asm_simp_tac = gen_simp_tac (false, true, false);
   363 val     full_simp_tac = gen_simp_tac (true,  false, false);
   364 val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
   365 val asm_full_simp_tac = gen_simp_tac (true,  true, true);
   366 
   367 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
   368 val safe_asm_full_simp_tac = basic_gen_simp_tac (true, true, false);
   369 
   370 (*the abstraction over the proof state delays the dereferencing*)
   371 fun          Simp_tac i st =          simp_tac (simpset ()) i st;
   372 fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
   373 fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
   374 fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
   375 fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
   376 
   377 
   378 
   379 (** simplification rules and conversions **)
   380 
   381 fun simp rew mode (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}) thm =
   382   let
   383     val tacf = solve_all_tac (subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   384     fun prover m th = apsome fst (Seq.pull (tacf m th));
   385   in rew mode prover mss thm end;
   386 
   387 val simp_thm = simp Drule.rewrite_thm;
   388 val simp_cterm = simp Drule.rewrite_cterm;
   389 
   390 val          simplify = simp_thm (false, false, false);
   391 val      asm_simplify = simp_thm (false, true, false);
   392 val     full_simplify = simp_thm (true, false, false);
   393 val asm_full_simplify = simp_thm (true, true, false);
   394 
   395 val          rewrite = simp_cterm (false, false, false);
   396 val      asm_rewrite = simp_cterm (false, true, false);
   397 val     full_rewrite = simp_cterm (true, false, false);
   398 val asm_full_rewrite = simp_cterm (true, true, false);
   399 
   400 
   401 
   402 (** concrete syntax of attributes **)
   403 
   404 (* add / del *)
   405 
   406 val simpN = "simp";
   407 val addN = "add";
   408 val delN = "del";
   409 val otherN = "other";
   410 
   411 fun simp_att change =
   412   (Args.$$$ addN >> K (op addsimps) ||
   413     Args.$$$ delN >> K (op delsimps) ||
   414     Scan.succeed (op addsimps))
   415   >> change
   416   |> Scan.lift
   417   |> Attrib.syntax;
   418 
   419 val simp_attr = (simp_att change_global_ss, simp_att change_local_ss);
   420 
   421 
   422 (* conversions *)
   423 
   424 fun conv_attr f =
   425   (Attrib.no_args (Attribute.rule (f o simpset_of)),
   426     Attrib.no_args (Attribute.rule (f o get_local_simpset)));
   427 
   428 
   429 (* setup_attrs *)
   430 
   431 val setup_attrs = Attrib.add_attributes
   432  [(simpN,               simp_attr, "simplification rule"),
   433   ("simplify",          conv_attr simplify, "simplify rule"),
   434   ("asm_simplify",      conv_attr asm_simplify, "simplify rule, using assumptions"),
   435   ("full_simplify",     conv_attr full_simplify, "fully simplify rule"),
   436   ("asm_full_simplify", conv_attr asm_full_simplify, "fully simplify rule, using assumptions")];
   437 
   438 
   439 
   440 (** proof methods **)
   441 
   442 (* simplification *)
   443 
   444 val simp_modifiers =
   445  [Args.$$$ simpN -- Args.$$$ addN >> K simp_add_local,
   446   Args.$$$ simpN -- Args.$$$ delN >> K simp_del_local,
   447   Args.$$$ otherN >> K I];	(* FIXME ?? *)
   448 
   449 val simp_modifiers' =
   450  [Args.$$$ addN >> K simp_add_local,
   451   Args.$$$ delN >> K simp_del_local,
   452   Args.$$$ otherN >> K I];
   453 
   454 val simp_args = Method.only_sectioned_args simp_modifiers';
   455 
   456 fun simp_meth tac ctxt = Method.METHOD (fn facts =>
   457   FIRSTGOAL (REPEAT_DETERM o etac Drule.thin_rl THEN'
   458     metacuts_tac (Attribute.thms_of facts) THEN'
   459     tac (get_local_simpset ctxt)));
   460 
   461 val simp_method = simp_args o simp_meth;
   462 
   463 
   464 (* setup_methods *)
   465 
   466 val setup_methods = Method.add_methods
   467  [(simpN,               simp_method asm_full_simp_tac, "simplification"),
   468   ("simp_tac",          simp_method simp_tac, "simp_tac"),
   469   ("asm_simp_tac",      simp_method asm_simp_tac, "asm_simp_tac"),
   470   ("full_simp_tac",     simp_method full_simp_tac, "full_simp_tac"),
   471   ("asm_full_simp_tac", simp_method asm_full_simp_tac, "asm_full_simp_tac"),
   472   ("asm_lr_simp_tac",   simp_method asm_lr_simp_tac, "asm_lr_simp_tac")];
   473 
   474 
   475 
   476 (** theory setup **)
   477 
   478 val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs, setup_methods];
   479 
   480 
   481 end;
   482 
   483 
   484 structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
   485 open BasicSimplifier;