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