src/Pure/meta_simplifier.ML
changeset 16458 4c6fd0c01d28
parent 16380 019ec70774ff
child 16665 b75568de32c6
equal deleted inserted replaced
16457:e0f22edf38a5 16458:4c6fd0c01d28
    40   val print_ss: simpset -> unit
    40   val print_ss: simpset -> unit
    41   val empty_ss: simpset
    41   val empty_ss: simpset
    42   val merge_ss: simpset * simpset -> simpset
    42   val merge_ss: simpset * simpset -> simpset
    43   type simproc
    43   type simproc
    44   val mk_simproc: string -> cterm list ->
    44   val mk_simproc: string -> cterm list ->
    45     (Sign.sg -> simpset -> term -> thm option) -> simproc
    45     (theory -> simpset -> term -> thm option) -> simproc
    46   val add_prems: thm list -> simpset -> simpset
    46   val add_prems: thm list -> simpset -> simpset
    47   val prems_of_ss: simpset -> thm list
    47   val prems_of_ss: simpset -> thm list
    48   val addsimps: simpset * thm list -> simpset
    48   val addsimps: simpset * thm list -> simpset
    49   val delsimps: simpset * thm list -> simpset
    49   val delsimps: simpset * thm list -> simpset
    50   val addeqcongs: simpset * thm list -> simpset
    50   val addeqcongs: simpset * thm list -> simpset
    73 sig
    73 sig
    74   include BASIC_META_SIMPLIFIER
    74   include BASIC_META_SIMPLIFIER
    75   exception SIMPLIFIER of string * thm
    75   exception SIMPLIFIER of string * thm
    76   val clear_ss: simpset -> simpset
    76   val clear_ss: simpset -> simpset
    77   exception SIMPROC_FAIL of string * exn
    77   exception SIMPROC_FAIL of string * exn
    78   val simproc_i: Sign.sg -> string -> term list
    78   val simproc_i: theory -> string -> term list
    79     -> (Sign.sg -> simpset -> term -> thm option) -> simproc
    79     -> (theory -> simpset -> term -> thm option) -> simproc
    80   val simproc: Sign.sg -> string -> string list
    80   val simproc: theory -> string -> string list
    81     -> (Sign.sg -> simpset -> term -> thm option) -> simproc
    81     -> (theory -> simpset -> term -> thm option) -> simproc
    82   val rewrite_cterm: bool * bool * bool ->
    82   val rewrite_cterm: bool * bool * bool ->
    83     (simpset -> thm -> thm option) -> simpset -> cterm -> thm
    83     (simpset -> thm -> thm option) -> simpset -> cterm -> thm
    84   val rewrite_aux: (simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm
    84   val rewrite_aux: (simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm
    85   val simplify_aux: (simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm
    85   val simplify_aux: (simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm
    86   val rewrite_term: Sign.sg -> thm list -> (term -> term option) list -> term -> term
    86   val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term
    87   val rewrite_thm: bool * bool * bool ->
    87   val rewrite_thm: bool * bool * bool ->
    88     (simpset -> thm -> thm option) -> simpset -> thm -> thm
    88     (simpset -> thm -> thm option) -> simpset -> thm -> thm
    89   val rewrite_goals_rule_aux: (simpset -> thm -> thm option) -> thm list -> thm -> thm
    89   val rewrite_goals_rule_aux: (simpset -> thm -> thm option) -> thm list -> thm -> thm
    90   val rewrite_goal_rule: bool * bool * bool ->
    90   val rewrite_goal_rule: bool * bool * bool ->
    91     (simpset -> thm -> thm option) -> simpset -> int -> thm -> thm
    91     (simpset -> thm -> thm option) -> simpset -> int -> thm -> thm
   114 fun println a =
   114 fun println a =
   115   if !simp_depth > !trace_simp_depth_limit then ()
   115   if !simp_depth > !trace_simp_depth_limit then ()
   116   else tracing (enclose "[" "]" (string_of_int(!simp_depth)) ^ a);
   116   else tracing (enclose "[" "]" (string_of_int(!simp_depth)) ^ a);
   117 
   117 
   118 fun prnt warn a = if warn then warning a else println a;
   118 fun prnt warn a = if warn then warning a else println a;
   119 fun prtm warn a sg t = prnt warn (a ^ "\n" ^ Sign.string_of_term sg t);
   119 fun prtm warn a thy t = prnt warn (a ^ "\n" ^ Sign.string_of_term thy t);
   120 fun prctm warn a t = prnt warn (a ^ "\n" ^ Display.string_of_cterm t);
   120 fun prctm warn a t = prnt warn (a ^ "\n" ^ Display.string_of_cterm t);
   121 
   121 
   122 in
   122 in
   123 
   123 
   124 fun debug warn a = if ! debug_simp then prnt warn a else ();
   124 fun debug warn a = if ! debug_simp then prnt warn a else ();
   125 fun trace warn a = if ! trace_simp then prnt warn a else ();
   125 fun trace warn a = if ! trace_simp then prnt warn a else ();
   126 
   126 
   127 fun debug_term warn a sign t = if ! debug_simp then prtm warn a sign t else ();
   127 fun debug_term warn a thy t = if ! debug_simp then prtm warn a thy t else ();
   128 fun trace_term warn a sign t = if ! trace_simp then prtm warn a sign t else ();
   128 fun trace_term warn a thy t = if ! trace_simp then prtm warn a thy t else ();
   129 fun trace_cterm warn a ct = if ! trace_simp then prctm warn a ct else ();
   129 fun trace_cterm warn a ct = if ! trace_simp then prctm warn a ct else ();
   130 fun trace_thm a th = if ! trace_simp then prctm false a (Thm.cprop_of th) else ();
   130 fun trace_thm a th = if ! trace_simp then prctm false a (Thm.cprop_of th) else ();
   131 
   131 
   132 fun trace_named_thm a (thm, name) =
   132 fun trace_named_thm a (thm, name) =
   133   if ! trace_simp then
   133   if ! trace_simp then
   229     solvers: solver list * solver list}
   229     solvers: solver list * solver list}
   230 and proc =
   230 and proc =
   231   Proc of
   231   Proc of
   232    {name: string,
   232    {name: string,
   233     lhs: cterm,
   233     lhs: cterm,
   234     proc: Sign.sg -> simpset -> term -> thm option,
   234     proc: theory -> simpset -> term -> thm option,
   235     id: stamp};
   235     id: stamp};
   236 
   236 
   237 fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = (id1 = id2);
   237 fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = (id1 = id2);
   238 
   238 
   239 fun rep_ss (Simpset args) = args;
   239 fun rep_ss (Simpset args) = args;
   352   let val id = stamp () in
   352   let val id = stamp () in
   353     Simproc (lhss |> map (fn lhs =>
   353     Simproc (lhss |> map (fn lhs =>
   354       Proc {name = name, lhs = lhs, proc = proc, id = id}))
   354       Proc {name = name, lhs = lhs, proc = proc, id = id}))
   355   end;
   355   end;
   356 
   356 
   357 fun simproc_i sg name = mk_simproc name o map (Thm.cterm_of sg o Logic.varify);
   357 fun simproc_i thy name = mk_simproc name o map (Thm.cterm_of thy o Logic.varify);
   358 fun simproc sg name = simproc_i sg name o map (Sign.simple_read_term sg TypeInfer.logicT);
   358 fun simproc thy name = simproc_i thy name o map (Sign.simple_read_term thy TypeInfer.logicT);
   359 
   359 
   360 
   360 
   361 
   361 
   362 (** simpset operations **)
   362 (** simpset operations **)
   363 
   363 
   405   not (term_varnames erhs subset Library.foldl add_term_varnames (term_varnames elhs, prems))
   405   not (term_varnames erhs subset Library.foldl add_term_varnames (term_varnames elhs, prems))
   406   orelse
   406   orelse
   407   not (term_tvars erhs subset (term_tvars elhs union List.concat (map term_tvars prems)));
   407   not (term_tvars erhs subset (term_tvars elhs union List.concat (map term_tvars prems)));
   408 
   408 
   409 (*simple test for looping rewrite rules and stupid orientations*)
   409 (*simple test for looping rewrite rules and stupid orientations*)
   410 fun reorient sign prems lhs rhs =
   410 fun reorient thy prems lhs rhs =
   411   rewrite_rule_extra_vars prems lhs rhs
   411   rewrite_rule_extra_vars prems lhs rhs
   412     orelse
   412     orelse
   413   is_Var (head_of lhs)
   413   is_Var (head_of lhs)
   414     orelse
   414     orelse
   415 (* turns t = x around, which causes a headache if x is a local variable -
   415 (* turns t = x around, which causes a headache if x is a local variable -
   418   andalso not(exists_subterm is_Var lhs)
   418   andalso not(exists_subterm is_Var lhs)
   419     orelse
   419     orelse
   420 *)
   420 *)
   421   exists (apl (lhs, Logic.occs)) (rhs :: prems)
   421   exists (apl (lhs, Logic.occs)) (rhs :: prems)
   422     orelse
   422     orelse
   423   null prems andalso Pattern.matches (Sign.tsig_of sign) (lhs, rhs)
   423   null prems andalso Pattern.matches (Sign.tsig_of thy) (lhs, rhs)
   424     (*the condition "null prems" is necessary because conditional rewrites
   424     (*the condition "null prems" is necessary because conditional rewrites
   425       with extra variables in the conditions may terminate although
   425       with extra variables in the conditions may terminate although
   426       the rhs is an instance of the lhs; example: ?m < ?n ==> f(?n) == f(?m)*)
   426       the rhs is an instance of the lhs; example: ?m < ?n ==> f(?n) == f(?m)*)
   427     orelse
   427     orelse
   428   is_Const lhs andalso not (is_Const rhs);
   428   is_Const lhs andalso not (is_Const rhs);
   429 
   429 
   430 fun decomp_simp thm =
   430 fun decomp_simp thm =
   431   let
   431   let
   432     val {sign, prop, ...} = Thm.rep_thm thm;
   432     val {thy, prop, ...} = Thm.rep_thm thm;
   433     val prems = Logic.strip_imp_prems prop;
   433     val prems = Logic.strip_imp_prems prop;
   434     val concl = Drule.strip_imp_concl (Thm.cprop_of thm);
   434     val concl = Drule.strip_imp_concl (Thm.cprop_of thm);
   435     val (lhs, rhs) = Drule.dest_equals concl handle TERM _ =>
   435     val (lhs, rhs) = Drule.dest_equals concl handle TERM _ =>
   436       raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm);
   436       raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm);
   437     val (_, elhs) = Drule.dest_equals (Thm.cprop_of (Thm.eta_conversion lhs));
   437     val (_, elhs) = Drule.dest_equals (Thm.cprop_of (Thm.eta_conversion lhs));
   439     val erhs = Pattern.eta_contract (term_of rhs);
   439     val erhs = Pattern.eta_contract (term_of rhs);
   440     val perm =
   440     val perm =
   441       var_perm (term_of elhs, erhs) andalso
   441       var_perm (term_of elhs, erhs) andalso
   442       not (term_of elhs aconv erhs) andalso
   442       not (term_of elhs aconv erhs) andalso
   443       not (is_Var (term_of elhs));
   443       not (is_Var (term_of elhs));
   444   in (sign, prems, term_of lhs, elhs, term_of rhs, perm) end;
   444   in (thy, prems, term_of lhs, elhs, term_of rhs, perm) end;
   445 
   445 
   446 fun decomp_simp' thm =
   446 fun decomp_simp' thm =
   447   let val (_, _, lhs, _, rhs, _) = decomp_simp thm in
   447   let val (_, _, lhs, _, rhs, _) = decomp_simp thm in
   448     if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", thm)
   448     if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", thm)
   449     else (lhs, rhs)
   449     else (lhs, rhs)
   474       then mk_eq_True ss (thm, name)
   474       then mk_eq_True ss (thm, name)
   475       else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm)
   475       else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm)
   476   end;
   476   end;
   477 
   477 
   478 fun orient_rrule ss (thm, name) =
   478 fun orient_rrule ss (thm, name) =
   479   let val (sign, prems, lhs, elhs, rhs, perm) = decomp_simp thm in
   479   let val (thy, prems, lhs, elhs, rhs, perm) = decomp_simp thm in
   480     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
   480     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
   481     else if reorient sign prems lhs rhs then
   481     else if reorient thy prems lhs rhs then
   482       if reorient sign prems rhs lhs
   482       if reorient thy prems rhs lhs
   483       then mk_eq_True ss (thm, name)
   483       then mk_eq_True ss (thm, name)
   484       else
   484       else
   485         let val Simpset (_, {mk_rews = {mk_sym, ...}, ...}) = ss in
   485         let val Simpset (_, {mk_rews = {mk_sym, ...}, ...}) = ss in
   486           (case mk_sym thm of
   486           (case mk_sym thm of
   487             NONE => []
   487             NONE => []
   716   let
   716   let
   717     val thm'' = transitive thm (transitive
   717     val thm'' = transitive thm (transitive
   718       (symmetric (Drule.beta_eta_conversion (lhs_of thm'))) thm')
   718       (symmetric (Drule.beta_eta_conversion (lhs_of thm'))) thm')
   719   in if msg then trace_thm "SUCCEEDED" thm' else (); SOME thm'' end
   719   in if msg then trace_thm "SUCCEEDED" thm' else (); SOME thm'' end
   720   handle THM _ =>
   720   handle THM _ =>
   721     let val {sign, prop = _ $ _ $ prop0, ...} = Thm.rep_thm thm in
   721     let val {thy, prop = _ $ _ $ prop0, ...} = Thm.rep_thm thm in
   722       trace_thm "Proved wrong thm (Check subgoaler?)" thm';
   722       trace_thm "Proved wrong thm (Check subgoaler?)" thm';
   723       trace_term false "Should have proved:" sign prop0;
   723       trace_term false "Should have proved:" thy prop0;
   724       NONE
   724       NONE
   725     end;
   725     end;
   726 
   726 
   727 
   727 
   728 (* mk_procrule *)
   728 (* mk_procrule *)
   771     (4) simplification procedures
   771     (4) simplification procedures
   772 
   772 
   773   IMPORTANT: rewrite rules must not introduce new Vars or TVars!
   773   IMPORTANT: rewrite rules must not introduce new Vars or TVars!
   774 *)
   774 *)
   775 
   775 
   776 fun rewritec (prover, signt, maxt) ss t =
   776 fun rewritec (prover, thyt, maxt) ss t =
   777   let
   777   let
   778     val Simpset ({rules, ...}, {congs, procs, termless, ...}) = ss;
   778     val Simpset ({rules, ...}, {congs, procs, termless, ...}) = ss;
   779     val eta_thm = Thm.eta_conversion t;
   779     val eta_thm = Thm.eta_conversion t;
   780     val eta_t' = rhs_of eta_thm;
   780     val eta_t' = rhs_of eta_thm;
   781     val eta_t = term_of eta_t';
   781     val eta_t = term_of eta_t';
   782     val tsigt = Sign.tsig_of signt;
   782     val tsigt = Sign.tsig_of thyt;
   783     fun rew {thm, name, lhs, elhs, fo, perm} =
   783     fun rew {thm, name, lhs, elhs, fo, perm} =
   784       let
   784       let
   785         val {sign, prop, maxidx, ...} = rep_thm thm;
   785         val {thy, prop, maxidx, ...} = rep_thm thm;
   786         val (rthm, elhs') = if maxt = ~1 then (thm, elhs)
   786         val (rthm, elhs') = if maxt = ~1 then (thm, elhs)
   787           else (Thm.incr_indexes (maxt+1) thm, Thm.cterm_incr_indexes (maxt+1) elhs);
   787           else (Thm.incr_indexes (maxt+1) thm, Thm.cterm_incr_indexes (maxt+1) elhs);
   788         val insts = if fo then Thm.cterm_first_order_match (elhs', eta_t')
   788         val insts = if fo then Thm.cterm_first_order_match (elhs', eta_t')
   789                           else Thm.cterm_match (elhs', eta_t');
   789                           else Thm.cterm_match (elhs', eta_t');
   790         val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm);
   790         val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm);
   835     in sort rrs ([],[]) end
   835     in sort rrs ([],[]) end
   836 
   836 
   837     fun proc_rews [] = NONE
   837     fun proc_rews [] = NONE
   838       | proc_rews (Proc {name, proc, lhs, ...} :: ps) =
   838       | proc_rews (Proc {name, proc, lhs, ...} :: ps) =
   839           if Pattern.matches tsigt (Thm.term_of lhs, Thm.term_of t) then
   839           if Pattern.matches tsigt (Thm.term_of lhs, Thm.term_of t) then
   840             (debug_term false ("Trying procedure " ^ quote name ^ " on:") signt eta_t;
   840             (debug_term false ("Trying procedure " ^ quote name ^ " on:") thyt eta_t;
   841              case transform_failure (curry SIMPROC_FAIL name)
   841              case transform_failure (curry SIMPROC_FAIL name)
   842                  (fn () => proc signt ss eta_t) () of
   842                  (fn () => proc thyt ss eta_t) () of
   843                NONE => (debug false "FAILED"; proc_rews ps)
   843                NONE => (debug false "FAILED"; proc_rews ps)
   844              | SOME raw_thm =>
   844              | SOME raw_thm =>
   845                  (trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm;
   845                  (trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm;
   846                   (case rews (mk_procrule raw_thm) of
   846                   (case rews (mk_procrule raw_thm) of
   847                     NONE => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^
   847                     NONE => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^
   857   end;
   857   end;
   858 
   858 
   859 
   859 
   860 (* conversion to apply a congruence rule to a term *)
   860 (* conversion to apply a congruence rule to a term *)
   861 
   861 
   862 fun congc (prover,signt,maxt) {thm=cong,lhs=lhs} t =
   862 fun congc (prover,thyt,maxt) {thm=cong,lhs=lhs} t =
   863   let val sign = Thm.sign_of_thm cong
   863   let val thy = Thm.theory_of_thm cong
   864       val rthm = if maxt = ~1 then cong else Thm.incr_indexes (maxt+1) cong;
   864       val rthm = if maxt = ~1 then cong else Thm.incr_indexes (maxt+1) cong;
   865       val rlhs = fst (Drule.dest_equals (Drule.strip_imp_concl (cprop_of rthm)));
   865       val rlhs = fst (Drule.dest_equals (Drule.strip_imp_concl (cprop_of rthm)));
   866       val insts = Thm.cterm_match (rlhs, t)
   866       val insts = Thm.cterm_match (rlhs, t)
   867       (* Pattern.match can raise Pattern.MATCH;
   867       (* Pattern.match can raise Pattern.MATCH;
   868          is handled when congc is called *)
   868          is handled when congc is called *)
   887   | transitive1 (SOME thm1) (SOME thm2) = SOME (transitive thm1 thm2)
   887   | transitive1 (SOME thm1) (SOME thm2) = SOME (transitive thm1 thm2)
   888 
   888 
   889 fun transitive2 thm = transitive1 (SOME thm);
   889 fun transitive2 thm = transitive1 (SOME thm);
   890 fun transitive3 thm = transitive1 thm o SOME;
   890 fun transitive3 thm = transitive1 thm o SOME;
   891 
   891 
   892 fun bottomc ((simprem, useprem, mutsimp), prover, sign, maxidx) =
   892 fun bottomc ((simprem, useprem, mutsimp), prover, thy, maxidx) =
   893   let
   893   let
   894     fun botc skel ss t =
   894     fun botc skel ss t =
   895           if is_Var skel then NONE
   895           if is_Var skel then NONE
   896           else
   896           else
   897           (case subc skel ss t of
   897           (case subc skel ss t of
   898              some as SOME thm1 =>
   898              some as SOME thm1 =>
   899                (case rewritec (prover, sign, maxidx) ss (rhs_of thm1) of
   899                (case rewritec (prover, thy, maxidx) ss (rhs_of thm1) of
   900                   SOME (thm2, skel2) =>
   900                   SOME (thm2, skel2) =>
   901                     transitive2 (transitive thm1 thm2)
   901                     transitive2 (transitive thm1 thm2)
   902                       (botc skel2 ss (rhs_of thm2))
   902                       (botc skel2 ss (rhs_of thm2))
   903                 | NONE => some)
   903                 | NONE => some)
   904            | NONE =>
   904            | NONE =>
   905                (case rewritec (prover, sign, maxidx) ss t of
   905                (case rewritec (prover, thy, maxidx) ss t of
   906                   SOME (thm2, skel2) => transitive2 thm2
   906                   SOME (thm2, skel2) => transitive2 thm2
   907                     (botc skel2 ss (rhs_of thm2))
   907                     (botc skel2 ss (rhs_of thm2))
   908                 | NONE => NONE))
   908                 | NONE => NONE))
   909 
   909 
   910     and try_botc ss t =
   910     and try_botc ss t =
   955                          NONE => appc ()
   955                          NONE => appc ()
   956                        | SOME cong =>
   956                        | SOME cong =>
   957   (*post processing: some partial applications h t1 ... tj, j <= length ts,
   957   (*post processing: some partial applications h t1 ... tj, j <= length ts,
   958     may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*)
   958     may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*)
   959                           (let
   959                           (let
   960                              val thm = congc (prover ss, sign, maxidx) cong t0;
   960                              val thm = congc (prover ss, thy, maxidx) cong t0;
   961                              val t = getOpt (Option.map rhs_of thm, t0);
   961                              val t = getOpt (Option.map rhs_of thm, t0);
   962                              val (cl, cr) = Thm.dest_comb t
   962                              val (cl, cr) = Thm.dest_comb t
   963                              val dVar = Var(("", 0), dummyT)
   963                              val dVar = Var(("", 0), dummyT)
   964                              val skel =
   964                              val skel =
   965                                list_comb (h, replicate (length ts) dVar)
   965                                list_comb (h, replicate (length ts) dVar)
  1010           let
  1010           let
  1011             val ss' = add_rrules (rev rrss, rev asms) ss;
  1011             val ss' = add_rrules (rev rrss, rev asms) ss;
  1012             val concl' =
  1012             val concl' =
  1013               Drule.mk_implies (prem, getOpt (Option.map rhs_of eq, concl));
  1013               Drule.mk_implies (prem, getOpt (Option.map rhs_of eq, concl));
  1014             val dprem = Option.map (curry (disch false) prem)
  1014             val dprem = Option.map (curry (disch false) prem)
  1015           in case rewritec (prover, sign, maxidx) ss' concl' of
  1015           in case rewritec (prover, thy, maxidx) ss' concl' of
  1016               NONE => rebuild prems concl' rrss asms ss (dprem eq)
  1016               NONE => rebuild prems concl' rrss asms ss (dprem eq)
  1017             | SOME (eq', _) => transitive2 (Library.foldl (disch false o swap)
  1017             | SOME (eq', _) => transitive2 (Library.foldl (disch false o swap)
  1018                   (valOf (transitive3 (dprem eq) eq'), prems))
  1018                   (valOf (transitive3 (dprem eq) eq'), prems))
  1019                 (mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) ss)
  1019                 (mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) ss)
  1020           end
  1020           end
  1094   (simp_depth := !simp_depth + 1;
  1094   (simp_depth := !simp_depth + 1;
  1095    if !simp_depth mod 10 = 0
  1095    if !simp_depth mod 10 = 0
  1096    then warning ("Simplification depth " ^ string_of_int (!simp_depth))
  1096    then warning ("Simplification depth " ^ string_of_int (!simp_depth))
  1097    else ();
  1097    else ();
  1098    trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ct;
  1098    trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ct;
  1099    let val {sign, t, maxidx, ...} = Thm.rep_cterm ct
  1099    let val {thy, t, maxidx, ...} = Thm.rep_cterm ct
  1100        val res = bottomc (mode, prover, sign, maxidx) ss ct
  1100        val res = bottomc (mode, prover, thy, maxidx) ss ct
  1101          handle THM (s, _, thms) =>
  1101          handle THM (s, _, thms) =>
  1102          error ("Exception THM was raised in simplifier:\n" ^ s ^ "\n" ^
  1102          error ("Exception THM was raised in simplifier:\n" ^ s ^ "\n" ^
  1103            Pretty.string_of (Display.pretty_thms thms))
  1103            Pretty.string_of (Display.pretty_thms thms))
  1104    in simp_depth := !simp_depth - 1; res end
  1104    in simp_depth := !simp_depth - 1; res end
  1105   ) handle exn => (simp_depth := !simp_depth - 1; raise exn);
  1105   ) handle exn => (simp_depth := !simp_depth - 1; raise exn);
  1113 fun simplify_aux _ _ [] = (fn th => th)
  1113 fun simplify_aux _ _ [] = (fn th => th)
  1114   | simplify_aux prover full thms =
  1114   | simplify_aux prover full thms =
  1115       Drule.fconv_rule (rewrite_cterm (full, false, false) prover (empty_ss addsimps thms));
  1115       Drule.fconv_rule (rewrite_cterm (full, false, false) prover (empty_ss addsimps thms));
  1116 
  1116 
  1117 (*simple term rewriting -- no proof*)
  1117 (*simple term rewriting -- no proof*)
  1118 fun rewrite_term sg rules procs =
  1118 fun rewrite_term thy rules procs =
  1119   Pattern.rewrite_term (Sign.tsig_of sg) (map decomp_simp' rules) procs;
  1119   Pattern.rewrite_term (Sign.tsig_of thy) (map decomp_simp' rules) procs;
  1120 
  1120 
  1121 fun rewrite_thm mode prover ss = Drule.fconv_rule (rewrite_cterm mode prover ss);
  1121 fun rewrite_thm mode prover ss = Drule.fconv_rule (rewrite_cterm mode prover ss);
  1122 
  1122 
  1123 (*Rewrite the subgoals of a proof state (represented by a theorem) *)
  1123 (*Rewrite the subgoals of a proof state (represented by a theorem) *)
  1124 fun rewrite_goals_rule_aux _ []   th = th
  1124 fun rewrite_goals_rule_aux _ []   th = th