src/Pure/Isar/toplevel.ML
changeset 56265 785569927666
parent 56147 9589605bcf41
child 56303 4cc3f4db3447
equal deleted inserted replaced
56260:3d79c132e657 56265:785569927666
    27   val pretty_abstract: state -> Pretty.T
    27   val pretty_abstract: state -> Pretty.T
    28   val quiet: bool Unsynchronized.ref
    28   val quiet: bool Unsynchronized.ref
    29   val interact: bool Unsynchronized.ref
    29   val interact: bool Unsynchronized.ref
    30   val timing: bool Unsynchronized.ref
    30   val timing: bool Unsynchronized.ref
    31   val profiling: int Unsynchronized.ref
    31   val profiling: int Unsynchronized.ref
    32   val debug: bool Unsynchronized.ref
    32   val debugging: Context.generic option -> ('a -> 'b) -> 'a -> 'b
    33   val debugging: ('a -> 'b) -> 'a -> 'b
    33   val controlled_execution: Context.generic option -> ('a -> 'b) -> 'a -> 'b
    34   val controlled_execution: ('a -> 'b) -> 'a -> 'b
       
    35   val program: (unit -> 'a) -> 'a
    34   val program: (unit -> 'a) -> 'a
    36   val thread: bool -> (unit -> unit) -> Thread.thread
    35   val thread: bool -> (unit -> unit) -> Thread.thread
    37   type transition
    36   type transition
    38   val empty: transition
    37   val empty: transition
    39   val print_of: transition -> bool
    38   val print_of: transition -> bool
   233 val profiling = Unsynchronized.ref 0;
   232 val profiling = Unsynchronized.ref 0;
   234 
   233 
   235 
   234 
   236 (* controlled execution *)
   235 (* controlled execution *)
   237 
   236 
   238 val debug = Runtime.debug;
   237 fun debugging arg = Runtime.debugging ML_Compiler.exn_message arg;
   239 fun debugging f = Runtime.debugging ML_Compiler.exn_message f;
   238 fun controlled_execution arg = Runtime.controlled_execution ML_Compiler.exn_message arg;
   240 fun controlled_execution f = Runtime.controlled_execution ML_Compiler.exn_message f;
       
   241 
   239 
   242 fun program body =
   240 fun program body =
   243  (body
   241  (body
   244   |> controlled_execution
   242   |> controlled_execution NONE
   245   |> Runtime.toplevel_error ML_Compiler.exn_error_message) ();
   243   |> Runtime.toplevel_error ML_Compiler.exn_error_message) ();
   246 
   244 
   247 fun thread interrupts body =
   245 fun thread interrupts body =
   248   Thread.fork
   246   Thread.fork
   249     (((fn () => body () handle exn => if Exn.is_interrupt exn then () else reraise exn)
   247     (((fn () => body () handle exn => if Exn.is_interrupt exn then () else reraise exn)
   250         |> debugging
   248         |> debugging NONE
   251         |> Runtime.toplevel_error
   249         |> Runtime.toplevel_error
   252           (fn exn =>
   250           (fn exn =>
   253             Output.urgent_message ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
   251             Output.urgent_message ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
   254       Simple_Thread.attributes interrupts);
   252       Simple_Thread.attributes interrupts);
   255 
   253 
   270 in
   268 in
   271 
   269 
   272 fun apply_transaction f g node =
   270 fun apply_transaction f g node =
   273   let
   271   let
   274     val cont_node = reset_presentation node;
   272     val cont_node = reset_presentation node;
       
   273     val context = cases_node I (Context.Proof o Proof.context_of) cont_node;
   275     fun state_error e nd = (State (SOME nd, SOME node), e);
   274     fun state_error e nd = (State (SOME nd, SOME node), e);
   276 
   275 
   277     val (result, err) =
   276     val (result, err) =
   278       cont_node
   277       cont_node
   279       |> controlled_execution f
   278       |> controlled_execution (SOME context) f
   280       |> state_error NONE
   279       |> state_error NONE
   281       handle exn => state_error (SOME exn) cont_node;
   280       handle exn => state_error (SOME exn) cont_node;
   282   in
   281   in
   283     (case err of
   282     (case err of
   284       NONE => tap g result
   283       NONE => tap g result
   303   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   302   Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
   304 
   303 
   305 local
   304 local
   306 
   305 
   307 fun apply_tr _ (Init f) (State (NONE, _)) =
   306 fun apply_tr _ (Init f) (State (NONE, _)) =
   308       State (SOME (Theory (Context.Theory (controlled_execution f ()), NONE)), NONE)
   307       State (SOME (Theory (Context.Theory (controlled_execution NONE f ()), NONE)), NONE)
   309   | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
   308   | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
   310       exit_transaction state
   309       exit_transaction state
   311   | apply_tr int (Keep f) state =
   310   | apply_tr int (Keep f) state =
   312       controlled_execution (fn x => tap (f int) x) state
   311       controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state
   313   | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
   312   | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
   314       apply_transaction (fn x => f int x) g state
   313       apply_transaction (fn x => f int x) g state
   315   | apply_tr _ _ _ = raise UNDEF;
   314   | apply_tr _ _ _ = raise UNDEF;
   316 
   315 
   317 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   316 fun apply_union _ [] state = raise FAILURE (state, UNDEF)