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