src/Pure/raw_simplifier.ML
author wenzelm
Thu Dec 12 21:14:33 2013 +0100 (2013-12-12)
changeset 54729 c5cd7a58cf2d
parent 54728 445e7947c6b5
child 54731 384ac33802b0
permissions -rw-r--r--
generic trace operations for main steps of Simplifier;
     1 (*  Title:      Pure/raw_simplifier.ML
     2     Author:     Tobias Nipkow and Stefan Berghofer, TU Muenchen
     3 
     4 Higher-order Simplification.
     5 *)
     6 
     7 infix 4
     8   addsimps delsimps addsimprocs delsimprocs
     9   setloop addloop delloop
    10   setSSolver addSSolver setSolver addSolver;
    11 
    12 signature BASIC_RAW_SIMPLIFIER =
    13 sig
    14   val simp_depth_limit: int Config.T
    15   val simp_trace_depth_limit: int Config.T
    16   val simp_debug: bool Config.T
    17   val simp_trace: bool Config.T
    18   type cong_name = bool * string
    19   type rrule
    20   val eq_rrule: rrule * rrule -> bool
    21   type proc
    22   type solver
    23   val mk_solver: string -> (Proof.context -> int -> tactic) -> solver
    24   type simpset
    25   val empty_ss: simpset
    26   val merge_ss: simpset * simpset -> simpset
    27   val dest_ss: simpset ->
    28    {simps: (string * thm) list,
    29     procs: (string * cterm list) list,
    30     congs: (cong_name * thm) list,
    31     weak_congs: cong_name list,
    32     loopers: string list,
    33     unsafe_solvers: string list,
    34     safe_solvers: string list}
    35   type simproc
    36   val eq_simproc: simproc * simproc -> bool
    37   val transform_simproc: morphism -> simproc -> simproc
    38   val make_simproc: {name: string, lhss: cterm list,
    39     proc: morphism -> Proof.context -> cterm -> thm option, identifier: thm list} -> simproc
    40   val mk_simproc: string -> cterm list -> (Proof.context -> term -> thm option) -> simproc
    41   val simpset_of: Proof.context -> simpset
    42   val put_simpset: simpset -> Proof.context -> Proof.context
    43   val global_context: theory -> simpset -> Proof.context
    44   val simpset_map: Proof.context -> (Proof.context -> Proof.context) -> simpset -> simpset
    45   val map_theory_simpset: (Proof.context -> Proof.context) -> theory -> theory
    46   val empty_simpset: Proof.context -> Proof.context
    47   val clear_simpset: Proof.context -> Proof.context
    48   val addsimps: Proof.context * thm list -> Proof.context
    49   val delsimps: Proof.context * thm list -> Proof.context
    50   val addsimprocs: Proof.context * simproc list -> Proof.context
    51   val delsimprocs: Proof.context * simproc list -> Proof.context
    52   val setloop: Proof.context * (Proof.context -> int -> tactic) -> Proof.context
    53   val addloop: Proof.context * (string * (Proof.context -> int -> tactic)) -> Proof.context
    54   val delloop: Proof.context * string -> Proof.context
    55   val setSSolver: Proof.context * solver -> Proof.context
    56   val addSSolver: Proof.context * solver -> Proof.context
    57   val setSolver: Proof.context * solver -> Proof.context
    58   val addSolver: Proof.context * solver -> Proof.context
    59 
    60   val rewrite_rule: thm list -> thm -> thm
    61   val rewrite_goals_rule: thm list -> thm -> thm
    62   val rewrite_goals_tac: thm list -> tactic
    63   val rewrite_goal_tac: thm list -> int -> tactic
    64   val prune_params_tac: tactic
    65   val fold_rule: thm list -> thm -> thm
    66   val fold_goals_tac: thm list -> tactic
    67   val norm_hhf: thm -> thm
    68   val norm_hhf_protect: thm -> thm
    69 end;
    70 
    71 signature RAW_SIMPLIFIER =
    72 sig
    73   include BASIC_RAW_SIMPLIFIER
    74   exception SIMPLIFIER of string * thm
    75   type trace_ops
    76   val set_trace_ops: trace_ops -> Proof.context -> Proof.context
    77   val internal_ss: simpset ->
    78    {congs: (cong_name * thm) list * cong_name list,
    79     procs: proc Net.net,
    80     mk_rews:
    81      {mk: Proof.context -> thm -> thm list,
    82       mk_cong: Proof.context -> thm -> thm,
    83       mk_sym: Proof.context -> thm -> thm option,
    84       mk_eq_True: Proof.context -> thm -> thm option,
    85       reorient: Proof.context -> term list -> term -> term -> bool},
    86     termless: term * term -> bool,
    87     subgoal_tac: Proof.context -> int -> tactic,
    88     loop_tacs: (string * (Proof.context -> int -> tactic)) list,
    89     solvers: solver list * solver list,
    90     trace_ops: trace_ops}
    91   val map_ss: (Proof.context -> Proof.context) -> Context.generic -> Context.generic
    92   val prems_of: Proof.context -> thm list
    93   val add_simp: thm -> Proof.context -> Proof.context
    94   val del_simp: thm -> Proof.context -> Proof.context
    95   val add_eqcong: thm -> Proof.context -> Proof.context
    96   val del_eqcong: thm -> Proof.context -> Proof.context
    97   val add_cong: thm -> Proof.context -> Proof.context
    98   val del_cong: thm -> Proof.context -> Proof.context
    99   val mksimps: Proof.context -> thm -> thm list
   100   val set_mksimps: (Proof.context -> thm -> thm list) -> Proof.context -> Proof.context
   101   val set_mkcong: (Proof.context -> thm -> thm) -> Proof.context -> Proof.context
   102   val set_mksym: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context
   103   val set_mkeqTrue: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context
   104   val set_termless: (term * term -> bool) -> Proof.context -> Proof.context
   105   val set_subgoaler: (Proof.context -> int -> tactic) -> Proof.context -> Proof.context
   106   val solver: Proof.context -> solver -> int -> tactic
   107   val simp_depth_limit_raw: Config.raw
   108   val default_mk_sym: Proof.context -> thm -> thm option
   109   val simproc_global_i: theory -> string -> term list ->
   110     (Proof.context -> term -> thm option) -> simproc
   111   val simproc_global: theory -> string -> string list ->
   112     (Proof.context -> term -> thm option) -> simproc
   113   val simp_trace_depth_limit_raw: Config.raw
   114   val simp_trace_depth_limit_default: int Unsynchronized.ref
   115   val simp_trace_default: bool Unsynchronized.ref
   116   val simp_trace_raw: Config.raw
   117   val simp_debug_raw: Config.raw
   118   val add_prems: thm list -> Proof.context -> Proof.context
   119   val debug_bounds: bool Unsynchronized.ref
   120   val set_reorient: (Proof.context -> term list -> term -> term -> bool) ->
   121     Proof.context -> Proof.context
   122   val set_solvers: solver list -> Proof.context -> Proof.context
   123   val rewrite_cterm: bool * bool * bool ->
   124     (Proof.context -> thm -> thm option) -> Proof.context -> conv
   125   val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term
   126   val rewrite_thm: bool * bool * bool ->
   127     (Proof.context -> thm -> thm option) -> Proof.context -> thm -> thm
   128   val generic_rewrite_goal_tac: bool * bool * bool ->
   129     (Proof.context -> tactic) -> Proof.context -> int -> tactic
   130   val rewrite: bool -> thm list -> conv
   131 end;
   132 
   133 structure Raw_Simplifier: RAW_SIMPLIFIER =
   134 struct
   135 
   136 (** datatype simpset **)
   137 
   138 (* congruence rules *)
   139 
   140 type cong_name = bool * string;
   141 
   142 fun cong_name (Const (a, _)) = SOME (true, a)
   143   | cong_name (Free (a, _)) = SOME (false, a)
   144   | cong_name _ = NONE;
   145 
   146 
   147 (* rewrite rules *)
   148 
   149 type rrule =
   150  {thm: thm,         (*the rewrite rule*)
   151   name: string,     (*name of theorem from which rewrite rule was extracted*)
   152   lhs: term,        (*the left-hand side*)
   153   elhs: cterm,      (*the etac-contracted lhs*)
   154   extra: bool,      (*extra variables outside of elhs*)
   155   fo: bool,         (*use first-order matching*)
   156   perm: bool};      (*the rewrite rule is permutative*)
   157 
   158 (*
   159 Remarks:
   160   - elhs is used for matching,
   161     lhs only for preservation of bound variable names;
   162   - fo is set iff
   163     either elhs is first-order (no Var is applied),
   164       in which case fo-matching is complete,
   165     or elhs is not a pattern,
   166       in which case there is nothing better to do;
   167 *)
   168 
   169 fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
   170   Thm.eq_thm_prop (thm1, thm2);
   171 
   172 (* FIXME: it seems that the conditions on extra variables are too liberal if
   173 prems are nonempty: does solving the prems really guarantee instantiation of
   174 all its Vars? Better: a dynamic check each time a rule is applied.
   175 *)
   176 fun rewrite_rule_extra_vars prems elhs erhs =
   177   let
   178     val elhss = elhs :: prems;
   179     val tvars = fold Term.add_tvars elhss [];
   180     val vars = fold Term.add_vars elhss [];
   181   in
   182     erhs |> Term.exists_type (Term.exists_subtype
   183       (fn TVar v => not (member (op =) tvars v) | _ => false)) orelse
   184     erhs |> Term.exists_subterm
   185       (fn Var v => not (member (op =) vars v) | _ => false)
   186   end;
   187 
   188 fun rrule_extra_vars elhs thm =
   189   rewrite_rule_extra_vars [] (term_of elhs) (Thm.full_prop_of thm);
   190 
   191 fun mk_rrule2 {thm, name, lhs, elhs, perm} =
   192   let
   193     val t = term_of elhs;
   194     val fo = Pattern.first_order t orelse not (Pattern.pattern t);
   195     val extra = rrule_extra_vars elhs thm;
   196   in {thm = thm, name = name, lhs = lhs, elhs = elhs, extra = extra, fo = fo, perm = perm} end;
   197 
   198 (*simple test for looping rewrite rules and stupid orientations*)
   199 fun default_reorient ctxt prems lhs rhs =
   200   rewrite_rule_extra_vars prems lhs rhs
   201     orelse
   202   is_Var (head_of lhs)
   203     orelse
   204 (* turns t = x around, which causes a headache if x is a local variable -
   205    usually it is very useful :-(
   206   is_Free rhs andalso not(is_Free lhs) andalso not(Logic.occs(rhs,lhs))
   207   andalso not(exists_subterm is_Var lhs)
   208     orelse
   209 *)
   210   exists (fn t => Logic.occs (lhs, t)) (rhs :: prems)
   211     orelse
   212   null prems andalso Pattern.matches (Proof_Context.theory_of ctxt) (lhs, rhs)
   213     (*the condition "null prems" is necessary because conditional rewrites
   214       with extra variables in the conditions may terminate although
   215       the rhs is an instance of the lhs; example: ?m < ?n ==> f(?n) == f(?m)*)
   216     orelse
   217   is_Const lhs andalso not (is_Const rhs);
   218 
   219 
   220 (* simplification procedures *)
   221 
   222 datatype proc =
   223   Proc of
   224    {name: string,
   225     lhs: cterm,
   226     proc: Proof.context -> cterm -> thm option,
   227     id: stamp * thm list};
   228 
   229 fun eq_procid ((s1: stamp, ths1: thm list), (s2, ths2)) =
   230   s1 = s2 andalso eq_list Thm.eq_thm (ths1, ths2);
   231 
   232 fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = eq_procid (id1, id2);
   233 
   234 
   235 (* solvers *)
   236 
   237 datatype solver =
   238   Solver of
   239    {name: string,
   240     solver: Proof.context -> int -> tactic,
   241     id: stamp};
   242 
   243 fun mk_solver name solver = Solver {name = name, solver = solver, id = stamp ()};
   244 
   245 fun solver_name (Solver {name, ...}) = name;
   246 fun solver ctxt (Solver {solver = tac, ...}) = tac ctxt;
   247 fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
   248 
   249 
   250 (* trace operations *)
   251 
   252 type trace_ops =
   253  {trace_invoke: {depth: int, term: term} -> Proof.context -> Proof.context,
   254   trace_apply: {unconditional: bool, term: term, thm: thm, name: string} ->
   255     Proof.context -> (Proof.context -> (thm * term) option) -> (thm * term) option};
   256 
   257 val no_trace_ops : trace_ops =
   258  {trace_invoke = fn _ => fn ctxt => ctxt,
   259   trace_apply = fn _ => fn ctxt => fn cont => cont ctxt};
   260 
   261 
   262 (* simplification sets *)
   263 
   264 (*A simpset contains data required during conversion:
   265     rules: discrimination net of rewrite rules;
   266     prems: current premises;
   267     bounds: maximal index of bound variables already used
   268       (for generating new names when rewriting under lambda abstractions);
   269     depth: simp_depth and exceeded flag;
   270     congs: association list of congruence rules and
   271            a list of `weak' congruence constants.
   272            A congruence is `weak' if it avoids normalization of some argument.
   273     procs: discrimination net of simplification procedures
   274       (functions that prove rewrite rules on the fly);
   275     mk_rews:
   276       mk: turn simplification thms into rewrite rules;
   277       mk_cong: prepare congruence rules;
   278       mk_sym: turn == around;
   279       mk_eq_True: turn P into P == True;
   280     termless: relation for ordered rewriting;*)
   281 
   282 datatype simpset =
   283   Simpset of
   284    {rules: rrule Net.net,
   285     prems: thm list,
   286     bounds: int * ((string * typ) * string) list,
   287     depth: int * bool Unsynchronized.ref} *
   288    {congs: (cong_name * thm) list * cong_name list,
   289     procs: proc Net.net,
   290     mk_rews:
   291      {mk: Proof.context -> thm -> thm list,
   292       mk_cong: Proof.context -> thm -> thm,
   293       mk_sym: Proof.context -> thm -> thm option,
   294       mk_eq_True: Proof.context -> thm -> thm option,
   295       reorient: Proof.context -> term list -> term -> term -> bool},
   296     termless: term * term -> bool,
   297     subgoal_tac: Proof.context -> int -> tactic,
   298     loop_tacs: (string * (Proof.context -> int -> tactic)) list,
   299     solvers: solver list * solver list,
   300     trace_ops: trace_ops};
   301 
   302 fun internal_ss (Simpset (_, ss2)) = ss2;
   303 
   304 fun make_ss1 (rules, prems, bounds, depth) =
   305   {rules = rules, prems = prems, bounds = bounds, depth = depth};
   306 
   307 fun map_ss1 f {rules, prems, bounds, depth} =
   308   make_ss1 (f (rules, prems, bounds, depth));
   309 
   310 fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =
   311   {congs = congs, procs = procs, mk_rews = mk_rews, termless = termless,
   312     subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers, trace_ops = trace_ops};
   313 
   314 fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops} =
   315   make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   316 
   317 fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2);
   318 
   319 fun dest_ss (Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...})) =
   320  {simps = Net.entries rules
   321     |> map (fn {name, thm, ...} => (name, thm)),
   322   procs = Net.entries procs
   323     |> map (fn Proc {name, lhs, id, ...} => ((name, lhs), id))
   324     |> partition_eq (eq_snd eq_procid)
   325     |> map (fn ps => (fst (fst (hd ps)), map (snd o fst) ps)),
   326   congs = #1 congs,
   327   weak_congs = #2 congs,
   328   loopers = map fst loop_tacs,
   329   unsafe_solvers = map solver_name (#1 solvers),
   330   safe_solvers = map solver_name (#2 solvers)};
   331 
   332 
   333 (* empty *)
   334 
   335 fun init_ss mk_rews termless subgoal_tac solvers trace_ops =
   336   make_simpset ((Net.empty, [], (0, []), (0, Unsynchronized.ref false)),
   337     (([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers, trace_ops));
   338 
   339 fun default_mk_sym _ th = SOME (th RS Drule.symmetric_thm);
   340 
   341 val empty_ss =
   342   init_ss
   343     {mk = fn _ => fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [],
   344       mk_cong = K I,
   345       mk_sym = default_mk_sym,
   346       mk_eq_True = K (K NONE),
   347       reorient = default_reorient}
   348     Term_Ord.termless (K (K no_tac))
   349     ([], [])
   350     no_trace_ops;
   351 
   352 
   353 (* merge *)  (*NOTE: ignores some fields of 2nd simpset*)
   354 
   355 fun merge_ss (ss1, ss2) =
   356   if pointer_eq (ss1, ss2) then ss1
   357   else
   358     let
   359       val Simpset ({rules = rules1, prems = prems1, bounds = bounds1, depth = depth1},
   360        {congs = (congs1, weak1), procs = procs1, mk_rews, termless, subgoal_tac,
   361         loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1), trace_ops}) = ss1;
   362       val Simpset ({rules = rules2, prems = prems2, bounds = bounds2, depth = depth2},
   363        {congs = (congs2, weak2), procs = procs2, mk_rews = _, termless = _, subgoal_tac = _,
   364         loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2), trace_ops = _}) = ss2;
   365 
   366       val rules' = Net.merge eq_rrule (rules1, rules2);
   367       val prems' = Thm.merge_thms (prems1, prems2);
   368       val bounds' = if #1 bounds1 < #1 bounds2 then bounds2 else bounds1;
   369       val depth' = if #1 depth1 < #1 depth2 then depth2 else depth1;
   370       val congs' = merge (Thm.eq_thm_prop o pairself #2) (congs1, congs2);
   371       val weak' = merge (op =) (weak1, weak2);
   372       val procs' = Net.merge eq_proc (procs1, procs2);
   373       val loop_tacs' = AList.merge (op =) (K true) (loop_tacs1, loop_tacs2);
   374       val unsafe_solvers' = merge eq_solver (unsafe_solvers1, unsafe_solvers2);
   375       val solvers' = merge eq_solver (solvers1, solvers2);
   376     in
   377       make_simpset ((rules', prems', bounds', depth'), ((congs', weak'), procs',
   378         mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers'), trace_ops))
   379     end;
   380 
   381 
   382 
   383 (** context data **)
   384 
   385 structure Simpset = Generic_Data
   386 (
   387   type T = simpset;
   388   val empty = empty_ss;
   389   val extend = I;
   390   val merge = merge_ss;
   391 );
   392 
   393 val simpset_of = Simpset.get o Context.Proof;
   394 
   395 fun map_simpset f = Context.proof_map (Simpset.map f);
   396 fun map_simpset1 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (map_ss1 f ss1, ss2));
   397 fun map_simpset2 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (ss1, map_ss2 f ss2));
   398 
   399 fun simpset_map ctxt f ss = ctxt |> map_simpset (K ss) |> f |> Context.Proof |> Simpset.get;
   400 
   401 fun put_simpset ss = map_simpset (fn context_ss =>
   402   let
   403     val Simpset ({rules, prems, ...},  (* FIXME prems from context (!?) *)
   404       {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, ...}) = ss;
   405     val Simpset ({bounds, depth, ...}, {trace_ops, ...}) = context_ss;
   406   in
   407     Simpset (make_ss1 (rules, prems, bounds, depth),
   408       make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops))
   409   end);
   410 
   411 fun global_context thy ss = Proof_Context.init_global thy |> put_simpset ss;
   412 
   413 val empty_simpset = put_simpset empty_ss;
   414 
   415 fun map_theory_simpset f thy =
   416   let
   417     val ctxt' = f (Proof_Context.init_global thy);
   418     val thy' = Proof_Context.theory_of ctxt';
   419   in Context.theory_map (Simpset.map (K (simpset_of ctxt'))) thy' end;
   420 
   421 fun map_ss f = Context.mapping (map_theory_simpset f) f;
   422 
   423 val clear_simpset =
   424   map_simpset (fn Simpset (_, {mk_rews, termless, subgoal_tac, solvers, trace_ops, ...}) =>
   425     init_ss mk_rews termless subgoal_tac solvers trace_ops);
   426 
   427 
   428 (* simp depth *)
   429 
   430 val simp_depth_limit_raw = Config.declare "simp_depth_limit" (K (Config.Int 100));
   431 val simp_depth_limit = Config.int simp_depth_limit_raw;
   432 
   433 val simp_trace_depth_limit_default = Unsynchronized.ref 1;
   434 val simp_trace_depth_limit_raw = Config.declare "simp_trace_depth_limit"
   435   (fn _ => Config.Int (! simp_trace_depth_limit_default));
   436 val simp_trace_depth_limit = Config.int simp_trace_depth_limit_raw;
   437 
   438 fun trace_depth ctxt msg =
   439   let
   440     val Simpset ({depth = (depth, exceeded), ...}, _) = simpset_of ctxt;
   441     val depth_limit = Config.get ctxt simp_trace_depth_limit;
   442   in
   443     if depth > depth_limit then
   444       if ! exceeded then () else (tracing "simp_trace_depth_limit exceeded!"; exceeded := true)
   445     else (tracing (enclose "[" "]" (string_of_int depth) ^ msg); exceeded := false)
   446   end;
   447 
   448 fun inc_simp_depth ctxt =
   449   ctxt |> map_simpset1 (fn (rules, prems, bounds, (depth, exceeded)) =>
   450     (rules, prems, bounds,
   451       (depth + 1,
   452         if depth = Config.get ctxt simp_trace_depth_limit
   453         then Unsynchronized.ref false else exceeded)));
   454 
   455 fun simp_depth ctxt =
   456   let val Simpset ({depth = (depth, _), ...}, _) = simpset_of ctxt
   457   in depth end;
   458 
   459 
   460 (* diagnostics *)
   461 
   462 exception SIMPLIFIER of string * thm;
   463 
   464 val simp_debug_raw = Config.declare "simp_debug" (K (Config.Bool false));
   465 val simp_debug = Config.bool simp_debug_raw;
   466 
   467 val simp_trace_default = Unsynchronized.ref false;
   468 val simp_trace_raw = Config.declare "simp_trace" (fn _ => Config.Bool (! simp_trace_default));
   469 val simp_trace = Config.bool simp_trace_raw;
   470 
   471 fun if_enabled ctxt flag f = if Config.get ctxt flag then f ctxt else ();
   472 
   473 local
   474 
   475 fun prnt ctxt warn a = if warn then warning a else trace_depth ctxt a;
   476 
   477 fun show_bounds ctxt t =
   478   let
   479     val Simpset ({bounds = (_, bs), ...}, _) = simpset_of ctxt;
   480     val names = Term.declare_term_names t Name.context;
   481     val xs = rev (#1 (fold_map Name.variant (rev (map #2 bs)) names));
   482     fun subst (((b, T), _), x') = (Free (b, T), Syntax_Trans.mark_bound_abs (x', T));
   483   in Term.subst_atomic (ListPair.map subst (bs, xs)) t end;
   484 
   485 in
   486 
   487 fun print_term ctxt warn a t =
   488   prnt ctxt warn (a () ^ "\n" ^
   489     Syntax.string_of_term ctxt (if Config.get ctxt simp_debug then t else show_bounds ctxt t));
   490 
   491 fun debug ctxt warn a = if_enabled ctxt simp_debug (fn _ => prnt ctxt warn (a ()));
   492 fun trace ctxt warn a = if_enabled ctxt simp_trace (fn _ => prnt ctxt warn (a ()));
   493 
   494 fun debug_term ctxt warn a t = if_enabled ctxt simp_debug (fn _ => print_term ctxt warn a t);
   495 fun trace_term ctxt warn a t = if_enabled ctxt simp_trace (fn _ => print_term ctxt warn a t);
   496 
   497 fun trace_cterm ctxt warn a ct =
   498   if_enabled ctxt simp_trace (fn _ => print_term ctxt warn a (Thm.term_of ct));
   499 
   500 fun trace_thm ctxt a th =
   501   if_enabled ctxt simp_trace (fn _ => print_term ctxt false a (Thm.full_prop_of th));
   502 
   503 fun trace_named_thm ctxt a (th, name) =
   504   if_enabled ctxt simp_trace (fn _ =>
   505     print_term ctxt false
   506       (fn () => if name = "" then a () else a () ^ " " ^ quote name ^ ":")
   507       (Thm.full_prop_of th));
   508 
   509 fun warn_thm ctxt a th = print_term ctxt true a (Thm.full_prop_of th);
   510 fun cond_warn_thm ctxt a th = Context_Position.if_visible ctxt (fn () => warn_thm ctxt a th) ();
   511 
   512 end;
   513 
   514 
   515 
   516 (** simpset operations **)
   517 
   518 (* context *)
   519 
   520 fun eq_bound (x: string, (y, _)) = x = y;
   521 
   522 fun add_bound bound =
   523   map_simpset1 (fn (rules, prems, (count, bounds), depth) =>
   524     (rules, prems, (count + 1, bound :: bounds), depth));
   525 
   526 fun prems_of ctxt =
   527   let val Simpset ({prems, ...}, _) = simpset_of ctxt in prems end;
   528 
   529 fun add_prems ths =
   530   map_simpset1 (fn (rules, prems, bounds, depth) => (rules, ths @ prems, bounds, depth));
   531 
   532 
   533 (* maintain simp rules *)
   534 
   535 fun del_rrule (rrule as {thm, elhs, ...}) ctxt =
   536   ctxt |> map_simpset1 (fn (rules, prems, bounds, depth) =>
   537     (Net.delete_term eq_rrule (term_of elhs, rrule) rules, prems, bounds, depth))
   538   handle Net.DELETE => (cond_warn_thm ctxt (fn () => "Rewrite rule not in simpset:") thm; ctxt);
   539 
   540 fun insert_rrule (rrule as {thm, name, ...}) ctxt =
   541  (trace_named_thm ctxt (fn () => "Adding rewrite rule") (thm, name);
   542   ctxt |> map_simpset1 (fn (rules, prems, bounds, depth) =>
   543     let
   544       val rrule2 as {elhs, ...} = mk_rrule2 rrule;
   545       val rules' = Net.insert_term eq_rrule (term_of elhs, rrule2) rules;
   546     in (rules', prems, bounds, depth) end)
   547   handle Net.INSERT => (cond_warn_thm ctxt (fn () => "Ignoring duplicate rewrite rule:") thm; ctxt));
   548 
   549 local
   550 
   551 fun vperm (Var _, Var _) = true
   552   | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
   553   | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
   554   | vperm (t, u) = (t = u);
   555 
   556 fun var_perm (t, u) =
   557   vperm (t, u) andalso eq_set (op =) (Term.add_vars t [], Term.add_vars u []);
   558 
   559 in
   560 
   561 fun decomp_simp thm =
   562   let
   563     val prop = Thm.prop_of thm;
   564     val prems = Logic.strip_imp_prems prop;
   565     val concl = Drule.strip_imp_concl (Thm.cprop_of thm);
   566     val (lhs, rhs) = Thm.dest_equals concl handle TERM _ =>
   567       raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm);
   568     val elhs = Thm.dest_arg (Thm.cprop_of (Thm.eta_conversion lhs));
   569     val erhs = Envir.eta_contract (term_of rhs);
   570     val perm =
   571       var_perm (term_of elhs, erhs) andalso
   572       not (term_of elhs aconv erhs) andalso
   573       not (is_Var (term_of elhs));
   574   in (prems, term_of lhs, elhs, term_of rhs, perm) end;
   575 
   576 end;
   577 
   578 fun decomp_simp' thm =
   579   let val (_, lhs, _, rhs, _) = decomp_simp thm in
   580     if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", thm)
   581     else (lhs, rhs)
   582   end;
   583 
   584 fun mk_eq_True ctxt (thm, name) =
   585   let val Simpset (_, {mk_rews = {mk_eq_True, ...}, ...}) = simpset_of ctxt in
   586     (case mk_eq_True ctxt thm of
   587       NONE => []
   588     | SOME eq_True =>
   589         let val (_, lhs, elhs, _, _) = decomp_simp eq_True;
   590         in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end)
   591   end;
   592 
   593 (*create the rewrite rule and possibly also the eq_True variant,
   594   in case there are extra vars on the rhs*)
   595 fun rrule_eq_True ctxt thm name lhs elhs rhs thm2 =
   596   let val rrule = {thm = thm, name = name, lhs = lhs, elhs = elhs, perm = false} in
   597     if rewrite_rule_extra_vars [] lhs rhs then
   598       mk_eq_True ctxt (thm2, name) @ [rrule]
   599     else [rrule]
   600   end;
   601 
   602 fun mk_rrule ctxt (thm, name) =
   603   let val (prems, lhs, elhs, rhs, perm) = decomp_simp thm in
   604     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
   605     else
   606       (*weak test for loops*)
   607       if rewrite_rule_extra_vars prems lhs rhs orelse is_Var (term_of elhs)
   608       then mk_eq_True ctxt (thm, name)
   609       else rrule_eq_True ctxt thm name lhs elhs rhs thm
   610   end;
   611 
   612 fun orient_rrule ctxt (thm, name) =
   613   let
   614     val (prems, lhs, elhs, rhs, perm) = decomp_simp thm;
   615     val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = simpset_of ctxt;
   616   in
   617     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
   618     else if reorient ctxt prems lhs rhs then
   619       if reorient ctxt prems rhs lhs
   620       then mk_eq_True ctxt (thm, name)
   621       else
   622         (case mk_sym ctxt thm of
   623           NONE => []
   624         | SOME thm' =>
   625             let val (_, lhs', elhs', rhs', _) = decomp_simp thm'
   626             in rrule_eq_True ctxt thm' name lhs' elhs' rhs' thm end)
   627     else rrule_eq_True ctxt thm name lhs elhs rhs thm
   628   end;
   629 
   630 fun extract_rews (ctxt, thms) =
   631   let val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt
   632   in maps (fn thm => map (rpair (Thm.get_name_hint thm)) (mk ctxt thm)) thms end;
   633 
   634 fun extract_safe_rrules (ctxt, thm) =
   635   maps (orient_rrule ctxt) (extract_rews (ctxt, [thm]));
   636 
   637 
   638 (* add/del rules explicitly *)
   639 
   640 fun comb_simps comb mk_rrule (ctxt, thms) =
   641   let
   642     val rews = extract_rews (ctxt, thms);
   643   in fold (fold comb o mk_rrule) rews ctxt end;
   644 
   645 fun ctxt addsimps thms =
   646   comb_simps insert_rrule (mk_rrule ctxt) (ctxt, thms);
   647 
   648 fun ctxt delsimps thms =
   649   comb_simps del_rrule (map mk_rrule2 o mk_rrule ctxt) (ctxt, thms);
   650 
   651 fun add_simp thm ctxt = ctxt addsimps [thm];
   652 fun del_simp thm ctxt = ctxt delsimps [thm];
   653 
   654 
   655 (* congs *)
   656 
   657 local
   658 
   659 fun is_full_cong_prems [] [] = true
   660   | is_full_cong_prems [] _ = false
   661   | is_full_cong_prems (p :: prems) varpairs =
   662       (case Logic.strip_assums_concl p of
   663         Const ("==", _) $ lhs $ rhs =>
   664           let val (x, xs) = strip_comb lhs and (y, ys) = strip_comb rhs in
   665             is_Var x andalso forall is_Bound xs andalso
   666             not (has_duplicates (op =) xs) andalso xs = ys andalso
   667             member (op =) varpairs (x, y) andalso
   668             is_full_cong_prems prems (remove (op =) (x, y) varpairs)
   669           end
   670       | _ => false);
   671 
   672 fun is_full_cong thm =
   673   let
   674     val prems = Thm.prems_of thm and concl = Thm.concl_of thm;
   675     val (lhs, rhs) = Logic.dest_equals concl;
   676     val (f, xs) = strip_comb lhs and (g, ys) = strip_comb rhs;
   677   in
   678     f = g andalso not (has_duplicates (op =) (xs @ ys)) andalso length xs = length ys andalso
   679     is_full_cong_prems prems (xs ~~ ys)
   680   end;
   681 
   682 fun mk_cong ctxt =
   683   let val Simpset (_, {mk_rews = {mk_cong = f, ...}, ...}) = simpset_of ctxt
   684   in f ctxt end;
   685 
   686 in
   687 
   688 fun add_eqcong thm ctxt = ctxt |> map_simpset2
   689   (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   690     let
   691       val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
   692         handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", thm);
   693     (*val lhs = Envir.eta_contract lhs;*)
   694       val a = the (cong_name (head_of lhs)) handle Option.Option =>
   695         raise SIMPLIFIER ("Congruence must start with a constant or free variable", thm);
   696       val (xs, weak) = congs;
   697       val _ =
   698         if AList.defined (op =) xs a then
   699           Context_Position.if_visible ctxt
   700             warning ("Overwriting congruence rule for " ^ quote (#2 a))
   701         else ();
   702       val xs' = AList.update (op =) (a, thm) xs;
   703       val weak' = if is_full_cong thm then weak else a :: weak;
   704     in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) end);
   705 
   706 fun del_eqcong thm ctxt = ctxt |> map_simpset2
   707   (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   708     let
   709       val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
   710         handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", thm);
   711     (*val lhs = Envir.eta_contract lhs;*)
   712       val a = the (cong_name (head_of lhs)) handle Option.Option =>
   713         raise SIMPLIFIER ("Congruence must start with a constant", thm);
   714       val (xs, _) = congs;
   715       val xs' = filter_out (fn (x : cong_name, _) => x = a) xs;
   716       val weak' = xs' |> map_filter (fn (a, thm) =>
   717         if is_full_cong thm then NONE else SOME a);
   718     in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) end);
   719 
   720 fun add_cong thm ctxt = add_eqcong (mk_cong ctxt thm) ctxt;
   721 fun del_cong thm ctxt = del_eqcong (mk_cong ctxt thm) ctxt;
   722 
   723 end;
   724 
   725 
   726 (* simprocs *)
   727 
   728 datatype simproc =
   729   Simproc of
   730     {name: string,
   731      lhss: cterm list,
   732      proc: morphism -> Proof.context -> cterm -> thm option,
   733      id: stamp * thm list};
   734 
   735 fun eq_simproc (Simproc {id = id1, ...}, Simproc {id = id2, ...}) = eq_procid (id1, id2);
   736 
   737 fun transform_simproc phi (Simproc {name, lhss, proc, id = (s, ths)}) =
   738   Simproc
   739    {name = name,
   740     lhss = map (Morphism.cterm phi) lhss,
   741     proc = Morphism.transform phi proc,
   742     id = (s, Morphism.fact phi ths)};
   743 
   744 fun make_simproc {name, lhss, proc, identifier} =
   745   Simproc {name = name, lhss = lhss, proc = proc, id = (stamp (), identifier)};
   746 
   747 fun mk_simproc name lhss proc =
   748   make_simproc {name = name, lhss = lhss, proc = fn _ => fn ctxt => fn ct =>
   749     proc ctxt (Thm.term_of ct), identifier = []};
   750 
   751 (* FIXME avoid global thy and Logic.varify_global *)
   752 fun simproc_global_i thy name = mk_simproc name o map (Thm.cterm_of thy o Logic.varify_global);
   753 fun simproc_global thy name = simproc_global_i thy name o map (Syntax.read_term_global thy);
   754 
   755 
   756 local
   757 
   758 fun add_proc (proc as Proc {name, lhs, ...}) ctxt =
   759  (trace_cterm ctxt false (fn () => "Adding simplification procedure " ^ quote name ^ " for") lhs;
   760   ctxt |> map_simpset2
   761     (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   762       (congs, Net.insert_term eq_proc (term_of lhs, proc) procs,
   763         mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops))
   764   handle Net.INSERT =>
   765     (Context_Position.if_visible ctxt
   766       warning ("Ignoring duplicate simplification procedure " ^ quote name); ctxt));
   767 
   768 fun del_proc (proc as Proc {name, lhs, ...}) ctxt =
   769   ctxt |> map_simpset2
   770     (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   771       (congs, Net.delete_term eq_proc (term_of lhs, proc) procs,
   772         mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops))
   773   handle Net.DELETE =>
   774     (Context_Position.if_visible ctxt
   775       warning ("Simplification procedure " ^ quote name ^ " not in simpset"); ctxt);
   776 
   777 fun prep_procs (Simproc {name, lhss, proc, id}) =
   778   lhss |> map (fn lhs => Proc {name = name, lhs = lhs, proc = Morphism.form proc, id = id});
   779 
   780 in
   781 
   782 fun ctxt addsimprocs ps = fold (fold add_proc o prep_procs) ps ctxt;
   783 fun ctxt delsimprocs ps = fold (fold del_proc o prep_procs) ps ctxt;
   784 
   785 end;
   786 
   787 
   788 (* mk_rews *)
   789 
   790 local
   791 
   792 fun map_mk_rews f =
   793   map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   794     let
   795       val {mk, mk_cong, mk_sym, mk_eq_True, reorient} = mk_rews;
   796       val (mk', mk_cong', mk_sym', mk_eq_True', reorient') =
   797         f (mk, mk_cong, mk_sym, mk_eq_True, reorient);
   798       val mk_rews' = {mk = mk', mk_cong = mk_cong', mk_sym = mk_sym', mk_eq_True = mk_eq_True',
   799         reorient = reorient'};
   800     in (congs, procs, mk_rews', termless, subgoal_tac, loop_tacs, solvers, trace_ops) end);
   801 
   802 in
   803 
   804 fun mksimps ctxt =
   805   let val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt
   806   in mk ctxt end;
   807 
   808 fun set_mksimps mk = map_mk_rews (fn (_, mk_cong, mk_sym, mk_eq_True, reorient) =>
   809   (mk, mk_cong, mk_sym, mk_eq_True, reorient));
   810 
   811 fun set_mkcong mk_cong = map_mk_rews (fn (mk, _, mk_sym, mk_eq_True, reorient) =>
   812   (mk, mk_cong, mk_sym, mk_eq_True, reorient));
   813 
   814 fun set_mksym mk_sym = map_mk_rews (fn (mk, mk_cong, _, mk_eq_True, reorient) =>
   815   (mk, mk_cong, mk_sym, mk_eq_True, reorient));
   816 
   817 fun set_mkeqTrue mk_eq_True = map_mk_rews (fn (mk, mk_cong, mk_sym, _, reorient) =>
   818   (mk, mk_cong, mk_sym, mk_eq_True, reorient));
   819 
   820 fun set_reorient reorient = map_mk_rews (fn (mk, mk_cong, mk_sym, mk_eq_True, _) =>
   821   (mk, mk_cong, mk_sym, mk_eq_True, reorient));
   822 
   823 end;
   824 
   825 
   826 (* termless *)
   827 
   828 fun set_termless termless =
   829   map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   830    (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   831 
   832 
   833 (* tactics *)
   834 
   835 fun set_subgoaler subgoal_tac =
   836   map_simpset2 (fn (congs, procs, mk_rews, termless, _, loop_tacs, solvers, trace_ops) =>
   837    (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   838 
   839 fun ctxt setloop tac = ctxt |>
   840   map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, _, solvers, trace_ops) =>
   841    (congs, procs, mk_rews, termless, subgoal_tac, [("", tac)], solvers, trace_ops));
   842 
   843 fun ctxt addloop (name, tac) = ctxt |>
   844   map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   845     (congs, procs, mk_rews, termless, subgoal_tac,
   846      AList.update (op =) (name, tac) loop_tacs, solvers, trace_ops));
   847 
   848 fun ctxt delloop name = ctxt |>
   849   map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   850     (congs, procs, mk_rews, termless, subgoal_tac,
   851      (if AList.defined (op =) loop_tacs name then ()
   852       else
   853         Context_Position.if_visible ctxt
   854           warning ("No such looper in simpset: " ^ quote name);
   855         AList.delete (op =) name loop_tacs), solvers, trace_ops));
   856 
   857 fun ctxt setSSolver solver = ctxt |> map_simpset2
   858   (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, _), trace_ops) =>
   859     (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, [solver]), trace_ops));
   860 
   861 fun ctxt addSSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
   862   subgoal_tac, loop_tacs, (unsafe_solvers, solvers), trace_ops) => (congs, procs, mk_rews, termless,
   863     subgoal_tac, loop_tacs, (unsafe_solvers, insert eq_solver solver solvers), trace_ops));
   864 
   865 fun ctxt setSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
   866   subgoal_tac, loop_tacs, (_, solvers), trace_ops) => (congs, procs, mk_rews, termless,
   867     subgoal_tac, loop_tacs, ([solver], solvers), trace_ops));
   868 
   869 fun ctxt addSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
   870   subgoal_tac, loop_tacs, (unsafe_solvers, solvers), trace_ops) => (congs, procs, mk_rews, termless,
   871     subgoal_tac, loop_tacs, (insert eq_solver solver unsafe_solvers, solvers), trace_ops));
   872 
   873 fun set_solvers solvers = map_simpset2 (fn (congs, procs, mk_rews, termless,
   874   subgoal_tac, loop_tacs, _, trace_ops) => (congs, procs, mk_rews, termless,
   875   subgoal_tac, loop_tacs, (solvers, solvers), trace_ops));
   876 
   877 
   878 (* trace operations *)
   879 
   880 fun set_trace_ops trace_ops =
   881   map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, _) =>
   882     (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   883 
   884 val trace_ops = #trace_ops o internal_ss o simpset_of;
   885 fun trace_invoke args ctxt = #trace_invoke (trace_ops ctxt) args ctxt;
   886 fun trace_apply args ctxt = #trace_apply (trace_ops ctxt) args ctxt;
   887 
   888 
   889 
   890 (** rewriting **)
   891 
   892 (*
   893   Uses conversions, see:
   894     L C Paulson, A higher-order implementation of rewriting,
   895     Science of Computer Programming 3 (1983), pages 119-149.
   896 *)
   897 
   898 fun check_conv ctxt msg thm thm' =
   899   let
   900     val thm'' = Thm.transitive thm thm' handle THM _ =>
   901      Thm.transitive thm (Thm.transitive
   902        (Thm.symmetric (Drule.beta_eta_conversion (Thm.lhs_of thm'))) thm')
   903   in if msg then trace_thm ctxt (fn () => "SUCCEEDED") thm' else (); SOME thm'' end
   904   handle THM _ =>
   905     let
   906       val _ $ _ $ prop0 = Thm.prop_of thm;
   907     in
   908       trace_thm ctxt (fn () => "Proved wrong thm (Check subgoaler?)") thm';
   909       trace_term ctxt false (fn () => "Should have proved:") prop0;
   910       NONE
   911     end;
   912 
   913 
   914 (* mk_procrule *)
   915 
   916 fun mk_procrule ctxt thm =
   917   let val (prems, lhs, elhs, rhs, _) = decomp_simp thm in
   918     if rewrite_rule_extra_vars prems lhs rhs
   919     then (cond_warn_thm ctxt (fn () => "Extra vars on rhs:") thm; [])
   920     else [mk_rrule2 {thm = thm, name = "", lhs = lhs, elhs = elhs, perm = false}]
   921   end;
   922 
   923 
   924 (* rewritec: conversion to apply the meta simpset to a term *)
   925 
   926 (*Since the rewriting strategy is bottom-up, we avoid re-normalizing already
   927   normalized terms by carrying around the rhs of the rewrite rule just
   928   applied. This is called the `skeleton'. It is decomposed in parallel
   929   with the term. Once a Var is encountered, the corresponding term is
   930   already in normal form.
   931   skel0 is a dummy skeleton that is to enforce complete normalization.*)
   932 
   933 val skel0 = Bound 0;
   934 
   935 (*Use rhs as skeleton only if the lhs does not contain unnormalized bits.
   936   The latter may happen iff there are weak congruence rules for constants
   937   in the lhs.*)
   938 
   939 fun uncond_skel ((_, weak), (lhs, rhs)) =
   940   if null weak then rhs  (*optimization*)
   941   else if exists_subterm
   942     (fn Const (a, _) => member (op =) weak (true, a)
   943       | Free (a, _) => member (op =) weak (false, a)
   944       | _ => false) lhs then skel0
   945   else rhs;
   946 
   947 (*Behaves like unconditional rule if rhs does not contain vars not in the lhs.
   948   Otherwise those vars may become instantiated with unnormalized terms
   949   while the premises are solved.*)
   950 
   951 fun cond_skel (args as (_, (lhs, rhs))) =
   952   if subset (op =) (Term.add_vars rhs [], Term.add_vars lhs []) then uncond_skel args
   953   else skel0;
   954 
   955 (*
   956   Rewriting -- we try in order:
   957     (1) beta reduction
   958     (2) unconditional rewrite rules
   959     (3) conditional rewrite rules
   960     (4) simplification procedures
   961 
   962   IMPORTANT: rewrite rules must not introduce new Vars or TVars!
   963 *)
   964 
   965 fun rewritec (prover, maxt) ctxt t =
   966   let
   967     val Simpset ({rules, ...}, {congs, procs, termless, ...}) = simpset_of ctxt;
   968     val eta_thm = Thm.eta_conversion t;
   969     val eta_t' = Thm.rhs_of eta_thm;
   970     val eta_t = term_of eta_t';
   971     fun rew {thm, name, lhs, elhs, extra, fo, perm} =
   972       let
   973         val prop = Thm.prop_of thm;
   974         val (rthm, elhs') =
   975           if maxt = ~1 orelse not extra then (thm, elhs)
   976           else (Thm.incr_indexes (maxt + 1) thm, Thm.incr_indexes_cterm (maxt + 1) elhs);
   977         val insts =
   978           if fo then Thm.first_order_match (elhs', eta_t')
   979           else Thm.match (elhs', eta_t');
   980         val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm);
   981         val prop' = Thm.prop_of thm';
   982         val unconditional = (Logic.count_prems prop' = 0);
   983         val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop');
   984         val trace_args = {unconditional = unconditional, term = eta_t, thm = thm', name = name};
   985       in
   986         if perm andalso not (termless (rhs', lhs'))
   987         then
   988          (trace_named_thm ctxt (fn () => "Cannot apply permutative rewrite rule") (thm, name);
   989           trace_thm ctxt (fn () => "Term does not become smaller:") thm';
   990           NONE)
   991         else
   992          (trace_named_thm ctxt (fn () => "Applying instance of rewrite rule") (thm, name);
   993           if unconditional
   994           then
   995            (trace_thm ctxt (fn () => "Rewriting:") thm';
   996             trace_apply trace_args ctxt (fn ctxt' =>
   997               let
   998                 val lr = Logic.dest_equals prop;
   999                 val SOME thm'' = check_conv ctxt' false eta_thm thm';
  1000               in SOME (thm'', uncond_skel (congs, lr)) end))
  1001           else
  1002            (trace_thm ctxt (fn () => "Trying to rewrite:") thm';
  1003             if simp_depth ctxt > Config.get ctxt simp_depth_limit
  1004             then
  1005               let
  1006                 val s = "simp_depth_limit exceeded - giving up";
  1007                 val _ = trace ctxt false (fn () => s);
  1008                 val _ = Context_Position.if_visible ctxt warning s;
  1009               in NONE end
  1010             else
  1011               trace_apply trace_args ctxt (fn ctxt' =>
  1012                 (case prover ctxt' thm' of
  1013                   NONE => (trace_thm ctxt' (fn () => "FAILED") thm'; NONE)
  1014                 | SOME thm2 =>
  1015                     (case check_conv ctxt' true eta_thm thm2 of
  1016                       NONE => NONE
  1017                     | SOME thm2' =>
  1018                         let
  1019                           val concl = Logic.strip_imp_concl prop;
  1020                           val lr = Logic.dest_equals concl;
  1021                         in SOME (thm2', cond_skel (congs, lr)) end)))))
  1022       end;
  1023 
  1024     fun rews [] = NONE
  1025       | rews (rrule :: rrules) =
  1026           let val opt = rew rrule handle Pattern.MATCH => NONE
  1027           in (case opt of NONE => rews rrules | some => some) end;
  1028 
  1029     fun sort_rrules rrs =
  1030       let
  1031         fun is_simple ({thm, ...}: rrule) =
  1032           (case Thm.prop_of thm of
  1033             Const ("==", _) $ _ $ _ => true
  1034           | _ => false);
  1035         fun sort [] (re1, re2) = re1 @ re2
  1036           | sort (rr :: rrs) (re1, re2) =
  1037               if is_simple rr
  1038               then sort rrs (rr :: re1, re2)
  1039               else sort rrs (re1, rr :: re2);
  1040       in sort rrs ([], []) end;
  1041 
  1042     fun proc_rews [] = NONE
  1043       | proc_rews (Proc {name, proc, lhs, ...} :: ps) =
  1044           if Pattern.matches (Proof_Context.theory_of ctxt) (Thm.term_of lhs, Thm.term_of t) then
  1045             (debug_term ctxt false (fn () => "Trying procedure " ^ quote name ^ " on:") eta_t;
  1046              (case proc ctxt eta_t' of
  1047                NONE => (debug ctxt false (fn () => "FAILED"); proc_rews ps)
  1048              | SOME raw_thm =>
  1049                  (trace_thm ctxt (fn () => "Procedure " ^ quote name ^ " produced rewrite rule:")
  1050                    raw_thm;
  1051                   (case rews (mk_procrule ctxt raw_thm) of
  1052                     NONE => (trace_cterm ctxt true (fn () => "IGNORED result of simproc " ^ quote name ^
  1053                       " -- does not match") t; proc_rews ps)
  1054                   | some => some))))
  1055           else proc_rews ps;
  1056   in
  1057     (case eta_t of
  1058       Abs _ $ _ => SOME (Thm.transitive eta_thm (Thm.beta_conversion false eta_t'), skel0)
  1059     | _ =>
  1060       (case rews (sort_rrules (Net.match_term rules eta_t)) of
  1061         NONE => proc_rews (Net.match_term procs eta_t)
  1062       | some => some))
  1063   end;
  1064 
  1065 
  1066 (* conversion to apply a congruence rule to a term *)
  1067 
  1068 fun congc prover ctxt maxt cong t =
  1069   let
  1070     val rthm = Thm.incr_indexes (maxt + 1) cong;
  1071     val rlhs = fst (Thm.dest_equals (Drule.strip_imp_concl (cprop_of rthm)));
  1072     val insts = Thm.match (rlhs, t)
  1073     (* Thm.match can raise Pattern.MATCH;
  1074        is handled when congc is called *)
  1075     val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm);
  1076     val _ = trace_thm ctxt (fn () => "Applying congruence rule:") thm';
  1077     fun err (msg, thm) = (trace_thm ctxt (fn () => msg) thm; NONE);
  1078   in
  1079     (case prover thm' of
  1080       NONE => err ("Congruence proof failed.  Could not prove", thm')
  1081     | SOME thm2 =>
  1082         (case check_conv ctxt true (Drule.beta_eta_conversion t) thm2 of
  1083           NONE => err ("Congruence proof failed.  Should not have proved", thm2)
  1084         | SOME thm2' =>
  1085             if op aconv (pairself term_of (Thm.dest_equals (cprop_of thm2')))
  1086             then NONE else SOME thm2'))
  1087   end;
  1088 
  1089 val (cA, (cB, cC)) =
  1090   apsnd Thm.dest_equals (Thm.dest_implies (hd (cprems_of Drule.imp_cong)));
  1091 
  1092 fun transitive1 NONE NONE = NONE
  1093   | transitive1 (SOME thm1) NONE = SOME thm1
  1094   | transitive1 NONE (SOME thm2) = SOME thm2
  1095   | transitive1 (SOME thm1) (SOME thm2) = SOME (Thm.transitive thm1 thm2);
  1096 
  1097 fun transitive2 thm = transitive1 (SOME thm);
  1098 fun transitive3 thm = transitive1 thm o SOME;
  1099 
  1100 fun bottomc ((simprem, useprem, mutsimp), prover, maxidx) =
  1101   let
  1102     fun botc skel ctxt t =
  1103       if is_Var skel then NONE
  1104       else
  1105         (case subc skel ctxt t of
  1106            some as SOME thm1 =>
  1107              (case rewritec (prover, maxidx) ctxt (Thm.rhs_of thm1) of
  1108                 SOME (thm2, skel2) =>
  1109                   transitive2 (Thm.transitive thm1 thm2)
  1110                     (botc skel2 ctxt (Thm.rhs_of thm2))
  1111               | NONE => some)
  1112          | NONE =>
  1113              (case rewritec (prover, maxidx) ctxt t of
  1114                 SOME (thm2, skel2) => transitive2 thm2
  1115                   (botc skel2 ctxt (Thm.rhs_of thm2))
  1116               | NONE => NONE))
  1117 
  1118     and try_botc ctxt t =
  1119       (case botc skel0 ctxt t of
  1120         SOME trec1 => trec1
  1121       | NONE => Thm.reflexive t)
  1122 
  1123     and subc skel ctxt t0 =
  1124         let val Simpset ({bounds, ...}, {congs, ...}) = simpset_of ctxt in
  1125           (case term_of t0 of
  1126               Abs (a, T, _) =>
  1127                 let
  1128                     val b = Name.bound (#1 bounds);
  1129                     val (v, t') = Thm.dest_abs (SOME b) t0;
  1130                     val b' = #1 (Term.dest_Free (Thm.term_of v));
  1131                     val _ =
  1132                       if b <> b' then
  1133                         warning ("Simplifier: renamed bound variable " ^
  1134                           quote b ^ " to " ^ quote b' ^ Position.here (Position.thread_data ()))
  1135                       else ();
  1136                     val ctxt' = add_bound ((b', T), a) ctxt;
  1137                     val skel' = (case skel of Abs (_, _, sk) => sk | _ => skel0);
  1138                 in
  1139                   (case botc skel' ctxt' t' of
  1140                     SOME thm => SOME (Thm.abstract_rule a v thm)
  1141                   | NONE => NONE)
  1142                 end
  1143             | t $ _ =>
  1144               (case t of
  1145                 Const ("==>", _) $ _  => impc t0 ctxt
  1146               | Abs _ =>
  1147                   let val thm = Thm.beta_conversion false t0
  1148                   in
  1149                     (case subc skel0 ctxt (Thm.rhs_of thm) of
  1150                       NONE => SOME thm
  1151                     | SOME thm' => SOME (Thm.transitive thm thm'))
  1152                   end
  1153               | _  =>
  1154                   let
  1155                     fun appc () =
  1156                       let
  1157                         val (tskel, uskel) =
  1158                           (case skel of
  1159                             tskel $ uskel => (tskel, uskel)
  1160                           | _ => (skel0, skel0));
  1161                         val (ct, cu) = Thm.dest_comb t0;
  1162                       in
  1163                         (case botc tskel ctxt ct of
  1164                           SOME thm1 =>
  1165                             (case botc uskel ctxt cu of
  1166                               SOME thm2 => SOME (Thm.combination thm1 thm2)
  1167                             | NONE => SOME (Thm.combination thm1 (Thm.reflexive cu)))
  1168                         | NONE =>
  1169                             (case botc uskel ctxt cu of
  1170                               SOME thm1 => SOME (Thm.combination (Thm.reflexive ct) thm1)
  1171                             | NONE => NONE))
  1172                       end;
  1173                     val (h, ts) = strip_comb t;
  1174                   in
  1175                     (case cong_name h of
  1176                       SOME a =>
  1177                         (case AList.lookup (op =) (fst congs) a of
  1178                            NONE => appc ()
  1179                         | SOME cong =>
  1180      (*post processing: some partial applications h t1 ... tj, j <= length ts,
  1181        may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*)
  1182                            (let
  1183                               val thm = congc (prover ctxt) ctxt maxidx cong t0;
  1184                               val t = the_default t0 (Option.map Thm.rhs_of thm);
  1185                               val (cl, cr) = Thm.dest_comb t
  1186                               val dVar = Var(("", 0), dummyT)
  1187                               val skel =
  1188                                 list_comb (h, replicate (length ts) dVar)
  1189                             in
  1190                               (case botc skel ctxt cl of
  1191                                 NONE => thm
  1192                               | SOME thm' =>
  1193                                   transitive3 thm (Thm.combination thm' (Thm.reflexive cr)))
  1194                             end handle Pattern.MATCH => appc ()))
  1195                      | _ => appc ())
  1196                   end)
  1197             | _ => NONE)
  1198         end
  1199     and impc ct ctxt =
  1200       if mutsimp then mut_impc0 [] ct [] [] ctxt
  1201       else nonmut_impc ct ctxt
  1202 
  1203     and rules_of_prem ctxt prem =
  1204       if maxidx_of_term (term_of prem) <> ~1
  1205       then (trace_cterm ctxt true
  1206         (fn () => "Cannot add premise as rewrite rule because it contains (type) unknowns:")
  1207         prem; ([], NONE))
  1208       else
  1209         let val asm = Thm.assume prem
  1210         in (extract_safe_rrules (ctxt, asm), SOME asm) end
  1211 
  1212     and add_rrules (rrss, asms) ctxt =
  1213       (fold o fold) insert_rrule rrss ctxt |> add_prems (map_filter I asms)
  1214 
  1215     and disch r prem eq =
  1216       let
  1217         val (lhs, rhs) = Thm.dest_equals (Thm.cprop_of eq);
  1218         val eq' =
  1219           Thm.implies_elim
  1220             (Thm.instantiate ([], [(cA, prem), (cB, lhs), (cC, rhs)]) Drule.imp_cong)
  1221             (Thm.implies_intr prem eq);
  1222       in
  1223         if not r then eq'
  1224         else
  1225           let
  1226             val (prem', concl) = Thm.dest_implies lhs;
  1227             val (prem'', _) = Thm.dest_implies rhs;
  1228           in
  1229             Thm.transitive
  1230               (Thm.transitive
  1231                 (Thm.instantiate ([], [(cA, prem'), (cB, prem), (cC, concl)]) Drule.swap_prems_eq)
  1232                 eq')
  1233               (Thm.instantiate ([], [(cA, prem), (cB, prem''), (cC, concl)]) Drule.swap_prems_eq)
  1234           end
  1235       end
  1236 
  1237     and rebuild [] _ _ _ _ eq = eq
  1238       | rebuild (prem :: prems) concl (_ :: rrss) (_ :: asms) ctxt eq =
  1239           let
  1240             val ctxt' = add_rrules (rev rrss, rev asms) ctxt;
  1241             val concl' =
  1242               Drule.mk_implies (prem, the_default concl (Option.map Thm.rhs_of eq));
  1243             val dprem = Option.map (disch false prem);
  1244           in
  1245             (case rewritec (prover, maxidx) ctxt' concl' of
  1246               NONE => rebuild prems concl' rrss asms ctxt (dprem eq)
  1247             | SOME (eq', _) =>
  1248                 transitive2 (fold (disch false) prems (the (transitive3 (dprem eq) eq')))
  1249                   (mut_impc0 (rev prems) (Thm.rhs_of eq') (rev rrss) (rev asms) ctxt))
  1250           end
  1251 
  1252     and mut_impc0 prems concl rrss asms ctxt =
  1253       let
  1254         val prems' = strip_imp_prems concl;
  1255         val (rrss', asms') = split_list (map (rules_of_prem ctxt) prems');
  1256       in
  1257         mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss')
  1258           (asms @ asms') [] [] [] [] ctxt ~1 ~1
  1259       end
  1260 
  1261     and mut_impc [] concl [] [] prems' rrss' asms' eqns ctxt changed k =
  1262         transitive1 (fold (fn (eq1, prem) => fn eq2 => transitive1 eq1
  1263             (Option.map (disch false prem) eq2)) (eqns ~~ prems') NONE)
  1264           (if changed > 0 then
  1265              mut_impc (rev prems') concl (rev rrss') (rev asms')
  1266                [] [] [] [] ctxt ~1 changed
  1267            else rebuild prems' concl rrss' asms' ctxt
  1268              (botc skel0 (add_rrules (rev rrss', rev asms') ctxt) concl))
  1269 
  1270       | mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms)
  1271           prems' rrss' asms' eqns ctxt changed k =
  1272         (case (if k = 0 then NONE else botc skel0 (add_rrules
  1273           (rev rrss' @ rrss, rev asms' @ asms) ctxt) prem) of
  1274             NONE => mut_impc prems concl rrss asms (prem :: prems')
  1275               (rrs :: rrss') (asm :: asms') (NONE :: eqns) ctxt changed
  1276               (if k = 0 then 0 else k - 1)
  1277         | SOME eqn =>
  1278             let
  1279               val prem' = Thm.rhs_of eqn;
  1280               val tprems = map term_of prems;
  1281               val i = 1 + fold Integer.max (map (fn p =>
  1282                 find_index (fn q => q aconv p) tprems) (Thm.hyps_of eqn)) ~1;
  1283               val (rrs', asm') = rules_of_prem ctxt prem';
  1284             in
  1285               mut_impc prems concl rrss asms (prem' :: prems')
  1286                 (rrs' :: rrss') (asm' :: asms')
  1287                 (SOME (fold_rev (disch true)
  1288                   (take i prems)
  1289                   (Drule.imp_cong_rule eqn (Thm.reflexive (Drule.list_implies
  1290                     (drop i prems, concl))))) :: eqns)
  1291                 ctxt (length prems') ~1
  1292             end)
  1293 
  1294     (*legacy code -- only for backwards compatibility*)
  1295     and nonmut_impc ct ctxt =
  1296       let
  1297         val (prem, conc) = Thm.dest_implies ct;
  1298         val thm1 = if simprem then botc skel0 ctxt prem else NONE;
  1299         val prem1 = the_default prem (Option.map Thm.rhs_of thm1);
  1300         val ctxt1 =
  1301           if not useprem then ctxt
  1302           else add_rrules (apsnd single (apfst single (rules_of_prem ctxt prem1))) ctxt
  1303       in
  1304         (case botc skel0 ctxt1 conc of
  1305           NONE =>
  1306             (case thm1 of
  1307               NONE => NONE
  1308             | SOME thm1' => SOME (Drule.imp_cong_rule thm1' (Thm.reflexive conc)))
  1309         | SOME thm2 =>
  1310             let val thm2' = disch false prem1 thm2 in
  1311               (case thm1 of
  1312                 NONE => SOME thm2'
  1313               | SOME thm1' =>
  1314                  SOME (Thm.transitive (Drule.imp_cong_rule thm1' (Thm.reflexive conc)) thm2'))
  1315             end)
  1316       end;
  1317 
  1318   in try_botc end;
  1319 
  1320 
  1321 (* Meta-rewriting: rewrites t to u and returns the theorem t==u *)
  1322 
  1323 (*
  1324   Parameters:
  1325     mode = (simplify A,
  1326             use A in simplifying B,
  1327             use prems of B (if B is again a meta-impl.) to simplify A)
  1328            when simplifying A ==> B
  1329     prover: how to solve premises in conditional rewrites and congruences
  1330 *)
  1331 
  1332 val debug_bounds = Unsynchronized.ref false;
  1333 
  1334 fun check_bounds ctxt ct =
  1335   if ! debug_bounds then
  1336     let
  1337       val Simpset ({bounds = (_, bounds), ...}, _) = simpset_of ctxt;
  1338       val bs =
  1339         fold_aterms
  1340           (fn Free (x, _) =>
  1341             if Name.is_bound x andalso not (AList.defined eq_bound bounds x)
  1342             then insert (op =) x else I
  1343           | _ => I) (term_of ct) [];
  1344     in
  1345       if null bs then ()
  1346       else
  1347         print_term ctxt true
  1348           (fn () => "Simplifier: term contains loose bounds: " ^ commas_quote bs)
  1349           (Thm.term_of ct)
  1350     end
  1351   else ();
  1352 
  1353 fun rewrite_cterm mode prover raw_ctxt raw_ct =
  1354   let
  1355     val thy = Proof_Context.theory_of raw_ctxt;
  1356 
  1357     val ct = Thm.adjust_maxidx_cterm ~1 raw_ct;
  1358     val {maxidx, ...} = Thm.rep_cterm ct;
  1359     val _ =
  1360       Theory.subthy (theory_of_cterm ct, thy) orelse
  1361         raise CTERM ("rewrite_cterm: bad background theory", [ct]);
  1362 
  1363     val ctxt =
  1364       raw_ctxt
  1365       |> Context_Position.set_visible false
  1366       |> inc_simp_depth
  1367       |> (fn ctxt => trace_invoke {depth = simp_depth ctxt, term = Thm.term_of ct} ctxt);
  1368 
  1369     val _ = trace_cterm ctxt false (fn () => "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:") ct;
  1370     val _ = check_bounds ctxt ct;
  1371   in bottomc (mode, Option.map Drule.flexflex_unique oo prover, maxidx) ctxt ct end;
  1372 
  1373 val simple_prover =
  1374   SINGLE o (fn ctxt => ALLGOALS (resolve_tac (prems_of ctxt)));
  1375 
  1376 fun rewrite _ [] ct = Thm.reflexive ct
  1377   | rewrite full thms ct =
  1378       rewrite_cterm (full, false, false) simple_prover
  1379         (global_context (Thm.theory_of_cterm ct) empty_ss addsimps thms) ct;
  1380 
  1381 val rewrite_rule = Conv.fconv_rule o rewrite true;
  1382 
  1383 (*simple term rewriting -- no proof*)
  1384 fun rewrite_term thy rules procs =
  1385   Pattern.rewrite_term thy (map decomp_simp' rules) procs;
  1386 
  1387 fun rewrite_thm mode prover ctxt = Conv.fconv_rule (rewrite_cterm mode prover ctxt);
  1388 
  1389 (*Rewrite the subgoals of a proof state (represented by a theorem)*)
  1390 fun rewrite_goals_rule thms th =
  1391   Conv.fconv_rule (Conv.prems_conv ~1 (rewrite_cterm (true, true, true) simple_prover
  1392     (global_context (Thm.theory_of_thm th) empty_ss addsimps thms))) th;
  1393 
  1394 
  1395 (** meta-rewriting tactics **)
  1396 
  1397 (*Rewrite all subgoals*)
  1398 fun rewrite_goals_tac defs = PRIMITIVE (rewrite_goals_rule defs);
  1399 
  1400 (*Rewrite one subgoal*)
  1401 fun generic_rewrite_goal_tac mode prover_tac ctxt i thm =
  1402   if 0 < i andalso i <= Thm.nprems_of thm then
  1403     Seq.single (Conv.gconv_rule (rewrite_cterm mode (SINGLE o prover_tac) ctxt) i thm)
  1404   else Seq.empty;
  1405 
  1406 fun rewrite_goal_tac rews i st =
  1407   generic_rewrite_goal_tac (true, false, false) (K no_tac)
  1408     (global_context (Thm.theory_of_thm st) empty_ss addsimps rews) i st;
  1409 
  1410 (*Prunes all redundant parameters from the proof state by rewriting.*)
  1411 val prune_params_tac = rewrite_goals_tac [Drule.triv_forall_equality];
  1412 
  1413 
  1414 (* for folding definitions, handling critical pairs *)
  1415 
  1416 (*The depth of nesting in a term*)
  1417 fun term_depth (Abs (_, _, t)) = 1 + term_depth t
  1418   | term_depth (f $ t) = 1 + Int.max (term_depth f, term_depth t)
  1419   | term_depth _ = 0;
  1420 
  1421 val lhs_of_thm = #1 o Logic.dest_equals o prop_of;
  1422 
  1423 (*folding should handle critical pairs!  E.g. K == Inl(0),  S == Inr(Inl(0))
  1424   Returns longest lhs first to avoid folding its subexpressions.*)
  1425 fun sort_lhs_depths defs =
  1426   let val keylist = AList.make (term_depth o lhs_of_thm) defs
  1427       val keys = sort_distinct (rev_order o int_ord) (map #2 keylist)
  1428   in map (AList.find (op =) keylist) keys end;
  1429 
  1430 val rev_defs = sort_lhs_depths o map Thm.symmetric;
  1431 
  1432 fun fold_rule defs = fold rewrite_rule (rev_defs defs);
  1433 fun fold_goals_tac defs = EVERY (map rewrite_goals_tac (rev_defs defs));
  1434 
  1435 
  1436 (* HHF normal form: !! before ==>, outermost !! generalized *)
  1437 
  1438 local
  1439 
  1440 fun gen_norm_hhf ss th =
  1441   (if Drule.is_norm_hhf (Thm.prop_of th) then th
  1442    else
  1443     Conv.fconv_rule
  1444       (rewrite_cterm (true, false, false) (K (K NONE))
  1445         (global_context (Thm.theory_of_thm th) ss)) th)
  1446   |> Thm.adjust_maxidx_thm ~1
  1447   |> Drule.gen_all;
  1448 
  1449 val hhf_ss =
  1450   simpset_of (empty_simpset (Context.proof_of (Context.the_thread_data ()))
  1451     addsimps Drule.norm_hhf_eqs);
  1452 
  1453 val hhf_protect_ss =
  1454   simpset_of (empty_simpset (Context.proof_of (Context.the_thread_data ()))
  1455     addsimps Drule.norm_hhf_eqs |> add_eqcong Drule.protect_cong);
  1456 
  1457 in
  1458 
  1459 val norm_hhf = gen_norm_hhf hhf_ss;
  1460 val norm_hhf_protect = gen_norm_hhf hhf_protect_ss;
  1461 
  1462 end;
  1463 
  1464 end;
  1465 
  1466 structure Basic_Meta_Simplifier: BASIC_RAW_SIMPLIFIER = Raw_Simplifier;
  1467 open Basic_Meta_Simplifier;