src/Pure/PIDE/document.ML
author wenzelm
Sun Jan 11 12:46:19 2015 +0100 (2015-01-11)
changeset 59347 2183c731f0a7
parent 59198 c73933e07c03
child 59685 c043306d2598
permissions -rw-r--r--
more explicit errors;
     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                    handle exn => (Output.system_message (Runtime.exn_message exn); reraise exn);
   444                 val future =
   445                   (singleton o Future.forks)
   446                    {name = "theory:" ^ name,
   447                     group = SOME (Future.new_group NONE),
   448                     deps = Future.task_of delay_request' :: maps (the_list o #2 o #2) deps,
   449                     pri = 0, interrupts = false} body;
   450               in (node, SOME (Future.task_of future)) end
   451             else (node, NONE));
   452       val execution' =
   453         {version_id = version_id, execution_id = execution_id, delay_request = delay_request'};
   454     in (versions, blobs, commands, execution') end));
   455 
   456 
   457 
   458 (** document update **)
   459 
   460 (* exec state assignment *)
   461 
   462 type assign_update = Command.exec option Inttab.table;  (*command id -> exec*)
   463 
   464 val assign_update_empty: assign_update = Inttab.empty;
   465 fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
   466 fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
   467 
   468 fun assign_update_new upd (tab: assign_update) =
   469   Inttab.update_new upd tab
   470     handle Inttab.DUP dup => err_dup "exec state assignment" dup;
   471 
   472 fun assign_update_result (tab: assign_update) =
   473   Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
   474 
   475 
   476 (* update *)
   477 
   478 local
   479 
   480 fun make_required nodes =
   481   let
   482     fun all_preds P =
   483       String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
   484       |> String_Graph.all_preds nodes
   485       |> Symtab.make_set;
   486 
   487     val all_visible = all_preds visible_node;
   488     val all_required = all_preds required_node;
   489   in
   490     Symtab.fold (fn (a, ()) =>
   491       exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
   492         Symtab.update (a, ())) all_visible all_required
   493   end;
   494 
   495 fun init_theory deps node span =
   496   let
   497     val master_dir = master_directory node;
   498     val header = read_header node span;
   499     val imports = #imports header;
   500     val parents =
   501       imports |> map (fn (import, _) =>
   502         (case loaded_theory import of
   503           SOME thy => thy
   504         | NONE =>
   505             Toplevel.end_theory (Position.file_only import)
   506               (case get_result (snd (the (AList.lookup (op =) deps import))) of
   507                 NONE => Toplevel.toplevel
   508               | SOME eval => Command.eval_result_state eval)));
   509     val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
   510   in Resources.begin_theory master_dir header parents end;
   511 
   512 fun check_theory full name node =
   513   is_some (loaded_theory name) orelse
   514   can get_header node andalso (not full orelse is_some (get_result node));
   515 
   516 fun last_common keywords state node_required node0 node =
   517   let
   518     fun update_flags prev (visible, initial) =
   519       let
   520         val visible' = visible andalso prev <> visible_last node;
   521         val initial' = initial andalso
   522           (case prev of
   523             NONE => true
   524           | SOME command_id =>
   525               not (Keyword.is_theory_begin keywords (the_command_name state command_id)));
   526       in (visible', initial') end;
   527 
   528     fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
   529       if ok then
   530         let
   531           val flags' as (visible', _) = update_flags prev flags;
   532           val ok' =
   533             (case (lookup_entry node0 command_id, opt_exec) of
   534               (SOME (eval0, _), SOME (eval, _)) =>
   535                 Command.eval_eq (eval0, eval) andalso
   536                   (visible' orelse node_required orelse Command.eval_running eval)
   537             | _ => false);
   538           val assign_update' = assign_update |> ok' ?
   539             (case opt_exec of
   540               SOME (eval, prints) =>
   541                 let
   542                   val command_visible = visible_command node command_id;
   543                   val command_overlays = overlays node command_id;
   544                   val command_name = the_command_name state command_id;
   545                 in
   546                   (case
   547                     Command.print command_visible command_overlays keywords command_name eval prints
   548                    of
   549                     SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
   550                   | NONE => I)
   551                 end
   552             | NONE => I);
   553         in SOME (prev, ok', flags', assign_update') end
   554       else NONE;
   555     val (common, ok, flags, assign_update') =
   556       iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
   557     val (common', flags') =
   558       if ok then
   559         let val last = Entries.get_after (get_entries node) common
   560         in (last, update_flags last flags) end
   561       else (common, flags);
   562   in (assign_update', common', flags') end;
   563 
   564 fun illegal_init _ = error "Illegal theory header after end of theory";
   565 
   566 fun new_exec keywords state node proper_init command_id' (assign_update, command_exec, init) =
   567   if not proper_init andalso is_none init then NONE
   568   else
   569     let
   570       val (_, (eval, _)) = command_exec;
   571 
   572       val command_visible = visible_command node command_id';
   573       val command_overlays = overlays node command_id';
   574       val (command_name, blob_digests, span0) = the_command state command_id';
   575       val blobs = map (resolve_blob state) blob_digests;
   576       val span = Lazy.force span0;
   577 
   578       val eval' =
   579         Command.eval keywords (master_directory node)
   580           (fn () => the_default illegal_init init span) blobs span eval;
   581       val prints' =
   582         perhaps (Command.print command_visible command_overlays keywords command_name eval') [];
   583       val exec' = (eval', prints');
   584 
   585       val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
   586       val init' = if Keyword.is_theory_begin keywords command_name then NONE else init;
   587     in SOME (assign_update', (command_id', (eval', prints')), init') end;
   588 
   589 fun removed_execs node0 (command_id, exec_ids) =
   590   subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
   591 
   592 in
   593 
   594 fun update old_version_id new_version_id edits state = Runtime.exn_trace_system (fn () =>
   595   let
   596     val old_version = the_version state old_version_id;
   597     val new_version =
   598       timeit "Document.edit_nodes"
   599         (fn () => old_version |> fold edit_nodes edits |> edit_keywords edits);
   600 
   601     val nodes = nodes_of new_version;
   602     val required = make_required nodes;
   603     val required0 = make_required (nodes_of old_version);
   604     val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
   605 
   606     val updated = timeit "Document.update" (fn () =>
   607       nodes |> String_Graph.schedule
   608         (fn deps => fn (name, node) =>
   609           (singleton o Future.forks)
   610             {name = "Document.update", group = NONE,
   611               deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
   612             (fn () =>
   613               timeit ("Document.update " ^ name) (fn () =>
   614                 Runtime.exn_trace_system (fn () =>
   615                   let
   616                     val keywords = the_default (Session.get_keywords ()) (get_keywords node);
   617                     val imports = map (apsnd Future.join) deps;
   618                     val imports_result_changed = exists (#4 o #1 o #2) imports;
   619                     val node_required = Symtab.defined required name;
   620                   in
   621                     if Symtab.defined edited name orelse visible_node node orelse
   622                       imports_result_changed orelse Symtab.defined required0 name <> node_required
   623                     then
   624                       let
   625                         val node0 = node_of old_version name;
   626                         val init = init_theory imports node;
   627                         val proper_init =
   628                           check_theory false name node andalso
   629                           forall (fn (name, (_, node)) => check_theory true name node) imports;
   630 
   631                         val (print_execs, common, (still_visible, initial)) =
   632                           if imports_result_changed then (assign_update_empty, NONE, (true, true))
   633                           else last_common keywords state node_required node0 node;
   634                         val common_command_exec = the_default_entry node common;
   635 
   636                         val (updated_execs, (command_id', (eval', _)), _) =
   637                           (print_execs, common_command_exec, if initial then SOME init else NONE)
   638                           |> (still_visible orelse node_required) ?
   639                             iterate_entries_after common
   640                               (fn ((prev, id), _) => fn res =>
   641                                 if not node_required andalso prev = visible_last node then NONE
   642                                 else new_exec keywords state node proper_init id res) node;
   643 
   644                         val assigned_execs =
   645                           (node0, updated_execs) |-> iterate_entries_after common
   646                             (fn ((_, command_id0), exec0) => fn res =>
   647                               if is_none exec0 then NONE
   648                               else if assign_update_defined updated_execs command_id0 then SOME res
   649                               else SOME (assign_update_new (command_id0, NONE) res));
   650 
   651                         val last_exec =
   652                           if command_id' = Document_ID.none then NONE else SOME command_id';
   653                         val result =
   654                           if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
   655                           else SOME eval';
   656 
   657                         val assign_update = assign_update_result assigned_execs;
   658                         val removed = maps (removed_execs node0) assign_update;
   659                         val _ = List.app Execution.cancel removed;
   660 
   661                         val node' = node
   662                           |> assign_update_apply assigned_execs
   663                           |> set_result result;
   664                         val assigned_node = SOME (name, node');
   665                         val result_changed = changed_result node0 node';
   666                       in ((removed, assign_update, assigned_node, result_changed), node') end
   667                     else (([], [], NONE, false), node)
   668                   end))))
   669       |> Future.joins |> map #1);
   670 
   671     val removed = maps #1 updated;
   672     val assign_update = maps #2 updated;
   673     val assigned_nodes = map_filter #3 updated;
   674 
   675     val state' = state
   676       |> define_version new_version_id (fold put_node assigned_nodes new_version);
   677 
   678   in (removed, assign_update, state') end);
   679 
   680 end;
   681 
   682 
   683 
   684 (** global state **)
   685 
   686 val global_state = Synchronized.var "Document.global_state" init_state;
   687 
   688 fun state () = Synchronized.value global_state;
   689 val change_state = Synchronized.change global_state;
   690 
   691 end;
   692