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