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