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