strictly monotonic Document.update: avoid disruptive cancel_execution, merely discontinue_execution and cancel/terminate old execs individually;
1 (* Title: Pure/PIDE/command.ML
4 Prover command execution: read -- eval -- print.
9 val read: (unit -> theory) -> Token.T list -> Toplevel.transition
11 val eval_result_state: eval -> Toplevel.state
12 val eval_same: eval * eval -> bool
13 val eval: (unit -> theory) -> Token.T list -> eval -> eval
15 val print: bool -> string -> eval -> print list -> print list option
16 type print_fn = Toplevel.transition -> Toplevel.state -> unit
17 val print_function: {name: string, pri: int, persistent: bool} ->
18 ({command_name: string} -> print_fn option) -> unit
19 val no_print_function: string -> unit
20 type exec = eval * print list
22 val exec_ids: exec option -> Document_ID.exec list
23 val exec: exec -> unit
26 structure Command: COMMAND =
29 (** memo results -- including physical interrupts! **)
32 Expr of Document_ID.exec * (unit -> 'a) |
33 Result of 'a Exn.result;
35 abstype 'a memo = Memo of 'a expr Synchronized.var
38 fun memo exec_id e = Memo (Synchronized.var "Command.memo" (Expr (exec_id, e)));
39 fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
41 fun memo_result (Memo v) =
42 (case Synchronized.value v of
43 Result res => Exn.release res
44 | Expr (exec_id, _) => error ("Unfinished execution result: " ^ Document_ID.print exec_id));
46 fun memo_exec (Memo v) =
47 (case Synchronized.value v of
49 | Expr (exec_id, _) =>
50 Synchronized.timed_access v (fn _ => SOME Time.zeroTime)
51 (fn Result res => SOME (res, Result res)
52 | Expr (exec_id, e) =>
53 uninterruptible (fn restore_attributes => fn () =>
55 val _ = Exec.running exec_id;
56 val res = Exn.capture (restore_attributes e) ();
57 val _ = Exec.finished exec_id (not (Exn.is_interrupt_exn res));
58 in SOME (res, Result res) end) ())
59 |> (fn NONE => error ("Concurrent execution attempt: " ^ Document_ID.print exec_id)
63 fun memo_fork params (Memo v) =
64 (case Synchronized.value v of
66 | _ => ignore ((singleton o Future.forks) params (fn () => memo_exec (Memo v))));
72 (** main phases of execution **)
78 val outer_syntax = #2 (Outer_Syntax.get_syntax ());
79 val command_reports = Outer_Syntax.command_reports outer_syntax;
82 Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
84 (case find_first Token.is_command span of
85 SOME tok => Token.position_of tok
86 | NONE => proper_range);
88 val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
89 val _ = Position.reports_text (token_reports @ maps command_reports span);
91 if is_malformed then Toplevel.malformed pos "Malformed command syntax"
93 (case Outer_Syntax.read_spans outer_syntax span of
95 if Keyword.is_control (Toplevel.name_of tr) then
96 Toplevel.malformed pos "Illegal control command"
97 else Toplevel.modify_init init tr
98 | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
99 | _ => Toplevel.malformed proper_range "Exactly one command expected")
100 handle ERROR msg => Toplevel.malformed proper_range msg
107 {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
108 val init_eval_state =
109 {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
111 datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
113 fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
114 val eval_result_state = #state o eval_result;
116 fun eval_same (Eval {exec_id, ...}, Eval {exec_id = exec_id', ...}) =
117 exec_id = exec_id' andalso Exec.is_stable exec_id;
122 if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
123 (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
124 (fn () => Toplevel.command_exception int tr st); ([], SOME st))
125 else Toplevel.command_errors int tr st;
127 fun check_cmts span tr st' =
128 Toplevel.setmp_thread_position tr
130 Outer_Syntax.side_comments span |> maps (fn cmt =>
131 (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
132 handle exn => ML_Compiler.exn_messages_ids exn)) ();
134 fun proof_status tr st =
135 (case try Toplevel.proof_of st of
136 SOME prf => Toplevel.status tr (Proof.status_markup prf)
139 fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
141 {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
144 val malformed' = Toplevel.is_malformed tr;
145 val is_init = Toplevel.is_init tr;
146 val is_proof = Keyword.is_proof (Toplevel.name_of tr);
148 val _ = Multithreading.interrupted ();
149 val _ = Toplevel.status tr Markup.running;
150 val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
151 val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
152 val errs = errs1 @ errs2;
153 val _ = Toplevel.status tr Markup.finished;
154 val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
159 val _ = if null errs then Exn.interrupt () else ();
160 val _ = Toplevel.status tr Markup.failed;
161 in {failed = true, malformed = malformed', command = tr, state = st} end
164 val _ = proof_status tr st';
165 in {failed = false, malformed = malformed', command = tr, state = st'} end)
170 fun eval init span eval0 =
172 val exec_id = Document_ID.make ();
176 Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
177 (fn () => read init span |> Toplevel.exec_id exec_id) ();
178 in eval_state span tr (eval_result eval0) end;
179 in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
186 datatype print = Print of
187 {name: string, pri: int, persistent: bool,
188 exec_id: Document_ID.exec, print_process: unit memo};
190 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
194 type print_function = string * (int * bool * ({command_name: string} -> print_fn option));
195 val print_functions = Synchronized.var "Command.print_functions" ([]: print_function list);
197 fun print_error tr e =
198 (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) e () handle exn =>
199 List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
201 fun print_persistent (Print {persistent, ...}) = persistent;
202 fun print_stable (Print {exec_id, ...}) = Exec.is_stable exec_id;
203 fun print_eq (Print {exec_id, ...}, Print {exec_id = exec_id', ...}) = exec_id = exec_id';
207 fun print command_visible command_name eval old_prints =
209 fun new_print (name, (pri, persistent, get_fn)) =
211 fun make_print strict print_fn =
213 val exec_id = Document_ID.make ();
216 val {failed, command, state = st', ...} = eval_result eval;
217 val tr = Toplevel.exec_id exec_id command;
219 if failed andalso not strict then ()
220 else print_error tr (fn () => print_fn tr st')
224 name = name, pri = pri, persistent = persistent,
225 exec_id = exec_id, print_process = memo exec_id process}
228 (case Exn.capture (Runtime.controlled_execution get_fn) {command_name = command_name} of
230 | Exn.Res (SOME print_fn) => SOME (make_print false print_fn)
231 | Exn.Exn exn => SOME (make_print true (fn _ => fn _ => reraise exn)))
235 if command_visible then
236 rev (Synchronized.value print_functions) |> map_filter (fn pr =>
237 (case find_first (fn Print {name, ...} => name = fst pr) old_prints of
238 SOME print => if print_stable print then SOME print else new_print pr
239 | NONE => new_print pr))
240 else filter (fn print => print_persistent print andalso print_stable print) old_prints;
242 if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
245 fun print_function {name, pri, persistent} f =
246 Synchronized.change print_functions (fn funs =>
247 (if not (AList.defined (op =) funs name) then ()
248 else warning ("Redefining command print function: " ^ quote name);
249 AList.update (op =) (name, (pri, persistent, f)) funs));
251 fun no_print_function name =
252 Synchronized.change print_functions (filter_out (equal name o #1));
257 print_function {name = "print_state", pri = 0, persistent = true}
258 (fn {command_name} => SOME (fn tr => fn st' =>
260 val is_init = Keyword.is_theory_begin command_name;
261 val is_proof = Keyword.is_proof command_name;
264 (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
265 in if do_print then Toplevel.print_state false st' else () end));
268 (* combined execution *)
270 type exec = eval * print list;
272 (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
274 fun exec_ids NONE = []
275 | exec_ids (SOME (Eval {exec_id, ...}, prints)) =
276 exec_id :: map (fn Print {exec_id, ...} => exec_id) prints;
280 fun run_print (Print {name, pri, print_process, ...}) =
281 (if Multithreading.enabled () then
282 memo_fork {name = name, group = NONE, deps = [], pri = pri, interrupts = true}
283 else memo_exec) print_process;
287 fun exec (Eval {eval_process, ...}, prints) =
288 (memo_exec eval_process; List.app run_print prints);