src/Pure/PIDE/document.ML
author wenzelm
Thu Feb 27 17:29:58 2014 +0100 (2014-02-27)
changeset 55788 67699e08e969
parent 54562 301a721af68b
child 55798 985bd3a325ab
permissions -rw-r--r--
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
integrity check of SHA1 digests produced in Scala vs. ML;
tuned signature;
     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_digest 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: (SHA1.digest * string list) Symtab.table,  (*digest -> digest, lines*)
   238   commands: (string * Command.blob_digest 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
   279       val sha1_digest = SHA1.digest text;
   280       val _ =
   281         digest = SHA1.rep sha1_digest orelse
   282           error ("Ill-defined blob: bad SHA1 digest " ^ digest ^ " vs. " ^ SHA1.rep sha1_digest);
   283       val blobs' = Symtab.update (digest, (sha1_digest, split_lines text)) blobs;
   284     in (versions, blobs', commands, execution) end);
   285 
   286 fun the_blob (State {blobs, ...}) digest =
   287   (case Symtab.lookup blobs digest of
   288     NONE => error ("Undefined blob: " ^ digest)
   289   | SOME content => content);
   290 
   291 
   292 (* commands *)
   293 
   294 fun define_command command_id name command_blobs text =
   295   map_state (fn (versions, blobs, commands, execution) =>
   296     let
   297       val id = Document_ID.print command_id;
   298       val span =
   299         Lazy.lazy (fn () =>
   300           Position.setmp_thread_data (Position.id_only id)
   301             (fn () => Thy_Syntax.parse_tokens (Keyword.get_lexicons ()) (Position.id id) text) ());
   302       val _ =
   303         Position.setmp_thread_data (Position.id_only id)
   304           (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
   305       val commands' =
   306         Inttab.update_new (command_id, (name, command_blobs, span)) commands
   307           handle Inttab.DUP dup => err_dup "command" dup;
   308     in (versions, blobs, commands', execution) end);
   309 
   310 fun the_command (State {commands, ...}) command_id =
   311   (case Inttab.lookup commands command_id of
   312     NONE => err_undef "command" command_id
   313   | SOME command => command);
   314 
   315 val the_command_name = #1 oo the_command;
   316 
   317 end;
   318 
   319 
   320 (* remove_versions *)
   321 
   322 fun remove_versions version_ids state = state |> map_state (fn (versions, _, _, execution) =>
   323   let
   324     val _ =
   325       member (op =) version_ids (#version_id execution) andalso
   326         error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
   327 
   328     val versions' = fold delete_version version_ids versions;
   329     val commands' =
   330       (versions', Inttab.empty) |->
   331         Inttab.fold (fn (_, version) => nodes_of version |>
   332           String_Graph.fold (fn (_, (node, _)) => node |>
   333             iterate_entries (fn ((_, command_id), _) =>
   334               SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
   335     val blobs' =
   336       (commands', Symtab.empty) |->
   337         Inttab.fold (fn (_, (_, blobs, _)) => blobs |>
   338           fold (fn Exn.Res (_, SOME b) => Symtab.update (b, the_blob state b) | _ => I));
   339 
   340   in (versions', blobs', commands', execution) end);
   341 
   342 
   343 (* document execution *)
   344 
   345 fun start_execution state = state |> map_state (fn (versions, blobs, commands, execution) =>
   346   timeit "Document.start_execution" (fn () =>
   347     let
   348       val {version_id, execution_id, delay_request, frontier} = execution;
   349 
   350       val delay = seconds (Options.default_real "editor_execution_delay");
   351 
   352       val _ = Future.cancel delay_request;
   353       val delay_request' = Event_Timer.future (Time.+ (Time.now (), delay));
   354 
   355       val new_tasks =
   356         nodes_of (the_version state version_id) |> String_Graph.schedule
   357           (fn deps => fn (name, node) =>
   358             if visible_node node orelse pending_result node then
   359               let
   360                 val more_deps =
   361                   Future.task_of delay_request' :: the_list (Symtab.lookup frontier name);
   362                 fun body () =
   363                   iterate_entries (fn (_, opt_exec) => fn () =>
   364                     (case opt_exec of
   365                       SOME exec =>
   366                         if Execution.is_running execution_id
   367                         then SOME (Command.exec execution_id exec)
   368                         else NONE
   369                     | NONE => NONE)) node ()
   370                   handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn;
   371                 val future =
   372                   (singleton o Future.forks)
   373                     {name = "theory:" ^ name, group = SOME (Future.new_group NONE),
   374                       deps = more_deps @ map #2 (maps #2 deps),
   375                       pri = 0, interrupts = false} body;
   376               in [(name, Future.task_of future)] end
   377             else []);
   378       val frontier' = (fold o fold) Symtab.update new_tasks frontier;
   379       val execution' =
   380         {version_id = version_id, execution_id = execution_id,
   381          delay_request = delay_request', frontier = frontier'};
   382     in (versions, blobs, commands, execution') end));
   383 
   384 
   385 
   386 (** document update **)
   387 
   388 (* exec state assignment *)
   389 
   390 type assign_update = Command.exec option Inttab.table;  (*command id -> exec*)
   391 
   392 val assign_update_empty: assign_update = Inttab.empty;
   393 fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
   394 fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
   395 
   396 fun assign_update_new upd (tab: assign_update) =
   397   Inttab.update_new upd tab
   398     handle Inttab.DUP dup => err_dup "exec state assignment" dup;
   399 
   400 fun assign_update_result (tab: assign_update) =
   401   Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
   402 
   403 
   404 (* update *)
   405 
   406 local
   407 
   408 fun make_required nodes =
   409   let
   410     fun all_preds P =
   411       String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
   412       |> String_Graph.all_preds nodes
   413       |> Symtab.make_set;
   414 
   415     val all_visible = all_preds visible_node;
   416     val all_required = all_preds required_node;
   417   in
   418     Symtab.fold (fn (a, ()) =>
   419       exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
   420         Symtab.update (a, ())) all_visible all_required
   421   end;
   422 
   423 fun loaded_theory name =
   424   (case try (unsuffix ".thy") name of
   425     SOME a => Thy_Info.lookup_theory a
   426   | NONE => NONE);
   427 
   428 fun init_theory deps node span =
   429   let
   430     val master_dir = master_directory node;
   431     val header = read_header node span;
   432     val imports = #imports header;
   433     val parents =
   434       imports |> map (fn (import, _) =>
   435         (case loaded_theory import of
   436           SOME thy => thy
   437         | NONE =>
   438             Toplevel.end_theory (Position.file_only import)
   439               (case get_result (snd (the (AList.lookup (op =) deps import))) of
   440                 NONE => Toplevel.toplevel
   441               | SOME eval => Command.eval_result_state eval)));
   442     val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
   443   in Thy_Load.begin_theory master_dir header parents end;
   444 
   445 fun check_theory full name node =
   446   is_some (loaded_theory name) orelse
   447   can get_header node andalso (not full orelse is_some (get_result node));
   448 
   449 fun last_common state node_required node0 node =
   450   let
   451     fun update_flags prev (visible, initial) =
   452       let
   453         val visible' = visible andalso prev <> visible_last node;
   454         val initial' = initial andalso
   455           (case prev of
   456             NONE => true
   457           | SOME command_id => not (Keyword.is_theory_begin (the_command_name state command_id)));
   458       in (visible', initial') end;
   459 
   460     fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
   461       if ok then
   462         let
   463           val flags' as (visible', _) = update_flags prev flags;
   464           val ok' =
   465             (case (lookup_entry node0 command_id, opt_exec) of
   466               (SOME (eval0, _), SOME (eval, _)) =>
   467                 Command.eval_eq (eval0, eval) andalso
   468                   (visible' orelse node_required orelse Command.eval_running eval)
   469             | _ => false);
   470           val assign_update' = assign_update |> ok' ?
   471             (case opt_exec of
   472               SOME (eval, prints) =>
   473                 let
   474                   val command_visible = visible_command node command_id;
   475                   val command_overlays = overlays node command_id;
   476                   val command_name = the_command_name state command_id;
   477                 in
   478                   (case Command.print command_visible command_overlays command_name eval prints of
   479                     SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
   480                   | NONE => I)
   481                 end
   482             | NONE => I);
   483         in SOME (prev, ok', flags', assign_update') end
   484       else NONE;
   485     val (common, ok, flags, assign_update') =
   486       iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
   487     val (common', flags') =
   488       if ok then
   489         let val last = Entries.get_after (get_entries node) common
   490         in (last, update_flags last flags) end
   491       else (common, flags);
   492   in (assign_update', common', flags') end;
   493 
   494 fun illegal_init _ = error "Illegal theory header after end of theory";
   495 
   496 fun new_exec state node proper_init command_id' (assign_update, command_exec, init) =
   497   if not proper_init andalso is_none init then NONE
   498   else
   499     let
   500       val (_, (eval, _)) = command_exec;
   501 
   502       val command_visible = visible_command node command_id';
   503       val command_overlays = overlays node command_id';
   504       val (command_name, blob_digests, span0) = the_command state command_id';
   505       val blobs = (map o Exn.map_result o apsnd o Option.map) (the_blob state) blob_digests;
   506       val span = Lazy.force span0;
   507 
   508       val eval' =
   509         Command.eval (fn () => the_default illegal_init init span)
   510           (master_directory node) blobs span eval;
   511       val prints' = perhaps (Command.print command_visible command_overlays command_name eval') [];
   512       val exec' = (eval', prints');
   513 
   514       val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
   515       val init' = if Keyword.is_theory_begin command_name then NONE else init;
   516     in SOME (assign_update', (command_id', (eval', prints')), init') end;
   517 
   518 fun removed_execs node0 (command_id, exec_ids) =
   519   subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
   520 
   521 in
   522 
   523 fun update old_version_id new_version_id edits state =
   524   let
   525     val old_version = the_version state old_version_id;
   526     val new_version = timeit "Document.edit_nodes" (fn () => fold edit_nodes edits old_version);
   527 
   528     val nodes = nodes_of new_version;
   529     val required = make_required nodes;
   530     val required0 = make_required (nodes_of old_version);
   531     val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
   532 
   533     val updated = timeit "Document.update" (fn () =>
   534       nodes |> String_Graph.schedule
   535         (fn deps => fn (name, node) =>
   536           (singleton o Future.forks)
   537             {name = "Document.update", group = NONE,
   538               deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
   539             (fn () => timeit ("Document.update " ^ name) (fn () =>
   540               let
   541                 val imports = map (apsnd Future.join) deps;
   542                 val imports_result_changed = exists (#4 o #1 o #2) imports;
   543                 val node_required = Symtab.defined required name;
   544               in
   545                 if Symtab.defined edited name orelse visible_node node orelse
   546                   imports_result_changed orelse Symtab.defined required0 name <> node_required
   547                 then
   548                   let
   549                     val node0 = node_of old_version name;
   550                     val init = init_theory imports node;
   551                     val proper_init =
   552                       check_theory false name node andalso
   553                       forall (fn (name, (_, node)) => check_theory true name node) imports;
   554 
   555                     val (print_execs, common, (still_visible, initial)) =
   556                       if imports_result_changed then (assign_update_empty, NONE, (true, true))
   557                       else last_common state node_required node0 node;
   558                     val common_command_exec = the_default_entry node common;
   559 
   560                     val (updated_execs, (command_id', (eval', _)), _) =
   561                       (print_execs, common_command_exec, if initial then SOME init else NONE)
   562                       |> (still_visible orelse node_required) ?
   563                         iterate_entries_after common
   564                           (fn ((prev, id), _) => fn res =>
   565                             if not node_required andalso prev = visible_last node then NONE
   566                             else new_exec state node proper_init id res) node;
   567 
   568                     val assigned_execs =
   569                       (node0, updated_execs) |-> iterate_entries_after common
   570                         (fn ((_, command_id0), exec0) => fn res =>
   571                           if is_none exec0 then NONE
   572                           else if assign_update_defined updated_execs command_id0 then SOME res
   573                           else SOME (assign_update_new (command_id0, NONE) res));
   574 
   575                     val last_exec =
   576                       if command_id' = Document_ID.none then NONE else SOME command_id';
   577                     val result =
   578                       if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
   579                       else SOME eval';
   580 
   581                     val assign_update = assign_update_result assigned_execs;
   582                     val removed = maps (removed_execs node0) assign_update;
   583                     val _ = List.app Execution.cancel removed;
   584 
   585                     val node' = node
   586                       |> assign_update_apply assigned_execs
   587                       |> set_result result;
   588                     val assigned_node = SOME (name, node');
   589                     val result_changed = changed_result node0 node';
   590                   in ((removed, assign_update, assigned_node, result_changed), node') end
   591                 else (([], [], NONE, false), node)
   592               end)))
   593       |> Future.joins |> map #1);
   594 
   595     val removed = maps #1 updated;
   596     val assign_update = maps #2 updated;
   597     val assigned_nodes = map_filter #3 updated;
   598 
   599     val state' = state
   600       |> define_version new_version_id (fold put_node assigned_nodes new_version);
   601 
   602   in (removed, assign_update, state') end;
   603 
   604 end;
   605 
   606 
   607 
   608 (** global state **)
   609 
   610 val global_state = Synchronized.var "Document.global_state" init_state;
   611 
   612 fun state () = Synchronized.value global_state;
   613 val change_state = Synchronized.change global_state;
   614 
   615 end;
   616