src/Pure/PIDE/document.ML
author wenzelm
Fri Jul 05 17:09:28 2013 +0200 (2013-07-05 ago)
changeset 52533 a95440dcd59c
parent 52532 c81d76f7f63d
child 52534 341ae9cd4743
permissions -rw-r--r--
clarified type Command.eval;
wenzelm@38150
     1
(*  Title:      Pure/PIDE/document.ML
wenzelm@38150
     2
    Author:     Makarius
wenzelm@38150
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@44478
     5
list of commands, with asynchronous read/eval/print processes.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@48707
    10
  type node_header = string * Thy_Header.header * string list
wenzelm@44157
    11
  datatype node_edit =
wenzelm@48755
    12
    Clear |    (* FIXME unused !? *)
wenzelm@52530
    13
    Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
    14
    Deps of node_header |
wenzelm@52530
    15
    Perspective of Document_ID.command list
wenzelm@44156
    16
  type edit = string * node_edit
wenzelm@38418
    17
  type state
wenzelm@38418
    18
  val init_state: state
wenzelm@52530
    19
  val define_command: Document_ID.command -> string -> string -> state -> state
wenzelm@52530
    20
  val remove_versions: Document_ID.version list -> state -> state
wenzelm@47343
    21
  val discontinue_execution: state -> unit
wenzelm@47404
    22
  val cancel_execution: state -> unit
wenzelm@47420
    23
  val start_execution: state -> unit
wenzelm@47628
    24
  val timing: bool Unsynchronized.ref
wenzelm@52530
    25
  val update: Document_ID.version -> Document_ID.version -> edit list -> state ->
wenzelm@52530
    26
    (Document_ID.command * Document_ID.exec list) list * state
wenzelm@43713
    27
  val state: unit -> state
wenzelm@43713
    28
  val change_state: (state -> state) -> unit
wenzelm@38150
    29
end;
wenzelm@38150
    30
wenzelm@38150
    31
structure Document: DOCUMENT =
wenzelm@38150
    32
struct
wenzelm@38150
    33
wenzelm@38418
    34
(** document structure **)
wenzelm@38418
    35
wenzelm@52530
    36
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document_ID.print id);
wenzelm@52530
    37
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document_ID.print id);
wenzelm@52530
    38
wenzelm@48707
    39
type node_header = string * Thy_Header.header * string list;
wenzelm@52530
    40
type perspective = Inttab.set * Document_ID.command option;
wenzelm@52530
    41
structure Entries = Linear_Set(type key = Document_ID.command val ord = int_ord);
wenzelm@38448
    42
wenzelm@43668
    43
abstype node = Node of
wenzelm@48707
    44
 {header: node_header,  (*master directory, theory header, errors*)
wenzelm@52508
    45
  perspective: perspective,  (*visible commands, last visible command*)
wenzelm@52532
    46
  entries: Command.exec option Entries.T * bool,  (*command entries with excecutions, stable*)
wenzelm@52526
    47
  result: Command.eval option}  (*result of last execution*)
wenzelm@50862
    48
and version = Version of node String_Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    49
with
wenzelm@38418
    50
wenzelm@47406
    51
fun make_node (header, perspective, entries, result) =
wenzelm@47406
    52
  Node {header = header, perspective = perspective, entries = entries, result = result};
wenzelm@43668
    53
wenzelm@47406
    54
fun map_node f (Node {header, perspective, entries, result}) =
wenzelm@47406
    55
  make_node (f (header, perspective, entries, result));
wenzelm@38418
    56
wenzelm@44476
    57
fun make_perspective command_ids : perspective =
wenzelm@52526
    58
  (Inttab.make_set command_ids, try List.last command_ids);
wenzelm@44441
    59
wenzelm@51294
    60
val no_header = ("", Thy_Header.make ("", Position.none) [] [], ["Bad theory header"]);
wenzelm@44441
    61
val no_perspective = make_perspective [];
wenzelm@44386
    62
wenzelm@49064
    63
val empty_node = make_node (no_header, no_perspective, (Entries.empty, true), NONE);
wenzelm@49064
    64
val clear_node =
wenzelm@49064
    65
  map_node (fn (header, _, _, _) => (header, no_perspective, (Entries.empty, true), NONE));
wenzelm@44386
    66
wenzelm@44386
    67
wenzelm@44386
    68
(* basic components *)
wenzelm@44386
    69
wenzelm@48927
    70
fun set_header header =
wenzelm@48927
    71
  map_node (fn (_, perspective, entries, result) => (header, perspective, entries, result));
wenzelm@48927
    72
wenzelm@48707
    73
fun get_header (Node {header = (master, header, errors), ...}) =
wenzelm@48707
    74
  if null errors then (master, header)
wenzelm@48707
    75
  else error (cat_lines errors);
wenzelm@48707
    76
wenzelm@48927
    77
fun read_header node span =
wenzelm@48927
    78
  let
wenzelm@51294
    79
    val (dir, {name = (name, _), imports, keywords}) = get_header node;
wenzelm@48927
    80
    val {name = (_, pos), imports = imports', ...} = Thy_Header.read_tokens span;
wenzelm@51294
    81
  in (dir, Thy_Header.make (name, pos) (map #1 imports ~~ map #2 imports') keywords) end;
wenzelm@44180
    82
wenzelm@44385
    83
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
    84
fun set_perspective ids =
wenzelm@47406
    85
  map_node (fn (header, _, entries, result) => (header, make_perspective ids, entries, result));
wenzelm@44385
    86
wenzelm@52526
    87
val visible_commands = #1 o get_perspective;
wenzelm@47345
    88
val visible_last = #2 o get_perspective;
wenzelm@47345
    89
val visible_node = is_some o visible_last
wenzelm@47345
    90
wenzelm@44444
    91
fun map_entries f =
wenzelm@49064
    92
  map_node (fn (header, perspective, (entries, stable), result) =>
wenzelm@49064
    93
    (header, perspective, (f entries, stable), result));
wenzelm@49064
    94
fun get_entries (Node {entries = (entries, _), ...}) = entries;
wenzelm@49064
    95
wenzelm@49064
    96
fun entries_stable stable =
wenzelm@49064
    97
  map_node (fn (header, perspective, (entries, _), result) =>
wenzelm@49064
    98
    (header, perspective, (entries, stable), result));
wenzelm@49064
    99
fun stable_entries (Node {entries = (_, stable), ...}) = stable;
wenzelm@44645
   100
wenzelm@44645
   101
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@49064
   102
fun iterate_entries_after start f (Node {entries = (entries, _), ...}) =
wenzelm@44645
   103
  (case Entries.get_after entries start of
wenzelm@44645
   104
    NONE => I
wenzelm@44645
   105
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   106
wenzelm@47339
   107
fun get_result (Node {result, ...}) = result;
wenzelm@44385
   108
fun set_result result =
wenzelm@47406
   109
  map_node (fn (header, perspective, entries, _) => (header, perspective, entries, result));
wenzelm@44180
   110
wenzelm@52533
   111
fun finished_theory node =
wenzelm@52533
   112
  (case Exn.capture (Command.eval_result o the) (get_result node) of
wenzelm@52533
   113
    Exn.Res {state = st, ...} => can (Toplevel.end_theory Position.none) st
wenzelm@52533
   114
  | _ => false);
wenzelm@52533
   115
wenzelm@50862
   116
fun get_node nodes name = String_Graph.get_node nodes name
wenzelm@50862
   117
  handle String_Graph.UNDEF _ => empty_node;
wenzelm@50862
   118
fun default_node name = String_Graph.default_node (name, empty_node);
wenzelm@50862
   119
fun update_node name f = default_node name #> String_Graph.map_node name f;
wenzelm@38418
   120
wenzelm@38418
   121
wenzelm@38448
   122
(* node edits and associated executions *)
wenzelm@38150
   123
wenzelm@44157
   124
datatype node_edit =
wenzelm@44185
   125
  Clear |
wenzelm@52530
   126
  Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
   127
  Deps of node_header |
wenzelm@52530
   128
  Perspective of Document_ID.command list;
wenzelm@44157
   129
wenzelm@44156
   130
type edit = string * node_edit;
wenzelm@38448
   131
wenzelm@49064
   132
val after_entry = Entries.get_after o get_entries;
wenzelm@44479
   133
wenzelm@49064
   134
fun lookup_entry node id =
wenzelm@49064
   135
  (case Entries.lookup (get_entries node) id of
wenzelm@44480
   136
    NONE => NONE
wenzelm@44480
   137
  | SOME (exec, _) => exec);
wenzelm@44480
   138
wenzelm@49064
   139
fun the_entry node id =
wenzelm@49064
   140
  (case Entries.lookup (get_entries node) id of
wenzelm@38448
   141
    NONE => err_undef "command entry" id
wenzelm@44476
   142
  | SOME (exec, _) => exec);
wenzelm@38150
   143
wenzelm@52532
   144
fun the_default_entry node (SOME id) = (id, the_default Command.no_exec (the_entry node id))
wenzelm@52532
   145
  | the_default_entry _ NONE = (Document_ID.none, Command.no_exec);
wenzelm@44476
   146
wenzelm@44482
   147
fun update_entry id exec =
wenzelm@44482
   148
  map_entries (Entries.update (id, exec));
wenzelm@44482
   149
wenzelm@44482
   150
fun reset_entry id node =
wenzelm@44482
   151
  if is_some (lookup_entry node id) then update_entry id NONE node else node;
wenzelm@38418
   152
wenzelm@38448
   153
fun reset_after id entries =
wenzelm@38448
   154
  (case Entries.get_after entries id of
wenzelm@38448
   155
    NONE => entries
wenzelm@38448
   156
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   157
wenzelm@43668
   158
val edit_node = map_entries o fold
wenzelm@43668
   159
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   160
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   161
wenzelm@38418
   162
wenzelm@38418
   163
(* version operations *)
wenzelm@38418
   164
wenzelm@50862
   165
val empty_version = Version String_Graph.empty;
wenzelm@44185
   166
wenzelm@38418
   167
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   168
val node_of = get_node o nodes_of;
wenzelm@38418
   169
wenzelm@44185
   170
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   171
wenzelm@44157
   172
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   173
  Version
wenzelm@44436
   174
    (case node_edit of
wenzelm@47406
   175
      Clear => update_node name clear_node nodes
wenzelm@47406
   176
    | Edits edits => update_node name (edit_node edits) nodes
wenzelm@48707
   177
    | Deps (master, header, errors) =>
wenzelm@44436
   178
        let
wenzelm@48927
   179
          val imports = map fst (#imports header);
wenzelm@48707
   180
          val errors1 =
wenzelm@48707
   181
            (Thy_Header.define_keywords header; errors)
wenzelm@48707
   182
              handle ERROR msg => errors @ [msg];
wenzelm@44436
   183
          val nodes1 = nodes
wenzelm@44436
   184
            |> default_node name
wenzelm@48927
   185
            |> fold default_node imports;
wenzelm@44436
   186
          val nodes2 = nodes1
wenzelm@50862
   187
            |> String_Graph.Keys.fold
wenzelm@50862
   188
                (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
wenzelm@48707
   189
          val (nodes3, errors2) =
wenzelm@50862
   190
            (String_Graph.add_deps_acyclic (name, imports) nodes2, errors1)
wenzelm@50862
   191
              handle String_Graph.CYCLES cs => (nodes2, errors1 @ map cycle_msg cs);
wenzelm@50862
   192
        in String_Graph.map_node name (set_header (master, header, errors2)) nodes3 end
wenzelm@47406
   193
    | Perspective perspective => update_node name (set_perspective perspective) nodes);
wenzelm@44444
   194
wenzelm@44222
   195
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   196
  Version (update_node name (K node) nodes);
wenzelm@38418
   197
wenzelm@38150
   198
end;
wenzelm@38150
   199
wenzelm@38418
   200
wenzelm@38418
   201
wenzelm@47389
   202
(** main state -- document structure and execution process **)
wenzelm@38418
   203
wenzelm@38418
   204
abstype state = State of
wenzelm@40398
   205
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@47335
   206
  commands: (string * Token.T list lazy) Inttab.table,  (*command_id -> named span*)
wenzelm@52530
   207
  execution:
wenzelm@52530
   208
    Document_ID.version * Future.group * bool Unsynchronized.ref}  (*current execution process*)
wenzelm@38418
   209
with
wenzelm@38418
   210
wenzelm@44674
   211
fun make_state (versions, commands, execution) =
wenzelm@44674
   212
  State {versions = versions, commands = commands, execution = execution};
wenzelm@38418
   213
wenzelm@44674
   214
fun map_state f (State {versions, commands, execution}) =
wenzelm@44674
   215
  make_state (f (versions, commands, execution));
wenzelm@38418
   216
wenzelm@38418
   217
val init_state =
wenzelm@52530
   218
  make_state (Inttab.make [(Document_ID.none, empty_version)], Inttab.empty,
wenzelm@52530
   219
    (Document_ID.none, Future.new_group NONE, Unsynchronized.ref false));
wenzelm@38418
   220
wenzelm@47420
   221
fun execution_of (State {execution, ...}) = execution;
wenzelm@47420
   222
wenzelm@38418
   223
wenzelm@38418
   224
(* document versions *)
wenzelm@38418
   225
wenzelm@52530
   226
fun define_version (id: Document_ID.version) version =
wenzelm@47420
   227
  map_state (fn (versions, commands, _) =>
wenzelm@47420
   228
    let
wenzelm@47420
   229
      val versions' = Inttab.update_new (id, version) versions
wenzelm@47420
   230
        handle Inttab.DUP dup => err_dup "document version" dup;
wenzelm@47420
   231
      val execution' = (id, Future.new_group NONE, Unsynchronized.ref true);
wenzelm@47420
   232
    in (versions', commands, execution') end);
wenzelm@38418
   233
wenzelm@52530
   234
fun the_version (State {versions, ...}) (id: Document_ID.version) =
wenzelm@38418
   235
  (case Inttab.lookup versions id of
wenzelm@38418
   236
    NONE => err_undef "document version" id
wenzelm@38418
   237
  | SOME version => version);
wenzelm@38418
   238
wenzelm@52530
   239
fun delete_version (id: Document_ID.version) versions = Inttab.delete id versions
wenzelm@44673
   240
  handle Inttab.UNDEF _ => err_undef "document version" id;
wenzelm@44673
   241
wenzelm@38418
   242
wenzelm@38418
   243
(* commands *)
wenzelm@38418
   244
wenzelm@52530
   245
fun define_command (id: Document_ID.command) name text =
wenzelm@44674
   246
  map_state (fn (versions, commands, execution) =>
wenzelm@38418
   247
    let
wenzelm@52530
   248
      val id_string = Document_ID.print id;
wenzelm@47395
   249
      val span = Lazy.lazy (fn () =>
wenzelm@47395
   250
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@51268
   251
          (fn () => Thy_Syntax.parse_tokens (Keyword.get_lexicons ()) (Position.id id_string) text)
wenzelm@51268
   252
            ());
wenzelm@47395
   253
      val _ =
wenzelm@47395
   254
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@50201
   255
          (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
wenzelm@38418
   256
      val commands' =
wenzelm@46945
   257
        Inttab.update_new (id, (name, span)) commands
wenzelm@38418
   258
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44674
   259
    in (versions, commands', execution) end);
wenzelm@38418
   260
wenzelm@52530
   261
fun the_command (State {commands, ...}) (id: Document_ID.command) =
wenzelm@44660
   262
  (case Inttab.lookup commands id of
wenzelm@38418
   263
    NONE => err_undef "command" id
wenzelm@44644
   264
  | SOME command => command);
wenzelm@38418
   265
wenzelm@47420
   266
end;
wenzelm@38418
   267
wenzelm@47420
   268
fun remove_versions ids state = state |> map_state (fn (versions, _, execution) =>
wenzelm@47420
   269
  let
wenzelm@47420
   270
    val _ = member (op =) ids (#1 execution) andalso
wenzelm@52530
   271
      error ("Attempt to remove execution version " ^ Document_ID.print (#1 execution));
wenzelm@47343
   272
wenzelm@47420
   273
    val versions' = fold delete_version ids versions;
wenzelm@47420
   274
    val commands' =
wenzelm@47420
   275
      (versions', Inttab.empty) |->
wenzelm@47420
   276
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@50862
   277
          String_Graph.fold (fn (_, (node, _)) => node |>
wenzelm@47420
   278
            iterate_entries (fn ((_, id), _) =>
wenzelm@47420
   279
              SOME o Inttab.insert (K true) (id, the_command state id))));
wenzelm@47420
   280
  in (versions', commands', execution) end);
wenzelm@38418
   281
wenzelm@38418
   282
wenzelm@38888
   283
wenzelm@47420
   284
(** document execution **)
wenzelm@47389
   285
wenzelm@47420
   286
val discontinue_execution = execution_of #> (fn (_, _, running) => running := false);
wenzelm@47420
   287
val cancel_execution = execution_of #> (fn (_, group, _) => Future.cancel_group group);
wenzelm@49906
   288
val terminate_execution = execution_of #> (fn (_, group, _) => Future.terminate group);
wenzelm@47345
   289
wenzelm@47420
   290
fun start_execution state =
wenzelm@47420
   291
  let
wenzelm@47420
   292
    val (version_id, group, running) = execution_of state;
wenzelm@47420
   293
    val _ =
wenzelm@47633
   294
      (singleton o Future.forks)
wenzelm@52508
   295
        {name = "Document.execution", group = SOME group, deps = [], pri = ~2, interrupts = true}
wenzelm@47633
   296
        (fn () =>
wenzelm@47633
   297
         (OS.Process.sleep (seconds 0.02);
wenzelm@50862
   298
          nodes_of (the_version state version_id) |> String_Graph.schedule
wenzelm@47633
   299
            (fn deps => fn (name, node) =>
wenzelm@47633
   300
              if not (visible_node node) andalso finished_theory node then
wenzelm@47633
   301
                Future.value ()
wenzelm@47633
   302
              else
wenzelm@47633
   303
                (singleton o Future.forks)
wenzelm@47633
   304
                  {name = "theory:" ^ name, group = SOME (Future.new_group (SOME group)),
wenzelm@47633
   305
                    deps = map (Future.task_of o #2) deps, pri = ~2, interrupts = false}
wenzelm@47633
   306
                  (fn () =>
wenzelm@52515
   307
                    iterate_entries (fn (_, opt_exec) => fn () =>
wenzelm@47633
   308
                      (case opt_exec of
wenzelm@52532
   309
                        SOME exec => if ! running then SOME (Command.exec_run exec) else NONE
wenzelm@47633
   310
                      | NONE => NONE)) node ()))));
wenzelm@47420
   311
  in () end;
wenzelm@47345
   312
wenzelm@47345
   313
wenzelm@47420
   314
wenzelm@47420
   315
(** document update **)
wenzelm@38418
   316
wenzelm@47628
   317
val timing = Unsynchronized.ref false;
wenzelm@47628
   318
fun timeit msg e = cond_timeit (! timing) msg e;
wenzelm@47628
   319
wenzelm@38418
   320
local
wenzelm@38418
   321
wenzelm@47335
   322
fun make_required nodes =
wenzelm@47335
   323
  let
wenzelm@47335
   324
    val all_visible =
wenzelm@50862
   325
      String_Graph.fold (fn (a, (node, _)) => visible_node node ? cons a) nodes []
wenzelm@50862
   326
      |> String_Graph.all_preds nodes
wenzelm@47345
   327
      |> map (rpair ()) |> Symtab.make;
wenzelm@47345
   328
wenzelm@47335
   329
    val required =
wenzelm@47345
   330
      Symtab.fold (fn (a, ()) =>
wenzelm@50862
   331
        exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
wenzelm@47345
   332
          Symtab.update (a, ())) all_visible Symtab.empty;
wenzelm@47335
   333
  in Symtab.defined required end;
wenzelm@47335
   334
wenzelm@48927
   335
fun init_theory deps node span =
wenzelm@47335
   336
  let
wenzelm@49173
   337
    (* FIXME provide files via Isabelle/Scala, not master_dir *)
wenzelm@48927
   338
    val (dir, header) = read_header node span;
wenzelm@47335
   339
    val master_dir =
wenzelm@48735
   340
      (case try Url.explode dir of
wenzelm@48735
   341
        SOME (Url.File path) => path
wenzelm@47335
   342
      | _ => Path.current);
wenzelm@48927
   343
    val imports = #imports header;
wenzelm@47335
   344
    val parents =
wenzelm@48927
   345
      imports |> map (fn (import, _) =>
wenzelm@47407
   346
        (case Thy_Info.lookup_theory import of
wenzelm@47335
   347
          SOME thy => thy
wenzelm@47335
   348
        | NONE =>
wenzelm@47339
   349
            Toplevel.end_theory (Position.file_only import)
wenzelm@52526
   350
              (#state
wenzelm@52533
   351
                (Command.eval_result
wenzelm@52526
   352
                  (the_default Command.no_eval
wenzelm@52526
   353
                    (get_result (snd (the (AList.lookup (op =) deps import)))))))));
wenzelm@48927
   354
    val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
wenzelm@47335
   355
  in Thy_Load.begin_theory master_dir header parents end;
wenzelm@47335
   356
wenzelm@47407
   357
fun check_theory full name node =
wenzelm@47407
   358
  is_some (Thy_Info.lookup_theory name) orelse
wenzelm@48707
   359
  can get_header node andalso (not full orelse is_some (get_result node));
wenzelm@47335
   360
wenzelm@44645
   361
fun last_common state last_visible node0 node =
wenzelm@44482
   362
  let
wenzelm@44645
   363
    fun update_flags prev (visible, initial) =
wenzelm@44645
   364
      let
wenzelm@44645
   365
        val visible' = visible andalso prev <> last_visible;
wenzelm@44645
   366
        val initial' = initial andalso
wenzelm@44645
   367
          (case prev of
wenzelm@44645
   368
            NONE => true
wenzelm@44645
   369
          | SOME id => not (Keyword.is_theory_begin (#1 (the_command state id))));
wenzelm@44645
   370
      in (visible', initial') end;
wenzelm@47630
   371
    fun get_common ((prev, id), opt_exec) (same, (_, flags)) =
wenzelm@47630
   372
      if same then
wenzelm@47630
   373
        let
wenzelm@47630
   374
          val flags' = update_flags prev flags;
wenzelm@47630
   375
          val same' =
wenzelm@47630
   376
            (case opt_exec of
wenzelm@47630
   377
              NONE => false
wenzelm@52533
   378
            | SOME (eval, _) =>
wenzelm@47630
   379
                (case lookup_entry node0 id of
wenzelm@47630
   380
                  NONE => false
wenzelm@52533
   381
                | SOME (eval0, _) =>
wenzelm@52533
   382
                    #exec_id eval = #exec_id eval0 andalso Command.stable_eval eval));
wenzelm@47630
   383
        in SOME (same', (prev, flags')) end
wenzelm@47630
   384
      else NONE;
wenzelm@47630
   385
    val (same, (common, flags)) =
wenzelm@47630
   386
      iterate_entries get_common node (true, (NONE, (true, true)));
wenzelm@44645
   387
  in
wenzelm@47630
   388
    if same then
wenzelm@44645
   389
      let val last = Entries.get_after (get_entries node) common
wenzelm@44645
   390
      in (last, update_flags last flags) end
wenzelm@47630
   391
    else (common, flags)
wenzelm@44645
   392
  end;
wenzelm@44645
   393
wenzelm@52515
   394
fun new_exec state proper_init command_visible command_id' (execs, command_exec, init) =
wenzelm@47407
   395
  if not proper_init andalso is_none init then NONE
wenzelm@44645
   396
  else
wenzelm@44645
   397
    let
wenzelm@52527
   398
      val (command_name, span) = the_command state command_id' ||> Lazy.force;
wenzelm@52526
   399
wenzelm@52514
   400
      fun illegal_init _ = error "Illegal theory header after end of theory";
wenzelm@44645
   401
      val (modify_init, init') =
wenzelm@52527
   402
        if Keyword.is_theory_begin command_name then
wenzelm@48927
   403
          (Toplevel.modify_init (fn () => the_default illegal_init init span), NONE)
wenzelm@44645
   404
        else (I, init);
wenzelm@52526
   405
wenzelm@52530
   406
      val exec_id' = Document_ID.make ();
wenzelm@52533
   407
      val eval_body' =
wenzelm@47406
   408
        Command.memo (fn () =>
wenzelm@52509
   409
          let
wenzelm@52532
   410
            val eval_state = Command.exec_result (snd command_exec);
wenzelm@52526
   411
            val tr =
wenzelm@52530
   412
              Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id'))
wenzelm@52526
   413
                (fn () =>
wenzelm@52526
   414
                  Command.read span
wenzelm@52526
   415
                  |> modify_init
wenzelm@52527
   416
                  |> Toplevel.put_id exec_id') ();
wenzelm@52526
   417
          in Command.eval span tr eval_state end);
wenzelm@52533
   418
      val eval' = {exec_id = exec_id', eval = eval_body'};
wenzelm@52530
   419
      val prints' = if command_visible then Command.print command_name eval' else [];
wenzelm@52533
   420
      val command_exec' = (command_id', (eval', prints'));
wenzelm@44674
   421
    in SOME (command_exec' :: execs, command_exec', init') end;
wenzelm@44482
   422
wenzelm@52527
   423
fun update_prints state node command_id =
wenzelm@52527
   424
  (case the_entry node command_id of
wenzelm@52533
   425
    SOME (eval, prints) =>
wenzelm@52527
   426
      let
wenzelm@52527
   427
        val (command_name, _) = the_command state command_id;
wenzelm@52530
   428
        val new_prints = Command.print command_name eval;
wenzelm@52527
   429
        val prints' =
wenzelm@52527
   430
          new_prints |> map (fn new_print =>
wenzelm@52527
   431
            (case find_first (fn {name, ...} => name = #name new_print) prints of
wenzelm@52532
   432
              SOME print => if Command.stable_print print then print else new_print
wenzelm@52527
   433
            | NONE => new_print));
wenzelm@52527
   434
      in
wenzelm@52527
   435
        if eq_list (op = o pairself #exec_id) (prints, prints') then NONE
wenzelm@52533
   436
        else SOME (command_id, (eval, prints'))
wenzelm@52527
   437
      end
wenzelm@52527
   438
  | NONE => NONE);
wenzelm@52527
   439
wenzelm@38418
   440
in
wenzelm@38418
   441
wenzelm@52530
   442
fun update (old_id: Document_ID.version) (new_id: Document_ID.version) edits state =
wenzelm@38418
   443
  let
wenzelm@38418
   444
    val old_version = the_version state old_id;
wenzelm@47628
   445
    val new_version = timeit "Document.edit_nodes" (fn () => fold edit_nodes edits old_version);
wenzelm@38418
   446
wenzelm@44544
   447
    val nodes = nodes_of new_version;
wenzelm@44544
   448
    val is_required = make_required nodes;
wenzelm@44544
   449
wenzelm@47628
   450
    val _ = timeit "Document.terminate_execution" (fn () => terminate_execution state);
wenzelm@47628
   451
    val updated = timeit "Document.update" (fn () =>
wenzelm@50862
   452
      nodes |> String_Graph.schedule
wenzelm@44199
   453
        (fn deps => fn (name, node) =>
wenzelm@47406
   454
          (singleton o Future.forks)
wenzelm@47406
   455
            {name = "Document.update", group = NONE,
wenzelm@47406
   456
              deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@47406
   457
            (fn () =>
wenzelm@47406
   458
              let
wenzelm@47406
   459
                val imports = map (apsnd Future.join) deps;
wenzelm@52527
   460
                val changed_imports = exists (#4 o #1 o #2) imports;
wenzelm@52514
   461
                val node_required = is_required name;
wenzelm@47406
   462
              in
wenzelm@52527
   463
                if changed_imports orelse AList.defined (op =) edits name orelse
wenzelm@49064
   464
                  not (stable_entries node) orelse not (finished_theory node)
wenzelm@47406
   465
                then
wenzelm@44482
   466
                  let
wenzelm@47406
   467
                    val node0 = node_of old_version name;
wenzelm@48927
   468
                    val init = init_theory imports node;
wenzelm@47407
   469
                    val proper_init =
wenzelm@47407
   470
                      check_theory false name node andalso
wenzelm@47407
   471
                      forall (fn (name, (_, node)) => check_theory true name node) imports;
wenzelm@47406
   472
wenzelm@52527
   473
                    val visible_commands = visible_commands node;
wenzelm@52527
   474
                    val visible_command = Inttab.defined visible_commands;
wenzelm@47345
   475
                    val last_visible = visible_last node;
wenzelm@52527
   476
wenzelm@52514
   477
                    val (common, (still_visible, initial)) =
wenzelm@52527
   478
                      if changed_imports then (NONE, (true, true))
wenzelm@47406
   479
                      else last_common state last_visible node0 node;
wenzelm@44674
   480
                    val common_command_exec = the_default_entry node common;
wenzelm@44479
   481
wenzelm@52533
   482
                    val (new_execs, (command_id', (eval', _)), _) =
wenzelm@44674
   483
                      ([], common_command_exec, if initial then SOME init else NONE) |>
wenzelm@52514
   484
                      (still_visible orelse node_required) ?
wenzelm@44645
   485
                        iterate_entries_after common
wenzelm@44482
   486
                          (fn ((prev, id), _) => fn res =>
wenzelm@52514
   487
                            if not node_required andalso prev = last_visible then NONE
wenzelm@52527
   488
                            else new_exec state proper_init (visible_command id) id res) node;
wenzelm@52527
   489
wenzelm@52527
   490
                    val updated_execs =
wenzelm@52527
   491
                      (visible_commands, new_execs) |-> Inttab.fold (fn (id, ()) =>
wenzelm@52533
   492
                        if exists (fn (_, ({exec_id = id', ...}, _)) => id = id') new_execs then I
wenzelm@52527
   493
                        else append (the_list (update_prints state node id)));
wenzelm@44479
   494
wenzelm@44482
   495
                    val no_execs =
wenzelm@44645
   496
                      iterate_entries_after common
wenzelm@44482
   497
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   498
                          if is_none exec0 then NONE
wenzelm@52533
   499
                          else if exists (fn (_, ({exec_id = id, ...}, _)) => id0 = id) updated_execs
wenzelm@52526
   500
                          then SOME res
wenzelm@44482
   501
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   502
wenzelm@52530
   503
                    val last_exec =
wenzelm@52530
   504
                      if command_id' = Document_ID.none then NONE else SOME command_id';
wenzelm@44482
   505
                    val result =
wenzelm@47407
   506
                      if is_some (after_entry node last_exec) then NONE
wenzelm@52526
   507
                      else SOME eval';
wenzelm@44480
   508
wenzelm@44482
   509
                    val node' = node
wenzelm@44482
   510
                      |> fold reset_entry no_execs
wenzelm@52527
   511
                      |> fold (fn (id, exec) => update_entry id (SOME exec)) updated_execs
wenzelm@52527
   512
                      |> entries_stable (null updated_execs)
wenzelm@47406
   513
                      |> set_result result;
wenzelm@47406
   514
                    val updated_node =
wenzelm@52527
   515
                      if null no_execs andalso null updated_execs then NONE
wenzelm@47406
   516
                      else SOME (name, node');
wenzelm@52527
   517
                    val changed_result = not (null no_execs) orelse not (null new_execs);
wenzelm@52527
   518
                  in ((no_execs, updated_execs, updated_node, changed_result), node') end
wenzelm@52527
   519
                else (([], [], NONE, false), node)
wenzelm@47406
   520
              end))
wenzelm@47628
   521
      |> Future.joins |> map #1);
wenzelm@44476
   522
wenzelm@44479
   523
    val command_execs =
wenzelm@52527
   524
      map (rpair []) (maps #1 updated) @
wenzelm@52532
   525
      map (fn (command_id, exec) => (command_id, Command.exec_ids exec)) (maps #2 updated);
wenzelm@47406
   526
    val updated_nodes = map_filter #3 updated;
wenzelm@38418
   527
wenzelm@44197
   528
    val state' = state
wenzelm@44476
   529
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@47388
   530
  in (command_execs, state') end;
wenzelm@38418
   531
wenzelm@38418
   532
end;
wenzelm@38418
   533
wenzelm@38418
   534
wenzelm@43713
   535
wenzelm@43713
   536
(** global state **)
wenzelm@43713
   537
wenzelm@52508
   538
val global_state = Synchronized.var "Document.global_state" init_state;
wenzelm@43713
   539
wenzelm@43713
   540
fun state () = Synchronized.value global_state;
wenzelm@43713
   541
val change_state = Synchronized.change global_state;
wenzelm@43713
   542
wenzelm@38418
   543
end;
wenzelm@38418
   544