src/Provers/simplifier.ML
author wenzelm
Fri Apr 30 18:10:03 1999 +0200 (1999-04-30)
changeset 6556 daa00919502b
parent 6534 5a838c1d9d2f
child 6911 ef0f25d0bc2d
permissions -rw-r--r--
theory data: copy;
     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 copy (ref ss) = (ref ss): T;            (*create new reference!*)
   270   val prep_ext = copy;
   271   fun merge (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
   272   fun print _ (ref ss) = print_ss ss;
   273 end;
   274 
   275 structure GlobalSimpset = TheoryDataFun(GlobalSimpsetArgs);
   276 val print_simpset = GlobalSimpset.print;
   277 val simpset_ref_of_sg = GlobalSimpset.get_sg;
   278 val simpset_ref_of = GlobalSimpset.get;
   279 
   280 
   281 (* access global simpset *)
   282 
   283 val simpset_of_sg = ! o simpset_ref_of_sg;
   284 val simpset_of = simpset_of_sg o Theory.sign_of;
   285 
   286 fun SIMPSET tacf state = tacf (simpset_of_sg (Thm.sign_of_thm state)) state;
   287 fun SIMPSET' tacf i state = tacf (simpset_of_sg (Thm.sign_of_thm state)) i state;
   288 
   289 val simpset = simpset_of o Context.the_context;
   290 val simpset_ref = simpset_ref_of_sg o Theory.sign_of o Context.the_context;
   291 
   292 
   293 (* change global simpset *)
   294 
   295 fun change_simpset f x = simpset_ref () := (f (simpset (), x));
   296 
   297 val Addsimps = change_simpset (op addsimps);
   298 val Delsimps = change_simpset (op delsimps);
   299 val Addsimprocs = change_simpset (op addsimprocs);
   300 val Delsimprocs = change_simpset (op delsimprocs);
   301 
   302 
   303 (* proof data kind 'Provers/simpset' *)
   304 
   305 structure LocalSimpsetArgs =
   306 struct
   307   val name = "Provers/simpset";
   308   type T = simpset;
   309   val init = simpset_of;
   310   fun print _ ss = print_ss ss;
   311 end;
   312 
   313 structure LocalSimpset = ProofDataFun(LocalSimpsetArgs);
   314 val print_local_simpset = LocalSimpset.print;
   315 val get_local_simpset = LocalSimpset.get;
   316 val put_local_simpset = LocalSimpset.put;
   317 
   318 
   319 (* attributes *)
   320 
   321 fun change_global_ss f (thy, th) =
   322   let val r = simpset_ref_of thy
   323   in r := f (! r, [th]); (thy, th) end;
   324 
   325 fun change_local_ss f (ctxt, th) =
   326   let val ss = f (get_local_simpset ctxt, [th])
   327   in (put_local_simpset ss ctxt, th) end;
   328 
   329 val simp_add_global = change_global_ss (op addsimps);
   330 val simp_del_global = change_global_ss (op delsimps);
   331 val simp_add_local = change_local_ss (op addsimps);
   332 val simp_del_local = change_local_ss (op delsimps);
   333 
   334 
   335 
   336 (** simplification tactics **)
   337 
   338 fun solve_all_tac (subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac) mss =
   339   let
   340     val ss =
   341       make_ss (mss, subgoal_tac, loop_tacs, unsafe_finish_tac, unsafe_finish_tac);
   342     val solve1_tac = (subgoal_tac ss THEN_ALL_NEW (K no_tac)) 1
   343   in DEPTH_SOLVE solve1_tac end;
   344 
   345 fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
   346 
   347 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
   348 fun basic_gen_simp_tac mode =
   349   fn (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}) =>
   350     let
   351       fun simp_loop_tac i =
   352         asm_rewrite_goal_tac mode
   353           (solve_all_tac (subgoal_tac,loop_tacs,finish_tac,unsafe_finish_tac))
   354           mss i
   355         THEN (finish_tac (prems_of_mss mss) i ORELSE
   356               TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
   357     in simp_loop_tac end;
   358 
   359 fun gen_simp_tac mode (ss as Simpset {unsafe_finish_tac, ...}) =
   360   basic_gen_simp_tac mode (ss setSSolver unsafe_finish_tac);
   361 
   362 val          simp_tac = gen_simp_tac (false, false, false);
   363 val      asm_simp_tac = gen_simp_tac (false, true, false);
   364 val     full_simp_tac = gen_simp_tac (true,  false, false);
   365 val   asm_lr_simp_tac = gen_simp_tac (true,  true, false);
   366 val asm_full_simp_tac = gen_simp_tac (true,  true, true);
   367 
   368 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
   369 val safe_asm_full_simp_tac = basic_gen_simp_tac (true, true, false);
   370 
   371 (*the abstraction over the proof state delays the dereferencing*)
   372 fun          Simp_tac i st =          simp_tac (simpset ()) i st;
   373 fun      Asm_simp_tac i st =      asm_simp_tac (simpset ()) i st;
   374 fun     Full_simp_tac i st =     full_simp_tac (simpset ()) i st;
   375 fun   Asm_lr_simp_tac i st =   asm_lr_simp_tac (simpset ()) i st;
   376 fun Asm_full_simp_tac i st = asm_full_simp_tac (simpset ()) i st;
   377 
   378 
   379 
   380 (** simplification rules and conversions **)
   381 
   382 fun simp rew mode (Simpset {mss, subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac}) thm =
   383   let
   384     val tacf = solve_all_tac (subgoal_tac, loop_tacs, finish_tac, unsafe_finish_tac);
   385     fun prover m th = apsome fst (Seq.pull (tacf m th));
   386   in rew mode prover mss thm end;
   387 
   388 val simp_thm = simp Drule.rewrite_thm;
   389 val simp_cterm = simp Drule.rewrite_cterm;
   390 
   391 val          simplify = simp_thm (false, false, false);
   392 val      asm_simplify = simp_thm (false, true, false);
   393 val     full_simplify = simp_thm (true, false, false);
   394 val asm_full_simplify = simp_thm (true, true, false);
   395 
   396 val          rewrite = simp_cterm (false, false, false);
   397 val      asm_rewrite = simp_cterm (false, true, false);
   398 val     full_rewrite = simp_cterm (true, false, false);
   399 val asm_full_rewrite = simp_cterm (true, true, false);
   400 
   401 
   402 
   403 (** concrete syntax of attributes **)
   404 
   405 (* add / del *)
   406 
   407 val simpN = "simp";
   408 val addN = "add";
   409 val delN = "del";
   410 val otherN = "other";
   411 
   412 fun simp_att change =
   413   (Args.$$$ addN >> K (op addsimps) ||
   414     Args.$$$ delN >> K (op delsimps) ||
   415     Scan.succeed (op addsimps))
   416   >> change
   417   |> Scan.lift
   418   |> Attrib.syntax;
   419 
   420 val simp_attr = (simp_att change_global_ss, simp_att change_local_ss);
   421 
   422 
   423 (* conversions *)
   424 
   425 fun conv_attr f =
   426   (Attrib.no_args (Drule.rule_attribute (f o simpset_of)),
   427     Attrib.no_args (Drule.rule_attribute (f o get_local_simpset)));
   428 
   429 
   430 (* setup_attrs *)
   431 
   432 val setup_attrs = Attrib.add_attributes
   433  [(simpN,               simp_attr, "simplification rule"),
   434   ("simplify",          conv_attr simplify, "simplify rule"),
   435   ("asm_simplify",      conv_attr asm_simplify, "simplify rule, using assumptions"),
   436   ("full_simplify",     conv_attr full_simplify, "fully simplify rule"),
   437   ("asm_full_simplify", conv_attr asm_full_simplify, "fully simplify rule, using assumptions")];
   438 
   439 
   440 
   441 (** proof methods **)
   442 
   443 (* simplification *)
   444 
   445 val simp_modifiers =
   446  [Args.$$$ simpN -- Args.$$$ addN >> K simp_add_local,
   447   Args.$$$ simpN -- Args.$$$ delN >> K simp_del_local,
   448   Args.$$$ otherN >> K I];	(* FIXME ?? *)
   449 
   450 val simp_modifiers' =
   451  [Args.$$$ addN >> K simp_add_local,
   452   Args.$$$ delN >> K simp_del_local,
   453   Args.$$$ otherN >> K I];
   454 
   455 val simp_args = Method.only_sectioned_args simp_modifiers';
   456 
   457 fun simp_meth proper tac ctxt = Method.METHOD (fn facts =>
   458   FIRSTGOAL
   459     ((if proper then REPEAT_DETERM o etac Drule.thin_rl THEN' metacuts_tac facts else K all_tac)
   460       THEN' tac (get_local_simpset ctxt)));
   461 
   462 val simp_method = simp_args oo simp_meth;
   463 
   464 
   465 (* setup_methods *)
   466 
   467 val setup_methods = Method.add_methods
   468  [(simpN,               simp_method true asm_full_simp_tac, "simplification"),
   469   ("simp_tac",          simp_method false simp_tac, "simp_tac"),
   470   ("asm_simp_tac",      simp_method false asm_simp_tac, "asm_simp_tac"),
   471   ("full_simp_tac",     simp_method false full_simp_tac, "full_simp_tac"),
   472   ("asm_full_simp_tac", simp_method false asm_full_simp_tac, "asm_full_simp_tac"),
   473   ("asm_lr_simp_tac",   simp_method false asm_lr_simp_tac, "asm_lr_simp_tac")];
   474 
   475 
   476 
   477 (** theory setup **)
   478 
   479 val setup = [GlobalSimpset.init, LocalSimpset.init, setup_attrs, setup_methods];
   480 
   481 
   482 end;
   483 
   484 
   485 structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;
   486 open BasicSimplifier;