5824
|
1 |
(* Title: Pure/Isar/method.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Proof methods.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature BASIC_METHOD =
|
|
9 |
sig
|
|
10 |
val print_methods: theory -> unit
|
|
11 |
val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
|
|
12 |
end;
|
|
13 |
|
|
14 |
signature METHOD =
|
|
15 |
sig
|
|
16 |
include BASIC_METHOD
|
6091
|
17 |
val METHOD: (thm list -> tactic) -> Proof.method
|
5824
|
18 |
val METHOD0: tactic -> Proof.method
|
|
19 |
val fail: Proof.method
|
|
20 |
val succeed: Proof.method
|
7419
|
21 |
val insert_tac: thm list -> int -> tactic
|
7574
|
22 |
val insert: thm list -> Proof.method
|
7555
|
23 |
val insert_facts: Proof.method
|
7419
|
24 |
val fold: thm list -> Proof.method
|
|
25 |
val unfold: thm list -> Proof.method
|
|
26 |
val multi_resolve: thm list -> thm -> thm Seq.seq
|
|
27 |
val multi_resolves: thm list -> thm list -> thm Seq.seq
|
6091
|
28 |
val rule_tac: thm list -> thm list -> int -> tactic
|
7130
|
29 |
val erule_tac: thm list -> thm list -> int -> tactic
|
6091
|
30 |
val rule: thm list -> Proof.method
|
7130
|
31 |
val erule: thm list -> Proof.method
|
7555
|
32 |
val assumption: Proof.context -> Proof.method
|
5916
|
33 |
exception METHOD_FAIL of (string * Position.T) * exn
|
7419
|
34 |
val help_methods: theory -> unit
|
5824
|
35 |
val method: theory -> Args.src -> Proof.context -> Proof.method
|
|
36 |
val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
|
|
37 |
-> theory -> theory
|
5884
|
38 |
val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
|
|
39 |
Proof.context -> Args.src -> Proof.context * 'a
|
7555
|
40 |
val ctxt_args: (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
|
5884
|
41 |
val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
|
7268
|
42 |
type modifier
|
|
43 |
val sectioned_args: ((Args.T list -> modifier * Args.T list) list ->
|
5884
|
44 |
Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
|
7268
|
45 |
(Args.T list -> modifier * Args.T list) list ->
|
5884
|
46 |
('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
|
7555
|
47 |
val bang_sectioned_args: (Args.T list -> modifier * Args.T list) list ->
|
|
48 |
(thm list -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
|
7268
|
49 |
val only_sectioned_args: (Args.T list -> modifier * Args.T list) list ->
|
5884
|
50 |
(Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
|
6091
|
51 |
val thms_args: (thm list -> Proof.method) -> Args.src -> Proof.context -> Proof.method
|
5824
|
52 |
datatype text =
|
|
53 |
Basic of (Proof.context -> Proof.method) |
|
|
54 |
Source of Args.src |
|
|
55 |
Then of text list |
|
|
56 |
Orelse of text list |
|
|
57 |
Try of text |
|
|
58 |
Repeat1 of text
|
|
59 |
val refine: text -> Proof.state -> Proof.state Seq.seq
|
7506
|
60 |
val refine_no_facts: text -> Proof.state -> Proof.state Seq.seq
|
5824
|
61 |
val proof: text option -> Proof.state -> Proof.state Seq.seq
|
6981
|
62 |
val local_qed: text option
|
|
63 |
-> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
|
6736
|
64 |
-> Proof.state -> Proof.state Seq.seq
|
6981
|
65 |
val local_terminal_proof: text * text option
|
|
66 |
-> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
|
6736
|
67 |
-> Proof.state -> Proof.state Seq.seq
|
6981
|
68 |
val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
|
|
69 |
-> Proof.state -> Proof.state Seq.seq
|
|
70 |
val local_default_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
|
6736
|
71 |
-> Proof.state -> Proof.state Seq.seq
|
6951
|
72 |
val global_qed: text option -> Proof.state -> theory * {kind: string, name: string, thm: thm}
|
6934
|
73 |
val global_terminal_proof: text * text option
|
|
74 |
-> Proof.state -> theory * {kind: string, name: string, thm: thm}
|
6532
|
75 |
val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
|
|
76 |
val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
|
5824
|
77 |
val setup: (theory -> theory) list
|
|
78 |
end;
|
|
79 |
|
|
80 |
structure Method: METHOD =
|
|
81 |
struct
|
|
82 |
|
|
83 |
|
|
84 |
(** proof methods **)
|
|
85 |
|
|
86 |
(* method from tactic *)
|
|
87 |
|
6849
|
88 |
val METHOD = Proof.method;
|
5824
|
89 |
fun METHOD0 tac = METHOD (fn [] => tac | _ => error "Method may not be used with facts");
|
|
90 |
|
|
91 |
|
|
92 |
(* primitive *)
|
|
93 |
|
|
94 |
val fail = METHOD (K no_tac);
|
|
95 |
val succeed = METHOD (K all_tac);
|
|
96 |
|
|
97 |
|
7419
|
98 |
(* insert *)
|
|
99 |
|
|
100 |
local
|
5824
|
101 |
|
6981
|
102 |
fun cut_rule_tac raw_rule =
|
|
103 |
let
|
|
104 |
val rule = Drule.forall_intr_vars raw_rule;
|
|
105 |
val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
|
7555
|
106 |
in Tactic.rtac (rule COMP revcut_rl) end;
|
6981
|
107 |
|
7419
|
108 |
in
|
5824
|
109 |
|
7419
|
110 |
fun insert_tac [] i = all_tac
|
|
111 |
| insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
|
6981
|
112 |
|
7555
|
113 |
val insert_facts = METHOD (ALLGOALS o insert_tac);
|
7574
|
114 |
fun insert thms = METHOD (fn facts => ALLGOALS (insert_tac (thms @ facts)));
|
7419
|
115 |
|
|
116 |
end;
|
5824
|
117 |
|
|
118 |
|
6532
|
119 |
(* fold / unfold definitions *)
|
|
120 |
|
7555
|
121 |
fun fold thms = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN fold_goals_tac thms);
|
|
122 |
fun unfold thms = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN rewrite_goals_tac thms);
|
6532
|
123 |
|
|
124 |
|
7419
|
125 |
(* multi_resolve *)
|
|
126 |
|
|
127 |
local
|
|
128 |
|
|
129 |
fun res th i rule =
|
|
130 |
Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
|
|
131 |
|
|
132 |
fun multi_res _ [] rule = Seq.single rule
|
|
133 |
| multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
|
|
134 |
|
|
135 |
in
|
|
136 |
|
|
137 |
val multi_resolve = multi_res 1;
|
|
138 |
fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
|
|
139 |
|
|
140 |
end;
|
|
141 |
|
|
142 |
|
5824
|
143 |
(* rule *)
|
|
144 |
|
7419
|
145 |
local
|
5824
|
146 |
|
7130
|
147 |
fun gen_rule_tac tac rules [] = tac rules
|
|
148 |
| gen_rule_tac tac erules facts =
|
5824
|
149 |
let
|
7419
|
150 |
val rules = multi_resolves facts erules;
|
7130
|
151 |
fun tactic i state = Seq.flat (Seq.map (fn rule => tac [rule] i state) rules);
|
|
152 |
in tactic end;
|
|
153 |
|
7419
|
154 |
in
|
|
155 |
|
7130
|
156 |
val rule_tac = gen_rule_tac Tactic.resolve_tac;
|
|
157 |
val erule_tac = gen_rule_tac Tactic.eresolve_tac;
|
5824
|
158 |
|
|
159 |
fun rule rules = METHOD (FIRSTGOAL o rule_tac rules);
|
7130
|
160 |
fun erule rules = METHOD (FIRSTGOAL o erule_tac rules);
|
5824
|
161 |
|
7419
|
162 |
end;
|
|
163 |
|
|
164 |
|
7555
|
165 |
(* assumption / finish *)
|
|
166 |
|
|
167 |
fun assm_tac ctxt =
|
|
168 |
assume_tac APPEND' resolve_tac (filter Thm.no_prems (ProofContext.prems_of ctxt));
|
7419
|
169 |
|
7555
|
170 |
fun assumption_tac ctxt [] = assm_tac ctxt
|
|
171 |
| assumption_tac _ [fact] = resolve_tac [fact]
|
|
172 |
| assumption_tac _ _ = K no_tac;
|
7419
|
173 |
|
7555
|
174 |
fun assumption ctxt = METHOD (FIRSTGOAL o assumption_tac ctxt);
|
|
175 |
fun finish ctxt = METHOD (K (FILTER Thm.no_prems (ALLGOALS (assm_tac ctxt))));
|
7419
|
176 |
|
|
177 |
|
5824
|
178 |
|
|
179 |
(** methods theory data **)
|
|
180 |
|
|
181 |
(* data kind 'Isar/methods' *)
|
|
182 |
|
|
183 |
structure MethodsDataArgs =
|
|
184 |
struct
|
|
185 |
val name = "Isar/methods";
|
|
186 |
type T =
|
|
187 |
{space: NameSpace.T,
|
|
188 |
meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
|
|
189 |
|
|
190 |
val empty = {space = NameSpace.empty, meths = Symtab.empty};
|
6546
|
191 |
val copy = I;
|
5824
|
192 |
val prep_ext = I;
|
|
193 |
fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
|
|
194 |
{space = NameSpace.merge (space1, space2),
|
|
195 |
meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
|
|
196 |
error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
|
|
197 |
|
7367
|
198 |
fun print_meths verbose {space, meths} =
|
5824
|
199 |
let
|
|
200 |
fun prt_meth (name, ((_, comment), _)) = Pretty.block
|
6849
|
201 |
[Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
|
5824
|
202 |
in
|
7367
|
203 |
if not verbose then ()
|
|
204 |
else Pretty.writeln (Display.pretty_name_space ("method name space", space));
|
6849
|
205 |
Pretty.writeln (Pretty.big_list "methods:"
|
|
206 |
(map prt_meth (NameSpace.cond_extern_table space meths)))
|
5824
|
207 |
end;
|
7367
|
208 |
|
|
209 |
fun print _ = print_meths true;
|
5824
|
210 |
end;
|
|
211 |
|
|
212 |
structure MethodsData = TheoryDataFun(MethodsDataArgs);
|
|
213 |
val print_methods = MethodsData.print;
|
7367
|
214 |
val help_methods = MethodsDataArgs.print_meths false o MethodsData.get;
|
5824
|
215 |
|
|
216 |
|
|
217 |
(* get methods *)
|
|
218 |
|
5916
|
219 |
exception METHOD_FAIL of (string * Position.T) * exn;
|
|
220 |
|
5824
|
221 |
fun method thy =
|
|
222 |
let
|
|
223 |
val {space, meths} = MethodsData.get thy;
|
|
224 |
|
5884
|
225 |
fun meth src =
|
|
226 |
let
|
|
227 |
val ((raw_name, _), pos) = Args.dest_src src;
|
|
228 |
val name = NameSpace.intern space raw_name;
|
|
229 |
in
|
5824
|
230 |
(case Symtab.lookup (meths, name) of
|
|
231 |
None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
|
5916
|
232 |
| Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
|
5824
|
233 |
end;
|
|
234 |
in meth end;
|
|
235 |
|
|
236 |
|
|
237 |
(* add_methods *)
|
|
238 |
|
|
239 |
fun add_methods raw_meths thy =
|
|
240 |
let
|
|
241 |
val full = Sign.full_name (Theory.sign_of thy);
|
|
242 |
val new_meths =
|
|
243 |
map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
|
|
244 |
|
|
245 |
val {space, meths} = MethodsData.get thy;
|
|
246 |
val space' = NameSpace.extend (space, map fst new_meths);
|
|
247 |
val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
|
|
248 |
error ("Duplicate declaration of method(s) " ^ commas_quote dups);
|
|
249 |
in
|
|
250 |
thy |> MethodsData.put {space = space', meths = meths'}
|
|
251 |
end;
|
|
252 |
|
|
253 |
(*implicit version*)
|
|
254 |
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
|
|
255 |
|
|
256 |
|
5884
|
257 |
|
|
258 |
(** method syntax **)
|
5824
|
259 |
|
5884
|
260 |
(* basic *)
|
|
261 |
|
|
262 |
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
|
|
263 |
Args.syntax "method" scan;
|
5824
|
264 |
|
7555
|
265 |
fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
|
|
266 |
#2 (syntax (Scan.succeed (f ctxt)) ctxt src);
|
|
267 |
|
|
268 |
fun no_args m = ctxt_args (K m);
|
5884
|
269 |
|
|
270 |
|
|
271 |
(* sections *)
|
5824
|
272 |
|
7268
|
273 |
type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
|
|
274 |
|
|
275 |
local
|
|
276 |
|
5884
|
277 |
fun sect ss = Scan.first (map (fn s => Scan.lift (s --| Args.$$$ ":")) ss);
|
|
278 |
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
|
|
279 |
fun thmss ss = Scan.repeat (thms ss) >> flat;
|
|
280 |
|
7268
|
281 |
fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
|
5824
|
282 |
|
7268
|
283 |
fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
|
|
284 |
Scan.succeed (apply m (ctxt, ths)))) >> #2;
|
5884
|
285 |
|
|
286 |
fun sectioned args ss = args ss -- Scan.repeat (section ss);
|
|
287 |
|
7268
|
288 |
in
|
5824
|
289 |
|
5884
|
290 |
fun sectioned_args args ss f src ctxt =
|
5921
|
291 |
let val (ctxt', (x, _)) = syntax (sectioned args ss) ctxt src
|
|
292 |
in f x ctxt' end;
|
5884
|
293 |
|
7555
|
294 |
fun bang_sectioned_args ss f = sectioned_args (K Args.bang_facts) ss f;
|
5884
|
295 |
fun only_sectioned_args ss f = sectioned_args (K (Scan.succeed ())) ss (fn () => f);
|
7268
|
296 |
|
5884
|
297 |
fun thms_args f = sectioned_args thmss [] (fn ths => fn _ => f ths);
|
5824
|
298 |
|
7268
|
299 |
end;
|
|
300 |
|
5824
|
301 |
|
|
302 |
|
|
303 |
(** method text **)
|
|
304 |
|
|
305 |
(* datatype text *)
|
|
306 |
|
|
307 |
datatype text =
|
|
308 |
Basic of (Proof.context -> Proof.method) |
|
|
309 |
Source of Args.src |
|
|
310 |
Then of text list |
|
|
311 |
Orelse of text list |
|
|
312 |
Try of text |
|
|
313 |
Repeat1 of text;
|
|
314 |
|
|
315 |
|
|
316 |
(* refine *)
|
|
317 |
|
|
318 |
fun refine text state =
|
|
319 |
let
|
|
320 |
val thy = Proof.theory_of state;
|
|
321 |
|
|
322 |
fun eval (Basic mth) = Proof.refine mth
|
|
323 |
| eval (Source src) = Proof.refine (method thy src)
|
|
324 |
| eval (Then txts) = Seq.EVERY (map eval txts)
|
|
325 |
| eval (Orelse txts) = Seq.FIRST (map eval txts)
|
|
326 |
| eval (Try txt) = Seq.TRY (eval txt)
|
|
327 |
| eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
|
|
328 |
in eval text state end;
|
|
329 |
|
7506
|
330 |
fun refine_no_facts text state =
|
|
331 |
state
|
|
332 |
|> Proof.goal_facts (K [])
|
|
333 |
|> refine text;
|
6404
|
334 |
|
5824
|
335 |
|
6404
|
336 |
(* structured proof steps *)
|
5824
|
337 |
|
7506
|
338 |
val default_text = Source (Args.src (("default", []), Position.none));
|
7555
|
339 |
|
|
340 |
fun finish_text None = Basic finish
|
|
341 |
| finish_text (Some txt) = Then [txt, Basic finish];
|
6872
|
342 |
|
5824
|
343 |
fun proof opt_text state =
|
|
344 |
state
|
|
345 |
|> Proof.assert_backward
|
6404
|
346 |
|> refine (if_none opt_text default_text)
|
5824
|
347 |
|> Seq.map Proof.enter_forward;
|
|
348 |
|
7439
|
349 |
fun local_qed opt_text = Proof.local_qed (refine (finish_text opt_text));
|
6934
|
350 |
fun local_terminal_proof (text, opt_text) pr = Seq.THEN (proof (Some text), local_qed opt_text pr);
|
7555
|
351 |
val local_immediate_proof = local_terminal_proof (Basic assumption, None);
|
6934
|
352 |
val local_default_proof = local_terminal_proof (default_text, None);
|
5824
|
353 |
|
6872
|
354 |
|
7439
|
355 |
fun global_qeds opt_text = Proof.global_qed (refine (finish_text opt_text));
|
5824
|
356 |
|
6951
|
357 |
fun global_qed opt_text state =
|
6872
|
358 |
state
|
6951
|
359 |
|> global_qeds opt_text
|
6872
|
360 |
|> Proof.check_result "Failed to finish proof" state
|
|
361 |
|> Seq.hd;
|
|
362 |
|
6934
|
363 |
fun global_terminal_proof (text, opt_text) state =
|
6872
|
364 |
state
|
|
365 |
|> proof (Some text)
|
|
366 |
|> Proof.check_result "Terminal proof method failed" state
|
6951
|
367 |
|> (Seq.flat o Seq.map (global_qeds opt_text))
|
6872
|
368 |
|> Proof.check_result "Failed to finish proof (after successful terminal method)" state
|
|
369 |
|> Seq.hd;
|
|
370 |
|
7555
|
371 |
val global_immediate_proof = global_terminal_proof (Basic assumption, None);
|
6934
|
372 |
val global_default_proof = global_terminal_proof (default_text, None);
|
5824
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
(** theory setup **)
|
|
377 |
|
|
378 |
(* pure_methods *)
|
|
379 |
|
|
380 |
val pure_methods =
|
|
381 |
[("fail", no_args fail, "force failure"),
|
|
382 |
("succeed", no_args succeed, "succeed"),
|
7574
|
383 |
("-", no_args insert_facts, "do nothing, inserting current facts only"),
|
|
384 |
("insert", thms_args insert, "insert facts (improper!)"),
|
7555
|
385 |
("fold", thms_args fold, "fold definitions, ignoring facts"),
|
|
386 |
("unfold", thms_args unfold, "unfold definitions, ignoring facts"),
|
7130
|
387 |
("rule", thms_args rule, "apply some rule"),
|
7419
|
388 |
("erule", thms_args erule, "apply some erule (improper!)"),
|
7555
|
389 |
("assumption", ctxt_args assumption, "proof by assumption, preferring facts")];
|
5824
|
390 |
|
|
391 |
|
|
392 |
(* setup *)
|
|
393 |
|
|
394 |
val setup = [MethodsData.init, add_methods pure_methods];
|
|
395 |
|
|
396 |
|
|
397 |
end;
|
|
398 |
|
|
399 |
|
|
400 |
structure BasicMethod: BASIC_METHOD = Method;
|
|
401 |
open BasicMethod;
|