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