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