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