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