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