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