src/Pure/Isar/toplevel.ML
changeset 23363 9981199bf865
parent 22588 4a859d13ef83
child 23426 d0efa182109f
equal deleted inserted replaced
23362:de1476695aa6 23363:9981199bf865
   356 
   356 
   357 fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
   357 fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
   358 
   358 
   359 val stale_theory = ERROR "Stale theory encountered after succesful execution!";
   359 val stale_theory = ERROR "Stale theory encountered after succesful execution!";
   360 
   360 
   361 fun map_theory f = History.map
   361 fun map_theory f = History.map_current
   362   (fn Theory (gthy, _) => Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, NONE)
   362   (fn Theory (gthy, _) => Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, NONE)
   363     | node => node);
   363     | node => node);
   364 
   364 
       
   365 fun context_position pos = History.map_current
       
   366   (fn Theory (Context.Proof lthy, ctxt) =>
       
   367         Theory (Context.Proof (ContextPosition.put pos lthy), ctxt)
       
   368     | Proof (prf, x) =>
       
   369         Proof (ProofHistory.map_current (Proof.map_context (ContextPosition.put pos)) prf, x)
       
   370     | node => node);
       
   371 
   365 fun return (result, NONE) = result
   372 fun return (result, NONE) = result
   366   | return (result, SOME exn) = raise FAILURE (result, exn);
   373   | return (result, SOME exn) = raise FAILURE (result, exn);
   367 
   374 
   368 in
   375 in
   369 
   376 
   370 fun transaction hist f (node, term) =
   377 fun transaction hist pos f (node, term) =
   371   let
   378   let
   372     val cont_node = map_theory Theory.checkpoint node;
   379     val cont_node = map_theory Theory.checkpoint node;
   373     val back_node = map_theory Theory.copy cont_node;
   380     val back_node = map_theory Theory.copy cont_node;
   374     fun state nd = State (nd, term);
   381     fun state nd = State (nd, term);
   375     fun normal_state nd = (state nd, NONE);
   382     fun normal_state nd = (state nd, NONE);
   376     fun error_state nd exn = (state nd, SOME exn);
   383     fun error_state nd exn = (state nd, SOME exn);
   377 
   384 
   378     val (result, err) =
   385     val (result, err) =
   379       cont_node
   386       cont_node
       
   387       |> context_position pos
   380       |> (f
   388       |> (f
   381           |> (if hist then History.apply' (History.current back_node) else History.map)
   389           |> (if hist then History.apply' (History.current back_node) else History.map_current)
   382           |> controlled_execution)
   390           |> controlled_execution)
       
   391       |> context_position Position.none
   383       |> normal_state
   392       |> normal_state
   384       handle exn => error_state cont_node exn;
   393       handle exn => error_state cont_node exn;
   385   in
   394   in
   386     if is_stale result
   395     if is_stale result
   387     then return (error_state back_node (the_default stale_theory err))
   396     then return (error_state back_node (the_default stale_theory err))
   419 
   428 
   420 local
   429 local
   421 
   430 
   422 fun keep_state int f = controlled_execution (fn x => tap (f int) x);
   431 fun keep_state int f = controlled_execution (fn x => tap (f int) x);
   423 
   432 
   424 fun apply_tr int (Init (f, term)) (state as Toplevel _) =
   433 fun apply_tr int _ (Init (f, term)) (state as Toplevel _) =
   425       let val node = Theory (Context.Theory (f int), NONE)
   434       let val node = Theory (Context.Theory (f int), NONE)
   426       in safe_exit state; State (History.init (undo_limit int) node, term) end
   435       in safe_exit state; State (History.init (undo_limit int) node, term) end
   427   | apply_tr int (InitEmpty f) state =
   436   | apply_tr int _ (InitEmpty f) state =
   428       (keep_state int (K f) state; safe_exit state; toplevel)
   437       (keep_state int (K f) state; safe_exit state; toplevel)
   429   | apply_tr _ Exit (State (node, term)) =
   438   | apply_tr _ _ Exit (State (node, term)) =
   430       (the_global_theory (History.current node); Toplevel (SOME (node, term)))
   439       (the_global_theory (History.current node); Toplevel (SOME (node, term)))
   431   | apply_tr _ UndoExit (Toplevel (SOME state_info)) = State state_info
   440   | apply_tr _ _ UndoExit (Toplevel (SOME state_info)) = State state_info
   432   | apply_tr _ Kill (State (node, (_, kill))) =
   441   | apply_tr _ _ Kill (State (node, (_, kill))) =
   433       (kill (the_global_theory (History.current node)); toplevel)
   442       (kill (the_global_theory (History.current node)); toplevel)
   434   | apply_tr _ (History f) (State (node, term)) = State (f node, term)
   443   | apply_tr _ _ (History f) (State (node, term)) = State (f node, term)
   435   | apply_tr int (Keep f) state = keep_state int f state
   444   | apply_tr int _ (Keep f) state = keep_state int f state
   436   | apply_tr int (Transaction (hist, f)) (State state) =
   445   | apply_tr int pos (Transaction (hist, f)) (State state) =
   437       transaction hist (fn x => f int x) state
   446       transaction hist pos (fn x => f int x) state
   438   | apply_tr _ _ _ = raise UNDEF;
   447   | apply_tr _ _ _ _ = raise UNDEF;
   439 
   448 
   440 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   449 fun apply_union _ _ [] state = raise FAILURE (state, UNDEF)
   441   | apply_union int (tr :: trs) state =
   450   | apply_union int pos (tr :: trs) state =
   442       apply_tr int tr state
   451       apply_tr int pos tr state
   443         handle UNDEF => apply_union int trs state
   452         handle UNDEF => apply_union int pos trs state
   444           | FAILURE (alt_state, UNDEF) => apply_union int trs alt_state
   453           | FAILURE (alt_state, UNDEF) => apply_union int pos trs alt_state
   445           | exn as FAILURE _ => raise exn
   454           | exn as FAILURE _ => raise exn
   446           | exn => raise FAILURE (state, exn);
   455           | exn => raise FAILURE (state, exn);
   447 
   456 
   448 in
   457 in
   449 
   458 
   450 fun apply_trans int trs state = (apply_union int trs state, NONE)
   459 fun apply_trans int pos trs state = (apply_union int pos trs state, NONE)
   451   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   460   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   452 
   461 
   453 end;
   462 end;
   454 
   463 
   455 
   464 
   658 
   667 
   659 (* apply transitions *)
   668 (* apply transitions *)
   660 
   669 
   661 local
   670 local
   662 
   671 
   663 fun app int (tr as Transition {trans, int_only, print, no_timing, ...}) state =
   672 fun app int (tr as Transition {trans, pos, int_only, print, no_timing, ...}) state =
   664   let
   673   let
   665     val _ =
   674     val _ =
   666       if not int andalso int_only then warning (command_msg "Interactive-only " tr)
   675       if not int andalso int_only then warning (command_msg "Interactive-only " tr)
   667       else ();
   676       else ();
   668 
   677 
   669     fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   678     fun do_timing f x = (warning (command_msg "" tr); timeap f x);
   670     fun do_profiling f x = profile (! profiling) f x;
   679     fun do_profiling f x = profile (! profiling) f x;
   671 
   680 
   672     val (result, opt_exn) =
   681     val (result, opt_exn) =
   673        state |> (apply_trans int trans
   682        state |> (apply_trans int pos trans
   674         |> (if ! profiling > 0 then do_profiling else I)
   683         |> (if ! profiling > 0 then do_profiling else I)
   675         |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
   684         |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
   676     val _ =
   685     val _ =
   677       if int andalso not (! quiet) andalso exists (member (op =) print) ("" :: ! print_mode)
   686       if int andalso not (! quiet) andalso exists (member (op =) print) ("" :: ! print_mode)
   678       then print_state false result else ();
   687       then print_state false result else ();