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