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}
12 type print = {name: string, pri: int, exec_id: Document_ID.exec, print_process: print_process}
13 type exec = eval * print list
14 val read: (unit -> theory) -> Token.T list -> Toplevel.transition
16 val eval_result_state: eval -> Toplevel.state
17 val eval: (unit -> theory) -> Token.T list -> eval -> eval
18 val print: string -> eval -> print list
19 type print_fn = Toplevel.transition -> Toplevel.state -> unit
20 val print_function: {name: string, pri: int} -> (string -> print_fn option) -> unit
22 val exec_ids: exec -> Document_ID.exec list
23 val exec_run: exec -> unit
24 val stable_eval: eval -> bool
25 val stable_print: print -> bool
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));
76 {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
77 type eval_process = eval_state memo;
78 type eval = {exec_id: Document_ID.exec, eval_process: eval_process};
80 type print_process = unit memo;
81 type print = {name: string, pri: int, exec_id: Document_ID.exec, print_process: print_process};
88 val outer_syntax = #2 (Outer_Syntax.get_syntax ());
89 val command_reports = Outer_Syntax.command_reports outer_syntax;
92 Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
94 (case find_first Token.is_command span of
95 SOME tok => Token.position_of tok
96 | NONE => proper_range);
98 val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
99 val _ = Position.reports_text (token_reports @ maps command_reports span);
101 if is_malformed then Toplevel.malformed pos "Malformed command syntax"
103 (case Outer_Syntax.read_spans outer_syntax span of
105 if Keyword.is_control (Toplevel.name_of tr) then
106 Toplevel.malformed pos "Illegal control command"
107 else Toplevel.modify_init init tr
108 | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
109 | _ => Toplevel.malformed proper_range "Exactly one command expected")
110 handle ERROR msg => Toplevel.malformed proper_range msg
116 val no_eval_state: eval_state =
117 {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
119 val no_eval: eval = {exec_id = Document_ID.none, eval_process = memo_value no_eval_state};
121 fun eval_result ({eval_process, ...}: eval) = memo_result eval_process;
122 val eval_result_state = #state o eval_result;
127 if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
128 (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
129 (fn () => Toplevel.command_exception int tr st); ([], SOME st))
130 else Toplevel.command_errors int tr st;
132 fun check_cmts span tr st' =
133 Toplevel.setmp_thread_position tr
135 Outer_Syntax.side_comments span |> maps (fn cmt =>
136 (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
137 handle exn => ML_Compiler.exn_messages_ids exn)) ();
139 fun proof_status tr st =
140 (case try Toplevel.proof_of st of
141 SOME prf => Toplevel.status tr (Proof.status_markup prf)
144 fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
146 {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
149 val malformed' = Toplevel.is_malformed tr;
150 val is_init = Toplevel.is_init tr;
151 val is_proof = Keyword.is_proof (Toplevel.name_of tr);
153 val _ = Multithreading.interrupted ();
154 val _ = Toplevel.status tr Markup.running;
155 val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
156 val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
157 val errs = errs1 @ errs2;
158 val _ = Toplevel.status tr Markup.finished;
159 val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
164 val _ = if null errs then Exn.interrupt () else ();
165 val _ = Toplevel.status tr Markup.failed;
166 in {failed = true, malformed = malformed', command = tr, state = st} end
169 val _ = proof_status tr st';
170 in {failed = false, malformed = malformed', command = tr, state = st'} end)
175 fun eval init span eval0 =
177 val exec_id = Document_ID.make ();
181 Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
182 (fn () => read init span |> Toplevel.put_id exec_id) ();
183 in eval_state span tr (eval_result eval0) end;
184 in {exec_id = exec_id, eval_process = memo process} end;
191 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
195 type print_function = string * (int * (string -> print_fn option));
196 val print_functions = Synchronized.var "Command.print_functions" ([]: print_function list);
198 fun output_error tr exn =
199 List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
201 fun print_error tr f x =
202 (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) f x
203 handle exn => output_error tr exn;
207 fun print command_name eval =
208 rev (Synchronized.value print_functions) |> map_filter (fn (name, (pri, get_print_fn)) =>
209 (case Exn.capture (Runtime.controlled_execution get_print_fn) command_name of
211 | Exn.Res (SOME print_fn) =>
213 val exec_id = Document_ID.make ();
216 val {failed, command, state = st', ...} = eval_result eval;
217 val tr = Toplevel.put_id exec_id command;
218 in if failed then () else print_error tr (fn () => print_fn tr st') () end;
219 in SOME {name = name, pri = pri, exec_id = exec_id, print_process = memo process} end
222 val exec_id = Document_ID.make ();
225 val {command, ...} = eval_result eval;
226 val tr = Toplevel.put_id exec_id command;
227 in output_error tr exn end;
228 in SOME {name = name, pri = pri, exec_id = exec_id, print_process = memo process} end));
230 fun print_function {name, pri} f =
231 Synchronized.change print_functions (fn funs =>
232 (if not (AList.defined (op =) funs name) then ()
233 else warning ("Redefining command print function: " ^ quote name);
234 AList.update (op =) (name, (pri, f)) funs));
239 print_function {name = "print_state", pri = 0} (fn command_name => SOME (fn tr => fn st' =>
241 val is_init = Keyword.is_theory_begin command_name;
242 val is_proof = Keyword.is_proof command_name;
245 (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
246 in if do_print then Toplevel.print_state false st' else () end));
250 (** managed evaluation **)
254 type exec = eval * print list;
255 val no_exec: exec = (no_eval, []);
257 fun exec_ids (({exec_id, ...}, prints): exec) = exec_id :: map #exec_id prints;
259 fun exec_run (({eval_process, ...}, prints): exec) =
260 (memo_eval eval_process;
261 prints |> List.app (fn {name, pri, print_process, ...} =>
262 memo_fork {name = name, group = NONE, deps = [], pri = pri, interrupts = true} print_process));
265 (* stable situations after cancellation *)
267 fun stable_goals exec_id =
268 not (Par_Exn.is_interrupted (Future.join_results (Goal.peek_futures exec_id)));
270 fun stable_eval ({exec_id, eval_process}: eval) =
271 stable_goals exec_id andalso memo_stable eval_process;
273 fun stable_print ({exec_id, print_process, ...}: print) =
274 stable_goals exec_id andalso memo_stable print_process;