src/Pure/PIDE/document.ML
author wenzelm
Wed Jul 23 14:50:20 2014 +0200 (2014-07-23)
changeset 57616 50ab1db5c0fd
parent 57615 df1b3452d71c
child 57638 ed58e740a699
permissions -rw-r--r--
avoid redundant data structure;
     1 (*  Title:      Pure/PIDE/document.ML
     2     Author:     Makarius
     3 
     4 Document as collection of named nodes, each consisting of an editable
     5 list of commands, associated with asynchronous execution process.
     6 *)
     7 
     8 signature DOCUMENT =
     9 sig
    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)
    13   datatype node_edit =
    14     Edits of (Document_ID.command option * Document_ID.command option) list |
    15     Deps of node_header |
    16     Perspective of bool * Document_ID.command list * overlay list
    17   type edit = string * node_edit
    18   type state
    19   val init_state: state
    20   val define_blob: string -> string -> state -> state
    21   type blob_digest = (string * string option) Exn.result
    22   val define_command: Document_ID.command -> string -> blob_digest list -> string ->
    23     state -> state
    24   val remove_versions: Document_ID.version list -> state -> state
    25   val start_execution: state -> state
    26   val update: Document_ID.version -> Document_ID.version -> edit list -> state ->
    27     Document_ID.exec list * (Document_ID.command * Document_ID.exec list) list * state
    28   val state: unit -> state
    29   val change_state: (state -> state) -> unit
    30 end;
    31 
    32 structure Document: DOCUMENT =
    33 struct
    34 
    35 val timing = Unsynchronized.ref false;
    36 fun timeit msg e = cond_timeit (! timing) msg e;
    37 
    38 
    39 
    40 (** document structure **)
    41 
    42 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document_ID.print id);
    43 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document_ID.print id);
    44 
    45 type node_header = string * Thy_Header.header * string list;
    46 
    47 type perspective =
    48  {required: bool,  (*required node*)
    49   visible: Inttab.set,  (*visible commands*)
    50   visible_last: Document_ID.command option,  (*last visible command*)
    51   overlays: (string * string list) list Inttab.table};  (*command id -> print function with args*)
    52 
    53 structure Entries = Linear_Set(type key = Document_ID.command val ord = int_ord);
    54 
    55 abstype node = Node of
    56  {header: node_header,  (*master directory, theory header, errors*)
    57   perspective: perspective,  (*command perspective*)
    58   entries: Command.exec option Entries.T,  (*command entries with excecutions*)
    59   result: Command.eval option}  (*result of last execution*)
    60 and version = Version of node String_Graph.T  (*development graph wrt. static imports*)
    61 with
    62 
    63 fun make_node (header, perspective, entries, result) =
    64   Node {header = header, perspective = perspective, entries = entries, result = result};
    65 
    66 fun map_node f (Node {header, perspective, entries, result}) =
    67   make_node (f (header, perspective, entries, result));
    68 
    69 fun make_perspective (required, command_ids, overlays) : perspective =
    70  {required = required,
    71   visible = Inttab.make_set command_ids,
    72   visible_last = try List.last command_ids,
    73   overlays = Inttab.make_list overlays};
    74 
    75 val no_header = ("", Thy_Header.make ("", Position.none) [] [], []);
    76 val no_perspective = make_perspective (false, [], []);
    77 
    78 val empty_node = make_node (no_header, no_perspective, Entries.empty, NONE);
    79 
    80 fun is_no_perspective ({required, visible, visible_last, overlays}: perspective) =
    81   not required andalso
    82   Inttab.is_empty visible andalso
    83   is_none visible_last andalso
    84   Inttab.is_empty overlays;
    85 
    86 fun is_empty_node (Node {header, perspective, entries, result}) =
    87   header = no_header andalso
    88   is_no_perspective perspective andalso
    89   Entries.is_empty entries andalso
    90   is_none result;
    91 
    92 
    93 (* basic components *)
    94 
    95 fun master_directory (Node {header = (master, _, _), ...}) =
    96   (case try Url.explode master of
    97     SOME (Url.File path) => path
    98   | _ => Path.current);
    99 
   100 fun set_header header =
   101   map_node (fn (_, perspective, entries, result) => (header, perspective, entries, result));
   102 
   103 fun get_header (Node {header = (master, header, errors), ...}) =
   104   if null errors then (master, header)
   105   else error (cat_lines errors);
   106 
   107 fun read_header node span =
   108   let
   109     val {name = (name, _), imports, keywords} = #2 (get_header node);
   110     val {name = (_, pos), imports = imports', ...} = Thy_Header.read_tokens span;
   111   in Thy_Header.make (name, pos) (map #1 imports ~~ map #2 imports') keywords end;
   112 
   113 fun get_perspective (Node {perspective, ...}) = perspective;
   114 fun set_perspective args =
   115   map_node (fn (header, _, entries, result) => (header, make_perspective args, entries, result));
   116 
   117 val required_node = #required o get_perspective;
   118 val visible_command = Inttab.defined o #visible o get_perspective;
   119 val visible_last = #visible_last o get_perspective;
   120 val visible_node = is_some o visible_last
   121 val overlays = Inttab.lookup_list o #overlays o get_perspective;
   122 
   123 fun map_entries f =
   124   map_node (fn (header, perspective, entries, result) => (header, perspective, f entries, result));
   125 fun get_entries (Node {entries, ...}) = entries;
   126 
   127 fun iterate_entries f = Entries.iterate NONE f o get_entries;
   128 fun iterate_entries_after start f (Node {entries, ...}) =
   129   (case Entries.get_after entries start of
   130     NONE => I
   131   | SOME id => Entries.iterate (SOME id) f entries);
   132 
   133 fun get_result (Node {result, ...}) = result;
   134 fun set_result result =
   135   map_node (fn (header, perspective, entries, _) => (header, perspective, entries, result));
   136 
   137 fun changed_result node node' =
   138   (case (get_result node, get_result node') of
   139     (SOME eval, SOME eval') => not (Command.eval_eq (eval, eval'))
   140   | (NONE, NONE) => false
   141   | _ => true);
   142 
   143 fun pending_result node =
   144   (case get_result node of
   145     SOME eval => not (Command.eval_finished eval)
   146   | NONE => false);
   147 
   148 fun get_node nodes name = String_Graph.get_node nodes name
   149   handle String_Graph.UNDEF _ => empty_node;
   150 fun default_node name = String_Graph.default_node (name, empty_node);
   151 fun update_node name f = default_node name #> String_Graph.map_node name f;
   152 
   153 
   154 (* node edits and associated executions *)
   155 
   156 type overlay = Document_ID.command * (string * string list);
   157 
   158 datatype node_edit =
   159   Edits of (Document_ID.command option * Document_ID.command option) list |
   160   Deps of node_header |
   161   Perspective of bool * Document_ID.command list * overlay list;
   162 
   163 type edit = string * node_edit;
   164 
   165 val after_entry = Entries.get_after o get_entries;
   166 
   167 fun lookup_entry node id =
   168   (case Entries.lookup (get_entries node) id of
   169     NONE => NONE
   170   | SOME (exec, _) => exec);
   171 
   172 fun the_entry node id =
   173   (case Entries.lookup (get_entries node) id of
   174     NONE => err_undef "command entry" id
   175   | SOME (exec, _) => exec);
   176 
   177 fun the_default_entry node (SOME id) = (id, the_default Command.no_exec (the_entry node id))
   178   | the_default_entry _ NONE = (Document_ID.none, Command.no_exec);
   179 
   180 fun assign_entry (command_id, exec) node =
   181   if is_none (Entries.lookup (get_entries node) command_id) then node
   182   else map_entries (Entries.update (command_id, exec)) node;
   183 
   184 fun reset_after id entries =
   185   (case Entries.get_after entries id of
   186     NONE => entries
   187   | SOME next => Entries.update (next, NONE) entries);
   188 
   189 val edit_node = map_entries o fold
   190   (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
   191     | (id, NONE) => Entries.delete_after id #> reset_after id);
   192 
   193 
   194 (* version operations *)
   195 
   196 val empty_version = Version String_Graph.empty;
   197 
   198 fun nodes_of (Version nodes) = nodes;
   199 val node_of = get_node o nodes_of;
   200 
   201 fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
   202 
   203 fun edit_nodes (name, node_edit) (Version nodes) =
   204   Version
   205     (case node_edit of
   206       Edits edits => update_node name (edit_node edits) nodes
   207     | Deps (master, header, errors) =>
   208         let
   209           val imports = map fst (#imports header);
   210           val errors1 =
   211             (Thy_Header.define_keywords header; errors)
   212               handle ERROR msg => errors @ [msg];
   213           val nodes1 = nodes
   214             |> default_node name
   215             |> fold default_node imports;
   216           val nodes2 = nodes1
   217             |> String_Graph.Keys.fold
   218                 (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
   219           val (nodes3, errors2) =
   220             (String_Graph.add_deps_acyclic (name, imports) nodes2, errors1)
   221               handle String_Graph.CYCLES cs => (nodes2, errors1 @ map cycle_msg cs);
   222         in String_Graph.map_node name (set_header (master, header, errors2)) nodes3 end
   223     | Perspective perspective => update_node name (set_perspective perspective) nodes);
   224 
   225 fun put_node (name, node) (Version nodes) =
   226   let
   227     val nodes1 = update_node name (K node) nodes;
   228     val nodes2 =
   229       if String_Graph.is_maximal nodes1 name andalso is_empty_node node
   230       then String_Graph.del_node name nodes1
   231       else nodes1;
   232   in Version nodes2 end;
   233 
   234 end;
   235 
   236 
   237 
   238 (** main state -- document structure and execution process **)
   239 
   240 type blob_digest = (string * string option) Exn.result;  (*file node name, raw digest*)
   241 
   242 type execution =
   243  {version_id: Document_ID.version,  (*static version id*)
   244   execution_id: Document_ID.execution,  (*dynamic execution id*)
   245   delay_request: unit future,  (*pending event timer request*)
   246   frontier: Future.task Symtab.table};  (*node name -> running execution task*)
   247 
   248 val no_execution: execution =
   249   {version_id = Document_ID.none, execution_id = Document_ID.none,
   250    delay_request = Future.value (), frontier = Symtab.empty};
   251 
   252 fun new_execution version_id delay_request frontier : execution =
   253   {version_id = version_id, execution_id = Execution.start (),
   254    delay_request = delay_request, frontier = frontier};
   255 
   256 abstype state = State of
   257  {versions: version Inttab.table,  (*version id -> document content*)
   258   blobs: (SHA1.digest * string list) Symtab.table,  (*raw digest -> digest, lines*)
   259   commands: (string * blob_digest list * Token.T list lazy) Inttab.table,
   260     (*command id -> name, inlined files, command span*)
   261   execution: execution}  (*current execution process*)
   262 with
   263 
   264 fun make_state (versions, blobs, commands, execution) =
   265   State {versions = versions, blobs = blobs, commands = commands, execution = execution};
   266 
   267 fun map_state f (State {versions, blobs, commands, execution}) =
   268   make_state (f (versions, blobs, commands, execution));
   269 
   270 val init_state =
   271   make_state (Inttab.make [(Document_ID.none, empty_version)],
   272     Symtab.empty, Inttab.empty, no_execution);
   273 
   274 
   275 (* document versions *)
   276 
   277 fun define_version version_id version =
   278   map_state (fn (versions, blobs, commands, {delay_request, frontier, ...}) =>
   279     let
   280       val versions' = Inttab.update_new (version_id, version) versions
   281         handle Inttab.DUP dup => err_dup "document version" dup;
   282       val execution' = new_execution version_id delay_request frontier;
   283     in (versions', blobs, commands, execution') end);
   284 
   285 fun the_version (State {versions, ...}) version_id =
   286   (case Inttab.lookup versions version_id of
   287     NONE => err_undef "document version" version_id
   288   | SOME version => version);
   289 
   290 fun delete_version version_id versions =
   291   Inttab.delete version_id versions
   292     handle Inttab.UNDEF _ => err_undef "document version" version_id;
   293 
   294 
   295 (* inlined files *)
   296 
   297 fun define_blob digest text =
   298   map_state (fn (versions, blobs, commands, execution) =>
   299     let
   300       val sha1_digest = SHA1.digest text;
   301       val _ =
   302         digest = SHA1.rep sha1_digest orelse
   303           error ("Ill-defined blob: bad SHA1 digest " ^ digest ^ " vs. " ^ SHA1.rep sha1_digest);
   304       val blobs' = Symtab.update (digest, (sha1_digest, split_lines text)) blobs;
   305     in (versions, blobs', commands, execution) end);
   306 
   307 fun the_blob (State {blobs, ...}) digest =
   308   (case Symtab.lookup blobs digest of
   309     NONE => error ("Undefined blob: " ^ digest)
   310   | SOME content => content);
   311 
   312 fun resolve_blob state (blob_digest: blob_digest) =
   313   blob_digest |> Exn.map_result (fn (file_node, raw_digest) =>
   314     (file_node, Option.map (the_blob state) raw_digest));
   315 
   316 
   317 (* commands *)
   318 
   319 fun define_command command_id name command_blobs text =
   320   map_state (fn (versions, blobs, commands, execution) =>
   321     let
   322       val id = Document_ID.print command_id;
   323       val span =
   324         Lazy.lazy (fn () =>
   325           Position.setmp_thread_data (Position.id_only id)
   326             (fn () => Thy_Syntax.parse_tokens (Keyword.get_lexicons ()) (Position.id id) text) ());
   327       val _ =
   328         Position.setmp_thread_data (Position.id_only id)
   329           (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
   330       val commands' =
   331         Inttab.update_new (command_id, (name, command_blobs, span)) commands
   332           handle Inttab.DUP dup => err_dup "command" dup;
   333     in (versions, blobs, commands', execution) end);
   334 
   335 fun the_command (State {commands, ...}) command_id =
   336   (case Inttab.lookup commands command_id of
   337     NONE => err_undef "command" command_id
   338   | SOME command => command);
   339 
   340 val the_command_name = #1 oo the_command;
   341 
   342 end;
   343 
   344 
   345 (* remove_versions *)
   346 
   347 fun remove_versions version_ids state = state |> map_state (fn (versions, _, _, execution) =>
   348   let
   349     val _ =
   350       member (op =) version_ids (#version_id execution) andalso
   351         error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
   352 
   353     val versions' = fold delete_version version_ids versions;
   354     val commands' =
   355       (versions', Inttab.empty) |->
   356         Inttab.fold (fn (_, version) => nodes_of version |>
   357           String_Graph.fold (fn (_, (node, _)) => node |>
   358             iterate_entries (fn ((_, command_id), _) =>
   359               SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
   360     val blobs' =
   361       (commands', Symtab.empty) |->
   362         Inttab.fold (fn (_, (_, blobs, _)) => blobs |>
   363           fold (fn Exn.Res (_, SOME b) => Symtab.update (b, the_blob state b) | _ => I));
   364 
   365   in (versions', blobs', commands', execution) end);
   366 
   367 
   368 (* document execution *)
   369 
   370 fun start_execution state = state |> map_state (fn (versions, blobs, commands, execution) =>
   371   timeit "Document.start_execution" (fn () =>
   372     let
   373       val {version_id, execution_id, delay_request, frontier} = execution;
   374 
   375       val delay = seconds (Options.default_real "editor_execution_delay");
   376 
   377       val _ = Future.cancel delay_request;
   378       val delay_request' = Event_Timer.future (Time.+ (Time.now (), delay));
   379 
   380       val new_tasks =
   381         nodes_of (the_version state version_id) |> String_Graph.schedule
   382           (fn deps => fn (name, node) =>
   383             if visible_node node orelse pending_result node then
   384               let
   385                 val more_deps =
   386                   Future.task_of delay_request' :: the_list (Symtab.lookup frontier name);
   387                 fun body () =
   388                   iterate_entries (fn (_, opt_exec) => fn () =>
   389                     (case opt_exec of
   390                       SOME exec =>
   391                         if Execution.is_running execution_id
   392                         then SOME (Command.exec execution_id exec)
   393                         else NONE
   394                     | NONE => NONE)) node ()
   395                   handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn;
   396                 val future =
   397                   (singleton o Future.forks)
   398                     {name = "theory:" ^ name, group = SOME (Future.new_group NONE),
   399                       deps = more_deps @ map #2 (maps #2 deps),
   400                       pri = 0, interrupts = false} body;
   401               in [(name, Future.task_of future)] end
   402             else []);
   403       val frontier' = (fold o fold) Symtab.update new_tasks frontier;
   404       val execution' =
   405         {version_id = version_id, execution_id = execution_id,
   406          delay_request = delay_request', frontier = frontier'};
   407     in (versions, blobs, commands, execution') end));
   408 
   409 
   410 
   411 (** document update **)
   412 
   413 (* exec state assignment *)
   414 
   415 type assign_update = Command.exec option Inttab.table;  (*command id -> exec*)
   416 
   417 val assign_update_empty: assign_update = Inttab.empty;
   418 fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
   419 fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
   420 
   421 fun assign_update_new upd (tab: assign_update) =
   422   Inttab.update_new upd tab
   423     handle Inttab.DUP dup => err_dup "exec state assignment" dup;
   424 
   425 fun assign_update_result (tab: assign_update) =
   426   Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
   427 
   428 
   429 (* update *)
   430 
   431 local
   432 
   433 fun make_required nodes =
   434   let
   435     fun all_preds P =
   436       String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
   437       |> String_Graph.all_preds nodes
   438       |> Symtab.make_set;
   439 
   440     val all_visible = all_preds visible_node;
   441     val all_required = all_preds required_node;
   442   in
   443     Symtab.fold (fn (a, ()) =>
   444       exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
   445         Symtab.update (a, ())) all_visible all_required
   446   end;
   447 
   448 fun loaded_theory name =
   449   (case try (unsuffix ".thy") name of
   450     SOME a => get_first Thy_Info.lookup_theory [a, Long_Name.base_name a]
   451   | NONE => NONE);
   452 
   453 fun init_theory deps node span =
   454   let
   455     val master_dir = master_directory node;
   456     val header = read_header node span;
   457     val imports = #imports header;
   458     val parents =
   459       imports |> map (fn (import, _) =>
   460         (case loaded_theory import of
   461           SOME thy => thy
   462         | NONE =>
   463             Toplevel.end_theory (Position.file_only import)
   464               (case get_result (snd (the (AList.lookup (op =) deps import))) of
   465                 NONE => Toplevel.toplevel
   466               | SOME eval => Command.eval_result_state eval)));
   467     val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
   468   in Resources.begin_theory master_dir header parents end;
   469 
   470 fun check_theory full name node =
   471   is_some (loaded_theory name) orelse
   472   can get_header node andalso (not full orelse is_some (get_result node));
   473 
   474 fun last_common state node_required node0 node =
   475   let
   476     fun update_flags prev (visible, initial) =
   477       let
   478         val visible' = visible andalso prev <> visible_last node;
   479         val initial' = initial andalso
   480           (case prev of
   481             NONE => true
   482           | SOME command_id => not (Keyword.is_theory_begin (the_command_name state command_id)));
   483       in (visible', initial') end;
   484 
   485     fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
   486       if ok then
   487         let
   488           val flags' as (visible', _) = update_flags prev flags;
   489           val ok' =
   490             (case (lookup_entry node0 command_id, opt_exec) of
   491               (SOME (eval0, _), SOME (eval, _)) =>
   492                 Command.eval_eq (eval0, eval) andalso
   493                   (visible' orelse node_required orelse Command.eval_running eval)
   494             | _ => false);
   495           val assign_update' = assign_update |> ok' ?
   496             (case opt_exec of
   497               SOME (eval, prints) =>
   498                 let
   499                   val command_visible = visible_command node command_id;
   500                   val command_overlays = overlays node command_id;
   501                   val command_name = the_command_name state command_id;
   502                 in
   503                   (case Command.print command_visible command_overlays command_name eval prints of
   504                     SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
   505                   | NONE => I)
   506                 end
   507             | NONE => I);
   508         in SOME (prev, ok', flags', assign_update') end
   509       else NONE;
   510     val (common, ok, flags, assign_update') =
   511       iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
   512     val (common', flags') =
   513       if ok then
   514         let val last = Entries.get_after (get_entries node) common
   515         in (last, update_flags last flags) end
   516       else (common, flags);
   517   in (assign_update', common', flags') end;
   518 
   519 fun illegal_init _ = error "Illegal theory header after end of theory";
   520 
   521 fun new_exec state node proper_init command_id' (assign_update, command_exec, init) =
   522   if not proper_init andalso is_none init then NONE
   523   else
   524     let
   525       val (_, (eval, _)) = command_exec;
   526 
   527       val command_visible = visible_command node command_id';
   528       val command_overlays = overlays node command_id';
   529       val (command_name, blob_digests, span0) = the_command state command_id';
   530       val blobs = map (resolve_blob state) blob_digests;
   531       val span = Lazy.force span0;
   532 
   533       val eval' =
   534         Command.eval (fn () => the_default illegal_init init span)
   535           (master_directory node) blobs span eval;
   536       val prints' = perhaps (Command.print command_visible command_overlays command_name eval') [];
   537       val exec' = (eval', prints');
   538 
   539       val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
   540       val init' = if Keyword.is_theory_begin command_name then NONE else init;
   541     in SOME (assign_update', (command_id', (eval', prints')), init') end;
   542 
   543 fun removed_execs node0 (command_id, exec_ids) =
   544   subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
   545 
   546 in
   547 
   548 fun update old_version_id new_version_id edits state =
   549   let
   550     val old_version = the_version state old_version_id;
   551     val new_version = timeit "Document.edit_nodes" (fn () => fold edit_nodes edits old_version);
   552 
   553     val nodes = nodes_of new_version;
   554     val required = make_required nodes;
   555     val required0 = make_required (nodes_of old_version);
   556     val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
   557 
   558     val updated = timeit "Document.update" (fn () =>
   559       nodes |> String_Graph.schedule
   560         (fn deps => fn (name, node) =>
   561           (singleton o Future.forks)
   562             {name = "Document.update", group = NONE,
   563               deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
   564             (fn () => timeit ("Document.update " ^ name) (fn () =>
   565               let
   566                 val imports = map (apsnd Future.join) deps;
   567                 val imports_result_changed = exists (#4 o #1 o #2) imports;
   568                 val node_required = Symtab.defined required name;
   569               in
   570                 if Symtab.defined edited name orelse visible_node node orelse
   571                   imports_result_changed orelse Symtab.defined required0 name <> node_required
   572                 then
   573                   let
   574                     val node0 = node_of old_version name;
   575                     val init = init_theory imports node;
   576                     val proper_init =
   577                       check_theory false name node andalso
   578                       forall (fn (name, (_, node)) => check_theory true name node) imports;
   579 
   580                     val (print_execs, common, (still_visible, initial)) =
   581                       if imports_result_changed then (assign_update_empty, NONE, (true, true))
   582                       else last_common state node_required node0 node;
   583                     val common_command_exec = the_default_entry node common;
   584 
   585                     val (updated_execs, (command_id', (eval', _)), _) =
   586                       (print_execs, common_command_exec, if initial then SOME init else NONE)
   587                       |> (still_visible orelse node_required) ?
   588                         iterate_entries_after common
   589                           (fn ((prev, id), _) => fn res =>
   590                             if not node_required andalso prev = visible_last node then NONE
   591                             else new_exec state node proper_init id res) node;
   592 
   593                     val assigned_execs =
   594                       (node0, updated_execs) |-> iterate_entries_after common
   595                         (fn ((_, command_id0), exec0) => fn res =>
   596                           if is_none exec0 then NONE
   597                           else if assign_update_defined updated_execs command_id0 then SOME res
   598                           else SOME (assign_update_new (command_id0, NONE) res));
   599 
   600                     val last_exec =
   601                       if command_id' = Document_ID.none then NONE else SOME command_id';
   602                     val result =
   603                       if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
   604                       else SOME eval';
   605 
   606                     val assign_update = assign_update_result assigned_execs;
   607                     val removed = maps (removed_execs node0) assign_update;
   608                     val _ = List.app Execution.cancel removed;
   609 
   610                     val node' = node
   611                       |> assign_update_apply assigned_execs
   612                       |> set_result result;
   613                     val assigned_node = SOME (name, node');
   614                     val result_changed = changed_result node0 node';
   615                   in ((removed, assign_update, assigned_node, result_changed), node') end
   616                 else (([], [], NONE, false), node)
   617               end)))
   618       |> Future.joins |> map #1);
   619 
   620     val removed = maps #1 updated;
   621     val assign_update = maps #2 updated;
   622     val assigned_nodes = map_filter #3 updated;
   623 
   624     val state' = state
   625       |> define_version new_version_id (fold put_node assigned_nodes new_version);
   626 
   627   in (removed, assign_update, state') end;
   628 
   629 end;
   630 
   631 
   632 
   633 (** global state **)
   634 
   635 val global_state = Synchronized.var "Document.global_state" init_state;
   636 
   637 fun state () = Synchronized.value global_state;
   638 val change_state = Synchronized.change global_state;
   639 
   640 end;
   641