1 (* Title: Pure/PIDE/document.ML
4 Document as collection of named nodes, each consisting of an editable
5 list of commands, associated with asynchronous execution process.
10 val timing: bool Unsynchronized.ref
11 type node_header = string * Thy_Header.header * string list
12 type overlay = Document_ID.command * string * string list
14 Clear | (* FIXME unused !? *)
15 Edits of (Document_ID.command option * Document_ID.command option) list |
17 Perspective of bool * Document_ID.command list * overlay list
18 type edit = string * node_edit
21 val define_command: Document_ID.command -> string -> string -> state -> state
22 val remove_versions: Document_ID.version list -> state -> state
23 val start_execution: state -> state
24 val update: Document_ID.version -> Document_ID.version -> edit list -> state ->
25 Document_ID.exec list * (Document_ID.command * Document_ID.exec list) list * state
26 val state: unit -> state
27 val change_state: (state -> state) -> unit
30 structure Document: DOCUMENT =
33 val timing = Unsynchronized.ref false;
34 fun timeit msg e = cond_timeit (! timing) msg e;
38 (** document structure **)
40 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document_ID.print id);
41 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document_ID.print id);
43 type node_header = string * Thy_Header.header * string list;
46 {required: bool, (*required node*)
47 visible: Inttab.set, (*visible commands*)
48 visible_last: Document_ID.command option, (*last visible command*)
49 overlays: (string * string list) list Inttab.table}; (*command id -> print function with args*)
51 structure Entries = Linear_Set(type key = Document_ID.command val ord = int_ord);
53 abstype node = Node of
54 {header: node_header, (*master directory, theory header, errors*)
55 perspective: perspective, (*command perspective*)
56 entries: Command.exec option Entries.T, (*command entries with excecutions*)
57 result: Command.eval option} (*result of last execution*)
58 and version = Version of node String_Graph.T (*development graph wrt. static imports*)
61 fun make_node (header, perspective, entries, result) =
62 Node {header = header, perspective = perspective, entries = entries, result = result};
64 fun map_node f (Node {header, perspective, entries, result}) =
65 make_node (f (header, perspective, entries, result));
67 fun make_perspective (required, command_ids, overlays) : perspective =
69 visible = Inttab.make_set command_ids,
70 visible_last = try List.last command_ids,
71 overlays = Inttab.make_list (map (fn (x, y, z) => (x, (y, z))) overlays)};
73 val no_header = ("", Thy_Header.make ("", Position.none) [] [], ["Bad theory header"]);
74 val no_perspective = make_perspective (false, [], []);
76 val empty_node = make_node (no_header, no_perspective, Entries.empty, NONE);
77 val clear_node = map_node (fn (header, _, _, _) => (header, no_perspective, Entries.empty, NONE));
80 (* basic components *)
82 fun set_header header =
83 map_node (fn (_, perspective, entries, result) => (header, perspective, entries, result));
85 fun get_header (Node {header = (master, header, errors), ...}) =
86 if null errors then (master, header)
87 else error (cat_lines errors);
89 fun read_header node span =
91 val (dir, {name = (name, _), imports, keywords}) = get_header node;
92 val {name = (_, pos), imports = imports', ...} = Thy_Header.read_tokens span;
93 in (dir, Thy_Header.make (name, pos) (map #1 imports ~~ map #2 imports') keywords) end;
95 fun get_perspective (Node {perspective, ...}) = perspective;
96 fun set_perspective args =
97 map_node (fn (header, _, entries, result) => (header, make_perspective args, entries, result));
99 val required_node = #required o get_perspective;
100 val visible_command = Inttab.defined o #visible o get_perspective;
101 val visible_last = #visible_last o get_perspective;
102 val visible_node = is_some o visible_last
103 val overlays = #overlays o get_perspective;
106 map_node (fn (header, perspective, entries, result) => (header, perspective, f entries, result));
107 fun get_entries (Node {entries, ...}) = entries;
109 fun iterate_entries f = Entries.iterate NONE f o get_entries;
110 fun iterate_entries_after start f (Node {entries, ...}) =
111 (case Entries.get_after entries start of
113 | SOME id => Entries.iterate (SOME id) f entries);
115 fun get_result (Node {result, ...}) = result;
116 fun set_result result =
117 map_node (fn (header, perspective, entries, _) => (header, perspective, entries, result));
119 fun changed_result node node' =
120 (case (get_result node, get_result node') of
121 (SOME eval, SOME eval') => not (Command.eval_eq (eval, eval'))
122 | (NONE, NONE) => false
125 fun pending_result node =
126 (case get_result node of
127 SOME eval => not (Command.eval_finished eval)
130 fun get_node nodes name = String_Graph.get_node nodes name
131 handle String_Graph.UNDEF _ => empty_node;
132 fun default_node name = String_Graph.default_node (name, empty_node);
133 fun update_node name f = default_node name #> String_Graph.map_node name f;
136 (* node edits and associated executions *)
138 type overlay = Document_ID.command * string * string list;
142 Edits of (Document_ID.command option * Document_ID.command option) list |
143 Deps of node_header |
144 Perspective of bool * Document_ID.command list * overlay list;
146 type edit = string * node_edit;
148 val after_entry = Entries.get_after o get_entries;
150 fun lookup_entry node id =
151 (case Entries.lookup (get_entries node) id of
153 | SOME (exec, _) => exec);
155 fun the_entry node id =
156 (case Entries.lookup (get_entries node) id of
157 NONE => err_undef "command entry" id
158 | SOME (exec, _) => exec);
160 fun the_default_entry node (SOME id) = (id, the_default Command.no_exec (the_entry node id))
161 | the_default_entry _ NONE = (Document_ID.none, Command.no_exec);
163 fun assign_entry (command_id, exec) node =
164 if is_none (Entries.lookup (get_entries node) command_id) then node
165 else map_entries (Entries.update (command_id, exec)) node;
167 fun reset_after id entries =
168 (case Entries.get_after entries id of
170 | SOME next => Entries.update (next, NONE) entries);
172 val edit_node = map_entries o fold
173 (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
174 | (id, NONE) => Entries.delete_after id #> reset_after id);
177 (* version operations *)
179 val empty_version = Version String_Graph.empty;
181 fun nodes_of (Version nodes) = nodes;
182 val node_of = get_node o nodes_of;
184 fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
186 fun edit_nodes (name, node_edit) (Version nodes) =
189 Clear => update_node name clear_node nodes
190 | Edits edits => update_node name (edit_node edits) nodes
191 | Deps (master, header, errors) =>
193 val imports = map fst (#imports header);
195 (Thy_Header.define_keywords header; errors)
196 handle ERROR msg => errors @ [msg];
199 |> fold default_node imports;
201 |> String_Graph.Keys.fold
202 (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
203 val (nodes3, errors2) =
204 (String_Graph.add_deps_acyclic (name, imports) nodes2, errors1)
205 handle String_Graph.CYCLES cs => (nodes2, errors1 @ map cycle_msg cs);
206 in String_Graph.map_node name (set_header (master, header, errors2)) nodes3 end
207 | Perspective perspective => update_node name (set_perspective perspective) nodes);
209 fun put_node (name, node) (Version nodes) =
210 Version (update_node name (K node) nodes);
216 (** main state -- document structure and execution process **)
219 {version_id: Document_ID.version, (*static version id*)
220 execution_id: Document_ID.execution, (*dynamic execution id*)
221 delay_request: unit future, (*pending event timer request*)
222 frontier: Future.task Symtab.table}; (*node name -> running execution task*)
224 val no_execution: execution =
225 {version_id = Document_ID.none, execution_id = Document_ID.none,
226 delay_request = Future.value (), frontier = Symtab.empty};
228 fun new_execution version_id delay_request frontier : execution =
229 {version_id = version_id, execution_id = Execution.start (),
230 delay_request = delay_request, frontier = frontier};
232 abstype state = State of
233 {versions: version Inttab.table, (*version id -> document content*)
234 commands: (string * Token.T list lazy) Inttab.table, (*command id -> named command span*)
235 execution: execution} (*current execution process*)
238 fun make_state (versions, commands, execution) =
239 State {versions = versions, commands = commands, execution = execution};
241 fun map_state f (State {versions, commands, execution}) =
242 make_state (f (versions, commands, execution));
245 make_state (Inttab.make [(Document_ID.none, empty_version)], Inttab.empty, no_execution);
248 (* document versions *)
250 fun define_version version_id version =
251 map_state (fn (versions, commands, {delay_request, frontier, ...}) =>
253 val versions' = Inttab.update_new (version_id, version) versions
254 handle Inttab.DUP dup => err_dup "document version" dup;
255 val execution' = new_execution version_id delay_request frontier;
256 in (versions', commands, execution') end);
258 fun the_version (State {versions, ...}) version_id =
259 (case Inttab.lookup versions version_id of
260 NONE => err_undef "document version" version_id
261 | SOME version => version);
263 fun delete_version version_id versions =
264 Inttab.delete version_id versions
265 handle Inttab.UNDEF _ => err_undef "document version" version_id;
270 fun define_command command_id name text =
271 map_state (fn (versions, commands, execution) =>
273 val id = Document_ID.print command_id;
276 Position.setmp_thread_data (Position.id_only id)
277 (fn () => Thy_Syntax.parse_tokens (Keyword.get_lexicons ()) (Position.id id) text) ());
279 Position.setmp_thread_data (Position.id_only id)
280 (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
282 Inttab.update_new (command_id, (name, span)) commands
283 handle Inttab.DUP dup => err_dup "command" dup;
284 in (versions, commands', execution) end);
286 fun the_command (State {commands, ...}) command_id =
287 (case Inttab.lookup commands command_id of
288 NONE => err_undef "command" command_id
289 | SOME command => command);
291 val the_command_name = #1 oo the_command;
296 (* remove_versions *)
298 fun remove_versions version_ids state = state |> map_state (fn (versions, _, execution) =>
301 member (op =) version_ids (#version_id execution) andalso
302 error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
304 val versions' = fold delete_version version_ids versions;
306 (versions', Inttab.empty) |->
307 Inttab.fold (fn (_, version) => nodes_of version |>
308 String_Graph.fold (fn (_, (node, _)) => node |>
309 iterate_entries (fn ((_, command_id), _) =>
310 SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
311 in (versions', commands', execution) end);
314 (* document execution *)
316 fun start_execution state = state |> map_state (fn (versions, commands, execution) =>
317 timeit "Document.start_execution" (fn () =>
319 val {version_id, execution_id, delay_request, frontier} = execution;
321 val delay = seconds (Options.default_real "editor_execution_delay");
323 val _ = Future.cancel delay_request;
324 val delay_request' = Event_Timer.future (Time.+ (Time.now (), delay));
327 nodes_of (the_version state version_id) |> String_Graph.schedule
328 (fn deps => fn (name, node) =>
329 if visible_node node orelse pending_result node then
332 Future.task_of delay_request' :: the_list (Symtab.lookup frontier name);
334 iterate_entries (fn (_, opt_exec) => fn () =>
337 if Execution.is_running execution_id
338 then SOME (Command.exec execution_id exec)
340 | NONE => NONE)) node ()
341 handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn;
343 (singleton o Future.forks)
344 {name = "theory:" ^ name, group = SOME (Future.new_group NONE),
345 deps = more_deps @ map #2 (maps #2 deps),
346 pri = 0, interrupts = false} body;
347 in [(name, Future.task_of future)] end
349 val frontier' = (fold o fold) Symtab.update new_tasks frontier;
351 {version_id = version_id, execution_id = execution_id,
352 delay_request = delay_request', frontier = frontier'};
353 in (versions, commands, execution') end));
357 (** document update **)
359 (* exec state assignment *)
361 type assign_update = Command.exec option Inttab.table; (*command id -> exec*)
363 val assign_update_empty: assign_update = Inttab.empty;
364 fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
365 fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
367 fun assign_update_new upd (tab: assign_update) =
368 Inttab.update_new upd tab
369 handle Inttab.DUP dup => err_dup "exec state assignment" dup;
371 fun assign_update_result (tab: assign_update) =
372 Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
379 fun make_required nodes =
382 String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
383 |> String_Graph.all_preds nodes
386 val all_visible = all_preds visible_node;
387 val all_required = all_preds required_node;
389 Symtab.fold (fn (a, ()) =>
390 exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
391 Symtab.update (a, ())) all_visible all_required
394 fun init_theory deps node span =
396 (* FIXME provide files via Isabelle/Scala, not master_dir *)
397 val (dir, header) = read_header node span;
399 (case try Url.explode dir of
400 SOME (Url.File path) => path
401 | _ => Path.current);
402 val imports = #imports header;
404 imports |> map (fn (import, _) =>
405 (case Thy_Info.lookup_theory import of
408 Toplevel.end_theory (Position.file_only import)
409 (case get_result (snd (the (AList.lookup (op =) deps import))) of
410 NONE => Toplevel.toplevel
411 | SOME eval => Command.eval_result_state eval)));
412 val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
413 in Thy_Load.begin_theory master_dir header parents end;
415 fun check_theory full name node =
416 is_some (Thy_Info.lookup_theory name) orelse
417 can get_header node andalso (not full orelse is_some (get_result node));
419 fun last_common state node_required node0 node =
421 fun update_flags prev (visible, initial) =
423 val visible' = visible andalso prev <> visible_last node;
424 val initial' = initial andalso
427 | SOME command_id => not (Keyword.is_theory_begin (the_command_name state command_id)));
428 in (visible', initial') end;
430 fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
433 val flags' as (visible', _) = update_flags prev flags;
435 (case (lookup_entry node0 command_id, opt_exec) of
436 (SOME (eval0, _), SOME (eval, _)) =>
437 Command.eval_eq (eval0, eval) andalso
438 (visible' orelse node_required orelse Command.eval_running eval)
440 val assign_update' = assign_update |> ok' ?
442 SOME (eval, prints) =>
444 val command_visible = visible_command node command_id;
445 val command_name = the_command_name state command_id;
447 (case Command.print command_visible command_name eval prints of
448 SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
452 in SOME (prev, ok', flags', assign_update') end
454 val (common, ok, flags, assign_update') =
455 iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
456 val (common', flags') =
458 let val last = Entries.get_after (get_entries node) common
459 in (last, update_flags last flags) end
460 else (common, flags);
461 in (assign_update', common', flags') end;
463 fun illegal_init _ = error "Illegal theory header after end of theory";
465 fun new_exec state proper_init command_visible command_id' (assign_update, command_exec, init) =
466 if not proper_init andalso is_none init then NONE
469 val (_, (eval, _)) = command_exec;
470 val (command_name, span) = the_command state command_id' ||> Lazy.force;
472 val eval' = Command.eval (fn () => the_default illegal_init init span) span eval;
473 val prints' = perhaps (Command.print command_visible command_name eval') [];
474 val exec' = (eval', prints');
476 val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
477 val init' = if Keyword.is_theory_begin command_name then NONE else init;
478 in SOME (assign_update', (command_id', (eval', prints')), init') end;
480 fun removed_execs node0 (command_id, exec_ids) =
481 subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
485 fun update old_version_id new_version_id edits state =
487 val old_version = the_version state old_version_id;
488 val new_version = timeit "Document.edit_nodes" (fn () => fold edit_nodes edits old_version);
490 val nodes = nodes_of new_version;
491 val required = make_required nodes;
492 val required0 = make_required (nodes_of old_version);
493 val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
495 val updated = timeit "Document.update" (fn () =>
496 nodes |> String_Graph.schedule
497 (fn deps => fn (name, node) =>
498 (singleton o Future.forks)
499 {name = "Document.update", group = NONE,
500 deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
501 (fn () => timeit ("Document.update " ^ name) (fn () =>
503 val imports = map (apsnd Future.join) deps;
504 val imports_result_changed = exists (#4 o #1 o #2) imports;
505 val node_required = Symtab.defined required name;
507 if Symtab.defined edited name orelse visible_node node orelse
508 imports_result_changed orelse Symtab.defined required0 name <> node_required
511 val node0 = node_of old_version name;
512 val init = init_theory imports node;
514 check_theory false name node andalso
515 forall (fn (name, (_, node)) => check_theory true name node) imports;
517 val (print_execs, common, (still_visible, initial)) =
518 if imports_result_changed then (assign_update_empty, NONE, (true, true))
519 else last_common state node_required node0 node;
520 val common_command_exec = the_default_entry node common;
522 val (updated_execs, (command_id', (eval', _)), _) =
523 (print_execs, common_command_exec, if initial then SOME init else NONE)
524 |> (still_visible orelse node_required) ?
525 iterate_entries_after common
526 (fn ((prev, id), _) => fn res =>
527 if not node_required andalso prev = visible_last node then NONE
528 else new_exec state proper_init (visible_command node id) id res) node;
531 (node0, updated_execs) |-> iterate_entries_after common
532 (fn ((_, command_id0), exec0) => fn res =>
533 if is_none exec0 then NONE
534 else if assign_update_defined updated_execs command_id0 then SOME res
535 else SOME (assign_update_new (command_id0, NONE) res));
538 if command_id' = Document_ID.none then NONE else SOME command_id';
540 if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
543 val assign_update = assign_update_result assigned_execs;
544 val removed = maps (removed_execs node0) assign_update;
545 val _ = List.app Execution.cancel removed;
548 |> assign_update_apply assigned_execs
549 |> set_result result;
550 val assigned_node = SOME (name, node');
551 val result_changed = changed_result node0 node';
552 in ((removed, assign_update, assigned_node, result_changed), node') end
553 else (([], [], NONE, false), node)
555 |> Future.joins |> map #1);
557 val removed = maps #1 updated;
558 val assign_update = maps #2 updated;
559 val assigned_nodes = map_filter #3 updated;
562 |> define_version new_version_id (fold put_node assigned_nodes new_version);
564 in (removed, assign_update, state') end;
572 val global_state = Synchronized.var "Document.global_state" init_state;
574 fun state () = Synchronized.value global_state;
575 val change_state = Synchronized.change global_state;