author | wenzelm |
Thu, 08 Jul 1999 18:34:59 +0200 | |
changeset 6932 | 77c14313af51 |
parent 6896 | 4bdc3600ddae |
child 6950 | ab6d35b7283f |
permissions | -rw-r--r-- |
5820 | 1 |
(* Title: Pure/Isar/proof.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Proof states and methods. |
|
6 |
*) |
|
7 |
||
8 |
signature PROOF = |
|
9 |
sig |
|
10 |
type context |
|
11 |
type state |
|
12 |
exception STATE of string * state |
|
6871 | 13 |
val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq |
5820 | 14 |
val context_of: state -> context |
15 |
val theory_of: state -> theory |
|
16 |
val sign_of: state -> Sign.sg |
|
6091 | 17 |
val the_facts: state -> thm list |
6776 | 18 |
val the_fact: state -> thm |
6091 | 19 |
val goal_facts: (state -> thm list) -> state -> state |
5820 | 20 |
val use_facts: state -> state |
21 |
val reset_facts: state -> state |
|
6891 | 22 |
val assert_forward: state -> state |
5820 | 23 |
val assert_backward: state -> state |
24 |
val enter_forward: state -> state |
|
6529 | 25 |
val verbose: bool ref |
5820 | 26 |
val print_state: state -> unit |
6776 | 27 |
val level: state -> int |
5820 | 28 |
type method |
6848 | 29 |
val method: (thm list -> tactic) -> method |
5820 | 30 |
val refine: (context -> method) -> state -> state Seq.seq |
6932 | 31 |
val export_thm: context -> thm -> thm |
5820 | 32 |
val bind: (indexname * string) list -> state -> state |
33 |
val bind_i: (indexname * term) list -> state -> state |
|
5936 | 34 |
val match_bind: (string list * string) list -> state -> state |
35 |
val match_bind_i: (term list * term) list -> state -> state |
|
6876 | 36 |
val have_thmss: thm list -> string -> context attribute list -> |
6091 | 37 |
(thm list * context attribute list) list -> state -> state |
6876 | 38 |
val simple_have_thms: string -> thm list -> state -> state |
5820 | 39 |
val fix: (string * string option) list -> state -> state |
40 |
val fix_i: (string * typ) list -> state -> state |
|
6932 | 41 |
val assm: (int -> tactic) * (int -> tactic) -> string -> context attribute list |
42 |
-> (string * (string list * string list)) list -> state -> state |
|
43 |
val assm_i: (int -> tactic) * (int -> tactic) -> string -> context attribute list |
|
44 |
-> (term * (term list * term list)) list -> state -> state |
|
45 |
val assume: string -> context attribute list -> (string * (string list * string list)) list |
|
46 |
-> state -> state |
|
47 |
val assume_i: string -> context attribute list -> (term * (term list * term list)) list |
|
48 |
-> state -> state |
|
49 |
val presume: string -> context attribute list -> (string * (string list * string list)) list |
|
50 |
-> state -> state |
|
51 |
val presume_i: string -> context attribute list -> (term * (term list * term list)) list |
|
52 |
-> state -> state |
|
53 |
val theorem: bstring -> theory attribute list -> string * (string list * string list) |
|
54 |
-> theory -> state |
|
55 |
val theorem_i: bstring -> theory attribute list -> term * (term list * term list) |
|
56 |
-> theory -> state |
|
57 |
val lemma: bstring -> theory attribute list -> string * (string list * string list) |
|
58 |
-> theory -> state |
|
59 |
val lemma_i: bstring -> theory attribute list -> term * (term list * term list) |
|
60 |
-> theory -> state |
|
5820 | 61 |
val chain: state -> state |
6932 | 62 |
val export_chain: state -> state Seq.seq |
6091 | 63 |
val from_facts: thm list -> state -> state |
6932 | 64 |
val show: string -> context attribute list -> string * (string list * string list) |
65 |
-> state -> state |
|
66 |
val show_i: string -> context attribute list -> term * (term list * term list) |
|
67 |
-> state -> state |
|
68 |
val have: string -> context attribute list -> string * (string list * string list) |
|
69 |
-> state -> state |
|
70 |
val have_i: string -> context attribute list -> term * (term list * term list) |
|
71 |
-> state -> state |
|
6404 | 72 |
val at_bottom: state -> bool |
6731 | 73 |
val local_qed: (state -> state Seq.seq) -> ({kind: string, name: string, thm: thm} -> unit) |
74 |
-> state -> state Seq.seq |
|
6896 | 75 |
val global_qed: (state -> state Seq.seq) -> bstring option -> theory attribute list option |
76 |
-> state -> (theory * {kind: string, name: string, thm: thm}) Seq.seq |
|
77 |
val begin_block: state -> state |
|
6932 | 78 |
val end_block: state -> state Seq.seq |
6896 | 79 |
val next_block: state -> state |
5820 | 80 |
end; |
81 |
||
82 |
signature PROOF_PRIVATE = |
|
83 |
sig |
|
84 |
include PROOF |
|
85 |
val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state |
|
86 |
end; |
|
87 |
||
88 |
structure Proof: PROOF_PRIVATE = |
|
89 |
struct |
|
90 |
||
91 |
||
92 |
(** proof state **) |
|
93 |
||
94 |
type context = ProofContext.context; |
|
95 |
||
96 |
||
97 |
(* type goal *) |
|
98 |
||
99 |
datatype kind = |
|
100 |
Theorem of theory attribute list | (*top-level theorem*) |
|
101 |
Lemma of theory attribute list | (*top-level lemma*) |
|
102 |
Goal of context attribute list | (*intermediate result, solving subgoal*) |
|
103 |
Aux of context attribute list ; (*intermediate result*) |
|
104 |
||
105 |
val kind_name = |
|
6001 | 106 |
fn Theorem _ => "theorem" | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have"; |
5820 | 107 |
|
108 |
type goal = |
|
6932 | 109 |
(kind * (*result kind*) |
110 |
string * (*result name*) |
|
111 |
term) * (*result statement*) |
|
112 |
(thm list * (*use facts*) |
|
113 |
thm); (*goal: subgoals ==> statement*) |
|
5820 | 114 |
|
115 |
||
116 |
(* type mode *) |
|
117 |
||
118 |
datatype mode = Forward | ForwardChain | Backward; |
|
119 |
||
120 |
val mode_name = |
|
6798 | 121 |
enclose "`" "'" o |
122 |
(fn Forward => "state" | ForwardChain => "chain" | Backward => "prove"); |
|
5820 | 123 |
|
124 |
||
125 |
(* type node *) |
|
126 |
||
127 |
type node = |
|
128 |
{context: context, |
|
6091 | 129 |
facts: thm list option, |
5820 | 130 |
mode: mode, |
131 |
goal: goal option}; |
|
132 |
||
133 |
fun make_node (context, facts, mode, goal) = |
|
134 |
{context = context, facts = facts, mode = mode, goal = goal}: node; |
|
135 |
||
136 |
||
137 |
(* datatype state *) |
|
138 |
||
139 |
datatype state = |
|
140 |
State of |
|
141 |
node * (*current*) |
|
142 |
node list; (*parents wrt. block structure*) |
|
143 |
||
144 |
exception STATE of string * state; |
|
145 |
||
146 |
fun err_malformed name state = |
|
147 |
raise STATE (name ^ ": internal error -- malformed proof state", state); |
|
148 |
||
6871 | 149 |
fun check_result msg state sq = |
150 |
(case Seq.pull sq of |
|
151 |
None => raise STATE (msg, state) |
|
152 |
| Some s_sq => Seq.cons s_sq); |
|
153 |
||
5820 | 154 |
|
155 |
fun map_current f (State ({context, facts, mode, goal}, nodes)) = |
|
156 |
State (make_node (f (context, facts, mode, goal)), nodes); |
|
157 |
||
158 |
fun init_state thy = |
|
5875 | 159 |
State (make_node (ProofContext.init thy, None, Forward, None), []); |
5820 | 160 |
|
161 |
||
162 |
||
163 |
(** basic proof state operations **) |
|
164 |
||
165 |
(* context *) |
|
166 |
||
167 |
fun context_of (State ({context, ...}, _)) = context; |
|
168 |
val theory_of = ProofContext.theory_of o context_of; |
|
169 |
val sign_of = ProofContext.sign_of o context_of; |
|
170 |
||
171 |
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal)); |
|
172 |
||
173 |
fun map_context_result f (state as State ({context, facts, mode, goal}, nodes)) = |
|
174 |
let val (context', result) = f context |
|
175 |
in (State (make_node (context', facts, mode, goal), nodes), result) end; |
|
176 |
||
177 |
||
178 |
fun put_data kind f = map_context o ProofContext.put_data kind f; |
|
179 |
val declare_term = map_context o ProofContext.declare_term; |
|
180 |
val add_binds = map_context o ProofContext.add_binds_i; |
|
6790 | 181 |
val auto_bind_goal = map_context o ProofContext.auto_bind_goal; |
6798 | 182 |
val auto_bind_facts = map_context oo ProofContext.auto_bind_facts; |
6091 | 183 |
val put_thms = map_context o ProofContext.put_thms; |
184 |
val put_thmss = map_context o ProofContext.put_thmss; |
|
6932 | 185 |
val assumptions = ProofContext.assumptions o context_of; |
5820 | 186 |
|
187 |
||
188 |
(* facts *) |
|
189 |
||
190 |
fun the_facts (State ({facts = Some facts, ...}, _)) = facts |
|
191 |
| the_facts state = raise STATE ("No current facts available", state); |
|
192 |
||
6776 | 193 |
fun the_fact state = |
194 |
(case the_facts state of |
|
195 |
[fact] => fact |
|
196 |
| _ => raise STATE ("Single fact expected", state)); |
|
197 |
||
6848 | 198 |
fun assert_facts state = (the_facts state; state); |
6932 | 199 |
fun get_facts (State ({facts, ...}, _)) = facts; |
6848 | 200 |
|
5820 | 201 |
fun put_facts facts state = |
202 |
state |
|
203 |
|> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) |
|
6798 | 204 |
|> put_thms ("facts", if_none facts []); |
5820 | 205 |
|
206 |
val reset_facts = put_facts None; |
|
207 |
||
208 |
fun have_facts (name, facts) state = |
|
209 |
state |
|
210 |
|> put_facts (Some facts) |
|
6798 | 211 |
|> put_thms (name, facts); |
5820 | 212 |
|
213 |
fun these_facts (state, ths) = have_facts ths state; |
|
214 |
||
215 |
||
216 |
(* goal *) |
|
217 |
||
6932 | 218 |
fun find_goal i (state as State ({goal = Some goal, ...}, _)) = (context_of state, (i, goal)) |
5820 | 219 |
| find_goal i (State ({goal = None, ...}, node :: nodes)) = |
220 |
find_goal (i + 1) (State (node, nodes)) |
|
221 |
| find_goal _ (state as State (_, [])) = err_malformed "find_goal" state; |
|
222 |
||
223 |
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal)); |
|
224 |
||
225 |
fun map_goal f (State ({context, facts, mode, goal = Some goal}, nodes)) = |
|
226 |
State (make_node (context, facts, mode, Some (f goal)), nodes) |
|
227 |
| map_goal f (State (nd, node :: nodes)) = |
|
228 |
let val State (node', nodes') = map_goal f (State (node, nodes)) |
|
229 |
in State (nd, node' :: nodes') end |
|
230 |
| map_goal _ state = state; |
|
231 |
||
232 |
fun goal_facts get state = |
|
233 |
state |
|
234 |
|> map_goal (fn (result, (_, thm)) => (result, (get state, thm))); |
|
235 |
||
236 |
fun use_facts state = |
|
237 |
state |
|
238 |
|> goal_facts the_facts |
|
239 |
|> reset_facts; |
|
240 |
||
241 |
||
242 |
(* mode *) |
|
243 |
||
244 |
fun get_mode (State ({mode, ...}, _)) = mode; |
|
245 |
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal)); |
|
246 |
||
247 |
val enter_forward = put_mode Forward; |
|
248 |
val enter_forward_chain = put_mode ForwardChain; |
|
249 |
val enter_backward = put_mode Backward; |
|
250 |
||
251 |
fun assert_mode pred state = |
|
252 |
let val mode = get_mode state in |
|
253 |
if pred mode then state |
|
6731 | 254 |
else raise STATE ("Illegal application of proof command in " ^ mode_name mode ^ " mode", state) |
5820 | 255 |
end; |
256 |
||
257 |
fun is_chain state = get_mode state = ForwardChain; |
|
258 |
val assert_forward = assert_mode (equal Forward); |
|
259 |
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain); |
|
260 |
val assert_backward = assert_mode (equal Backward); |
|
261 |
||
262 |
||
263 |
(* blocks *) |
|
264 |
||
6776 | 265 |
fun level (State (_, nodes)) = length nodes; |
266 |
||
5820 | 267 |
fun open_block (State (node, nodes)) = State (node, node :: nodes); |
268 |
||
269 |
fun new_block state = |
|
270 |
state |
|
271 |
|> open_block |
|
272 |
|> put_goal None; |
|
273 |
||
6896 | 274 |
fun close_block (state as State (_, node :: nodes)) = |
275 |
State (node, nodes) |
|
276 |
|> map_context (ProofContext.transfer_used_names (context_of state)) |
|
5820 | 277 |
| close_block state = raise STATE ("Unbalanced block parentheses", state); |
278 |
||
279 |
||
280 |
||
281 |
(** print_state **) |
|
282 |
||
6529 | 283 |
val verbose = ProofContext.verbose; |
284 |
||
6756 | 285 |
fun print_facts _ None = () |
6871 | 286 |
| print_facts s (Some ths) = Pretty.writeln (Pretty.big_list (s ^ " facts:") |
6932 | 287 |
(map Display.pretty_thm_no_hyps ths)); |
6756 | 288 |
|
5820 | 289 |
fun print_state (state as State ({context, facts, mode, goal = _}, nodes)) = |
290 |
let |
|
5945
63184e276c1d
print_state: use begin_goal from Goals.current_goals_markers;
wenzelm
parents:
5936
diff
changeset
|
291 |
val ref (_, _, begin_goal) = Goals.current_goals_markers; |
5820 | 292 |
|
293 |
fun levels_up 0 = "" |
|
6932 | 294 |
| levels_up 1 = " (1 level up)" |
5820 | 295 |
| levels_up i = " (" ^ string_of_int i ^ " levels up)"; |
296 |
||
6932 | 297 |
fun print_goal (_, (i, ((kind, name, _), (goal_facts, thm)))) = |
6756 | 298 |
(print_facts "Using" (if null goal_facts then None else Some goal_facts); |
299 |
writeln (kind_name kind ^ " " ^ quote name ^ levels_up (i div 2) ^ ":"); |
|
5945
63184e276c1d
print_state: use begin_goal from Goals.current_goals_markers;
wenzelm
parents:
5936
diff
changeset
|
300 |
Locale.print_goals_marker begin_goal (! goals_limit) thm); |
6848 | 301 |
|
302 |
val ctxt_strings = ProofContext.strings_of_context context; |
|
5820 | 303 |
in |
6891 | 304 |
if ! verbose then writeln ("Nesting level: " ^ string_of_int (length nodes div 2)) else (); |
5820 | 305 |
writeln ""; |
6798 | 306 |
writeln (mode_name mode ^ " mode"); |
5993 | 307 |
writeln ""; |
6529 | 308 |
if ! verbose orelse mode = Forward then |
6848 | 309 |
(if null ctxt_strings then () else (seq writeln ctxt_strings; writeln ""); |
6756 | 310 |
print_facts "Current" facts; |
6529 | 311 |
print_goal (find_goal 0 state)) |
6756 | 312 |
else if mode = ForwardChain then print_facts "Picking" facts |
6529 | 313 |
else print_goal (find_goal 0 state) |
5820 | 314 |
end; |
315 |
||
316 |
||
317 |
||
318 |
(** proof steps **) |
|
319 |
||
320 |
(* datatype method *) |
|
321 |
||
6848 | 322 |
datatype method = Method of thm list -> tactic; |
5820 | 323 |
val method = Method; |
324 |
||
325 |
||
326 |
(* refine goal *) |
|
327 |
||
328 |
fun check_sign sg state = |
|
329 |
if Sign.subsig (sg, sign_of state) then state |
|
330 |
else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state); |
|
331 |
||
332 |
fun refine meth_fun (state as State ({context, ...}, _)) = |
|
6848 | 333 |
let |
334 |
val Method meth = meth_fun context; |
|
6932 | 335 |
val (_, (_, (result, (facts, thm)))) = find_goal 0 state; |
5820 | 336 |
|
6848 | 337 |
fun refn thm' = |
338 |
state |
|
339 |
|> check_sign (Thm.sign_of_thm thm') |
|
340 |
|> map_goal (K (result, (facts, thm'))); |
|
341 |
in Seq.map refn (meth facts thm) end; |
|
5820 | 342 |
|
343 |
||
6932 | 344 |
(* export *) |
5820 | 345 |
|
6932 | 346 |
local |
347 |
||
348 |
fun varify_frees fixes thm = |
|
5820 | 349 |
let |
350 |
fun get_free x (None, t as Free (y, _)) = if x = y then Some t else None |
|
351 |
| get_free _ (opt, _) = opt; |
|
352 |
||
353 |
fun find_free t x = foldl_aterms (get_free x) (None, t); |
|
354 |
||
6887 | 355 |
val {sign, prop, ...} = Thm.rep_thm thm; |
6932 | 356 |
val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) fixes); |
5820 | 357 |
in |
358 |
thm |
|
359 |
|> Drule.forall_intr_list frees |
|
6887 | 360 |
|> Drule.forall_elim_vars 0 |
5820 | 361 |
end; |
362 |
||
6932 | 363 |
fun most_general_varify_tfrees thm = |
6790 | 364 |
let |
365 |
val {hyps, prop, ...} = Thm.rep_thm thm; |
|
366 |
val frees = foldr Term.add_term_frees (prop :: hyps, []); |
|
367 |
val leave_tfrees = foldr Term.add_term_tfree_names (frees, []); |
|
368 |
in thm |> Thm.varifyT' leave_tfrees end; |
|
369 |
||
6932 | 370 |
fun diff_context inner None = (ProofContext.fixed_names inner, ProofContext.assumptions inner) |
371 |
| diff_context inner (Some outer) = |
|
372 |
(ProofContext.fixed_names inner \\ ProofContext.fixed_names outer, |
|
373 |
Library.drop (length (ProofContext.assumptions outer), ProofContext.assumptions inner)); |
|
374 |
||
375 |
in |
|
376 |
||
377 |
fun export fixes casms thm = |
|
378 |
thm |
|
379 |
|> Drule.implies_intr_list casms |
|
380 |
|> varify_frees fixes |
|
381 |
|> most_general_varify_tfrees; |
|
382 |
||
383 |
fun export_wrt inner opt_outer = |
|
384 |
let |
|
385 |
val (fixes, asms) = diff_context inner opt_outer; |
|
386 |
val casms = map #1 asms; |
|
387 |
val tacs = map #2 asms; |
|
388 |
in (export fixes casms, tacs) end; |
|
389 |
||
390 |
end; |
|
5820 | 391 |
|
6848 | 392 |
|
6932 | 393 |
(* export results *) |
394 |
||
395 |
fun RANGE [] _ = all_tac |
|
396 |
| RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i; |
|
397 |
||
398 |
fun export_goal raw_rule inner_state state = |
|
399 |
let |
|
400 |
val (outer, (_, (result, (facts, thm)))) = find_goal 0 state; |
|
401 |
val (exp, tacs) = export_wrt (context_of inner_state) (Some outer); |
|
402 |
val rule = exp raw_rule; |
|
403 |
val thmq = FIRSTGOAL (Tactic.rtac rule THEN' RANGE (map #1 tacs)) thm; |
|
404 |
in Seq.map (fn th => map_goal (K (result, (facts, th))) state) thmq end; |
|
405 |
||
406 |
||
407 |
fun export_thm inner thm = |
|
408 |
let val (exp, tacs) = export_wrt inner None in |
|
409 |
(case Seq.chop (2, RANGE (map #2 tacs) 1 (exp thm)) of |
|
410 |
([thm'], _) => thm' |
|
411 |
| ([], _) => raise THM ("export: failed", 0, [thm]) |
|
412 |
| _ => raise THM ("export: more than one result", 0, [thm])) |
|
413 |
end; |
|
414 |
||
415 |
||
416 |
fun export_facts inner_state opt_outer_state state = |
|
417 |
let |
|
418 |
val thms = the_facts inner_state; |
|
419 |
val (exp, tacs) = export_wrt (context_of inner_state) (apsome context_of opt_outer_state); |
|
420 |
val thmqs = map (RANGE (map #2 tacs) 1 o exp) thms; |
|
421 |
in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end; |
|
422 |
||
423 |
fun transfer_facts inner_state state = |
|
424 |
(case get_facts inner_state of |
|
425 |
None => Seq.single (reset_facts state) |
|
426 |
| Some ths => export_facts inner_state (Some state) state); |
|
427 |
||
428 |
||
429 |
(* prepare result *) |
|
430 |
||
431 |
fun prep_result state t raw_thm = |
|
5820 | 432 |
let |
433 |
val ctxt = context_of state; |
|
434 |
fun err msg = raise STATE (msg, state); |
|
435 |
||
436 |
val ngoals = Thm.nprems_of raw_thm; |
|
437 |
val _ = |
|
438 |
if ngoals = 0 then () |
|
439 |
else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!")); |
|
440 |
||
6932 | 441 |
val thm = raw_thm RS Drule.rev_triv_goal; |
442 |
val {hyps, prop, sign, maxidx, ...} = Thm.rep_thm thm; |
|
5820 | 443 |
val tsig = Sign.tsig_of sign; |
6932 | 444 |
|
445 |
val casms = map #1 (assumptions state); |
|
446 |
val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms); |
|
5820 | 447 |
in |
6932 | 448 |
if not (null bad_hyps) then |
449 |
err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) bad_hyps)) |
|
5820 | 450 |
(* FIXME else if not (Pattern.matches tsig (t, Logic.skip_flexpairs prop)) then |
451 |
err ("Proved a different theorem: " ^ Sign.string_of_term sign prop) *) |
|
6932 | 452 |
else Drule.forall_elim_vars (maxidx + 1) thm |
5820 | 453 |
end; |
454 |
||
455 |
||
456 |
(* prepare final result *) |
|
457 |
||
458 |
fun strip_flexflex thm = |
|
459 |
Seq.hd (Thm.flexflex_rule thm) handle THM _ => thm; |
|
460 |
||
461 |
fun final_result state pre_thm = |
|
462 |
let |
|
463 |
val thm = |
|
464 |
pre_thm |
|
465 |
|> strip_flexflex |
|
466 |
|> Thm.strip_shyps |
|
467 |
|> Drule.standard; |
|
468 |
||
469 |
val str_of_sort = Sign.str_of_sort (Thm.sign_of_thm thm); |
|
470 |
val xshyps = Thm.extra_shyps thm; |
|
471 |
in |
|
472 |
if not (null xshyps) then |
|
473 |
raise STATE ("Extra sort hypotheses: " ^ commas (map str_of_sort xshyps), state) |
|
474 |
else thm |
|
475 |
end; |
|
476 |
||
477 |
||
478 |
||
479 |
(*** structured proof commands ***) |
|
480 |
||
481 |
(** context **) |
|
482 |
||
483 |
(* bind *) |
|
484 |
||
485 |
fun gen_bind f x state = |
|
486 |
state |
|
487 |
|> assert_forward |
|
488 |
|> map_context (f x) |
|
489 |
|> reset_facts; |
|
490 |
||
491 |
val bind = gen_bind ProofContext.add_binds; |
|
492 |
val bind_i = gen_bind ProofContext.add_binds_i; |
|
493 |
||
494 |
val match_bind = gen_bind ProofContext.match_binds; |
|
495 |
val match_bind_i = gen_bind ProofContext.match_binds_i; |
|
496 |
||
497 |
||
6091 | 498 |
(* have_thmss *) |
5820 | 499 |
|
6876 | 500 |
fun have_thmss ths name atts ths_atts state = |
5820 | 501 |
state |
502 |
|> assert_forward |
|
6876 | 503 |
|> map_context_result (ProofContext.have_thmss ths (PureThy.default_name name) atts ths_atts) |
5820 | 504 |
|> these_facts; |
505 |
||
6876 | 506 |
fun simple_have_thms name thms = have_thmss [] name [] [(thms, [])]; |
507 |
||
5820 | 508 |
|
509 |
(* fix *) |
|
510 |
||
511 |
fun gen_fix f xs state = |
|
512 |
state |
|
513 |
|> assert_forward |
|
514 |
|> map_context (f xs) |
|
515 |
|> reset_facts; |
|
516 |
||
517 |
val fix = gen_fix ProofContext.fix; |
|
518 |
val fix_i = gen_fix ProofContext.fix_i; |
|
519 |
||
520 |
||
521 |
(* assume *) |
|
522 |
||
6932 | 523 |
fun gen_assume f tacs name atts props state = |
5820 | 524 |
state |
525 |
|> assert_forward |
|
6932 | 526 |
|> map_context_result (f tacs (PureThy.default_name name) atts props) |
6798 | 527 |
|> (fn (st, (facts, prems)) => |
528 |
(st, facts) |
|
529 |
|> these_facts |
|
530 |
|> put_thms ("prems", prems)); |
|
5820 | 531 |
|
6932 | 532 |
val assm = gen_assume ProofContext.assume; |
533 |
val assm_i = gen_assume ProofContext.assume_i; |
|
534 |
||
535 |
val assume = assm (assume_tac, K all_tac); |
|
536 |
val assume_i = assm_i (assume_tac, K all_tac); |
|
537 |
val presume = assm (K all_tac, K all_tac); |
|
538 |
val presume_i = assm_i (K all_tac, K all_tac); |
|
5820 | 539 |
|
540 |
||
541 |
||
542 |
(** goals **) |
|
543 |
||
544 |
(* forward chaining *) |
|
545 |
||
546 |
fun chain state = |
|
547 |
state |
|
548 |
|> assert_forward |
|
6848 | 549 |
|> assert_facts |
5820 | 550 |
|> enter_forward_chain; |
551 |
||
6932 | 552 |
fun export_chain state = |
553 |
state |
|
554 |
|> assert_forward |
|
555 |
|> export_facts state None |
|
556 |
|> Seq.map chain; |
|
557 |
||
5820 | 558 |
fun from_facts facts state = |
559 |
state |
|
560 |
|> put_facts (Some facts) |
|
561 |
|> chain; |
|
562 |
||
563 |
||
564 |
(* setup goals *) |
|
565 |
||
5936 | 566 |
fun setup_goal opt_block prepp kind name atts raw_propp state = |
5820 | 567 |
let |
6932 | 568 |
val (state', prop) = |
5936 | 569 |
state |
570 |
|> assert_forward_or_chain |
|
571 |
|> enter_forward |
|
572 |
|> opt_block |
|
573 |
|> map_context_result (fn c => prepp (c, raw_propp)); |
|
6932 | 574 |
val cprop = Thm.cterm_of (sign_of state') prop; |
575 |
val casms = map #1 (assumptions state'); |
|
5820 | 576 |
|
6932 | 577 |
val revcut_rl = Drule.incr_indexes_wrt [] [] (cprop :: casms) [] Drule.revcut_rl; |
578 |
fun cut_asm (casm, thm) = (Thm.assume casm COMP revcut_rl) RS thm; |
|
579 |
val goal = foldr cut_asm (casms, Drule.mk_triv_goal cprop); |
|
5820 | 580 |
in |
5936 | 581 |
state' |
6932 | 582 |
|> put_goal (Some ((kind atts, (PureThy.default_name name), prop), ([], goal))) |
6790 | 583 |
|> auto_bind_goal prop |
5820 | 584 |
|> (if is_chain state then use_facts else reset_facts) |
585 |
|> new_block |
|
586 |
|> enter_backward |
|
587 |
end; |
|
588 |
||
589 |
||
590 |
(*global goals*) |
|
591 |
fun global_goal prep kind name atts x thy = |
|
592 |
setup_goal I prep kind name atts x (init_state thy); |
|
593 |
||
5936 | 594 |
val theorem = global_goal ProofContext.bind_propp Theorem; |
595 |
val theorem_i = global_goal ProofContext.bind_propp_i Theorem; |
|
596 |
val lemma = global_goal ProofContext.bind_propp Lemma; |
|
597 |
val lemma_i = global_goal ProofContext.bind_propp_i Lemma; |
|
5820 | 598 |
|
599 |
||
600 |
(*local goals*) |
|
601 |
fun local_goal prep kind name atts x = |
|
602 |
setup_goal open_block prep kind name atts x; |
|
603 |
||
5936 | 604 |
val show = local_goal ProofContext.bind_propp Goal; |
605 |
val show_i = local_goal ProofContext.bind_propp_i Goal; |
|
606 |
val have = local_goal ProofContext.bind_propp Aux; |
|
607 |
val have_i = local_goal ProofContext.bind_propp_i Aux; |
|
5820 | 608 |
|
609 |
||
610 |
||
611 |
(** conclusions **) |
|
612 |
||
613 |
(* current goal *) |
|
614 |
||
615 |
fun current_goal (State ({goal = Some goal, ...}, _)) = goal |
|
616 |
| current_goal state = raise STATE ("No current goal!", state); |
|
617 |
||
6404 | 618 |
fun assert_current_goal true (state as State ({goal = None, ...}, _)) = |
619 |
raise STATE ("No goal in this block!", state) |
|
620 |
| assert_current_goal false (state as State ({goal = Some _, ...}, _)) = |
|
621 |
raise STATE ("Goal present in this block!", state) |
|
622 |
| assert_current_goal _ state = state; |
|
5820 | 623 |
|
624 |
fun assert_bottom true (state as State (_, _ :: _)) = |
|
625 |
raise STATE ("Not at bottom of proof!", state) |
|
626 |
| assert_bottom false (state as State (_, [])) = |
|
627 |
raise STATE ("Already at bottom of proof!", state) |
|
628 |
| assert_bottom _ state = state; |
|
629 |
||
6404 | 630 |
val at_bottom = can (assert_bottom true o close_block); |
631 |
||
6932 | 632 |
fun end_proof bot state = |
5820 | 633 |
state |
634 |
|> assert_forward |
|
635 |
|> close_block |
|
636 |
|> assert_bottom bot |
|
6871 | 637 |
|> assert_current_goal true; |
5820 | 638 |
|
639 |
||
6404 | 640 |
(* local_qed *) |
5820 | 641 |
|
6731 | 642 |
fun finish_local print_result state = |
5820 | 643 |
let |
6932 | 644 |
val ((kind, name, t), (_, raw_thm)) = current_goal state; |
645 |
val result = prep_result state t raw_thm; |
|
5820 | 646 |
val (atts, opt_solve) = |
647 |
(case kind of |
|
6932 | 648 |
Goal atts => (atts, export_goal result state) |
5820 | 649 |
| Aux atts => (atts, Seq.single) |
6932 | 650 |
| _ => err_malformed "finish_local" state); |
5820 | 651 |
in |
6731 | 652 |
print_result {kind = kind_name kind, name = name, thm = result}; |
5820 | 653 |
state |
654 |
|> close_block |
|
6871 | 655 |
|> auto_bind_facts name [t] |
6876 | 656 |
|> have_thmss [] name atts [Thm.no_attributes [result]] |
5820 | 657 |
|> opt_solve |
658 |
end; |
|
659 |
||
6731 | 660 |
fun local_qed finalize print_result state = |
6404 | 661 |
state |
6932 | 662 |
|> end_proof false |
6871 | 663 |
|> finalize |
6731 | 664 |
|> (Seq.flat o Seq.map (finish_local print_result)); |
5820 | 665 |
|
666 |
||
6404 | 667 |
(* global_qed *) |
5820 | 668 |
|
669 |
fun finish_global alt_name alt_atts state = |
|
670 |
let |
|
6932 | 671 |
val ((kind, def_name, t), (_, raw_thm)) = current_goal state; |
672 |
val result = final_result state (prep_result state t raw_thm); |
|
5820 | 673 |
|
674 |
val name = if_none alt_name def_name; |
|
675 |
val atts = |
|
676 |
(case kind of |
|
677 |
Theorem atts => if_none alt_atts atts |
|
6091 | 678 |
| Lemma atts => (if_none alt_atts atts) @ [Drule.tag_lemma] |
6932 | 679 |
| _ => err_malformed "finish_global" state); |
5820 | 680 |
|
6091 | 681 |
val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state); |
6529 | 682 |
in (thy', {kind = kind_name kind, name = name, thm = result'}) end; |
5820 | 683 |
|
6871 | 684 |
(*Note: should inspect first result only, backtracking may destroy theory*) |
6404 | 685 |
fun global_qed finalize alt_name alt_atts state = |
5820 | 686 |
state |
6932 | 687 |
|> end_proof true |
6871 | 688 |
|> finalize |
689 |
|> Seq.map (finish_global alt_name alt_atts); |
|
5820 | 690 |
|
691 |
||
6896 | 692 |
|
693 |
(** blocks **) |
|
694 |
||
695 |
(* begin_block *) |
|
696 |
||
697 |
fun begin_block state = |
|
698 |
state |
|
699 |
|> assert_forward |
|
700 |
|> new_block |
|
701 |
|> open_block; |
|
702 |
||
703 |
||
704 |
(* end_block *) |
|
705 |
||
706 |
fun end_block state = |
|
707 |
state |
|
708 |
|> assert_forward |
|
709 |
|> close_block |
|
710 |
|> assert_current_goal false |
|
711 |
|> close_block |
|
6932 | 712 |
|> transfer_facts state; |
6896 | 713 |
|
714 |
||
715 |
(* next_block *) |
|
716 |
||
717 |
fun next_block state = |
|
718 |
state |
|
719 |
|> assert_forward |
|
720 |
|> close_block |
|
6932 | 721 |
|> assert_current_goal true |
6896 | 722 |
|> new_block; |
723 |
||
724 |
||
5820 | 725 |
end; |