src/Pure/PIDE/document.ML
author wenzelm
Sun May 19 18:10:45 2019 +0200 (5 months ago)
changeset 70284 3e17c3a5fd39
parent 70283 9ebbb53f4b50
child 70610 d14ddb1df52c
permissions -rw-r--r--
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
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@52536
     5
list of commands, associated with asynchronous execution process.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@52796
    10
  val timing: bool Unsynchronized.ref
wenzelm@59715
    11
  type node_header = {master: string, header: Thy_Header.header, errors: string list}
wenzelm@52862
    12
  type overlay = Document_ID.command * (string * string list)
wenzelm@44157
    13
  datatype node_edit =
wenzelm@52530
    14
    Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
    15
    Deps of node_header |
wenzelm@52849
    16
    Perspective of bool * Document_ID.command list * overlay list
wenzelm@44156
    17
  type edit = string * node_edit
wenzelm@38418
    18
  type state
wenzelm@38418
    19
  val init_state: state
wenzelm@54519
    20
  val define_blob: string -> string -> state -> state
wenzelm@56458
    21
  type blob_digest = (string * string option) Exn.result
wenzelm@59685
    22
  val define_command: Document_ID.command -> string -> blob_digest list -> int ->
wenzelm@59085
    23
    ((int * int) * string) list -> state -> state
wenzelm@60880
    24
  val command_exec: state -> string -> Document_ID.command -> Command.exec option
wenzelm@52530
    25
  val remove_versions: Document_ID.version list -> state -> state
wenzelm@52774
    26
  val start_execution: state -> state
wenzelm@68381
    27
  val update: Document_ID.version -> Document_ID.version -> edit list -> string list -> state ->
wenzelm@70284
    28
    string list * Document_ID.exec list * (Document_ID.command * Document_ID.exec list) list * state
wenzelm@43713
    29
  val state: unit -> state
wenzelm@43713
    30
  val change_state: (state -> state) -> unit
wenzelm@38150
    31
end;
wenzelm@38150
    32
wenzelm@38150
    33
structure Document: DOCUMENT =
wenzelm@38150
    34
struct
wenzelm@38150
    35
wenzelm@52796
    36
val timing = Unsynchronized.ref false;
wenzelm@52796
    37
fun timeit msg e = cond_timeit (! timing) msg e;
wenzelm@52796
    38
wenzelm@52796
    39
wenzelm@52796
    40
wenzelm@38418
    41
(** document structure **)
wenzelm@38418
    42
wenzelm@52530
    43
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ Document_ID.print id);
wenzelm@52530
    44
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ Document_ID.print id);
wenzelm@52530
    45
wenzelm@59715
    46
type node_header =
wenzelm@59715
    47
  {master: string, header: Thy_Header.header, errors: string list};
wenzelm@52849
    48
wenzelm@52849
    49
type perspective =
wenzelm@52849
    50
 {required: bool,  (*required node*)
wenzelm@52849
    51
  visible: Inttab.set,  (*visible commands*)
wenzelm@52849
    52
  visible_last: Document_ID.command option,  (*last visible command*)
wenzelm@57874
    53
  overlays: (string * string list) list Inttab.table};  (*command id -> print functions with args*)
wenzelm@52849
    54
wenzelm@52530
    55
structure Entries = Linear_Set(type key = Document_ID.command val ord = int_ord);
wenzelm@38448
    56
wenzelm@43668
    57
abstype node = Node of
wenzelm@48707
    58
 {header: node_header,  (*master directory, theory header, errors*)
wenzelm@59086
    59
  keywords: Keyword.keywords option,  (*outer syntax keywords*)
wenzelm@52849
    60
  perspective: perspective,  (*command perspective*)
wenzelm@66369
    61
  entries: Command.exec option Entries.T,  (*command entries with executions*)
wenzelm@68336
    62
  result: (Document_ID.command * Command.eval) option,  (*result of last execution*)
wenzelm@68336
    63
  consolidated: unit lazy}  (*consolidated status of eval forks*)
wenzelm@50862
    64
and version = Version of node String_Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    65
with
wenzelm@38418
    66
wenzelm@68336
    67
fun make_node (header, keywords, perspective, entries, result, consolidated) =
wenzelm@59086
    68
  Node {header = header, keywords = keywords, perspective = perspective,
wenzelm@68336
    69
    entries = entries, result = result, consolidated = consolidated};
wenzelm@43668
    70
wenzelm@68336
    71
fun map_node f (Node {header, keywords, perspective, entries, result, consolidated}) =
wenzelm@68336
    72
  make_node (f (header, keywords, perspective, entries, result, consolidated));
wenzelm@38418
    73
wenzelm@52849
    74
fun make_perspective (required, command_ids, overlays) : perspective =
wenzelm@52849
    75
 {required = required,
wenzelm@52849
    76
  visible = Inttab.make_set command_ids,
wenzelm@52849
    77
  visible_last = try List.last command_ids,
wenzelm@52862
    78
  overlays = Inttab.make_list overlays};
wenzelm@44441
    79
wenzelm@59715
    80
val no_header: node_header =
wenzelm@59715
    81
  {master = "", header = Thy_Header.make ("", Position.none) [] [], errors = []};
wenzelm@52849
    82
val no_perspective = make_perspective (false, [], []);
wenzelm@44386
    83
wenzelm@68197
    84
val empty_node =
wenzelm@68336
    85
  make_node (no_header, NONE, no_perspective, Entries.empty, NONE, Lazy.value ());
wenzelm@44386
    86
wenzelm@57615
    87
fun is_no_perspective ({required, visible, visible_last, overlays}: perspective) =
wenzelm@57615
    88
  not required andalso
wenzelm@57615
    89
  Inttab.is_empty visible andalso
wenzelm@57615
    90
  is_none visible_last andalso
wenzelm@57615
    91
  Inttab.is_empty overlays;
wenzelm@57615
    92
wenzelm@68336
    93
fun is_empty_node (Node {header, keywords, perspective, entries, result, consolidated}) =
wenzelm@57615
    94
  header = no_header andalso
wenzelm@59086
    95
  is_none keywords andalso
wenzelm@57615
    96
  is_no_perspective perspective andalso
wenzelm@57615
    97
  Entries.is_empty entries andalso
wenzelm@66379
    98
  is_none result andalso
wenzelm@68336
    99
  Lazy.is_finished consolidated;
wenzelm@57615
   100
wenzelm@44386
   101
wenzelm@44386
   102
(* basic components *)
wenzelm@44386
   103
wenzelm@59715
   104
fun master_directory (Node {header = {master, ...}, ...}) =
wenzelm@54558
   105
  (case try Url.explode master of
wenzelm@54558
   106
    SOME (Url.File path) => path
wenzelm@54558
   107
  | _ => Path.current);
wenzelm@54558
   108
wenzelm@59715
   109
fun set_header master header errors =
wenzelm@68336
   110
  map_node (fn (_, keywords, perspective, entries, result, consolidated) =>
wenzelm@66379
   111
    ({master = master, header = header, errors = errors},
wenzelm@68336
   112
      keywords, perspective, entries, result, consolidated));
wenzelm@59086
   113
wenzelm@59715
   114
fun get_header (Node {header, ...}) = header;
wenzelm@48707
   115
wenzelm@59086
   116
fun set_keywords keywords =
wenzelm@68336
   117
  map_node (fn (header, _, perspective, entries, result, consolidated) =>
wenzelm@68336
   118
    (header, keywords, perspective, entries, result, consolidated));
wenzelm@59086
   119
wenzelm@59086
   120
fun get_keywords (Node {keywords, ...}) = keywords;
wenzelm@59086
   121
wenzelm@48927
   122
fun read_header node span =
wenzelm@48927
   123
  let
wenzelm@59715
   124
    val {header, errors, ...} = get_header node;
wenzelm@59715
   125
    val _ =
wenzelm@59715
   126
      if null errors then ()
wenzelm@59715
   127
      else
wenzelm@59715
   128
        cat_lines errors |>
wenzelm@59715
   129
        (case Position.get_id (Position.thread_data ()) of
wenzelm@59715
   130
          NONE => I
wenzelm@59715
   131
        | SOME id => Protocol_Message.command_positions_yxml id)
wenzelm@59715
   132
        |> error;
wenzelm@59934
   133
    val {name = (name, _), imports, ...} = header;
wenzelm@67500
   134
    val {name = (_, pos), imports = imports', keywords} =
wenzelm@67500
   135
      Thy_Header.read_tokens Position.none span;
wenzelm@63022
   136
    val imports'' = (map #1 imports ~~ map #2 imports') handle ListPair.UnequalLengths => imports;
wenzelm@63022
   137
  in Thy_Header.make (name, pos) imports'' keywords end;
wenzelm@54526
   138
wenzelm@44385
   139
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@59086
   140
wenzelm@52808
   141
fun set_perspective args =
wenzelm@68336
   142
  map_node (fn (header, keywords, _, entries, result, consolidated) =>
wenzelm@68336
   143
    (header, keywords, make_perspective args, entries, result, consolidated));
wenzelm@44385
   144
wenzelm@52849
   145
val required_node = #required o get_perspective;
wenzelm@52849
   146
val visible_command = Inttab.defined o #visible o get_perspective;
wenzelm@52849
   147
val visible_last = #visible_last o get_perspective;
wenzelm@47345
   148
val visible_node = is_some o visible_last
wenzelm@52850
   149
val overlays = Inttab.lookup_list o #overlays o get_perspective;
wenzelm@47345
   150
wenzelm@44444
   151
fun map_entries f =
wenzelm@68336
   152
  map_node (fn (header, keywords, perspective, entries, result, consolidated) =>
wenzelm@68336
   153
    (header, keywords, perspective, f entries, result, consolidated));
wenzelm@59086
   154
wenzelm@52761
   155
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44645
   156
wenzelm@44645
   157
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@52761
   158
fun iterate_entries_after start f (Node {entries, ...}) =
wenzelm@44645
   159
  (case Entries.get_after entries start of
wenzelm@44645
   160
    NONE => I
wenzelm@44645
   161
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   162
wenzelm@47339
   163
fun get_result (Node {result, ...}) = result;
wenzelm@59086
   164
wenzelm@44385
   165
fun set_result result =
wenzelm@68336
   166
  map_node (fn (header, keywords, perspective, entries, _, consolidated) =>
wenzelm@68336
   167
    (header, keywords, perspective, entries, result, consolidated));
wenzelm@52566
   168
wenzelm@52772
   169
fun pending_result node =
wenzelm@52772
   170
  (case get_result node of
wenzelm@68336
   171
    SOME (_, eval) => not (Command.eval_finished eval)
wenzelm@52772
   172
  | NONE => false);
wenzelm@52533
   173
wenzelm@59198
   174
fun finished_result node =
wenzelm@59198
   175
  (case get_result node of
wenzelm@68336
   176
    SOME (_, eval) => Command.eval_finished eval
wenzelm@59198
   177
  | NONE => false);
wenzelm@59198
   178
wenzelm@66379
   179
fun finished_result_theory node =
wenzelm@68184
   180
  if finished_result node then
wenzelm@68336
   181
    let
wenzelm@68336
   182
      val (result_id, eval) = the (get_result node);
wenzelm@68336
   183
      val st = Command.eval_result_state eval;
wenzelm@68336
   184
    in SOME (result_id, Toplevel.end_theory Position.none st) handle ERROR _ => NONE end
wenzelm@68184
   185
  else NONE;
wenzelm@66379
   186
wenzelm@68336
   187
val reset_consolidated =
wenzelm@66379
   188
  map_node (fn (header, keywords, perspective, entries, result, _) =>
wenzelm@68336
   189
    (header, keywords, perspective, entries, result, Lazy.lazy I));
wenzelm@68336
   190
wenzelm@68336
   191
fun get_consolidated (Node {consolidated, ...}) = consolidated;
wenzelm@68336
   192
wenzelm@68336
   193
val is_consolidated = Lazy.is_finished o get_consolidated;
wenzelm@66379
   194
wenzelm@68336
   195
fun commit_consolidated node =
wenzelm@68336
   196
  (Lazy.force (get_consolidated node); Output.status (Markup.markup_only Markup.consolidated));
wenzelm@68336
   197
wenzelm@68336
   198
fun could_consolidate node =
wenzelm@68336
   199
  not (is_consolidated node) andalso is_some (finished_result_theory node);
wenzelm@66379
   200
wenzelm@50862
   201
fun get_node nodes name = String_Graph.get_node nodes name
wenzelm@50862
   202
  handle String_Graph.UNDEF _ => empty_node;
wenzelm@50862
   203
fun default_node name = String_Graph.default_node (name, empty_node);
wenzelm@50862
   204
fun update_node name f = default_node name #> String_Graph.map_node name f;
wenzelm@38418
   205
wenzelm@38418
   206
wenzelm@38448
   207
(* node edits and associated executions *)
wenzelm@38150
   208
wenzelm@52862
   209
type overlay = Document_ID.command * (string * string list);
wenzelm@52849
   210
wenzelm@44157
   211
datatype node_edit =
wenzelm@52530
   212
  Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
   213
  Deps of node_header |
wenzelm@52849
   214
  Perspective of bool * Document_ID.command list * overlay list;
wenzelm@44157
   215
wenzelm@44156
   216
type edit = string * node_edit;
wenzelm@38448
   217
wenzelm@49064
   218
val after_entry = Entries.get_after o get_entries;
wenzelm@44479
   219
wenzelm@49064
   220
fun lookup_entry node id =
wenzelm@49064
   221
  (case Entries.lookup (get_entries node) id of
wenzelm@44480
   222
    NONE => NONE
wenzelm@44480
   223
  | SOME (exec, _) => exec);
wenzelm@44480
   224
wenzelm@49064
   225
fun the_entry node id =
wenzelm@49064
   226
  (case Entries.lookup (get_entries node) id of
wenzelm@38448
   227
    NONE => err_undef "command entry" id
wenzelm@44476
   228
  | SOME (exec, _) => exec);
wenzelm@38150
   229
wenzelm@52566
   230
fun assign_entry (command_id, exec) node =
wenzelm@52566
   231
  if is_none (Entries.lookup (get_entries node) command_id) then node
wenzelm@52566
   232
  else map_entries (Entries.update (command_id, exec)) node;
wenzelm@38418
   233
wenzelm@38448
   234
fun reset_after id entries =
wenzelm@38448
   235
  (case Entries.get_after entries id of
wenzelm@38448
   236
    NONE => entries
wenzelm@38448
   237
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   238
wenzelm@43668
   239
val edit_node = map_entries o fold
wenzelm@43668
   240
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   241
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   242
wenzelm@38418
   243
wenzelm@38418
   244
(* version operations *)
wenzelm@38418
   245
wenzelm@50862
   246
val empty_version = Version String_Graph.empty;
wenzelm@44185
   247
wenzelm@38418
   248
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   249
val node_of = get_node o nodes_of;
wenzelm@38418
   250
wenzelm@44185
   251
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   252
wenzelm@44157
   253
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   254
  Version
wenzelm@44436
   255
    (case node_edit of
wenzelm@54562
   256
      Edits edits => update_node name (edit_node edits) nodes
wenzelm@59715
   257
    | Deps {master, header, errors} =>
wenzelm@44436
   258
        let
wenzelm@48927
   259
          val imports = map fst (#imports header);
wenzelm@44436
   260
          val nodes1 = nodes
wenzelm@44436
   261
            |> default_node name
wenzelm@48927
   262
            |> fold default_node imports;
wenzelm@44436
   263
          val nodes2 = nodes1
wenzelm@50862
   264
            |> String_Graph.Keys.fold
wenzelm@50862
   265
                (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
wenzelm@59086
   266
          val (nodes3, errors1) =
wenzelm@59086
   267
            (String_Graph.add_deps_acyclic (name, imports) nodes2, errors)
wenzelm@59086
   268
              handle String_Graph.CYCLES cs => (nodes2, errors @ map cycle_msg cs);
wenzelm@59715
   269
        in String_Graph.map_node name (set_header master header errors1) nodes3 end
wenzelm@47406
   270
    | Perspective perspective => update_node name (set_perspective perspective) nodes);
wenzelm@44444
   271
wenzelm@59086
   272
fun update_keywords name nodes =
wenzelm@59086
   273
  nodes |> String_Graph.map_node name (fn node =>
wenzelm@59086
   274
    if is_empty_node node then node
wenzelm@59086
   275
    else
wenzelm@59086
   276
      let
wenzelm@59715
   277
        val {master, header, errors} = get_header node;
wenzelm@59086
   278
        val imports_keywords = map_filter (get_keywords o get_node nodes o #1) (#imports header);
wenzelm@59086
   279
        val keywords =
wenzelm@59086
   280
          Library.foldl Keyword.merge_keywords (Session.get_keywords (), imports_keywords);
wenzelm@59086
   281
        val (keywords', errors') =
wenzelm@59086
   282
          (Keyword.add_keywords (#keywords header) keywords, errors)
wenzelm@59086
   283
            handle ERROR msg =>
wenzelm@59086
   284
              (keywords, if member (op =) errors msg then errors else errors @ [msg]);
wenzelm@59086
   285
      in
wenzelm@59086
   286
        node
wenzelm@59715
   287
        |> set_header master header errors'
wenzelm@59086
   288
        |> set_keywords (SOME keywords')
wenzelm@59086
   289
      end);
wenzelm@59086
   290
wenzelm@59086
   291
fun edit_keywords edits (Version nodes) =
wenzelm@59086
   292
  Version
wenzelm@59086
   293
    (fold update_keywords
wenzelm@59086
   294
      (String_Graph.all_succs nodes (map_filter (fn (a, Deps _) => SOME a | _ => NONE) edits))
wenzelm@59086
   295
      nodes);
wenzelm@59086
   296
wenzelm@70282
   297
fun is_suppressed_node (nodes: node String_Graph.T) (name, node) =
wenzelm@70282
   298
  String_Graph.is_maximal nodes name andalso is_empty_node node;
wenzelm@70282
   299
wenzelm@44222
   300
fun put_node (name, node) (Version nodes) =
wenzelm@57616
   301
  let
wenzelm@57616
   302
    val nodes1 = update_node name (K node) nodes;
wenzelm@57616
   303
    val nodes2 =
wenzelm@70282
   304
      if is_suppressed_node nodes1 (name, node)
wenzelm@57616
   305
      then String_Graph.del_node name nodes1
wenzelm@57616
   306
      else nodes1;
wenzelm@57616
   307
  in Version nodes2 end;
wenzelm@38418
   308
wenzelm@38150
   309
end;
wenzelm@38150
   310
wenzelm@38418
   311
wenzelm@52595
   312
wenzelm@52595
   313
(** main state -- document structure and execution process **)
wenzelm@52536
   314
wenzelm@56458
   315
type blob_digest = (string * string option) Exn.result;  (*file node name, raw digest*)
wenzelm@55798
   316
wenzelm@52774
   317
type execution =
wenzelm@52774
   318
 {version_id: Document_ID.version,  (*static version id*)
wenzelm@52774
   319
  execution_id: Document_ID.execution,  (*dynamic execution id*)
wenzelm@68366
   320
  delay_request: unit future,  (*pending event timer request*)
wenzelm@68366
   321
  parallel_prints: Command.exec list};  (*parallel prints for early execution*)
wenzelm@52604
   322
wenzelm@52774
   323
val no_execution: execution =
wenzelm@59193
   324
  {version_id = Document_ID.none,
wenzelm@59193
   325
   execution_id = Document_ID.none,
wenzelm@68366
   326
   delay_request = Future.value (),
wenzelm@68366
   327
   parallel_prints = []};
wenzelm@52774
   328
wenzelm@68366
   329
fun new_execution version_id delay_request parallel_prints : execution =
wenzelm@59193
   330
  {version_id = version_id,
wenzelm@59193
   331
   execution_id = Execution.start (),
wenzelm@68366
   332
   delay_request = delay_request,
wenzelm@68366
   333
   parallel_prints = parallel_prints};
wenzelm@52604
   334
wenzelm@38418
   335
abstype state = State of
wenzelm@52536
   336
 {versions: version Inttab.table,  (*version id -> document content*)
wenzelm@55798
   337
  blobs: (SHA1.digest * string list) Symtab.table,  (*raw digest -> digest, lines*)
wenzelm@59689
   338
  commands: (string * blob_digest list * int * Token.T list lazy) Inttab.table,
wenzelm@59689
   339
    (*command id -> name, inlined files, token index of files, command span*)
wenzelm@52536
   340
  execution: execution}  (*current execution process*)
wenzelm@38418
   341
with
wenzelm@38418
   342
wenzelm@54519
   343
fun make_state (versions, blobs, commands, execution) =
wenzelm@54519
   344
  State {versions = versions, blobs = blobs, commands = commands, execution = execution};
wenzelm@38418
   345
wenzelm@54519
   346
fun map_state f (State {versions, blobs, commands, execution}) =
wenzelm@54519
   347
  make_state (f (versions, blobs, commands, execution));
wenzelm@38418
   348
wenzelm@38418
   349
val init_state =
wenzelm@54519
   350
  make_state (Inttab.make [(Document_ID.none, empty_version)],
wenzelm@54519
   351
    Symtab.empty, Inttab.empty, no_execution);
wenzelm@38418
   352
wenzelm@38418
   353
wenzelm@38418
   354
(* document versions *)
wenzelm@38418
   355
wenzelm@68366
   356
fun parallel_prints_node node =
wenzelm@68366
   357
  iterate_entries (fn (_, opt_exec) => fn rev_result =>
wenzelm@68366
   358
    (case opt_exec of
wenzelm@68366
   359
      SOME (eval, prints) =>
wenzelm@68366
   360
        (case filter Command.parallel_print prints of
wenzelm@68366
   361
          [] => SOME rev_result
wenzelm@68366
   362
        | prints' => SOME ((eval, prints') :: rev_result))
wenzelm@68366
   363
    | NONE => NONE)) node [] |> rev;
wenzelm@68366
   364
wenzelm@68366
   365
fun define_version version_id version assigned_nodes =
wenzelm@59193
   366
  map_state (fn (versions, blobs, commands, {delay_request, ...}) =>
wenzelm@47420
   367
    let
wenzelm@68366
   368
      val version' = fold put_node assigned_nodes version;
wenzelm@68366
   369
      val versions' = Inttab.update_new (version_id, version') versions
wenzelm@47420
   370
        handle Inttab.DUP dup => err_dup "document version" dup;
wenzelm@68366
   371
      val parallel_prints = maps (parallel_prints_node o #2) assigned_nodes;
wenzelm@68366
   372
      val execution' = new_execution version_id delay_request parallel_prints;
wenzelm@54519
   373
    in (versions', blobs, commands, execution') end);
wenzelm@38418
   374
wenzelm@52536
   375
fun the_version (State {versions, ...}) version_id =
wenzelm@52536
   376
  (case Inttab.lookup versions version_id of
wenzelm@52536
   377
    NONE => err_undef "document version" version_id
wenzelm@38418
   378
  | SOME version => version);
wenzelm@38418
   379
wenzelm@52536
   380
fun delete_version version_id versions =
wenzelm@52536
   381
  Inttab.delete version_id versions
wenzelm@52536
   382
    handle Inttab.UNDEF _ => err_undef "document version" version_id;
wenzelm@44673
   383
wenzelm@38418
   384
wenzelm@54519
   385
(* inlined files *)
wenzelm@54519
   386
wenzelm@54519
   387
fun define_blob digest text =
wenzelm@54519
   388
  map_state (fn (versions, blobs, commands, execution) =>
wenzelm@57638
   389
    let val blobs' = Symtab.update (digest, (SHA1.fake digest, split_lines text)) blobs
wenzelm@54519
   390
    in (versions, blobs', commands, execution) end);
wenzelm@54519
   391
wenzelm@54519
   392
fun the_blob (State {blobs, ...}) digest =
wenzelm@54519
   393
  (case Symtab.lookup blobs digest of
wenzelm@54519
   394
    NONE => error ("Undefined blob: " ^ digest)
wenzelm@55788
   395
  | SOME content => content);
wenzelm@54519
   396
wenzelm@56447
   397
fun resolve_blob state (blob_digest: blob_digest) =
wenzelm@61077
   398
  blob_digest |> Exn.map_res (fn (file_node, raw_digest) =>
wenzelm@56458
   399
    (file_node, Option.map (the_blob state) raw_digest));
wenzelm@56447
   400
wenzelm@59685
   401
fun blob_reports pos (blob_digest: blob_digest) =
wenzelm@59685
   402
  (case blob_digest of Exn.Res (file_node, _) => [(pos, Markup.path file_node)] | _ => []);
wenzelm@59685
   403
wenzelm@54519
   404
wenzelm@38418
   405
(* commands *)
wenzelm@38418
   406
wenzelm@59702
   407
fun define_command command_id name blobs_digests blobs_index toks =
wenzelm@54519
   408
  map_state (fn (versions, blobs, commands, execution) =>
wenzelm@38418
   409
    let
wenzelm@52536
   410
      val id = Document_ID.print command_id;
wenzelm@52536
   411
      val span =
wenzelm@66167
   412
        Lazy.lazy_name "Document.define_command" (fn () =>
wenzelm@52536
   413
          Position.setmp_thread_data (Position.id_only id)
wenzelm@59685
   414
            (fn () =>
wenzelm@59685
   415
              let
wenzelm@59685
   416
                val (tokens, _) = fold_map Token.make toks (Position.id id);
wenzelm@59685
   417
                val _ =
wenzelm@59702
   418
                  if blobs_index < 0
wenzelm@59702
   419
                  then (*inlined errors*)
wenzelm@59702
   420
                    map_filter Exn.get_exn blobs_digests
wenzelm@59702
   421
                    |> List.app (Output.error_message o Runtime.exn_message)
wenzelm@59702
   422
                  else (*auxiliary files*)
wenzelm@60027
   423
                    let val pos = Token.pos_of (nth tokens blobs_index)
wenzelm@60027
   424
                    in Position.reports (maps (blob_reports pos) blobs_digests) end;
wenzelm@59685
   425
              in tokens end) ());
wenzelm@59685
   426
      val commands' =
wenzelm@59702
   427
        Inttab.update_new (command_id, (name, blobs_digests, blobs_index, span)) commands
wenzelm@59685
   428
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@47395
   429
      val _ =
wenzelm@52536
   430
        Position.setmp_thread_data (Position.id_only id)
wenzelm@50201
   431
          (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
wenzelm@54519
   432
    in (versions, blobs, commands', execution) end);
wenzelm@38418
   433
wenzelm@52536
   434
fun the_command (State {commands, ...}) command_id =
wenzelm@52536
   435
  (case Inttab.lookup commands command_id of
wenzelm@52536
   436
    NONE => err_undef "command" command_id
wenzelm@44644
   437
  | SOME command => command);
wenzelm@38418
   438
wenzelm@52569
   439
val the_command_name = #1 oo the_command;
wenzelm@52569
   440
wenzelm@66379
   441
wenzelm@66379
   442
(* execution *)
wenzelm@66379
   443
wenzelm@66379
   444
fun get_execution (State {execution, ...}) = execution;
wenzelm@66379
   445
fun get_execution_version state = the_version state (#version_id (get_execution state));
wenzelm@66379
   446
wenzelm@60880
   447
fun command_exec state node_name command_id =
wenzelm@60880
   448
  let
wenzelm@66379
   449
    val version = get_execution_version state;
wenzelm@66379
   450
    val node = get_node (nodes_of version) node_name;
wenzelm@60880
   451
  in the_entry node command_id end;
wenzelm@60880
   452
wenzelm@47420
   453
end;
wenzelm@38418
   454
wenzelm@52566
   455
wenzelm@52566
   456
(* remove_versions *)
wenzelm@52566
   457
wenzelm@54519
   458
fun remove_versions version_ids state = state |> map_state (fn (versions, _, _, execution) =>
wenzelm@47420
   459
  let
wenzelm@52536
   460
    val _ =
wenzelm@52536
   461
      member (op =) version_ids (#version_id execution) andalso
wenzelm@52536
   462
        error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
wenzelm@47343
   463
wenzelm@52536
   464
    val versions' = fold delete_version version_ids versions;
wenzelm@47420
   465
    val commands' =
wenzelm@47420
   466
      (versions', Inttab.empty) |->
wenzelm@47420
   467
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@50862
   468
          String_Graph.fold (fn (_, (node, _)) => node |>
wenzelm@52536
   469
            iterate_entries (fn ((_, command_id), _) =>
wenzelm@52536
   470
              SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
wenzelm@54519
   471
    val blobs' =
wenzelm@54519
   472
      (commands', Symtab.empty) |->
wenzelm@59689
   473
        Inttab.fold (fn (_, (_, blobs, _, _)) => blobs |>
wenzelm@56458
   474
          fold (fn Exn.Res (_, SOME b) => Symtab.update (b, the_blob state b) | _ => I));
wenzelm@54519
   475
wenzelm@54519
   476
  in (versions', blobs', commands', execution) end);
wenzelm@38418
   477
wenzelm@38418
   478
wenzelm@52604
   479
(* document execution *)
wenzelm@47389
   480
wenzelm@60198
   481
fun make_required nodes =
wenzelm@60198
   482
  let
wenzelm@60198
   483
    fun all_preds P =
wenzelm@60198
   484
      String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
wenzelm@60198
   485
      |> String_Graph.all_preds nodes
wenzelm@60198
   486
      |> Symtab.make_set;
wenzelm@60198
   487
wenzelm@60198
   488
    val all_visible = all_preds visible_node;
wenzelm@60198
   489
    val all_required = all_preds required_node;
wenzelm@60198
   490
  in
wenzelm@60198
   491
    Symtab.fold (fn (a, ()) =>
wenzelm@60198
   492
      exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
wenzelm@60198
   493
        Symtab.update (a, ())) all_visible all_required
wenzelm@60198
   494
  end;
wenzelm@60198
   495
wenzelm@54519
   496
fun start_execution state = state |> map_state (fn (versions, blobs, commands, execution) =>
wenzelm@52796
   497
  timeit "Document.start_execution" (fn () =>
wenzelm@52796
   498
    let
wenzelm@68366
   499
      val {version_id, execution_id, delay_request, parallel_prints} = execution;
wenzelm@52798
   500
wenzelm@52798
   501
      val delay = seconds (Options.default_real "editor_execution_delay");
wenzelm@52774
   502
wenzelm@52798
   503
      val _ = Future.cancel delay_request;
wenzelm@69826
   504
      val delay_request' = Event_Timer.future {physical = true} (Time.now () + delay);
wenzelm@68366
   505
      val delay = Future.task_of delay_request';
wenzelm@68366
   506
wenzelm@68366
   507
      val parallel_prints' = parallel_prints
wenzelm@68366
   508
        |> map_filter (Command.exec_parallel_prints execution_id [delay]);
wenzelm@52798
   509
wenzelm@59198
   510
      fun finished_import (name, (node, _)) =
wenzelm@65445
   511
        finished_result node orelse is_some (Thy_Info.lookup_theory name);
wenzelm@60198
   512
wenzelm@60198
   513
      val nodes = nodes_of (the_version state version_id);
wenzelm@60198
   514
      val required = make_required nodes;
wenzelm@59193
   515
      val _ =
wenzelm@60198
   516
        nodes |> String_Graph.schedule
wenzelm@52573
   517
          (fn deps => fn (name, node) =>
wenzelm@60198
   518
            if Symtab.defined required name orelse visible_node node orelse pending_result node then
wenzelm@52774
   519
              let
wenzelm@52774
   520
                val future =
wenzelm@52774
   521
                  (singleton o Future.forks)
wenzelm@59193
   522
                   {name = "theory:" ^ name,
wenzelm@59193
   523
                    group = SOME (Future.new_group NONE),
wenzelm@68366
   524
                    deps = delay :: Execution.active_tasks name @ maps (the_list o #2 o #2) deps,
wenzelm@68866
   525
                    pri = 0, interrupts = false}
wenzelm@68866
   526
                  (fn () =>
wenzelm@68866
   527
                   (Execution.worker_task_active true name;
wenzelm@68866
   528
                    if forall finished_import deps then
wenzelm@68866
   529
                      iterate_entries (fn (_, opt_exec) => fn () =>
wenzelm@68866
   530
                        (case opt_exec of
wenzelm@68866
   531
                          SOME exec =>
wenzelm@68866
   532
                            if Execution.is_running execution_id
wenzelm@68866
   533
                            then SOME (Command.exec execution_id exec)
wenzelm@68866
   534
                            else NONE
wenzelm@68866
   535
                        | NONE => NONE)) node ()
wenzelm@68866
   536
                    else ();
wenzelm@68866
   537
                    Execution.worker_task_active false name)
wenzelm@68866
   538
                      handle exn =>
wenzelm@68866
   539
                       (Output.system_message (Runtime.exn_message exn);
wenzelm@68866
   540
                        Execution.worker_task_active false name;
wenzelm@68866
   541
                        Exn.reraise exn));
wenzelm@59198
   542
              in (node, SOME (Future.task_of future)) end
wenzelm@59198
   543
            else (node, NONE));
wenzelm@52798
   544
      val execution' =
wenzelm@68366
   545
        {version_id = version_id, execution_id = execution_id,
wenzelm@68366
   546
          delay_request = delay_request', parallel_prints = parallel_prints'};
wenzelm@54519
   547
    in (versions, blobs, commands, execution') end));
wenzelm@47345
   548
wenzelm@47345
   549
wenzelm@47420
   550
wenzelm@47420
   551
(** document update **)
wenzelm@38418
   552
wenzelm@52566
   553
(* exec state assignment *)
wenzelm@52566
   554
wenzelm@52566
   555
type assign_update = Command.exec option Inttab.table;  (*command id -> exec*)
wenzelm@52566
   556
wenzelm@52566
   557
val assign_update_empty: assign_update = Inttab.empty;
wenzelm@52566
   558
fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
wenzelm@68336
   559
fun assign_update_change entry (tab: assign_update) = Inttab.update entry tab;
wenzelm@52566
   560
fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
wenzelm@52566
   561
wenzelm@52566
   562
fun assign_update_new upd (tab: assign_update) =
wenzelm@52566
   563
  Inttab.update_new upd tab
wenzelm@52566
   564
    handle Inttab.DUP dup => err_dup "exec state assignment" dup;
wenzelm@52566
   565
wenzelm@52566
   566
fun assign_update_result (tab: assign_update) =
wenzelm@52566
   567
  Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
wenzelm@52566
   568
wenzelm@52566
   569
wenzelm@52566
   570
(* update *)
wenzelm@52566
   571
wenzelm@38418
   572
local
wenzelm@38418
   573
wenzelm@48927
   574
fun init_theory deps node span =
wenzelm@47335
   575
  let
wenzelm@54526
   576
    val master_dir = master_directory node;
wenzelm@54526
   577
    val header = read_header node span;
wenzelm@48927
   578
    val imports = #imports header;
wenzelm@59716
   579
wenzelm@68869
   580
    fun maybe_eval_result eval = Command.eval_result_state eval
wenzelm@69886
   581
      handle Fail _ => Toplevel.init_toplevel ();
wenzelm@68869
   582
wenzelm@68869
   583
    fun maybe_end_theory pos st = SOME (Toplevel.end_theory pos st)
wenzelm@68869
   584
      handle ERROR msg => (Output.error_message msg; NONE);
wenzelm@68869
   585
wenzelm@59716
   586
    val parents_reports =
wenzelm@59716
   587
      imports |> map_filter (fn (import, pos) =>
wenzelm@65445
   588
        (case Thy_Info.lookup_theory import of
wenzelm@59716
   589
          NONE =>
wenzelm@59716
   590
            maybe_end_theory pos
wenzelm@52536
   591
              (case get_result (snd (the (AList.lookup (op =) deps import))) of
wenzelm@69886
   592
                NONE => Toplevel.init_toplevel ()
wenzelm@68869
   593
              | SOME (_, eval) => maybe_eval_result eval)
wenzelm@59716
   594
        | some => some)
wenzelm@59716
   595
        |> Option.map (fn thy => (thy, (pos, Theory.get_markup thy))));
wenzelm@59716
   596
wenzelm@59716
   597
    val parents =
wenzelm@67380
   598
      if null parents_reports then [Theory.get_pure ()] else map #1 parents_reports;
wenzelm@59716
   599
    val _ = Position.reports (map #2 parents_reports);
wenzelm@68323
   600
    val thy = Resources.begin_theory master_dir header parents;
wenzelm@68323
   601
    val _ = Output.status (Markup.markup_only Markup.initialized);
wenzelm@68323
   602
  in thy end;
wenzelm@47335
   603
wenzelm@67215
   604
fun check_root_theory node =
wenzelm@67215
   605
  let
wenzelm@67215
   606
    val master_dir = master_directory node;
wenzelm@67215
   607
    val header = #header (get_header node);
wenzelm@67215
   608
    val header_name = #1 (#name header);
wenzelm@67215
   609
    val parent =
wenzelm@67215
   610
      if header_name = Sessions.root_name then
wenzelm@67215
   611
        SOME (Thy_Info.get_theory Sessions.theory_name)
wenzelm@67215
   612
      else if member (op =) Thy_Header.ml_roots header_name then
wenzelm@67215
   613
        SOME (Thy_Info.get_theory Thy_Header.ml_bootstrapN)
wenzelm@67215
   614
      else NONE;
wenzelm@67215
   615
  in parent |> Option.map (fn thy => Resources.begin_theory master_dir header [thy]) end;
wenzelm@62895
   616
wenzelm@47407
   617
fun check_theory full name node =
wenzelm@65445
   618
  is_some (Thy_Info.lookup_theory name) orelse
wenzelm@59715
   619
  null (#errors (get_header node)) andalso (not full orelse is_some (get_result node));
wenzelm@47335
   620
wenzelm@58923
   621
fun last_common keywords state node_required node0 node =
wenzelm@44482
   622
  let
wenzelm@44645
   623
    fun update_flags prev (visible, initial) =
wenzelm@44645
   624
      let
wenzelm@52569
   625
        val visible' = visible andalso prev <> visible_last node;
wenzelm@44645
   626
        val initial' = initial andalso
wenzelm@44645
   627
          (case prev of
wenzelm@44645
   628
            NONE => true
wenzelm@59735
   629
          | SOME command_id => the_command_name state command_id <> Thy_Header.theoryN);
wenzelm@44645
   630
      in (visible', initial') end;
wenzelm@52569
   631
wenzelm@52784
   632
    fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
wenzelm@52784
   633
      if ok then
wenzelm@47630
   634
        let
wenzelm@52784
   635
          val flags' as (visible', _) = update_flags prev flags;
wenzelm@52784
   636
          val ok' =
wenzelm@52586
   637
            (case (lookup_entry node0 command_id, opt_exec) of
wenzelm@52784
   638
              (SOME (eval0, _), SOME (eval, _)) =>
wenzelm@52784
   639
                Command.eval_eq (eval0, eval) andalso
wenzelm@52784
   640
                  (visible' orelse node_required orelse Command.eval_running eval)
wenzelm@52586
   641
            | _ => false);
wenzelm@52784
   642
          val assign_update' = assign_update |> ok' ?
wenzelm@52569
   643
            (case opt_exec of
wenzelm@52569
   644
              SOME (eval, prints) =>
wenzelm@52569
   645
                let
wenzelm@52569
   646
                  val command_visible = visible_command node command_id;
wenzelm@52850
   647
                  val command_overlays = overlays node command_id;
wenzelm@52569
   648
                  val command_name = the_command_name state command_id;
wenzelm@52569
   649
                in
wenzelm@58923
   650
                  (case
wenzelm@58923
   651
                    Command.print command_visible command_overlays keywords command_name eval prints
wenzelm@58923
   652
                   of
wenzelm@52569
   653
                    SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
wenzelm@52569
   654
                  | NONE => I)
wenzelm@52569
   655
                end
wenzelm@52569
   656
            | NONE => I);
wenzelm@52784
   657
        in SOME (prev, ok', flags', assign_update') end
wenzelm@47630
   658
      else NONE;
wenzelm@52784
   659
    val (common, ok, flags, assign_update') =
wenzelm@52569
   660
      iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
wenzelm@52569
   661
    val (common', flags') =
wenzelm@52784
   662
      if ok then
wenzelm@52569
   663
        let val last = Entries.get_after (get_entries node) common
wenzelm@52569
   664
        in (last, update_flags last flags) end
wenzelm@52569
   665
      else (common, flags);
wenzelm@52569
   666
  in (assign_update', common', flags') end;
wenzelm@44645
   667
wenzelm@62895
   668
fun illegal_init _ = error "Illegal theory header";
wenzelm@52534
   669
wenzelm@58923
   670
fun new_exec keywords state node proper_init command_id' (assign_update, command_exec, init) =
wenzelm@47407
   671
  if not proper_init andalso is_none init then NONE
wenzelm@44645
   672
  else
wenzelm@44645
   673
    let
wenzelm@52850
   674
      val command_visible = visible_command node command_id';
wenzelm@52850
   675
      val command_overlays = overlays node command_id';
wenzelm@59689
   676
      val (command_name, blob_digests, blobs_index, span0) = the_command state command_id';
wenzelm@56447
   677
      val blobs = map (resolve_blob state) blob_digests;
wenzelm@54519
   678
      val span = Lazy.force span0;
wenzelm@52566
   679
wenzelm@54519
   680
      val eval' =
wenzelm@66367
   681
        Command.eval keywords (master_directory node) (fn () => the_default illegal_init init span)
wenzelm@68184
   682
          (blobs, blobs_index) command_id' span (#1 (#2 command_exec));
wenzelm@58923
   683
      val prints' =
wenzelm@58923
   684
        perhaps (Command.print command_visible command_overlays keywords command_name eval') [];
wenzelm@52566
   685
      val exec' = (eval', prints');
wenzelm@44482
   686
wenzelm@52566
   687
      val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
wenzelm@59735
   688
      val init' = if command_name = Thy_Header.theoryN then NONE else init;
wenzelm@66367
   689
    in SOME (assign_update', (command_id', exec'), init') end;
wenzelm@52602
   690
wenzelm@52655
   691
fun removed_execs node0 (command_id, exec_ids) =
wenzelm@52655
   692
  subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
wenzelm@52602
   693
wenzelm@68336
   694
fun print_consolidation options the_command_span node_name (assign_update, node) =
wenzelm@68336
   695
  (case finished_result_theory node of
wenzelm@68336
   696
    SOME (result_id, thy) =>
wenzelm@68336
   697
      let
wenzelm@68380
   698
        val active_tasks =
wenzelm@68380
   699
          (node, false) |-> iterate_entries (fn (_, opt_exec) => fn active =>
wenzelm@68380
   700
            if active then NONE
wenzelm@68380
   701
            else
wenzelm@68380
   702
              (case opt_exec of
wenzelm@68380
   703
                NONE => NONE
wenzelm@68380
   704
              | SOME (eval, _) =>
wenzelm@68380
   705
                  SOME (not (null (Execution.snapshot [Command.eval_exec_id eval])))));
wenzelm@68336
   706
      in
wenzelm@68380
   707
        if not active_tasks then
wenzelm@68336
   708
          let
wenzelm@68336
   709
            val consolidation =
wenzelm@68336
   710
              if Options.bool options "editor_presentation" then
wenzelm@68336
   711
                let
wenzelm@68336
   712
                  val (_, offsets, rev_segments) =
wenzelm@68336
   713
                    iterate_entries (fn (_, opt_exec) => fn (offset, offsets, segments) =>
wenzelm@68336
   714
                      (case opt_exec of
wenzelm@68336
   715
                        SOME (eval, _) =>
wenzelm@68336
   716
                          let
wenzelm@68336
   717
                            val command_id = Command.eval_command_id eval;
wenzelm@68336
   718
                            val span = the_command_span command_id;
wenzelm@68336
   719
wenzelm@68336
   720
                            val exec_id = Command.eval_exec_id eval;
wenzelm@68336
   721
                            val tr = Command.eval_result_command eval;
wenzelm@68336
   722
                            val st' = Command.eval_result_state eval;
wenzelm@68336
   723
wenzelm@68336
   724
                            val offset' = offset + the_default 0 (Command_Span.symbol_length span);
wenzelm@68336
   725
                            val offsets' = offsets
wenzelm@68336
   726
                              |> Inttab.update (command_id, offset)
wenzelm@68336
   727
                              |> Inttab.update (exec_id, offset);
wenzelm@68336
   728
                            val segments' = (span, tr, st') :: segments;
wenzelm@68336
   729
                          in SOME (offset', offsets', segments') end
wenzelm@68336
   730
                      | NONE => NONE)) node (0, Inttab.empty, []);
wenzelm@68336
   731
wenzelm@68336
   732
                  val adjust = Inttab.lookup offsets;
wenzelm@68336
   733
                  val segments =
wenzelm@68336
   734
                    rev rev_segments |> map (fn (span, tr, st') =>
wenzelm@68336
   735
                      {span = Command_Span.adjust_offsets adjust span, command = tr, state = st'});
wenzelm@68336
   736
wenzelm@68336
   737
                  val presentation_context: Thy_Info.presentation_context =
wenzelm@68336
   738
                   {options = options,
wenzelm@68336
   739
                    file_pos = Position.file node_name,
wenzelm@68336
   740
                    adjust_pos = Position.adjust_offsets adjust,
wenzelm@68336
   741
                    segments = segments};
wenzelm@68336
   742
                in
wenzelm@68344
   743
                  fn _ =>
wenzelm@68538
   744
                    Exn.release
wenzelm@68538
   745
                      (Exn.capture (Thy_Info.apply_presentation presentation_context) thy
wenzelm@68538
   746
                        before commit_consolidated node)
wenzelm@68336
   747
                end
wenzelm@68344
   748
              else fn _ => commit_consolidated node;
wenzelm@68336
   749
wenzelm@68336
   750
            val result_entry =
wenzelm@68336
   751
              (case lookup_entry node result_id of
wenzelm@68336
   752
                NONE => err_undef "result command entry" result_id
wenzelm@68336
   753
              | SOME (eval, prints) =>
wenzelm@68344
   754
                  let
wenzelm@68344
   755
                    val print = eval |> Command.print0
wenzelm@68344
   756
                      {pri = Task_Queue.urgent_pri + 1, print_fn = K consolidation};
wenzelm@68344
   757
                  in (result_id, SOME (eval, print :: prints)) end);
wenzelm@68336
   758
wenzelm@68336
   759
            val assign_update' = assign_update |> assign_update_change result_entry;
wenzelm@68336
   760
            val node' = node |> assign_entry result_entry;
wenzelm@68336
   761
          in (assign_update', node') end
wenzelm@68336
   762
        else (assign_update, node)
wenzelm@68336
   763
      end
wenzelm@68336
   764
  | NONE => (assign_update, node));
wenzelm@38418
   765
in
wenzelm@38418
   766
wenzelm@68336
   767
fun update old_version_id new_version_id edits consolidate state =
wenzelm@68336
   768
  Runtime.exn_trace_system (fn () =>
wenzelm@38418
   769
  let
wenzelm@68336
   770
    val options = Options.default ();
wenzelm@68336
   771
    val the_command_span = Outer_Syntax.make_span o Lazy.force o #4 o the_command state;
wenzelm@68336
   772
wenzelm@52536
   773
    val old_version = the_version state old_version_id;
wenzelm@59086
   774
    val new_version =
wenzelm@59086
   775
      timeit "Document.edit_nodes"
wenzelm@59086
   776
        (fn () => old_version |> fold edit_nodes edits |> edit_keywords edits);
wenzelm@38418
   777
wenzelm@68381
   778
    val consolidate = Symtab.defined (Symtab.make_set consolidate);
wenzelm@44544
   779
    val nodes = nodes_of new_version;
wenzelm@52776
   780
    val required = make_required nodes;
wenzelm@52776
   781
    val required0 = make_required (nodes_of old_version);
wenzelm@52772
   782
    val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
wenzelm@44544
   783
wenzelm@47628
   784
    val updated = timeit "Document.update" (fn () =>
wenzelm@50862
   785
      nodes |> String_Graph.schedule
wenzelm@44199
   786
        (fn deps => fn (name, node) =>
wenzelm@47406
   787
          (singleton o Future.forks)
wenzelm@47406
   788
            {name = "Document.update", group = NONE,
wenzelm@52716
   789
              deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@59056
   790
            (fn () =>
wenzelm@59056
   791
              timeit ("Document.update " ^ name) (fn () =>
wenzelm@59056
   792
                Runtime.exn_trace_system (fn () =>
wenzelm@44482
   793
                  let
wenzelm@67215
   794
                    val root_theory = check_root_theory node;
wenzelm@59086
   795
                    val keywords = the_default (Session.get_keywords ()) (get_keywords node);
wenzelm@68336
   796
wenzelm@68381
   797
                    val maybe_consolidate = consolidate name andalso could_consolidate node;
wenzelm@59056
   798
                    val imports = map (apsnd Future.join) deps;
wenzelm@59056
   799
                    val imports_result_changed = exists (#4 o #1 o #2) imports;
wenzelm@59056
   800
                    val node_required = Symtab.defined required name;
wenzelm@59056
   801
                  in
wenzelm@68336
   802
                    if Symtab.defined edited name orelse maybe_consolidate orelse
wenzelm@68336
   803
                      visible_node node orelse imports_result_changed orelse
wenzelm@68336
   804
                      Symtab.defined required0 name <> node_required
wenzelm@59056
   805
                    then
wenzelm@59056
   806
                      let
wenzelm@59056
   807
                        val node0 = node_of old_version name;
wenzelm@59056
   808
                        val init = init_theory imports node;
wenzelm@59056
   809
                        val proper_init =
wenzelm@67215
   810
                          is_some root_theory orelse
wenzelm@62895
   811
                            check_theory false name node andalso
wenzelm@62895
   812
                            forall (fn (name, (_, node)) => check_theory true name node) imports;
wenzelm@47406
   813
wenzelm@59056
   814
                        val (print_execs, common, (still_visible, initial)) =
wenzelm@70283
   815
                          if imports_result_changed
wenzelm@70283
   816
                          then (assign_update_empty, NONE, (true, true))
wenzelm@59056
   817
                          else last_common keywords state node_required node0 node;
wenzelm@62895
   818
wenzelm@62895
   819
                        val common_command_exec =
wenzelm@62895
   820
                          (case common of
wenzelm@62895
   821
                            SOME id => (id, the_default Command.no_exec (the_entry node id))
wenzelm@67215
   822
                          | NONE => (Document_ID.none, Command.init_exec root_theory));
wenzelm@44479
   823
wenzelm@66367
   824
                        val (updated_execs, (command_id', exec'), _) =
wenzelm@59056
   825
                          (print_execs, common_command_exec, if initial then SOME init else NONE)
wenzelm@59056
   826
                          |> (still_visible orelse node_required) ?
wenzelm@59056
   827
                            iterate_entries_after common
wenzelm@59056
   828
                              (fn ((prev, id), _) => fn res =>
wenzelm@59056
   829
                                if not node_required andalso prev = visible_last node then NONE
wenzelm@59056
   830
                                else new_exec keywords state node proper_init id res) node;
wenzelm@44479
   831
wenzelm@68336
   832
                        val assign_update =
wenzelm@59056
   833
                          (node0, updated_execs) |-> iterate_entries_after common
wenzelm@59056
   834
                            (fn ((_, command_id0), exec0) => fn res =>
wenzelm@59056
   835
                              if is_none exec0 then NONE
wenzelm@59056
   836
                              else if assign_update_defined updated_execs command_id0 then SOME res
wenzelm@59056
   837
                              else SOME (assign_update_new (command_id0, NONE) res));
wenzelm@44480
   838
wenzelm@59056
   839
                        val last_exec =
wenzelm@59056
   840
                          if command_id' = Document_ID.none then NONE else SOME command_id';
wenzelm@59056
   841
                        val result =
wenzelm@59056
   842
                          if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
wenzelm@68336
   843
                          else SOME (command_id', #1 exec');
wenzelm@52602
   844
wenzelm@66379
   845
                        val result_changed =
wenzelm@68336
   846
                          not (eq_option (Command.eval_eq o apply2 #2) (get_result node0, result));
wenzelm@68336
   847
                        val (assign_update', node') = node
wenzelm@68336
   848
                          |> assign_update_apply assign_update
wenzelm@66379
   849
                          |> set_result result
wenzelm@68336
   850
                          |> result_changed ? reset_consolidated
wenzelm@68336
   851
                          |> pair assign_update
wenzelm@68336
   852
                          |> (not result_changed andalso maybe_consolidate) ?
wenzelm@68336
   853
                              print_consolidation options the_command_span name;
wenzelm@68336
   854
wenzelm@68336
   855
                        val assign_result = assign_update_result assign_update';
wenzelm@68336
   856
                        val removed = maps (removed_execs node0) assign_result;
wenzelm@68336
   857
                        val _ = List.app Execution.cancel removed;
wenzelm@68336
   858
wenzelm@59056
   859
                        val assigned_node = SOME (name, node');
wenzelm@68336
   860
                      in ((removed, assign_result, assigned_node, result_changed), node') end
wenzelm@59056
   861
                    else (([], [], NONE, false), node)
wenzelm@59056
   862
                  end))))
wenzelm@47628
   863
      |> Future.joins |> map #1);
wenzelm@44476
   864
wenzelm@52655
   865
    val removed = maps #1 updated;
wenzelm@68336
   866
    val assign_result = maps #2 updated;
wenzelm@52602
   867
    val assigned_nodes = map_filter #3 updated;
wenzelm@52602
   868
wenzelm@44197
   869
    val state' = state
wenzelm@68366
   870
      |> define_version new_version_id new_version assigned_nodes;
wenzelm@52602
   871
wenzelm@70284
   872
  in (Symtab.keys edited, removed, assign_result, state') end);
wenzelm@38418
   873
wenzelm@38418
   874
end;
wenzelm@38418
   875
wenzelm@38418
   876
wenzelm@43713
   877
wenzelm@43713
   878
(** global state **)
wenzelm@43713
   879
wenzelm@52508
   880
val global_state = Synchronized.var "Document.global_state" init_state;
wenzelm@43713
   881
wenzelm@43713
   882
fun state () = Synchronized.value global_state;
wenzelm@43713
   883
val change_state = Synchronized.change global_state;
wenzelm@43713
   884
wenzelm@38418
   885
end;