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