src/Pure/Isar/toplevel.ML
author wenzelm
Fri Sep 10 23:11:58 2010 +0200 (2010-09-10 ago)
changeset 39285 85728a4b5620
parent 39237 be1acdcd55dc
child 39513 fce2202892c4
permissions -rw-r--r--
avoid extra wrapping for interrupts;
     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 = Output.debugging;
   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 => priority ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
   235       Simple_Thread.attributes interrupts);
   236 
   237 
   238 (* node transactions -- maintaining stable checkpoints *)
   239 
   240 exception FAILURE of state * exn;
   241 
   242 local
   243 
   244 fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
   245   | reset_presentation node = node;
   246 
   247 fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
   248   | is_draft_theory _ = false;
   249 
   250 fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
   251 
   252 fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
   253   | stale_error some = some;
   254 
   255 fun map_theory f (Theory (gthy, ctxt)) =
   256       Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
   257   | map_theory _ node = node;
   258 
   259 in
   260 
   261 fun apply_transaction f g node =
   262   let
   263     val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
   264     val cont_node = reset_presentation node;
   265     val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
   266     fun state_error e nd = (State (SOME nd, SOME node), e);
   267 
   268     val (result, err) =
   269       cont_node
   270       |> Runtime.controlled_execution f
   271       |> map_theory Theory.checkpoint
   272       |> state_error NONE
   273       handle exn => state_error (SOME exn) cont_node;
   274 
   275     val (result', err') =
   276       if is_stale result then state_error (stale_error err) back_node
   277       else (result, err);
   278   in
   279     (case err' of
   280       NONE => tap g result'
   281     | SOME exn => raise FAILURE (result', exn))
   282   end;
   283 
   284 end;
   285 
   286 
   287 (* primitive transitions *)
   288 
   289 datatype trans =
   290   Init of string * (unit -> theory) |    (*theory name, init*)
   291   Exit |                                 (*formal exit of theory*)
   292   Keep of bool -> state -> unit |        (*peek at state*)
   293   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   294 
   295 local
   296 
   297 fun apply_tr _ (Init (_, f)) (State (NONE, _)) =
   298       State (SOME (Theory (Context.Theory
   299           (Theory.checkpoint (Runtime.controlled_execution f ())), NONE)), NONE)
   300   | apply_tr _ Exit (State (prev as SOME (Theory (Context.Theory _, _)), _)) =
   301       State (NONE, prev)
   302   | apply_tr int (Keep f) state =
   303       Runtime.controlled_execution (fn x => tap (f int) x) state
   304   | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
   305       apply_transaction (fn x => f int x) g state
   306   | apply_tr _ _ _ = raise UNDEF;
   307 
   308 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   309   | apply_union int (tr :: trs) state =
   310       apply_union int trs state
   311         handle Runtime.UNDEF => apply_tr int tr state
   312           | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
   313           | exn as FAILURE _ => raise exn
   314           | exn => raise FAILURE (state, exn);
   315 
   316 in
   317 
   318 fun apply_trans int trs state = (apply_union int trs state, NONE)
   319   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   320 
   321 end;
   322 
   323 
   324 (* datatype transition *)
   325 
   326 datatype transition = Transition of
   327  {name: string,              (*command name*)
   328   pos: Position.T,           (*source position*)
   329   int_only: bool,            (*interactive-only*)
   330   print: bool,               (*print result state*)
   331   no_timing: bool,           (*suppress timing*)
   332   trans: trans list};        (*primitive transitions (union)*)
   333 
   334 fun make_transition (name, pos, int_only, print, no_timing, trans) =
   335   Transition {name = name, pos = pos, int_only = int_only, print = print, no_timing = no_timing,
   336     trans = trans};
   337 
   338 fun map_transition f (Transition {name, pos, int_only, print, no_timing, trans}) =
   339   make_transition (f (name, pos, int_only, print, no_timing, trans));
   340 
   341 val empty = make_transition ("", Position.none, false, false, false, []);
   342 
   343 
   344 (* diagnostics *)
   345 
   346 fun init_of (Transition {trans = [Init (name, _)], ...}) = SOME name
   347   | init_of _ = NONE;
   348 
   349 fun print_of (Transition {print, ...}) = print;
   350 fun name_of (Transition {name, ...}) = name;
   351 fun pos_of (Transition {pos, ...}) = pos;
   352 fun str_of tr = quote (name_of tr) ^ Position.str_of (pos_of tr);
   353 
   354 fun command_msg msg tr = msg ^ "command " ^ str_of tr;
   355 fun at_command tr = command_msg "At " tr;
   356 
   357 fun type_error tr state =
   358   ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
   359 
   360 
   361 (* modify transitions *)
   362 
   363 fun name name = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
   364   (name, pos, int_only, print, no_timing, trans));
   365 
   366 fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
   367   (name, pos, int_only, print, no_timing, trans));
   368 
   369 fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
   370   (name, pos, int_only, print, no_timing, trans));
   371 
   372 val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
   373   (name, pos, int_only, print, true, trans));
   374 
   375 fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
   376   (name, pos, int_only, print, no_timing, tr :: trans));
   377 
   378 val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, _) =>
   379   (name, pos, int_only, print, no_timing, []));
   380 
   381 fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
   382   (name, pos, int_only, print, no_timing, trans));
   383 
   384 val print = set_print true;
   385 
   386 
   387 (* basic transitions *)
   388 
   389 fun init_theory name f = add_trans (Init (name, f));
   390 
   391 fun modify_init f tr =
   392   (case init_of tr of
   393     SOME name => init_theory name f (reset_trans tr)
   394   | NONE => tr);
   395 
   396 val exit = add_trans Exit;
   397 val keep' = add_trans o Keep;
   398 
   399 fun present_transaction f g = add_trans (Transaction (f, g));
   400 fun transaction f = present_transaction f (K ());
   401 
   402 fun keep f = add_trans (Keep (fn _ => f));
   403 fun imperative f = keep (fn _ => f ());
   404 
   405 fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
   406 fun malformed pos msg =
   407   empty |> name "<malformed>" |> position pos |> imperative (fn () => error msg);
   408 
   409 val unknown_theory = imperative (fn () => warning "Unknown theory context");
   410 val unknown_proof = imperative (fn () => warning "Unknown proof context");
   411 val unknown_context = imperative (fn () => warning "Unknown context");
   412 
   413 
   414 (* theory transitions *)
   415 
   416 fun generic_theory f = transaction (fn _ =>
   417   (fn Theory (gthy, _) => Theory (f gthy, NONE)
   418     | _ => raise UNDEF));
   419 
   420 fun theory' f = transaction (fn int =>
   421   (fn Theory (Context.Theory thy, _) =>
   422       let val thy' = thy
   423         |> Sign.new_group
   424         |> Theory.checkpoint
   425         |> f int
   426         |> Sign.reset_group;
   427       in Theory (Context.Theory thy', NONE) end
   428     | _ => raise UNDEF));
   429 
   430 fun theory f = theory' (K f);
   431 
   432 fun begin_local_theory begin f = transaction (fn _ =>
   433   (fn Theory (Context.Theory thy, _) =>
   434         let
   435           val lthy = f thy;
   436           val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
   437         in Theory (gthy, SOME lthy) end
   438     | _ => raise UNDEF));
   439 
   440 val end_local_theory = transaction (fn _ =>
   441   (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
   442     | _ => raise UNDEF));
   443 
   444 local
   445 
   446 fun local_theory_presentation loc f = present_transaction (fn int =>
   447   (fn Theory (gthy, _) =>
   448         let
   449           val finish = loc_finish loc gthy;
   450           val lthy' = loc_begin loc gthy
   451             |> Local_Theory.new_group
   452             |> f int
   453             |> Local_Theory.reset_group;
   454         in Theory (finish lthy', SOME lthy') end
   455     | _ => raise UNDEF));
   456 
   457 in
   458 
   459 fun local_theory' loc f = local_theory_presentation loc f (K ());
   460 fun local_theory loc f = local_theory' loc (K f);
   461 fun present_local_theory loc = local_theory_presentation loc (K I);
   462 
   463 end;
   464 
   465 
   466 (* proof transitions *)
   467 
   468 fun end_proof f = transaction (fn int =>
   469   (fn Proof (prf, (finish, _)) =>
   470         let val state = Proof_Node.current prf in
   471           if can (Proof.assert_bottom true) state then
   472             let
   473               val ctxt' = f int state;
   474               val gthy' = finish ctxt';
   475             in Theory (gthy', SOME ctxt') end
   476           else raise UNDEF
   477         end
   478     | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
   479     | _ => raise UNDEF));
   480 
   481 local
   482 
   483 fun begin_proof init finish = transaction (fn int =>
   484   (fn Theory (gthy, _) =>
   485     let
   486       val prf = init int gthy;
   487       val skip = ! skip_proofs;
   488       val schematic = Proof.schematic_goal prf;
   489     in
   490       if skip andalso schematic then
   491         warning "Cannot skip proof of schematic goal statement"
   492       else ();
   493       if skip andalso not schematic then
   494         SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
   495       else Proof (Proof_Node.init prf, (finish gthy, gthy))
   496     end
   497   | _ => raise UNDEF));
   498 
   499 in
   500 
   501 fun local_theory_to_proof' loc f = begin_proof
   502   (fn int => fn gthy => f int (Local_Theory.new_group (loc_begin loc gthy)))
   503   (fn gthy => loc_finish loc gthy o Local_Theory.reset_group);
   504 
   505 fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
   506 
   507 fun theory_to_proof f = begin_proof
   508   (K (fn Context.Theory thy => f (Theory.checkpoint (Sign.new_group thy)) | _ => raise UNDEF))
   509   (K (Context.Theory o Sign.reset_group o ProofContext.theory_of));
   510 
   511 end;
   512 
   513 val forget_proof = transaction (fn _ =>
   514   (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   515     | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   516     | _ => raise UNDEF));
   517 
   518 val present_proof = present_transaction (fn _ =>
   519   (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
   520     | skip as SkipProof _ => skip
   521     | _ => raise UNDEF));
   522 
   523 fun proofs' f = transaction (fn int =>
   524   (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
   525     | skip as SkipProof _ => skip
   526     | _ => raise UNDEF));
   527 
   528 fun proof' f = proofs' (Seq.single oo f);
   529 val proofs = proofs' o K;
   530 val proof = proof' o K;
   531 
   532 fun actual_proof f = transaction (fn _ =>
   533   (fn Proof (prf, x) => Proof (f prf, x)
   534     | _ => raise UNDEF));
   535 
   536 fun skip_proof f = transaction (fn _ =>
   537   (fn SkipProof (h, x) => SkipProof (f h, x)
   538     | _ => raise UNDEF));
   539 
   540 fun skip_proof_to_theory pred = transaction (fn _ =>
   541   (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
   542     | _ => raise UNDEF));
   543 
   544 
   545 
   546 (** toplevel transactions **)
   547 
   548 (* identification *)
   549 
   550 fun get_id (Transition {pos, ...}) = Position.get_id pos;
   551 fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
   552 
   553 
   554 (* thread position *)
   555 
   556 fun setmp_thread_position (Transition {pos, ...}) f x =
   557   Position.setmp_thread_data pos f x;
   558 
   559 fun status tr m =
   560   setmp_thread_position tr (fn () => Output.status (Markup.markup m "")) ();
   561 
   562 fun error_msg tr msg =
   563   setmp_thread_position tr (fn () => Output.error_msg msg) ();
   564 
   565 
   566 (* post-transition hooks *)
   567 
   568 local
   569   val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
   570 in
   571 
   572 fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
   573 fun get_hooks () = ! hooks;
   574 
   575 end;
   576 
   577 
   578 (* apply transitions *)
   579 
   580 local
   581 
   582 fun app int (tr as Transition {trans, print, no_timing, ...}) =
   583   setmp_thread_position tr (fn state =>
   584     let
   585       fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   586       fun do_profiling f x = profile (! profiling) f x;
   587 
   588       val (result, status) =
   589          state |>
   590           (apply_trans int trans
   591             |> (! profiling > 0 andalso not no_timing) ? do_profiling
   592             |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
   593 
   594       val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
   595     in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
   596 
   597 in
   598 
   599 fun transition int tr st =
   600   let
   601     val hooks = get_hooks ();
   602     fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
   603 
   604     val ctxt = try context_of st;
   605     val res =
   606       (case app int tr st of
   607         (_, SOME Runtime.TERMINATE) => NONE
   608       | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
   609       | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
   610       | (st', NONE) => SOME (st', NONE));
   611     val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
   612   in res end;
   613 
   614 end;
   615 
   616 
   617 (* nested commands *)
   618 
   619 fun command tr st =
   620   (case transition (! interact) tr st of
   621     SOME (st', NONE) => st'
   622   | SOME (_, SOME (exn, info)) =>
   623       if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
   624   | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
   625 
   626 fun command_result tr st =
   627   let val st' = command tr st
   628   in (st', st') end;
   629 
   630 
   631 (* excursion of units, consisting of commands with proof *)
   632 
   633 structure States = Proof_Data
   634 (
   635   type T = state list future option;
   636   fun init _ = NONE;
   637 );
   638 
   639 fun proof_result immediate (tr, proof_trs) st =
   640   let val st' = command tr st in
   641     if immediate orelse
   642       null proof_trs orelse
   643       Keyword.is_schematic_goal (name_of tr) orelse
   644       exists (Keyword.is_qed_global o name_of) proof_trs orelse
   645       not (can proof_of st') orelse
   646       Proof.is_relevant (proof_of st')
   647     then
   648       let val (states, st'') = fold_map command_result proof_trs st'
   649       in (Lazy.value ((tr, st') :: (proof_trs ~~ states)), st'') end
   650     else
   651       let
   652         val (body_trs, end_tr) = split_last proof_trs;
   653         val finish = Context.Theory o ProofContext.theory_of;
   654 
   655         val future_proof = Proof.global_future_proof
   656           (fn prf =>
   657             Future.fork_pri ~1 (fn () =>
   658               let val (states, result_state) =
   659                 (case st' of State (SOME (Proof (_, (_, orig_gthy))), prev)
   660                   => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy))), prev))
   661                 |> fold_map command_result body_trs
   662                 ||> command (end_tr |> set_print false);
   663               in (states, presentation_context_of result_state) end))
   664           #> (fn (states, ctxt) => States.put (SOME states) ctxt);
   665 
   666         val st'' = st' |> command (end_tr |> reset_trans |> end_proof (K future_proof));
   667 
   668         val states =
   669           (case States.get (presentation_context_of st'') of
   670             NONE => raise Fail ("No future states for " ^ name_of tr ^ Position.str_of (pos_of tr))
   671           | SOME states => states);
   672         val result = Lazy.lazy
   673           (fn () => (tr, st') :: (body_trs ~~ Future.join states) @ [(end_tr, st'')]);
   674 
   675       in (result, st'') end
   676   end;
   677 
   678 fun excursion input =
   679   let
   680     val end_pos = if null input then error "No input" else pos_of (fst (List.last input));
   681     val immediate = not (Goal.future_enabled ());
   682     val (results, end_state) = fold_map (proof_result immediate) input toplevel;
   683     val thy = end_theory end_pos end_state;
   684   in (Lazy.lazy (fn () => maps Lazy.force results), thy) end;
   685 
   686 end;