thm/prf: separate official name vs. additional tags;
1 (* Title: Provers/clasimp.ML
3 Author: David von Oheimb, TU Muenchen
5 Combination of classical reasoner and simplifier (depends on
6 splitter.ML, classical.ML, blast.ML).
9 infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2 addsimps2 delsimps2
10 addSss addss addss' addIffs delIffs;
12 signature CLASIMP_DATA =
14 structure Splitter: SPLITTER
15 structure Classical: CLASSICAL
16 structure Blast: BLAST
17 sharing type Classical.claset = Blast.claset
28 val change_clasimpset: (clasimpset -> clasimpset) -> unit
29 val clasimpset: unit -> clasimpset
30 val addSIs2: clasimpset * thm list -> clasimpset
31 val addSEs2: clasimpset * thm list -> clasimpset
32 val addSDs2: clasimpset * thm list -> clasimpset
33 val addIs2: clasimpset * thm list -> clasimpset
34 val addEs2: clasimpset * thm list -> clasimpset
35 val addDs2: clasimpset * thm list -> clasimpset
36 val addsimps2: clasimpset * thm list -> clasimpset
37 val delsimps2: clasimpset * thm list -> clasimpset
38 val addSss: claset * simpset -> claset
39 val addss: claset * simpset -> claset
40 val addss': claset * simpset -> claset
41 val addIffs: clasimpset * thm list -> clasimpset
42 val delIffs: clasimpset * thm list -> clasimpset
43 val AddIffs: thm list -> unit
44 val DelIffs: thm list -> unit
45 val CLASIMPSET: (clasimpset -> tactic) -> tactic
46 val CLASIMPSET': (clasimpset -> 'a -> tactic) -> 'a -> tactic
47 val clarsimp_tac: clasimpset -> int -> tactic
48 val Clarsimp_tac: int -> tactic
49 val mk_auto_tac: clasimpset -> int -> int -> tactic
50 val auto_tac: clasimpset -> tactic
52 val auto: unit -> unit
53 val force_tac: clasimpset -> int -> tactic
54 val Force_tac: int -> tactic
55 val force: int -> unit
56 val fast_simp_tac: clasimpset -> int -> tactic
57 val slow_simp_tac: clasimpset -> int -> tactic
58 val best_simp_tac: clasimpset -> int -> tactic
59 val attrib: (clasimpset * thm list -> clasimpset) -> attribute
60 val get_local_clasimpset: Proof.context -> clasimpset
61 val local_clasimpset_of: Proof.context -> clasimpset
62 val iff_add: attribute
63 val iff_add': attribute
64 val iff_del: attribute
65 val iff_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
66 val clasimp_modifiers: (Args.T list -> (Method.modifier * Args.T list)) list
67 val setup: theory -> theory
70 functor ClasimpFun(Data: CLASIMP_DATA): CLASIMP =
75 type claset = Classical.claset;
76 type clasimpset = claset * simpset;
78 fun change_clasimpset_of thy f =
79 let val (cs', ss') = f (Classical.get_claset thy, Simplifier.get_simpset thy) in
80 Classical.change_claset_of thy (fn _ => cs');
81 Simplifier.change_simpset_of thy (fn _ => ss')
84 fun change_clasimpset f = change_clasimpset_of (Context.the_context ()) f;
85 fun clasimpset () = (Classical.claset (), Simplifier.simpset ());
88 (* clasimpset operations *)
90 (*this interface for updating a clasimpset is rudimentary and just for
91 convenience for the most common cases*)
93 fun pair_upd1 f ((a,b),x) = (f(a,x), b);
94 fun pair_upd2 f ((a,b),x) = (a, f(b,x));
96 fun op addSIs2 arg = pair_upd1 Classical.addSIs arg;
97 fun op addSEs2 arg = pair_upd1 Classical.addSEs arg;
98 fun op addSDs2 arg = pair_upd1 Classical.addSDs arg;
99 fun op addIs2 arg = pair_upd1 Classical.addIs arg;
100 fun op addEs2 arg = pair_upd1 Classical.addEs arg;
101 fun op addDs2 arg = pair_upd1 Classical.addDs arg;
102 fun op addsimps2 arg = pair_upd2 Simplifier.addsimps arg;
103 fun op delsimps2 arg = pair_upd2 Simplifier.delsimps arg;
105 (*not totally safe: may instantiate unknowns that appear also in other subgoals*)
106 val safe_asm_full_simp_tac = Simplifier.generic_simp_tac true (true,true,true);
108 (*Add a simpset to a classical set!*)
109 (*Caution: only one simpset added can be added by each of addSss and addss*)
110 fun cs addSss ss = Classical.addSafter (cs, ("safe_asm_full_simp_tac",
111 CHANGED o safe_asm_full_simp_tac ss));
112 fun cs addss ss = Classical.addbefore (cs, ("asm_full_simp_tac",
113 CHANGED o Simplifier.asm_full_simp_tac ss));
114 fun cs addss' ss = Classical.addbefore (cs, ("asm_full_simp_tac",
115 CHANGED o Simplifier.asm_lr_simp_tac ss));
117 (*Attach a suffix, provided we have a name to start with.*)
118 fun suffix_thm "" _ = I
119 | suffix_thm a b = PureThy.put_name_hint (a^b);
121 (* iffs: addition of rules to simpsets and clasets simultaneously *)
123 (*Takes (possibly conditional) theorems of the form A<->B to
124 the Safe Intr rule B==>A and
125 the Safe Destruct rule A==>B.
126 Also ~A goes to the Safe Elim rule A ==> ?R
127 Failing other cases, A is added as a Safe Intr rule*)
130 fun addIff decls1 decls2 simp ((cs, ss), th) =
132 val name = PureThy.get_name_hint th;
133 val n = nprems_of th;
134 val (elim, intro) = if n = 0 then decls1 else decls2;
135 val zero_rotate = zero_var_indexes o rotate_prems n;
137 (elim (intro (cs, [suffix_thm name "_iff2" (zero_rotate (th RS iffD2))]),
138 [suffix_thm name "_iff1" (Tactic.make_elim (zero_rotate (th RS iffD1)))])
139 handle THM _ => (elim (cs, [suffix_thm name "_iff1" (zero_rotate (th RS notE))])
140 handle THM _ => intro (cs, [th])),
144 fun delIff delcs delss ((cs, ss), th) =
145 let val zero_rotate = zero_var_indexes o rotate_prems (nprems_of th) in
146 (delcs (cs, [zero_rotate (th RS iffD2),
147 Tactic.make_elim (zero_rotate (th RS iffD1))])
148 handle THM _ => (delcs (cs, [zero_rotate (th RS notE)])
149 handle THM _ => delcs (cs, [th])),
153 fun modifier att (x, ths) =
154 fst (foldl_map (Library.apply [att]) (x, rev ths));
156 val addXIs = modifier (ContextRules.intro_query NONE);
157 val addXEs = modifier (ContextRules.elim_query NONE);
158 val addXDs = modifier (ContextRules.dest_query NONE);
159 val delXs = modifier ContextRules.rule_del;
165 (addIff (Classical.addSEs, Classical.addSIs)
166 (Classical.addEs, Classical.addIs)
167 Simplifier.addsimps);
168 val op delIffs = Library.foldl (delIff Classical.delrules Simplifier.delsimps);
170 fun AddIffs thms = change_clasimpset (fn css => css addIffs thms);
171 fun DelIffs thms = change_clasimpset (fn css => css delIffs thms);
173 fun addIffs_generic (x, ths) =
174 Library.foldl (addIff (addXEs, addXIs) (addXEs, addXIs) #1) ((x, ()), ths) |> #1;
176 fun delIffs_generic (x, ths) =
177 Library.foldl (delIff delXs #1) ((x, ()), ths) |> #1;
184 fun CLASIMPSET tacf state =
185 Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss))) state;
187 fun CLASIMPSET' tacf i state =
188 Classical.CLASET (fn cs => Simplifier.SIMPSET (fn ss => tacf (cs, ss) i)) state;
191 fun clarsimp_tac (cs, ss) =
192 safe_asm_full_simp_tac ss THEN_ALL_NEW
193 Classical.clarify_tac (cs addSss ss);
195 fun Clarsimp_tac i = clarsimp_tac (clasimpset ()) i;
200 fun blast_depth_tac cs m i thm =
201 Blast.depth_tac cs m i thm
202 handle Blast.TRANS s => (warning ("Blast_tac: " ^ s); Seq.empty);
204 (* a variant of depth_tac that avoids interference of the simplifier
205 with dup_step_tac when they are combined by auto_tac *)
207 fun slow_step_tac' cs = Classical.appWrappers cs
208 (Classical.instp_step_tac cs APPEND' Classical.haz_step_tac cs);
209 in fun nodup_depth_tac cs m i state = SELECT_GOAL
210 (Classical.safe_steps_tac cs 1 THEN_ELSE
211 (DEPTH_SOLVE (nodup_depth_tac cs m 1),
212 Classical.inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
213 (slow_step_tac' cs 1 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) 1))
217 (*Designed to be idempotent, except if blast_depth_tac instantiates variables
218 in some of the subgoals*)
219 fun mk_auto_tac (cs, ss) m n =
220 let val cs' = cs addss ss
222 blast_depth_tac cs m (* fast but can't use wrappers *)
224 (CHANGED o nodup_depth_tac cs' n); (* slower but more general *)
225 in EVERY [ALLGOALS (Simplifier.asm_full_simp_tac ss),
226 TRY (Classical.safe_tac cs),
227 REPEAT (FIRSTGOAL maintac),
228 TRY (Classical.safe_tac (cs addSss ss)),
232 fun auto_tac css = mk_auto_tac css 4 2;
233 fun Auto_tac st = auto_tac (clasimpset ()) st;
234 fun auto () = by Auto_tac;
239 (* aimed to solve the given subgoal totally, using whatever tools possible *)
240 fun force_tac (cs,ss) = let val cs' = cs addss ss in SELECT_GOAL (EVERY [
241 Classical.clarify_tac cs' 1,
242 IF_UNSOLVED (Simplifier.asm_full_simp_tac ss 1),
243 ALLGOALS (Classical.first_best_tac cs')]) end;
244 fun Force_tac i = force_tac (clasimpset ()) i;
245 fun force i = by (Force_tac i);
248 (* basic combinations *)
250 fun ADDSS tac (cs, ss) = let val cs' = cs addss ss in tac cs' end;
252 val fast_simp_tac = ADDSS Classical.fast_tac;
253 val slow_simp_tac = ADDSS Classical.slow_tac;
254 val best_simp_tac = ADDSS Classical.best_tac;
257 (* access clasimpset *)
259 fun get_local_clasimpset ctxt =
260 (Classical.get_local_claset ctxt, Simplifier.get_local_simpset ctxt);
262 fun local_clasimpset_of ctxt =
263 (Classical.local_claset_of ctxt, Simplifier.local_simpset_of ctxt);
268 fun attrib f = Thm.declaration_attribute (fn th =>
269 fn Context.Theory thy => (change_clasimpset_of thy (fn css => f (css, [th])); Context.Theory thy)
270 | Context.Proof ctxt =>
272 val cs = Classical.get_local_claset ctxt;
273 val ss = Simplifier.get_local_simpset ctxt;
274 val (cs', ss') = f ((cs, ss), [th]);
277 |> Classical.put_local_claset cs'
278 |> Simplifier.put_local_simpset ss';
279 in Context.Proof ctxt' end);
281 fun attrib' f (x, th) = (f (x, [th]), th);
283 val iff_add = attrib (op addIffs);
284 val iff_add' = attrib' addIffs_generic;
285 val iff_del = attrib (op delIffs) o attrib' delIffs_generic;
287 val iff_att = Attrib.syntax (Scan.lift
288 (Args.del >> K iff_del ||
289 Scan.option Args.add -- Args.query >> K iff_add' ||
290 Scan.option Args.add >> K iff_add));
293 (* method modifiers *)
298 [Args.$$$ iffN -- Scan.option Args.add -- Args.colon >> K ((I, iff_add): Method.modifier),
299 Args.$$$ iffN -- Scan.option Args.add -- Args.query_colon >> K (I, iff_add'),
300 Args.$$$ iffN -- Args.del -- Args.colon >> K (I, iff_del)];
302 val clasimp_modifiers =
303 Simplifier.simp_modifiers @ Splitter.split_modifiers @
304 Classical.cla_modifiers @ iff_modifiers;
309 fun clasimp_meth tac prems ctxt = Method.METHOD (fn facts =>
310 ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (local_clasimpset_of ctxt));
312 fun clasimp_meth' tac prems ctxt = Method.METHOD (fn facts =>
313 HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (local_clasimpset_of ctxt)));
315 val clasimp_method = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth;
316 val clasimp_method' = Method.bang_sectioned_args clasimp_modifiers o clasimp_meth';
320 Method.bang_sectioned_args' clasimp_modifiers (Scan.lift (Scan.option (Args.nat -- Args.nat))) m;
322 fun auto_meth NONE = clasimp_meth (CHANGED_PROP o auto_tac)
323 | auto_meth (SOME (m, n)) = clasimp_meth (CHANGED_PROP o (fn css => mk_auto_tac css m n));
329 (Attrib.add_attributes
330 [("iff", iff_att, "declaration of Simplifier / Classical rules")] #>
332 [("fastsimp", clasimp_method' fast_simp_tac, "combined fast and simp"),
333 ("slowsimp", clasimp_method' slow_simp_tac, "combined slow and simp"),
334 ("bestsimp", clasimp_method' best_simp_tac, "combined best and simp"),
335 ("force", clasimp_method' force_tac, "force"),
336 ("auto", auto_args auto_meth, "auto"),
337 ("clarsimp", clasimp_method' (CHANGED_PROP oo clarsimp_tac), "clarify simplified goal")]);