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