src/Pure/meta_simplifier.ML
changeset 15011 35be762f58f9
parent 15006 107e4dfd3b96
child 15023 0e4689f411d5
equal deleted inserted replaced
15010:72fbe711e414 15011:35be762f58f9
    18 end;
    18 end;
    19 
    19 
    20 signature AUX_SIMPLIFIER =
    20 signature AUX_SIMPLIFIER =
    21 sig
    21 sig
    22   type meta_simpset
    22   type meta_simpset
       
    23   type simpset
    23   type simproc
    24   type simproc
       
    25   val full_mk_simproc: string -> cterm list
       
    26     -> (simpset -> Sign.sg -> thm list -> term -> thm option) -> simproc
    24   val mk_simproc: string -> cterm list
    27   val mk_simproc: string -> cterm list
    25     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
    28     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
    26   type solver
    29   type solver
    27   val mk_solver: string -> (thm list -> int -> tactic) -> solver
    30   val mk_solver: string -> (thm list -> int -> tactic) -> solver
    28   type simpset
       
    29   val empty_ss: simpset
    31   val empty_ss: simpset
    30   val rep_ss: simpset ->
    32   val rep_ss: simpset ->
    31    {mss: meta_simpset,
    33    {mss: meta_simpset,
    32     mk_cong: thm -> thm,
    34     mk_cong: thm -> thm,
    33     subgoal_tac: simpset -> int -> tactic,
    35     subgoal_tac: simpset -> int -> tactic,
    34     loop_tacs: (string * (int -> tactic)) list,
    36     loop_tacs: (string * (int -> tactic)) list,
    35     unsafe_solvers: solver list,
    37     unsafe_solvers: solver list,
    36     solvers: solver list};
    38     solvers: solver list}
       
    39   val from_mss: meta_simpset -> simpset
       
    40   val ss_of            : thm list -> simpset
    37   val print_ss: simpset -> unit
    41   val print_ss: simpset -> unit
    38   val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
    42   val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
    39   val setloop:      simpset *             (int -> tactic) -> simpset
    43   val setloop:      simpset *             (int -> tactic) -> simpset
    40   val addloop:      simpset *  (string * (int -> tactic)) -> simpset
    44   val addloop:      simpset *  (string * (int -> tactic)) -> simpset
    41   val delloop:      simpset *   string                    -> simpset
    45   val delloop:      simpset *   string                    -> simpset
    61   val generic_simp_tac: bool -> bool * bool * bool -> simpset -> int -> tactic
    65   val generic_simp_tac: bool -> bool * bool * bool -> simpset -> int -> tactic
    62   val simproc: Sign.sg -> string -> string list
    66   val simproc: Sign.sg -> string -> string list
    63     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
    67     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
    64   val simproc_i: Sign.sg -> string -> term list
    68   val simproc_i: Sign.sg -> string -> term list
    65     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
    69     -> (Sign.sg -> thm list -> term -> thm option) -> simproc
       
    70   val full_simproc: Sign.sg -> string -> string list
       
    71     -> (simpset -> Sign.sg -> thm list -> term -> thm option) -> simproc
       
    72   val full_simproc_i: Sign.sg -> string -> term list
       
    73     -> (simpset -> Sign.sg -> thm list -> term -> thm option) -> simproc
    66   val clear_ss  : simpset -> simpset
    74   val clear_ss  : simpset -> simpset
    67   val simp_thm  : bool * bool * bool -> simpset -> thm -> thm
    75   val simp_thm  : bool * bool * bool -> simpset -> thm -> thm
    68   val simp_cterm: bool * bool * bool -> simpset -> cterm -> thm
    76   val simp_cterm: bool * bool * bool -> simpset -> cterm -> thm
    69 end;
    77 end;
    70 
    78 
    83   val del_simps         : meta_simpset * thm list -> meta_simpset
    91   val del_simps         : meta_simpset * thm list -> meta_simpset
    84   val mss_of            : thm list -> meta_simpset
    92   val mss_of            : thm list -> meta_simpset
    85   val add_congs         : meta_simpset * thm list -> meta_simpset
    93   val add_congs         : meta_simpset * thm list -> meta_simpset
    86   val del_congs         : meta_simpset * thm list -> meta_simpset
    94   val del_congs         : meta_simpset * thm list -> meta_simpset
    87   val add_simprocs      : meta_simpset *
    95   val add_simprocs      : meta_simpset *
    88     (string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list
    96     (string * cterm list * (simpset -> Sign.sg -> thm list -> term -> thm option) * stamp) list
    89       -> meta_simpset
    97       -> meta_simpset
    90   val del_simprocs      : meta_simpset *
    98   val del_simprocs      : meta_simpset *
    91     (string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list
    99     (string * cterm list * (simpset -> Sign.sg -> thm list -> term -> thm option) * stamp) list
    92       -> meta_simpset
   100       -> meta_simpset
    93   val add_prems         : meta_simpset * thm list -> meta_simpset
   101   val add_prems         : meta_simpset * thm list -> meta_simpset
    94   val prems_of_mss      : meta_simpset -> thm list
   102   val prems_of_mss      : meta_simpset -> thm list
    95   val set_mk_rews       : meta_simpset * (thm -> thm list) -> meta_simpset
   103   val set_mk_rews       : meta_simpset * (thm -> thm list) -> meta_simpset
    96   val set_mk_sym        : meta_simpset * (thm -> thm option) -> meta_simpset
   104   val set_mk_sym        : meta_simpset * (thm -> thm option) -> meta_simpset
    98   val get_mk_rews       : meta_simpset -> thm -> thm list
   106   val get_mk_rews       : meta_simpset -> thm -> thm list
    99   val get_mk_sym        : meta_simpset -> thm -> thm option
   107   val get_mk_sym        : meta_simpset -> thm -> thm option
   100   val get_mk_eq_True    : meta_simpset -> thm -> thm option
   108   val get_mk_eq_True    : meta_simpset -> thm -> thm option
   101   val set_termless      : meta_simpset * (term * term -> bool) -> meta_simpset
   109   val set_termless      : meta_simpset * (term * term -> bool) -> meta_simpset
   102   val rewrite_cterm: bool * bool * bool ->
   110   val rewrite_cterm: bool * bool * bool ->
   103     (meta_simpset -> thm -> thm option) -> meta_simpset -> cterm -> thm
   111     (meta_simpset -> thm -> thm option) -> simpset -> cterm -> thm
   104   val rewrite_aux       : (meta_simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm
   112   val rewrite_aux       : (meta_simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm
   105   val simplify_aux      : (meta_simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm
   113   val simplify_aux      : (meta_simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm
   106   val rewrite_thm       : bool * bool * bool
   114   val rewrite_thm       : bool * bool * bool
   107                           -> (meta_simpset -> thm -> thm option)
   115                           -> (meta_simpset -> thm -> thm option)
   108                           -> meta_simpset -> thm -> thm
   116                           -> simpset -> thm -> thm
   109   val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
   117   val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
   110   val rewrite_goal_rule : bool* bool * bool
   118   val rewrite_goal_rule : bool* bool * bool
   111                           -> (meta_simpset -> thm -> thm option)
   119                           -> (meta_simpset -> thm -> thm option)
   112                           -> meta_simpset -> int -> thm -> thm
   120                           -> simpset -> int -> thm -> thm
   113   val rewrite_term: Sign.sg -> thm list -> (term -> term option) list -> term -> term
   121   val rewrite_term: Sign.sg -> thm list -> (term -> term option) list -> term -> term
   114   val asm_rewrite_goal_tac: bool*bool*bool ->
   122   val asm_rewrite_goal_tac: bool*bool*bool ->
   115     (meta_simpset -> tactic) -> meta_simpset -> int -> tactic
   123     (meta_simpset -> tactic) -> simpset -> int -> tactic
   116 
   124 
   117 end;
   125 end;
   118 
   126 
   119 structure MetaSimplifier : META_SIMPLIFIER =
   127 structure MetaSimplifier : META_SIMPLIFIER =
   120 struct
   128 struct
   179            in which case fo-matching is complete,
   187            in which case fo-matching is complete,
   180     or elhs is not a pattern,
   188     or elhs is not a pattern,
   181        in which case there is nothing better to do.
   189        in which case there is nothing better to do.
   182 *)
   190 *)
   183 type cong = {thm: thm, lhs: cterm};
   191 type cong = {thm: thm, lhs: cterm};
   184 type meta_simproc =
       
   185  {name: string, proc: Sign.sg -> thm list -> term -> thm option, lhs: cterm, id: stamp};
       
   186 
   192 
   187 fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
   193 fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
   188   #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
   194   #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
   189 
   195 
   190 fun eq_cong ({thm = thm1, ...}: cong, {thm = thm2, ...}: cong) =
   196 fun eq_cong ({thm = thm1, ...}: cong, {thm = thm2, ...}: cong) =
   191   #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
   197   #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
   192 
   198 
   193 fun eq_prem (thm1, thm2) =
   199 fun eq_prem (thm1, thm2) =
   194   #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
   200   #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
   195 
       
   196 fun eq_simproc ({id = s1, ...}:meta_simproc, {id = s2, ...}:meta_simproc) = (s1 = s2);
       
   197 
       
   198 fun mk_simproc (name, proc, lhs, id) =
       
   199   {name = name, proc = proc, lhs = lhs, id = id};
       
   200 
   201 
   201 
   202 
   202 (* datatype mss *)
   203 (* datatype mss *)
   203 
   204 
   204 (*
   205 (*
   217              mk_eq_True: turns P into P == True - logic specific;
   218              mk_eq_True: turns P into P == True - logic specific;
   218     termless: relation for ordered rewriting;
   219     termless: relation for ordered rewriting;
   219     depth: depth of conditional rewriting;
   220     depth: depth of conditional rewriting;
   220 *)
   221 *)
   221 
   222 
       
   223 datatype solver = Solver of string * (thm list -> int -> tactic) * stamp;
       
   224 
   222 datatype meta_simpset =
   225 datatype meta_simpset =
   223   Mss of {
   226   Mss of {
   224     rules: rrule Net.net,
   227     rules: rrule Net.net,
   225     congs: (string * cong) list * string list,
   228     congs: (string * cong) list * string list,
   226     procs: meta_simproc Net.net,
   229     procs: meta_simproc Net.net,
   228     prems: thm list,
   231     prems: thm list,
   229     mk_rews: {mk: thm -> thm list,
   232     mk_rews: {mk: thm -> thm list,
   230               mk_sym: thm -> thm option,
   233               mk_sym: thm -> thm option,
   231               mk_eq_True: thm -> thm option},
   234               mk_eq_True: thm -> thm option},
   232     termless: term * term -> bool,
   235     termless: term * term -> bool,
   233     depth: int};
   236     depth: int}
       
   237 and simpset =
       
   238   Simpset of {
       
   239     mss: meta_simpset,
       
   240     mk_cong: thm -> thm,
       
   241     subgoal_tac: simpset -> int -> tactic,
       
   242     loop_tacs: (string * (int -> tactic)) list,
       
   243     unsafe_solvers: solver list,
       
   244     solvers: solver list}
       
   245 withtype meta_simproc =
       
   246  {name: string, proc: simpset -> Sign.sg -> thm list -> term -> thm option, lhs: cterm, id: stamp};
   234 
   247 
   235 fun mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth) =
   248 fun mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth) =
   236   Mss {rules = rules, congs = congs, procs = procs, bounds = bounds,
   249   Mss {rules = rules, congs = congs, procs = procs, bounds = bounds,
   237        prems=prems, mk_rews=mk_rews, termless=termless, depth=depth};
   250        prems=prems, mk_rews=mk_rews, termless=termless, depth=depth};
   238 
   251 
   254            then warning("Simplification depth " ^ string_of_int depth1)
   267            then warning("Simplification depth " ^ string_of_int depth1)
   255            else ();
   268            else ();
   256            Some(mk_mss(rules,congs,procs,bounds,prems,mk_rews,termless,depth1))
   269            Some(mk_mss(rules,congs,procs,bounds,prems,mk_rews,termless,depth1))
   257           )
   270           )
   258   end;
   271   end;
       
   272 
       
   273 datatype simproc =
       
   274   Simproc of string * cterm list * (simpset -> Sign.sg -> thm list -> term -> thm option) * stamp
       
   275 
       
   276 fun eq_simproc ({id = s1, ...}:meta_simproc, {id = s2, ...}:meta_simproc) = (s1 = s2);
       
   277 
       
   278 fun mk_simproc (name, proc, lhs, id) =
       
   279   {name = name, proc = proc, lhs = lhs, id = id};
   259 
   280 
   260 
   281 
   261 (** simpset operations **)
   282 (** simpset operations **)
   262 
   283 
   263 (* term variables *)
   284 (* term variables *)
   589 
   610 
   590 (** simplification procedures **)
   611 (** simplification procedures **)
   591 
   612 
   592 (* datatype simproc *)
   613 (* datatype simproc *)
   593 
   614 
   594 datatype simproc =
   615 fun full_mk_simproc name lhss proc =
   595   Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
   616   Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
       
   617 
       
   618 fun full_simproc sg name ss =
       
   619   full_mk_simproc name (map (fn s => Thm.read_cterm sg (s, TypeInfer.logicT)) ss);
       
   620 fun full_simproc_i sg name = full_mk_simproc name o map (Thm.cterm_of sg);
   596 
   621 
   597 fun mk_simproc name lhss proc =
   622 fun mk_simproc name lhss proc =
   598   Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
   623   Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, K proc, stamp ());
   599 
   624 
   600 fun simproc sg name ss =
   625 fun simproc sg name ss =
   601   mk_simproc name (map (fn s => Thm.read_cterm sg (s, TypeInfer.logicT)) ss);
   626   mk_simproc name (map (fn s => Thm.read_cterm sg (s, TypeInfer.logicT)) ss);
   602 fun simproc_i sg name = mk_simproc name o map (Thm.cterm_of sg);
   627 fun simproc_i sg name = mk_simproc name o map (Thm.cterm_of sg);
   603 
   628 
   605 
   630 
   606 
   631 
   607 
   632 
   608 (** solvers **)
   633 (** solvers **)
   609 
   634 
   610 datatype solver = Solver of string * (thm list -> int -> tactic) * stamp;
       
   611 
       
   612 fun mk_solver name solver = Solver (name, solver, stamp());
   635 fun mk_solver name solver = Solver (name, solver, stamp());
   613 fun eq_solver (Solver (_, _, s1), Solver(_, _, s2)) = s1 = s2;
   636 fun eq_solver (Solver (_, _, s1), Solver(_, _, s2)) = s1 = s2;
   614 
   637 
   615 val merge_solvers = gen_merge_lists eq_solver;
   638 val merge_solvers = gen_merge_lists eq_solver;
   616 
   639 
   622 
   645 
   623 (** simplification sets **)
   646 (** simplification sets **)
   624 
   647 
   625 (* type simpset *)
   648 (* type simpset *)
   626 
   649 
   627 datatype simpset =
       
   628   Simpset of {
       
   629     mss: meta_simpset,
       
   630     mk_cong: thm -> thm,
       
   631     subgoal_tac: simpset -> int -> tactic,
       
   632     loop_tacs: (string * (int -> tactic)) list,
       
   633     unsafe_solvers: solver list,
       
   634     solvers: solver list};
       
   635 
       
   636 fun make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers solvers =
   650 fun make_ss mss mk_cong subgoal_tac loop_tacs unsafe_solvers solvers =
   637   Simpset {mss = mss, mk_cong = mk_cong, subgoal_tac = subgoal_tac,
   651   Simpset {mss = mss, mk_cong = mk_cong, subgoal_tac = subgoal_tac,
   638     loop_tacs = loop_tacs, unsafe_solvers = unsafe_solvers, solvers = solvers};
   652     loop_tacs = loop_tacs, unsafe_solvers = unsafe_solvers, solvers = solvers};
   639 
   653 
   640 val empty_ss =
   654 fun from_mss mss = make_ss mss I (K (K no_tac)) [] [] [];
   641   let val mss = set_mk_sym (empty_mss, Some o symmetric_fun)
   655 
   642   in make_ss mss I (K (K no_tac)) [] [] [] end;
   656 val empty_ss = from_mss (set_mk_sym (empty_mss, Some o symmetric_fun));
   643 
   657 
   644 fun rep_ss (Simpset args) = args;
   658 fun rep_ss (Simpset args) = args;
   645 fun prems_of_ss (Simpset {mss, ...}) = prems_of_mss mss;
   659 fun prems_of_ss (Simpset {mss, ...}) = prems_of_mss mss;
   646 
   660 
   647 
   661 
   848   IMPORTANT: rewrite rules must not introduce new Vars or TVars!
   862   IMPORTANT: rewrite rules must not introduce new Vars or TVars!
   849 
   863 
   850 *)
   864 *)
   851 
   865 
   852 fun rewritec (prover, signt, maxt)
   866 fun rewritec (prover, signt, maxt)
   853              (mss as Mss{rules, procs, termless, prems, congs, depth,...}) t =
   867              (ss as Simpset{mss=mss as Mss{rules, procs, termless, prems, congs, depth,...},...}) t =
   854   let
   868   let
   855     val eta_thm = Thm.eta_conversion t;
   869     val eta_thm = Thm.eta_conversion t;
   856     val eta_t' = rhs_of eta_thm;
   870     val eta_t' = rhs_of eta_thm;
   857     val eta_t = term_of eta_t';
   871     val eta_t = term_of eta_t';
   858     val tsigt = Sign.tsig_of signt;
   872     val tsigt = Sign.tsig_of signt;
   915     fun proc_rews ([]:meta_simproc list) = None
   929     fun proc_rews ([]:meta_simproc list) = None
   916       | proc_rews ({name, proc, lhs, ...} :: ps) =
   930       | proc_rews ({name, proc, lhs, ...} :: ps) =
   917           if Pattern.matches tsigt (term_of lhs, term_of t) then
   931           if Pattern.matches tsigt (term_of lhs, term_of t) then
   918             (debug_term false ("Trying procedure " ^ quote name ^ " on:") signt eta_t;
   932             (debug_term false ("Trying procedure " ^ quote name ^ " on:") signt eta_t;
   919              case transform_failure (curry SIMPROC_FAIL name)
   933              case transform_failure (curry SIMPROC_FAIL name)
   920                  (fn () => proc signt prems eta_t) () of
   934                  (fn () => proc ss signt prems eta_t) () of
   921                None => (debug false "FAILED"; proc_rews ps)
   935                None => (debug false "FAILED"; proc_rews ps)
   922              | Some raw_thm =>
   936              | Some raw_thm =>
   923                  (trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm;
   937                  (trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm;
   924                   (case rews (mk_procrule raw_thm) of
   938                   (case rews (mk_procrule raw_thm) of
   925                     None => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^
   939                     None => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^
   967   | transitive1 (Some thm1) (Some thm2) = Some (transitive thm1 thm2)
   981   | transitive1 (Some thm1) (Some thm2) = Some (transitive thm1 thm2)
   968 
   982 
   969 fun transitive2 thm = transitive1 (Some thm);
   983 fun transitive2 thm = transitive1 (Some thm);
   970 fun transitive3 thm = transitive1 thm o Some;
   984 fun transitive3 thm = transitive1 thm o Some;
   971 
   985 
   972 fun bottomc ((simprem,useprem,mutsimp), prover, sign, maxidx) =
   986 fun replace_mss (Simpset{mss=_,mk_cong,subgoal_tac,loop_tacs,unsafe_solvers,solvers}) mss_new =
       
   987     Simpset{mss=mss_new,mk_cong=mk_cong,subgoal_tac=subgoal_tac,loop_tacs=loop_tacs,
       
   988 	    unsafe_solvers=unsafe_solvers,solvers=solvers};
       
   989 
       
   990 fun bottomc ((simprem,useprem,mutsimp), prover, sign, maxidx) (ss as Simpset{mss,...}) =
   973   let
   991   let
   974     fun botc skel mss t =
   992     fun botc skel mss t =
   975           if is_Var skel then None
   993           if is_Var skel then None
   976           else
   994           else
   977           (case subc skel mss t of
   995           (case subc skel mss t of
   978              some as Some thm1 =>
   996              some as Some thm1 =>
   979                (case rewritec (prover, sign, maxidx) mss (rhs_of thm1) of
   997                (case rewritec (prover, sign, maxidx) (replace_mss ss mss) (rhs_of thm1) of
   980                   Some (thm2, skel2) =>
   998                   Some (thm2, skel2) =>
   981                     transitive2 (transitive thm1 thm2)
   999                     transitive2 (transitive thm1 thm2)
   982                       (botc skel2 mss (rhs_of thm2))
  1000                       (botc skel2 mss (rhs_of thm2))
   983                 | None => some)
  1001                 | None => some)
   984            | None =>
  1002            | None =>
   985                (case rewritec (prover, sign, maxidx) mss t of
  1003                (case rewritec (prover, sign, maxidx) (replace_mss ss mss) t of
   986                   Some (thm2, skel2) => transitive2 thm2
  1004                   Some (thm2, skel2) => transitive2 thm2
   987                     (botc skel2 mss (rhs_of thm2))
  1005                     (botc skel2 mss (rhs_of thm2))
   988                 | None => None))
  1006                 | None => None))
   989 
  1007 
   990     and try_botc mss t =
  1008     and try_botc mss t =
  1091           let
  1109           let
  1092             val mss' = add_rrules (rev rrss, rev asms) mss;
  1110             val mss' = add_rrules (rev rrss, rev asms) mss;
  1093             val concl' =
  1111             val concl' =
  1094               Drule.mk_implies (prem, if_none (apsome rhs_of eq) concl);
  1112               Drule.mk_implies (prem, if_none (apsome rhs_of eq) concl);
  1095             val dprem = apsome (curry (disch false) prem)
  1113             val dprem = apsome (curry (disch false) prem)
  1096           in case rewritec (prover, sign, maxidx) mss' concl' of
  1114           in case rewritec (prover, sign, maxidx) (replace_mss ss mss') concl' of
  1097               None => rebuild prems concl' rrss asms mss (dprem eq)
  1115               None => rebuild prems concl' rrss asms mss (dprem eq)
  1098             | Some (eq', _) => transitive2 (foldl (disch false o swap)
  1116             | Some (eq', _) => transitive2 (foldl (disch false o swap)
  1099                   (the (transitive3 (dprem eq) eq'), prems))
  1117                   (the (transitive3 (dprem eq) eq'), prems))
  1100                 (mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) mss)
  1118                 (mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) mss)
  1101           end
  1119           end
  1155              | Some thm1' =>
  1173              | Some thm1' =>
  1156                  Some (transitive (Drule.imp_cong' thm1' (reflexive conc)) thm2'))
  1174                  Some (transitive (Drule.imp_cong' thm1' (reflexive conc)) thm2'))
  1157            end)
  1175            end)
  1158        end
  1176        end
  1159 
  1177 
  1160  in try_botc end;
  1178  in try_botc mss end;
  1161 
  1179 
  1162 
  1180 
  1163 (*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***)
  1181 (*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***)
  1164 
  1182 
  1165 (*
  1183 (*
  1170            when simplifying A ==> B
  1188            when simplifying A ==> B
  1171     mss: contains equality theorems of the form [|p1,...|] ==> t==u
  1189     mss: contains equality theorems of the form [|p1,...|] ==> t==u
  1172     prover: how to solve premises in conditional rewrites and congruences
  1190     prover: how to solve premises in conditional rewrites and congruences
  1173 *)
  1191 *)
  1174 
  1192 
  1175 fun rewrite_cterm mode prover mss ct =
  1193 fun rewrite_cterm mode prover (ss as Simpset{mss,...}) ct =
  1176   let val {sign, t, maxidx, ...} = rep_cterm ct
  1194   let val {sign, t, maxidx, ...} = rep_cterm ct
  1177       val Mss{depth, ...} = mss
  1195       val Mss{depth, ...} = mss
  1178   in trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ct;
  1196   in trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ct;
  1179      simp_depth := depth;
  1197      simp_depth := depth;
  1180      bottomc (mode, prover, sign, maxidx) mss ct
  1198      bottomc (mode, prover, sign, maxidx) ss ct
  1181   end
  1199   end
  1182   handle THM (s, _, thms) =>
  1200   handle THM (s, _, thms) =>
  1183     error ("Exception THM was raised in simplifier:\n" ^ s ^ "\n" ^
  1201     error ("Exception THM was raised in simplifier:\n" ^ s ^ "\n" ^
  1184       Pretty.string_of (Display.pretty_thms thms));
  1202       Pretty.string_of (Display.pretty_thms thms));
  1185 
  1203 
       
  1204 val ss_of = from_mss o mss_of
       
  1205 
  1186 (*Rewrite a cterm*)
  1206 (*Rewrite a cterm*)
  1187 fun rewrite_aux _ _ [] = (fn ct => Thm.reflexive ct)
  1207 fun rewrite_aux _ _ [] = (fn ct => Thm.reflexive ct)
  1188   | rewrite_aux prover full thms = rewrite_cterm (full, false, false) prover (mss_of thms);
  1208   | rewrite_aux prover full thms = rewrite_cterm (full, false, false) prover (ss_of thms);
  1189 
  1209 
  1190 (*Rewrite a theorem*)
  1210 (*Rewrite a theorem*)
  1191 fun simplify_aux _ _ [] = (fn th => th)
  1211 fun simplify_aux _ _ [] = (fn th => th)
  1192   | simplify_aux prover full thms =
  1212   | simplify_aux prover full thms =
  1193       Drule.fconv_rule (rewrite_cterm (full, false, false) prover (mss_of thms));
  1213       Drule.fconv_rule (rewrite_cterm (full, false, false) prover (ss_of thms));
  1194 
  1214 
  1195 fun rewrite_thm mode prover mss = Drule.fconv_rule (rewrite_cterm mode prover mss);
  1215 fun rewrite_thm mode prover mss = Drule.fconv_rule (rewrite_cterm mode prover mss);
  1196 
  1216 
  1197 (*Rewrite the subgoals of a proof state (represented by a theorem) *)
  1217 (*Rewrite the subgoals of a proof state (represented by a theorem) *)
  1198 fun rewrite_goals_rule_aux _ []   th = th
  1218 fun rewrite_goals_rule_aux _ []   th = th
  1199   | rewrite_goals_rule_aux prover thms th =
  1219   | rewrite_goals_rule_aux prover thms th =
  1200       Drule.fconv_rule (Drule.goals_conv (K true) (rewrite_cterm (true, true, false) prover
  1220       Drule.fconv_rule (Drule.goals_conv (K true) (rewrite_cterm (true, true, false) prover
  1201         (mss_of thms))) th;
  1221         (ss_of thms))) th;
  1202 
  1222 
  1203 (*Rewrite the subgoal of a proof state (represented by a theorem) *)
  1223 (*Rewrite the subgoal of a proof state (represented by a theorem) *)
  1204 fun rewrite_goal_rule mode prover mss i thm =
  1224 fun rewrite_goal_rule mode prover ss i thm =
  1205   if 0 < i  andalso  i <= nprems_of thm
  1225   if 0 < i  andalso  i <= nprems_of thm
  1206   then Drule.fconv_rule (Drule.goals_conv (fn j => j=i) (rewrite_cterm mode prover mss)) thm
  1226   then Drule.fconv_rule (Drule.goals_conv (fn j => j=i) (rewrite_cterm mode prover ss)) thm
  1207   else raise THM("rewrite_goal_rule",i,[thm]);
  1227   else raise THM("rewrite_goal_rule",i,[thm]);
  1208 
  1228 
  1209 
  1229 
  1210 (*simple term rewriting -- without proofs*)
  1230 (*simple term rewriting -- without proofs*)
  1211 fun rewrite_term sg rules procs =
  1231 fun rewrite_term sg rules procs =
  1227 
  1247 
  1228 fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
  1248 fun loop_tac loop_tacs = FIRST'(map snd loop_tacs);
  1229 
  1249 
  1230 (*note: may instantiate unknowns that appear also in other subgoals*)
  1250 (*note: may instantiate unknowns that appear also in other subgoals*)
  1231 fun generic_simp_tac safe mode =
  1251 fun generic_simp_tac safe mode =
  1232   fn (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers, ...}) =>
  1252   fn (ss as Simpset {mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, solvers, ...}) =>
  1233     let
  1253     let
  1234       val solvs = app_sols (if safe then solvers else unsafe_solvers);
  1254       val solvs = app_sols (if safe then solvers else unsafe_solvers);
  1235       fun simp_loop_tac i =
  1255       fun simp_loop_tac i =
  1236         asm_rewrite_goal_tac mode
  1256         asm_rewrite_goal_tac mode
  1237           (solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers))
  1257           (solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers))
  1238           mss i
  1258           ss i
  1239         THEN (solvs (prems_of_mss mss) i ORELSE
  1259         THEN (solvs (prems_of_ss ss) i ORELSE
  1240               TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
  1260               TRY ((loop_tac loop_tacs THEN_ALL_NEW simp_loop_tac) i))
  1241     in simp_loop_tac end;
  1261     in simp_loop_tac end;
  1242 
  1262 
  1243 (** simplification rules and conversions **)
  1263 (** simplification rules and conversions **)
  1244 
  1264 
  1245 fun simp rew mode
  1265 fun simp rew mode
  1246      (Simpset {mss, mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
  1266      (ss as Simpset {mk_cong, subgoal_tac, loop_tacs, unsafe_solvers, ...}) thm =
  1247   let
  1267   let
  1248     val tacf = solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers);
  1268     val tacf = solve_all_tac (mk_cong, subgoal_tac, loop_tacs, unsafe_solvers);
  1249     fun prover m th = apsome fst (Seq.pull (tacf m th));
  1269     fun prover m th = apsome fst (Seq.pull (tacf m th));
  1250   in rew mode prover mss thm end;
  1270   in rew mode prover ss thm end;
  1251 
  1271 
  1252 val simp_thm = simp rewrite_thm;
  1272 val simp_thm = simp rewrite_thm;
  1253 val simp_cterm = simp rewrite_cterm;
  1273 val simp_cterm = simp rewrite_cterm;
  1254 
  1274 
  1255 end;
  1275 end;