src/Pure/Isar/toplevel.ML
author wenzelm
Tue Jan 09 18:18:21 2018 +0100 (18 months ago ago)
changeset 67391 d55e52e25d9a
parent 67390 a256051dd3d6
child 67392 1256460c063a
permissions -rw-r--r--
clarified signature;
     1 (*  Title:      Pure/Isar/toplevel.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Isabelle/Isar toplevel transactions.
     5 *)
     6 
     7 signature TOPLEVEL =
     8 sig
     9   exception UNDEF
    10   type state
    11   val theory_toplevel: theory -> state
    12   val toplevel: state
    13   val is_toplevel: state -> bool
    14   val is_theory: state -> bool
    15   val is_proof: state -> bool
    16   val is_skipped_proof: state -> bool
    17   val level: state -> int
    18   val previous_theory_of: state -> theory option
    19   val context_of: state -> Proof.context
    20   val generic_theory_of: state -> generic_theory
    21   val theory_of: state -> theory
    22   val proof_of: state -> Proof.state
    23   val proof_position_of: state -> int
    24   val end_theory: Position.T -> state -> theory
    25   val presentation_context: state -> Proof.context
    26   val presentation_state: Proof.context -> state
    27   val pretty_context: state -> Pretty.T list
    28   val pretty_state: state -> Pretty.T list
    29   val string_of_state: state -> string
    30   val pretty_abstract: state -> Pretty.T
    31   type transition
    32   val empty: transition
    33   val name_of: transition -> string
    34   val pos_of: transition -> Position.T
    35   val type_error: transition -> string
    36   val name: string -> transition -> transition
    37   val position: Position.T -> transition -> transition
    38   val init_theory: (unit -> theory) -> transition -> transition
    39   val is_init: transition -> bool
    40   val modify_init: (unit -> theory) -> transition -> transition
    41   val exit: transition -> transition
    42   val keep: (state -> unit) -> transition -> transition
    43   val keep': (bool -> state -> unit) -> transition -> transition
    44   val keep_proof: (state -> unit) -> transition -> transition
    45   val ignored: Position.T -> transition
    46   val is_ignored: transition -> bool
    47   val malformed: Position.T -> string -> transition
    48   val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
    49   val theory': (bool -> theory -> theory) -> transition -> transition
    50   val theory: (theory -> theory) -> transition -> transition
    51   val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
    52   val end_local_theory: transition -> transition
    53   val open_target: (generic_theory -> local_theory) -> transition -> transition
    54   val close_target: transition -> transition
    55   val local_theory': (bool * Position.T) option -> (xstring * Position.T) option ->
    56     (bool -> local_theory -> local_theory) -> transition -> transition
    57   val local_theory: (bool * Position.T) option -> (xstring * Position.T) option ->
    58     (local_theory -> local_theory) -> transition -> transition
    59   val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
    60     transition -> transition
    61   val local_theory_to_proof': (bool * Position.T) option -> (xstring * Position.T) option ->
    62     (bool -> local_theory -> Proof.state) -> transition -> transition
    63   val local_theory_to_proof: (bool * Position.T) option -> (xstring * Position.T) option ->
    64     (local_theory -> Proof.state) -> transition -> transition
    65   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
    66   val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
    67   val forget_proof: bool -> transition -> transition
    68   val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
    69   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    70   val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
    71   val proof: (Proof.state -> Proof.state) -> transition -> transition
    72   val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
    73   val skip_proof: (unit -> unit) -> transition -> transition
    74   val skip_proof_open: transition -> transition
    75   val skip_proof_close: transition -> transition
    76   val exec_id: Document_ID.exec -> transition -> transition
    77   val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
    78   val add_hook: (transition -> state -> state -> unit) -> unit
    79   val get_timing: transition -> Time.time
    80   val put_timing: Time.time -> transition -> transition
    81   val transition: bool -> transition -> state -> state * (exn * string) option
    82   val command_errors: bool -> transition -> state -> Runtime.error list * state option
    83   val command_exception: bool -> transition -> state -> state
    84   val reset_theory: state -> state option
    85   val reset_proof: state -> state option
    86   type result
    87   val join_results: result -> (transition * state) list
    88   val element_result: Keyword.keywords -> transition Thy_Syntax.element -> state -> result * state
    89 end;
    90 
    91 structure Toplevel: TOPLEVEL =
    92 struct
    93 
    94 (** toplevel state **)
    95 
    96 exception UNDEF = Runtime.UNDEF;
    97 
    98 
    99 (* datatype node *)
   100 
   101 datatype node =
   102   Theory of generic_theory * Proof.context option
   103     (*theory with presentation context*) |
   104   Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
   105     (*proof node, finish, original theory*) |
   106   Skipped_Proof of int * (generic_theory * generic_theory);
   107     (*proof depth, resulting theory, original theory*)
   108 
   109 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   110 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   111 val skipped_proof_node = fn Skipped_Proof _ => true | _ => false;
   112 
   113 fun cases_node f _ (Theory (gthy, _)) = f gthy
   114   | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
   115   | cases_node f _ (Skipped_Proof (_, (gthy, _))) = f gthy;
   116 
   117 val context_node = cases_node Context.proof_of Proof.context_of;
   118 
   119 
   120 (* datatype state *)
   121 
   122 datatype state = State of node option * node option;  (*current, previous*)
   123 
   124 fun theory_toplevel thy = State (SOME (Theory (Context.Theory thy, NONE)), NONE);
   125 
   126 val toplevel = State (NONE, NONE);
   127 
   128 fun is_toplevel (State (NONE, _)) = true
   129   | is_toplevel _ = false;
   130 
   131 fun level (State (NONE, _)) = 0
   132   | level (State (SOME (Theory _), _)) = 0
   133   | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
   134   | level (State (SOME (Skipped_Proof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
   135 
   136 fun str_of_state (State (NONE, SOME (Theory (Context.Theory thy, _)))) =
   137       "at top level, result theory " ^ quote (Context.theory_name thy)
   138   | str_of_state (State (NONE, _)) = "at top level"
   139   | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
   140   | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
   141   | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
   142   | str_of_state (State (SOME (Skipped_Proof _), _)) = "in skipped proof mode";
   143 
   144 
   145 (* current node *)
   146 
   147 fun node_of (State (NONE, _)) = raise UNDEF
   148   | node_of (State (SOME node, _)) = node;
   149 
   150 fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
   151 fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
   152 fun is_skipped_proof state = not (is_toplevel state) andalso skipped_proof_node (node_of state);
   153 
   154 fun node_case f g state = cases_node f g (node_of state);
   155 
   156 fun previous_theory_of (State (_, NONE)) = NONE
   157   | previous_theory_of (State (_, SOME prev)) =
   158       SOME (cases_node Context.theory_of Proof.theory_of prev);
   159 
   160 val context_of = node_case Context.proof_of Proof.context_of;
   161 val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
   162 val theory_of = node_case Context.theory_of Proof.theory_of;
   163 val proof_of = node_case (fn _ => error "No proof state") I;
   164 
   165 fun proof_position_of state =
   166   (case node_of state of
   167     Proof (prf, _) => Proof_Node.position prf
   168   | _ => ~1);
   169 
   170 fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = thy
   171   | end_theory pos (State (NONE, _)) = error ("Bad theory" ^ Position.here pos)
   172   | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.here pos);
   173 
   174 
   175 (* presentation context *)
   176 
   177 structure Presentation_State = Proof_Data
   178 (
   179   type T = state option;
   180   fun init _ = NONE;
   181 );
   182 
   183 fun presentation_context state =
   184   (case try node_of state of
   185     SOME (Theory (_, SOME ctxt)) => ctxt
   186   | SOME node => context_node node
   187   | NONE =>
   188       (case try Theory.get_pure () of
   189         SOME thy => Proof_Context.init_global thy
   190       | NONE => raise UNDEF))
   191   |> Presentation_State.put (SOME state);
   192 
   193 fun presentation_state ctxt =
   194   (case Presentation_State.get ctxt of
   195     NONE => State (SOME (Theory (Context.Proof ctxt, SOME ctxt)), NONE)
   196   | SOME state => state);
   197 
   198 
   199 (* print state *)
   200 
   201 fun pretty_context state =
   202   (case try node_of state of
   203     NONE => []
   204   | SOME node =>
   205       let
   206         val gthy =
   207           (case node of
   208             Theory (gthy, _) => gthy
   209           | Proof (_, (_, gthy)) => gthy
   210           | Skipped_Proof (_, (_, gthy)) => gthy);
   211         val lthy = Context.cases (Named_Target.theory_init) I gthy;
   212       in Local_Theory.pretty lthy end);
   213 
   214 fun pretty_state state =
   215   (case try node_of state of
   216     NONE => []
   217   | SOME (Theory _) => []
   218   | SOME (Proof (prf, _)) => Proof.pretty_state (Proof_Node.current prf)
   219   | SOME (Skipped_Proof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)]);
   220 
   221 val string_of_state = pretty_state #> Pretty.chunks #> Pretty.string_of;
   222 
   223 fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
   224 
   225 val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty_abstract);
   226 
   227 
   228 
   229 (** toplevel transitions **)
   230 
   231 (* node transactions -- maintaining stable checkpoints *)
   232 
   233 exception FAILURE of state * exn;
   234 
   235 local
   236 
   237 fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
   238   | reset_presentation node = node;
   239 
   240 in
   241 
   242 fun apply_transaction f g node =
   243   let
   244     val cont_node = reset_presentation node;
   245     val context = cases_node I (Context.Proof o Proof.context_of) cont_node;
   246     fun state_error e nd = (State (SOME nd, SOME node), e);
   247 
   248     val (result, err) =
   249       cont_node
   250       |> Runtime.controlled_execution (SOME context) f
   251       |> state_error NONE
   252       handle exn => state_error (SOME exn) cont_node;
   253   in
   254     (case err of
   255       NONE => tap g result
   256     | SOME exn => raise FAILURE (result, exn))
   257   end;
   258 
   259 val exit_transaction =
   260   apply_transaction
   261     (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
   262       | node => node) (K ())
   263   #> (fn State (node', _) => State (NONE, node'));
   264 
   265 end;
   266 
   267 
   268 (* primitive transitions *)
   269 
   270 datatype trans =
   271   Init of unit -> theory |               (*init theory*)
   272   Exit |                                 (*formal exit of theory*)
   273   Keep of bool -> state -> unit |        (*peek at state*)
   274   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   275 
   276 local
   277 
   278 fun apply_tr _ (Init f) (State (NONE, _)) =
   279       State (SOME (Theory (Context.Theory (Runtime.controlled_execution NONE f ()), NONE)), NONE)
   280   | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
   281       exit_transaction state
   282   | apply_tr int (Keep f) state =
   283       Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state
   284   | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
   285       apply_transaction (fn x => f int x) g state
   286   | apply_tr _ _ _ = raise UNDEF;
   287 
   288 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   289   | apply_union int (tr :: trs) state =
   290       apply_union int trs state
   291         handle Runtime.UNDEF => apply_tr int tr state
   292           | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
   293           | exn as FAILURE _ => raise exn
   294           | exn => raise FAILURE (state, exn);
   295 
   296 in
   297 
   298 fun apply_trans int trs state = (apply_union int trs state, NONE)
   299   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   300 
   301 end;
   302 
   303 
   304 (* datatype transition *)
   305 
   306 datatype transition = Transition of
   307  {name: string,              (*command name*)
   308   pos: Position.T,           (*source position*)
   309   timing: Time.time,         (*prescient timing information*)
   310   trans: trans list};        (*primitive transitions (union)*)
   311 
   312 fun make_transition (name, pos, timing, trans) =
   313   Transition {name = name, pos = pos, timing = timing, trans = trans};
   314 
   315 fun map_transition f (Transition {name, pos, timing, trans}) =
   316   make_transition (f (name, pos, timing, trans));
   317 
   318 val empty = make_transition ("", Position.none, Time.zeroTime, []);
   319 
   320 
   321 (* diagnostics *)
   322 
   323 fun name_of (Transition {name, ...}) = name;
   324 fun pos_of (Transition {pos, ...}) = pos;
   325 
   326 fun command_msg msg tr =
   327   msg ^ "command " ^ quote (Markup.markup Markup.keyword1 (name_of tr)) ^
   328     Position.here (pos_of tr);
   329 
   330 fun at_command tr = command_msg "At " tr;
   331 fun type_error tr = command_msg "Bad context for " tr;
   332 
   333 
   334 (* modify transitions *)
   335 
   336 fun name name = map_transition (fn (_, pos, timing, trans) =>
   337   (name, pos, timing, trans));
   338 
   339 fun position pos = map_transition (fn (name, _, timing, trans) =>
   340   (name, pos, timing, trans));
   341 
   342 fun add_trans tr = map_transition (fn (name, pos, timing, trans) =>
   343   (name, pos, timing, tr :: trans));
   344 
   345 val reset_trans = map_transition (fn (name, pos, timing, _) =>
   346   (name, pos, timing, []));
   347 
   348 
   349 (* basic transitions *)
   350 
   351 fun init_theory f = add_trans (Init f);
   352 
   353 fun is_init (Transition {trans = [Init _], ...}) = true
   354   | is_init _ = false;
   355 
   356 fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
   357 
   358 val exit = add_trans Exit;
   359 val keep' = add_trans o Keep;
   360 
   361 fun present_transaction f g = add_trans (Transaction (f, g));
   362 fun transaction f = present_transaction f (K ());
   363 
   364 fun keep f = add_trans (Keep (fn _ => f));
   365 
   366 fun keep_proof f =
   367   keep (fn st =>
   368     if is_proof st then f st
   369     else if is_skipped_proof st then ()
   370     else warning "No proof state");
   371 
   372 fun ignored pos = empty |> name "<ignored>" |> position pos |> keep (fn _ => ());
   373 fun is_ignored tr = name_of tr = "<ignored>";
   374 
   375 fun malformed pos msg =
   376   empty |> name "<malformed>" |> position pos |> keep (fn _ => error msg);
   377 
   378 
   379 (* theory transitions *)
   380 
   381 fun generic_theory f = transaction (fn _ =>
   382   (fn Theory (gthy, _) => Theory (f gthy, NONE)
   383     | _ => raise UNDEF));
   384 
   385 fun theory' f = transaction (fn int =>
   386   (fn Theory (Context.Theory thy, _) =>
   387       let val thy' = thy
   388         |> Sign.new_group
   389         |> f int
   390         |> Sign.reset_group;
   391       in Theory (Context.Theory thy', NONE) end
   392     | _ => raise UNDEF));
   393 
   394 fun theory f = theory' (K f);
   395 
   396 fun begin_local_theory begin f = transaction (fn _ =>
   397   (fn Theory (Context.Theory thy, _) =>
   398         let
   399           val lthy = f thy;
   400           val gthy = if begin then Context.Proof lthy else Context.Theory (Named_Target.exit lthy);
   401           val _ =
   402             (case Local_Theory.pretty lthy of
   403               [] => ()
   404             | prts => Output.state (Pretty.string_of (Pretty.chunks prts)));
   405         in Theory (gthy, SOME lthy) end
   406     | _ => raise UNDEF));
   407 
   408 val end_local_theory = transaction (fn _ =>
   409   (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (Named_Target.exit lthy), SOME lthy)
   410     | _ => raise UNDEF));
   411 
   412 fun open_target f = transaction (fn _ =>
   413   (fn Theory (gthy, _) =>
   414         let val lthy = f gthy
   415         in Theory (Context.Proof lthy, SOME lthy) end
   416     | _ => raise UNDEF));
   417 
   418 val close_target = transaction (fn _ =>
   419   (fn Theory (Context.Proof lthy, _) =>
   420         (case try Local_Theory.close_target lthy of
   421           SOME ctxt' =>
   422             let
   423               val gthy' =
   424                 if can Local_Theory.assert ctxt'
   425                 then Context.Proof ctxt'
   426                 else Context.Theory (Proof_Context.theory_of ctxt');
   427             in Theory (gthy', SOME lthy) end
   428         | NONE => raise UNDEF)
   429     | _ => raise UNDEF));
   430 
   431 fun restricted_context (SOME (strict, scope)) =
   432       Proof_Context.map_naming (Name_Space.restricted strict scope)
   433   | restricted_context NONE = I;
   434 
   435 fun local_theory' restricted target f = present_transaction (fn int =>
   436   (fn Theory (gthy, _) =>
   437         let
   438           val (finish, lthy) = Named_Target.switch target gthy;
   439           val lthy' = lthy
   440             |> restricted_context restricted
   441             |> Local_Theory.new_group
   442             |> f int
   443             |> Local_Theory.reset_group;
   444         in Theory (finish lthy', SOME lthy') end
   445     | _ => raise UNDEF))
   446   (K ());
   447 
   448 fun local_theory restricted target f = local_theory' restricted target (K f);
   449 
   450 fun present_local_theory target = present_transaction (fn _ =>
   451   (fn Theory (gthy, _) =>
   452         let val (finish, lthy) = Named_Target.switch target gthy;
   453         in Theory (finish lthy, SOME lthy) end
   454     | _ => raise UNDEF));
   455 
   456 
   457 (* proof transitions *)
   458 
   459 fun end_proof f = transaction (fn int =>
   460   (fn Proof (prf, (finish, _)) =>
   461         let val state = Proof_Node.current prf in
   462           if can (Proof.assert_bottom true) state then
   463             let
   464               val ctxt' = f int state;
   465               val gthy' = finish ctxt';
   466             in Theory (gthy', SOME ctxt') end
   467           else raise UNDEF
   468         end
   469     | Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
   470     | _ => raise UNDEF));
   471 
   472 local
   473 
   474 fun begin_proof init = transaction (fn int =>
   475   (fn Theory (gthy, _) =>
   476     let
   477       val (finish, prf) = init int gthy;
   478       val document = Options.default_string "document";
   479       val skip = (document = "" orelse document = "false") andalso Goal.skip_proofs_enabled ();
   480       val schematic_goal = try Proof.schematic_goal prf;
   481       val _ =
   482         if skip andalso schematic_goal = SOME true then
   483           warning "Cannot skip proof of schematic goal statement"
   484         else ();
   485     in
   486       if skip andalso schematic_goal = SOME false then
   487         Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
   488       else Proof (Proof_Node.init prf, (finish, gthy))
   489     end
   490   | _ => raise UNDEF));
   491 
   492 in
   493 
   494 fun local_theory_to_proof' restricted target f = begin_proof
   495   (fn int => fn gthy =>
   496     let
   497       val (finish, lthy) = Named_Target.switch target gthy;
   498       val prf = lthy
   499         |> restricted_context restricted
   500         |> Local_Theory.new_group
   501         |> f int;
   502     in (finish o Local_Theory.reset_group, prf) end);
   503 
   504 fun local_theory_to_proof restricted target f =
   505   local_theory_to_proof' restricted target (K f);
   506 
   507 fun theory_to_proof f = begin_proof
   508   (fn _ => fn gthy =>
   509     (Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of,
   510       (case gthy of
   511         Context.Theory thy => f (Sign.new_group thy)
   512       | _ => raise UNDEF)));
   513 
   514 end;
   515 
   516 fun forget_proof strict = transaction (fn _ =>
   517   (fn Proof (prf, (_, orig_gthy)) =>
   518         if strict andalso not (Proof.has_bottom_goal (Proof_Node.current prf))
   519         then raise UNDEF else Theory (orig_gthy, NONE)
   520     | Skipped_Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   521     | _ => raise UNDEF));
   522 
   523 fun proofs' f = transaction (fn int =>
   524   (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
   525     | skip as Skipped_Proof _ => skip
   526     | _ => raise UNDEF));
   527 
   528 fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
   529 val proofs = proofs' o K;
   530 val proof = proof' o K;
   531 
   532 
   533 (* skipped proofs *)
   534 
   535 fun actual_proof f = transaction (fn _ =>
   536   (fn Proof (prf, x) => Proof (f prf, x)
   537     | _ => raise UNDEF));
   538 
   539 fun skip_proof f = transaction (fn _ =>
   540   (fn skip as Skipped_Proof _ => (f (); skip)
   541     | _ => raise UNDEF));
   542 
   543 val skip_proof_open = transaction (fn _ =>
   544   (fn Skipped_Proof (d, x) => Skipped_Proof (d + 1, x)
   545     | _ => raise UNDEF));
   546 
   547 val skip_proof_close = transaction (fn _ =>
   548   (fn Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
   549     | Skipped_Proof (d, x) => Skipped_Proof (d - 1, x)
   550     | _ => raise UNDEF));
   551 
   552 
   553 
   554 (** toplevel transactions **)
   555 
   556 (* runtime position *)
   557 
   558 fun exec_id id (tr as Transition {pos, ...}) =
   559   position (Position.put_id (Document_ID.print id) pos) tr;
   560 
   561 fun setmp_thread_position (Transition {pos, ...}) f x =
   562   Position.setmp_thread_data pos f x;
   563 
   564 
   565 (* post-transition hooks *)
   566 
   567 local
   568   val hooks =
   569     Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list);
   570 in
   571 
   572 fun add_hook hook = Synchronized.change hooks (cons hook);
   573 fun get_hooks () = Synchronized.value hooks;
   574 
   575 end;
   576 
   577 
   578 (* apply transitions *)
   579 
   580 fun get_timing (Transition {timing, ...}) = timing;
   581 fun put_timing timing = map_transition (fn (name, pos, _, trans) => (name, pos, timing, trans));
   582 
   583 local
   584 
   585 fun app int (tr as Transition {trans, ...}) =
   586   setmp_thread_position tr (fn state =>
   587     let
   588       val timing_start = Timing.start ();
   589       val (result, opt_err) = apply_trans int trans state;
   590       val timing_result = Timing.result timing_start;
   591       val timing_props =
   592         Markup.command_timing :: (Markup.nameN, name_of tr) :: Position.properties_of (pos_of tr);
   593       val _ = Timing.protocol_message timing_props timing_result;
   594     in (result, Option.map (fn UNDEF => ERROR (type_error tr) | exn => exn) opt_err) end);
   595 
   596 in
   597 
   598 fun transition int tr st =
   599   let
   600     val (st', opt_err) =
   601       Context.setmp_generic_context (try (Context.Proof o presentation_context) st)
   602         (fn () => app int tr st) ();
   603     val opt_err' = opt_err |> Option.map
   604       (fn Runtime.EXCURSION_FAIL exn_info => exn_info
   605         | exn => (Runtime.exn_context (try context_of st) exn, at_command tr));
   606     val _ = get_hooks () |> List.app (fn f => (try (fn () => f tr st st') (); ()));
   607   in (st', opt_err') end;
   608 
   609 end;
   610 
   611 
   612 (* managed commands *)
   613 
   614 fun command_errors int tr st =
   615   (case transition int tr st of
   616     (st', NONE) => ([], SOME st')
   617   | (_, SOME (exn, _)) => (Runtime.exn_messages exn, NONE));
   618 
   619 fun command_exception int tr st =
   620   (case transition int tr st of
   621     (st', NONE) => st'
   622   | (_, SOME (exn, info)) =>
   623       if Exn.is_interrupt exn then Exn.reraise exn
   624       else raise Runtime.EXCURSION_FAIL (exn, info));
   625 
   626 val command = command_exception false;
   627 
   628 
   629 (* reset state *)
   630 
   631 local
   632 
   633 fun reset_state check trans st =
   634   if check st then NONE
   635   else #2 (command_errors false (trans empty) st);
   636 
   637 in
   638 
   639 val reset_theory = reset_state is_theory (forget_proof false);
   640 
   641 val reset_proof =
   642   reset_state is_proof
   643     (transaction (fn _ =>
   644       (fn Theory (gthy, _) => Skipped_Proof (0, (gthy, gthy))
   645         | _ => raise UNDEF)));
   646 
   647 end;
   648 
   649 
   650 (* scheduled proof result *)
   651 
   652 datatype result =
   653   Result of transition * state |
   654   Result_List of result list |
   655   Result_Future of result future;
   656 
   657 fun join_results (Result x) = [x]
   658   | join_results (Result_List xs) = maps join_results xs
   659   | join_results (Result_Future x) = join_results (Future.join x);
   660 
   661 local
   662 
   663 structure Result = Proof_Data
   664 (
   665   type T = result;
   666   fun init _ = Result_List [];
   667 );
   668 
   669 val get_result = Result.get o Proof.context_of;
   670 val put_result = Proof.map_context o Result.put;
   671 
   672 fun timing_estimate elem =
   673   let val trs = tl (Thy_Syntax.flat_element elem)
   674   in fold (fn tr => fn t => get_timing tr + t) trs Time.zeroTime end;
   675 
   676 fun proof_future_enabled estimate st =
   677   (case try proof_of st of
   678     NONE => false
   679   | SOME state =>
   680       not (Proof.is_relevant state) andalso
   681        (if can (Proof.assert_bottom true) state
   682         then Goal.future_enabled 1
   683         else Goal.future_enabled 2 orelse Goal.future_enabled_timing estimate));
   684 
   685 fun atom_result keywords tr st =
   686   let
   687     val st' =
   688       if Goal.future_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then
   689         (Execution.fork
   690           {name = "Toplevel.diag", pos = pos_of tr, pri = ~1}
   691           (fn () => command tr st); st)
   692       else command tr st;
   693   in (Result (tr, st'), st') end;
   694 
   695 in
   696 
   697 fun element_result keywords (Thy_Syntax.Element (tr, NONE)) st = atom_result keywords tr st
   698   | element_result keywords (elem as Thy_Syntax.Element (head_tr, SOME element_rest)) st =
   699       let
   700         val (head_result, st') = atom_result keywords head_tr st;
   701         val (body_elems, end_tr) = element_rest;
   702         val estimate = timing_estimate elem;
   703       in
   704         if not (proof_future_enabled estimate st')
   705         then
   706           let
   707             val proof_trs = maps Thy_Syntax.flat_element body_elems @ [end_tr];
   708             val (proof_results, st'') = fold_map (atom_result keywords) proof_trs st';
   709           in (Result_List (head_result :: proof_results), st'') end
   710         else
   711           let
   712             val finish = Context.Theory o Proof_Context.theory_of;
   713 
   714             val future_proof =
   715               Proof.future_proof (fn state =>
   716                 Execution.fork
   717                   {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = ~1}
   718                   (fn () =>
   719                     let
   720                       val State (SOME (Proof (prf, (_, orig_gthy))), prev) = st';
   721                       val prf' = Proof_Node.apply (K state) prf;
   722                       val (result, result_state) =
   723                         State (SOME (Proof (prf', (finish, orig_gthy))), prev)
   724                         |> fold_map (element_result keywords) body_elems ||> command end_tr;
   725                     in (Result_List result, presentation_context result_state) end))
   726               #> (fn (res, state') => state' |> put_result (Result_Future res));
   727 
   728             val forked_proof =
   729               proof (future_proof #>
   730                 (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
   731               end_proof (fn _ => future_proof #>
   732                 (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
   733 
   734             val st'' = st'
   735               |> command (head_tr |> reset_trans |> forked_proof);
   736             val end_result = Result (end_tr, st'');
   737             val result =
   738               Result_List [head_result, Result.get (presentation_context st''), end_result];
   739           in (result, st'') end
   740       end;
   741 
   742 end;
   743 
   744 end;