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