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