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