src/Pure/simplifier.ML
changeset 17883 efa1bc2bdcc6
parent 17723 ee5b42e3cbb4
child 17898 ff78ecd1e768
     1.1 --- a/src/Pure/simplifier.ML	Mon Oct 17 23:10:20 2005 +0200
     1.2 +++ b/src/Pure/simplifier.ML	Mon Oct 17 23:10:21 2005 +0200
     1.3 @@ -9,21 +9,13 @@
     1.4  signature BASIC_SIMPLIFIER =
     1.5  sig
     1.6    include BASIC_META_SIMPLIFIER
     1.7 -  type context_solver
     1.8 -  val mk_context_solver: string -> (Proof.context -> thm list -> int -> tactic)
     1.9 -    -> context_solver
    1.10 -  type context_simproc
    1.11 -  val mk_context_simproc: string -> cterm list ->
    1.12 -    (Proof.context -> simpset -> term -> thm option) -> context_simproc
    1.13    val print_simpset: theory -> unit
    1.14 -  val simpset_ref_of_sg: theory -> simpset ref    (*obsolete*)
    1.15 -  val simpset_ref_of: theory -> simpset ref
    1.16 -  val simpset_of_sg: theory -> simpset            (*obsolete*)
    1.17 +  val change_simpset_of: theory -> (simpset -> simpset) -> unit
    1.18 +  val change_simpset: (simpset -> simpset) -> unit
    1.19    val simpset_of: theory -> simpset
    1.20 +  val simpset: unit -> simpset
    1.21    val SIMPSET: (simpset -> tactic) -> tactic
    1.22    val SIMPSET': (simpset -> 'a -> tactic) -> 'a -> tactic
    1.23 -  val simpset: unit -> simpset
    1.24 -  val simpset_ref: unit -> simpset ref
    1.25    val Addsimps: thm list -> unit
    1.26    val Delsimps: thm list -> unit
    1.27    val Addsimprocs: simproc list -> unit
    1.28 @@ -54,29 +46,19 @@
    1.29    include BASIC_SIMPLIFIER
    1.30    val clear_ss: simpset -> simpset
    1.31    val debug_bounds: bool ref
    1.32 -  val inherit_bounds: simpset -> simpset -> simpset
    1.33 +  val inherit_context: simpset -> simpset -> simpset
    1.34 +  val the_context: simpset -> Context.proof
    1.35 +  val set_context: Context.proof -> simpset -> simpset
    1.36    val simproc_i: theory -> string -> term list
    1.37      -> (theory -> simpset -> term -> thm option) -> simproc
    1.38    val simproc: theory -> string -> string list
    1.39      -> (theory -> simpset -> term -> thm option) -> simproc
    1.40 -  val context_simproc_i: theory -> string -> term list
    1.41 -    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
    1.42 -  val context_simproc: theory -> string -> string list
    1.43 -    -> (Proof.context -> simpset -> term -> thm option) -> context_simproc
    1.44    val          rewrite: simpset -> cterm -> thm
    1.45    val      asm_rewrite: simpset -> cterm -> thm
    1.46    val     full_rewrite: simpset -> cterm -> thm
    1.47    val   asm_lr_rewrite: simpset -> cterm -> thm
    1.48    val asm_full_rewrite: simpset -> cterm -> thm
    1.49 -  val add_context_simprocs: context_simproc list -> theory -> theory
    1.50 -  val del_context_simprocs: context_simproc list -> theory -> theory
    1.51 -  val set_context_subgoaler: (Proof.context -> simpset -> int -> tactic) -> theory -> theory
    1.52 -  val reset_context_subgoaler: theory -> theory
    1.53 -  val add_context_looper: string * (Proof.context -> int -> Tactical.tactic) ->
    1.54 -    theory -> theory
    1.55 -  val del_context_looper: string -> theory -> theory
    1.56 -  val add_context_unsafe_solver: context_solver -> theory -> theory
    1.57 -  val add_context_safe_solver: context_solver -> theory -> theory
    1.58 +  val get_simpset: theory -> simpset
    1.59    val print_local_simpset: Proof.context -> unit
    1.60    val get_local_simpset: Proof.context -> simpset
    1.61    val put_local_simpset: simpset -> Proof.context -> Proof.context
    1.62 @@ -90,7 +72,6 @@
    1.63    val cong_del_global: theory attribute
    1.64    val cong_add_local: Proof.context attribute
    1.65    val cong_del_local: Proof.context attribute
    1.66 -  val change_simpset_of: (simpset * 'a -> simpset) -> 'a -> theory -> theory
    1.67    val simp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
    1.68    val method_setup: (Args.T list -> (Method.modifier * Args.T list)) list
    1.69      -> (theory -> theory) list
    1.70 @@ -100,212 +81,66 @@
    1.71  structure Simplifier: SIMPLIFIER =
    1.72  struct
    1.73  
    1.74 -open MetaSimplifier;
    1.75 -
    1.76 -
    1.77 -(** context dependent simpset components **)
    1.78 -
    1.79 -(* datatype context_solver *)
    1.80 -
    1.81 -datatype context_solver =
    1.82 -  ContextSolver of (string * (Proof.context -> thm list -> int -> tactic)) * stamp;
    1.83 -
    1.84 -fun mk_context_solver name f = ContextSolver ((name, f), stamp ());
    1.85 -fun eq_context_solver (ContextSolver (_, id1), ContextSolver (_, id2)) = (id1 = id2);
    1.86 -val merge_context_solvers = gen_merge_lists eq_context_solver;
    1.87 -
    1.88 -
    1.89 -(* datatype context_simproc *)
    1.90 -
    1.91 -datatype context_simproc = ContextSimproc of
    1.92 -  (string * cterm list * (Proof.context -> simpset -> term -> thm option)) * stamp;
    1.93 -
    1.94 -fun mk_context_simproc name lhss f = ContextSimproc ((name, lhss, f), stamp ());
    1.95 -fun eq_context_simproc (ContextSimproc (_, id1), ContextSimproc (_, id2)) = (id1 = id2);
    1.96 -val merge_context_simprocs = gen_merge_lists eq_context_simproc;
    1.97 -
    1.98 -fun context_simproc_i thy name =
    1.99 -  mk_context_simproc name o map (Thm.cterm_of thy o Logic.varify);
   1.100 -
   1.101 -fun context_simproc thy name =
   1.102 -  context_simproc_i thy name o map (Sign.read_term thy);
   1.103 -
   1.104 -
   1.105 -(* datatype context_ss *)
   1.106 -
   1.107 -datatype context_ss = ContextSS of
   1.108 - {simprocs: context_simproc list,
   1.109 -  subgoal_tac: (Proof.context -> simpset -> int -> tactic) option,
   1.110 -  loop_tacs: (string * (Proof.context -> int -> tactic)) list,
   1.111 -  unsafe_solvers: context_solver list,
   1.112 -  solvers: context_solver list};
   1.113 +(** simpset data **)
   1.114  
   1.115 -fun context_ss ctxt ss ctxt_ss =
   1.116 -  let
   1.117 -    val ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} = ctxt_ss;
   1.118 -    fun prep_simproc (ContextSimproc ((name, lhss, f), _)) =
   1.119 -      mk_simproc name lhss (K (f ctxt));
   1.120 -    fun add_loop (name, f) simpset = simpset addloop (name, f ctxt);
   1.121 -    fun add_solver add (ContextSolver ((name, f), _)) simpset =
   1.122 -      add (simpset, mk_solver name (f ctxt));
   1.123 -  in
   1.124 -    ((case subgoal_tac of NONE => ss | SOME tac => ss setsubgoaler tac ctxt)
   1.125 -      addsimprocs map prep_simproc simprocs)
   1.126 -    |> fold_rev add_loop loop_tacs
   1.127 -    |> fold_rev (add_solver (op addSolver)) unsafe_solvers
   1.128 -    |> fold_rev (add_solver (op addSSolver)) solvers
   1.129 -  end;
   1.130 -
   1.131 -fun make_context_ss (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =
   1.132 -  ContextSS {simprocs = simprocs, subgoal_tac = subgoal_tac, loop_tacs = loop_tacs,
   1.133 -    unsafe_solvers = unsafe_solvers, solvers = solvers};
   1.134 -
   1.135 -val empty_context_ss = make_context_ss ([], NONE, [], [], []);
   1.136 -
   1.137 -fun merge_context_ss (ctxt_ss1, ctxt_ss2) =
   1.138 -  let
   1.139 -    val ContextSS {simprocs = simprocs1, subgoal_tac = subgoal_tac1, loop_tacs = loop_tacs1,
   1.140 -      unsafe_solvers = unsafe_solvers1, solvers = solvers1} = ctxt_ss1;
   1.141 -    val ContextSS {simprocs = simprocs2, subgoal_tac = subgoal_tac2, loop_tacs = loop_tacs2,
   1.142 -      unsafe_solvers = unsafe_solvers2, solvers = solvers2} = ctxt_ss2;
   1.143 -
   1.144 -    val simprocs' = merge_context_simprocs simprocs1 simprocs2;
   1.145 -    val subgoal_tac' = (case subgoal_tac1 of NONE => subgoal_tac2 | some => some);
   1.146 -    val loop_tacs' = merge_alists loop_tacs1 loop_tacs2;
   1.147 -    val unsafe_solvers' = merge_context_solvers unsafe_solvers1 unsafe_solvers2;
   1.148 -    val solvers' = merge_context_solvers solvers1 solvers2;
   1.149 -  in make_context_ss (simprocs', subgoal_tac', loop_tacs', unsafe_solvers', solvers') end;
   1.150 -
   1.151 -
   1.152 -
   1.153 -(** global and local simpset data **)
   1.154 -
   1.155 -(* theory data kind 'Pure/simpset' *)
   1.156 +(* global simpsets *)
   1.157  
   1.158  structure GlobalSimpset = TheoryDataFun
   1.159  (struct
   1.160    val name = "Pure/simpset";
   1.161 -  type T = simpset ref * context_ss;
   1.162 +  type T = simpset ref;
   1.163  
   1.164 -  val empty = (ref empty_ss, empty_context_ss);
   1.165 -  fun copy (ref ss, ctxt_ss) = (ref ss, ctxt_ss): T;            (*create new reference!*)
   1.166 +  val empty = ref empty_ss;
   1.167 +  fun copy (ref ss) = ref ss: T;            (*create new reference!*)
   1.168    val extend = copy;
   1.169 -  fun merge _ ((ref ss1, ctxt_ss1), (ref ss2, ctxt_ss2)) =
   1.170 -    (ref (merge_ss (ss1, ss2)), merge_context_ss (ctxt_ss1, ctxt_ss2));
   1.171 -  fun print _ (ref ss, _) = print_ss ss;
   1.172 +  fun merge _ (ref ss1, ref ss2) = ref (merge_ss (ss1, ss2));
   1.173 +  fun print _ (ref ss) = print_ss ss;
   1.174  end);
   1.175  
   1.176  val _ = Context.add_setup [GlobalSimpset.init];
   1.177  val print_simpset = GlobalSimpset.print;
   1.178 -
   1.179 -val simpset_ref_of = #1 o GlobalSimpset.get;
   1.180 -val simpset_ref_of_sg = simpset_ref_of;
   1.181 -val get_context_ss = #2 o GlobalSimpset.get o ProofContext.theory_of;
   1.182 -
   1.183 -fun map_context_ss f = GlobalSimpset.map (apsnd
   1.184 -  (fn ContextSS {simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers} =>
   1.185 -    make_context_ss (f (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers))));
   1.186 -
   1.187 -
   1.188 -(* access global simpset *)
   1.189 -
   1.190 -val simpset_of = ! o simpset_ref_of;
   1.191 -val simpset_of_sg = simpset_of;
   1.192 -
   1.193 -fun SIMPSET tacf state = tacf (simpset_of (Thm.theory_of_thm state)) state;
   1.194 -fun SIMPSET' tacf i state = tacf (simpset_of (Thm.theory_of_thm state)) i state;
   1.195 +val get_simpset = ! o GlobalSimpset.get;
   1.196  
   1.197 -val simpset = simpset_of o Context.the_context;
   1.198 -val simpset_ref = simpset_ref_of o Context.the_context;
   1.199 -
   1.200 -
   1.201 -(* change global simpset *)
   1.202 +val change_simpset_of = change o GlobalSimpset.get;
   1.203 +fun change_simpset f = change_simpset_of (Context.the_context ()) f;
   1.204  
   1.205 -fun change_simpset_of f x thy =
   1.206 -  let val r = simpset_ref_of thy
   1.207 -  in r := f (! r, x); thy end;
   1.208 -
   1.209 -fun change_simpset f x = (change_simpset_of f x (Context.the_context ()); ());
   1.210 -
   1.211 -val Addsimps = change_simpset (op addsimps);
   1.212 -val Delsimps = change_simpset (op delsimps);
   1.213 -val Addsimprocs = change_simpset (op addsimprocs);
   1.214 -val Delsimprocs = change_simpset (op delsimprocs);
   1.215 -val Addcongs = change_simpset (op addcongs);
   1.216 -val Delcongs = change_simpset (op delcongs);
   1.217 +fun simpset_of thy = MetaSimplifier.set_context (Context.init_proof thy) (get_simpset thy);
   1.218 +val simpset = simpset_of o Context.the_context;
   1.219  
   1.220  
   1.221 -(* change context dependent components *)
   1.222 -
   1.223 -fun add_context_simprocs procs =
   1.224 -  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
   1.225 -    (merge_context_simprocs procs simprocs, subgoal_tac, loop_tacs,
   1.226 -      unsafe_solvers, solvers));
   1.227 -
   1.228 -fun del_context_simprocs procs =
   1.229 -  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
   1.230 -    (gen_rems eq_context_simproc (simprocs, procs), subgoal_tac, loop_tacs,
   1.231 -      unsafe_solvers, solvers));
   1.232 -
   1.233 -fun set_context_subgoaler tac =
   1.234 -  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
   1.235 -    (simprocs, SOME tac, loop_tacs, unsafe_solvers, solvers));
   1.236 -
   1.237 -val reset_context_subgoaler =
   1.238 -  map_context_ss (fn (simprocs, _, loop_tacs, unsafe_solvers, solvers) =>
   1.239 -    (simprocs, NONE, loop_tacs, unsafe_solvers, solvers));
   1.240 +fun SIMPSET tacf st = tacf (simpset_of (Thm.theory_of_thm st)) st;
   1.241 +fun SIMPSET' tacf i st = tacf (simpset_of (Thm.theory_of_thm st)) i st;
   1.242  
   1.243 -fun add_context_looper (name, tac) =
   1.244 -  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
   1.245 -    (simprocs, subgoal_tac, merge_alists [(name, tac)] loop_tacs,
   1.246 -      unsafe_solvers, solvers));
   1.247 -
   1.248 -fun del_context_looper name =
   1.249 -  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
   1.250 -    (simprocs, subgoal_tac, filter_out (equal name o #1) loop_tacs,
   1.251 -      unsafe_solvers, solvers));
   1.252 -
   1.253 -fun add_context_unsafe_solver solver =
   1.254 -  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
   1.255 -    (simprocs, subgoal_tac, loop_tacs,
   1.256 -      merge_context_solvers [solver] unsafe_solvers, solvers));
   1.257 -
   1.258 -fun add_context_safe_solver solver =
   1.259 -  map_context_ss (fn (simprocs, subgoal_tac, loop_tacs, unsafe_solvers, solvers) =>
   1.260 -    (simprocs, subgoal_tac, loop_tacs, unsafe_solvers,
   1.261 -      merge_context_solvers [solver] solvers));
   1.262 +fun Addsimps args = change_simpset (fn ss => ss addsimps args);
   1.263 +fun Delsimps args = change_simpset (fn ss => ss delsimps args);
   1.264 +fun Addsimprocs args = change_simpset (fn ss => ss addsimprocs args);
   1.265 +fun Delsimprocs args = change_simpset (fn ss => ss delsimprocs args);
   1.266 +fun Addcongs args = change_simpset (fn ss => ss addcongs args);
   1.267 +fun Delcongs args = change_simpset (fn ss => ss delcongs args);
   1.268  
   1.269  
   1.270 -(* proof data kind 'Pure/simpset' *)
   1.271 +(* local simpsets *)
   1.272  
   1.273  structure LocalSimpset = ProofDataFun
   1.274  (struct
   1.275    val name = "Pure/simpset";
   1.276    type T = simpset;
   1.277 -  val init = simpset_of;
   1.278 -  fun print ctxt ss = print_ss (context_ss ctxt ss (get_context_ss ctxt));
   1.279 +  val init = get_simpset;
   1.280 +  fun print _ ss = print_ss ss;
   1.281  end);
   1.282  
   1.283  val _ = Context.add_setup [LocalSimpset.init];
   1.284  val print_local_simpset = LocalSimpset.print;
   1.285 -
   1.286  val get_local_simpset = LocalSimpset.get;
   1.287  val put_local_simpset = LocalSimpset.put;
   1.288 -fun map_local_simpset f ctxt = put_local_simpset (f (get_local_simpset ctxt)) ctxt;
   1.289  
   1.290 -fun local_simpset_of ctxt =
   1.291 -  context_ss ctxt (get_local_simpset ctxt) (get_context_ss ctxt);
   1.292 +fun local_simpset_of ctxt = MetaSimplifier.set_context ctxt (get_local_simpset ctxt);
   1.293  
   1.294  
   1.295  (* attributes *)
   1.296  
   1.297 -fun change_global_ss f (thy, th) =
   1.298 -  let val r = simpset_ref_of thy
   1.299 -  in r := f (! r, [th]); (thy, th) end;
   1.300 -
   1.301 -fun change_local_ss f (ctxt, th) =
   1.302 -  let val ss = f (get_local_simpset ctxt, [th])
   1.303 -  in (put_local_simpset ss ctxt, th) end;
   1.304 +fun change_global_ss f (thy, th) = (change_simpset_of thy (fn ss => f (ss, [th])); (thy, th));
   1.305 +fun change_local_ss f (ctxt, th) = (LocalSimpset.map (fn ss => f (ss, [th])) ctxt, th);
   1.306  
   1.307  val simp_add_global = change_global_ss (op addsimps);
   1.308  val simp_del_global = change_global_ss (op delsimps);
   1.309 @@ -337,17 +172,17 @@
   1.310  
   1.311  (* conversions *)
   1.312  
   1.313 -val          simplify = simp_thm (false, false, false);
   1.314 -val      asm_simplify = simp_thm (false, true, false);
   1.315 -val     full_simplify = simp_thm (true, false, false);
   1.316 -val   asm_lr_simplify = simp_thm (true, true, false);
   1.317 -val asm_full_simplify = simp_thm (true, true, true);
   1.318 +val          simplify = MetaSimplifier.simp_thm (false, false, false);
   1.319 +val      asm_simplify = MetaSimplifier.simp_thm (false, true, false);
   1.320 +val     full_simplify = MetaSimplifier.simp_thm (true, false, false);
   1.321 +val   asm_lr_simplify = MetaSimplifier.simp_thm (true, true, false);
   1.322 +val asm_full_simplify = MetaSimplifier.simp_thm (true, true, true);
   1.323  
   1.324 -val          rewrite = simp_cterm (false, false, false);
   1.325 -val      asm_rewrite = simp_cterm (false, true, false);
   1.326 -val     full_rewrite = simp_cterm (true, false, false);
   1.327 -val   asm_lr_rewrite = simp_cterm (true, true, false);
   1.328 -val asm_full_rewrite = simp_cterm (true, true, true);
   1.329 +val          rewrite = MetaSimplifier.simp_cterm (false, false, false);
   1.330 +val      asm_rewrite = MetaSimplifier.simp_cterm (false, true, false);
   1.331 +val     full_rewrite = MetaSimplifier.simp_cterm (true, false, false);
   1.332 +val   asm_lr_rewrite = MetaSimplifier.simp_cterm (true, true, false);
   1.333 +val asm_full_rewrite = MetaSimplifier.simp_cterm (true, true, true);
   1.334  
   1.335  
   1.336  
   1.337 @@ -386,7 +221,8 @@
   1.338  
   1.339  fun simplified_att get args =
   1.340    Attrib.syntax (conv_mode -- args >> (fn (f, ths) =>
   1.341 -    Drule.rule_attribute (fn x => f ((if null ths then I else clear_ss) (get x) addsimps ths))));
   1.342 +    Drule.rule_attribute (fn x =>
   1.343 +      f ((if null ths then I else MetaSimplifier.clear_ss) (get x) addsimps ths))));
   1.344  
   1.345  in
   1.346  
   1.347 @@ -424,7 +260,7 @@
   1.348    >> (curry (Library.foldl op o) I o rev)) x;
   1.349  
   1.350  val cong_modifiers =
   1.351 - [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local):Method.modifier),
   1.352 + [Args.$$$ congN -- Args.colon >> K ((I, cong_add_local): Method.modifier),
   1.353    Args.$$$ congN -- Args.add -- Args.colon >> K (I, cong_add_local),
   1.354    Args.$$$ congN -- Args.del -- Args.colon >> K (I, cong_del_local)];
   1.355  
   1.356 @@ -432,13 +268,14 @@
   1.357   [Args.$$$ simpN -- Args.colon >> K (I, simp_add_local),
   1.358    Args.$$$ simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
   1.359    Args.$$$ simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
   1.360 -  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
   1.361 +  Args.$$$ simpN -- Args.$$$ onlyN -- Args.colon
   1.362 +    >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add_local)]
   1.363     @ cong_modifiers;
   1.364  
   1.365  val simp_modifiers' =
   1.366   [Args.add -- Args.colon >> K (I, simp_add_local),
   1.367    Args.del -- Args.colon >> K (I, simp_del_local),
   1.368 -  Args.$$$ onlyN -- Args.colon >> K (map_local_simpset clear_ss, simp_add_local)]
   1.369 +  Args.$$$ onlyN -- Args.colon >> K (LocalSimpset.map MetaSimplifier.clear_ss, simp_add_local)]
   1.370     @ cong_modifiers;
   1.371  
   1.372  fun simp_args more_mods =
   1.373 @@ -483,13 +320,16 @@
   1.374      fun mksimps thm = mk_eq (Drule.forall_elim_vars (#maxidx (Thm.rep_thm thm) + 1) thm);
   1.375  
   1.376      fun init_ss thy =
   1.377 -      (simpset_ref_of thy :=
   1.378 +      (GlobalSimpset.get thy :=
   1.379          empty_ss setsubgoaler asm_simp_tac
   1.380          setSSolver safe_solver
   1.381          setSolver unsafe_solver
   1.382          setmksimps mksimps; thy);
   1.383    in method_setup [] @ [init_ss] end;
   1.384  
   1.385 +
   1.386 +open MetaSimplifier;
   1.387 +
   1.388  end;
   1.389  
   1.390  structure BasicSimplifier: BASIC_SIMPLIFIER = Simplifier;