src/Pure/Isar/toplevel.ML
changeset 69887 b9985133805d
parent 69886 0cb8753bdb50
child 69888 6db51f45b5f9
equal deleted inserted replaced
69886:0cb8753bdb50 69887:b9985133805d
    35   val pos_of: transition -> Position.T
    35   val pos_of: transition -> Position.T
    36   val timing_of: transition -> Time.time
    36   val timing_of: transition -> Time.time
    37   val type_error: transition -> string
    37   val type_error: transition -> string
    38   val name: string -> transition -> transition
    38   val name: string -> transition -> transition
    39   val position: Position.T -> transition -> transition
    39   val position: Position.T -> transition -> transition
       
    40   val markers: Input.source list -> transition -> transition
    40   val timing: Time.time -> transition -> transition
    41   val timing: Time.time -> transition -> transition
    41   val init_theory: (unit -> theory) -> transition -> transition
    42   val init_theory: (unit -> theory) -> transition -> transition
    42   val is_init: transition -> bool
    43   val is_init: transition -> bool
    43   val modify_init: (unit -> theory) -> transition -> transition
    44   val modify_init: (unit -> theory) -> transition -> transition
    44   val exit: transition -> transition
    45   val exit: transition -> transition
    83   val command_errors: bool -> transition -> state -> Runtime.error list * state option
    84   val command_errors: bool -> transition -> state -> Runtime.error list * state option
    84   val command_exception: bool -> transition -> state -> state
    85   val command_exception: bool -> transition -> state -> state
    85   val reset_theory: state -> state option
    86   val reset_theory: state -> state option
    86   val reset_proof: state -> state option
    87   val reset_proof: state -> state option
    87   val reset_notepad: state -> state option
    88   val reset_notepad: state -> state option
       
    89   val fork_presentation: transition -> transition * transition
    88   type result
    90   type result
    89   val join_results: result -> (transition * state) list
    91   val join_results: result -> (transition * state) list
    90   val element_result: Keyword.keywords -> transition Thy_Element.element -> state -> result * state
    92   val element_result: Keyword.keywords -> transition Thy_Element.element -> state -> result * state
    91 end;
    93 end;
    92 
    94 
   140     (*current node with presentation context, previous theory*)
   142     (*current node with presentation context, previous theory*)
   141 
   143 
   142 fun node_of (State ((node, _), _)) = node;
   144 fun node_of (State ((node, _), _)) = node;
   143 fun previous_theory_of (State (_, prev_thy)) = prev_thy;
   145 fun previous_theory_of (State (_, prev_thy)) = prev_thy;
   144 
   146 
   145 fun init_toplevel () =
   147 fun init_toplevel () = State (node_presentation Toplevel, NONE);
   146   let val thy0 = Theory.get_pure () handle Fail _ => Context.the_global_context ();
   148 fun theory_toplevel thy = State (node_presentation (Theory (Context.Theory thy)), NONE);
   147   in State ((Toplevel, Proof_Context.init_global thy0), NONE) end;
   149 
   148 
       
   149 fun theory_toplevel thy =
       
   150   State (node_presentation (Theory (Context.Theory thy)), NONE);
       
   151 
   150 
   152 fun level state =
   151 fun level state =
   153   (case node_of state of
   152   (case node_of state of
   154     Toplevel => 0
   153     Toplevel => 0
   155   | Theory _ => 0
   154   | Theory _ => 0
   287   (*node transaction and presentation*)
   286   (*node transaction and presentation*)
   288   Transaction of (bool -> node -> node_presentation) * (state -> unit);
   287   Transaction of (bool -> node -> node_presentation) * (state -> unit);
   289 
   288 
   290 local
   289 local
   291 
   290 
   292 fun apply_tr _ (Init f) (State ((Toplevel, _), _)) =
   291 fun apply_tr int trans state =
   293       let val node = Theory (Context.Theory (Runtime.controlled_execution NONE f ()))
   292   (case (trans, node_of state) of
   294       in State (node_presentation node, NONE) end
   293     (Init f, Toplevel) =>
   295   | apply_tr _ Exit (State ((node as Theory (Context.Theory thy), _), _)) =
   294       Runtime.controlled_execution NONE (fn () =>
       
   295         State (node_presentation (Theory (Context.Theory (f ()))), NONE)) ()
       
   296   | (Exit, node as Theory (Context.Theory thy)) =>
   296       let
   297       let
   297         val State ((node', pr_ctxt), _) =
   298         val State ((node', pr_ctxt), _) =
   298           node |> apply_transaction
   299           node |> apply_transaction
   299             (fn _ => node_presentation (Theory (Context.Theory (Theory.end_theory thy))))
   300             (fn _ => node_presentation (Theory (Context.Theory (Theory.end_theory thy))))
   300             (K ());
   301             (K ());
   301       in State ((Toplevel, pr_ctxt), get_theory node') end
   302       in State ((Toplevel, pr_ctxt), get_theory node') end
   302   | apply_tr int (Keep f) state =
   303   | (Keep f, node) =>
   303       Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state
   304       Runtime.controlled_execution (try generic_theory_of state)
   304   | apply_tr _ (Transaction _) (State ((Toplevel, _), _)) = raise UNDEF
   305         (fn () => (f int state; State (node_presentation node, previous_theory_of state))) ()
   305   | apply_tr int (Transaction (f, g)) (State ((node, _), _)) =
   306   | (Transaction _, Toplevel) => raise UNDEF
   306       apply_transaction (fn x => f int x) g node
   307   | (Transaction (f, g), node) => apply_transaction (fn x => f int x) g node
   307   | apply_tr _ _ _ = raise UNDEF;
   308   | _ => raise UNDEF);
   308 
   309 
   309 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   310 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   310   | apply_union int (tr :: trs) state =
   311   | apply_union int (tr :: trs) state =
   311       apply_union int trs state
   312       apply_union int trs state
   312         handle Runtime.UNDEF => apply_tr int tr state
   313         handle Runtime.UNDEF => apply_tr int tr state
   313           | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
   314           | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
   314           | exn as FAILURE _ => raise exn
   315           | exn as FAILURE _ => raise exn
   315           | exn => raise FAILURE (state, exn);
   316           | exn => raise FAILURE (state, exn);
   316 
   317 
       
   318 fun apply_markers markers (state as State ((node, pr_ctxt), prev_thy)) =
       
   319   let
       
   320     val state' =
       
   321       Runtime.controlled_execution (try generic_theory_of state)
       
   322         (fn () => State ((node, fold Document_Marker.evaluate markers pr_ctxt), prev_thy)) ();
       
   323   in (state', NONE) end
       
   324   handle exn => (state, SOME exn);
       
   325 
   317 in
   326 in
   318 
   327 
   319 fun apply_trans int trs state = (apply_union int trs state, NONE)
   328 fun apply_trans int trans markers state =
       
   329   (apply_union int trans state |> apply_markers markers)
   320   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   330   handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
   321 
   331 
   322 end;
   332 end;
   323 
   333 
   324 
   334 
   325 (* datatype transition *)
   335 (* datatype transition *)
   326 
   336 
   327 datatype transition = Transition of
   337 datatype transition = Transition of
   328  {name: string,              (*command name*)
   338  {name: string,               (*command name*)
   329   pos: Position.T,           (*source position*)
   339   pos: Position.T,            (*source position*)
   330   timing: Time.time,         (*prescient timing information*)
   340   markers: Input.source list, (*semantic document markers*)
   331   trans: trans list};        (*primitive transitions (union)*)
   341   timing: Time.time,          (*prescient timing information*)
   332 
   342   trans: trans list};         (*primitive transitions (union)*)
   333 fun make_transition (name, pos, timing, trans) =
   343 
   334   Transition {name = name, pos = pos, timing = timing, trans = trans};
   344 fun make_transition (name, pos, markers, timing, trans) =
   335 
   345   Transition {name = name, pos = pos, markers = markers, timing = timing, trans = trans};
   336 fun map_transition f (Transition {name, pos, timing, trans}) =
   346 
   337   make_transition (f (name, pos, timing, trans));
   347 fun map_transition f (Transition {name, pos, markers, timing, trans}) =
   338 
   348   make_transition (f (name, pos, markers, timing, trans));
   339 val empty = make_transition ("", Position.none, Time.zeroTime, []);
   349 
       
   350 val empty = make_transition ("", Position.none, [], Time.zeroTime, []);
   340 
   351 
   341 
   352 
   342 (* diagnostics *)
   353 (* diagnostics *)
   343 
   354 
   344 fun name_of (Transition {name, ...}) = name;
   355 fun name_of (Transition {name, ...}) = name;
   353 fun type_error tr = command_msg "Bad context for " tr;
   364 fun type_error tr = command_msg "Bad context for " tr;
   354 
   365 
   355 
   366 
   356 (* modify transitions *)
   367 (* modify transitions *)
   357 
   368 
   358 fun name name = map_transition (fn (_, pos, timing, trans) =>
   369 fun name name = map_transition (fn (_, pos, markers, timing, trans) =>
   359   (name, pos, timing, trans));
   370   (name, pos, markers, timing, trans));
   360 
   371 
   361 fun position pos = map_transition (fn (name, _, timing, trans) =>
   372 fun position pos = map_transition (fn (name, _, markers, timing, trans) =>
   362   (name, pos, timing, trans));
   373   (name, pos, markers, timing, trans));
   363 
   374 
   364 fun timing timing = map_transition (fn (name, pos, _, trans) =>
   375 fun markers markers = map_transition (fn (name, pos, _, timing, trans) =>
   365   (name, pos, timing, trans));
   376   (name, pos, markers, timing, trans));
   366 
   377 
   367 fun add_trans tr = map_transition (fn (name, pos, timing, trans) =>
   378 fun timing timing = map_transition (fn (name, pos, markers, _, trans) =>
   368   (name, pos, timing, tr :: trans));
   379   (name, pos, markers, timing, trans));
   369 
   380 
   370 val reset_trans = map_transition (fn (name, pos, timing, _) =>
   381 fun add_trans tr = map_transition (fn (name, pos, markers, timing, trans) =>
   371   (name, pos, timing, []));
   382   (name, pos, markers, timing, tr :: trans));
       
   383 
       
   384 val reset_trans = map_transition (fn (name, pos, markers, timing, _) =>
       
   385   (name, pos, markers, timing, []));
   372 
   386 
   373 
   387 
   374 (* basic transitions *)
   388 (* basic transitions *)
   375 
   389 
   376 fun init_theory f = add_trans (Init f);
   390 fun init_theory f = add_trans (Init f);
   603 
   617 
   604 (* apply transitions *)
   618 (* apply transitions *)
   605 
   619 
   606 local
   620 local
   607 
   621 
   608 fun app int (tr as Transition {trans, ...}) =
   622 fun app int (tr as Transition {markers, trans, ...}) =
   609   setmp_thread_position tr
   623   setmp_thread_position tr
   610     (Timing.protocol (name_of tr) (pos_of tr) (apply_trans int trans)
   624     (Timing.protocol (name_of tr) (pos_of tr) (apply_trans int trans markers)
   611       ##> Option.map (fn UNDEF => ERROR (type_error tr) | exn => exn));
   625       ##> Option.map (fn UNDEF => ERROR (type_error tr) | exn => exn));
   612 
   626 
   613 in
   627 in
   614 
   628 
   615 fun transition int tr st =
   629 fun transition int tr st =
   705       not (Proof.is_relevant state) andalso
   719       not (Proof.is_relevant state) andalso
   706        (if can (Proof.assert_bottom true) state
   720        (if can (Proof.assert_bottom true) state
   707         then Future.proofs_enabled 1
   721         then Future.proofs_enabled 1
   708         else Future.proofs_enabled 2 orelse Future.proofs_enabled_timing estimate));
   722         else Future.proofs_enabled 2 orelse Future.proofs_enabled_timing estimate));
   709 
   723 
       
   724 val empty_markers = markers [];
       
   725 val empty_trans = reset_trans #> keep (K ());
       
   726 
       
   727 in
       
   728 
       
   729 fun fork_presentation tr = (tr |> empty_markers, tr |> empty_trans);
       
   730 
   710 fun atom_result keywords tr st =
   731 fun atom_result keywords tr st =
   711   let
   732   let
   712     val st' =
   733     val st' =
   713       if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then
   734       if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then
   714         (Execution.fork
   735         let
   715           {name = "Toplevel.diag", pos = pos_of tr, pri = ~1}
   736           val (tr1, tr2) = fork_presentation tr;
   716           (fn () => command tr st); st)
   737           val _ =
       
   738             Execution.fork {name = "Toplevel.diag", pos = pos_of tr, pri = ~1}
       
   739               (fn () => command tr1 st);
       
   740         in command tr2 st end
   717       else command tr st;
   741       else command tr st;
   718   in (Result (tr, st'), st') end;
   742   in (Result (tr, st'), st') end;
   719 
       
   720 in
       
   721 
   743 
   722 fun element_result keywords (Thy_Element.Element (tr, NONE)) st = atom_result keywords tr st
   744 fun element_result keywords (Thy_Element.Element (tr, NONE)) st = atom_result keywords tr st
   723   | element_result keywords (elem as Thy_Element.Element (head_tr, SOME element_rest)) st =
   745   | element_result keywords (elem as Thy_Element.Element (head_tr, SOME element_rest)) st =
   724       let
   746       let
   725         val (head_result, st') = atom_result keywords head_tr st;
   747         val (head_result, st') = atom_result keywords head_tr st;
   732             val proof_trs = maps Thy_Element.flat_element body_elems @ [end_tr];
   754             val proof_trs = maps Thy_Element.flat_element body_elems @ [end_tr];
   733             val (proof_results, st'') = fold_map (atom_result keywords) proof_trs st';
   755             val (proof_results, st'') = fold_map (atom_result keywords) proof_trs st';
   734           in (Result_List (head_result :: proof_results), st'') end
   756           in (Result_List (head_result :: proof_results), st'') end
   735         else
   757         else
   736           let
   758           let
       
   759             val (end_tr1, end_tr2) = fork_presentation end_tr;
       
   760 
   737             val finish = Context.Theory o Proof_Context.theory_of;
   761             val finish = Context.Theory o Proof_Context.theory_of;
   738 
   762 
   739             val future_proof =
   763             val future_proof =
   740               Proof.future_proof (fn state =>
   764               Proof.future_proof (fn state =>
   741                 Execution.fork
   765                 Execution.fork
   742                   {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = ~1}
   766                   {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = ~1}
   743                   (fn () =>
   767                   (fn () =>
   744                     let
   768                     let
   745                       val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st';
   769                       val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st';
   746                       val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy));
   770                       val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy));
   747                       val (result, result_state) =
   771                       val (results, result_state) =
   748                         State (node_presentation node', prev_thy)
   772                         State (node_presentation node', prev_thy)
   749                         |> fold_map (element_result keywords) body_elems ||> command end_tr;
   773                         |> fold_map (element_result keywords) body_elems ||> command end_tr1;
   750                     in (Result_List result, presentation_context0 result_state) end))
   774                     in (Result_List results, presentation_context0 result_state) end))
   751               #> (fn (res, state') => state' |> put_result (Result_Future res));
   775               #> (fn (res, state') => state' |> put_result (Result_Future res));
   752 
   776 
   753             val forked_proof =
   777             val forked_proof =
   754               proof (future_proof #>
   778               proof (future_proof #>
   755                 (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
   779                 (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
   756               end_proof (fn _ => future_proof #>
   780               end_proof (fn _ => future_proof #>
   757                 (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
   781                 (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
   758 
   782 
   759             val st'' = st'
   783             val st'' = st' |> command (head_tr |> reset_trans |> forked_proof);
   760               |> command (head_tr |> reset_trans |> forked_proof);
   784             val end_st = st'' |> command end_tr2;
   761             val end_result = Result (end_tr, st'');
   785             val end_result = Result (end_tr, end_st);
   762             val result =
   786             val result =
   763               Result_List [head_result, Result.get (presentation_context0 st''), end_result];
   787               Result_List [head_result, Result.get (presentation_context0 st''), end_result];
   764           in (result, st'') end
   788           in (result, end_st) end
   765       end;
   789       end;
   766 
   790 
   767 end;
   791 end;
   768 
   792 
   769 end;
   793 end;