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