src/Pure/Isar/toplevel.ML
changeset 76811 56d76e8cecf4
parent 76810 3f211d126ddc
child 76812 9e09030737e5
equal deleted inserted replaced
76810:3f211d126ddc 76811:56d76e8cecf4
   275   (*node transaction and presentation*)
   275   (*node transaction and presentation*)
   276   Transaction of (bool -> node -> node_presentation) * presentation;
   276   Transaction of (bool -> node -> node_presentation) * presentation;
   277 
   277 
   278 local
   278 local
   279 
   279 
   280 exception FAILURE of state * exn;
       
   281 
       
   282 fun apply_presentation g (st as State (node, (prev_thy, _))) =
   280 fun apply_presentation g (st as State (node, (prev_thy, _))) =
   283   State (node, (prev_thy, g st));
   281   State (node, (prev_thy, g st));
   284 
   282 
   285 fun no_update f node state =
   283 fun no_update f node state =
   286   Runtime.controlled_execution (try generic_theory_of state)
   284   Runtime.controlled_execution (try generic_theory_of state)
   290         val state' = State (node_presentation node, (prev_thy, NONE));
   288         val state' = State (node_presentation node, (prev_thy, NONE));
   291       in apply_presentation f state' end) ()
   289       in apply_presentation f state' end) ()
   292 
   290 
   293 fun update f g node =
   291 fun update f g node =
   294   let
   292   let
   295     val node_pr = node_presentation node;
       
   296     val context = cases_proper_node I (Context.Proof o Proof.context_of) node;
   293     val context = cases_proper_node I (Context.Proof o Proof.context_of) node;
   297     fun make_state node_pr' = State (node_pr', (get_theory node, NONE));
   294     fun next_state node_pr' = State (node_pr', (get_theory node, NONE));
   298 
   295   in Runtime.controlled_execution (SOME context) (f #> next_state #> apply_presentation g) node end;
   299     val (st', err) =
       
   300       (Runtime.controlled_execution (SOME context) (f #> make_state #> apply_presentation g) node,
       
   301         NONE) handle exn => (make_state node_pr, SOME exn);
       
   302   in
       
   303     (case err of
       
   304       NONE => st'
       
   305     | SOME exn => raise FAILURE (st', exn))
       
   306   end;
       
   307 
   296 
   308 fun apply_tr int trans state =
   297 fun apply_tr int trans state =
   309   (case (trans, node_of state) of
   298   (case (trans, node_of state) of
   310     (Init f, Toplevel) =>
   299     (Init f, Toplevel) =>
   311       Runtime.controlled_execution NONE (fn () =>
   300       Runtime.controlled_execution NONE (fn () =>
   322   | (Keep f, node) => no_update (fn x => f int x) node state
   311   | (Keep f, node) => no_update (fn x => f int x) node state
   323   | (Transaction _, Toplevel) => raise UNDEF
   312   | (Transaction _, Toplevel) => raise UNDEF
   324   | (Transaction (f, g), node) => update (fn x => f int x) g node
   313   | (Transaction (f, g), node) => update (fn x => f int x) g node
   325   | _ => raise UNDEF);
   314   | _ => raise UNDEF);
   326 
   315 
   327 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   316 fun apply_union _ [] _ = raise UNDEF
   328   | apply_union int (tr :: trs) state =
   317   | apply_union int (tr :: trs) state =
   329       apply_union int trs state
   318       apply_union int trs state
   330         handle Runtime.UNDEF => apply_tr int tr state
   319         handle Runtime.UNDEF => apply_tr int tr state;
   331           | FAILURE (alt_state, Runtime.UNDEF) => apply_tr int tr alt_state
       
   332           | exn as FAILURE _ => raise exn
       
   333           | exn => raise FAILURE (state, exn);
       
   334 
   320 
   335 fun apply_markers name markers (state as State ((node, pr_ctxt), prev_thy)) =
   321 fun apply_markers name markers (state as State ((node, pr_ctxt), prev_thy)) =
   336   let
   322   let
   337     val state' =
   323     val state' =
   338       Runtime.controlled_execution (try generic_theory_of state)
   324       Runtime.controlled_execution (try generic_theory_of state)
   339         (fn () => State ((node, fold (Document_Marker.evaluate name) markers pr_ctxt), prev_thy)) ();
   325         (fn () => State ((node, fold (Document_Marker.evaluate name) markers pr_ctxt), prev_thy)) ();
   340   in (state', NONE) end
   326   in (state', NONE) end;
   341   handle exn => (state, SOME exn);
       
   342 
   327 
   343 in
   328 in
   344 
   329 
   345 fun apply_trans int name markers trans state =
   330 fun apply_capture int name markers trans state =
   346   (apply_union int trans state |> apply_markers name markers)
   331   (apply_union int trans state |> apply_markers name markers)
   347   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   332     handle exn => (state, SOME exn);
   348 
   333 
   349 end;
   334 end;
   350 
   335 
   351 
   336 
   352 (* datatype transition *)
   337 (* datatype transition *)
   641       else opt_err;
   626       else opt_err;
   642   in (st', opt_err') end;
   627   in (st', opt_err') end;
   643 
   628 
   644 fun app int (tr as Transition {name, markers, trans, ...}) =
   629 fun app int (tr as Transition {name, markers, trans, ...}) =
   645   setmp_thread_position tr
   630   setmp_thread_position tr
   646     (Timing.protocol (name_of tr) (pos_of tr) (apply_trans int name markers trans)
   631     (Timing.protocol (name_of tr) (pos_of tr) (apply_capture int name markers trans)
   647       ##> Option.map (fn Runtime.UNDEF => ERROR (type_error tr) | exn => exn)
   632       ##> Option.map (fn Runtime.UNDEF => ERROR (type_error tr) | exn => exn)
   648       #> show_state);
   633       #> show_state);
   649 
   634 
   650 in
   635 in
   651 
   636