src/Pure/Isar/toplevel.ML
author wenzelm
Thu Aug 30 19:18:49 2012 +0200 (2012-08-30 ago)
changeset 49011 9c68e43502ce
parent 49010 72808e956879
child 49012 8686c36fa27d
permissions -rw-r--r--
some support for registering forked proofs within Proof.state, using its bottom context;
tuned signature;
     1 (*  Title:      Pure/Isar/toplevel.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Isabelle/Isar toplevel transactions.
     5 *)
     6 
     7 signature TOPLEVEL =
     8 sig
     9   exception UNDEF
    10   type state
    11   val toplevel: state
    12   val is_toplevel: state -> bool
    13   val is_theory: state -> bool
    14   val is_proof: state -> bool
    15   val level: state -> int
    16   val presentation_context_of: state -> Proof.context
    17   val previous_context_of: state -> Proof.context option
    18   val context_of: state -> Proof.context
    19   val generic_theory_of: state -> generic_theory
    20   val theory_of: state -> theory
    21   val proof_of: state -> Proof.state
    22   val proof_position_of: state -> int
    23   val end_theory: Position.T -> state -> theory
    24   val print_state_context: state -> unit
    25   val print_state: bool -> state -> unit
    26   val pretty_abstract: state -> Pretty.T
    27   val quiet: bool Unsynchronized.ref
    28   val debug: bool Unsynchronized.ref
    29   val interact: bool Unsynchronized.ref
    30   val timing: bool Unsynchronized.ref
    31   val profiling: int Unsynchronized.ref
    32   val skip_proofs: bool Unsynchronized.ref
    33   val program: (unit -> 'a) -> 'a
    34   val thread: bool -> (unit -> unit) -> Thread.thread
    35   type transition
    36   val empty: transition
    37   val print_of: transition -> bool
    38   val name_of: transition -> string
    39   val pos_of: transition -> Position.T
    40   val name: string -> transition -> transition
    41   val position: Position.T -> transition -> transition
    42   val interactive: bool -> transition -> transition
    43   val set_print: bool -> transition -> transition
    44   val print: transition -> transition
    45   val no_timing: transition -> transition
    46   val init_theory: (unit -> theory) -> transition -> transition
    47   val is_init: transition -> bool
    48   val modify_init: (unit -> theory) -> transition -> transition
    49   val exit: transition -> transition
    50   val keep: (state -> unit) -> transition -> transition
    51   val keep': (bool -> state -> unit) -> transition -> transition
    52   val imperative: (unit -> unit) -> transition -> transition
    53   val ignored: Position.T -> transition
    54   val malformed: Position.T -> string -> transition
    55   val is_malformed: transition -> bool
    56   val 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 ("Bad theory" ^ Position.here pos)
   192   | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.here 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 
   362 fun command_msg msg tr = msg ^ "command " ^ quote (name_of tr) ^ Position.here (pos_of tr);
   363 fun at_command tr = command_msg "At " tr;
   364 
   365 fun type_error tr state =
   366   ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
   367 
   368 
   369 (* modify transitions *)
   370 
   371 fun name name = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
   372   (name, pos, int_only, print, no_timing, trans));
   373 
   374 fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
   375   (name, pos, int_only, print, no_timing, trans));
   376 
   377 fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
   378   (name, pos, int_only, print, no_timing, trans));
   379 
   380 val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
   381   (name, pos, int_only, print, true, trans));
   382 
   383 fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
   384   (name, pos, int_only, print, no_timing, tr :: trans));
   385 
   386 val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, _) =>
   387   (name, pos, int_only, print, no_timing, []));
   388 
   389 fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
   390   (name, pos, int_only, print, no_timing, trans));
   391 
   392 val print = set_print true;
   393 
   394 
   395 (* basic transitions *)
   396 
   397 fun init_theory f = add_trans (Init f);
   398 
   399 fun is_init (Transition {trans = [Init _], ...}) = true
   400   | is_init _ = false;
   401 
   402 fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
   403 
   404 val exit = add_trans Exit;
   405 val keep' = add_trans o Keep;
   406 
   407 fun present_transaction f g = add_trans (Transaction (f, g));
   408 fun transaction f = present_transaction f (K ());
   409 
   410 fun keep f = add_trans (Keep (fn _ => f));
   411 fun imperative f = keep (fn _ => f ());
   412 
   413 fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
   414 
   415 val malformed_name = "<malformed>";
   416 fun malformed pos msg =
   417   empty |> name malformed_name |> position pos |> imperative (fn () => error msg);
   418 fun is_malformed tr = name_of tr = malformed_name;
   419 
   420 val unknown_theory = imperative (fn () => warning "Unknown theory context");
   421 val unknown_proof = imperative (fn () => warning "Unknown proof context");
   422 val unknown_context = imperative (fn () => warning "Unknown context");
   423 
   424 
   425 (* theory transitions *)
   426 
   427 val global_theory_group =
   428   Sign.new_group #>
   429   Context.theory_map Thm.begin_proofs #> Theory.checkpoint;
   430 
   431 val local_theory_group =
   432   Local_Theory.new_group #>
   433   Local_Theory.raw_theory (Context.theory_map Thm.begin_proofs #> Theory.checkpoint);
   434 
   435 fun generic_theory f = transaction (fn _ =>
   436   (fn Theory (gthy, _) => Theory (f gthy, NONE)
   437     | _ => raise UNDEF));
   438 
   439 fun theory' f = transaction (fn int =>
   440   (fn Theory (Context.Theory thy, _) =>
   441       let val thy' = thy
   442         |> global_theory_group
   443         |> f int
   444         |> Sign.reset_group;
   445       in Theory (Context.Theory thy', NONE) end
   446     | _ => raise UNDEF));
   447 
   448 fun theory f = theory' (K f);
   449 
   450 fun begin_local_theory begin f = transaction (fn _ =>
   451   (fn Theory (Context.Theory thy, _) =>
   452         let
   453           val lthy = f thy;
   454           val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
   455         in Theory (gthy, SOME lthy) end
   456     | _ => raise UNDEF));
   457 
   458 val end_local_theory = transaction (fn _ =>
   459   (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
   460     | _ => raise UNDEF));
   461 
   462 fun open_target f = transaction (fn _ =>
   463   (fn Theory (gthy, _) =>
   464         let val lthy = f gthy
   465         in Theory (Context.Proof lthy, SOME lthy) end
   466     | _ => raise UNDEF));
   467 
   468 val close_target = transaction (fn _ =>
   469   (fn Theory (Context.Proof lthy, _) =>
   470         (case try Local_Theory.close_target lthy of
   471           SOME lthy' => Theory (Context.Proof lthy', SOME lthy)
   472         | NONE => raise UNDEF)
   473     | _ => raise UNDEF));
   474 
   475 
   476 local
   477 
   478 fun local_theory_presentation loc f = present_transaction (fn int =>
   479   (fn Theory (gthy, _) =>
   480         let
   481           val (finish, lthy) = loc_begin loc gthy;
   482           val lthy' = lthy
   483             |> local_theory_group
   484             |> f int
   485             |> Local_Theory.reset_group;
   486         in Theory (finish lthy', SOME lthy') end
   487     | _ => raise UNDEF));
   488 
   489 in
   490 
   491 fun local_theory' loc f = local_theory_presentation loc f (K ());
   492 fun local_theory loc f = local_theory' loc (K f);
   493 fun present_local_theory loc = local_theory_presentation loc (K I);
   494 
   495 end;
   496 
   497 
   498 (* proof transitions *)
   499 
   500 fun end_proof f = transaction (fn int =>
   501   (fn Proof (prf, (finish, _)) =>
   502         let val state = Proof_Node.current prf in
   503           if can (Proof.assert_bottom true) state then
   504             let
   505               val ctxt' = f int state;
   506               val gthy' = finish ctxt';
   507             in Theory (gthy', SOME ctxt') end
   508           else raise UNDEF
   509         end
   510     | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
   511     | _ => raise UNDEF));
   512 
   513 local
   514 
   515 fun begin_proof init = transaction (fn int =>
   516   (fn Theory (gthy, _) =>
   517     let
   518       val (finish, prf) = init int gthy;
   519       val skip = ! skip_proofs;
   520       val (is_goal, no_skip) =
   521         (true, Proof.schematic_goal prf) handle ERROR _ => (false, true);
   522       val _ =
   523         if is_goal andalso skip andalso no_skip then
   524           warning "Cannot skip proof of schematic goal statement"
   525         else ();
   526     in
   527       if skip andalso not no_skip then
   528         SkipProof (0, (finish (Proof.global_skip_proof int prf), gthy))
   529       else Proof (Proof_Node.init prf, (finish, gthy))
   530     end
   531   | _ => raise UNDEF));
   532 
   533 in
   534 
   535 fun local_theory_to_proof' loc f = begin_proof
   536   (fn int => fn gthy =>
   537     let val (finish, lthy) = loc_begin loc gthy
   538     in (finish o Local_Theory.reset_group, f int (local_theory_group lthy)) end);
   539 
   540 fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
   541 
   542 fun theory_to_proof f = begin_proof
   543   (fn _ => fn gthy =>
   544     (Context.Theory o Sign.reset_group o Proof_Context.theory_of,
   545       (case gthy of Context.Theory thy => f (global_theory_group thy) | _ => raise UNDEF)));
   546 
   547 end;
   548 
   549 val forget_proof = transaction (fn _ =>
   550   (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   551     | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
   552     | _ => raise UNDEF));
   553 
   554 val present_proof = present_transaction (fn _ =>
   555   (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
   556     | skip as SkipProof _ => skip
   557     | _ => raise UNDEF));
   558 
   559 fun proofs' f = transaction (fn int =>
   560   (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
   561     | skip as SkipProof _ => skip
   562     | _ => raise UNDEF));
   563 
   564 fun proof' f = proofs' (Seq.single oo f);
   565 val proofs = proofs' o K;
   566 val proof = proof' o K;
   567 
   568 fun actual_proof f = transaction (fn _ =>
   569   (fn Proof (prf, x) => Proof (f prf, x)
   570     | _ => raise UNDEF));
   571 
   572 fun skip_proof f = transaction (fn _ =>
   573   (fn SkipProof (h, x) => SkipProof (f h, x)
   574     | _ => raise UNDEF));
   575 
   576 fun skip_proof_to_theory pred = transaction (fn _ =>
   577   (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
   578     | _ => raise UNDEF));
   579 
   580 
   581 
   582 (** toplevel transactions **)
   583 
   584 (* identification *)
   585 
   586 fun get_id (Transition {pos, ...}) = Position.get_id pos;
   587 fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
   588 
   589 
   590 (* thread position *)
   591 
   592 fun setmp_thread_position (Transition {pos, ...}) f x =
   593   Position.setmp_thread_data pos f x;
   594 
   595 fun status tr m =
   596   setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
   597 
   598 fun error_msg tr msg =
   599   setmp_thread_position tr (fn () => Output.error_msg' msg) ();
   600 
   601 
   602 (* post-transition hooks *)
   603 
   604 local
   605   val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
   606 in
   607 
   608 fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
   609 fun get_hooks () = ! hooks;
   610 
   611 end;
   612 
   613 
   614 (* apply transitions *)
   615 
   616 local
   617 
   618 fun app int (tr as Transition {trans, print, no_timing, ...}) =
   619   setmp_thread_position tr (fn state =>
   620     let
   621       fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   622       fun do_profiling f x = profile (! profiling) f x;
   623 
   624       val (result, status) =
   625          state |>
   626           (apply_trans int trans
   627             |> (! profiling > 0 andalso not no_timing) ? do_profiling
   628             |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
   629 
   630       val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
   631     in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
   632 
   633 in
   634 
   635 fun transition int tr st =
   636   let
   637     val hooks = get_hooks ();
   638     fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
   639 
   640     val ctxt = try context_of st;
   641     val res =
   642       (case app int tr st of
   643         (_, SOME Runtime.TERMINATE) => NONE
   644       | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
   645       | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
   646       | (st', NONE) => SOME (st', NONE));
   647     val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
   648   in res end;
   649 
   650 end;
   651 
   652 
   653 (* nested commands *)
   654 
   655 fun command tr st =
   656   (case transition (! interact) tr st of
   657     SOME (st', NONE) => st'
   658   | SOME (_, SOME (exn, info)) =>
   659       if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
   660   | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
   661 
   662 fun command_result tr st =
   663   let val st' = command tr st
   664   in ((tr, st'), st') end;
   665 
   666 
   667 (* scheduled proof result *)
   668 
   669 structure Result = Proof_Data
   670 (
   671   type T = (transition * state) list future;
   672   val empty: T = Future.value [];
   673   fun init _ = empty;
   674 );
   675 
   676 fun proof_result immediate (tr, proof_trs) st =
   677   let val st' = command tr st in
   678     if immediate orelse null proof_trs orelse not (can proof_of st')
   679     then
   680       let val (results, st'') = fold_map command_result proof_trs st'
   681       in (Future.value ((tr, st') :: results), 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 (result, 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 ||> command end_tr;
   695                 in (result, presentation_context_of result_state) end))
   696           #-> Result.put;
   697 
   698         val st'' = st'
   699           |> command (tr |> set_print false |> reset_trans |> end_proof (K future_proof));
   700         val result =
   701           Result.get (presentation_context_of st'')
   702           |> Future.map (fn body => (tr, st') :: body @ [(end_tr, st'')]);
   703 
   704       in (result, st'') end
   705   end;
   706 
   707 end;