src/Pure/Isar/toplevel.ML
author wenzelm
Tue Feb 19 17:55:26 2013 +0100 (2013-02-19 ago)
changeset 51222 8c3e5fb41139
parent 51219 2464ba6e6fc9
child 51226 1973089f1dba
permissions -rw-r--r--
improved scheduling of forked proofs, based on elapsed time estimate (from last run via session log file);
     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 toplevel: state
    12   val is_toplevel: state -> bool
    13   val is_theory: state -> bool
    14   val is_proof: state -> bool
    15   val level: state -> int
    16   val presentation_context_of: state -> Proof.context
    17   val previous_context_of: state -> Proof.context option
    18   val context_of: state -> Proof.context
    19   val generic_theory_of: state -> generic_theory
    20   val theory_of: state -> theory
    21   val proof_of: state -> Proof.state
    22   val proof_position_of: state -> int
    23   val end_theory: Position.T -> state -> theory
    24   val print_state_context: state -> unit
    25   val print_state: bool -> state -> unit
    26   val pretty_abstract: state -> Pretty.T
    27   val quiet: bool Unsynchronized.ref
    28   val debug: bool Unsynchronized.ref
    29   val interact: bool Unsynchronized.ref
    30   val timing: bool Unsynchronized.ref
    31   val profiling: int Unsynchronized.ref
    32   val skip_proofs: bool Unsynchronized.ref
    33   val program: (unit -> 'a) -> 'a
    34   val thread: bool -> (unit -> unit) -> Thread.thread
    35   type transition
    36   val empty: transition
    37   val print_of: transition -> bool
    38   val name_of: transition -> string
    39   val pos_of: transition -> Position.T
    40   val name: string -> transition -> transition
    41   val position: Position.T -> transition -> transition
    42   val interactive: bool -> transition -> transition
    43   val set_print: bool -> transition -> transition
    44   val print: transition -> transition
    45   val no_timing: transition -> transition
    46   val init_theory: (unit -> theory) -> transition -> transition
    47   val is_init: transition -> bool
    48   val modify_init: (unit -> theory) -> transition -> transition
    49   val exit: transition -> transition
    50   val keep: (state -> unit) -> transition -> transition
    51   val keep': (bool -> state -> unit) -> transition -> transition
    52   val imperative: (unit -> unit) -> transition -> transition
    53   val ignored: Position.T -> transition
    54   val malformed: Position.T -> string -> transition
    55   val is_malformed: transition -> bool
    56   val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
    57   val theory': (bool -> theory -> theory) -> transition -> transition
    58   val theory: (theory -> theory) -> transition -> transition
    59   val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
    60   val end_local_theory: transition -> transition
    61   val open_target: (generic_theory -> local_theory) -> transition -> transition
    62   val close_target: transition -> transition
    63   val local_theory': (xstring * Position.T) option -> (bool -> local_theory -> local_theory) ->
    64     transition -> transition
    65   val local_theory: (xstring * Position.T) option -> (local_theory -> local_theory) ->
    66     transition -> transition
    67   val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
    68     transition -> transition
    69   val local_theory_to_proof': (xstring * Position.T) option ->
    70     (bool -> local_theory -> Proof.state) -> transition -> transition
    71   val local_theory_to_proof: (xstring * Position.T) option ->
    72     (local_theory -> Proof.state) -> transition -> transition
    73   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
    74   val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
    75   val forget_proof: transition -> transition
    76   val present_proof: (state -> unit) -> transition -> transition
    77   val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
    78   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    79   val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
    80   val proof: (Proof.state -> Proof.state) -> transition -> transition
    81   val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
    82   val skip_proof: (int -> int) -> transition -> transition
    83   val skip_proof_to_theory: (int -> bool) -> transition -> transition
    84   val get_id: transition -> string option
    85   val put_id: string -> transition -> transition
    86   val unknown_theory: transition -> transition
    87   val unknown_proof: transition -> transition
    88   val unknown_context: transition -> transition
    89   val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
    90   val status: transition -> Markup.T -> unit
    91   val add_hook: (transition -> state -> state -> unit) -> unit
    92   val approximative_id: transition -> Properties.T
    93   val get_timing: transition -> Timing.timing
    94   val put_timing: Timing.timing -> transition -> transition
    95   val transition: bool -> transition -> state -> (state * (exn * string) option) option
    96   val command: transition -> state -> state
    97   val proof_result: bool -> transition * transition list -> state ->
    98     (transition * state) list future * state
    99 end;
   100 
   101 structure Toplevel: TOPLEVEL =
   102 struct
   103 
   104 (** toplevel state **)
   105 
   106 exception UNDEF = Runtime.UNDEF;
   107 
   108 
   109 (* local theory wrappers *)
   110 
   111 val loc_init = Named_Target.context_cmd;
   112 val loc_exit = Local_Theory.assert_bottom true #> Local_Theory.exit_global;
   113 
   114 fun loc_begin loc (Context.Theory thy) =
   115       (Context.Theory o loc_exit, loc_init (the_default ("-", Position.none) loc) thy)
   116   | loc_begin NONE (Context.Proof lthy) =
   117       (Context.Proof o Local_Theory.restore, lthy)
   118   | loc_begin (SOME loc) (Context.Proof lthy) =
   119       (Context.Proof o Named_Target.reinit lthy, loc_init loc (loc_exit lthy));
   120 
   121 
   122 (* datatype node *)
   123 
   124 datatype node =
   125   Theory of generic_theory * Proof.context option
   126     (*theory with presentation context*) |
   127   Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
   128     (*proof node, finish, original theory*) |
   129   SkipProof of int * (generic_theory * generic_theory);
   130     (*proof depth, resulting theory, original theory*)
   131 
   132 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
   133 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
   134 
   135 fun cases_node f _ (Theory (gthy, _)) = f gthy
   136   | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
   137   | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
   138 
   139 val context_node = cases_node Context.proof_of Proof.context_of;
   140 
   141 
   142 (* datatype state *)
   143 
   144 datatype state = State of node option * node option;  (*current, previous*)
   145 
   146 val toplevel = State (NONE, NONE);
   147 
   148 fun is_toplevel (State (NONE, _)) = true
   149   | is_toplevel _ = false;
   150 
   151 fun level (State (NONE, _)) = 0
   152   | level (State (SOME (Theory _), _)) = 0
   153   | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
   154   | level (State (SOME (SkipProof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
   155 
   156 fun str_of_state (State (NONE, _)) = "at top level"
   157   | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
   158   | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
   159   | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
   160   | str_of_state (State (SOME (SkipProof _), _)) = "in skipped proof mode";
   161 
   162 
   163 (* current node *)
   164 
   165 fun node_of (State (NONE, _)) = raise UNDEF
   166   | node_of (State (SOME node, _)) = node;
   167 
   168 fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
   169 fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
   170 
   171 fun node_case f g state = cases_node f g (node_of state);
   172 
   173 fun presentation_context_of state =
   174   (case try node_of state of
   175     SOME (Theory (_, SOME ctxt)) => ctxt
   176   | SOME node => context_node node
   177   | NONE => raise UNDEF);
   178 
   179 fun previous_context_of (State (_, NONE)) = NONE
   180   | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
   181 
   182 val context_of = node_case Context.proof_of Proof.context_of;
   183 val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
   184 val theory_of = node_case Context.theory_of Proof.theory_of;
   185 val proof_of = node_case (fn _ => raise UNDEF) I;
   186 
   187 fun proof_position_of state =
   188   (case node_of state of
   189     Proof (prf, _) => Proof_Node.position prf
   190   | _ => raise UNDEF);
   191 
   192 fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = thy
   193   | end_theory pos (State (NONE, _)) = error ("Bad theory" ^ Position.here pos)
   194   | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.here pos);
   195 
   196 
   197 (* print state *)
   198 
   199 val pretty_context = Local_Theory.pretty o Context.cases (Named_Target.theory_init) I;
   200 
   201 fun print_state_context state =
   202   (case try node_of state of
   203     NONE => []
   204   | SOME (Theory (gthy, _)) => pretty_context gthy
   205   | SOME (Proof (_, (_, gthy))) => pretty_context gthy
   206   | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
   207   |> Pretty.chunks |> Pretty.writeln;
   208 
   209 fun print_state prf_only state =
   210   (case try node_of state of
   211     NONE => []
   212   | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
   213   | SOME (Proof (prf, _)) =>
   214       Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
   215   | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
   216   |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
   217 
   218 fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
   219 
   220 
   221 
   222 (** toplevel transitions **)
   223 
   224 val quiet = Unsynchronized.ref false;
   225 val debug = Runtime.debug;
   226 val interact = Unsynchronized.ref false;
   227 val timing = Unsynchronized.ref false;
   228 val profiling = Unsynchronized.ref 0;
   229 val skip_proofs = Unsynchronized.ref false;
   230 
   231 fun program body =
   232  (body
   233   |> Runtime.controlled_execution
   234   |> Runtime.toplevel_error (Output.error_msg o ML_Compiler.exn_message)) ();
   235 
   236 fun thread interrupts body =
   237   Thread.fork
   238     (((fn () => body () handle exn => if Exn.is_interrupt exn then () else reraise exn)
   239         |> Runtime.debugging
   240         |> Runtime.toplevel_error
   241           (fn exn =>
   242             Output.urgent_message ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
   243       Simple_Thread.attributes interrupts);
   244 
   245 
   246 (* node transactions -- maintaining stable checkpoints *)
   247 
   248 exception FAILURE of state * exn;
   249 
   250 local
   251 
   252 fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
   253   | reset_presentation node = node;
   254 
   255 fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
   256   | is_draft_theory _ = false;
   257 
   258 fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
   259 
   260 fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
   261   | stale_error some = some;
   262 
   263 fun map_theory f (Theory (gthy, ctxt)) =
   264       Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
   265   | map_theory _ node = node;
   266 
   267 in
   268 
   269 fun apply_transaction f g node =
   270   let
   271     val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
   272     val cont_node = reset_presentation node;
   273     val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
   274     fun state_error e nd = (State (SOME nd, SOME node), e);
   275 
   276     val (result, err) =
   277       cont_node
   278       |> Runtime.controlled_execution f
   279       |> map_theory Theory.checkpoint
   280       |> state_error NONE
   281       handle exn => state_error (SOME exn) cont_node;
   282 
   283     val (result', err') =
   284       if is_stale result then state_error (stale_error err) back_node
   285       else (result, err);
   286   in
   287     (case err' of
   288       NONE => tap g result'
   289     | SOME exn => raise FAILURE (result', exn))
   290   end;
   291 
   292 val exit_transaction =
   293   apply_transaction
   294     (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
   295       | node => node) (K ())
   296   #> (fn State (node', _) => State (NONE, node'));
   297 
   298 end;
   299 
   300 
   301 (* primitive transitions *)
   302 
   303 datatype trans =
   304   Init of unit -> theory |               (*init theory*)
   305   Exit |                                 (*formal exit of theory*)
   306   Keep of bool -> state -> unit |        (*peek at state*)
   307   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   308 
   309 local
   310 
   311 fun apply_tr _ (Init f) (State (NONE, _)) =
   312       State (SOME (Theory (Context.Theory
   313           (Theory.checkpoint (Runtime.controlled_execution f ())), NONE)), NONE)
   314   | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
   315       exit_transaction state
   316   | apply_tr int (Keep f) state =
   317       Runtime.controlled_execution (fn x => tap (f int) x) state
   318   | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
   319       apply_transaction (fn x => f int x) g state
   320   | apply_tr _ _ _ = raise UNDEF;
   321 
   322 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   323   | apply_union int (tr :: trs) state =
   324       apply_union int trs state
   325         handle Runtime.UNDEF => apply_tr int tr state
   326           | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
   327           | exn as FAILURE _ => raise exn
   328           | exn => raise FAILURE (state, exn);
   329 
   330 in
   331 
   332 fun apply_trans int trs state = (apply_union int trs state, NONE)
   333   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   334 
   335 end;
   336 
   337 
   338 (* datatype transition *)
   339 
   340 datatype transition = Transition of
   341  {name: string,              (*command name*)
   342   pos: Position.T,           (*source position*)
   343   int_only: bool,            (*interactive-only*)
   344   print: bool,               (*print result state*)
   345   no_timing: bool,           (*suppress timing*)
   346   timing: Timing.timing,     (*prescient timing information*)
   347   trans: trans list};        (*primitive transitions (union)*)
   348 
   349 fun make_transition (name, pos, int_only, print, no_timing, timing, trans) =
   350   Transition {name = name, pos = pos, int_only = int_only, print = print,
   351     no_timing = no_timing, timing = timing, trans = trans};
   352 
   353 fun map_transition f (Transition {name, pos, int_only, print, no_timing, timing, trans}) =
   354   make_transition (f (name, pos, int_only, print, no_timing, timing, trans));
   355 
   356 val empty = make_transition ("", Position.none, false, false, false, Timing.zero, []);
   357 
   358 
   359 (* diagnostics *)
   360 
   361 fun print_of (Transition {print, ...}) = print;
   362 fun name_of (Transition {name, ...}) = name;
   363 fun pos_of (Transition {pos, ...}) = pos;
   364 
   365 fun command_msg msg tr = msg ^ "command " ^ quote (name_of tr) ^ Position.here (pos_of tr);
   366 fun at_command tr = command_msg "At " tr;
   367 
   368 fun type_error tr state =
   369   ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
   370 
   371 
   372 (* modify transitions *)
   373 
   374 fun name name = map_transition (fn (_, pos, int_only, print, no_timing, timing, trans) =>
   375   (name, pos, int_only, print, no_timing, timing, trans));
   376 
   377 fun position pos = map_transition (fn (name, _, int_only, print, no_timing, timing, trans) =>
   378   (name, pos, int_only, print, no_timing, timing, trans));
   379 
   380 fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, timing, trans) =>
   381   (name, pos, int_only, print, no_timing, timing, trans));
   382 
   383 val no_timing = map_transition (fn (name, pos, int_only, print, _, timing, trans) =>
   384   (name, pos, int_only, print, true, timing, trans));
   385 
   386 fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, timing, trans) =>
   387   (name, pos, int_only, print, no_timing, timing, tr :: trans));
   388 
   389 val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, timing, _) =>
   390   (name, pos, int_only, print, no_timing, timing, []));
   391 
   392 fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, timing, trans) =>
   393   (name, pos, int_only, print, no_timing, timing, trans));
   394 
   395 val print = set_print true;
   396 
   397 
   398 (* basic transitions *)
   399 
   400 fun init_theory f = add_trans (Init f);
   401 
   402 fun is_init (Transition {trans = [Init _], ...}) = true
   403   | is_init _ = false;
   404 
   405 fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
   406 
   407 val exit = add_trans Exit;
   408 val keep' = add_trans o Keep;
   409 
   410 fun present_transaction f g = add_trans (Transaction (f, g));
   411 fun transaction f = present_transaction f (K ());
   412 
   413 fun keep f = add_trans (Keep (fn _ => f));
   414 fun imperative f = keep (fn _ => f ());
   415 
   416 fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
   417 
   418 val malformed_name = "<malformed>";
   419 fun malformed pos msg =
   420   empty |> name malformed_name |> position pos |> imperative (fn () => error msg);
   421 fun is_malformed tr = name_of tr = malformed_name;
   422 
   423 val unknown_theory = imperative (fn () => warning "Unknown theory context");
   424 val unknown_proof = imperative (fn () => warning "Unknown proof context");
   425 val unknown_context = imperative (fn () => warning "Unknown context");
   426 
   427 
   428 (* theory transitions *)
   429 
   430 fun generic_theory f = transaction (fn _ =>
   431   (fn Theory (gthy, _) => Theory (f gthy, NONE)
   432     | _ => raise UNDEF));
   433 
   434 fun theory' f = transaction (fn int =>
   435   (fn Theory (Context.Theory thy, _) =>
   436       let val thy' = thy
   437         |> Sign.new_group
   438         |> Theory.checkpoint
   439         |> f int
   440         |> Sign.reset_group;
   441       in Theory (Context.Theory thy', NONE) end
   442     | _ => raise UNDEF));
   443 
   444 fun theory f = theory' (K f);
   445 
   446 fun begin_local_theory begin f = transaction (fn _ =>
   447   (fn Theory (Context.Theory thy, _) =>
   448         let
   449           val lthy = f thy;
   450           val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
   451         in Theory (gthy, SOME lthy) end
   452     | _ => raise UNDEF));
   453 
   454 val end_local_theory = transaction (fn _ =>
   455   (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
   456     | _ => raise UNDEF));
   457 
   458 fun open_target f = transaction (fn _ =>
   459   (fn Theory (gthy, _) =>
   460         let val lthy = f gthy
   461         in Theory (Context.Proof lthy, SOME lthy) end
   462     | _ => raise UNDEF));
   463 
   464 val close_target = transaction (fn _ =>
   465   (fn Theory (Context.Proof lthy, _) =>
   466         (case try Local_Theory.close_target lthy of
   467           SOME ctxt' =>
   468             let
   469               val gthy' =
   470                 if can Local_Theory.assert ctxt'
   471                 then Context.Proof ctxt'
   472                 else Context.Theory (Proof_Context.theory_of ctxt');
   473             in Theory (gthy', SOME lthy) end
   474         | NONE => raise UNDEF)
   475     | _ => raise UNDEF));
   476 
   477 
   478 local
   479 
   480 fun local_theory_presentation loc f = present_transaction (fn int =>
   481   (fn Theory (gthy, _) =>
   482         let
   483           val (finish, lthy) = loc_begin loc gthy;
   484           val lthy' = lthy
   485             |> Local_Theory.new_group
   486             |> f int
   487             |> Local_Theory.reset_group;
   488         in Theory (finish lthy', SOME lthy') end
   489     | _ => raise UNDEF));
   490 
   491 in
   492 
   493 fun local_theory' loc f = local_theory_presentation loc f (K ());
   494 fun local_theory loc f = local_theory' loc (K f);
   495 fun present_local_theory loc = local_theory_presentation loc (K I);
   496 
   497 end;
   498 
   499 
   500 (* proof transitions *)
   501 
   502 fun end_proof f = transaction (fn int =>
   503   (fn Proof (prf, (finish, _)) =>
   504         let val state = Proof_Node.current prf in
   505           if can (Proof.assert_bottom true) state then
   506             let
   507               val ctxt' = f int state;
   508               val gthy' = finish ctxt';
   509             in Theory (gthy', SOME ctxt') end
   510           else raise UNDEF
   511         end
   512     | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
   513     | _ => raise UNDEF));
   514 
   515 local
   516 
   517 fun begin_proof init = transaction (fn int =>
   518   (fn Theory (gthy, _) =>
   519     let
   520       val (finish, prf) = init int gthy;
   521       val skip = ! skip_proofs;
   522       val (is_goal, no_skip) =
   523         (true, Proof.schematic_goal prf) handle ERROR _ => (false, true);
   524       val _ =
   525         if is_goal andalso skip andalso no_skip then
   526           warning "Cannot skip proof of schematic goal statement"
   527         else ();
   528     in
   529       if skip andalso not no_skip then
   530         SkipProof (0, (finish (Proof.global_skip_proof int prf), gthy))
   531       else Proof (Proof_Node.init prf, (finish, gthy))
   532     end
   533   | _ => raise UNDEF));
   534 
   535 in
   536 
   537 fun local_theory_to_proof' loc f = begin_proof
   538   (fn int => fn gthy =>
   539     let val (finish, lthy) = loc_begin loc gthy
   540     in (finish o Local_Theory.reset_group, f int (Local_Theory.new_group lthy)) end);
   541 
   542 fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
   543 
   544 fun theory_to_proof f = begin_proof
   545   (fn _ => fn gthy =>
   546     (Context.Theory o Sign.reset_group o Proof_Context.theory_of,
   547       (case gthy of
   548         Context.Theory thy => f (Theory.checkpoint (Sign.new_group thy))
   549       | _ => raise UNDEF)));
   550 
   551 end;
   552 
   553 val forget_proof = transaction (fn _ =>
   554   (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   555     | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   556     | _ => raise UNDEF));
   557 
   558 val present_proof = present_transaction (fn _ =>
   559   (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
   560     | skip as SkipProof _ => skip
   561     | _ => raise UNDEF));
   562 
   563 fun proofs' f = transaction (fn int =>
   564   (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
   565     | skip as SkipProof _ => skip
   566     | _ => raise UNDEF));
   567 
   568 fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
   569 val proofs = proofs' o K;
   570 val proof = proof' o K;
   571 
   572 fun actual_proof f = transaction (fn _ =>
   573   (fn Proof (prf, x) => Proof (f prf, x)
   574     | _ => raise UNDEF));
   575 
   576 fun skip_proof f = transaction (fn _ =>
   577   (fn SkipProof (h, x) => SkipProof (f h, x)
   578     | _ => raise UNDEF));
   579 
   580 fun skip_proof_to_theory pred = transaction (fn _ =>
   581   (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
   582     | _ => raise UNDEF));
   583 
   584 
   585 
   586 (** toplevel transactions **)
   587 
   588 (* identification *)
   589 
   590 fun get_id (Transition {pos, ...}) = Position.get_id pos;
   591 fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
   592 
   593 
   594 (* thread position *)
   595 
   596 fun setmp_thread_position (Transition {pos, ...}) f x =
   597   Position.setmp_thread_data pos f x;
   598 
   599 fun status tr m =
   600   setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
   601 
   602 
   603 (* post-transition hooks *)
   604 
   605 local
   606   val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
   607 in
   608 
   609 fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
   610 fun get_hooks () = ! hooks;
   611 
   612 end;
   613 
   614 
   615 (* apply transitions *)
   616 
   617 fun approximative_id tr =
   618   (Markup.nameN, name_of tr) :: Position.properties_of (pos_of tr);
   619 
   620 fun get_timing (Transition {timing, ...}) = timing;
   621 fun put_timing timing = map_transition (fn (name, pos, int_only, print, no_timing, _, trans) =>
   622   (name, pos, int_only, print, no_timing, timing, trans));
   623 
   624 local
   625 
   626 fun timing_message tr t =
   627   if Timing.is_relevant t andalso not (Position.is_reported (pos_of tr)) then
   628     Output.protocol_message
   629       (Markup.command_timing :: approximative_id tr @ Markup.timing_properties t) ""
   630     handle Fail _ => ()
   631   else ();
   632 
   633 fun app int (tr as Transition {trans, print, no_timing, ...}) =
   634   setmp_thread_position tr (fn state =>
   635     let
   636       fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   637       fun do_profiling f x = profile (! profiling) f x;
   638 
   639       val start = Timing.start ();
   640 
   641       val (result, status) =
   642          state |>
   643           (apply_trans int trans
   644             |> (! profiling > 0 andalso not no_timing) ? do_profiling
   645             |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
   646 
   647       val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
   648 
   649       val _ = timing_message tr (Timing.result start);
   650     in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
   651 
   652 in
   653 
   654 fun transition int tr st =
   655   let
   656     val hooks = get_hooks ();
   657     fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
   658 
   659     val ctxt = try context_of st;
   660     val res =
   661       (case app int tr st of
   662         (_, SOME Runtime.TERMINATE) => NONE
   663       | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
   664       | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
   665       | (st', NONE) => SOME (st', NONE));
   666     val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
   667   in res end;
   668 
   669 end;
   670 
   671 
   672 (* nested commands *)
   673 
   674 fun command tr st =
   675   (case transition (! interact) tr st of
   676     SOME (st', NONE) => st'
   677   | SOME (_, SOME (exn, info)) =>
   678       if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
   679   | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
   680 
   681 fun command_result tr st =
   682   let val st' = command tr st
   683   in ((tr, st'), st') end;
   684 
   685 
   686 (* scheduled proof result *)
   687 
   688 structure Result = Proof_Data
   689 (
   690   type T = (transition * state) list future;
   691   val empty: T = Future.value [];
   692   fun init _ = empty;
   693 );
   694 
   695 fun proof_result immediate (tr, proof_trs) st =
   696   let val st' = command tr st in
   697     if immediate orelse null proof_trs orelse not (can proof_of st')
   698     then
   699       let val (results, st'') = fold_map command_result proof_trs st'
   700       in (Future.value ((tr, st') :: results), st'') end
   701     else
   702       let
   703         val (body_trs, end_tr) = split_last proof_trs;
   704         val finish = Context.Theory o Proof_Context.theory_of;
   705 
   706         val timing_estimate = fold (Timing.add o get_timing) proof_trs Timing.zero;
   707         val timing_order =
   708           Real.floor (Real.max (Math.log10 (Time.toReal (#elapsed timing_estimate)), ~3.0));
   709         val pri = Int.min (timing_order - 3, ~1);
   710 
   711         val future_proof = Proof.global_future_proof
   712           (fn prf =>
   713             Goal.fork_name "Toplevel.future_proof" pri
   714               (fn () =>
   715                 let val (result, result_state) =
   716                   (case st' of State (SOME (Proof (_, (_, orig_gthy))), prev)
   717                     => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy))), prev))
   718                   |> fold_map command_result body_trs ||> command end_tr;
   719                 in (result, presentation_context_of result_state) end))
   720           #-> Result.put;
   721 
   722         val st'' = st'
   723           |> command (tr |> set_print false |> reset_trans |> end_proof (K future_proof));
   724         val result =
   725           Result.get (presentation_context_of st'')
   726           |> Future.map (fn body => (tr, st') :: body @ [(end_tr, st'')]);
   727 
   728       in (result, st'') end
   729   end;
   730 
   731 end;