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