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