src/Pure/raw_simplifier.ML
changeset 54731 384ac33802b0
parent 54729 c5cd7a58cf2d
child 54742 7a86358a3c0b
     1.1 --- a/src/Pure/raw_simplifier.ML	Thu Dec 12 21:28:13 2013 +0100
     1.2 +++ b/src/Pure/raw_simplifier.ML	Thu Dec 12 22:38:25 2013 +0100
     1.3 @@ -73,7 +73,7 @@
     1.4    include BASIC_RAW_SIMPLIFIER
     1.5    exception SIMPLIFIER of string * thm
     1.6    type trace_ops
     1.7 -  val set_trace_ops: trace_ops -> Proof.context -> Proof.context
     1.8 +  val set_trace_ops: trace_ops -> theory -> theory
     1.9    val internal_ss: simpset ->
    1.10     {congs: (cong_name * thm) list * cong_name list,
    1.11      procs: proc Net.net,
    1.12 @@ -86,8 +86,7 @@
    1.13      termless: term * term -> bool,
    1.14      subgoal_tac: Proof.context -> int -> tactic,
    1.15      loop_tacs: (string * (Proof.context -> int -> tactic)) list,
    1.16 -    solvers: solver list * solver list,
    1.17 -    trace_ops: trace_ops}
    1.18 +    solvers: solver list * solver list}
    1.19    val map_ss: (Proof.context -> Proof.context) -> Context.generic -> Context.generic
    1.20    val prems_of: Proof.context -> thm list
    1.21    val add_simp: thm -> Proof.context -> Proof.context
    1.22 @@ -247,18 +246,6 @@
    1.23  fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
    1.24  
    1.25  
    1.26 -(* trace operations *)
    1.27 -
    1.28 -type trace_ops =
    1.29 - {trace_invoke: {depth: int, term: term} -> Proof.context -> Proof.context,
    1.30 -  trace_apply: {unconditional: bool, term: term, thm: thm, name: string} ->
    1.31 -    Proof.context -> (Proof.context -> (thm * term) option) -> (thm * term) option};
    1.32 -
    1.33 -val no_trace_ops : trace_ops =
    1.34 - {trace_invoke = fn _ => fn ctxt => ctxt,
    1.35 -  trace_apply = fn _ => fn ctxt => fn cont => cont ctxt};
    1.36 -
    1.37 -
    1.38  (* simplification sets *)
    1.39  
    1.40  (*A simpset contains data required during conversion:
    1.41 @@ -296,8 +283,7 @@
    1.42      termless: term * term -> bool,
    1.43      subgoal_tac: Proof.context -> int -> tactic,
    1.44      loop_tacs: (string * (Proof.context -> int -> tactic)) list,
    1.45 -    solvers: solver list * solver list,
    1.46 -    trace_ops: trace_ops};
    1.47 +    solvers: solver list * solver list};
    1.48  
    1.49  fun internal_ss (Simpset (_, ss2)) = ss2;
    1.50  
    1.51 @@ -307,12 +293,12 @@
    1.52  fun map_ss1 f {rules, prems, bounds, depth} =
    1.53    make_ss1 (f (rules, prems, bounds, depth));
    1.54  
    1.55 -fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =
    1.56 +fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =
    1.57    {congs = congs, procs = procs, mk_rews = mk_rews, termless = termless,
    1.58 -    subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers, trace_ops = trace_ops};
    1.59 +    subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers};
    1.60  
    1.61 -fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops} =
    1.62 -  make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
    1.63 +fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers} =
    1.64 +  make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers));
    1.65  
    1.66  fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2);
    1.67  
    1.68 @@ -332,9 +318,9 @@
    1.69  
    1.70  (* empty *)
    1.71  
    1.72 -fun init_ss mk_rews termless subgoal_tac solvers trace_ops =
    1.73 +fun init_ss mk_rews termless subgoal_tac solvers =
    1.74    make_simpset ((Net.empty, [], (0, []), (0, Unsynchronized.ref false)),
    1.75 -    (([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers, trace_ops));
    1.76 +    (([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers));
    1.77  
    1.78  fun default_mk_sym _ th = SOME (th RS Drule.symmetric_thm);
    1.79  
    1.80 @@ -345,9 +331,7 @@
    1.81        mk_sym = default_mk_sym,
    1.82        mk_eq_True = K (K NONE),
    1.83        reorient = default_reorient}
    1.84 -    Term_Ord.termless (K (K no_tac))
    1.85 -    ([], [])
    1.86 -    no_trace_ops;
    1.87 +    Term_Ord.termless (K (K no_tac)) ([], []);
    1.88  
    1.89  
    1.90  (* merge *)  (*NOTE: ignores some fields of 2nd simpset*)
    1.91 @@ -358,10 +342,10 @@
    1.92      let
    1.93        val Simpset ({rules = rules1, prems = prems1, bounds = bounds1, depth = depth1},
    1.94         {congs = (congs1, weak1), procs = procs1, mk_rews, termless, subgoal_tac,
    1.95 -        loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1), trace_ops}) = ss1;
    1.96 +        loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1;
    1.97        val Simpset ({rules = rules2, prems = prems2, bounds = bounds2, depth = depth2},
    1.98         {congs = (congs2, weak2), procs = procs2, mk_rews = _, termless = _, subgoal_tac = _,
    1.99 -        loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2), trace_ops = _}) = ss2;
   1.100 +        loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2;
   1.101  
   1.102        val rules' = Net.merge eq_rrule (rules1, rules2);
   1.103        val prems' = Thm.merge_thms (prems1, prems2);
   1.104 @@ -375,7 +359,7 @@
   1.105        val solvers' = merge eq_solver (solvers1, solvers2);
   1.106      in
   1.107        make_simpset ((rules', prems', bounds', depth'), ((congs', weak'), procs',
   1.108 -        mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers'), trace_ops))
   1.109 +        mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers')))
   1.110      end;
   1.111  
   1.112  
   1.113 @@ -400,13 +384,9 @@
   1.114  
   1.115  fun put_simpset ss = map_simpset (fn context_ss =>
   1.116    let
   1.117 -    val Simpset ({rules, prems, ...},  (* FIXME prems from context (!?) *)
   1.118 -      {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, ...}) = ss;
   1.119 -    val Simpset ({bounds, depth, ...}, {trace_ops, ...}) = context_ss;
   1.120 -  in
   1.121 -    Simpset (make_ss1 (rules, prems, bounds, depth),
   1.122 -      make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops))
   1.123 -  end);
   1.124 +    val Simpset ({rules, prems, ...}, ss2) = ss;  (* FIXME prems from context (!?) *)
   1.125 +    val Simpset ({bounds, depth, ...}, _) = context_ss;
   1.126 +  in Simpset (make_ss1 (rules, prems, bounds, depth), ss2) end);
   1.127  
   1.128  fun global_context thy ss = Proof_Context.init_global thy |> put_simpset ss;
   1.129  
   1.130 @@ -421,8 +401,8 @@
   1.131  fun map_ss f = Context.mapping (map_theory_simpset f) f;
   1.132  
   1.133  val clear_simpset =
   1.134 -  map_simpset (fn Simpset (_, {mk_rews, termless, subgoal_tac, solvers, trace_ops, ...}) =>
   1.135 -    init_ss mk_rews termless subgoal_tac solvers trace_ops);
   1.136 +  map_simpset (fn Simpset (_, {mk_rews, termless, subgoal_tac, solvers, ...}) =>
   1.137 +    init_ss mk_rews termless subgoal_tac solvers);
   1.138  
   1.139  
   1.140  (* simp depth *)
   1.141 @@ -686,7 +666,7 @@
   1.142  in
   1.143  
   1.144  fun add_eqcong thm ctxt = ctxt |> map_simpset2
   1.145 -  (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.146 +  (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.147      let
   1.148        val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
   1.149          handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", thm);
   1.150 @@ -701,10 +681,10 @@
   1.151          else ();
   1.152        val xs' = AList.update (op =) (a, thm) xs;
   1.153        val weak' = if is_full_cong thm then weak else a :: weak;
   1.154 -    in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) end);
   1.155 +    in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end);
   1.156  
   1.157  fun del_eqcong thm ctxt = ctxt |> map_simpset2
   1.158 -  (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.159 +  (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.160      let
   1.161        val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
   1.162          handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", thm);
   1.163 @@ -715,7 +695,7 @@
   1.164        val xs' = filter_out (fn (x : cong_name, _) => x = a) xs;
   1.165        val weak' = xs' |> map_filter (fn (a, thm) =>
   1.166          if is_full_cong thm then NONE else SOME a);
   1.167 -    in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) end);
   1.168 +    in ((xs', weak'), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end);
   1.169  
   1.170  fun add_cong thm ctxt = add_eqcong (mk_cong ctxt thm) ctxt;
   1.171  fun del_cong thm ctxt = del_eqcong (mk_cong ctxt thm) ctxt;
   1.172 @@ -758,18 +738,18 @@
   1.173  fun add_proc (proc as Proc {name, lhs, ...}) ctxt =
   1.174   (trace_cterm ctxt false (fn () => "Adding simplification procedure " ^ quote name ^ " for") lhs;
   1.175    ctxt |> map_simpset2
   1.176 -    (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.177 +    (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.178        (congs, Net.insert_term eq_proc (term_of lhs, proc) procs,
   1.179 -        mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops))
   1.180 +        mk_rews, termless, subgoal_tac, loop_tacs, solvers))
   1.181    handle Net.INSERT =>
   1.182      (Context_Position.if_visible ctxt
   1.183        warning ("Ignoring duplicate simplification procedure " ^ quote name); ctxt));
   1.184  
   1.185  fun del_proc (proc as Proc {name, lhs, ...}) ctxt =
   1.186    ctxt |> map_simpset2
   1.187 -    (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.188 +    (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.189        (congs, Net.delete_term eq_proc (term_of lhs, proc) procs,
   1.190 -        mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops))
   1.191 +        mk_rews, termless, subgoal_tac, loop_tacs, solvers))
   1.192    handle Net.DELETE =>
   1.193      (Context_Position.if_visible ctxt
   1.194        warning ("Simplification procedure " ^ quote name ^ " not in simpset"); ctxt);
   1.195 @@ -790,14 +770,14 @@
   1.196  local
   1.197  
   1.198  fun map_mk_rews f =
   1.199 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.200 +  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.201      let
   1.202        val {mk, mk_cong, mk_sym, mk_eq_True, reorient} = mk_rews;
   1.203        val (mk', mk_cong', mk_sym', mk_eq_True', reorient') =
   1.204          f (mk, mk_cong, mk_sym, mk_eq_True, reorient);
   1.205        val mk_rews' = {mk = mk', mk_cong = mk_cong', mk_sym = mk_sym', mk_eq_True = mk_eq_True',
   1.206          reorient = reorient'};
   1.207 -    in (congs, procs, mk_rews', termless, subgoal_tac, loop_tacs, solvers, trace_ops) end);
   1.208 +    in (congs, procs, mk_rews', termless, subgoal_tac, loop_tacs, solvers) end);
   1.209  
   1.210  in
   1.211  
   1.212 @@ -826,62 +806,75 @@
   1.213  (* termless *)
   1.214  
   1.215  fun set_termless termless =
   1.216 -  map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.217 -   (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   1.218 +  map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers) =>
   1.219 +   (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers));
   1.220  
   1.221  
   1.222  (* tactics *)
   1.223  
   1.224  fun set_subgoaler subgoal_tac =
   1.225 -  map_simpset2 (fn (congs, procs, mk_rews, termless, _, loop_tacs, solvers, trace_ops) =>
   1.226 -   (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   1.227 +  map_simpset2 (fn (congs, procs, mk_rews, termless, _, loop_tacs, solvers) =>
   1.228 +   (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers));
   1.229  
   1.230  fun ctxt setloop tac = ctxt |>
   1.231 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, _, solvers, trace_ops) =>
   1.232 -   (congs, procs, mk_rews, termless, subgoal_tac, [("", tac)], solvers, trace_ops));
   1.233 +  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, _, solvers) =>
   1.234 +   (congs, procs, mk_rews, termless, subgoal_tac, [("", tac)], solvers));
   1.235  
   1.236  fun ctxt addloop (name, tac) = ctxt |>
   1.237 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.238 +  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.239      (congs, procs, mk_rews, termless, subgoal_tac,
   1.240 -     AList.update (op =) (name, tac) loop_tacs, solvers, trace_ops));
   1.241 +     AList.update (op =) (name, tac) loop_tacs, solvers));
   1.242  
   1.243  fun ctxt delloop name = ctxt |>
   1.244 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops) =>
   1.245 +  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) =>
   1.246      (congs, procs, mk_rews, termless, subgoal_tac,
   1.247       (if AList.defined (op =) loop_tacs name then ()
   1.248        else
   1.249          Context_Position.if_visible ctxt
   1.250            warning ("No such looper in simpset: " ^ quote name);
   1.251 -        AList.delete (op =) name loop_tacs), solvers, trace_ops));
   1.252 +        AList.delete (op =) name loop_tacs), solvers));
   1.253  
   1.254  fun ctxt setSSolver solver = ctxt |> map_simpset2
   1.255 -  (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, _), trace_ops) =>
   1.256 -    (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, [solver]), trace_ops));
   1.257 +  (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, _)) =>
   1.258 +    (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, [solver])));
   1.259  
   1.260  fun ctxt addSSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
   1.261 -  subgoal_tac, loop_tacs, (unsafe_solvers, solvers), trace_ops) => (congs, procs, mk_rews, termless,
   1.262 -    subgoal_tac, loop_tacs, (unsafe_solvers, insert eq_solver solver solvers), trace_ops));
   1.263 +  subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless,
   1.264 +    subgoal_tac, loop_tacs, (unsafe_solvers, insert eq_solver solver solvers)));
   1.265  
   1.266  fun ctxt setSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
   1.267 -  subgoal_tac, loop_tacs, (_, solvers), trace_ops) => (congs, procs, mk_rews, termless,
   1.268 -    subgoal_tac, loop_tacs, ([solver], solvers), trace_ops));
   1.269 +  subgoal_tac, loop_tacs, (_, solvers)) => (congs, procs, mk_rews, termless,
   1.270 +    subgoal_tac, loop_tacs, ([solver], solvers)));
   1.271  
   1.272  fun ctxt addSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, termless,
   1.273 -  subgoal_tac, loop_tacs, (unsafe_solvers, solvers), trace_ops) => (congs, procs, mk_rews, termless,
   1.274 -    subgoal_tac, loop_tacs, (insert eq_solver solver unsafe_solvers, solvers), trace_ops));
   1.275 +  subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless,
   1.276 +    subgoal_tac, loop_tacs, (insert eq_solver solver unsafe_solvers, solvers)));
   1.277  
   1.278  fun set_solvers solvers = map_simpset2 (fn (congs, procs, mk_rews, termless,
   1.279 -  subgoal_tac, loop_tacs, _, trace_ops) => (congs, procs, mk_rews, termless,
   1.280 -  subgoal_tac, loop_tacs, (solvers, solvers), trace_ops));
   1.281 +  subgoal_tac, loop_tacs, _) => (congs, procs, mk_rews, termless,
   1.282 +  subgoal_tac, loop_tacs, (solvers, solvers)));
   1.283  
   1.284  
   1.285  (* trace operations *)
   1.286  
   1.287 -fun set_trace_ops trace_ops =
   1.288 -  map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, _) =>
   1.289 -    (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers, trace_ops));
   1.290 +type trace_ops =
   1.291 + {trace_invoke: {depth: int, term: term} -> Proof.context -> Proof.context,
   1.292 +  trace_apply: {unconditional: bool, term: term, thm: thm, name: string} ->
   1.293 +    Proof.context -> (Proof.context -> (thm * term) option) -> (thm * term) option};
   1.294  
   1.295 -val trace_ops = #trace_ops o internal_ss o simpset_of;
   1.296 +structure Trace_Ops = Theory_Data
   1.297 +(
   1.298 +  type T = trace_ops;
   1.299 +  val empty: T =
   1.300 +   {trace_invoke = fn _ => fn ctxt => ctxt,
   1.301 +    trace_apply = fn _ => fn ctxt => fn cont => cont ctxt};
   1.302 +  val extend = I;
   1.303 +  fun merge (trace_ops, _) = trace_ops;
   1.304 +);
   1.305 +
   1.306 +val set_trace_ops = Trace_Ops.put;
   1.307 +
   1.308 +val trace_ops = Trace_Ops.get o Proof_Context.theory_of;
   1.309  fun trace_invoke args ctxt = #trace_invoke (trace_ops ctxt) args ctxt;
   1.310  fun trace_apply args ctxt = #trace_apply (trace_ops ctxt) args ctxt;
   1.311