de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
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 type node_header = string * Thy_Header.header * string list
12 Clear | (* FIXME unused !? *)
13 Edits of (Document_ID.command option * Document_ID.command option) list |
15 Perspective of Document_ID.command list
16 type edit = string * node_edit
19 val define_command: Document_ID.command -> string -> string -> state -> state
20 val remove_versions: Document_ID.version list -> state -> state
21 val start_execution: state -> state
22 val timing: bool Unsynchronized.ref
23 val update: Document_ID.version -> Document_ID.version -> edit list -> state ->
24 Document_ID.exec list * (Document_ID.command * Document_ID.exec list) list * state
25 val state: unit -> state
26 val change_state: (state -> state) -> unit
29 structure Document: DOCUMENT =
32 (** document structure **)
34 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document_ID.print id);
35 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document_ID.print id);
37 type node_header = string * Thy_Header.header * string list;
38 type perspective = Inttab.set * Document_ID.command option;
39 structure Entries = Linear_Set(type key = Document_ID.command val ord = int_ord);
41 abstype node = Node of
42 {header: node_header, (*master directory, theory header, errors*)
43 perspective: perspective, (*visible commands, last visible command*)
44 entries: Command.exec option Entries.T, (*command entries with excecutions*)
45 result: Command.eval option} (*result of last execution*)
46 and version = Version of node String_Graph.T (*development graph wrt. static imports*)
49 fun make_node (header, perspective, entries, result) =
50 Node {header = header, perspective = perspective, entries = entries, result = result};
52 fun map_node f (Node {header, perspective, entries, result}) =
53 make_node (f (header, perspective, entries, result));
55 fun make_perspective command_ids : perspective =
56 (Inttab.make_set command_ids, try List.last command_ids);
58 val no_header = ("", Thy_Header.make ("", Position.none) [] [], ["Bad theory header"]);
59 val no_perspective = make_perspective [];
61 val empty_node = make_node (no_header, no_perspective, Entries.empty, NONE);
62 val clear_node = map_node (fn (header, _, _, _) => (header, no_perspective, Entries.empty, NONE));
65 (* basic components *)
67 fun set_header header =
68 map_node (fn (_, perspective, entries, result) => (header, perspective, entries, result));
70 fun get_header (Node {header = (master, header, errors), ...}) =
71 if null errors then (master, header)
72 else error (cat_lines errors);
74 fun read_header node span =
76 val (dir, {name = (name, _), imports, keywords}) = get_header node;
77 val {name = (_, pos), imports = imports', ...} = Thy_Header.read_tokens span;
78 in (dir, Thy_Header.make (name, pos) (map #1 imports ~~ map #2 imports') keywords) end;
80 fun get_perspective (Node {perspective, ...}) = perspective;
81 fun set_perspective ids =
82 map_node (fn (header, _, entries, result) => (header, make_perspective ids, entries, result));
84 val visible_command = Inttab.defined o #1 o get_perspective;
85 val visible_last = #2 o get_perspective;
86 val visible_node = is_some o visible_last
89 map_node (fn (header, perspective, entries, result) => (header, perspective, f entries, result));
90 fun get_entries (Node {entries, ...}) = entries;
92 fun iterate_entries f = Entries.iterate NONE f o get_entries;
93 fun iterate_entries_after start f (Node {entries, ...}) =
94 (case Entries.get_after entries start of
96 | SOME id => Entries.iterate (SOME id) f entries);
98 fun get_result (Node {result, ...}) = result;
99 fun set_result result =
100 map_node (fn (header, perspective, entries, _) => (header, perspective, entries, result));
102 fun changed_result node node' =
103 (case (get_result node, get_result node') of
104 (SOME eval, SOME eval') => not (Command.eval_eq (eval, eval'))
105 | (NONE, NONE) => false
108 fun pending_result node =
109 (case get_result node of
110 SOME eval => not (Command.eval_finished eval)
113 fun get_node nodes name = String_Graph.get_node nodes name
114 handle String_Graph.UNDEF _ => empty_node;
115 fun default_node name = String_Graph.default_node (name, empty_node);
116 fun update_node name f = default_node name #> String_Graph.map_node name f;
119 (* node edits and associated executions *)
123 Edits of (Document_ID.command option * Document_ID.command option) list |
124 Deps of node_header |
125 Perspective of Document_ID.command list;
127 type edit = string * node_edit;
129 val after_entry = Entries.get_after o get_entries;
131 fun lookup_entry node id =
132 (case Entries.lookup (get_entries node) id of
134 | SOME (exec, _) => exec);
136 fun the_entry node id =
137 (case Entries.lookup (get_entries node) id of
138 NONE => err_undef "command entry" id
139 | SOME (exec, _) => exec);
141 fun the_default_entry node (SOME id) = (id, the_default Command.no_exec (the_entry node id))
142 | the_default_entry _ NONE = (Document_ID.none, Command.no_exec);
144 fun assign_entry (command_id, exec) node =
145 if is_none (Entries.lookup (get_entries node) command_id) then node
146 else map_entries (Entries.update (command_id, exec)) node;
148 fun reset_after id entries =
149 (case Entries.get_after entries id of
151 | SOME next => Entries.update (next, NONE) entries);
153 val edit_node = map_entries o fold
154 (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
155 | (id, NONE) => Entries.delete_after id #> reset_after id);
158 (* version operations *)
160 val empty_version = Version String_Graph.empty;
162 fun nodes_of (Version nodes) = nodes;
163 val node_of = get_node o nodes_of;
165 fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
167 fun edit_nodes (name, node_edit) (Version nodes) =
170 Clear => update_node name clear_node nodes
171 | Edits edits => update_node name (edit_node edits) nodes
172 | Deps (master, header, errors) =>
174 val imports = map fst (#imports header);
176 (Thy_Header.define_keywords header; errors)
177 handle ERROR msg => errors @ [msg];
180 |> fold default_node imports;
182 |> String_Graph.Keys.fold
183 (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
184 val (nodes3, errors2) =
185 (String_Graph.add_deps_acyclic (name, imports) nodes2, errors1)
186 handle String_Graph.CYCLES cs => (nodes2, errors1 @ map cycle_msg cs);
187 in String_Graph.map_node name (set_header (master, header, errors2)) nodes3 end
188 | Perspective perspective => update_node name (set_perspective perspective) nodes);
190 fun put_node (name, node) (Version nodes) =
191 Version (update_node name (K node) nodes);
197 (** main state -- document structure and execution process **)
200 {version_id: Document_ID.version, (*static version id*)
201 execution_id: Document_ID.execution, (*dynamic execution id*)
202 frontier: Future.task Symtab.table}; (*node name -> running execution task*)
204 val no_execution: execution =
205 {version_id = Document_ID.none, execution_id = Document_ID.none, frontier = Symtab.empty};
207 fun new_execution version_id frontier : execution =
208 {version_id = version_id, execution_id = Execution.start (), frontier = frontier};
210 abstype state = State of
211 {versions: version Inttab.table, (*version id -> document content*)
212 commands: (string * Token.T list lazy) Inttab.table, (*command id -> named command span*)
213 execution: execution} (*current execution process*)
216 fun make_state (versions, commands, execution) =
217 State {versions = versions, commands = commands, execution = execution};
219 fun map_state f (State {versions, commands, execution}) =
220 make_state (f (versions, commands, execution));
223 make_state (Inttab.make [(Document_ID.none, empty_version)], Inttab.empty, no_execution);
226 (* document versions *)
228 fun define_version version_id version =
229 map_state (fn (versions, commands, {frontier, ...}) =>
231 val versions' = Inttab.update_new (version_id, version) versions
232 handle Inttab.DUP dup => err_dup "document version" dup;
233 val execution' = new_execution version_id frontier;
234 in (versions', commands, execution') end);
236 fun the_version (State {versions, ...}) version_id =
237 (case Inttab.lookup versions version_id of
238 NONE => err_undef "document version" version_id
239 | SOME version => version);
241 fun delete_version version_id versions =
242 Inttab.delete version_id versions
243 handle Inttab.UNDEF _ => err_undef "document version" version_id;
248 fun define_command command_id name text =
249 map_state (fn (versions, commands, execution) =>
251 val id = Document_ID.print command_id;
254 Position.setmp_thread_data (Position.id_only id)
255 (fn () => Thy_Syntax.parse_tokens (Keyword.get_lexicons ()) (Position.id id) text) ());
257 Position.setmp_thread_data (Position.id_only id)
258 (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
260 Inttab.update_new (command_id, (name, span)) commands
261 handle Inttab.DUP dup => err_dup "command" dup;
262 in (versions, commands', execution) end);
264 fun the_command (State {commands, ...}) command_id =
265 (case Inttab.lookup commands command_id of
266 NONE => err_undef "command" command_id
267 | SOME command => command);
269 val the_command_name = #1 oo the_command;
274 (* remove_versions *)
276 fun remove_versions version_ids state = state |> map_state (fn (versions, _, execution) =>
279 member (op =) version_ids (#version_id execution) andalso
280 error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
282 val versions' = fold delete_version version_ids versions;
284 (versions', Inttab.empty) |->
285 Inttab.fold (fn (_, version) => nodes_of version |>
286 String_Graph.fold (fn (_, (node, _)) => node |>
287 iterate_entries (fn ((_, command_id), _) =>
288 SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
289 in (versions', commands', execution) end);
292 (* document execution *)
294 fun start_execution state = state |> map_state (fn (versions, commands, execution) =>
296 val {version_id, execution_id, frontier} = execution;
297 val pri = Options.default_int "editor_execution_priority";
300 if Execution.is_running execution_id then
301 nodes_of (the_version state version_id) |> String_Graph.schedule
302 (fn deps => fn (name, node) =>
303 if visible_node node orelse pending_result node then
305 val former_task = Symtab.lookup frontier name;
307 iterate_entries (fn (_, opt_exec) => fn () =>
310 if Execution.is_running execution_id
311 then SOME (Command.exec execution_id exec)
313 | NONE => NONE)) node ()
314 handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn;
316 (singleton o Future.forks)
317 {name = "theory:" ^ name, group = SOME (Future.new_group NONE),
318 deps = the_list former_task @ map #2 (maps #2 deps),
319 pri = pri, interrupts = false} body;
320 in [(name, Future.task_of future)] end
323 val frontier' = (fold o fold) Symtab.update new_tasks frontier;
324 val execution' = {version_id = version_id, execution_id = execution_id, frontier = frontier'};
325 in (versions, commands, execution') end);
329 (** document update **)
331 (* exec state assignment *)
333 type assign_update = Command.exec option Inttab.table; (*command id -> exec*)
335 val assign_update_empty: assign_update = Inttab.empty;
336 fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
337 fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
339 fun assign_update_new upd (tab: assign_update) =
340 Inttab.update_new upd tab
341 handle Inttab.DUP dup => err_dup "exec state assignment" dup;
343 fun assign_update_result (tab: assign_update) =
344 Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
349 val timing = Unsynchronized.ref false;
350 fun timeit msg e = cond_timeit (! timing) msg e;
354 fun make_required nodes =
357 String_Graph.fold (fn (a, (node, _)) => visible_node node ? cons a) nodes []
358 |> String_Graph.all_preds nodes
359 |> map (rpair ()) |> Symtab.make;
362 Symtab.fold (fn (a, ()) =>
363 exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
364 Symtab.update (a, ())) all_visible Symtab.empty;
367 fun init_theory deps node span =
369 (* FIXME provide files via Isabelle/Scala, not master_dir *)
370 val (dir, header) = read_header node span;
372 (case try Url.explode dir of
373 SOME (Url.File path) => path
374 | _ => Path.current);
375 val imports = #imports header;
377 imports |> map (fn (import, _) =>
378 (case Thy_Info.lookup_theory import of
381 Toplevel.end_theory (Position.file_only import)
382 (case get_result (snd (the (AList.lookup (op =) deps import))) of
383 NONE => Toplevel.toplevel
384 | SOME eval => Command.eval_result_state eval)));
385 val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
386 in Thy_Load.begin_theory master_dir header parents end;
388 fun check_theory full name node =
389 is_some (Thy_Info.lookup_theory name) orelse
390 can get_header node andalso (not full orelse is_some (get_result node));
392 fun last_common state node_required node0 node =
394 fun update_flags prev (visible, initial) =
396 val visible' = visible andalso prev <> visible_last node;
397 val initial' = initial andalso
400 | SOME command_id => not (Keyword.is_theory_begin (the_command_name state command_id)));
401 in (visible', initial') end;
403 fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
406 val flags' as (visible', _) = update_flags prev flags;
408 (case (lookup_entry node0 command_id, opt_exec) of
409 (SOME (eval0, _), SOME (eval, _)) =>
410 Command.eval_eq (eval0, eval) andalso
411 (visible' orelse node_required orelse Command.eval_running eval)
413 val assign_update' = assign_update |> ok' ?
415 SOME (eval, prints) =>
417 val command_visible = visible_command node command_id;
418 val command_name = the_command_name state command_id;
420 (case Command.print command_visible command_name eval prints of
421 SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
425 in SOME (prev, ok', flags', assign_update') end
427 val (common, ok, flags, assign_update') =
428 iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
429 val (common', flags') =
431 let val last = Entries.get_after (get_entries node) common
432 in (last, update_flags last flags) end
433 else (common, flags);
434 in (assign_update', common', flags') end;
436 fun illegal_init _ = error "Illegal theory header after end of theory";
438 fun new_exec state proper_init command_visible command_id' (assign_update, command_exec, init) =
439 if not proper_init andalso is_none init then NONE
442 val (_, (eval, _)) = command_exec;
443 val (command_name, span) = the_command state command_id' ||> Lazy.force;
445 val eval' = Command.eval (fn () => the_default illegal_init init span) span eval;
446 val prints' = perhaps (Command.print command_visible command_name eval') [];
447 val exec' = (eval', prints');
449 val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
450 val init' = if Keyword.is_theory_begin command_name then NONE else init;
451 in SOME (assign_update', (command_id', (eval', prints')), init') end;
453 fun removed_execs node0 (command_id, exec_ids) =
454 subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
458 fun update old_version_id new_version_id edits state =
460 val old_version = the_version state old_version_id;
461 val new_version = timeit "Document.edit_nodes" (fn () => fold edit_nodes edits old_version);
463 val nodes = nodes_of new_version;
464 val required = make_required nodes;
465 val required0 = make_required (nodes_of old_version);
466 val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
468 val updated = timeit "Document.update" (fn () =>
469 nodes |> String_Graph.schedule
470 (fn deps => fn (name, node) =>
471 (singleton o Future.forks)
472 {name = "Document.update", group = NONE,
473 deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
476 val imports = map (apsnd Future.join) deps;
477 val imports_result_changed = exists (#4 o #1 o #2) imports;
478 val node_required = Symtab.defined required name;
480 if Symtab.defined edited name orelse visible_node node orelse
481 imports_result_changed orelse Symtab.defined required0 name <> node_required
484 val node0 = node_of old_version name;
485 val init = init_theory imports node;
487 check_theory false name node andalso
488 forall (fn (name, (_, node)) => check_theory true name node) imports;
490 val (print_execs, common, (still_visible, initial)) =
491 if imports_result_changed then (assign_update_empty, NONE, (true, true))
492 else last_common state node_required node0 node;
493 val common_command_exec = the_default_entry node common;
495 val (updated_execs, (command_id', (eval', _)), _) =
496 (print_execs, common_command_exec, if initial then SOME init else NONE)
497 |> (still_visible orelse node_required) ?
498 iterate_entries_after common
499 (fn ((prev, id), _) => fn res =>
500 if not node_required andalso prev = visible_last node then NONE
501 else new_exec state proper_init (visible_command node id) id res) node;
504 (node0, updated_execs) |-> iterate_entries_after common
505 (fn ((_, command_id0), exec0) => fn res =>
506 if is_none exec0 then NONE
507 else if assign_update_defined updated_execs command_id0 then SOME res
508 else SOME (assign_update_new (command_id0, NONE) res));
511 if command_id' = Document_ID.none then NONE else SOME command_id';
513 if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
516 val assign_update = assign_update_result assigned_execs;
517 val removed = maps (removed_execs node0) assign_update;
518 val _ = List.app Execution.cancel removed;
521 |> assign_update_apply assigned_execs
522 |> set_result result;
523 val assigned_node = SOME (name, node');
524 val result_changed = changed_result node0 node';
525 in ((removed, assign_update, assigned_node, result_changed), node') end
526 else (([], [], NONE, false), node)
528 |> Future.joins |> map #1);
530 val removed = maps #1 updated;
531 val assign_update = maps #2 updated;
532 val assigned_nodes = map_filter #3 updated;
535 |> define_version new_version_id (fold put_node assigned_nodes new_version);
537 in (removed, assign_update, state') end;
545 val global_state = Synchronized.var "Document.global_state" init_state;
547 fun state () = Synchronized.value global_state;
548 val change_state = Synchronized.change global_state;