author | wenzelm |
Mon, 21 Jan 2002 16:15:16 +0100 | |
changeset 12829 | c92128238f85 |
parent 12399 | 2ba27248af7f |
child 13650 | 31bd2a8cdbe2 |
permissions | -rw-r--r-- |
5824 | 1 |
(* Title: Pure/Isar/method.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
8807 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5824 | 5 |
|
6 |
Proof methods. |
|
7 |
*) |
|
8 |
||
9 |
signature BASIC_METHOD = |
|
10 |
sig |
|
11731 | 11 |
val trace_rules: bool ref |
5824 | 12 |
val print_methods: theory -> unit |
13 |
val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit |
|
14 |
end; |
|
15 |
||
16 |
signature METHOD = |
|
17 |
sig |
|
18 |
include BASIC_METHOD |
|
12055 | 19 |
val trace: Proof.context -> thm list -> unit |
12144 | 20 |
val RAW_METHOD: (thm list -> tactic) -> Proof.method |
21 |
val RAW_METHOD_CASES: |
|
22 |
(thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> Proof.method |
|
6091 | 23 |
val METHOD: (thm list -> tactic) -> Proof.method |
8372 | 24 |
val METHOD_CASES: |
25 |
(thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> Proof.method |
|
9706 | 26 |
val SIMPLE_METHOD: tactic -> Proof.method |
27 |
val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> Proof.method |
|
5824 | 28 |
val fail: Proof.method |
29 |
val succeed: Proof.method |
|
8167 | 30 |
val defer: int option -> Proof.method |
31 |
val prefer: int -> Proof.method |
|
7419 | 32 |
val insert_tac: thm list -> int -> tactic |
7574 | 33 |
val insert: thm list -> Proof.method |
7555 | 34 |
val insert_facts: Proof.method |
7601 | 35 |
val unfold: thm list -> Proof.method |
7419 | 36 |
val fold: thm list -> Proof.method |
37 |
val multi_resolve: thm list -> thm -> thm Seq.seq |
|
38 |
val multi_resolves: thm list -> thm list -> thm Seq.seq |
|
12347 | 39 |
val rules_tac: Proof.context -> int option -> int -> tactic |
6091 | 40 |
val rule_tac: thm list -> thm list -> int -> tactic |
10309 | 41 |
val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic |
6091 | 42 |
val rule: thm list -> Proof.method |
10744 | 43 |
val erule: int -> thm list -> Proof.method |
44 |
val drule: int -> thm list -> Proof.method |
|
45 |
val frule: int -> thm list -> Proof.method |
|
8195 | 46 |
val this: Proof.method |
7555 | 47 |
val assumption: Proof.context -> Proof.method |
12119 | 48 |
val impose_hyps_tac: Proof.context -> tactic |
8351 | 49 |
val set_tactic: (Proof.context -> thm list -> tactic) -> unit |
50 |
val tactic: string -> Proof.context -> Proof.method |
|
5916 | 51 |
exception METHOD_FAIL of (string * Position.T) * exn |
5824 | 52 |
val method: theory -> Args.src -> Proof.context -> Proof.method |
9539 | 53 |
val add_method: bstring * (Args.src -> Proof.context -> Proof.method) * string |
54 |
-> theory -> theory |
|
5824 | 55 |
val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list |
56 |
-> theory -> theory |
|
5884 | 57 |
val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) -> |
8282 | 58 |
Args.src -> Proof.context -> Proof.context * 'a |
8351 | 59 |
val simple_args: (Args.T list -> 'a * Args.T list) |
60 |
-> ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method |
|
7555 | 61 |
val ctxt_args: (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method |
5884 | 62 |
val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method |
7268 | 63 |
type modifier |
7601 | 64 |
val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) -> |
7268 | 65 |
(Args.T list -> modifier * Args.T list) list -> |
9864 | 66 |
('a -> Proof.context -> 'b) -> Args.src -> Proof.context -> 'b |
7601 | 67 |
val bang_sectioned_args: |
68 |
(Args.T list -> modifier * Args.T list) list -> |
|
9864 | 69 |
(thm list -> Proof.context -> 'a) -> Args.src -> Proof.context -> 'a |
9777 | 70 |
val bang_sectioned_args': |
71 |
(Args.T list -> modifier * Args.T list) list -> |
|
72 |
(Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) -> |
|
9864 | 73 |
('a -> thm list -> Proof.context -> 'b) -> Args.src -> Proof.context -> 'b |
7601 | 74 |
val only_sectioned_args: |
75 |
(Args.T list -> modifier * Args.T list) list -> |
|
9864 | 76 |
(Proof.context -> 'a) -> Args.src -> Proof.context -> 'a |
77 |
val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> Args.src -> Proof.context -> 'a |
|
78 |
val thms_args: (thm list -> 'a) -> Args.src -> Proof.context -> 'a |
|
79 |
val thm_args: (thm -> 'a) -> Args.src -> Proof.context -> 'a |
|
5824 | 80 |
datatype text = |
81 |
Basic of (Proof.context -> Proof.method) | |
|
82 |
Source of Args.src | |
|
83 |
Then of text list | |
|
84 |
Orelse of text list | |
|
85 |
Try of text | |
|
86 |
Repeat1 of text |
|
87 |
val refine: text -> Proof.state -> Proof.state Seq.seq |
|
8238 | 88 |
val refine_end: text -> Proof.state -> Proof.state Seq.seq |
5824 | 89 |
val proof: text option -> Proof.state -> Proof.state Seq.seq |
8966 | 90 |
val local_qed: bool -> text option |
12144 | 91 |
-> (Proof.context -> string * (string * thm list) list -> unit) * |
12055 | 92 |
(Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq |
6981 | 93 |
val local_terminal_proof: text * text option |
12144 | 94 |
-> (Proof.context -> string * (string * thm list) list -> unit) * |
12055 | 95 |
(Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq |
12144 | 96 |
val local_default_proof: (Proof.context -> string * (string * thm list) list -> unit) * |
12055 | 97 |
(Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq |
12144 | 98 |
val local_immediate_proof: (Proof.context -> string * (string * thm list) list -> unit) * |
12055 | 99 |
(Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq |
12144 | 100 |
val local_done_proof: (Proof.context -> string * (string * thm list) list -> unit) * |
12055 | 101 |
(Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq |
8966 | 102 |
val global_qed: bool -> text option |
12244 | 103 |
-> Proof.state -> theory * ((string * string) * (string * thm list) list) |
6934 | 104 |
val global_terminal_proof: text * text option |
12244 | 105 |
-> Proof.state -> theory * ((string * string) * (string * thm list) list) |
106 |
val global_default_proof: Proof.state -> theory * ((string * string) * (string * thm list) list) |
|
107 |
val global_immediate_proof: Proof.state -> |
|
108 |
theory * ((string * string) * (string * thm list) list) |
|
109 |
val global_done_proof: Proof.state -> theory * ((string * string) * (string * thm list) list) |
|
9539 | 110 |
val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic) |
111 |
-> Args.src -> Proof.context -> Proof.method |
|
112 |
val goal_args': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) |
|
113 |
-> ('a -> int -> tactic) -> Args.src -> Proof.context -> Proof.method |
|
5824 | 114 |
val setup: (theory -> theory) list |
115 |
end; |
|
116 |
||
117 |
structure Method: METHOD = |
|
118 |
struct |
|
119 |
||
120 |
||
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
121 |
(** proof methods **) |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
122 |
|
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
123 |
(* tracing *) |
11731 | 124 |
|
125 |
val trace_rules = ref false; |
|
126 |
||
12055 | 127 |
fun trace ctxt rules = |
12262 | 128 |
conditional (! trace_rules andalso not (null rules)) (fn () => |
129 |
Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules) |
|
130 |
|> Pretty.string_of |> tracing); |
|
11731 | 131 |
|
132 |
||
8372 | 133 |
(* make methods *) |
5824 | 134 |
|
12144 | 135 |
val RAW_METHOD = Proof.method; |
136 |
val RAW_METHOD_CASES = Proof.method_cases; |
|
137 |
||
138 |
fun METHOD m = Proof.method (fn facts => TRY Tactic.conjunction_tac THEN m facts); |
|
139 |
fun METHOD_CASES m = |
|
140 |
Proof.method_cases (fn facts => Seq.THEN (TRY Tactic.conjunction_tac, m facts)); |
|
8372 | 141 |
|
5824 | 142 |
|
143 |
(* primitive *) |
|
144 |
||
145 |
val fail = METHOD (K no_tac); |
|
146 |
val succeed = METHOD (K all_tac); |
|
147 |
||
148 |
||
8167 | 149 |
(* shuffle *) |
150 |
||
8240 | 151 |
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); |
8167 | 152 |
fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1))); |
153 |
||
154 |
||
7419 | 155 |
(* insert *) |
156 |
||
157 |
local |
|
5824 | 158 |
|
6981 | 159 |
fun cut_rule_tac raw_rule = |
160 |
let |
|
161 |
val rule = Drule.forall_intr_vars raw_rule; |
|
162 |
val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl; |
|
7555 | 163 |
in Tactic.rtac (rule COMP revcut_rl) end; |
6981 | 164 |
|
7419 | 165 |
in |
5824 | 166 |
|
7419 | 167 |
fun insert_tac [] i = all_tac |
168 |
| insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); |
|
6981 | 169 |
|
7555 | 170 |
val insert_facts = METHOD (ALLGOALS o insert_tac); |
7664 | 171 |
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); |
7419 | 172 |
|
9706 | 173 |
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac); |
174 |
fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac)); |
|
175 |
||
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
176 |
end; |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
177 |
|
9706 | 178 |
|
12384 | 179 |
(* unfold/fold definitions *) |
180 |
||
181 |
fun unfold ths = SIMPLE_METHOD (CHANGED_PROP (rewrite_goals_tac ths)); |
|
182 |
fun fold ths = SIMPLE_METHOD (CHANGED_PROP (fold_goals_tac ths)); |
|
6532 | 183 |
|
12384 | 184 |
|
12829 | 185 |
(* atomize rule statements *) |
186 |
||
187 |
fun atomize false = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o ObjectLogic.atomize_tac) |
|
188 |
| atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac))); |
|
189 |
||
190 |
||
12384 | 191 |
(* unfold intro/elim rules *) |
192 |
||
193 |
fun intro ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths)); |
|
194 |
fun elim ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths)); |
|
9484 | 195 |
|
196 |
||
7419 | 197 |
(* multi_resolve *) |
198 |
||
199 |
local |
|
200 |
||
201 |
fun res th i rule = |
|
202 |
Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty; |
|
203 |
||
204 |
fun multi_res _ [] rule = Seq.single rule |
|
205 |
| multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule)); |
|
206 |
||
207 |
in |
|
208 |
||
209 |
val multi_resolve = multi_res 1; |
|
8372 | 210 |
fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules)); |
7419 | 211 |
|
212 |
end; |
|
213 |
||
214 |
||
12347 | 215 |
(* rules_tac *) |
8372 | 216 |
|
7419 | 217 |
local |
5824 | 218 |
|
12359 | 219 |
val remdups_tac = SUBGOAL (fn (g, i) => |
220 |
let val prems = Logic.strip_assums_hyp g in |
|
221 |
REPEAT_DETERM_N (length prems - length (gen_distinct op aconv prems)) |
|
222 |
(Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i) |
|
223 |
end); |
|
12347 | 224 |
|
225 |
fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac; |
|
226 |
||
227 |
fun gen_eq_set e s1 s2 = |
|
228 |
length s1 = length s2 andalso |
|
229 |
gen_subset e (s1, s2) andalso gen_subset e (s2, s1); |
|
230 |
||
12350 | 231 |
val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist; |
12347 | 232 |
|
233 |
fun safe_step_tac ctxt = |
|
12350 | 234 |
ContextRules.Swrap ctxt |
235 |
(eq_assume_tac ORELSE' |
|
236 |
bires_tac true (ContextRules.netpair_bang ctxt)); |
|
12347 | 237 |
|
238 |
fun unsafe_step_tac ctxt = |
|
12350 | 239 |
ContextRules.wrap ctxt |
240 |
(assume_tac APPEND' |
|
241 |
bires_tac false (ContextRules.netpair_bang ctxt) APPEND' |
|
242 |
bires_tac false (ContextRules.netpair ctxt)); |
|
12347 | 243 |
|
244 |
fun step_tac ctxt i = |
|
245 |
REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE |
|
246 |
REMDUPS (unsafe_step_tac ctxt) i; |
|
247 |
||
248 |
fun intpr_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else |
|
249 |
let |
|
250 |
val ps = Logic.strip_assums_hyp g; |
|
251 |
val c = Logic.strip_assums_concl g; |
|
252 |
in |
|
253 |
if gen_mem (fn ((ps1, c1), (ps2, c2)) => |
|
254 |
c1 aconv c2 andalso gen_eq_set op aconv ps1 ps2) ((ps, c), gs) then no_tac |
|
255 |
else (step_tac ctxt THEN_ALL_NEW intpr_tac ctxt ((ps, c) :: gs) (d + 1) lim) i |
|
256 |
end); |
|
257 |
||
258 |
in |
|
259 |
||
12359 | 260 |
fun rules_tac ctxt opt_lim = |
261 |
SELECT_GOAL (DEEPEN (2, if_none opt_lim 20) (intpr_tac ctxt [] 0) 4 1); |
|
12347 | 262 |
|
263 |
end; |
|
264 |
||
265 |
||
266 |
(* rule_tac etc. *) |
|
267 |
||
268 |
local |
|
269 |
||
10541
fdec07d4f047
resolveq(_cases)_tac moved to HOL/Tools/induct_method.ML;
wenzelm
parents:
10529
diff
changeset
|
270 |
fun gen_rule_tac tac rules [] i st = tac rules i st |
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
271 |
| gen_rule_tac tac rules facts i st = |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
272 |
Seq.flat (Seq.map (fn rule => (tac o single) rule i st) (multi_resolves facts rules)); |
7130 | 273 |
|
10744 | 274 |
fun gen_arule_tac tac j rules facts = |
275 |
EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac); |
|
276 |
||
11785 | 277 |
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) => |
278 |
let |
|
279 |
val rules = |
|
280 |
if not (null arg_rules) then arg_rules |
|
12399 | 281 |
else flat (ContextRules.find_rules false facts goal ctxt) |
12055 | 282 |
in trace ctxt rules; tac rules facts i end); |
10309 | 283 |
|
10744 | 284 |
fun meth tac x = METHOD (HEADGOAL o tac x); |
285 |
fun meth' tac x y = METHOD (HEADGOAL o tac x y); |
|
8220 | 286 |
|
7419 | 287 |
in |
288 |
||
10744 | 289 |
val rule_tac = gen_rule_tac Tactic.resolve_tac; |
290 |
val rule = meth rule_tac; |
|
291 |
val some_rule_tac = gen_some_rule_tac rule_tac; |
|
292 |
val some_rule = meth' some_rule_tac; |
|
293 |
||
294 |
val erule = meth' (gen_arule_tac Tactic.eresolve_tac); |
|
295 |
val drule = meth' (gen_arule_tac Tactic.dresolve_tac); |
|
296 |
val frule = meth' (gen_arule_tac Tactic.forward_tac); |
|
5824 | 297 |
|
7419 | 298 |
end; |
299 |
||
300 |
||
8195 | 301 |
(* this *) |
302 |
||
8671 | 303 |
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); |
8195 | 304 |
|
305 |
||
306 |
(* assumption *) |
|
7555 | 307 |
|
10378
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
wenzelm
parents:
10309
diff
changeset
|
308 |
fun asm_tac ths = |
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
wenzelm
parents:
10309
diff
changeset
|
309 |
foldr (op APPEND') (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths, K no_tac); |
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
wenzelm
parents:
10309
diff
changeset
|
310 |
|
10405 | 311 |
fun assm_tac ctxt = |
312 |
assume_tac APPEND' |
|
313 |
asm_tac (ProofContext.prems_of ctxt) APPEND' |
|
314 |
Tactic.rtac Drule.reflexive_thm; |
|
7419 | 315 |
|
7555 | 316 |
fun assumption_tac ctxt [] = assm_tac ctxt |
10378
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
wenzelm
parents:
10309
diff
changeset
|
317 |
| assumption_tac _ [fact] = asm_tac [fact] |
7555 | 318 |
| assumption_tac _ _ = K no_tac; |
7419 | 319 |
|
8671 | 320 |
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt); |
7419 | 321 |
|
322 |
||
9539 | 323 |
(* res_inst_tac etc. *) |
8238 | 324 |
|
12119 | 325 |
(*robustify instantiation by imposing (part of) the present static context*) |
326 |
val impose_hyps_tac = |
|
327 |
PRIMITIVE o Drule.impose_hyps o flat o map #1 o ProofContext.assumptions_of; |
|
328 |
||
9539 | 329 |
(*Note: insts refer to the implicit (!!) goal context; use at your own risk*) |
12119 | 330 |
fun gen_res_inst _ tac _ (quant, ([], thms)) = |
9565
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
331 |
METHOD (fn facts => (quant (insert_tac facts THEN' tac thms))) |
12119 | 332 |
| gen_res_inst tac _ ctxt (quant, (insts, [thm])) = |
333 |
METHOD (fn facts => (impose_hyps_tac ctxt THEN quant (insert_tac facts THEN' tac insts thm))) |
|
334 |
| gen_res_inst _ _ _ _ = error "Cannot have instantiations with multiple rules"; |
|
8238 | 335 |
|
9565
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
336 |
val res_inst = gen_res_inst Tactic.res_inst_tac Tactic.resolve_tac; |
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
337 |
val eres_inst = gen_res_inst Tactic.eres_inst_tac Tactic.eresolve_tac; |
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
338 |
val dres_inst = gen_res_inst Tactic.dres_inst_tac Tactic.dresolve_tac; |
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
339 |
val forw_inst = gen_res_inst Tactic.forw_inst_tac Tactic.forward_tac; |
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
340 |
val cut_inst = gen_res_inst Tactic.cut_inst_tac Tactic.cut_facts_tac; |
8238 | 341 |
|
342 |
||
8329 | 343 |
(* simple Prolog interpreter *) |
344 |
||
345 |
fun prolog_tac rules facts = |
|
346 |
DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules))); |
|
347 |
||
348 |
val prolog = METHOD o prolog_tac; |
|
349 |
||
350 |
||
8351 | 351 |
(* ML tactics *) |
352 |
||
353 |
val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic); |
|
354 |
fun set_tactic f = tactic_ref := f; |
|
355 |
||
356 |
fun tactic txt ctxt = METHOD (fn facts => |
|
9631 | 357 |
(Context.use_mltext |
358 |
("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \ |
|
359 |
\let val thm = PureIsar.ProofContext.get_thm_closure ctxt\n\ |
|
360 |
\ and thms = PureIsar.ProofContext.get_thms_closure ctxt in\n" |
|
361 |
^ txt ^ |
|
362 |
"\nend in PureIsar.Method.set_tactic tactic end") |
|
363 |
false None; |
|
364 |
Context.setmp (Some (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts)); |
|
8351 | 365 |
|
366 |
||
5824 | 367 |
|
368 |
(** methods theory data **) |
|
369 |
||
370 |
(* data kind 'Isar/methods' *) |
|
371 |
||
372 |
structure MethodsDataArgs = |
|
373 |
struct |
|
374 |
val name = "Isar/methods"; |
|
375 |
type T = |
|
376 |
{space: NameSpace.T, |
|
377 |
meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table}; |
|
378 |
||
379 |
val empty = {space = NameSpace.empty, meths = Symtab.empty}; |
|
6546 | 380 |
val copy = I; |
5824 | 381 |
val prep_ext = I; |
382 |
fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) = |
|
383 |
{space = NameSpace.merge (space1, space2), |
|
384 |
meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups => |
|
385 |
error ("Attempt to merge different versions of methods " ^ commas_quote dups)}; |
|
386 |
||
9222 | 387 |
fun print _ {space, meths} = |
5824 | 388 |
let |
389 |
fun prt_meth (name, ((_, comment), _)) = Pretty.block |
|
6849 | 390 |
[Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
5824 | 391 |
in |
8720 | 392 |
[Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))] |
9222 | 393 |
|> Pretty.chunks |> Pretty.writeln |
5824 | 394 |
end; |
395 |
end; |
|
396 |
||
397 |
structure MethodsData = TheoryDataFun(MethodsDataArgs); |
|
398 |
val print_methods = MethodsData.print; |
|
7611 | 399 |
|
5824 | 400 |
|
401 |
(* get methods *) |
|
402 |
||
5916 | 403 |
exception METHOD_FAIL of (string * Position.T) * exn; |
404 |
||
5824 | 405 |
fun method thy = |
406 |
let |
|
407 |
val {space, meths} = MethodsData.get thy; |
|
408 |
||
5884 | 409 |
fun meth src = |
410 |
let |
|
411 |
val ((raw_name, _), pos) = Args.dest_src src; |
|
412 |
val name = NameSpace.intern space raw_name; |
|
413 |
in |
|
5824 | 414 |
(case Symtab.lookup (meths, name) of |
415 |
None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos) |
|
5916 | 416 |
| Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src)) |
5824 | 417 |
end; |
418 |
in meth end; |
|
419 |
||
420 |
||
9194 | 421 |
(* add_method(s) *) |
5824 | 422 |
|
423 |
fun add_methods raw_meths thy = |
|
424 |
let |
|
425 |
val full = Sign.full_name (Theory.sign_of thy); |
|
426 |
val new_meths = |
|
427 |
map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths; |
|
428 |
||
429 |
val {space, meths} = MethodsData.get thy; |
|
430 |
val space' = NameSpace.extend (space, map fst new_meths); |
|
431 |
val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups => |
|
432 |
error ("Duplicate declaration of method(s) " ^ commas_quote dups); |
|
433 |
in |
|
434 |
thy |> MethodsData.put {space = space', meths = meths'} |
|
435 |
end; |
|
436 |
||
9194 | 437 |
val add_method = add_methods o Library.single; |
438 |
||
5824 | 439 |
(*implicit version*) |
440 |
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]); |
|
441 |
||
442 |
||
5884 | 443 |
|
444 |
(** method syntax **) |
|
5824 | 445 |
|
5884 | 446 |
(* basic *) |
447 |
||
448 |
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) = |
|
449 |
Args.syntax "method" scan; |
|
5824 | 450 |
|
8351 | 451 |
fun simple_args scan f src ctxt : Proof.method = |
452 |
#2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt); |
|
453 |
||
7555 | 454 |
fun ctxt_args (f: Proof.context -> Proof.method) src ctxt = |
8282 | 455 |
#2 (syntax (Scan.succeed (f ctxt)) src ctxt); |
7555 | 456 |
|
457 |
fun no_args m = ctxt_args (K m); |
|
5884 | 458 |
|
459 |
||
460 |
(* sections *) |
|
5824 | 461 |
|
7268 | 462 |
type modifier = (Proof.context -> Proof.context) * Proof.context attribute; |
463 |
||
464 |
local |
|
465 |
||
8381 | 466 |
fun sect ss = Scan.first (map Scan.lift ss); |
5884 | 467 |
fun thms ss = Scan.unless (sect ss) Attrib.local_thms; |
468 |
fun thmss ss = Scan.repeat (thms ss) >> flat; |
|
469 |
||
7268 | 470 |
fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]); |
5824 | 471 |
|
7268 | 472 |
fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt => |
473 |
Scan.succeed (apply m (ctxt, ths)))) >> #2; |
|
5884 | 474 |
|
7601 | 475 |
fun sectioned args ss = args -- Scan.repeat (section ss); |
5884 | 476 |
|
7268 | 477 |
in |
5824 | 478 |
|
5884 | 479 |
fun sectioned_args args ss f src ctxt = |
8282 | 480 |
let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt |
5921 | 481 |
in f x ctxt' end; |
5884 | 482 |
|
7601 | 483 |
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f; |
9777 | 484 |
fun bang_sectioned_args' ss scan f = |
485 |
sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f); |
|
7601 | 486 |
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f); |
7268 | 487 |
|
8093 | 488 |
fun thms_ctxt_args f = sectioned_args (thmss []) [] f; |
489 |
fun thms_args f = thms_ctxt_args (K o f); |
|
9706 | 490 |
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected"); |
5824 | 491 |
|
7268 | 492 |
end; |
493 |
||
5824 | 494 |
|
12347 | 495 |
(* rules syntax *) |
496 |
||
497 |
local |
|
498 |
||
499 |
val introN = "intro"; |
|
500 |
val elimN = "elim"; |
|
501 |
val destN = "dest"; |
|
502 |
val ruleN = "rule"; |
|
503 |
||
504 |
fun modifier name kind kind' att = |
|
505 |
Args.$$$ name |-- (kind >> K None || kind' |-- Args.nat --| Args.colon >> Some) |
|
506 |
>> (pair (I: Proof.context -> Proof.context) o att); |
|
507 |
||
508 |
val rules_modifiers = |
|
12384 | 509 |
[modifier destN Args.bang_colon Args.bang ContextRules.dest_bang_local, |
12350 | 510 |
modifier destN Args.colon (Scan.succeed ()) ContextRules.dest_local, |
511 |
modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang_local, |
|
512 |
modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim_local, |
|
513 |
modifier introN Args.bang_colon Args.bang ContextRules.intro_bang_local, |
|
514 |
modifier introN Args.colon (Scan.succeed ()) ContextRules.intro_local, |
|
515 |
Args.del -- Args.colon >> K (I, ContextRules.rule_del_local)]; |
|
12347 | 516 |
|
517 |
in |
|
518 |
||
519 |
fun rules_args m = bang_sectioned_args' rules_modifiers (Scan.lift (Scan.option Args.nat)) m; |
|
520 |
||
521 |
fun rules_meth n prems ctxt = METHOD (fn facts => |
|
12350 | 522 |
HEADGOAL (insert_tac (prems @ facts) THEN' ObjectLogic.atomize_tac THEN' rules_tac ctxt n)); |
12347 | 523 |
|
524 |
end; |
|
525 |
||
526 |
||
9539 | 527 |
(* tactic syntax *) |
8238 | 528 |
|
10744 | 529 |
fun nat_thms_args f = uncurry f oo |
530 |
(#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.local_thmss)); |
|
531 |
||
8238 | 532 |
val insts = |
9539 | 533 |
Scan.optional |
9565
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
534 |
(Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --| |
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
535 |
Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss; |
8238 | 536 |
|
12119 | 537 |
fun inst_args f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts) src ctxt)); |
8537 | 538 |
|
539 |
||
12119 | 540 |
fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >> |
541 |
(fn (quant, s) => SIMPLE_METHOD' quant (K (impose_hyps_tac ctxt) THEN' tac s))) src ctxt); |
|
8537 | 542 |
|
9539 | 543 |
fun goal_args args tac = goal_args' (Scan.lift args) tac; |
8238 | 544 |
|
545 |
||
5824 | 546 |
|
547 |
(** method text **) |
|
548 |
||
549 |
(* datatype text *) |
|
550 |
||
551 |
datatype text = |
|
552 |
Basic of (Proof.context -> Proof.method) | |
|
553 |
Source of Args.src | |
|
554 |
Then of text list | |
|
555 |
Orelse of text list | |
|
556 |
Try of text | |
|
557 |
Repeat1 of text; |
|
558 |
||
559 |
||
560 |
(* refine *) |
|
561 |
||
8238 | 562 |
fun gen_refine f text state = |
5824 | 563 |
let |
564 |
val thy = Proof.theory_of state; |
|
565 |
||
8238 | 566 |
fun eval (Basic mth) = f mth |
567 |
| eval (Source src) = f (method thy src) |
|
5824 | 568 |
| eval (Then txts) = Seq.EVERY (map eval txts) |
569 |
| eval (Orelse txts) = Seq.FIRST (map eval txts) |
|
570 |
| eval (Try txt) = Seq.TRY (eval txt) |
|
571 |
| eval (Repeat1 txt) = Seq.REPEAT1 (eval txt); |
|
572 |
in eval text state end; |
|
573 |
||
8238 | 574 |
val refine = gen_refine Proof.refine; |
575 |
val refine_end = gen_refine Proof.refine_end; |
|
6404 | 576 |
|
5824 | 577 |
|
6404 | 578 |
(* structured proof steps *) |
5824 | 579 |
|
7506 | 580 |
val default_text = Source (Args.src (("default", []), Position.none)); |
8195 | 581 |
val this_text = Basic (K this); |
9706 | 582 |
val done_text = Basic (K (SIMPLE_METHOD all_tac)); |
7555 | 583 |
|
8966 | 584 |
fun close_text asm = Basic (fn ctxt => METHOD (K |
585 |
(FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac)))); |
|
586 |
||
587 |
fun finish_text asm None = close_text asm |
|
588 |
| finish_text asm (Some txt) = Then [txt, close_text asm]; |
|
6872 | 589 |
|
5824 | 590 |
fun proof opt_text state = |
591 |
state |
|
592 |
|> Proof.assert_backward |
|
6404 | 593 |
|> refine (if_none opt_text default_text) |
8242 | 594 |
|> Seq.map (Proof.goal_facts (K [])) |
5824 | 595 |
|> Seq.map Proof.enter_forward; |
596 |
||
8966 | 597 |
fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text)); |
598 |
fun local_terminal_proof (text, opt_text) pr = |
|
599 |
Seq.THEN (proof (Some text), local_qed true opt_text pr); |
|
600 |
val local_default_proof = local_terminal_proof (default_text, None); |
|
8195 | 601 |
val local_immediate_proof = local_terminal_proof (this_text, None); |
8966 | 602 |
fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr); |
5824 | 603 |
|
6872 | 604 |
|
8966 | 605 |
fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text)); |
5824 | 606 |
|
8966 | 607 |
fun global_qed asm opt_text state = |
6872 | 608 |
state |
8966 | 609 |
|> global_qeds asm opt_text |
6872 | 610 |
|> Proof.check_result "Failed to finish proof" state |
611 |
|> Seq.hd; |
|
612 |
||
8966 | 613 |
fun global_term_proof asm (text, opt_text) state = |
6872 | 614 |
state |
615 |
|> proof (Some text) |
|
616 |
|> Proof.check_result "Terminal proof method failed" state |
|
8966 | 617 |
|> (Seq.flat o Seq.map (global_qeds asm opt_text)) |
6872 | 618 |
|> Proof.check_result "Failed to finish proof (after successful terminal method)" state |
619 |
|> Seq.hd; |
|
620 |
||
8966 | 621 |
val global_terminal_proof = global_term_proof true; |
6934 | 622 |
val global_default_proof = global_terminal_proof (default_text, None); |
8966 | 623 |
val global_immediate_proof = global_terminal_proof (this_text, None); |
624 |
val global_done_proof = global_term_proof false (done_text, None); |
|
5824 | 625 |
|
626 |
||
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
627 |
|
5824 | 628 |
(** theory setup **) |
629 |
||
9539 | 630 |
(* misc tactic emulations *) |
631 |
||
632 |
val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac; |
|
633 |
val thin_meth = goal_args Args.name Tactic.thin_tac; |
|
634 |
val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac; |
|
9631 | 635 |
val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac; |
9539 | 636 |
|
637 |
||
5824 | 638 |
(* pure_methods *) |
639 |
||
640 |
val pure_methods = |
|
641 |
[("fail", no_args fail, "force failure"), |
|
642 |
("succeed", no_args succeed, "succeed"), |
|
9587 | 643 |
("-", no_args insert_facts, "do nothing (insert current facts only)"), |
9539 | 644 |
("insert", thms_args insert, "insert theorems, ignoring facts (improper)"), |
7601 | 645 |
("unfold", thms_args unfold, "unfold definitions"), |
12384 | 646 |
("intro", thms_args intro, "repeatedly apply introduction rules"), |
647 |
("elim", thms_args elim, "repeatedly apply elimination rules"), |
|
7601 | 648 |
("fold", thms_args fold, "fold definitions"), |
12829 | 649 |
("atomize", (atomize o #2) oo syntax (Args.mode "full"), |
11962 | 650 |
"present local premises as object-level statements"), |
12347 | 651 |
("rules", rules_args rules_meth, "apply many rules, including proof search"), |
12384 | 652 |
("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"), |
10744 | 653 |
("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"), |
654 |
("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"), |
|
655 |
("frule", nat_thms_args frule, "apply rule in forward manner (improper)"), |
|
8195 | 656 |
("this", no_args this, "apply current facts as rules"), |
8238 | 657 |
("assumption", ctxt_args assumption, "proof by assumption, preferring facts"), |
9539 | 658 |
("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"), |
659 |
("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"), |
|
660 |
("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"), |
|
661 |
("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"), |
|
662 |
("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"), |
|
9565
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
663 |
("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation!)"), |
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
wenzelm
parents:
9539
diff
changeset
|
664 |
("thin_tac", thin_meth, "remove premise (dynamic instantiation!)"), |
9631 | 665 |
("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation!)"), |
666 |
("rotate_tac", rotate_meth, "rotate assumptions of goal"), |
|
8351 | 667 |
("prolog", thms_args prolog, "simple prolog interpreter"), |
668 |
("tactic", simple_args Args.name tactic, "ML tactic as proof method")]; |
|
5824 | 669 |
|
670 |
||
671 |
(* setup *) |
|
672 |
||
8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset
|
673 |
val setup = |
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
674 |
[MethodsData.init, add_methods pure_methods, |
12350 | 675 |
(#1 o PureThy.add_thms [(("", Drule.equal_intr_rule), [ContextRules.intro_query_global None])])]; |
5824 | 676 |
|
677 |
||
678 |
end; |
|
679 |
||
680 |
||
681 |
structure BasicMethod: BASIC_METHOD = Method; |
|
682 |
open BasicMethod; |