1 (* Title: Pure/PIDE/command.ML
4 Prover command execution: read -- eval -- print.
10 type eval = {exec_id: Document_ID.exec, eval_process: eval_process}
11 val eval_result_state: eval -> Toplevel.state
13 type print = {name: string, pri: int, exec_id: Document_ID.exec, print_process: print_process}
14 type exec = eval * print list
16 val exec_ids: exec option -> Document_ID.exec list
17 val stable_eval: eval -> bool
18 val stable_print: print -> bool
19 val read: (unit -> theory) -> Token.T list -> Toplevel.transition
20 val eval: (unit -> theory) -> Token.T list -> eval -> eval
21 val print: bool -> string -> eval -> print list -> print list option
22 type print_fn = Toplevel.transition -> Toplevel.state -> unit
23 val print_function: {name: string, pri: int} -> (string -> print_fn option) -> unit
24 val no_print_function: string -> unit
25 val execute: exec -> unit
28 structure Command: COMMAND =
31 (** memo results -- including physical interrupts! **)
35 Result of 'a Exn.result;
37 abstype 'a memo = Memo of 'a expr Synchronized.var
40 fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
41 fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
43 fun memo_eval (Memo v) =
44 (case Synchronized.value v of
47 Synchronized.guarded_access v
48 (fn Result res => SOME (res, Result res)
50 let val res = Exn.capture e (); (*sic!*)
51 in SOME (res, Result res) end))
54 fun memo_fork params (Memo v) =
55 (case Synchronized.value v of
57 | _ => ignore ((singleton o Future.forks) params (fn () => memo_eval (Memo v))));
59 fun memo_result (Memo v) =
60 (case Synchronized.value v of
61 Result res => Exn.release res
62 | _ => raise Fail "Unfinished memo result");
64 fun memo_stable (Memo v) =
65 (case Synchronized.value v of
67 | Result res => not (Exn.is_interrupt_exn res));
73 (** main phases of execution **)
75 (* basic type definitions *)
78 {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
80 {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
82 type eval_process = eval_state memo;
83 type eval = {exec_id: Document_ID.exec, eval_process: eval_process};
85 fun eval_result ({eval_process, ...}: eval) = memo_result eval_process;
86 val eval_result_state = #state o eval_result;
88 type print_process = unit memo;
89 type print = {name: string, pri: int, exec_id: Document_ID.exec, print_process: print_process};
91 type exec = eval * print list;
92 val no_exec: exec = ({exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
94 fun exec_ids (NONE: exec option) = []
95 | exec_ids (SOME ({exec_id, ...}, prints)) = exec_id :: map #exec_id prints;
100 fun stable_goals exec_id =
101 not (Par_Exn.is_interrupted (Future.join_results (Goal.peek_futures exec_id)));
103 fun stable_eval ({exec_id, eval_process}: eval) =
104 stable_goals exec_id andalso memo_stable eval_process;
106 fun stable_print ({exec_id, print_process, ...}: print) =
107 stable_goals exec_id andalso memo_stable print_process;
114 val outer_syntax = #2 (Outer_Syntax.get_syntax ());
115 val command_reports = Outer_Syntax.command_reports outer_syntax;
118 Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
120 (case find_first Token.is_command span of
121 SOME tok => Token.position_of tok
122 | NONE => proper_range);
124 val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
125 val _ = Position.reports_text (token_reports @ maps command_reports span);
127 if is_malformed then Toplevel.malformed pos "Malformed command syntax"
129 (case Outer_Syntax.read_spans outer_syntax span of
131 if Keyword.is_control (Toplevel.name_of tr) then
132 Toplevel.malformed pos "Illegal control command"
133 else Toplevel.modify_init init tr
134 | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
135 | _ => Toplevel.malformed proper_range "Exactly one command expected")
136 handle ERROR msg => Toplevel.malformed proper_range msg
145 if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
146 (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
147 (fn () => Toplevel.command_exception int tr st); ([], SOME st))
148 else Toplevel.command_errors int tr st;
150 fun check_cmts span tr st' =
151 Toplevel.setmp_thread_position tr
153 Outer_Syntax.side_comments span |> maps (fn cmt =>
154 (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
155 handle exn => ML_Compiler.exn_messages_ids exn)) ();
157 fun proof_status tr st =
158 (case try Toplevel.proof_of st of
159 SOME prf => Toplevel.status tr (Proof.status_markup prf)
162 fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
164 {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
167 val malformed' = Toplevel.is_malformed tr;
168 val is_init = Toplevel.is_init tr;
169 val is_proof = Keyword.is_proof (Toplevel.name_of tr);
171 val _ = Multithreading.interrupted ();
172 val _ = Toplevel.status tr Markup.running;
173 val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
174 val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
175 val errs = errs1 @ errs2;
176 val _ = Toplevel.status tr Markup.finished;
177 val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
182 val _ = if null errs then Exn.interrupt () else ();
183 val _ = Toplevel.status tr Markup.failed;
184 in {failed = true, malformed = malformed', command = tr, state = st} end
187 val _ = proof_status tr st';
188 in {failed = false, malformed = malformed', command = tr, state = st'} end)
193 fun eval init span eval0 =
195 val exec_id = Document_ID.make ();
199 Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
200 (fn () => read init span |> Toplevel.exec_id exec_id) ();
201 in eval_state span tr (eval_result eval0) end;
202 in {exec_id = exec_id, eval_process = memo process} end;
209 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
213 type print_function = string * (int * (string -> print_fn option));
214 val print_functions = Synchronized.var "Command.print_functions" ([]: print_function list);
216 fun print_error tr e =
217 (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) e () handle exn =>
218 List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
222 fun print command_visible command_name eval old_prints =
224 fun new_print (name, (pri, get_print_fn)) =
226 fun make_print strict print_fn =
228 val exec_id = Document_ID.make ();
231 val {failed, command, state = st', ...} = eval_result eval;
232 val tr = Toplevel.exec_id exec_id command;
234 if failed andalso not strict then ()
235 else print_error tr (fn () => print_fn tr st')
237 in {name = name, pri = pri, exec_id = exec_id, print_process = memo process} end;
239 (case Exn.capture (Runtime.controlled_execution get_print_fn) command_name of
241 | Exn.Res (SOME print_fn) => SOME (make_print false print_fn)
242 | Exn.Exn exn => SOME (make_print true (fn _ => fn _ => reraise exn)))
246 if command_visible then
247 rev (Synchronized.value print_functions) |> map_filter (fn pr =>
248 (case find_first (equal (fst pr) o #name) old_prints of
249 SOME print => if stable_print print then SOME print else new_print pr
250 | NONE => new_print pr))
251 else filter stable_print old_prints;
253 if eq_list (op = o pairself #exec_id) (old_prints, new_prints) then NONE
257 fun print_function {name, pri} f =
258 Synchronized.change print_functions (fn funs =>
259 (if not (AList.defined (op =) funs name) then ()
260 else warning ("Redefining command print function: " ^ quote name);
261 AList.update (op =) (name, (pri, f)) funs));
263 fun no_print_function name =
264 Synchronized.change print_functions (filter_out (equal name o #1));
269 print_function {name = "print_state", pri = 0} (fn command_name => SOME (fn tr => fn st' =>
271 val is_init = Keyword.is_theory_begin command_name;
272 val is_proof = Keyword.is_proof command_name;
275 (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
276 in if do_print then Toplevel.print_state false st' else () end));
279 (* overall execution process *)
281 fun run_print ({name, pri, print_process, ...}: print) =
282 (if Multithreading.enabled () then
283 memo_fork {name = name, group = NONE, deps = [], pri = pri, interrupts = true}
284 else memo_eval) print_process;
286 fun execute (({eval_process, ...}, prints): exec) =
287 (memo_eval eval_process; List.app run_print prints);