src/Pure/Isar/toplevel.ML
changeset 32792 a08a2b962a09
parent 32738 15bb09ca0378
child 33223 d27956b4d3b4
equal deleted inserted replaced
32791:e6d47ce70d27 32792:a08a2b962a09
   124   Proof of ProofNode.T * ((Proof.context -> generic_theory) * generic_theory)
   124   Proof of ProofNode.T * ((Proof.context -> generic_theory) * generic_theory)
   125     (*proof node, finish, original theory*) |
   125     (*proof node, finish, original theory*) |
   126   SkipProof of int * (generic_theory * generic_theory);
   126   SkipProof of int * (generic_theory * generic_theory);
   127     (*proof depth, resulting theory, original theory*)
   127     (*proof depth, resulting theory, original theory*)
   128 
   128 
   129 val the_global_theory = fn Theory (Context.Theory thy, _) => thy | _ => raise UNDEF;
       
   130 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   129 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   131 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   130 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   132 
   131 
   133 fun cases_node f _ (Theory (gthy, _)) = f gthy
   132 fun cases_node f _ (Theory (gthy, _)) = f gthy
   134   | cases_node _ g (Proof (prf, _)) = g (ProofNode.current prf)
   133   | cases_node _ g (Proof (prf, _)) = g (ProofNode.current prf)
   254       Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, ctxt)
   253       Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, ctxt)
   255   | map_theory _ node = node;
   254   | map_theory _ node = node;
   256 
   255 
   257 in
   256 in
   258 
   257 
   259 fun apply_transaction pos f g (node, exit) =
   258 fun apply_transaction f g (node, exit) =
   260   let
   259   let
   261     val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
   260     val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
   262     val cont_node = reset_presentation node;
   261     val cont_node = reset_presentation node;
   263     val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
   262     val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
   264     fun state_error e nd = (State (SOME (nd, exit), SOME (node, exit)), e);
   263     fun state_error e nd = (State (SOME (nd, exit), SOME (node, exit)), e);
   291   Keep of bool -> state -> unit |                (*peek at state*)
   290   Keep of bool -> state -> unit |                (*peek at state*)
   292   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   291   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   293 
   292 
   294 local
   293 local
   295 
   294 
   296 fun apply_tr int _ (Init (_, f, exit)) (State (NONE, _)) =
   295 fun apply_tr int (Init (_, f, exit)) (State (NONE, _)) =
   297       State (SOME (Theory (Context.Theory (Theory.checkpoint (f int)), NONE), exit), NONE)
   296       State (SOME (Theory (Context.Theory (Theory.checkpoint (f int)), NONE), exit), NONE)
   298   | apply_tr _ _ Exit (State (prev as SOME (Theory (Context.Theory _, _), _), _)) =
   297   | apply_tr _ Exit (State (prev as SOME (Theory (Context.Theory _, _), _), _)) =
   299       State (NONE, prev)
   298       State (NONE, prev)
   300   | apply_tr _ _ CommitExit (State (NONE, SOME (Theory (Context.Theory thy, _), exit))) =
   299   | apply_tr _ CommitExit (State (NONE, SOME (Theory (Context.Theory thy, _), exit))) =
   301       (Runtime.controlled_execution exit thy; toplevel)
   300       (Runtime.controlled_execution exit thy; toplevel)
   302   | apply_tr int _ (Keep f) state =
   301   | apply_tr int (Keep f) state =
   303       Runtime.controlled_execution (fn x => tap (f int) x) state
   302       Runtime.controlled_execution (fn x => tap (f int) x) state
   304   | apply_tr int pos (Transaction (f, g)) (State (SOME state, _)) =
   303   | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
   305       apply_transaction pos (fn x => f int x) g state
   304       apply_transaction (fn x => f int x) g state
   306   | apply_tr _ _ _ _ = raise UNDEF;
   305   | apply_tr _ _ _ = raise UNDEF;
   307 
   306 
   308 fun apply_union _ _ [] state = raise FAILURE (state, UNDEF)
   307 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   309   | apply_union int pos (tr :: trs) state =
   308   | apply_union int (tr :: trs) state =
   310       apply_union int pos trs state
   309       apply_union int trs state
   311         handle Runtime.UNDEF => apply_tr int pos tr state
   310         handle Runtime.UNDEF => apply_tr int tr state
   312           | FAILURE (alt_state, UNDEF) => apply_tr int pos tr alt_state
   311           | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
   313           | exn as FAILURE _ => raise exn
   312           | exn as FAILURE _ => raise exn
   314           | exn => raise FAILURE (state, exn);
   313           | exn => raise FAILURE (state, exn);
   315 
   314 
   316 in
   315 in
   317 
   316 
   318 fun apply_trans int pos trs state = (apply_union int pos trs state, NONE)
   317 fun apply_trans int trs state = (apply_union int trs state, NONE)
   319   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   318   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   320 
   319 
   321 end;
   320 end;
   322 
   321 
   323 
   322 
   560 
   559 
   561 (* apply transitions *)
   560 (* apply transitions *)
   562 
   561 
   563 local
   562 local
   564 
   563 
   565 fun app int (tr as Transition {trans, pos, print, no_timing, ...}) =
   564 fun app int (tr as Transition {trans, print, no_timing, ...}) =
   566   setmp_thread_position tr (fn state =>
   565   setmp_thread_position tr (fn state =>
   567     let
   566     let
   568       fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   567       fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   569       fun do_profiling f x = profile (! profiling) f x;
   568       fun do_profiling f x = profile (! profiling) f x;
   570 
   569 
   571       val (result, status) =
   570       val (result, status) =
   572          state |> (apply_trans int pos trans
   571          state |> (apply_trans int trans
   573           |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
   572           |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
   574           |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
   573           |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
   575 
   574 
   576       val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
   575       val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
   577     in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
   576     in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);