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