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