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