author | wenzelm |
Sun, 15 Mar 2009 20:19:14 +0100 | |
changeset 30540 | 5e2d9604a3d3 |
parent 30515 | bca05b17b618 |
child 30544 | 0ed8fe16331a |
permissions | -rw-r--r-- |
5824 | 1 |
(* Title: Pure/Isar/method.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
17110 | 4 |
Isar proof methods. |
5824 | 5 |
*) |
6 |
||
7 |
signature BASIC_METHOD = |
|
8 |
sig |
|
17110 | 9 |
val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq |
10 |
val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq |
|
11731 | 11 |
val trace_rules: bool ref |
5824 | 12 |
end; |
13 |
||
14 |
signature METHOD = |
|
15 |
sig |
|
16 |
include BASIC_METHOD |
|
30508
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
17 |
type method |
25957
2cfb703fa8d8
improved apply: handle thread position, apply to context here;
wenzelm
parents:
25699
diff
changeset
|
18 |
val apply: Position.T -> (Proof.context -> method) -> Proof.context -> thm list -> cases_tactic |
18227 | 19 |
val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method |
17110 | 20 |
val RAW_METHOD: (thm list -> tactic) -> method |
18227 | 21 |
val METHOD_CASES: (thm list -> cases_tactic) -> method |
17110 | 22 |
val METHOD: (thm list -> tactic) -> method |
23 |
val fail: method |
|
24 |
val succeed: method |
|
25 |
val insert_tac: thm list -> int -> tactic |
|
26 |
val insert: thm list -> method |
|
27 |
val insert_facts: method |
|
28 |
val SIMPLE_METHOD: tactic -> method |
|
21592 | 29 |
val SIMPLE_METHOD': (int -> tactic) -> method |
30 |
val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method |
|
17110 | 31 |
val defer: int option -> method |
32 |
val prefer: int -> method |
|
20289 | 33 |
val cheating: bool -> Proof.context -> method |
17110 | 34 |
val intro: thm list -> method |
35 |
val elim: thm list -> method |
|
20289 | 36 |
val unfold: thm list -> Proof.context -> method |
37 |
val fold: thm list -> Proof.context -> method |
|
17110 | 38 |
val atomize: bool -> method |
39 |
val this: method |
|
20289 | 40 |
val fact: thm list -> Proof.context -> method |
30234
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
wenzelm
parents:
30190
diff
changeset
|
41 |
val assm_tac: Proof.context -> int -> tactic |
20289 | 42 |
val assumption: Proof.context -> method |
43 |
val close: bool -> Proof.context -> method |
|
44 |
val trace: Proof.context -> thm list -> unit |
|
6091 | 45 |
val rule_tac: thm list -> thm list -> int -> tactic |
20289 | 46 |
val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic |
25270 | 47 |
val intros_tac: thm list -> thm list -> tactic |
17110 | 48 |
val rule: thm list -> method |
49 |
val erule: int -> thm list -> method |
|
50 |
val drule: int -> thm list -> method |
|
51 |
val frule: int -> thm list -> method |
|
27235 | 52 |
val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context |
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26291
diff
changeset
|
53 |
val tactic: string * Position.T -> Proof.context -> method |
27235 | 54 |
val raw_tactic: string * Position.T -> Proof.context -> method |
27729 | 55 |
type src = Args.src |
5824 | 56 |
datatype text = |
23349 | 57 |
Basic of (Proof.context -> method) * Position.T | |
15703 | 58 |
Source of src | |
20030 | 59 |
Source_i of src | |
5824 | 60 |
Then of text list | |
61 |
Orelse of text list | |
|
62 |
Try of text | |
|
19186 | 63 |
Repeat1 of text | |
64 |
SelectGoals of int * text |
|
17857 | 65 |
val primitive_text: (thm -> thm) -> text |
66 |
val succeed_text: text |
|
17110 | 67 |
val default_text: text |
68 |
val this_text: text |
|
69 |
val done_text: text |
|
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
70 |
val sorry_text: bool -> text |
23349 | 71 |
val finish_text: text option * bool -> Position.T -> text |
27729 | 72 |
val print_methods: theory -> unit |
26892 | 73 |
val intern: theory -> xstring -> string |
74 |
val defined: theory -> string -> bool |
|
20289 | 75 |
val method: theory -> src -> Proof.context -> method |
76 |
val method_i: theory -> src -> Proof.context -> method |
|
77 |
val add_methods: (bstring * (src -> Proof.context -> method) * string) list |
|
17110 | 78 |
-> theory -> theory |
20289 | 79 |
val add_method: bstring * (src -> Proof.context -> method) * string |
17110 | 80 |
-> theory -> theory |
30512 | 81 |
val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context |
82 |
val setup: binding -> (Proof.context -> method) context_parser -> string -> theory -> theory |
|
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26291
diff
changeset
|
83 |
val method_setup: bstring -> string * Position.T -> string -> theory -> theory |
30512 | 84 |
val simple_args: 'a parser -> ('a -> Proof.context -> method) -> src -> Proof.context -> method |
20289 | 85 |
val ctxt_args: (Proof.context -> method) -> src -> Proof.context -> method |
86 |
val no_args: method -> src -> Proof.context -> method |
|
30540 | 87 |
type modifier = (Proof.context -> Proof.context) * attribute |
88 |
val section: modifier parser list -> thm list context_parser |
|
89 |
val sections: modifier parser list -> thm list list context_parser |
|
30512 | 90 |
val sectioned_args: 'a context_parser -> modifier parser list -> |
20289 | 91 |
('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b |
30512 | 92 |
val bang_sectioned_args: modifier parser list -> |
20289 | 93 |
(thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a |
30512 | 94 |
val bang_sectioned_args': modifier parser list -> 'a context_parser -> |
20289 | 95 |
('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b |
30512 | 96 |
val only_sectioned_args: modifier parser list -> (Proof.context -> 'a) -> src -> |
20289 | 97 |
Proof.context -> 'a |
30512 | 98 |
val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a |
20289 | 99 |
val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a |
100 |
val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a |
|
30512 | 101 |
val goal_args: 'a parser -> ('a -> int -> tactic) -> src -> Proof.context -> method |
102 |
val goal_args': 'a context_parser -> ('a -> int -> tactic) -> src -> Proof.context -> method |
|
103 |
val goal_args_ctxt: 'a parser -> (Proof.context -> 'a -> int -> tactic) -> src -> |
|
104 |
Proof.context -> method |
|
105 |
val goal_args_ctxt': 'a context_parser -> (Proof.context -> 'a -> int -> tactic) -> src -> |
|
106 |
Proof.context -> method |
|
107 |
val parse: text parser |
|
5824 | 108 |
end; |
109 |
||
110 |
structure Method: METHOD = |
|
111 |
struct |
|
112 |
||
17110 | 113 |
(** generic tools **) |
114 |
||
115 |
(* goal addressing *) |
|
116 |
||
117 |
fun FINDGOAL tac st = |
|
118 |
let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n) |
|
119 |
in find 1 (Thm.nprems_of st) st end; |
|
120 |
||
121 |
fun HEADGOAL tac = tac 1; |
|
122 |
||
123 |
||
5824 | 124 |
|
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
125 |
(** proof methods **) |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
126 |
|
17110 | 127 |
(* datatype method *) |
11731 | 128 |
|
18227 | 129 |
datatype method = Meth of thm list -> cases_tactic; |
11731 | 130 |
|
25957
2cfb703fa8d8
improved apply: handle thread position, apply to context here;
wenzelm
parents:
25699
diff
changeset
|
131 |
fun apply pos meth_fun ctxt facts goal = Position.setmp_thread_data_seq pos |
2cfb703fa8d8
improved apply: handle thread position, apply to context here;
wenzelm
parents:
25699
diff
changeset
|
132 |
(fn () => let val Meth meth = meth_fun ctxt in meth facts goal end) (); |
11731 | 133 |
|
17756 | 134 |
val RAW_METHOD_CASES = Meth; |
11731 | 135 |
|
17110 | 136 |
fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac); |
12144 | 137 |
|
17110 | 138 |
fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts => |
21687 | 139 |
Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts)); |
8372 | 140 |
|
21687 | 141 |
fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts); |
5824 | 142 |
|
143 |
val fail = METHOD (K no_tac); |
|
144 |
val succeed = METHOD (K all_tac); |
|
145 |
||
146 |
||
17110 | 147 |
(* insert facts *) |
7419 | 148 |
|
149 |
local |
|
5824 | 150 |
|
21579 | 151 |
fun cut_rule_tac rule = |
152 |
Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl); |
|
6981 | 153 |
|
7419 | 154 |
in |
5824 | 155 |
|
7419 | 156 |
fun insert_tac [] i = all_tac |
157 |
| insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); |
|
6981 | 158 |
|
7555 | 159 |
val insert_facts = METHOD (ALLGOALS o insert_tac); |
7664 | 160 |
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); |
7419 | 161 |
|
9706 | 162 |
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac); |
21592 | 163 |
fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac)); |
164 |
val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL; |
|
9706 | 165 |
|
12324
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
166 |
end; |
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
wenzelm
parents:
12311
diff
changeset
|
167 |
|
9706 | 168 |
|
17110 | 169 |
(* shuffle subgoals *) |
170 |
||
171 |
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); |
|
18939 | 172 |
fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i))); |
17110 | 173 |
|
174 |
||
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
175 |
(* cheating *) |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
176 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
177 |
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
|
178 |
(SkipProof.cheat_tac (ProofContext.theory_of ctxt)))); |
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
179 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
180 |
|
17110 | 181 |
(* unfold intro/elim rules *) |
182 |
||
21592 | 183 |
fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths)); |
184 |
fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths)); |
|
17110 | 185 |
|
186 |
||
12384 | 187 |
(* unfold/fold definitions *) |
188 |
||
18877 | 189 |
fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths)); |
190 |
fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths)); |
|
6532 | 191 |
|
12384 | 192 |
|
12829 | 193 |
(* atomize rule statements *) |
194 |
||
23590
ad95084a5c63
renamed ObjectLogic.atomize_tac to ObjectLogic.atomize_prems_tac;
wenzelm
parents:
23577
diff
changeset
|
195 |
fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o ObjectLogic.atomize_prems_tac) |
12829 | 196 |
| atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac))); |
197 |
||
198 |
||
18039 | 199 |
(* this -- resolve facts directly *) |
12384 | 200 |
|
17110 | 201 |
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); |
9484 | 202 |
|
203 |
||
18039 | 204 |
(* fact -- composition by facts from context *) |
205 |
||
21592 | 206 |
fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt) |
207 |
| fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules); |
|
18039 | 208 |
|
209 |
||
17110 | 210 |
(* assumption *) |
7419 | 211 |
|
212 |
local |
|
213 |
||
19778 | 214 |
fun cond_rtac cond rule = SUBGOAL (fn (prop, i) => |
215 |
if cond (Logic.strip_assums_concl prop) |
|
216 |
then Tactic.rtac rule i else no_tac); |
|
7419 | 217 |
|
29857
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents:
29301
diff
changeset
|
218 |
in |
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
kleing
parents:
29301
diff
changeset
|
219 |
|
30234
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
wenzelm
parents:
30190
diff
changeset
|
220 |
fun assm_tac ctxt = |
17110 | 221 |
assume_tac APPEND' |
23349 | 222 |
Goal.assume_rule_tac ctxt APPEND' |
19778 | 223 |
cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND' |
224 |
cond_rtac (can Logic.dest_term) Drule.termI; |
|
17110 | 225 |
|
23349 | 226 |
fun assumption ctxt = METHOD (HEADGOAL o |
30234
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
wenzelm
parents:
30190
diff
changeset
|
227 |
(fn [] => assm_tac ctxt |
23349 | 228 |
| [fact] => solve_tac [fact] |
229 |
| _ => K no_tac)); |
|
230 |
||
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
231 |
fun close immed ctxt = METHOD (K |
23349 | 232 |
(FILTER Thm.no_prems |
30234
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
wenzelm
parents:
30190
diff
changeset
|
233 |
((if immed then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))); |
7419 | 234 |
|
235 |
end; |
|
236 |
||
237 |
||
17110 | 238 |
(* rule etc. -- single-step refinements *) |
12347 | 239 |
|
17110 | 240 |
val trace_rules = ref false; |
12347 | 241 |
|
17110 | 242 |
fun trace ctxt rules = |
21962 | 243 |
if ! trace_rules andalso not (null rules) then |
17110 | 244 |
Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules) |
21962 | 245 |
|> Pretty.string_of |> tracing |
246 |
else (); |
|
12347 | 247 |
|
248 |
local |
|
249 |
||
18841 | 250 |
fun gen_rule_tac tac rules facts = |
251 |
(fn i => fn st => |
|
252 |
if null facts then tac rules i st |
|
253 |
else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules)) |
|
21687 | 254 |
THEN_ALL_NEW Goal.norm_hhf_tac; |
7130 | 255 |
|
10744 | 256 |
fun gen_arule_tac tac j rules facts = |
257 |
EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac); |
|
258 |
||
11785 | 259 |
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) => |
260 |
let |
|
261 |
val rules = |
|
262 |
if not (null arg_rules) then arg_rules |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19307
diff
changeset
|
263 |
else flat (ContextRules.find_rules false facts goal ctxt) |
12055 | 264 |
in trace ctxt rules; tac rules facts i end); |
10309 | 265 |
|
10744 | 266 |
fun meth tac x = METHOD (HEADGOAL o tac x); |
267 |
fun meth' tac x y = METHOD (HEADGOAL o tac x y); |
|
8220 | 268 |
|
7419 | 269 |
in |
270 |
||
10744 | 271 |
val rule_tac = gen_rule_tac Tactic.resolve_tac; |
272 |
val rule = meth rule_tac; |
|
273 |
val some_rule_tac = gen_some_rule_tac rule_tac; |
|
274 |
val some_rule = meth' some_rule_tac; |
|
275 |
||
276 |
val erule = meth' (gen_arule_tac Tactic.eresolve_tac); |
|
277 |
val drule = meth' (gen_arule_tac Tactic.dresolve_tac); |
|
278 |
val frule = meth' (gen_arule_tac Tactic.forward_tac); |
|
5824 | 279 |
|
7419 | 280 |
end; |
281 |
||
282 |
||
25270 | 283 |
(* intros_tac -- pervasive search spanned by intro rules *) |
284 |
||
285 |
fun intros_tac intros facts = |
|
286 |
ALLGOALS (insert_tac facts THEN' |
|
287 |
REPEAT_ALL_NEW (resolve_tac intros)) |
|
288 |
THEN Tactic.distinct_subgoals_tac; |
|
289 |
||
290 |
||
8351 | 291 |
(* ML tactics *) |
292 |
||
26472
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
293 |
structure TacticData = ProofDataFun |
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
294 |
( |
27235 | 295 |
type T = thm list -> tactic; |
26472
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
296 |
fun init _ = undefined; |
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
297 |
); |
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
298 |
|
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
299 |
val set_tactic = TacticData.put; |
8351 | 300 |
|
26472
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
301 |
fun ml_tactic (txt, pos) ctxt = |
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
302 |
let |
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
303 |
val ctxt' = ctxt |> Context.proof_map |
27235 | 304 |
(ML_Context.expression pos |
305 |
"fun tactic (facts: thm list) : tactic" |
|
26472
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
306 |
"Context.map_proof (Method.set_tactic tactic)" txt); |
27235 | 307 |
in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') end; |
23425 | 308 |
|
309 |
fun tactic txt ctxt = METHOD (ml_tactic txt ctxt); |
|
310 |
fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt); |
|
8351 | 311 |
|
312 |
||
5824 | 313 |
|
17110 | 314 |
(** method syntax **) |
315 |
||
316 |
(* method text *) |
|
317 |
||
318 |
type src = Args.src; |
|
5824 | 319 |
|
17110 | 320 |
datatype text = |
23349 | 321 |
Basic of (Proof.context -> method) * Position.T | |
17110 | 322 |
Source of src | |
20030 | 323 |
Source_i of src | |
17110 | 324 |
Then of text list | |
325 |
Orelse of text list | |
|
326 |
Try of text | |
|
19186 | 327 |
Repeat1 of text | |
328 |
SelectGoals of int * text; |
|
17110 | 329 |
|
23349 | 330 |
fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r)), Position.none); |
331 |
val succeed_text = Basic (K succeed, Position.none); |
|
17110 | 332 |
val default_text = Source (Args.src (("default", []), Position.none)); |
23349 | 333 |
val this_text = Basic (K this, Position.none); |
334 |
val done_text = Basic (K (SIMPLE_METHOD all_tac), Position.none); |
|
335 |
fun sorry_text int = Basic (cheating int, Position.none); |
|
17110 | 336 |
|
23349 | 337 |
fun finish_text (NONE, immed) pos = Basic (close immed, pos) |
338 |
| finish_text (SOME txt, immed) pos = Then [txt, Basic (close immed, pos)]; |
|
17110 | 339 |
|
340 |
||
341 |
(* method definitions *) |
|
5824 | 342 |
|
24116 | 343 |
structure Methods = TheoryDataFun |
22846 | 344 |
( |
20289 | 345 |
type T = (((src -> Proof.context -> method) * string) * stamp) NameSpace.table; |
16347 | 346 |
val empty = NameSpace.empty_table; |
6546 | 347 |
val copy = I; |
16448 | 348 |
val extend = I; |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23590
diff
changeset
|
349 |
fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup => |
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23590
diff
changeset
|
350 |
error ("Attempt to merge different versions of method " ^ quote dup); |
22846 | 351 |
); |
5824 | 352 |
|
22846 | 353 |
fun print_methods thy = |
354 |
let |
|
24116 | 355 |
val meths = Methods.get thy; |
22846 | 356 |
fun prt_meth (name, ((_, comment), _)) = Pretty.block |
357 |
[Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
|
358 |
in |
|
359 |
[Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))] |
|
360 |
|> Pretty.chunks |> Pretty.writeln |
|
361 |
end; |
|
7611 | 362 |
|
5824 | 363 |
|
364 |
(* get methods *) |
|
365 |
||
26892 | 366 |
val intern = NameSpace.intern o #1 o Methods.get; |
367 |
val defined = Symtab.defined o #2 o Methods.get; |
|
368 |
||
20030 | 369 |
fun method_i thy = |
5824 | 370 |
let |
24116 | 371 |
val meths = #2 (Methods.get thy); |
5884 | 372 |
fun meth src = |
20030 | 373 |
let val ((name, _), pos) = Args.dest_src src in |
17412 | 374 |
(case Symtab.lookup meths name of |
15531 | 375 |
NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos) |
27751 | 376 |
| SOME ((mth, _), _) => (Position.report (Markup.method name) pos; mth src)) |
5824 | 377 |
end; |
378 |
in meth end; |
|
379 |
||
24116 | 380 |
fun method thy = method_i thy o Args.map_name (NameSpace.intern (#1 (Methods.get thy))); |
20030 | 381 |
|
5824 | 382 |
|
17110 | 383 |
(* add method *) |
5824 | 384 |
|
385 |
fun add_methods raw_meths thy = |
|
386 |
let |
|
16145 | 387 |
val new_meths = raw_meths |> map (fn (name, f, comment) => |
29004 | 388 |
(Binding.name name, ((f, comment), stamp ()))); |
5824 | 389 |
|
30466 | 390 |
fun add meths = fold (snd oo NameSpace.define (Sign.naming_of thy)) new_meths meths |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23590
diff
changeset
|
391 |
handle Symtab.DUP dup => error ("Duplicate declaration of method " ^ quote dup); |
24116 | 392 |
in Methods.map add thy end; |
5824 | 393 |
|
9194 | 394 |
val add_method = add_methods o Library.single; |
395 |
||
5824 | 396 |
|
30512 | 397 |
(* method setup *) |
398 |
||
399 |
fun syntax scan = Args.context_syntax "method" scan; |
|
400 |
||
401 |
fun setup name scan comment = |
|
402 |
add_methods [(Binding.name_of name, |
|
403 |
fn src => fn ctxt => let val (m, ctxt') = syntax scan src ctxt in m ctxt' end, comment)]; |
|
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
404 |
|
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26291
diff
changeset
|
405 |
fun method_setup name (txt, pos) cmt = |
26472
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
406 |
Context.theory_map (ML_Context.expression pos |
22118 | 407 |
"val method: bstring * (Method.src -> Proof.context -> Proof.method) * string" |
22086 | 408 |
"Context.map_theory (Method.add_method method)" |
26472
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
wenzelm
parents:
26463
diff
changeset
|
409 |
("(" ^ quote name ^ ", " ^ txt ^ ", " ^ quote cmt ^ ")")); |
17356
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
410 |
|
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
wenzelm
parents:
17314
diff
changeset
|
411 |
|
5884 | 412 |
|
17110 | 413 |
(** concrete syntax **) |
5824 | 414 |
|
27813
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
415 |
structure P = OuterParse; |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
416 |
|
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
417 |
|
5884 | 418 |
(* basic *) |
419 |
||
17110 | 420 |
fun simple_args scan f src ctxt : method = |
21879 | 421 |
fst (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt); |
8351 | 422 |
|
20289 | 423 |
fun ctxt_args (f: Proof.context -> method) src ctxt = |
21879 | 424 |
fst (syntax (Scan.succeed (f ctxt)) src ctxt); |
7555 | 425 |
|
426 |
fun no_args m = ctxt_args (K m); |
|
5884 | 427 |
|
428 |
||
429 |
(* sections *) |
|
5824 | 430 |
|
20289 | 431 |
type modifier = (Proof.context -> Proof.context) * attribute; |
7268 | 432 |
|
433 |
local |
|
434 |
||
24010
2ef318813e1a
method section scanners: added [[declaration]] syntax, ignore sid-effects of thms;
wenzelm
parents:
23937
diff
changeset
|
435 |
fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat; |
30190 | 436 |
fun app (f, att) (context, ths) = Library.foldl_map att (Context.map_proof f context, ths); |
5824 | 437 |
|
30540 | 438 |
in |
439 |
||
24022 | 440 |
fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|-- |
441 |
(fn (m, ths) => Scan.succeed (app m (context, ths)))); |
|
5884 | 442 |
|
30540 | 443 |
fun sections ss = Scan.repeat (section ss); |
5824 | 444 |
|
5884 | 445 |
fun sectioned_args args ss f src ctxt = |
30540 | 446 |
let val ((x, _), ctxt') = syntax (args -- sections ss) src ctxt |
5921 | 447 |
in f x ctxt' end; |
5884 | 448 |
|
7601 | 449 |
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f; |
9777 | 450 |
fun bang_sectioned_args' ss scan f = |
451 |
sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f); |
|
7601 | 452 |
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f); |
7268 | 453 |
|
18999 | 454 |
fun thms_ctxt_args f = sectioned_args (thms []) [] f; |
8093 | 455 |
fun thms_args f = thms_ctxt_args (K o f); |
9706 | 456 |
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected"); |
5824 | 457 |
|
7268 | 458 |
end; |
459 |
||
5824 | 460 |
|
30515 | 461 |
(* extra rule methods *) |
462 |
||
463 |
fun xrule_meth m = |
|
464 |
Scan.lift (Scan.optional (Args.parens OuterParse.nat) 0) -- Attrib.thms >> |
|
465 |
(fn (n, ths) => K (m n ths)); |
|
466 |
||
467 |
||
9539 | 468 |
(* tactic syntax *) |
8238 | 469 |
|
30515 | 470 |
fun goal_args' args tac src ctxt = fst (syntax (Args.goal_spec -- args >> |
21592 | 471 |
(fn (quant, s) => SIMPLE_METHOD'' quant (tac s))) src ctxt); |
8537 | 472 |
|
9539 | 473 |
fun goal_args args tac = goal_args' (Scan.lift args) tac; |
8238 | 474 |
|
14174
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13650
diff
changeset
|
475 |
fun goal_args_ctxt' args tac src ctxt = |
30515 | 476 |
fst (syntax (Args.goal_spec -- args >> |
21592 | 477 |
(fn (quant, s) => SIMPLE_METHOD'' quant (tac ctxt s))) src ctxt); |
8238 | 478 |
|
14174
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
ballarin
parents:
13650
diff
changeset
|
479 |
fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac; |
5824 | 480 |
|
14718 | 481 |
|
27813
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
482 |
(* outer parser *) |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
483 |
|
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
484 |
fun is_symid_meth s = |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
485 |
s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.ident_or_symbolic s; |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
486 |
|
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
487 |
local |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
488 |
|
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
489 |
fun meth4 x = |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
490 |
(P.position (P.xname >> rpair []) >> (Source o Args.src) || |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
491 |
P.$$$ "(" |-- P.!!! (meth0 --| P.$$$ ")")) x |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
492 |
and meth3 x = |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
493 |
(meth4 --| P.$$$ "?" >> Try || |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
494 |
meth4 --| P.$$$ "+" >> Repeat1 || |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
495 |
meth4 -- (P.$$$ "[" |-- Scan.optional P.nat 1 --| P.$$$ "]") >> (SelectGoals o swap) || |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
496 |
meth4) x |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
497 |
and meth2 x = |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
498 |
(P.position (P.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) || |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
499 |
meth3) x |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
500 |
and meth1 x = (P.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
501 |
and meth0 x = (P.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x; |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
502 |
|
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
503 |
in val parse = meth3 end; |
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
504 |
|
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27751
diff
changeset
|
505 |
|
18708 | 506 |
(* theory setup *) |
5824 | 507 |
|
26463 | 508 |
val _ = Context.>> (Context.map_theory |
30515 | 509 |
(setup (Binding.name "fail") (Scan.succeed (K fail)) "force failure" #> |
510 |
setup (Binding.name "succeed") (Scan.succeed (K succeed)) "succeed" #> |
|
511 |
setup (Binding.name "-") (Scan.succeed (K insert_facts)) |
|
512 |
"do nothing (insert current facts only)" #> |
|
513 |
setup (Binding.name "insert") (Attrib.thms >> (K o insert)) |
|
514 |
"insert theorems, ignoring facts (improper)" #> |
|
515 |
setup (Binding.name "intro") (Attrib.thms >> (K o intro)) |
|
516 |
"repeatedly apply introduction rules" #> |
|
517 |
setup (Binding.name "elim") (Attrib.thms >> (K o elim)) |
|
518 |
"repeatedly apply elimination rules" #> |
|
519 |
setup (Binding.name "unfold") (Attrib.thms >> unfold_meth) "unfold definitions" #> |
|
520 |
setup (Binding.name "fold") (Attrib.thms >> fold_meth) "fold definitions" #> |
|
521 |
setup (Binding.name "atomize") (Args.mode "full" >> (K o atomize)) |
|
522 |
"present local premises as object-level statements" #> |
|
523 |
setup (Binding.name "rule") (Attrib.thms >> some_rule) "apply some intro/elim rule" #> |
|
524 |
setup (Binding.name "erule") (xrule_meth erule) "apply rule in elimination manner (improper)" #> |
|
525 |
setup (Binding.name "drule") (xrule_meth drule) "apply rule in destruct manner (improper)" #> |
|
526 |
setup (Binding.name "frule") (xrule_meth frule) "apply rule in forward manner (improper)" #> |
|
527 |
setup (Binding.name "this") (Scan.succeed (K this)) "apply current facts as rules" #> |
|
528 |
setup (Binding.name "fact") (Attrib.thms >> fact) "composition by facts from context" #> |
|
529 |
setup (Binding.name "assumption") (Scan.succeed assumption) |
|
530 |
"proof by assumption, preferring facts" #> |
|
531 |
setup (Binding.name "rename_tac") (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name) >> |
|
532 |
(fn (quant, xs) => K (SIMPLE_METHOD'' quant (Tactic.rename_tac xs)))) |
|
533 |
"rename parameters of goal" #> |
|
534 |
setup (Binding.name "rotate_tac") (Args.goal_spec -- Scan.lift (Scan.optional P.int 1) >> |
|
535 |
(fn (quant, i) => K (SIMPLE_METHOD'' quant (Tactic.rotate_tac i)))) |
|
536 |
"rotate assumptions of goal" #> |
|
537 |
setup (Binding.name "tactic") (Scan.lift (P.position Args.name) >> tactic) |
|
538 |
"ML tactic as proof method" #> |
|
539 |
setup (Binding.name "raw_tactic") (Scan.lift (P.position Args.name) >> raw_tactic) |
|
540 |
"ML tactic as raw proof method")); |
|
5824 | 541 |
|
542 |
||
16145 | 543 |
(*final declarations of this structure!*) |
544 |
val unfold = unfold_meth; |
|
545 |
val fold = fold_meth; |
|
546 |
||
5824 | 547 |
end; |
548 |
||
549 |
structure BasicMethod: BASIC_METHOD = Method; |
|
550 |
open BasicMethod; |
|
30508
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
551 |
|
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
552 |
val RAW_METHOD_CASES = Method.RAW_METHOD_CASES; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
553 |
val RAW_METHOD = Method.RAW_METHOD; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
554 |
val METHOD_CASES = Method.METHOD_CASES; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
555 |
val METHOD = Method.METHOD; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
556 |
val SIMPLE_METHOD = Method.SIMPLE_METHOD; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
557 |
val SIMPLE_METHOD' = Method.SIMPLE_METHOD'; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
558 |
val SIMPLE_METHOD'' = Method.SIMPLE_METHOD''; |
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
wenzelm
parents:
30466
diff
changeset
|
559 |