src/Pure/raw_simplifier.ML
changeset 51717 9e7d1c139569
parent 51611 0a7b4e0384d0
child 52037 837211662fb8
     1.1 --- a/src/Pure/raw_simplifier.ML	Tue Apr 16 17:54:14 2013 +0200
     1.2 +++ b/src/Pure/raw_simplifier.ML	Thu Apr 18 17:07:01 2013 +0200
     1.3 @@ -18,10 +18,10 @@
     1.4    type cong_name = bool * string
     1.5    type rrule
     1.6    val eq_rrule: rrule * rrule -> bool
     1.7 -  type simpset
     1.8    type proc
     1.9    type solver
    1.10 -  val mk_solver: string -> (simpset -> int -> tactic) -> solver
    1.11 +  val mk_solver: string -> (Proof.context -> int -> tactic) -> solver
    1.12 +  type simpset
    1.13    val empty_ss: simpset
    1.14    val merge_ss: simpset * simpset -> simpset
    1.15    val dest_ss: simpset ->
    1.16 @@ -36,21 +36,28 @@
    1.17    val eq_simproc: simproc * simproc -> bool
    1.18    val transform_simproc: morphism -> simproc -> simproc
    1.19    val make_simproc: {name: string, lhss: cterm list,
    1.20 -    proc: morphism -> simpset -> cterm -> thm option, identifier: thm list} -> simproc
    1.21 -  val mk_simproc: string -> cterm list -> (theory -> simpset -> term -> thm option) -> simproc
    1.22 -  val addsimps: simpset * thm list -> simpset
    1.23 -  val delsimps: simpset * thm list -> simpset
    1.24 -  val addsimprocs: simpset * simproc list -> simpset
    1.25 -  val delsimprocs: simpset * simproc list -> simpset
    1.26 -  val setloop': simpset * (simpset -> int -> tactic) -> simpset
    1.27 -  val setloop: simpset * (int -> tactic) -> simpset
    1.28 -  val addloop': simpset * (string * (simpset -> int -> tactic)) -> simpset
    1.29 -  val addloop: simpset * (string * (int -> tactic)) -> simpset
    1.30 -  val delloop: simpset * string -> simpset
    1.31 -  val setSSolver: simpset * solver -> simpset
    1.32 -  val addSSolver: simpset * solver -> simpset
    1.33 -  val setSolver: simpset * solver -> simpset
    1.34 -  val addSolver: simpset * solver -> simpset
    1.35 +    proc: morphism -> Proof.context -> cterm -> thm option, identifier: thm list} -> simproc
    1.36 +  val mk_simproc: string -> cterm list -> (Proof.context -> term -> thm option) -> simproc
    1.37 +  val simpset_of: Proof.context -> simpset
    1.38 +  val put_simpset: simpset -> Proof.context -> Proof.context
    1.39 +  val global_context: theory -> simpset -> Proof.context
    1.40 +  val simpset_map: Proof.context -> (Proof.context -> Proof.context) -> simpset -> simpset
    1.41 +  val map_theory_simpset: (Proof.context -> Proof.context) -> theory -> theory
    1.42 +  val empty_simpset: Proof.context -> Proof.context
    1.43 +  val clear_simpset: Proof.context -> Proof.context
    1.44 +  val addsimps: Proof.context * thm list -> Proof.context
    1.45 +  val delsimps: Proof.context * thm list -> Proof.context
    1.46 +  val addsimprocs: Proof.context * simproc list -> Proof.context
    1.47 +  val delsimprocs: Proof.context * simproc list -> Proof.context
    1.48 +  val setloop': Proof.context * (Proof.context -> int -> tactic) -> Proof.context
    1.49 +  val setloop: Proof.context * (int -> tactic) -> Proof.context
    1.50 +  val addloop': Proof.context * (string * (Proof.context -> int -> tactic)) -> Proof.context
    1.51 +  val addloop: Proof.context * (string * (int -> tactic)) -> Proof.context
    1.52 +  val delloop: Proof.context * string -> Proof.context
    1.53 +  val setSSolver: Proof.context * solver -> Proof.context
    1.54 +  val addSSolver: Proof.context * solver -> Proof.context
    1.55 +  val setSolver: Proof.context * solver -> Proof.context
    1.56 +  val addSolver: Proof.context * solver -> Proof.context
    1.57  
    1.58    val rewrite_rule: thm list -> thm -> thm
    1.59    val rewrite_goals_rule: thm list -> thm -> thm
    1.60 @@ -71,62 +78,58 @@
    1.61     {rules: rrule Net.net,
    1.62      prems: thm list,
    1.63      bounds: int * ((string * typ) * string) list,
    1.64 -    depth: int * bool Unsynchronized.ref,
    1.65 -    context: Proof.context option} *
    1.66 +    depth: int * bool Unsynchronized.ref} *
    1.67     {congs: (cong_name * thm) list * cong_name list,
    1.68      procs: proc Net.net,
    1.69      mk_rews:
    1.70 -     {mk: simpset -> thm -> thm list,
    1.71 -      mk_cong: simpset -> thm -> thm,
    1.72 -      mk_sym: simpset -> thm -> thm option,
    1.73 -      mk_eq_True: simpset -> thm -> thm option,
    1.74 -      reorient: theory -> term list -> term -> term -> bool},
    1.75 +     {mk: Proof.context -> thm -> thm list,
    1.76 +      mk_cong: Proof.context -> thm -> thm,
    1.77 +      mk_sym: Proof.context -> thm -> thm option,
    1.78 +      mk_eq_True: Proof.context -> thm -> thm option,
    1.79 +      reorient: Proof.context -> term list -> term -> term -> bool},
    1.80      termless: term * term -> bool,
    1.81 -    subgoal_tac: simpset -> int -> tactic,
    1.82 -    loop_tacs: (string * (simpset -> int -> tactic)) list,
    1.83 +    subgoal_tac: Proof.context -> int -> tactic,
    1.84 +    loop_tacs: (string * (Proof.context -> int -> tactic)) list,
    1.85      solvers: solver list * solver list}
    1.86 -  val prems_of: simpset -> thm list
    1.87 -  val add_simp: thm -> simpset -> simpset
    1.88 -  val del_simp: thm -> simpset -> simpset
    1.89 -  val add_eqcong: thm -> simpset -> simpset
    1.90 -  val del_eqcong: thm -> simpset -> simpset
    1.91 -  val add_cong: thm -> simpset -> simpset
    1.92 -  val del_cong: thm -> simpset -> simpset
    1.93 -  val mksimps: simpset -> thm -> thm list
    1.94 -  val set_mksimps: (simpset -> thm -> thm list) -> simpset -> simpset
    1.95 -  val set_mkcong: (simpset -> thm -> thm) -> simpset -> simpset
    1.96 -  val set_mksym: (simpset -> thm -> thm option) -> simpset -> simpset
    1.97 -  val set_mkeqTrue: (simpset -> thm -> thm option) -> simpset -> simpset
    1.98 -  val set_termless: (term * term -> bool) -> simpset -> simpset
    1.99 -  val set_subgoaler: (simpset -> int -> tactic) -> simpset -> simpset
   1.100 -  val solver: simpset -> solver -> int -> tactic
   1.101 +  val map_ss: (Proof.context -> Proof.context) -> Context.generic -> Context.generic
   1.102 +  val prems_of: Proof.context -> thm list
   1.103 +  val add_simp: thm -> Proof.context -> Proof.context
   1.104 +  val del_simp: thm -> Proof.context -> Proof.context
   1.105 +  val add_eqcong: thm -> Proof.context -> Proof.context
   1.106 +  val del_eqcong: thm -> Proof.context -> Proof.context
   1.107 +  val add_cong: thm -> Proof.context -> Proof.context
   1.108 +  val del_cong: thm -> Proof.context -> Proof.context
   1.109 +  val mksimps: Proof.context -> thm -> thm list
   1.110 +  val set_mksimps: (Proof.context -> thm -> thm list) -> Proof.context -> Proof.context
   1.111 +  val set_mkcong: (Proof.context -> thm -> thm) -> Proof.context -> Proof.context
   1.112 +  val set_mksym: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context
   1.113 +  val set_mkeqTrue: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context
   1.114 +  val set_termless: (term * term -> bool) -> Proof.context -> Proof.context
   1.115 +  val set_subgoaler: (Proof.context -> int -> tactic) -> Proof.context -> Proof.context
   1.116 +  val solver: Proof.context -> solver -> int -> tactic
   1.117    val simp_depth_limit_raw: Config.raw
   1.118 -  val clear_ss: simpset -> simpset
   1.119 -  val default_mk_sym: simpset -> thm -> thm option
   1.120 -  val simproc_global_i: theory -> string -> term list
   1.121 -    -> (theory -> simpset -> term -> thm option) -> simproc
   1.122 -  val simproc_global: theory -> string -> string list
   1.123 -    -> (theory -> simpset -> term -> thm option) -> simproc
   1.124 +  val default_mk_sym: Proof.context -> thm -> thm option
   1.125 +  val simproc_global_i: theory -> string -> term list ->
   1.126 +    (Proof.context -> term -> thm option) -> simproc
   1.127 +  val simproc_global: theory -> string -> string list ->
   1.128 +    (Proof.context -> term -> thm option) -> simproc
   1.129    val simp_trace_depth_limit_raw: Config.raw
   1.130    val simp_trace_depth_limit_default: int Unsynchronized.ref
   1.131    val simp_trace_default: bool Unsynchronized.ref
   1.132    val simp_trace_raw: Config.raw
   1.133    val simp_debug_raw: Config.raw
   1.134 -  val add_prems: thm list -> simpset -> simpset
   1.135 -  val inherit_context: simpset -> simpset -> simpset
   1.136 -  val the_context: simpset -> Proof.context
   1.137 -  val context: Proof.context -> simpset -> simpset
   1.138 -  val global_context: theory -> simpset -> simpset
   1.139 -  val with_context: Proof.context -> (simpset -> simpset) -> simpset -> simpset
   1.140 +  val add_prems: thm list -> Proof.context -> Proof.context
   1.141    val debug_bounds: bool Unsynchronized.ref
   1.142 -  val set_reorient: (theory -> term list -> term -> term -> bool) -> simpset -> simpset
   1.143 -  val set_solvers: solver list -> simpset -> simpset
   1.144 -  val rewrite_cterm: bool * bool * bool -> (simpset -> thm -> thm option) -> simpset -> conv
   1.145 +  val set_reorient: (Proof.context -> term list -> term -> term -> bool) ->
   1.146 +    Proof.context -> Proof.context
   1.147 +  val set_solvers: solver list -> Proof.context -> Proof.context
   1.148 +  val rewrite_cterm: bool * bool * bool ->
   1.149 +    (Proof.context -> thm -> thm option) -> Proof.context -> conv
   1.150    val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term
   1.151    val rewrite_thm: bool * bool * bool ->
   1.152 -    (simpset -> thm -> thm option) -> simpset -> thm -> thm
   1.153 +    (Proof.context -> thm -> thm option) -> Proof.context -> thm -> thm
   1.154    val generic_rewrite_goal_tac: bool * bool * bool ->
   1.155 -    (simpset -> tactic) -> simpset -> int -> tactic
   1.156 +    (Proof.context -> tactic) -> Proof.context -> int -> tactic
   1.157    val rewrite: bool -> thm list -> conv
   1.158    val simplify: bool -> thm list -> thm -> thm
   1.159  end;
   1.160 @@ -170,223 +173,6 @@
   1.161  fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
   1.162    Thm.eq_thm_prop (thm1, thm2);
   1.163  
   1.164 -
   1.165 -(* simplification sets, procedures, and solvers *)
   1.166 -
   1.167 -(*A simpset contains data required during conversion:
   1.168 -    rules: discrimination net of rewrite rules;
   1.169 -    prems: current premises;
   1.170 -    bounds: maximal index of bound variables already used
   1.171 -      (for generating new names when rewriting under lambda abstractions);
   1.172 -    depth: simp_depth and exceeded flag;
   1.173 -    congs: association list of congruence rules and
   1.174 -           a list of `weak' congruence constants.
   1.175 -           A congruence is `weak' if it avoids normalization of some argument.
   1.176 -    procs: discrimination net of simplification procedures
   1.177 -      (functions that prove rewrite rules on the fly);
   1.178 -    mk_rews:
   1.179 -      mk: turn simplification thms into rewrite rules;
   1.180 -      mk_cong: prepare congruence rules;
   1.181 -      mk_sym: turn == around;
   1.182 -      mk_eq_True: turn P into P == True;
   1.183 -    termless: relation for ordered rewriting;*)
   1.184 -
   1.185 -datatype simpset =
   1.186 -  Simpset of
   1.187 -   {rules: rrule Net.net,
   1.188 -    prems: thm list,
   1.189 -    bounds: int * ((string * typ) * string) list,
   1.190 -    depth: int * bool Unsynchronized.ref,
   1.191 -    context: Proof.context option} *
   1.192 -   {congs: (cong_name * thm) list * cong_name list,
   1.193 -    procs: proc Net.net,
   1.194 -    mk_rews:
   1.195 -     {mk: simpset -> thm -> thm list,
   1.196 -      mk_cong: simpset -> thm -> thm,
   1.197 -      mk_sym: simpset -> thm -> thm option,
   1.198 -      mk_eq_True: simpset -> thm -> thm option,
   1.199 -      reorient: theory -> term list -> term -> term -> bool},
   1.200 -    termless: term * term -> bool,
   1.201 -    subgoal_tac: simpset -> int -> tactic,
   1.202 -    loop_tacs: (string * (simpset -> int -> tactic)) list,
   1.203 -    solvers: solver list * solver list}
   1.204 -and proc =
   1.205 -  Proc of
   1.206 -   {name: string,
   1.207 -    lhs: cterm,
   1.208 -    proc: simpset -> cterm -> thm option,
   1.209 -    id: stamp * thm list}
   1.210 -and solver =
   1.211 -  Solver of
   1.212 -   {name: string,
   1.213 -    solver: simpset -> int -> tactic,
   1.214 -    id: stamp};
   1.215 -
   1.216 -
   1.217 -fun internal_ss (Simpset args) = args;
   1.218 -
   1.219 -fun make_ss1 (rules, prems, bounds, depth, context) =
   1.220 -  {rules = rules, prems = prems, bounds = bounds, depth = depth, context = context};
   1.221 -
   1.222 -fun map_ss1 f {rules, prems, bounds, depth, context} =
   1.223 -  make_ss1 (f (rules, prems, bounds, depth, context));
   1.224 -
   1.225 -fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =
   1.226 -  {congs = congs, procs = procs, mk_rews = mk_rews, termless = termless,
   1.227 -    subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers};
   1.228 -
   1.229 -fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers} =
   1.230 -  make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers));
   1.231 -
   1.232 -fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2);
   1.233 -
   1.234 -fun map_simpset1 f (Simpset (r1, r2)) = Simpset (map_ss1 f r1, r2);
   1.235 -fun map_simpset2 f (Simpset (r1, r2)) = Simpset (r1, map_ss2 f r2);
   1.236 -
   1.237 -fun prems_of (Simpset ({prems, ...}, _)) = prems;
   1.238 -
   1.239 -fun eq_procid ((s1: stamp, ths1: thm list), (s2, ths2)) =
   1.240 -  s1 = s2 andalso eq_list Thm.eq_thm (ths1, ths2);
   1.241 -fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = eq_procid (id1, id2);
   1.242 -
   1.243 -fun mk_solver name solver = Solver {name = name, solver = solver, id = stamp ()};
   1.244 -
   1.245 -fun solver_name (Solver {name, ...}) = name;
   1.246 -fun solver ss (Solver {solver = tac, ...}) = tac ss;
   1.247 -fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
   1.248 -
   1.249 -
   1.250 -(* simp depth *)
   1.251 -
   1.252 -val simp_depth_limit_raw = Config.declare "simp_depth_limit" (K (Config.Int 100));
   1.253 -val simp_depth_limit = Config.int simp_depth_limit_raw;
   1.254 -
   1.255 -val simp_trace_depth_limit_default = Unsynchronized.ref 1;
   1.256 -val simp_trace_depth_limit_raw = Config.declare "simp_trace_depth_limit"
   1.257 -  (fn _ => Config.Int (! simp_trace_depth_limit_default));
   1.258 -val simp_trace_depth_limit = Config.int simp_trace_depth_limit_raw;
   1.259 -
   1.260 -fun simp_trace_depth_limit_of NONE = ! simp_trace_depth_limit_default
   1.261 -  | simp_trace_depth_limit_of (SOME ctxt) = Config.get ctxt simp_trace_depth_limit;
   1.262 -
   1.263 -fun trace_depth (Simpset ({depth = (depth, exceeded), context, ...}, _)) msg =
   1.264 -  if depth > simp_trace_depth_limit_of context then
   1.265 -    if ! exceeded then () else (tracing "simp_trace_depth_limit exceeded!"; exceeded := true)
   1.266 -  else
   1.267 -    (tracing (enclose "[" "]" (string_of_int depth) ^ msg); exceeded := false);
   1.268 -
   1.269 -val inc_simp_depth = map_simpset1 (fn (rules, prems, bounds, (depth, exceeded), context) =>
   1.270 -  (rules, prems, bounds,
   1.271 -    (depth + 1,
   1.272 -      if depth = simp_trace_depth_limit_of context
   1.273 -      then Unsynchronized.ref false else exceeded), context));
   1.274 -
   1.275 -fun simp_depth (Simpset ({depth = (depth, _), ...}, _)) = depth;
   1.276 -
   1.277 -
   1.278 -(* diagnostics *)
   1.279 -
   1.280 -exception SIMPLIFIER of string * thm;
   1.281 -
   1.282 -val simp_debug_raw = Config.declare "simp_debug" (K (Config.Bool false));
   1.283 -val simp_debug = Config.bool simp_debug_raw;
   1.284 -
   1.285 -val simp_trace_default = Unsynchronized.ref false;
   1.286 -val simp_trace_raw = Config.declare "simp_trace" (fn _ => Config.Bool (! simp_trace_default));
   1.287 -val simp_trace = Config.bool simp_trace_raw;
   1.288 -
   1.289 -fun if_enabled (Simpset ({context, ...}, _)) flag f =
   1.290 -  (case context of
   1.291 -    SOME ctxt => if Config.get ctxt flag then f ctxt else ()
   1.292 -  | NONE => ())
   1.293 -
   1.294 -fun if_visible (Simpset ({context, ...}, _)) f x =
   1.295 -  (case context of
   1.296 -    SOME ctxt => Context_Position.if_visible ctxt f x
   1.297 -  | NONE => ());
   1.298 -
   1.299 -local
   1.300 -
   1.301 -fun prnt ss warn a = if warn then warning a else trace_depth ss a;
   1.302 -
   1.303 -fun show_bounds (Simpset ({bounds = (_, bs), ...}, _)) t =
   1.304 -  let
   1.305 -    val names = Term.declare_term_names t Name.context;
   1.306 -    val xs = rev (#1 (fold_map Name.variant (rev (map #2 bs)) names));
   1.307 -    fun subst (((b, T), _), x') = (Free (b, T), Syntax_Trans.mark_bound_abs (x', T));
   1.308 -  in Term.subst_atomic (ListPair.map subst (bs, xs)) t end;
   1.309 -
   1.310 -fun print_term ss warn a t ctxt = prnt ss warn (a () ^ "\n" ^
   1.311 -  Syntax.string_of_term ctxt
   1.312 -    (if Config.get ctxt simp_debug then t else show_bounds ss t));
   1.313 -
   1.314 -in
   1.315 -
   1.316 -fun print_term_global ss warn a thy t =
   1.317 -  print_term ss warn (K a) t (Proof_Context.init_global thy);
   1.318 -
   1.319 -fun debug warn a ss = if_enabled ss simp_debug (fn _ => prnt ss warn (a ()));
   1.320 -fun trace warn a ss = if_enabled ss simp_trace (fn _ => prnt ss warn (a ()));
   1.321 -
   1.322 -fun debug_term warn a ss t = if_enabled ss simp_debug (print_term ss warn a t);
   1.323 -fun trace_term warn a ss t = if_enabled ss simp_trace (print_term ss warn a t);
   1.324 -
   1.325 -fun trace_cterm warn a ss ct =
   1.326 -  if_enabled ss simp_trace (print_term ss warn a (Thm.term_of ct));
   1.327 -
   1.328 -fun trace_thm a ss th =
   1.329 -  if_enabled ss simp_trace (print_term ss false a (Thm.full_prop_of th));
   1.330 -
   1.331 -fun trace_named_thm a ss (th, name) =
   1.332 -  if_enabled ss simp_trace (print_term ss false
   1.333 -    (fn () => if name = "" then a () else a () ^ " " ^ quote name ^ ":")
   1.334 -    (Thm.full_prop_of th));
   1.335 -
   1.336 -fun warn_thm a ss th =
   1.337 -  print_term_global ss true a (Thm.theory_of_thm th) (Thm.full_prop_of th);
   1.338 -
   1.339 -fun cond_warn_thm a ss th = if_visible ss (fn () => warn_thm a ss th) ();
   1.340 -
   1.341 -end;
   1.342 -
   1.343 -
   1.344 -
   1.345 -(** simpset operations **)
   1.346 -
   1.347 -(* context *)
   1.348 -
   1.349 -fun eq_bound (x: string, (y, _)) = x = y;
   1.350 -
   1.351 -fun add_bound bound = map_simpset1 (fn (rules, prems, (count, bounds), depth, context) =>
   1.352 -  (rules, prems, (count + 1, bound :: bounds), depth, context));
   1.353 -
   1.354 -fun add_prems ths = map_simpset1 (fn (rules, prems, bounds, depth, context) =>
   1.355 -  (rules, ths @ prems, bounds, depth, context));
   1.356 -
   1.357 -fun inherit_context (Simpset ({bounds, depth, context, ...}, _)) =
   1.358 -  map_simpset1 (fn (rules, prems, _, _, _) => (rules, prems, bounds, depth, context));
   1.359 -
   1.360 -fun the_context (Simpset ({context = SOME ctxt, ...}, _)) = ctxt
   1.361 -  | the_context _ = raise Fail "Simplifier: no proof context in simpset";
   1.362 -
   1.363 -fun context ctxt =
   1.364 -  map_simpset1 (fn (rules, prems, bounds, depth, _) => (rules, prems, bounds, depth, SOME ctxt));
   1.365 -
   1.366 -val global_context = context o Proof_Context.init_global;
   1.367 -
   1.368 -fun activate_context thy ss =
   1.369 -  let
   1.370 -    val ctxt = the_context ss;
   1.371 -    val ctxt' = ctxt
   1.372 -      |> Context.raw_transfer (Theory.merge (thy, Proof_Context.theory_of ctxt))
   1.373 -      |> Context_Position.set_visible false;
   1.374 -  in context ctxt' ss end;
   1.375 -
   1.376 -fun with_context ctxt f ss = inherit_context ss (f (context ctxt ss));
   1.377 -
   1.378 -
   1.379 -(* maintain simp rules *)
   1.380 -
   1.381  (* FIXME: it seems that the conditions on extra variables are too liberal if
   1.382  prems are nonempty: does solving the prems really guarantee instantiation of
   1.383  all its Vars? Better: a dynamic check each time a rule is applied.
   1.384 @@ -413,30 +199,8 @@
   1.385      val extra = rrule_extra_vars elhs thm;
   1.386    in {thm = thm, name = name, lhs = lhs, elhs = elhs, extra = extra, fo = fo, perm = perm} end;
   1.387  
   1.388 -fun del_rrule (rrule as {thm, elhs, ...}) ss =
   1.389 -  ss |> map_simpset1 (fn (rules, prems, bounds, depth, context) =>
   1.390 -    (Net.delete_term eq_rrule (term_of elhs, rrule) rules, prems, bounds, depth, context))
   1.391 -  handle Net.DELETE => (cond_warn_thm "Rewrite rule not in simpset:" ss thm; ss);
   1.392 -
   1.393 -fun insert_rrule (rrule as {thm, name, ...}) ss =
   1.394 - (trace_named_thm (fn () => "Adding rewrite rule") ss (thm, name);
   1.395 -  ss |> map_simpset1 (fn (rules, prems, bounds, depth, context) =>
   1.396 -    let
   1.397 -      val rrule2 as {elhs, ...} = mk_rrule2 rrule;
   1.398 -      val rules' = Net.insert_term eq_rrule (term_of elhs, rrule2) rules;
   1.399 -    in (rules', prems, bounds, depth, context) end)
   1.400 -  handle Net.INSERT => (cond_warn_thm "Ignoring duplicate rewrite rule:" ss thm; ss));
   1.401 -
   1.402 -fun vperm (Var _, Var _) = true
   1.403 -  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
   1.404 -  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
   1.405 -  | vperm (t, u) = (t = u);
   1.406 -
   1.407 -fun var_perm (t, u) =
   1.408 -  vperm (t, u) andalso eq_set (op =) (Term.add_vars t [], Term.add_vars u []);
   1.409 -
   1.410  (*simple test for looping rewrite rules and stupid orientations*)
   1.411 -fun default_reorient thy prems lhs rhs =
   1.412 +fun default_reorient ctxt prems lhs rhs =
   1.413    rewrite_rule_extra_vars prems lhs rhs
   1.414      orelse
   1.415    is_Var (head_of lhs)
   1.416 @@ -449,13 +213,338 @@
   1.417  *)
   1.418    exists (fn t => Logic.occs (lhs, t)) (rhs :: prems)
   1.419      orelse
   1.420 -  null prems andalso Pattern.matches thy (lhs, rhs)
   1.421 +  null prems andalso Pattern.matches (Proof_Context.theory_of ctxt) (lhs, rhs)
   1.422      (*the condition "null prems" is necessary because conditional rewrites
   1.423        with extra variables in the conditions may terminate although
   1.424        the rhs is an instance of the lhs; example: ?m < ?n ==> f(?n) == f(?m)*)
   1.425      orelse
   1.426    is_Const lhs andalso not (is_Const rhs);
   1.427  
   1.428 +
   1.429 +(* simplification procedures *)
   1.430 +
   1.431 +datatype proc =
   1.432 +  Proc of
   1.433 +   {name: string,
   1.434 +    lhs: cterm,
   1.435 +    proc: Proof.context -> cterm -> thm option,
   1.436 +    id: stamp * thm list};
   1.437 +
   1.438 +fun eq_procid ((s1: stamp, ths1: thm list), (s2, ths2)) =
   1.439 +  s1 = s2 andalso eq_list Thm.eq_thm (ths1, ths2);
   1.440 +
   1.441 +fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = eq_procid (id1, id2);
   1.442 +
   1.443 +
   1.444 +(* solvers *)
   1.445 +
   1.446 +datatype solver =
   1.447 +  Solver of
   1.448 +   {name: string,
   1.449 +    solver: Proof.context -> int -> tactic,
   1.450 +    id: stamp};
   1.451 +
   1.452 +fun mk_solver name solver = Solver {name = name, solver = solver, id = stamp ()};
   1.453 +
   1.454 +fun solver_name (Solver {name, ...}) = name;
   1.455 +fun solver ctxt (Solver {solver = tac, ...}) = tac ctxt;
   1.456 +fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
   1.457 +
   1.458 +
   1.459 +(* simplification sets *)
   1.460 +
   1.461 +(*A simpset contains data required during conversion:
   1.462 +    rules: discrimination net of rewrite rules;
   1.463 +    prems: current premises;
   1.464 +    bounds: maximal index of bound variables already used
   1.465 +      (for generating new names when rewriting under lambda abstractions);
   1.466 +    depth: simp_depth and exceeded flag;
   1.467 +    congs: association list of congruence rules and
   1.468 +           a list of `weak' congruence constants.
   1.469 +           A congruence is `weak' if it avoids normalization of some argument.
   1.470 +    procs: discrimination net of simplification procedures
   1.471 +      (functions that prove rewrite rules on the fly);
   1.472 +    mk_rews:
   1.473 +      mk: turn simplification thms into rewrite rules;
   1.474 +      mk_cong: prepare congruence rules;
   1.475 +      mk_sym: turn == around;
   1.476 +      mk_eq_True: turn P into P == True;
   1.477 +    termless: relation for ordered rewriting;*)
   1.478 +
   1.479 +datatype simpset =
   1.480 +  Simpset of
   1.481 +   {rules: rrule Net.net,
   1.482 +    prems: thm list,
   1.483 +    bounds: int * ((string * typ) * string) list,
   1.484 +    depth: int * bool Unsynchronized.ref} *
   1.485 +   {congs: (cong_name * thm) list * cong_name list,
   1.486 +    procs: proc Net.net,
   1.487 +    mk_rews:
   1.488 +     {mk: Proof.context -> thm -> thm list,
   1.489 +      mk_cong: Proof.context -> thm -> thm,
   1.490 +      mk_sym: Proof.context -> thm -> thm option,
   1.491 +      mk_eq_True: Proof.context -> thm -> thm option,
   1.492 +      reorient: Proof.context -> term list -> term -> term -> bool},
   1.493 +    termless: term * term -> bool,
   1.494 +    subgoal_tac: Proof.context -> int -> tactic,
   1.495 +    loop_tacs: (string * (Proof.context -> int -> tactic)) list,
   1.496 +    solvers: solver list * solver list};
   1.497 +
   1.498 +fun internal_ss (Simpset args) = args;
   1.499 +
   1.500 +fun make_ss1 (rules, prems, bounds, depth) =
   1.501 +  {rules = rules, prems = prems, bounds = bounds, depth = depth};
   1.502 +
   1.503 +fun map_ss1 f {rules, prems, bounds, depth} =
   1.504 +  make_ss1 (f (rules, prems, bounds, depth));
   1.505 +
   1.506 +fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =
   1.507 +  {congs = congs, procs = procs, mk_rews = mk_rews, termless = termless,
   1.508 +    subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers};
   1.509 +
   1.510 +fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers} =
   1.511 +  make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers));
   1.512 +
   1.513 +fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2);
   1.514 +
   1.515 +fun dest_ss (Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...})) =
   1.516 + {simps = Net.entries rules
   1.517 +    |> map (fn {name, thm, ...} => (name, thm)),
   1.518 +  procs = Net.entries procs
   1.519 +    |> map (fn Proc {name, lhs, id, ...} => ((name, lhs), id))
   1.520 +    |> partition_eq (eq_snd eq_procid)
   1.521 +    |> map (fn ps => (fst (fst (hd ps)), map (snd o fst) ps)),
   1.522 +  congs = #1 congs,
   1.523 +  weak_congs = #2 congs,
   1.524 +  loopers = map fst loop_tacs,
   1.525 +  unsafe_solvers = map solver_name (#1 solvers),
   1.526 +  safe_solvers = map solver_name (#2 solvers)};
   1.527 +
   1.528 +
   1.529 +(* empty *)
   1.530 +
   1.531 +fun init_ss mk_rews termless subgoal_tac solvers =
   1.532 +  make_simpset ((Net.empty, [], (0, []), (0, Unsynchronized.ref false)),
   1.533 +    (([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers));
   1.534 +
   1.535 +fun default_mk_sym _ th = SOME (th RS Drule.symmetric_thm);
   1.536 +
   1.537 +val empty_ss =
   1.538 +  init_ss
   1.539 +    {mk = fn _ => fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [],
   1.540 +      mk_cong = K I,
   1.541 +      mk_sym = default_mk_sym,
   1.542 +      mk_eq_True = K (K NONE),
   1.543 +      reorient = default_reorient}
   1.544 +    Term_Ord.termless (K (K no_tac)) ([], []);
   1.545 +
   1.546 +
   1.547 +(* merge *)  (*NOTE: ignores some fields of 2nd simpset*)
   1.548 +
   1.549 +fun merge_ss (ss1, ss2) =
   1.550 +  if pointer_eq (ss1, ss2) then ss1
   1.551 +  else
   1.552 +    let
   1.553 +      val Simpset ({rules = rules1, prems = prems1, bounds = bounds1, depth = depth1},
   1.554 +       {congs = (congs1, weak1), procs = procs1, mk_rews, termless, subgoal_tac,
   1.555 +        loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1;
   1.556 +      val Simpset ({rules = rules2, prems = prems2, bounds = bounds2, depth = depth2},
   1.557 +       {congs = (congs2, weak2), procs = procs2, mk_rews = _, termless = _, subgoal_tac = _,
   1.558 +        loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2;
   1.559 +
   1.560 +      val rules' = Net.merge eq_rrule (rules1, rules2);
   1.561 +      val prems' = Thm.merge_thms (prems1, prems2);
   1.562 +      val bounds' = if #1 bounds1 < #1 bounds2 then bounds2 else bounds1;
   1.563 +      val depth' = if #1 depth1 < #1 depth2 then depth2 else depth1;
   1.564 +      val congs' = merge (Thm.eq_thm_prop o pairself #2) (congs1, congs2);
   1.565 +      val weak' = merge (op =) (weak1, weak2);
   1.566 +      val procs' = Net.merge eq_proc (procs1, procs2);
   1.567 +      val loop_tacs' = AList.merge (op =) (K true) (loop_tacs1, loop_tacs2);
   1.568 +      val unsafe_solvers' = merge eq_solver (unsafe_solvers1, unsafe_solvers2);
   1.569 +      val solvers' = merge eq_solver (solvers1, solvers2);
   1.570 +    in
   1.571 +      make_simpset ((rules', prems', bounds', depth'), ((congs', weak'), procs',
   1.572 +        mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers')))
   1.573 +    end;
   1.574 +
   1.575 +
   1.576 +
   1.577 +(** context data **)
   1.578 +
   1.579 +structure Simpset = Generic_Data
   1.580 +(
   1.581 +  type T = simpset;
   1.582 +  val empty = empty_ss;
   1.583 +  val extend = I;
   1.584 +  val merge = merge_ss;
   1.585 +);
   1.586 +
   1.587 +val simpset_of = Simpset.get o Context.Proof;
   1.588 +
   1.589 +fun map_simpset f = Context.proof_map (Simpset.map f);
   1.590 +fun map_simpset1 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (map_ss1 f ss1, ss2));
   1.591 +fun map_simpset2 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (ss1, map_ss2 f ss2));
   1.592 +
   1.593 +fun simpset_map ctxt f ss = ctxt |> map_simpset (K ss) |> f |> Context.Proof |> Simpset.get;
   1.594 +
   1.595 +fun put_simpset (Simpset ({rules, prems, ...}, ss2)) =  (* FIXME prems from context (!?) *)
   1.596 +  map_simpset (fn Simpset ({bounds, depth, ...}, _) =>
   1.597 +    Simpset (make_ss1 (rules, prems, bounds, depth), ss2));
   1.598 +
   1.599 +fun global_context thy ss = Proof_Context.init_global thy |> put_simpset ss;
   1.600 +
   1.601 +val empty_simpset = put_simpset empty_ss;
   1.602 +
   1.603 +fun map_theory_simpset f thy =
   1.604 +  let
   1.605 +    val ctxt' = f (Proof_Context.init_global thy);
   1.606 +    val thy' = Proof_Context.theory_of ctxt';
   1.607 +  in Context.theory_map (Simpset.map (K (simpset_of ctxt'))) thy' end;
   1.608 +
   1.609 +fun map_ss f = Context.mapping (map_theory_simpset f) f;
   1.610 +
   1.611 +val clear_simpset =
   1.612 +  map_simpset (fn Simpset (_, {mk_rews, termless, subgoal_tac, solvers, ...}) =>
   1.613 +    init_ss mk_rews termless subgoal_tac solvers);
   1.614 +
   1.615 +
   1.616 +(* simp depth *)
   1.617 +
   1.618 +val simp_depth_limit_raw = Config.declare "simp_depth_limit" (K (Config.Int 100));
   1.619 +val simp_depth_limit = Config.int simp_depth_limit_raw;
   1.620 +
   1.621 +val simp_trace_depth_limit_default = Unsynchronized.ref 1;
   1.622 +val simp_trace_depth_limit_raw = Config.declare "simp_trace_depth_limit"
   1.623 +  (fn _ => Config.Int (! simp_trace_depth_limit_default));
   1.624 +val simp_trace_depth_limit = Config.int simp_trace_depth_limit_raw;
   1.625 +
   1.626 +fun trace_depth ctxt msg =
   1.627 +  let
   1.628 +    val Simpset ({depth = (depth, exceeded), ...}, _) = simpset_of ctxt;
   1.629 +    val depth_limit = Config.get ctxt simp_trace_depth_limit;
   1.630 +  in
   1.631 +    if depth > depth_limit then
   1.632 +      if ! exceeded then () else (tracing "simp_trace_depth_limit exceeded!"; exceeded := true)
   1.633 +    else (tracing (enclose "[" "]" (string_of_int depth) ^ msg); exceeded := false)
   1.634 +  end;
   1.635 +
   1.636 +fun inc_simp_depth ctxt =
   1.637 +  ctxt |> map_simpset1 (fn (rules, prems, bounds, (depth, exceeded)) =>
   1.638 +    (rules, prems, bounds,
   1.639 +      (depth + 1,
   1.640 +        if depth = Config.get ctxt simp_trace_depth_limit
   1.641 +        then Unsynchronized.ref false else exceeded)));
   1.642 +
   1.643 +fun simp_depth ctxt =
   1.644 +  let val Simpset ({depth = (depth, _), ...}, _) = simpset_of ctxt
   1.645 +  in depth end;
   1.646 +
   1.647 +
   1.648 +(* diagnostics *)
   1.649 +
   1.650 +exception SIMPLIFIER of string * thm;
   1.651 +
   1.652 +val simp_debug_raw = Config.declare "simp_debug" (K (Config.Bool false));
   1.653 +val simp_debug = Config.bool simp_debug_raw;
   1.654 +
   1.655 +val simp_trace_default = Unsynchronized.ref false;
   1.656 +val simp_trace_raw = Config.declare "simp_trace" (fn _ => Config.Bool (! simp_trace_default));
   1.657 +val simp_trace = Config.bool simp_trace_raw;
   1.658 +
   1.659 +fun if_enabled ctxt flag f = if Config.get ctxt flag then f ctxt else ();
   1.660 +
   1.661 +local
   1.662 +
   1.663 +fun prnt ctxt warn a = if warn then warning a else trace_depth ctxt a;
   1.664 +
   1.665 +fun show_bounds ctxt t =
   1.666 +  let
   1.667 +    val Simpset ({bounds = (_, bs), ...}, _) = simpset_of ctxt;
   1.668 +    val names = Term.declare_term_names t Name.context;
   1.669 +    val xs = rev (#1 (fold_map Name.variant (rev (map #2 bs)) names));
   1.670 +    fun subst (((b, T), _), x') = (Free (b, T), Syntax_Trans.mark_bound_abs (x', T));
   1.671 +  in Term.subst_atomic (ListPair.map subst (bs, xs)) t end;
   1.672 +
   1.673 +in
   1.674 +
   1.675 +fun print_term ctxt warn a t =
   1.676 +  prnt ctxt warn (a () ^ "\n" ^
   1.677 +    Syntax.string_of_term ctxt (if Config.get ctxt simp_debug then t else show_bounds ctxt t));
   1.678 +
   1.679 +fun debug ctxt warn a = if_enabled ctxt simp_debug (fn _ => prnt ctxt warn (a ()));
   1.680 +fun trace ctxt warn a = if_enabled ctxt simp_trace (fn _ => prnt ctxt warn (a ()));
   1.681 +
   1.682 +fun debug_term ctxt warn a t = if_enabled ctxt simp_debug (fn _ => print_term ctxt warn a t);
   1.683 +fun trace_term ctxt warn a t = if_enabled ctxt simp_trace (fn _ => print_term ctxt warn a t);
   1.684 +
   1.685 +fun trace_cterm ctxt warn a ct =
   1.686 +  if_enabled ctxt simp_trace (fn _ => print_term ctxt warn a (Thm.term_of ct));
   1.687 +
   1.688 +fun trace_thm ctxt a th =
   1.689 +  if_enabled ctxt simp_trace (fn _ => print_term ctxt false a (Thm.full_prop_of th));
   1.690 +
   1.691 +fun trace_named_thm ctxt a (th, name) =
   1.692 +  if_enabled ctxt simp_trace (fn _ =>
   1.693 +    print_term ctxt false
   1.694 +      (fn () => if name = "" then a () else a () ^ " " ^ quote name ^ ":")
   1.695 +      (Thm.full_prop_of th));
   1.696 +
   1.697 +fun warn_thm ctxt a th = print_term ctxt true a (Thm.full_prop_of th);
   1.698 +fun cond_warn_thm ctxt a th = Context_Position.if_visible ctxt (fn () => warn_thm ctxt a th) ();
   1.699 +
   1.700 +end;
   1.701 +
   1.702 +
   1.703 +
   1.704 +(** simpset operations **)
   1.705 +
   1.706 +(* context *)
   1.707 +
   1.708 +fun eq_bound (x: string, (y, _)) = x = y;
   1.709 +
   1.710 +fun add_bound bound =
   1.711 +  map_simpset1 (fn (rules, prems, (count, bounds), depth) =>
   1.712 +    (rules, prems, (count + 1, bound :: bounds), depth));
   1.713 +
   1.714 +fun prems_of ctxt =
   1.715 +  let val Simpset ({prems, ...}, _) = simpset_of ctxt in prems end;
   1.716 +
   1.717 +fun add_prems ths =
   1.718 +  map_simpset1 (fn (rules, prems, bounds, depth) => (rules, ths @ prems, bounds, depth));
   1.719 +
   1.720 +fun activate_context thy ctxt = ctxt  (* FIXME ?? *)
   1.721 +  |> Context.raw_transfer (Theory.merge (thy, Proof_Context.theory_of ctxt))
   1.722 +  |> Context_Position.set_visible false;
   1.723 +
   1.724 +
   1.725 +(* maintain simp rules *)
   1.726 +
   1.727 +fun del_rrule (rrule as {thm, elhs, ...}) ctxt =
   1.728 +  ctxt |> map_simpset1 (fn (rules, prems, bounds, depth) =>
   1.729 +    (Net.delete_term eq_rrule (term_of elhs, rrule) rules, prems, bounds, depth))
   1.730 +  handle Net.DELETE => (cond_warn_thm ctxt (fn () => "Rewrite rule not in simpset:") thm; ctxt);
   1.731 +
   1.732 +fun insert_rrule (rrule as {thm, name, ...}) ctxt =
   1.733 + (trace_named_thm ctxt (fn () => "Adding rewrite rule") (thm, name);
   1.734 +  ctxt |> map_simpset1 (fn (rules, prems, bounds, depth) =>
   1.735 +    let
   1.736 +      val rrule2 as {elhs, ...} = mk_rrule2 rrule;
   1.737 +      val rules' = Net.insert_term eq_rrule (term_of elhs, rrule2) rules;
   1.738 +    in (rules', prems, bounds, depth) end)
   1.739 +  handle Net.INSERT => (cond_warn_thm ctxt (fn () => "Ignoring duplicate rewrite rule:") thm; ctxt));
   1.740 +
   1.741 +local
   1.742 +
   1.743 +fun vperm (Var _, Var _) = true
   1.744 +  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
   1.745 +  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
   1.746 +  | vperm (t, u) = (t = u);
   1.747 +
   1.748 +fun var_perm (t, u) =
   1.749 +  vperm (t, u) andalso eq_set (op =) (Term.add_vars t [], Term.add_vars u []);
   1.750 +
   1.751 +in
   1.752 +
   1.753  fun decomp_simp thm =
   1.754    let
   1.755      val thy = Thm.theory_of_thm thm;
   1.756 @@ -472,79 +561,83 @@
   1.757        not (is_Var (term_of elhs));
   1.758    in (thy, prems, term_of lhs, elhs, term_of rhs, perm) end;
   1.759  
   1.760 +end;
   1.761 +
   1.762  fun decomp_simp' thm =
   1.763    let val (_, _, lhs, _, rhs, _) = decomp_simp thm in
   1.764      if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", thm)
   1.765      else (lhs, rhs)
   1.766    end;
   1.767  
   1.768 -fun mk_eq_True (ss as Simpset (_, {mk_rews = {mk_eq_True, ...}, ...})) (thm, name) =
   1.769 -  (case mk_eq_True ss thm of
   1.770 -    NONE => []
   1.771 -  | SOME eq_True =>
   1.772 -      let
   1.773 -        val (_, _, lhs, elhs, _, _) = decomp_simp eq_True;
   1.774 -      in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end);
   1.775 +fun mk_eq_True ctxt (thm, name) =
   1.776 +  let val Simpset (_, {mk_rews = {mk_eq_True, ...}, ...}) = simpset_of ctxt in
   1.777 +    (case mk_eq_True ctxt thm of
   1.778 +      NONE => []
   1.779 +    | SOME eq_True =>
   1.780 +        let val (_, _, lhs, elhs, _, _) = decomp_simp eq_True;
   1.781 +        in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end)
   1.782 +  end;
   1.783  
   1.784  (*create the rewrite rule and possibly also the eq_True variant,
   1.785    in case there are extra vars on the rhs*)
   1.786 -fun rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm2) =
   1.787 +fun rrule_eq_True (thm, name, lhs, elhs, rhs, ctxt, thm2) =
   1.788    let val rrule = {thm = thm, name = name, lhs = lhs, elhs = elhs, perm = false} in
   1.789      if rewrite_rule_extra_vars [] lhs rhs then
   1.790 -      mk_eq_True ss (thm2, name) @ [rrule]
   1.791 +      mk_eq_True ctxt (thm2, name) @ [rrule]
   1.792      else [rrule]
   1.793    end;
   1.794  
   1.795 -fun mk_rrule ss (thm, name) =
   1.796 +fun mk_rrule ctxt (thm, name) =
   1.797    let val (_, prems, lhs, elhs, rhs, perm) = decomp_simp thm in
   1.798      if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
   1.799      else
   1.800        (*weak test for loops*)
   1.801        if rewrite_rule_extra_vars prems lhs rhs orelse is_Var (term_of elhs)
   1.802 -      then mk_eq_True ss (thm, name)
   1.803 -      else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm)
   1.804 +      then mk_eq_True ctxt (thm, name)
   1.805 +      else rrule_eq_True (thm, name, lhs, elhs, rhs, ctxt, thm)
   1.806    end;
   1.807  
   1.808 -fun orient_rrule ss (thm, name) =
   1.809 +fun orient_rrule ctxt (thm, name) =
   1.810    let
   1.811      val (thy, prems, lhs, elhs, rhs, perm) = decomp_simp thm;
   1.812 -    val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = ss;
   1.813 +    val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = simpset_of ctxt;
   1.814    in
   1.815      if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
   1.816 -    else if reorient thy prems lhs rhs then
   1.817 -      if reorient thy prems rhs lhs
   1.818 -      then mk_eq_True ss (thm, name)
   1.819 +    else if reorient ctxt prems lhs rhs then
   1.820 +      if reorient ctxt prems rhs lhs
   1.821 +      then mk_eq_True ctxt (thm, name)
   1.822        else
   1.823 -        (case mk_sym ss thm of
   1.824 +        (case mk_sym ctxt thm of
   1.825            NONE => []
   1.826          | SOME thm' =>
   1.827              let val (_, _, lhs', elhs', rhs', _) = decomp_simp thm'
   1.828 -            in rrule_eq_True (thm', name, lhs', elhs', rhs', ss, thm) end)
   1.829 -    else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm)
   1.830 +            in rrule_eq_True (thm', name, lhs', elhs', rhs', ctxt, thm) end)
   1.831 +    else rrule_eq_True (thm, name, lhs, elhs, rhs, ctxt, thm)
   1.832    end;
   1.833  
   1.834 -fun extract_rews (ss as Simpset (_, {mk_rews = {mk, ...}, ...}), thms) =
   1.835 -  maps (fn thm => map (rpair (Thm.get_name_hint thm)) (mk ss thm)) thms;
   1.836 +fun extract_rews (ctxt, thms) =
   1.837 +  let val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt
   1.838 +  in maps (fn thm => map (rpair (Thm.get_name_hint thm)) (mk ctxt thm)) thms end;
   1.839  
   1.840 -fun extract_safe_rrules (ss, thm) =
   1.841 -  maps (orient_rrule ss) (extract_rews (ss, [thm]));
   1.842 +fun extract_safe_rrules (ctxt, thm) =
   1.843 +  maps (orient_rrule ctxt) (extract_rews (ctxt, [thm]));
   1.844  
   1.845  
   1.846  (* add/del rules explicitly *)
   1.847  
   1.848 -fun comb_simps comb mk_rrule (ss, thms) =
   1.849 +fun comb_simps comb mk_rrule (ctxt, thms) =
   1.850    let
   1.851 -    val rews = extract_rews (ss, thms);
   1.852 -  in fold (fold comb o mk_rrule) rews ss end;
   1.853 +    val rews = extract_rews (ctxt, thms);
   1.854 +  in fold (fold comb o mk_rrule) rews ctxt end;
   1.855  
   1.856 -fun ss addsimps thms =
   1.857 -  comb_simps insert_rrule (mk_rrule ss) (ss, thms);
   1.858 +fun ctxt addsimps thms =
   1.859 +  comb_simps insert_rrule (mk_rrule ctxt) (ctxt, thms);
   1.860  
   1.861 -fun ss delsimps thms =
   1.862 -  comb_simps del_rrule (map mk_rrule2 o mk_rrule ss) (ss, thms);
   1.863 +fun ctxt delsimps thms =
   1.864 +  comb_simps del_rrule (map mk_rrule2 o mk_rrule ctxt) (ctxt, thms);
   1.865  
   1.866 -fun add_simp thm ss = ss addsimps [thm];
   1.867 -fun del_simp thm ss = ss delsimps [thm];
   1.868 +fun add_simp thm ctxt = ctxt addsimps [thm];
   1.869 +fun del_simp thm ctxt = ctxt delsimps [thm];
   1.870  
   1.871  
   1.872  (* congs *)
   1.873 @@ -574,11 +667,13 @@
   1.874      is_full_cong_prems prems (xs ~~ ys)
   1.875    end;
   1.876  
   1.877 -fun mk_cong (ss as Simpset (_, {mk_rews = {mk_cong = f, ...}, ...})) = f ss;
   1.878 +fun mk_cong ctxt =
   1.879 +  let val Simpset (_, {mk_rews = {mk_cong = f, ...}, ...}) = simpset_of ctxt
   1.880 +  in f ctxt end;
   1.881  
   1.882  in
   1.883  
   1.884 -fun add_eqcong thm ss = ss |>
   1.885 +fun add_eqcong thm ctxt = ctxt |>
   1.886    map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.887      let
   1.888        val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
   1.889 @@ -588,14 +683,15 @@
   1.890          raise SIMPLIFIER ("Congruence must start with a constant or free variable", thm);
   1.891        val (xs, weak) = congs;
   1.892        val _ =
   1.893 -        if AList.defined (op =) xs a
   1.894 -        then if_visible ss warning ("Overwriting congruence rule for " ^ quote (#2 a))
   1.895 +        if AList.defined (op =) xs a then
   1.896 +          Context_Position.if_visible ctxt
   1.897 +            warning ("Overwriting congruence rule for " ^ quote (#2 a))
   1.898          else ();
   1.899        val xs' = AList.update (op =) (a, thm) xs;
   1.900        val weak' = if is_full_cong thm then weak else a :: weak;
   1.901      in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end);
   1.902  
   1.903 -fun del_eqcong thm ss = ss |>
   1.904 +fun del_eqcong thm ctxt = ctxt |>
   1.905    map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.906      let
   1.907        val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
   1.908 @@ -609,8 +705,8 @@
   1.909          if is_full_cong thm then NONE else SOME a);
   1.910      in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end);
   1.911  
   1.912 -fun add_cong thm ss = add_eqcong (mk_cong ss thm) ss;
   1.913 -fun del_cong thm ss = del_eqcong (mk_cong ss thm) ss;
   1.914 +fun add_cong thm ctxt = add_eqcong (mk_cong ctxt thm) ctxt;
   1.915 +fun del_cong thm ctxt = del_eqcong (mk_cong ctxt thm) ctxt;
   1.916  
   1.917  end;
   1.918  
   1.919 @@ -621,7 +717,7 @@
   1.920    Simproc of
   1.921      {name: string,
   1.922       lhss: cterm list,
   1.923 -     proc: morphism -> simpset -> cterm -> thm option,
   1.924 +     proc: morphism -> Proof.context -> cterm -> thm option,
   1.925       id: stamp * thm list};
   1.926  
   1.927  fun eq_simproc (Simproc {id = id1, ...}, Simproc {id = id2, ...}) = eq_procid (id1, id2);
   1.928 @@ -637,8 +733,8 @@
   1.929    Simproc {name = name, lhss = lhss, proc = proc, id = (stamp (), identifier)};
   1.930  
   1.931  fun mk_simproc name lhss proc =
   1.932 -  make_simproc {name = name, lhss = lhss, proc = fn _ => fn ss => fn ct =>
   1.933 -    proc (Proof_Context.theory_of (the_context ss)) ss (Thm.term_of ct), identifier = []};
   1.934 +  make_simproc {name = name, lhss = lhss, proc = fn _ => fn ctxt => fn ct =>
   1.935 +    proc ctxt (Thm.term_of ct), identifier = []};
   1.936  
   1.937  (* FIXME avoid global thy and Logic.varify_global *)
   1.938  fun simproc_global_i thy name = mk_simproc name o map (Thm.cterm_of thy o Logic.varify_global);
   1.939 @@ -647,28 +743,30 @@
   1.940  
   1.941  local
   1.942  
   1.943 -fun add_proc (proc as Proc {name, lhs, ...}) ss =
   1.944 - (trace_cterm false (fn () => "Adding simplification procedure " ^ quote name ^ " for") ss lhs;
   1.945 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.946 +fun add_proc (proc as Proc {name, lhs, ...}) ctxt =
   1.947 + (trace_cterm ctxt false (fn () => "Adding simplification procedure " ^ quote name ^ " for") lhs;
   1.948 +  ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.949      (congs, Net.insert_term eq_proc (term_of lhs, proc) procs,
   1.950 -      mk_rews, termless, subgoal_tac, loop_tacs, solvers)) ss
   1.951 +      mk_rews, termless, subgoal_tac, loop_tacs, solvers))
   1.952    handle Net.INSERT =>
   1.953 -    (if_visible ss warning ("Ignoring duplicate simplification procedure " ^ quote name); ss));
   1.954 +    (Context_Position.if_visible ctxt
   1.955 +      warning ("Ignoring duplicate simplification procedure " ^ quote name); ctxt));
   1.956  
   1.957 -fun del_proc (proc as Proc {name, lhs, ...}) ss =
   1.958 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.959 +fun del_proc (proc as Proc {name, lhs, ...}) ctxt =
   1.960 +  ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.961      (congs, Net.delete_term eq_proc (term_of lhs, proc) procs,
   1.962 -      mk_rews, termless, subgoal_tac, loop_tacs, solvers)) ss
   1.963 +      mk_rews, termless, subgoal_tac, loop_tacs, solvers))
   1.964    handle Net.DELETE =>
   1.965 -    (if_visible ss warning ("Simplification procedure " ^ quote name ^ " not in simpset"); ss);
   1.966 +    (Context_Position.if_visible ctxt
   1.967 +      warning ("Simplification procedure " ^ quote name ^ " not in simpset"); ctxt);
   1.968  
   1.969  fun prep_procs (Simproc {name, lhss, proc, id}) =
   1.970    lhss |> map (fn lhs => Proc {name = name, lhs = lhs, proc = Morphism.form proc, id = id});
   1.971  
   1.972  in
   1.973  
   1.974 -fun ss addsimprocs ps = fold (fold add_proc o prep_procs) ps ss;
   1.975 -fun ss delsimprocs ps = fold (fold del_proc o prep_procs) ps ss;
   1.976 +fun ctxt addsimprocs ps = fold (fold add_proc o prep_procs) ps ctxt;
   1.977 +fun ctxt delsimprocs ps = fold (fold del_proc o prep_procs) ps ctxt;
   1.978  
   1.979  end;
   1.980  
   1.981 @@ -688,7 +786,9 @@
   1.982  
   1.983  in
   1.984  
   1.985 -fun mksimps (ss as Simpset (_, {mk_rews = {mk, ...}, ...})) = mk ss;
   1.986 +fun mksimps ctxt =
   1.987 +  let val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt
   1.988 +  in mk ctxt end;
   1.989  
   1.990  fun set_mksimps mk = map_mk_rews (fn (_, mk_cong, mk_sym, mk_eq_True, reorient) =>
   1.991    (mk, mk_cong, mk_sym, mk_eq_True, reorient));
   1.992 @@ -721,39 +821,41 @@
   1.993    map_simpset2 (fn (congs, procs, mk_rews, termless, _, loop_tacs, solvers) =>
   1.994     (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers));
   1.995  
   1.996 -fun ss setloop' tac = ss |>
   1.997 +fun ctxt setloop' tac = ctxt |>
   1.998    map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, _, solvers) =>
   1.999     (congs, procs, mk_rews, termless, subgoal_tac, [("", tac)], solvers));
  1.1000  
  1.1001 -fun ss setloop tac = ss setloop' (K tac);
  1.1002 +fun ctxt setloop tac = ctxt setloop' (K tac);
  1.1003  
  1.1004 -fun ss addloop' (name, tac) = ss |>
  1.1005 +fun ctxt addloop' (name, tac) = ctxt |>
  1.1006    map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
  1.1007      (congs, procs, mk_rews, termless, subgoal_tac,
  1.1008       AList.update (op =) (name, tac) loop_tacs, solvers));
  1.1009  
  1.1010 -fun ss addloop (name, tac) = ss addloop' (name, K tac);
  1.1011 +fun ctxt addloop (name, tac) = ctxt addloop' (name, K tac);
  1.1012  
  1.1013 -fun ss delloop name = ss |>
  1.1014 +fun ctxt delloop name = ctxt |>
  1.1015    map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
  1.1016      (congs, procs, mk_rews, termless, subgoal_tac,
  1.1017       (if AList.defined (op =) loop_tacs name then ()
  1.1018 -      else if_visible ss warning ("No such looper in simpset: " ^ quote name);
  1.1019 -      AList.delete (op =) name loop_tacs), solvers));
  1.1020 +      else
  1.1021 +        Context_Position.if_visible ctxt
  1.1022 +          warning ("No such looper in simpset: " ^ quote name);
  1.1023 +        AList.delete (op =) name loop_tacs), solvers));
  1.1024  
  1.1025 -fun ss setSSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1026 +fun ctxt setSSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1027    subgoal_tac, loop_tacs, (unsafe_solvers, _)) =>
  1.1028      (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, [solver])));
  1.1029  
  1.1030 -fun ss addSSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1031 +fun ctxt addSSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1032    subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless,
  1.1033      subgoal_tac, loop_tacs, (unsafe_solvers, insert eq_solver solver solvers)));
  1.1034  
  1.1035 -fun ss setSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1036 +fun ctxt setSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1037    subgoal_tac, loop_tacs, (_, solvers)) => (congs, procs, mk_rews, termless,
  1.1038      subgoal_tac, loop_tacs, ([solver], solvers)));
  1.1039  
  1.1040 -fun ss addSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1041 +fun ctxt addSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
  1.1042    subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless,
  1.1043      subgoal_tac, loop_tacs, (insert eq_solver solver unsafe_solvers, solvers)));
  1.1044  
  1.1045 @@ -762,73 +864,6 @@
  1.1046    subgoal_tac, loop_tacs, (solvers, solvers)));
  1.1047  
  1.1048  
  1.1049 -(* empty *)
  1.1050 -
  1.1051 -fun init_ss mk_rews termless subgoal_tac solvers =
  1.1052 -  make_simpset ((Net.empty, [], (0, []), (0, Unsynchronized.ref false), NONE),
  1.1053 -    (([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers));
  1.1054 -
  1.1055 -fun clear_ss (ss as Simpset (_, {mk_rews, termless, subgoal_tac, solvers, ...})) =
  1.1056 -  init_ss mk_rews termless subgoal_tac solvers
  1.1057 -  |> inherit_context ss;
  1.1058 -
  1.1059 -fun default_mk_sym _ th = SOME (th RS Drule.symmetric_thm);
  1.1060 -
  1.1061 -val empty_ss =
  1.1062 -  init_ss
  1.1063 -    {mk = fn _ => fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [],
  1.1064 -      mk_cong = K I,
  1.1065 -      mk_sym = default_mk_sym,
  1.1066 -      mk_eq_True = K (K NONE),
  1.1067 -      reorient = default_reorient}
  1.1068 -    Term_Ord.termless (K (K no_tac)) ([], []);
  1.1069 -
  1.1070 -
  1.1071 -(* merge *)  (*NOTE: ignores some fields of 2nd simpset*)
  1.1072 -
  1.1073 -fun merge_ss (ss1, ss2) =
  1.1074 -  if pointer_eq (ss1, ss2) then ss1
  1.1075 -  else
  1.1076 -    let
  1.1077 -      val Simpset ({rules = rules1, prems = prems1, bounds = bounds1, depth = depth1, context = _},
  1.1078 -       {congs = (congs1, weak1), procs = procs1, mk_rews, termless, subgoal_tac,
  1.1079 -        loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1;
  1.1080 -      val Simpset ({rules = rules2, prems = prems2, bounds = bounds2, depth = depth2, context = _},
  1.1081 -       {congs = (congs2, weak2), procs = procs2, mk_rews = _, termless = _, subgoal_tac = _,
  1.1082 -        loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2;
  1.1083 -
  1.1084 -      val rules' = Net.merge eq_rrule (rules1, rules2);
  1.1085 -      val prems' = Thm.merge_thms (prems1, prems2);
  1.1086 -      val bounds' = if #1 bounds1 < #1 bounds2 then bounds2 else bounds1;
  1.1087 -      val depth' = if #1 depth1 < #1 depth2 then depth2 else depth1;
  1.1088 -      val congs' = merge (Thm.eq_thm_prop o pairself #2) (congs1, congs2);
  1.1089 -      val weak' = merge (op =) (weak1, weak2);
  1.1090 -      val procs' = Net.merge eq_proc (procs1, procs2);
  1.1091 -      val loop_tacs' = AList.merge (op =) (K true) (loop_tacs1, loop_tacs2);
  1.1092 -      val unsafe_solvers' = merge eq_solver (unsafe_solvers1, unsafe_solvers2);
  1.1093 -      val solvers' = merge eq_solver (solvers1, solvers2);
  1.1094 -    in
  1.1095 -      make_simpset ((rules', prems', bounds', depth', NONE), ((congs', weak'), procs',
  1.1096 -        mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers')))
  1.1097 -    end;
  1.1098 -
  1.1099 -
  1.1100 -(* dest_ss *)
  1.1101 -
  1.1102 -fun dest_ss (Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...})) =
  1.1103 - {simps = Net.entries rules
  1.1104 -    |> map (fn {name, thm, ...} => (name, thm)),
  1.1105 -  procs = Net.entries procs
  1.1106 -    |> map (fn Proc {name, lhs, id, ...} => ((name, lhs), id))
  1.1107 -    |> partition_eq (eq_snd eq_procid)
  1.1108 -    |> map (fn ps => (fst (fst (hd ps)), map (snd o fst) ps)),
  1.1109 -  congs = #1 congs,
  1.1110 -  weak_congs = #2 congs,
  1.1111 -  loopers = map fst loop_tacs,
  1.1112 -  unsafe_solvers = map solver_name (#1 solvers),
  1.1113 -  safe_solvers = map solver_name (#2 solvers)};
  1.1114 -
  1.1115 -
  1.1116  
  1.1117  (** rewriting **)
  1.1118  
  1.1119 @@ -838,28 +873,28 @@
  1.1120      Science of Computer Programming 3 (1983), pages 119-149.
  1.1121  *)
  1.1122  
  1.1123 -fun check_conv msg ss thm thm' =
  1.1124 +fun check_conv ctxt msg thm thm' =
  1.1125    let
  1.1126      val thm'' = Thm.transitive thm thm' handle THM _ =>
  1.1127       Thm.transitive thm (Thm.transitive
  1.1128         (Thm.symmetric (Drule.beta_eta_conversion (Thm.lhs_of thm'))) thm')
  1.1129 -  in if msg then trace_thm (fn () => "SUCCEEDED") ss thm' else (); SOME thm'' end
  1.1130 +  in if msg then trace_thm ctxt (fn () => "SUCCEEDED") thm' else (); SOME thm'' end
  1.1131    handle THM _ =>
  1.1132      let
  1.1133        val _ $ _ $ prop0 = Thm.prop_of thm;
  1.1134      in
  1.1135 -      trace_thm (fn () => "Proved wrong thm (Check subgoaler?)") ss thm';
  1.1136 -      trace_term false (fn () => "Should have proved:") ss prop0;
  1.1137 +      trace_thm ctxt (fn () => "Proved wrong thm (Check subgoaler?)") thm';
  1.1138 +      trace_term ctxt false (fn () => "Should have proved:") prop0;
  1.1139        NONE
  1.1140      end;
  1.1141  
  1.1142  
  1.1143  (* mk_procrule *)
  1.1144  
  1.1145 -fun mk_procrule ss thm =
  1.1146 +fun mk_procrule ctxt thm =
  1.1147    let val (_, prems, lhs, elhs, rhs, _) = decomp_simp thm in
  1.1148      if rewrite_rule_extra_vars prems lhs rhs
  1.1149 -    then (cond_warn_thm "Extra vars on rhs:" ss thm; [])
  1.1150 +    then (cond_warn_thm ctxt (fn () => "Extra vars on rhs:") thm; [])
  1.1151      else [mk_rrule2 {thm = thm, name = "", lhs = lhs, elhs = elhs, perm = false}]
  1.1152    end;
  1.1153  
  1.1154 @@ -905,10 +940,9 @@
  1.1155    IMPORTANT: rewrite rules must not introduce new Vars or TVars!
  1.1156  *)
  1.1157  
  1.1158 -fun rewritec (prover, thyt, maxt) ss t =
  1.1159 +fun rewritec (prover, thyt, maxt) ctxt t =
  1.1160    let
  1.1161 -    val ctxt = the_context ss;
  1.1162 -    val Simpset ({rules, ...}, {congs, procs, termless, ...}) = ss;
  1.1163 +    val Simpset ({rules, ...}, {congs, procs, termless, ...}) = simpset_of ctxt;
  1.1164      val eta_thm = Thm.eta_conversion t;
  1.1165      val eta_t' = Thm.rhs_of eta_thm;
  1.1166      val eta_t = term_of eta_t';
  1.1167 @@ -927,36 +961,37 @@
  1.1168          val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop')
  1.1169        in
  1.1170          if perm andalso not (termless (rhs', lhs'))
  1.1171 -        then (trace_named_thm (fn () => "Cannot apply permutative rewrite rule") ss (thm, name);
  1.1172 -              trace_thm (fn () => "Term does not become smaller:") ss thm'; NONE)
  1.1173 -        else (trace_named_thm (fn () => "Applying instance of rewrite rule") ss (thm, name);
  1.1174 +        then (trace_named_thm ctxt (fn () => "Cannot apply permutative rewrite rule") (thm, name);
  1.1175 +              trace_thm ctxt (fn () => "Term does not become smaller:") thm'; NONE)
  1.1176 +        else (trace_named_thm ctxt (fn () => "Applying instance of rewrite rule") (thm, name);
  1.1177             if unconditional
  1.1178             then
  1.1179 -             (trace_thm (fn () => "Rewriting:") ss thm';
  1.1180 +             (trace_thm ctxt (fn () => "Rewriting:") thm';
  1.1181                let
  1.1182                  val lr = Logic.dest_equals prop;
  1.1183 -                val SOME thm'' = check_conv false ss eta_thm thm';
  1.1184 +                val SOME thm'' = check_conv ctxt false eta_thm thm';
  1.1185                in SOME (thm'', uncond_skel (congs, lr)) end)
  1.1186             else
  1.1187 -             (trace_thm (fn () => "Trying to rewrite:") ss thm';
  1.1188 -              if simp_depth ss > Config.get ctxt simp_depth_limit
  1.1189 +             (trace_thm ctxt (fn () => "Trying to rewrite:") thm';
  1.1190 +              if simp_depth ctxt > Config.get ctxt simp_depth_limit
  1.1191                then
  1.1192                  let
  1.1193                    val s = "simp_depth_limit exceeded - giving up";
  1.1194 -                  val _ = trace false (fn () => s) ss;
  1.1195 -                  val _ = if_visible ss warning s;
  1.1196 +                  val _ = trace ctxt false (fn () => s);
  1.1197 +                  val _ = Context_Position.if_visible ctxt warning s;
  1.1198                  in NONE end
  1.1199                else
  1.1200 -              case prover ss thm' of
  1.1201 -                NONE => (trace_thm (fn () => "FAILED") ss thm'; NONE)
  1.1202 +              case prover ctxt thm' of
  1.1203 +                NONE => (trace_thm ctxt (fn () => "FAILED") thm'; NONE)
  1.1204                | SOME thm2 =>
  1.1205 -                  (case check_conv true ss eta_thm thm2 of
  1.1206 -                     NONE => NONE |
  1.1207 -                     SOME thm2' =>
  1.1208 -                       let val concl = Logic.strip_imp_concl prop
  1.1209 -                           val lr = Logic.dest_equals concl
  1.1210 -                       in SOME (thm2', cond_skel (congs, lr)) end)))
  1.1211 -      end
  1.1212 +                  (case check_conv ctxt true eta_thm thm2 of
  1.1213 +                    NONE => NONE
  1.1214 +                  | SOME thm2' =>
  1.1215 +                      let
  1.1216 +                        val concl = Logic.strip_imp_concl prop;
  1.1217 +                        val lr = Logic.dest_equals concl;
  1.1218 +                      in SOME (thm2', cond_skel (congs, lr)) end)))
  1.1219 +      end;
  1.1220  
  1.1221      fun rews [] = NONE
  1.1222        | rews (rrule :: rrules) =
  1.1223 @@ -979,15 +1014,15 @@
  1.1224      fun proc_rews [] = NONE
  1.1225        | proc_rews (Proc {name, proc, lhs, ...} :: ps) =
  1.1226            if Pattern.matches thyt (Thm.term_of lhs, Thm.term_of t) then
  1.1227 -            (debug_term false (fn () => "Trying procedure " ^ quote name ^ " on:") ss eta_t;
  1.1228 -             case proc ss eta_t' of
  1.1229 -               NONE => (debug false (fn () => "FAILED") ss; proc_rews ps)
  1.1230 +            (debug_term ctxt false (fn () => "Trying procedure " ^ quote name ^ " on:") eta_t;
  1.1231 +             case proc ctxt eta_t' of
  1.1232 +               NONE => (debug ctxt false (fn () => "FAILED"); proc_rews ps)
  1.1233               | SOME raw_thm =>
  1.1234 -                 (trace_thm (fn () => "Procedure " ^ quote name ^ " produced rewrite rule:")
  1.1235 -                   ss raw_thm;
  1.1236 -                  (case rews (mk_procrule ss raw_thm) of
  1.1237 -                    NONE => (trace_cterm true (fn () => "IGNORED result of simproc " ^ quote name ^
  1.1238 -                      " -- does not match") ss t; proc_rews ps)
  1.1239 +                 (trace_thm ctxt (fn () => "Procedure " ^ quote name ^ " produced rewrite rule:")
  1.1240 +                   raw_thm;
  1.1241 +                  (case rews (mk_procrule ctxt raw_thm) of
  1.1242 +                    NONE => (trace_cterm ctxt true (fn () => "IGNORED result of simproc " ^ quote name ^
  1.1243 +                      " -- does not match") t; proc_rews ps)
  1.1244                    | some => some)))
  1.1245            else proc_rews ps;
  1.1246    in
  1.1247 @@ -1002,20 +1037,21 @@
  1.1248  
  1.1249  (* conversion to apply a congruence rule to a term *)
  1.1250  
  1.1251 -fun congc prover ss maxt cong t =
  1.1252 -  let val rthm = Thm.incr_indexes (maxt + 1) cong;
  1.1253 -      val rlhs = fst (Thm.dest_equals (Drule.strip_imp_concl (cprop_of rthm)));
  1.1254 -      val insts = Thm.match (rlhs, t)
  1.1255 -      (* Thm.match can raise Pattern.MATCH;
  1.1256 -         is handled when congc is called *)
  1.1257 -      val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm);
  1.1258 -      val _ = trace_thm (fn () => "Applying congruence rule:") ss thm';
  1.1259 -      fun err (msg, thm) = (trace_thm (fn () => msg) ss thm; NONE)
  1.1260 +fun congc prover ctxt maxt cong t =
  1.1261 +  let
  1.1262 +    val rthm = Thm.incr_indexes (maxt + 1) cong;
  1.1263 +    val rlhs = fst (Thm.dest_equals (Drule.strip_imp_concl (cprop_of rthm)));
  1.1264 +    val insts = Thm.match (rlhs, t)
  1.1265 +    (* Thm.match can raise Pattern.MATCH;
  1.1266 +       is handled when congc is called *)
  1.1267 +    val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm);
  1.1268 +    val _ = trace_thm ctxt (fn () => "Applying congruence rule:") thm';
  1.1269 +    fun err (msg, thm) = (trace_thm ctxt (fn () => msg) thm; NONE);
  1.1270    in
  1.1271      (case prover thm' of
  1.1272        NONE => err ("Congruence proof failed.  Could not prove", thm')
  1.1273      | SOME thm2 =>
  1.1274 -        (case check_conv true ss (Drule.beta_eta_conversion t) thm2 of
  1.1275 +        (case check_conv ctxt true (Drule.beta_eta_conversion t) thm2 of
  1.1276            NONE => err ("Congruence proof failed.  Should not have proved", thm2)
  1.1277          | SOME thm2' =>
  1.1278              if op aconv (pairself term_of (Thm.dest_equals (cprop_of thm2')))
  1.1279 @@ -1035,108 +1071,111 @@
  1.1280  
  1.1281  fun bottomc ((simprem, useprem, mutsimp), prover, thy, maxidx) =
  1.1282    let
  1.1283 -    fun botc skel ss t =
  1.1284 +    fun botc skel ctxt t =
  1.1285            if is_Var skel then NONE
  1.1286            else
  1.1287 -          (case subc skel ss t of
  1.1288 +          (case subc skel ctxt t of
  1.1289               some as SOME thm1 =>
  1.1290 -               (case rewritec (prover, thy, maxidx) ss (Thm.rhs_of thm1) of
  1.1291 +               (case rewritec (prover, thy, maxidx) ctxt (Thm.rhs_of thm1) of
  1.1292                    SOME (thm2, skel2) =>
  1.1293                      transitive2 (Thm.transitive thm1 thm2)
  1.1294 -                      (botc skel2 ss (Thm.rhs_of thm2))
  1.1295 +                      (botc skel2 ctxt (Thm.rhs_of thm2))
  1.1296                  | NONE => some)
  1.1297             | NONE =>
  1.1298 -               (case rewritec (prover, thy, maxidx) ss t of
  1.1299 +               (case rewritec (prover, thy, maxidx) ctxt t of
  1.1300                    SOME (thm2, skel2) => transitive2 thm2
  1.1301 -                    (botc skel2 ss (Thm.rhs_of thm2))
  1.1302 +                    (botc skel2 ctxt (Thm.rhs_of thm2))
  1.1303                  | NONE => NONE))
  1.1304  
  1.1305 -    and try_botc ss t =
  1.1306 -          (case botc skel0 ss t of
  1.1307 -             SOME trec1 => trec1 | NONE => (Thm.reflexive t))
  1.1308 +    and try_botc ctxt t =
  1.1309 +          (case botc skel0 ctxt t of
  1.1310 +            SOME trec1 => trec1
  1.1311 +          | NONE => (Thm.reflexive t))
  1.1312  
  1.1313 -    and subc skel (ss as Simpset ({bounds, ...}, {congs, ...})) t0 =
  1.1314 -       (case term_of t0 of
  1.1315 -           Abs (a, T, _) =>
  1.1316 -             let
  1.1317 -                 val b = Name.bound (#1 bounds);
  1.1318 -                 val (v, t') = Thm.dest_abs (SOME b) t0;
  1.1319 -                 val b' = #1 (Term.dest_Free (Thm.term_of v));
  1.1320 -                 val _ =
  1.1321 -                   if b <> b' then
  1.1322 -                     warning ("Simplifier: renamed bound variable " ^
  1.1323 -                       quote b ^ " to " ^ quote b' ^ Position.here (Position.thread_data ()))
  1.1324 -                   else ();
  1.1325 -                 val ss' = add_bound ((b', T), a) ss;
  1.1326 -                 val skel' = case skel of Abs (_, _, sk) => sk | _ => skel0;
  1.1327 -             in case botc skel' ss' t' of
  1.1328 -                  SOME thm => SOME (Thm.abstract_rule a v thm)
  1.1329 -                | NONE => NONE
  1.1330 -             end
  1.1331 -         | t $ _ => (case t of
  1.1332 -             Const ("==>", _) $ _  => impc t0 ss
  1.1333 -           | Abs _ =>
  1.1334 -               let val thm = Thm.beta_conversion false t0
  1.1335 -               in case subc skel0 ss (Thm.rhs_of thm) of
  1.1336 -                    NONE => SOME thm
  1.1337 -                  | SOME thm' => SOME (Thm.transitive thm thm')
  1.1338 -               end
  1.1339 -           | _  =>
  1.1340 -               let fun appc () =
  1.1341 -                     let
  1.1342 -                       val (tskel, uskel) = case skel of
  1.1343 -                           tskel $ uskel => (tskel, uskel)
  1.1344 -                         | _ => (skel0, skel0);
  1.1345 -                       val (ct, cu) = Thm.dest_comb t0
  1.1346 -                     in
  1.1347 -                     (case botc tskel ss ct of
  1.1348 -                        SOME thm1 =>
  1.1349 -                          (case botc uskel ss cu of
  1.1350 -                             SOME thm2 => SOME (Thm.combination thm1 thm2)
  1.1351 -                           | NONE => SOME (Thm.combination thm1 (Thm.reflexive cu)))
  1.1352 -                      | NONE =>
  1.1353 -                          (case botc uskel ss cu of
  1.1354 -                             SOME thm1 => SOME (Thm.combination (Thm.reflexive ct) thm1)
  1.1355 -                           | NONE => NONE))
  1.1356 -                     end
  1.1357 -                   val (h, ts) = strip_comb t
  1.1358 -               in case cong_name h of
  1.1359 -                    SOME a =>
  1.1360 -                      (case AList.lookup (op =) (fst congs) a of
  1.1361 -                         NONE => appc ()
  1.1362 -                       | SOME cong =>
  1.1363 -  (*post processing: some partial applications h t1 ... tj, j <= length ts,
  1.1364 -    may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*)
  1.1365 -                          (let
  1.1366 -                             val thm = congc (prover ss) ss maxidx cong t0;
  1.1367 -                             val t = the_default t0 (Option.map Thm.rhs_of thm);
  1.1368 -                             val (cl, cr) = Thm.dest_comb t
  1.1369 -                             val dVar = Var(("", 0), dummyT)
  1.1370 -                             val skel =
  1.1371 -                               list_comb (h, replicate (length ts) dVar)
  1.1372 -                           in case botc skel ss cl of
  1.1373 -                                NONE => thm
  1.1374 -                              | SOME thm' => transitive3 thm
  1.1375 -                                  (Thm.combination thm' (Thm.reflexive cr))
  1.1376 -                           end handle Pattern.MATCH => appc ()))
  1.1377 -                  | _ => appc ()
  1.1378 -               end)
  1.1379 -         | _ => NONE)
  1.1380 +    and subc skel ctxt t0 =
  1.1381 +        let val Simpset ({bounds, ...}, {congs, ...}) = simpset_of ctxt in
  1.1382 +          (case term_of t0 of
  1.1383 +              Abs (a, T, _) =>
  1.1384 +                let
  1.1385 +                    val b = Name.bound (#1 bounds);
  1.1386 +                    val (v, t') = Thm.dest_abs (SOME b) t0;
  1.1387 +                    val b' = #1 (Term.dest_Free (Thm.term_of v));
  1.1388 +                    val _ =
  1.1389 +                      if b <> b' then
  1.1390 +                        warning ("Simplifier: renamed bound variable " ^
  1.1391 +                          quote b ^ " to " ^ quote b' ^ Position.here (Position.thread_data ()))
  1.1392 +                      else ();
  1.1393 +                    val ctxt' = add_bound ((b', T), a) ctxt;
  1.1394 +                    val skel' = case skel of Abs (_, _, sk) => sk | _ => skel0;
  1.1395 +                in
  1.1396 +                  (case botc skel' ctxt' t' of
  1.1397 +                    SOME thm => SOME (Thm.abstract_rule a v thm)
  1.1398 +                  | NONE => NONE)
  1.1399 +                end
  1.1400 +            | t $ _ => (case t of
  1.1401 +                Const ("==>", _) $ _  => impc t0 ctxt
  1.1402 +              | Abs _ =>
  1.1403 +                  let val thm = Thm.beta_conversion false t0
  1.1404 +                  in case subc skel0 ctxt (Thm.rhs_of thm) of
  1.1405 +                       NONE => SOME thm
  1.1406 +                     | SOME thm' => SOME (Thm.transitive thm thm')
  1.1407 +                  end
  1.1408 +              | _  =>
  1.1409 +                  let fun appc () =
  1.1410 +                        let
  1.1411 +                          val (tskel, uskel) = case skel of
  1.1412 +                              tskel $ uskel => (tskel, uskel)
  1.1413 +                            | _ => (skel0, skel0);
  1.1414 +                          val (ct, cu) = Thm.dest_comb t0
  1.1415 +                        in
  1.1416 +                        (case botc tskel ctxt ct of
  1.1417 +                           SOME thm1 =>
  1.1418 +                             (case botc uskel ctxt cu of
  1.1419 +                                SOME thm2 => SOME (Thm.combination thm1 thm2)
  1.1420 +                              | NONE => SOME (Thm.combination thm1 (Thm.reflexive cu)))
  1.1421 +                         | NONE =>
  1.1422 +                             (case botc uskel ctxt cu of
  1.1423 +                                SOME thm1 => SOME (Thm.combination (Thm.reflexive ct) thm1)
  1.1424 +                              | NONE => NONE))
  1.1425 +                        end
  1.1426 +                      val (h, ts) = strip_comb t
  1.1427 +                  in case cong_name h of
  1.1428 +                       SOME a =>
  1.1429 +                         (case AList.lookup (op =) (fst congs) a of
  1.1430 +                            NONE => appc ()
  1.1431 +                          | SOME cong =>
  1.1432 +     (*post processing: some partial applications h t1 ... tj, j <= length ts,
  1.1433 +       may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*)
  1.1434 +                             (let
  1.1435 +                                val thm = congc (prover ctxt) ctxt maxidx cong t0;
  1.1436 +                                val t = the_default t0 (Option.map Thm.rhs_of thm);
  1.1437 +                                val (cl, cr) = Thm.dest_comb t
  1.1438 +                                val dVar = Var(("", 0), dummyT)
  1.1439 +                                val skel =
  1.1440 +                                  list_comb (h, replicate (length ts) dVar)
  1.1441 +                              in case botc skel ctxt cl of
  1.1442 +                                   NONE => thm
  1.1443 +                                 | SOME thm' => transitive3 thm
  1.1444 +                                     (Thm.combination thm' (Thm.reflexive cr))
  1.1445 +                              end handle Pattern.MATCH => appc ()))
  1.1446 +                     | _ => appc ()
  1.1447 +                  end)
  1.1448 +            | _ => NONE)
  1.1449 +        end
  1.1450 +    and impc ct ctxt =
  1.1451 +      if mutsimp then mut_impc0 [] ct [] [] ctxt else nonmut_impc ct ctxt
  1.1452  
  1.1453 -    and impc ct ss =
  1.1454 -      if mutsimp then mut_impc0 [] ct [] [] ss else nonmut_impc ct ss
  1.1455 -
  1.1456 -    and rules_of_prem ss prem =
  1.1457 +    and rules_of_prem ctxt prem =
  1.1458        if maxidx_of_term (term_of prem) <> ~1
  1.1459 -      then (trace_cterm true
  1.1460 +      then (trace_cterm ctxt true
  1.1461          (fn () => "Cannot add premise as rewrite rule because it contains (type) unknowns:")
  1.1462 -          ss prem; ([], NONE))
  1.1463 +        prem; ([], NONE))
  1.1464        else
  1.1465          let val asm = Thm.assume prem
  1.1466 -        in (extract_safe_rrules (ss, asm), SOME asm) end
  1.1467 +        in (extract_safe_rrules (ctxt, asm), SOME asm) end
  1.1468  
  1.1469 -    and add_rrules (rrss, asms) ss =
  1.1470 -      (fold o fold) insert_rrule rrss ss |> add_prems (map_filter I asms)
  1.1471 +    and add_rrules (rrss, asms) ctxt =
  1.1472 +      (fold o fold) insert_rrule rrss ctxt |> add_prems (map_filter I asms)
  1.1473  
  1.1474      and disch r prem eq =
  1.1475        let
  1.1476 @@ -1157,44 +1196,44 @@
  1.1477        end
  1.1478  
  1.1479      and rebuild [] _ _ _ _ eq = eq
  1.1480 -      | rebuild (prem :: prems) concl (_ :: rrss) (_ :: asms) ss eq =
  1.1481 +      | rebuild (prem :: prems) concl (_ :: rrss) (_ :: asms) ctxt eq =
  1.1482            let
  1.1483 -            val ss' = add_rrules (rev rrss, rev asms) ss;
  1.1484 +            val ctxt' = add_rrules (rev rrss, rev asms) ctxt;
  1.1485              val concl' =
  1.1486                Drule.mk_implies (prem, the_default concl (Option.map Thm.rhs_of eq));
  1.1487              val dprem = Option.map (disch false prem)
  1.1488            in
  1.1489 -            (case rewritec (prover, thy, maxidx) ss' concl' of
  1.1490 -              NONE => rebuild prems concl' rrss asms ss (dprem eq)
  1.1491 +            (case rewritec (prover, thy, maxidx) ctxt' concl' of
  1.1492 +              NONE => rebuild prems concl' rrss asms ctxt (dprem eq)
  1.1493              | SOME (eq', _) => transitive2 (fold (disch false)
  1.1494                    prems (the (transitive3 (dprem eq) eq')))
  1.1495 -                (mut_impc0 (rev prems) (Thm.rhs_of eq') (rev rrss) (rev asms) ss))
  1.1496 +                (mut_impc0 (rev prems) (Thm.rhs_of eq') (rev rrss) (rev asms) ctxt))
  1.1497            end
  1.1498  
  1.1499 -    and mut_impc0 prems concl rrss asms ss =
  1.1500 +    and mut_impc0 prems concl rrss asms ctxt =
  1.1501        let
  1.1502          val prems' = strip_imp_prems concl;
  1.1503 -        val (rrss', asms') = split_list (map (rules_of_prem ss) prems')
  1.1504 +        val (rrss', asms') = split_list (map (rules_of_prem ctxt) prems')
  1.1505        in
  1.1506          mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss')
  1.1507 -          (asms @ asms') [] [] [] [] ss ~1 ~1
  1.1508 +          (asms @ asms') [] [] [] [] ctxt ~1 ~1
  1.1509        end
  1.1510  
  1.1511 -    and mut_impc [] concl [] [] prems' rrss' asms' eqns ss changed k =
  1.1512 +    and mut_impc [] concl [] [] prems' rrss' asms' eqns ctxt changed k =
  1.1513          transitive1 (fold (fn (eq1, prem) => fn eq2 => transitive1 eq1
  1.1514              (Option.map (disch false prem) eq2)) (eqns ~~ prems') NONE)
  1.1515            (if changed > 0 then
  1.1516               mut_impc (rev prems') concl (rev rrss') (rev asms')
  1.1517 -               [] [] [] [] ss ~1 changed
  1.1518 -           else rebuild prems' concl rrss' asms' ss
  1.1519 -             (botc skel0 (add_rrules (rev rrss', rev asms') ss) concl))
  1.1520 +               [] [] [] [] ctxt ~1 changed
  1.1521 +           else rebuild prems' concl rrss' asms' ctxt
  1.1522 +             (botc skel0 (add_rrules (rev rrss', rev asms') ctxt) concl))
  1.1523  
  1.1524        | mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms)
  1.1525 -          prems' rrss' asms' eqns ss changed k =
  1.1526 +          prems' rrss' asms' eqns ctxt changed k =
  1.1527          case (if k = 0 then NONE else botc skel0 (add_rrules
  1.1528 -          (rev rrss' @ rrss, rev asms' @ asms) ss) prem) of
  1.1529 +          (rev rrss' @ rrss, rev asms' @ asms) ctxt) prem) of
  1.1530              NONE => mut_impc prems concl rrss asms (prem :: prems')
  1.1531 -              (rrs :: rrss') (asm :: asms') (NONE :: eqns) ss changed
  1.1532 +              (rrs :: rrss') (asm :: asms') (NONE :: eqns) ctxt changed
  1.1533                (if k = 0 then 0 else k - 1)
  1.1534            | SOME eqn =>
  1.1535              let
  1.1536 @@ -1202,26 +1241,26 @@
  1.1537                val tprems = map term_of prems;
  1.1538                val i = 1 + fold Integer.max (map (fn p =>
  1.1539                  find_index (fn q => q aconv p) tprems) (Thm.hyps_of eqn)) ~1;
  1.1540 -              val (rrs', asm') = rules_of_prem ss prem'
  1.1541 +              val (rrs', asm') = rules_of_prem ctxt prem';
  1.1542              in mut_impc prems concl rrss asms (prem' :: prems')
  1.1543                (rrs' :: rrss') (asm' :: asms') (SOME (fold_rev (disch true)
  1.1544                  (take i prems)
  1.1545                  (Drule.imp_cong_rule eqn (Thm.reflexive (Drule.list_implies
  1.1546                    (drop i prems, concl))))) :: eqns)
  1.1547 -                  ss (length prems') ~1
  1.1548 +                  ctxt (length prems') ~1
  1.1549              end
  1.1550  
  1.1551       (*legacy code - only for backwards compatibility*)
  1.1552 -    and nonmut_impc ct ss =
  1.1553 +    and nonmut_impc ct ctxt =
  1.1554        let
  1.1555          val (prem, conc) = Thm.dest_implies ct;
  1.1556 -        val thm1 = if simprem then botc skel0 ss prem else NONE;
  1.1557 +        val thm1 = if simprem then botc skel0 ctxt prem else NONE;
  1.1558          val prem1 = the_default prem (Option.map Thm.rhs_of thm1);
  1.1559 -        val ss1 =
  1.1560 -          if not useprem then ss
  1.1561 -          else add_rrules (apsnd single (apfst single (rules_of_prem ss prem1))) ss
  1.1562 +        val ctxt1 =
  1.1563 +          if not useprem then ctxt
  1.1564 +          else add_rrules (apsnd single (apfst single (rules_of_prem ctxt prem1))) ctxt
  1.1565        in
  1.1566 -        (case botc skel0 ss1 conc of
  1.1567 +        (case botc skel0 ctxt1 conc of
  1.1568            NONE =>
  1.1569              (case thm1 of
  1.1570                NONE => NONE
  1.1571 @@ -1251,42 +1290,47 @@
  1.1572  
  1.1573  val debug_bounds = Unsynchronized.ref false;
  1.1574  
  1.1575 -fun check_bounds ss ct =
  1.1576 +fun check_bounds ctxt ct =
  1.1577    if ! debug_bounds then
  1.1578      let
  1.1579 -      val Simpset ({bounds = (_, bounds), ...}, _) = ss;
  1.1580 -      val bs = fold_aterms (fn Free (x, _) =>
  1.1581 -          if Name.is_bound x andalso not (AList.defined eq_bound bounds x)
  1.1582 -          then insert (op =) x else I
  1.1583 -        | _ => I) (term_of ct) [];
  1.1584 +      val Simpset ({bounds = (_, bounds), ...}, _) = simpset_of ctxt;
  1.1585 +      val bs =
  1.1586 +        fold_aterms
  1.1587 +          (fn Free (x, _) =>
  1.1588 +            if Name.is_bound x andalso not (AList.defined eq_bound bounds x)
  1.1589 +            then insert (op =) x else I
  1.1590 +          | _ => I) (term_of ct) [];
  1.1591      in
  1.1592        if null bs then ()
  1.1593 -      else print_term_global ss true ("Simplifier: term contains loose bounds: " ^ commas_quote bs)
  1.1594 -        (Thm.theory_of_cterm ct) (Thm.term_of ct)
  1.1595 +      else
  1.1596 +        print_term ctxt true
  1.1597 +          (fn () => "Simplifier: term contains loose bounds: " ^ commas_quote bs)
  1.1598 +          (Thm.term_of ct)
  1.1599      end
  1.1600    else ();
  1.1601  
  1.1602 -fun rewrite_cterm mode prover raw_ss raw_ct =
  1.1603 +fun rewrite_cterm mode prover raw_ctxt raw_ct =
  1.1604    let
  1.1605      val thy = Thm.theory_of_cterm raw_ct;
  1.1606      val ct = Thm.adjust_maxidx_cterm ~1 raw_ct;
  1.1607      val {maxidx, ...} = Thm.rep_cterm ct;
  1.1608 -    val ss = inc_simp_depth (activate_context thy raw_ss);
  1.1609 -    val depth = simp_depth ss;
  1.1610 +    val ctxt = inc_simp_depth (activate_context thy raw_ctxt);
  1.1611 +    val depth = simp_depth ctxt;
  1.1612      val _ =
  1.1613        if depth mod 20 = 0 then
  1.1614 -        if_visible ss warning ("Simplification depth " ^ string_of_int depth)
  1.1615 +        Context_Position.if_visible ctxt warning ("Simplification depth " ^ string_of_int depth)
  1.1616        else ();
  1.1617 -    val _ = trace_cterm false (fn () => "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:") ss ct;
  1.1618 -    val _ = check_bounds ss ct;
  1.1619 -  in bottomc (mode, Option.map Drule.flexflex_unique oo prover, thy, maxidx) ss ct end;
  1.1620 +    val _ = trace_cterm ctxt false (fn () => "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:") ct;
  1.1621 +    val _ = check_bounds ctxt ct;
  1.1622 +  in bottomc (mode, Option.map Drule.flexflex_unique oo prover, thy, maxidx) ctxt ct end;
  1.1623  
  1.1624  val simple_prover =
  1.1625 -  SINGLE o (fn ss => ALLGOALS (resolve_tac (prems_of ss)));
  1.1626 +  SINGLE o (fn ctxt => ALLGOALS (resolve_tac (prems_of ctxt)));
  1.1627  
  1.1628  fun rewrite _ [] ct = Thm.reflexive ct
  1.1629 -  | rewrite full thms ct = rewrite_cterm (full, false, false) simple_prover
  1.1630 -      (global_context (Thm.theory_of_cterm ct) empty_ss addsimps thms) ct;
  1.1631 +  | rewrite full thms ct =
  1.1632 +      rewrite_cterm (full, false, false) simple_prover
  1.1633 +        (global_context (Thm.theory_of_cterm ct) empty_ss addsimps thms) ct;
  1.1634  
  1.1635  fun simplify full thms = Conv.fconv_rule (rewrite full thms);
  1.1636  val rewrite_rule = simplify true;
  1.1637 @@ -1295,7 +1339,7 @@
  1.1638  fun rewrite_term thy rules procs =
  1.1639    Pattern.rewrite_term thy (map decomp_simp' rules) procs;
  1.1640  
  1.1641 -fun rewrite_thm mode prover ss = Conv.fconv_rule (rewrite_cterm mode prover ss);
  1.1642 +fun rewrite_thm mode prover ctxt = Conv.fconv_rule (rewrite_cterm mode prover ctxt);
  1.1643  
  1.1644  (*Rewrite the subgoals of a proof state (represented by a theorem)*)
  1.1645  fun rewrite_goals_rule thms th =
  1.1646 @@ -1309,16 +1353,14 @@
  1.1647  fun rewrite_goals_tac defs = PRIMITIVE (rewrite_goals_rule defs);
  1.1648  
  1.1649  (*Rewrite one subgoal*)
  1.1650 -fun generic_rewrite_goal_tac mode prover_tac ss i thm =
  1.1651 +fun generic_rewrite_goal_tac mode prover_tac ctxt i thm =
  1.1652    if 0 < i andalso i <= Thm.nprems_of thm then
  1.1653 -    Seq.single (Conv.gconv_rule (rewrite_cterm mode (SINGLE o prover_tac) ss) i thm)
  1.1654 +    Seq.single (Conv.gconv_rule (rewrite_cterm mode (SINGLE o prover_tac) ctxt) i thm)
  1.1655    else Seq.empty;
  1.1656  
  1.1657 -fun rewrite_goal_tac rews =
  1.1658 -  let val ss = empty_ss addsimps rews in
  1.1659 -    fn i => fn st => generic_rewrite_goal_tac (true, false, false) (K no_tac)
  1.1660 -      (global_context (Thm.theory_of_thm st) ss) i st
  1.1661 -  end;
  1.1662 +fun rewrite_goal_tac rews i st =
  1.1663 +  generic_rewrite_goal_tac (true, false, false) (K no_tac)
  1.1664 +    (global_context (Thm.theory_of_thm st) empty_ss addsimps rews) i st;
  1.1665  
  1.1666  (*Prunes all redundant parameters from the proof state by rewriting.*)
  1.1667  val prune_params_tac = rewrite_goals_tac [Drule.triv_forall_equality];
  1.1668 @@ -1352,17 +1394,25 @@
  1.1669  
  1.1670  fun gen_norm_hhf ss th =
  1.1671    (if Drule.is_norm_hhf (Thm.prop_of th) then th
  1.1672 -   else Conv.fconv_rule
  1.1673 -    (rewrite_cterm (true, false, false) (K (K NONE)) (global_context (Thm.theory_of_thm th) ss)) th)
  1.1674 +   else
  1.1675 +    Conv.fconv_rule
  1.1676 +      (rewrite_cterm (true, false, false) (K (K NONE))
  1.1677 +        (global_context (Thm.theory_of_thm th) ss)) th)
  1.1678    |> Thm.adjust_maxidx_thm ~1
  1.1679    |> Drule.gen_all;
  1.1680  
  1.1681 -val hhf_ss = empty_ss addsimps Drule.norm_hhf_eqs;
  1.1682 +val hhf_ss =
  1.1683 +  simpset_of (empty_simpset (Context.proof_of (Context.the_thread_data ()))
  1.1684 +    addsimps Drule.norm_hhf_eqs);
  1.1685 +
  1.1686 +val hhf_protect_ss =
  1.1687 +  simpset_of (empty_simpset (Context.proof_of (Context.the_thread_data ()))
  1.1688 +    addsimps Drule.norm_hhf_eqs |> add_eqcong Drule.protect_cong);
  1.1689  
  1.1690  in
  1.1691  
  1.1692  val norm_hhf = gen_norm_hhf hhf_ss;
  1.1693 -val norm_hhf_protect = gen_norm_hhf (hhf_ss |> add_eqcong Drule.protect_cong);
  1.1694 +val norm_hhf_protect = gen_norm_hhf hhf_protect_ss;
  1.1695  
  1.1696  end;
  1.1697