author | wenzelm |
Thu, 30 Nov 2006 14:17:29 +0100 | |
changeset 21605 | 4e7307e229b3 |
parent 21592 | 8831206d7f41 |
child 21687 | f689f729afab |
permissions | -rw-r--r-- |
5824 | 1 |
(* Title: Pure/Isar/method.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
17110 | 5 |
Isar proof methods. |
5824 | 6 |
*) |
7 |
||
8 |
signature BASIC_METHOD = |
|
9 |
sig |
|
17110 | 10 |
val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq |
11 |
val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq |
|
12 |
type method |
|
11731 | 13 |
val trace_rules: bool ref |
5824 | 14 |
val print_methods: theory -> unit |
20289 | 15 |
val Method: bstring -> (Args.src -> Proof.context -> method) -> string -> unit |
5824 | 16 |
end; |
17 |
||
18 |
signature METHOD = |
|
19 |
sig |
|
20 |
include BASIC_METHOD |
|
18227 | 21 |
val apply: method -> thm list -> cases_tactic |
22 |
val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method |
|
17110 | 23 |
val RAW_METHOD: (thm list -> tactic) -> method |
18227 | 24 |
val METHOD_CASES: (thm list -> cases_tactic) -> method |
17110 | 25 |
val METHOD: (thm list -> tactic) -> method |
26 |
val fail: method |
|
27 |
val succeed: method |
|
28 |
val insert_tac: thm list -> int -> tactic |
|
29 |
val insert: thm list -> method |
|
30 |
val insert_facts: method |
|
31 |
val SIMPLE_METHOD: tactic -> method |
|
21592 | 32 |
val SIMPLE_METHOD': (int -> tactic) -> method |
33 |
val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method |
|
17110 | 34 |
val defer: int option -> method |
35 |
val prefer: int -> method |
|
20289 | 36 |
val cheating: bool -> Proof.context -> method |
17110 | 37 |
val intro: thm list -> method |
38 |
val elim: thm list -> method |
|
20289 | 39 |
val unfold: thm list -> Proof.context -> method |
40 |
val fold: thm list -> Proof.context -> method |
|
17110 | 41 |
val atomize: bool -> method |
42 |
val this: method |
|
20289 | 43 |
val fact: thm list -> Proof.context -> method |
44 |
val assumption: Proof.context -> method |
|
45 |
val close: bool -> Proof.context -> method |
|
46 |
val trace: Proof.context -> thm list -> unit |
|
6091 | 47 |
val rule_tac: thm list -> thm list -> int -> tactic |
20289 | 48 |
val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic |
17110 | 49 |
val rule: thm list -> method |
50 |
val erule: int -> thm list -> method |
|
51 |
val drule: int -> thm list -> method |
|
52 |
val frule: int -> thm list -> method |
|
20289 | 53 |
val iprover_tac: Proof.context -> int option -> int -> tactic |
54 |
val set_tactic: (Proof.context -> thm list -> tactic) -> unit |
|
55 |
val tactic: string -> Proof.context -> method |
|
17110 | 56 |
type src |
5824 | 57 |
datatype text = |
20289 | 58 |
Basic of (Proof.context -> method) | |
15703 | 59 |
Source of src | |
20030 | 60 |
Source_i of src | |
5824 | 61 |
Then of text list | |
62 |
Orelse of text list | |
|
63 |
Try of text | |
|
19186 | 64 |
Repeat1 of text | |
65 |
SelectGoals of int * text |
|
17857 | 66 |
val primitive_text: (thm -> thm) -> text |
67 |
val succeed_text: text |
|
17110 | 68 |
val default_text: text |
69 |
val this_text: text |
|
70 |
val done_text: text |
|
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
71 |
val sorry_text: bool -> text |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
72 |
val finish_text: text option * bool -> text |
17110 | 73 |
exception METHOD_FAIL of (string * Position.T) * exn |
20289 | 74 |
val method: theory -> src -> Proof.context -> method |
75 |
val method_i: theory -> src -> Proof.context -> method |
|
76 |
val add_methods: (bstring * (src -> Proof.context -> method) * string) list |
|
17110 | 77 |
-> theory -> theory |
20289 | 78 |
val add_method: bstring * (src -> Proof.context -> method) * string |
17110 | 79 |
-> theory -> theory |
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
80 |
val method_setup: bstring * string * string -> theory -> theory |
18999 | 81 |
val syntax: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) |
20289 | 82 |
-> src -> Proof.context -> Proof.context * 'a |
17110 | 83 |
val simple_args: (Args.T list -> 'a * Args.T list) |
20289 | 84 |
-> ('a -> Proof.context -> method) -> src -> Proof.context -> method |
85 |
val ctxt_args: (Proof.context -> method) -> src -> Proof.context -> method |
|
86 |
val no_args: method -> src -> Proof.context -> method |
|
17110 | 87 |
type modifier |
18999 | 88 |
val sectioned_args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) -> |
17110 | 89 |
(Args.T list -> modifier * Args.T list) list -> |
20289 | 90 |
('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b |
17110 | 91 |
val bang_sectioned_args: |
92 |
(Args.T list -> modifier * Args.T list) list -> |
|
20289 | 93 |
(thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a |
17110 | 94 |
val bang_sectioned_args': |
95 |
(Args.T list -> modifier * Args.T list) list -> |
|
18999 | 96 |
(Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) -> |
20289 | 97 |
('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b |
17110 | 98 |
val only_sectioned_args: |
99 |
(Args.T list -> modifier * Args.T list) list -> |
|
20289 | 100 |
(Proof.context -> 'a) -> src -> Proof.context -> 'a |
101 |
val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src -> |
|
102 |
Proof.context -> 'a |
|
103 |
val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a |
|
104 |
val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a |
|
9539 | 105 |
val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic) |
20289 | 106 |
-> src -> Proof.context -> method |
18999 | 107 |
val goal_args': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) |
20289 | 108 |
-> ('a -> int -> tactic) -> src -> Proof.context -> method |
17110 | 109 |
val goal_args_ctxt: (Args.T list -> 'a * Args.T list) -> |
20289 | 110 |
(Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method |
18999 | 111 |
val goal_args_ctxt': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) -> |
20289 | 112 |
(Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method |
5824 | 113 |
end; |
114 |
||
115 |
structure Method: METHOD = |
|
116 |
struct |
|
117 |
||
17110 | 118 |
(** generic tools **) |
119 |
||
120 |
(* goal addressing *) |
|
121 |
||
122 |
fun FINDGOAL tac st = |
|
123 |
let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n) |
|
124 |
in find 1 (Thm.nprems_of st) st end; |
|
125 |
||
126 |
fun HEADGOAL tac = tac 1; |
|
127 |
||
128 |
||
5824 | 129 |
|
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
130 |
(** proof methods **) |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
131 |
|
17110 | 132 |
(* datatype method *) |
11731 | 133 |
|
18227 | 134 |
datatype method = Meth of thm list -> cases_tactic; |
11731 | 135 |
|
17756 | 136 |
fun apply (Meth m) = m; |
11731 | 137 |
|
17756 | 138 |
val RAW_METHOD_CASES = Meth; |
11731 | 139 |
|
17110 | 140 |
fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac); |
12144 | 141 |
|
17110 | 142 |
fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts => |
18474 | 143 |
Seq.THEN (ALLGOALS Tactic.conjunction_tac, tac facts)); |
8372 | 144 |
|
18474 | 145 |
fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Tactic.conjunction_tac THEN tac facts); |
5824 | 146 |
|
147 |
val fail = METHOD (K no_tac); |
|
148 |
val succeed = METHOD (K all_tac); |
|
149 |
||
150 |
||
17110 | 151 |
(* insert facts *) |
7419 | 152 |
|
153 |
local |
|
5824 | 154 |
|
21579 | 155 |
fun cut_rule_tac rule = |
156 |
Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl); |
|
6981 | 157 |
|
7419 | 158 |
in |
5824 | 159 |
|
7419 | 160 |
fun insert_tac [] i = all_tac |
161 |
| insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); |
|
6981 | 162 |
|
7555 | 163 |
val insert_facts = METHOD (ALLGOALS o insert_tac); |
7664 | 164 |
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); |
7419 | 165 |
|
9706 | 166 |
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac); |
21592 | 167 |
fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac)); |
168 |
val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL; |
|
9706 | 169 |
|
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
170 |
end; |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
171 |
|
9706 | 172 |
|
17110 | 173 |
(* shuffle subgoals *) |
174 |
||
175 |
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); |
|
18939 | 176 |
fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i))); |
17110 | 177 |
|
178 |
||
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
179 |
(* cheating *) |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
180 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
181 |
fun cheating int ctxt = METHOD (K (setmp quick_and_dirty (int orelse ! quick_and_dirty) |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
182 |
(SkipProof.cheat_tac (ProofContext.theory_of ctxt)))); |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
183 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
184 |
|
17110 | 185 |
(* unfold intro/elim rules *) |
186 |
||
21592 | 187 |
fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths)); |
188 |
fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths)); |
|
17110 | 189 |
|
190 |
||
12384 | 191 |
(* unfold/fold definitions *) |
192 |
||
18877 | 193 |
fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths)); |
194 |
fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths)); |
|
6532 | 195 |
|
12384 | 196 |
|
12829 | 197 |
(* atomize rule statements *) |
198 |
||
21592 | 199 |
fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o ObjectLogic.atomize_tac) |
12829 | 200 |
| atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac))); |
201 |
||
202 |
||
18039 | 203 |
(* this -- resolve facts directly *) |
12384 | 204 |
|
17110 | 205 |
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); |
9484 | 206 |
|
207 |
||
18039 | 208 |
(* fact -- composition by facts from context *) |
209 |
||
21592 | 210 |
fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt) |
211 |
| fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules); |
|
18039 | 212 |
|
213 |
||
17110 | 214 |
(* assumption *) |
7419 | 215 |
|
216 |
local |
|
217 |
||
17110 | 218 |
fun asm_tac ths = |
219 |
foldr (op APPEND') (K no_tac) (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths); |
|
220 |
||
19778 | 221 |
fun cond_rtac cond rule = SUBGOAL (fn (prop, i) => |
222 |
if cond (Logic.strip_assums_concl prop) |
|
223 |
then Tactic.rtac rule i else no_tac); |
|
7419 | 224 |
|
17110 | 225 |
fun assm_tac ctxt = |
226 |
assume_tac APPEND' |
|
20224
9c40a144ee0e
moved basic assumption operations from structure ProofContext to Assumption;
wenzelm
parents:
20117
diff
changeset
|
227 |
asm_tac (Assumption.prems_of ctxt) APPEND' |
19778 | 228 |
cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND' |
229 |
cond_rtac (can Logic.dest_term) Drule.termI; |
|
17110 | 230 |
|
231 |
fun assumption_tac ctxt [] = assm_tac ctxt |
|
232 |
| assumption_tac _ [fact] = asm_tac [fact] |
|
233 |
| assumption_tac _ _ = K no_tac; |
|
7419 | 234 |
|
235 |
in |
|
236 |
||
17110 | 237 |
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt); |
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
238 |
fun close immed ctxt = METHOD (K |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
239 |
(FILTER Thm.no_prems ((if immed then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))); |
7419 | 240 |
|
241 |
end; |
|
242 |
||
243 |
||
17110 | 244 |
(* rule etc. -- single-step refinements *) |
12347 | 245 |
|
17110 | 246 |
val trace_rules = ref false; |
12347 | 247 |
|
17110 | 248 |
fun trace ctxt rules = |
249 |
conditional (! trace_rules andalso not (null rules)) (fn () => |
|
250 |
Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules) |
|
251 |
|> Pretty.string_of |> tracing); |
|
12347 | 252 |
|
253 |
local |
|
254 |
||
18841 | 255 |
fun gen_rule_tac tac rules facts = |
256 |
(fn i => fn st => |
|
257 |
if null facts then tac rules i st |
|
258 |
else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules)) |
|
259 |
THEN_ALL_NEW Tactic.norm_hhf_tac; |
|
7130 | 260 |
|
10744 | 261 |
fun gen_arule_tac tac j rules facts = |
262 |
EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac); |
|
263 |
||
11785 | 264 |
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) => |
265 |
let |
|
266 |
val rules = |
|
267 |
if not (null arg_rules) then arg_rules |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19307
diff
changeset
|
268 |
else flat (ContextRules.find_rules false facts goal ctxt) |
12055 | 269 |
in trace ctxt rules; tac rules facts i end); |
10309 | 270 |
|
10744 | 271 |
fun meth tac x = METHOD (HEADGOAL o tac x); |
272 |
fun meth' tac x y = METHOD (HEADGOAL o tac x y); |
|
8220 | 273 |
|
7419 | 274 |
in |
275 |
||
10744 | 276 |
val rule_tac = gen_rule_tac Tactic.resolve_tac; |
277 |
val rule = meth rule_tac; |
|
278 |
val some_rule_tac = gen_some_rule_tac rule_tac; |
|
279 |
val some_rule = meth' some_rule_tac; |
|
280 |
||
281 |
val erule = meth' (gen_arule_tac Tactic.eresolve_tac); |
|
282 |
val drule = meth' (gen_arule_tac Tactic.dresolve_tac); |
|
283 |
val frule = meth' (gen_arule_tac Tactic.forward_tac); |
|
5824 | 284 |
|
7419 | 285 |
end; |
286 |
||
287 |
||
17587 | 288 |
(* iprover -- intuitionistic proof search *) |
17110 | 289 |
|
290 |
local |
|
291 |
||
292 |
val remdups_tac = SUBGOAL (fn (g, i) => |
|
293 |
let val prems = Logic.strip_assums_hyp g in |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18999
diff
changeset
|
294 |
REPEAT_DETERM_N (length prems - length (distinct op aconv prems)) |
17110 | 295 |
(Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i) |
296 |
end); |
|
297 |
||
298 |
fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac; |
|
299 |
||
300 |
val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist; |
|
301 |
||
302 |
fun safe_step_tac ctxt = |
|
303 |
ContextRules.Swrap ctxt |
|
304 |
(eq_assume_tac ORELSE' |
|
305 |
bires_tac true (ContextRules.netpair_bang ctxt)); |
|
8195 | 306 |
|
17110 | 307 |
fun unsafe_step_tac ctxt = |
308 |
ContextRules.wrap ctxt |
|
309 |
(assume_tac APPEND' |
|
310 |
bires_tac false (ContextRules.netpair_bang ctxt) APPEND' |
|
311 |
bires_tac false (ContextRules.netpair ctxt)); |
|
312 |
||
313 |
fun step_tac ctxt i = |
|
314 |
REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE |
|
315 |
REMDUPS (unsafe_step_tac ctxt) i; |
|
316 |
||
317 |
fun intprover_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else |
|
318 |
let |
|
319 |
val ps = Logic.strip_assums_hyp g; |
|
320 |
val c = Logic.strip_assums_concl g; |
|
321 |
in |
|
18921 | 322 |
if member (fn ((ps1, c1), (ps2, c2)) => |
19307 | 323 |
c1 aconv c2 andalso |
324 |
length ps1 = length ps2 andalso |
|
325 |
gen_eq_set (op aconv) (ps1, ps2)) gs (ps, c) then no_tac |
|
17110 | 326 |
else (step_tac ctxt THEN_ALL_NEW intprover_tac ctxt ((ps, c) :: gs) (d + 1) lim) i |
327 |
end); |
|
328 |
||
329 |
in |
|
330 |
||
17587 | 331 |
fun iprover_tac ctxt opt_lim = |
18939 | 332 |
SELECT_GOAL (DEEPEN (2, the_default 20 opt_lim) (intprover_tac ctxt [] 0) 4 1); |
17110 | 333 |
|
334 |
end; |
|
8195 | 335 |
|
336 |
||
8351 | 337 |
(* ML tactics *) |
338 |
||
20289 | 339 |
val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic); |
8351 | 340 |
fun set_tactic f = tactic_ref := f; |
341 |
||
342 |
fun tactic txt ctxt = METHOD (fn facts => |
|
9631 | 343 |
(Context.use_mltext |
20289 | 344 |
("let fun tactic (ctxt: Proof.context) (facts: thm list) : tactic = \ |
16500
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
wenzelm
parents:
16448
diff
changeset
|
345 |
\let val thm = ProofContext.get_thm_closure ctxt o PureThy.Name\n\ |
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
wenzelm
parents:
16448
diff
changeset
|
346 |
\ and thms = ProofContext.get_thms_closure ctxt o PureThy.Name in\n" |
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
wenzelm
parents:
16448
diff
changeset
|
347 |
^ txt ^ |
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
wenzelm
parents:
16448
diff
changeset
|
348 |
"\nend in Method.set_tactic tactic end") |
15531 | 349 |
false NONE; |
350 |
Context.setmp (SOME (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts)); |
|
8351 | 351 |
|
352 |
||
5824 | 353 |
|
17110 | 354 |
(** method syntax **) |
355 |
||
356 |
(* method text *) |
|
357 |
||
358 |
type src = Args.src; |
|
5824 | 359 |
|
17110 | 360 |
datatype text = |
20289 | 361 |
Basic of (Proof.context -> method) | |
17110 | 362 |
Source of src | |
20030 | 363 |
Source_i of src | |
17110 | 364 |
Then of text list | |
365 |
Orelse of text list | |
|
366 |
Try of text | |
|
19186 | 367 |
Repeat1 of text | |
368 |
SelectGoals of int * text; |
|
17110 | 369 |
|
17857 | 370 |
val primitive_text = Basic o K o SIMPLE_METHOD o PRIMITIVE; |
371 |
val succeed_text = Basic (K succeed); |
|
17110 | 372 |
val default_text = Source (Args.src (("default", []), Position.none)); |
373 |
val this_text = Basic (K this); |
|
374 |
val done_text = Basic (K (SIMPLE_METHOD all_tac)); |
|
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
375 |
val sorry_text = Basic o cheating; |
17110 | 376 |
|
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
377 |
fun finish_text (NONE, immed) = Basic (close immed) |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
378 |
| finish_text (SOME txt, immed) = Then [txt, Basic (close immed)]; |
17110 | 379 |
|
380 |
||
381 |
(* method definitions *) |
|
5824 | 382 |
|
16448 | 383 |
structure MethodsData = TheoryDataFun |
384 |
(struct |
|
5824 | 385 |
val name = "Isar/methods"; |
20289 | 386 |
type T = (((src -> Proof.context -> method) * string) * stamp) NameSpace.table; |
5824 | 387 |
|
16347 | 388 |
val empty = NameSpace.empty_table; |
6546 | 389 |
val copy = I; |
16448 | 390 |
val extend = I; |
17496 | 391 |
fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups => |
16347 | 392 |
error ("Attempt to merge different versions of method(s) " ^ commas_quote dups); |
5824 | 393 |
|
16347 | 394 |
fun print _ meths = |
5824 | 395 |
let |
396 |
fun prt_meth (name, ((_, comment), _)) = Pretty.block |
|
6849 | 397 |
[Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
5824 | 398 |
in |
16347 | 399 |
[Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))] |
9222 | 400 |
|> Pretty.chunks |> Pretty.writeln |
5824 | 401 |
end; |
16448 | 402 |
end); |
5824 | 403 |
|
18708 | 404 |
val _ = Context.add_setup MethodsData.init; |
5824 | 405 |
val print_methods = MethodsData.print; |
7611 | 406 |
|
5824 | 407 |
|
408 |
(* get methods *) |
|
409 |
||
5916 | 410 |
exception METHOD_FAIL of (string * Position.T) * exn; |
411 |
||
20030 | 412 |
fun method_i thy = |
5824 | 413 |
let |
20030 | 414 |
val meths = #2 (MethodsData.get thy); |
5884 | 415 |
fun meth src = |
20030 | 416 |
let val ((name, _), pos) = Args.dest_src src in |
17412 | 417 |
(case Symtab.lookup meths name of |
15531 | 418 |
NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos) |
419 |
| SOME ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src)) |
|
5824 | 420 |
end; |
421 |
in meth end; |
|
422 |
||
20030 | 423 |
fun method thy = method_i thy o Args.map_name (NameSpace.intern (#1 (MethodsData.get thy))); |
424 |
||
5824 | 425 |
|
17110 | 426 |
(* add method *) |
5824 | 427 |
|
428 |
fun add_methods raw_meths thy = |
|
429 |
let |
|
16145 | 430 |
val new_meths = raw_meths |> map (fn (name, f, comment) => |
16347 | 431 |
(name, ((f, comment), stamp ()))); |
5824 | 432 |
|
17110 | 433 |
fun add meths = NameSpace.extend_table (Sign.naming_of thy) (meths, new_meths) |
16347 | 434 |
handle Symtab.DUPS dups => |
435 |
error ("Duplicate declaration of method(s) " ^ commas_quote dups); |
|
436 |
in MethodsData.map add thy end; |
|
5824 | 437 |
|
9194 | 438 |
val add_method = add_methods o Library.single; |
439 |
||
18728 | 440 |
fun Method name meth cmt = Context.>> (add_method (name, meth, cmt)); |
5824 | 441 |
|
442 |
||
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
443 |
(* method_setup *) |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
444 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
445 |
fun method_setup (name, txt, cmt) = |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
446 |
Context.use_let |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
447 |
"val thm = PureThy.get_thm_closure (Context.the_context ()) o PureThy.Name;\n\ |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
448 |
\val thms = PureThy.get_thms_closure (Context.the_context ()) o PureThy.Name;\n\ |
20289 | 449 |
\val method: bstring * (Method.src -> Proof.context -> Proof.method) * string" |
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
450 |
"Method.add_method method" |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
451 |
("(" ^ Library.quote name ^ ", " ^ txt ^ ", " ^ Library.quote cmt ^ ")"); |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
452 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
453 |
|
5884 | 454 |
|
17110 | 455 |
(** concrete syntax **) |
5824 | 456 |
|
5884 | 457 |
(* basic *) |
458 |
||
18999 | 459 |
fun syntax scan = Args.context_syntax "method" scan; |
5824 | 460 |
|
17110 | 461 |
fun simple_args scan f src ctxt : method = |
8351 | 462 |
#2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt); |
463 |
||
20289 | 464 |
fun ctxt_args (f: Proof.context -> method) src ctxt = |
8282 | 465 |
#2 (syntax (Scan.succeed (f ctxt)) src ctxt); |
7555 | 466 |
|
467 |
fun no_args m = ctxt_args (K m); |
|
5884 | 468 |
|
469 |
||
470 |
(* sections *) |
|
5824 | 471 |
|
20289 | 472 |
type modifier = (Proof.context -> Proof.context) * attribute; |
7268 | 473 |
|
474 |
local |
|
475 |
||
8381 | 476 |
fun sect ss = Scan.first (map Scan.lift ss); |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19307
diff
changeset
|
477 |
fun thms ss = Scan.repeat (Scan.unless (sect ss) Attrib.multi_thm) >> flat; |
5884 | 478 |
|
19186 | 479 |
fun app (f, att) (context, ths) = foldl_map att (Context.map_proof f context, ths); |
5824 | 480 |
|
18999 | 481 |
fun section ss = (sect ss -- thms ss) :-- (fn (m, ths) => Scan.depend (fn context => |
19186 | 482 |
Scan.succeed (app m (context, ths)))) >> #2; |
5884 | 483 |
|
7601 | 484 |
fun sectioned args ss = args -- Scan.repeat (section ss); |
5884 | 485 |
|
7268 | 486 |
in |
5824 | 487 |
|
5884 | 488 |
fun sectioned_args args ss f src ctxt = |
8282 | 489 |
let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt |
5921 | 490 |
in f x ctxt' end; |
5884 | 491 |
|
7601 | 492 |
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f; |
9777 | 493 |
fun bang_sectioned_args' ss scan f = |
494 |
sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f); |
|
7601 | 495 |
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f); |
7268 | 496 |
|
18999 | 497 |
fun thms_ctxt_args f = sectioned_args (thms []) [] f; |
8093 | 498 |
fun thms_args f = thms_ctxt_args (K o f); |
9706 | 499 |
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected"); |
5824 | 500 |
|
7268 | 501 |
end; |
502 |
||
5824 | 503 |
|
17587 | 504 |
(* iprover syntax *) |
12347 | 505 |
|
506 |
local |
|
507 |
||
508 |
val introN = "intro"; |
|
509 |
val elimN = "elim"; |
|
510 |
val destN = "dest"; |
|
511 |
val ruleN = "rule"; |
|
512 |
||
513 |
fun modifier name kind kind' att = |
|
15531 | 514 |
Args.$$$ name |-- (kind >> K NONE || kind' |-- Args.nat --| Args.colon >> SOME) |
20289 | 515 |
>> (pair (I: Proof.context -> Proof.context) o att); |
12347 | 516 |
|
17587 | 517 |
val iprover_modifiers = |
18728 | 518 |
[modifier destN Args.bang_colon Args.bang ContextRules.dest_bang, |
519 |
modifier destN Args.colon (Scan.succeed ()) ContextRules.dest, |
|
520 |
modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang, |
|
521 |
modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim, |
|
522 |
modifier introN Args.bang_colon Args.bang ContextRules.intro_bang, |
|
523 |
modifier introN Args.colon (Scan.succeed ()) ContextRules.intro, |
|
524 |
Args.del -- Args.colon >> K (I, ContextRules.rule_del)]; |
|
12347 | 525 |
|
526 |
in |
|
527 |
||
18640 | 528 |
val iprover_meth = |
529 |
bang_sectioned_args' iprover_modifiers (Scan.lift (Scan.option Args.nat)) |
|
530 |
(fn n => fn prems => fn ctxt => METHOD (fn facts => |
|
531 |
HEADGOAL (insert_tac (prems @ facts) THEN' |
|
532 |
ObjectLogic.atomize_tac THEN' iprover_tac ctxt n))); |
|
12347 | 533 |
|
534 |
end; |
|
535 |
||
536 |
||
9539 | 537 |
(* tactic syntax *) |
8238 | 538 |
|
10744 | 539 |
fun nat_thms_args f = uncurry f oo |
18999 | 540 |
(#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.thms)); |
10744 | 541 |
|
12119 | 542 |
fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >> |
21592 | 543 |
(fn (quant, s) => SIMPLE_METHOD'' quant (tac s))) src ctxt); |
8537 | 544 |
|
9539 | 545 |
fun goal_args args tac = goal_args' (Scan.lift args) tac; |
8238 | 546 |
|
14174
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13650
diff
changeset
|
547 |
fun goal_args_ctxt' args tac src ctxt = |
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13650
diff
changeset
|
548 |
#2 (syntax (Args.goal_spec HEADGOAL -- args >> |
21592 | 549 |
(fn (quant, s) => SIMPLE_METHOD'' quant (tac ctxt s))) src ctxt); |
8238 | 550 |
|
14174
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13650
diff
changeset
|
551 |
fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac; |
5824 | 552 |
|
14718 | 553 |
|
18708 | 554 |
(* theory setup *) |
5824 | 555 |
|
18708 | 556 |
val _ = Context.add_setup (add_methods |
5824 | 557 |
[("fail", no_args fail, "force failure"), |
558 |
("succeed", no_args succeed, "succeed"), |
|
9587 | 559 |
("-", no_args insert_facts, "do nothing (insert current facts only)"), |
9539 | 560 |
("insert", thms_args insert, "insert theorems, ignoring facts (improper)"), |
12384 | 561 |
("intro", thms_args intro, "repeatedly apply introduction rules"), |
562 |
("elim", thms_args elim, "repeatedly apply elimination rules"), |
|
18877 | 563 |
("unfold", thms_ctxt_args unfold_meth, "unfold definitions"), |
564 |
("fold", thms_ctxt_args fold_meth, "fold definitions"), |
|
12829 | 565 |
("atomize", (atomize o #2) oo syntax (Args.mode "full"), |
11962 | 566 |
"present local premises as object-level statements"), |
18640 | 567 |
("iprover", iprover_meth, "intuitionistic proof search"), |
12384 | 568 |
("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"), |
10744 | 569 |
("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"), |
570 |
("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"), |
|
571 |
("frule", nat_thms_args frule, "apply rule in forward manner (improper)"), |
|
8195 | 572 |
("this", no_args this, "apply current facts as rules"), |
18039 | 573 |
("fact", thms_ctxt_args fact, "composition by facts from context"), |
8238 | 574 |
("assumption", ctxt_args assumption, "proof by assumption, preferring facts"), |
20335 | 575 |
("rename_tac", goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac, |
576 |
"rename parameters of goal (dynamic instantiation)"), |
|
577 |
("rotate_tac", goal_args (Scan.optional Args.int 1) Tactic.rotate_tac, |
|
578 |
"rotate assumptions of goal"), |
|
18708 | 579 |
("tactic", simple_args Args.name tactic, "ML tactic as proof method")]); |
5824 | 580 |
|
581 |
||
16145 | 582 |
(*final declarations of this structure!*) |
583 |
val unfold = unfold_meth; |
|
584 |
val fold = fold_meth; |
|
585 |
||
5824 | 586 |
end; |
587 |
||
588 |
structure BasicMethod: BASIC_METHOD = Method; |
|
589 |
open BasicMethod; |