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