6 |
6 |
7 signature TOPLEVEL = |
7 signature TOPLEVEL = |
8 sig |
8 sig |
9 exception UNDEF |
9 exception UNDEF |
10 type state |
10 type state |
|
11 val init_toplevel: unit -> state |
11 val theory_toplevel: theory -> state |
12 val theory_toplevel: theory -> state |
12 val init: unit -> state |
|
13 val is_toplevel: state -> bool |
13 val is_toplevel: state -> bool |
14 val is_theory: state -> bool |
14 val is_theory: state -> bool |
15 val is_proof: state -> bool |
15 val is_proof: state -> bool |
16 val is_skipped_proof: state -> bool |
16 val is_skipped_proof: state -> bool |
17 val level: state -> int |
17 val level: state -> int |
126 |
126 |
127 (* datatype state *) |
127 (* datatype state *) |
128 |
128 |
129 type node_presentation = node * Proof.context; |
129 type node_presentation = node * Proof.context; |
130 |
130 |
131 fun node_presentation0 node = |
131 fun init_presentation () = |
132 (node, cases_proper_node Context.proof_of Proof.context_of node); |
132 Proof_Context.init_global (Theory.get_pure_bootstrap ()); |
|
133 |
|
134 fun node_presentation node = |
|
135 (node, cases_node init_presentation Context.proof_of Proof.context_of node); |
|
136 |
133 |
137 |
134 datatype state = |
138 datatype state = |
135 State of node_presentation * theory option; |
139 State of node_presentation * theory option; |
136 (*current node with presentation context, previous theory*) |
140 (*current node with presentation context, previous theory*) |
137 |
141 |
138 fun node_of (State ((node, _), _)) = node; |
142 fun node_of (State ((node, _), _)) = node; |
139 fun previous_theory_of (State (_, prev_thy)) = prev_thy; |
143 fun previous_theory_of (State (_, prev_thy)) = prev_thy; |
140 |
144 |
141 fun theory_toplevel thy = |
145 fun init_toplevel () = |
142 State (node_presentation0 (Theory (Context.Theory thy)), NONE); |
|
143 |
|
144 fun init () = |
|
145 let val thy0 = Theory.get_pure () handle Fail _ => Context.the_global_context (); |
146 let val thy0 = Theory.get_pure () handle Fail _ => Context.the_global_context (); |
146 in State ((Toplevel, Proof_Context.init_global thy0), NONE) end; |
147 in State ((Toplevel, Proof_Context.init_global thy0), NONE) end; |
|
148 |
|
149 fun theory_toplevel thy = |
|
150 State (node_presentation (Theory (Context.Theory thy)), NONE); |
147 |
151 |
148 fun level state = |
152 fun level state = |
149 (case node_of state of |
153 (case node_of state of |
150 Toplevel => 0 |
154 Toplevel => 0 |
151 | Theory _ => 0 |
155 | Theory _ => 0 |
211 presentation_context0 state |
215 presentation_context0 state |
212 |> Presentation_State.put (SOME (State (current, NONE))); |
216 |> Presentation_State.put (SOME (State (current, NONE))); |
213 |
217 |
214 fun presentation_state ctxt = |
218 fun presentation_state ctxt = |
215 (case Presentation_State.get ctxt of |
219 (case Presentation_State.get ctxt of |
216 NONE => State (node_presentation0 (Theory (Context.Proof ctxt)), NONE) |
220 NONE => State (node_presentation (Theory (Context.Proof ctxt)), NONE) |
217 | SOME state => state); |
221 | SOME state => state); |
218 |
222 |
219 |
223 |
220 (* print state *) |
224 (* print state *) |
221 |
225 |
253 |
257 |
254 exception FAILURE of state * exn; |
258 exception FAILURE of state * exn; |
255 |
259 |
256 fun apply_transaction f g node = |
260 fun apply_transaction f g node = |
257 let |
261 let |
258 val node_pr = node_presentation0 node; |
262 val node_pr = node_presentation node; |
259 val context = cases_proper_node I (Context.Proof o Proof.context_of) node; |
263 val context = cases_proper_node I (Context.Proof o Proof.context_of) node; |
260 fun state_error e node_pr' = (State (node_pr', get_theory node), e); |
264 fun state_error e node_pr' = (State (node_pr', get_theory node), e); |
261 |
265 |
262 val (result, err) = |
266 val (result, err) = |
263 node |
267 node |
285 |
289 |
286 local |
290 local |
287 |
291 |
288 fun apply_tr _ (Init f) (State ((Toplevel, _), _)) = |
292 fun apply_tr _ (Init f) (State ((Toplevel, _), _)) = |
289 let val node = Theory (Context.Theory (Runtime.controlled_execution NONE f ())) |
293 let val node = Theory (Context.Theory (Runtime.controlled_execution NONE f ())) |
290 in State (node_presentation0 node, NONE) end |
294 in State (node_presentation node, NONE) end |
291 | apply_tr _ Exit (State ((node as Theory (Context.Theory thy), _), _)) = |
295 | apply_tr _ Exit (State ((node as Theory (Context.Theory thy), _), _)) = |
292 let |
296 let |
293 val State ((node', pr_ctxt), _) = |
297 val State ((node', pr_ctxt), _) = |
294 node |> apply_transaction |
298 node |> apply_transaction |
295 (fn _ => node_presentation0 (Theory (Context.Theory (Theory.end_theory thy)))) |
299 (fn _ => node_presentation (Theory (Context.Theory (Theory.end_theory thy)))) |
296 (K ()); |
300 (K ()); |
297 in State ((Toplevel, pr_ctxt), get_theory node') end |
301 in State ((Toplevel, pr_ctxt), get_theory node') end |
298 | apply_tr int (Keep f) state = |
302 | apply_tr int (Keep f) state = |
299 Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state |
303 Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state |
300 | apply_tr _ (Transaction _) (State ((Toplevel, _), _)) = raise UNDEF |
304 | apply_tr _ (Transaction _) (State ((Toplevel, _), _)) = raise UNDEF |
379 val exit = add_trans Exit; |
383 val exit = add_trans Exit; |
380 val keep' = add_trans o Keep; |
384 val keep' = add_trans o Keep; |
381 |
385 |
382 fun present_transaction f g = add_trans (Transaction (f, g)); |
386 fun present_transaction f g = add_trans (Transaction (f, g)); |
383 fun transaction f = present_transaction f (K ()); |
387 fun transaction f = present_transaction f (K ()); |
384 fun transaction0 f = present_transaction (node_presentation0 oo f) (K ()); |
388 fun transaction0 f = present_transaction (node_presentation oo f) (K ()); |
385 |
389 |
386 fun keep f = add_trans (Keep (fn _ => f)); |
390 fun keep f = add_trans (Keep (fn _ => f)); |
387 |
391 |
388 fun keep_proof f = |
392 fun keep_proof f = |
389 keep (fn st => |
393 keep (fn st => |
399 |
403 |
400 |
404 |
401 (* theory transitions *) |
405 (* theory transitions *) |
402 |
406 |
403 fun generic_theory f = transaction (fn _ => |
407 fun generic_theory f = transaction (fn _ => |
404 (fn Theory gthy => node_presentation0 (Theory (f gthy)) |
408 (fn Theory gthy => node_presentation (Theory (f gthy)) |
405 | _ => raise UNDEF)); |
409 | _ => raise UNDEF)); |
406 |
410 |
407 fun theory' f = transaction (fn int => |
411 fun theory' f = transaction (fn int => |
408 (fn Theory (Context.Theory thy) => |
412 (fn Theory (Context.Theory thy) => |
409 let val thy' = thy |
413 let val thy' = thy |
410 |> Sign.new_group |
414 |> Sign.new_group |
411 |> f int |
415 |> f int |
412 |> Sign.reset_group; |
416 |> Sign.reset_group; |
413 in node_presentation0 (Theory (Context.Theory thy')) end |
417 in node_presentation (Theory (Context.Theory thy')) end |
414 | _ => raise UNDEF)); |
418 | _ => raise UNDEF)); |
415 |
419 |
416 fun theory f = theory' (K f); |
420 fun theory f = theory' (K f); |
417 |
421 |
418 fun begin_local_theory begin f = transaction (fn _ => |
422 fun begin_local_theory begin f = transaction (fn _ => |
486 val ctxt' = f int state; |
490 val ctxt' = f int state; |
487 val gthy' = finish ctxt'; |
491 val gthy' = finish ctxt'; |
488 in (Theory gthy', ctxt') end |
492 in (Theory gthy', ctxt') end |
489 else raise UNDEF |
493 else raise UNDEF |
490 end |
494 end |
491 | Skipped_Proof (0, (gthy, _)) => node_presentation0 (Theory gthy) |
495 | Skipped_Proof (0, (gthy, _)) => node_presentation (Theory gthy) |
492 | _ => raise UNDEF)); |
496 | _ => raise UNDEF)); |
493 |
497 |
494 local |
498 local |
495 |
499 |
496 fun begin_proof init_proof = transaction0 (fn int => |
500 fun begin_proof init_proof = transaction0 (fn int => |
739 (fn () => |
743 (fn () => |
740 let |
744 let |
741 val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st'; |
745 val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st'; |
742 val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy)); |
746 val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy)); |
743 val (result, result_state) = |
747 val (result, result_state) = |
744 State (node_presentation0 node', prev_thy) |
748 State (node_presentation node', prev_thy) |
745 |> fold_map (element_result keywords) body_elems ||> command end_tr; |
749 |> fold_map (element_result keywords) body_elems ||> command end_tr; |
746 in (Result_List result, presentation_context0 result_state) end)) |
750 in (Result_List result, presentation_context0 result_state) end)) |
747 #> (fn (res, state') => state' |> put_result (Result_Future res)); |
751 #> (fn (res, state') => state' |> put_result (Result_Future res)); |
748 |
752 |
749 val forked_proof = |
753 val forked_proof = |