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