src/Pure/PIDE/document.ML
author wenzelm
Mon Aug 07 14:06:24 2017 +0200 (22 months ago ago)
changeset 66372 d003b44674c1
parent 66370 b60afdf1177d
child 66382 6392766f3c25
permissions -rw-r--r--
tuned spelling;
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@52530
    27
  val update: Document_ID.version -> Document_ID.version -> edit list -> state ->
wenzelm@52655
    28
    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@66372
    61
  entries: Command.exec option Entries.T,  (*command entries with executions*)
wenzelm@52526
    62
  result: Command.eval option}  (*result of last execution*)
wenzelm@50862
    63
and version = Version of node String_Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    64
with
wenzelm@38418
    65
wenzelm@59086
    66
fun make_node (header, keywords, perspective, entries, result) =
wenzelm@59086
    67
  Node {header = header, keywords = keywords, perspective = perspective,
wenzelm@59086
    68
    entries = entries, result = result};
wenzelm@43668
    69
wenzelm@59086
    70
fun map_node f (Node {header, keywords, perspective, entries, result}) =
wenzelm@59086
    71
  make_node (f (header, keywords, perspective, entries, result));
wenzelm@38418
    72
wenzelm@52849
    73
fun make_perspective (required, command_ids, overlays) : perspective =
wenzelm@52849
    74
 {required = required,
wenzelm@52849
    75
  visible = Inttab.make_set command_ids,
wenzelm@52849
    76
  visible_last = try List.last command_ids,
wenzelm@52862
    77
  overlays = Inttab.make_list overlays};
wenzelm@44441
    78
wenzelm@59715
    79
val no_header: node_header =
wenzelm@59715
    80
  {master = "", header = Thy_Header.make ("", Position.none) [] [], errors = []};
wenzelm@52849
    81
val no_perspective = make_perspective (false, [], []);
wenzelm@44386
    82
wenzelm@59086
    83
val empty_node = make_node (no_header, NONE, no_perspective, Entries.empty, NONE);
wenzelm@44386
    84
wenzelm@57615
    85
fun is_no_perspective ({required, visible, visible_last, overlays}: perspective) =
wenzelm@57615
    86
  not required andalso
wenzelm@57615
    87
  Inttab.is_empty visible andalso
wenzelm@57615
    88
  is_none visible_last andalso
wenzelm@57615
    89
  Inttab.is_empty overlays;
wenzelm@57615
    90
wenzelm@59086
    91
fun is_empty_node (Node {header, keywords, perspective, entries, result}) =
wenzelm@57615
    92
  header = no_header andalso
wenzelm@59086
    93
  is_none keywords andalso
wenzelm@57615
    94
  is_no_perspective perspective andalso
wenzelm@57615
    95
  Entries.is_empty entries andalso
wenzelm@57615
    96
  is_none result;
wenzelm@57615
    97
wenzelm@44386
    98
wenzelm@44386
    99
(* basic components *)
wenzelm@44386
   100
wenzelm@59715
   101
fun master_directory (Node {header = {master, ...}, ...}) =
wenzelm@54558
   102
  (case try Url.explode master of
wenzelm@54558
   103
    SOME (Url.File path) => path
wenzelm@54558
   104
  | _ => Path.current);
wenzelm@54558
   105
wenzelm@59715
   106
fun set_header master header errors =
wenzelm@59086
   107
  map_node (fn (_, keywords, perspective, entries, result) =>
wenzelm@59715
   108
    ({master = master, header = header, errors = errors}, keywords, perspective, entries, result));
wenzelm@59086
   109
wenzelm@59715
   110
fun get_header (Node {header, ...}) = header;
wenzelm@48707
   111
wenzelm@59086
   112
fun set_keywords keywords =
wenzelm@59086
   113
  map_node (fn (header, _, perspective, entries, result) =>
wenzelm@59086
   114
    (header, keywords, perspective, entries, result));
wenzelm@59086
   115
wenzelm@59086
   116
fun get_keywords (Node {keywords, ...}) = keywords;
wenzelm@59086
   117
wenzelm@48927
   118
fun read_header node span =
wenzelm@48927
   119
  let
wenzelm@59715
   120
    val {header, errors, ...} = get_header node;
wenzelm@59715
   121
    val _ =
wenzelm@59715
   122
      if null errors then ()
wenzelm@59715
   123
      else
wenzelm@59715
   124
        cat_lines errors |>
wenzelm@59715
   125
        (case Position.get_id (Position.thread_data ()) of
wenzelm@59715
   126
          NONE => I
wenzelm@59715
   127
        | SOME id => Protocol_Message.command_positions_yxml id)
wenzelm@59715
   128
        |> error;
wenzelm@59934
   129
    val {name = (name, _), imports, ...} = header;
wenzelm@59934
   130
    val {name = (_, pos), imports = imports', keywords} = Thy_Header.read_tokens span;
wenzelm@63022
   131
    val imports'' = (map #1 imports ~~ map #2 imports') handle ListPair.UnequalLengths => imports;
wenzelm@63022
   132
  in Thy_Header.make (name, pos) imports'' keywords end;
wenzelm@54526
   133
wenzelm@44385
   134
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@59086
   135
wenzelm@52808
   136
fun set_perspective args =
wenzelm@59086
   137
  map_node (fn (header, keywords, _, entries, result) =>
wenzelm@59086
   138
    (header, keywords, make_perspective args, entries, result));
wenzelm@44385
   139
wenzelm@52849
   140
val required_node = #required o get_perspective;
wenzelm@52849
   141
val visible_command = Inttab.defined o #visible o get_perspective;
wenzelm@52849
   142
val visible_last = #visible_last o get_perspective;
wenzelm@47345
   143
val visible_node = is_some o visible_last
wenzelm@52850
   144
val overlays = Inttab.lookup_list o #overlays o get_perspective;
wenzelm@47345
   145
wenzelm@44444
   146
fun map_entries f =
wenzelm@59086
   147
  map_node (fn (header, keywords, perspective, entries, result) =>
wenzelm@59086
   148
    (header, keywords, perspective, f entries, result));
wenzelm@59086
   149
wenzelm@52761
   150
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44645
   151
wenzelm@44645
   152
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@52761
   153
fun iterate_entries_after start f (Node {entries, ...}) =
wenzelm@44645
   154
  (case Entries.get_after entries start of
wenzelm@44645
   155
    NONE => I
wenzelm@44645
   156
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   157
wenzelm@47339
   158
fun get_result (Node {result, ...}) = result;
wenzelm@59086
   159
wenzelm@44385
   160
fun set_result result =
wenzelm@59086
   161
  map_node (fn (header, keywords, perspective, entries, _) =>
wenzelm@59086
   162
    (header, keywords, perspective, entries, result));
wenzelm@44180
   163
wenzelm@52566
   164
fun changed_result node node' =
wenzelm@52566
   165
  (case (get_result node, get_result node') of
wenzelm@52607
   166
    (SOME eval, SOME eval') => not (Command.eval_eq (eval, eval'))
wenzelm@52566
   167
  | (NONE, NONE) => false
wenzelm@52566
   168
  | _ => true);
wenzelm@52566
   169
wenzelm@52772
   170
fun pending_result node =
wenzelm@52772
   171
  (case get_result node of
wenzelm@52772
   172
    SOME eval => not (Command.eval_finished eval)
wenzelm@52772
   173
  | NONE => false);
wenzelm@52533
   174
wenzelm@59198
   175
fun finished_result node =
wenzelm@59198
   176
  (case get_result node of
wenzelm@59198
   177
    SOME eval => Command.eval_finished eval
wenzelm@59198
   178
  | NONE => false);
wenzelm@59198
   179
wenzelm@50862
   180
fun get_node nodes name = String_Graph.get_node nodes name
wenzelm@50862
   181
  handle String_Graph.UNDEF _ => empty_node;
wenzelm@50862
   182
fun default_node name = String_Graph.default_node (name, empty_node);
wenzelm@50862
   183
fun update_node name f = default_node name #> String_Graph.map_node name f;
wenzelm@38418
   184
wenzelm@38418
   185
wenzelm@38448
   186
(* node edits and associated executions *)
wenzelm@38150
   187
wenzelm@52862
   188
type overlay = Document_ID.command * (string * string list);
wenzelm@52849
   189
wenzelm@44157
   190
datatype node_edit =
wenzelm@52530
   191
  Edits of (Document_ID.command option * Document_ID.command option) list |
wenzelm@48707
   192
  Deps of node_header |
wenzelm@52849
   193
  Perspective of bool * Document_ID.command list * overlay list;
wenzelm@44157
   194
wenzelm@44156
   195
type edit = string * node_edit;
wenzelm@38448
   196
wenzelm@49064
   197
val after_entry = Entries.get_after o get_entries;
wenzelm@44479
   198
wenzelm@49064
   199
fun lookup_entry node id =
wenzelm@49064
   200
  (case Entries.lookup (get_entries node) id of
wenzelm@44480
   201
    NONE => NONE
wenzelm@44480
   202
  | SOME (exec, _) => exec);
wenzelm@44480
   203
wenzelm@49064
   204
fun the_entry node id =
wenzelm@49064
   205
  (case Entries.lookup (get_entries node) id of
wenzelm@38448
   206
    NONE => err_undef "command entry" id
wenzelm@44476
   207
  | SOME (exec, _) => exec);
wenzelm@38150
   208
wenzelm@52566
   209
fun assign_entry (command_id, exec) node =
wenzelm@52566
   210
  if is_none (Entries.lookup (get_entries node) command_id) then node
wenzelm@52566
   211
  else map_entries (Entries.update (command_id, exec)) node;
wenzelm@38418
   212
wenzelm@38448
   213
fun reset_after id entries =
wenzelm@38448
   214
  (case Entries.get_after entries id of
wenzelm@38448
   215
    NONE => entries
wenzelm@38448
   216
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   217
wenzelm@43668
   218
val edit_node = map_entries o fold
wenzelm@43668
   219
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   220
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   221
wenzelm@38418
   222
wenzelm@38418
   223
(* version operations *)
wenzelm@38418
   224
wenzelm@50862
   225
val empty_version = Version String_Graph.empty;
wenzelm@44185
   226
wenzelm@38418
   227
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   228
val node_of = get_node o nodes_of;
wenzelm@38418
   229
wenzelm@44185
   230
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   231
wenzelm@44157
   232
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   233
  Version
wenzelm@44436
   234
    (case node_edit of
wenzelm@54562
   235
      Edits edits => update_node name (edit_node edits) nodes
wenzelm@59715
   236
    | Deps {master, header, errors} =>
wenzelm@44436
   237
        let
wenzelm@48927
   238
          val imports = map fst (#imports header);
wenzelm@44436
   239
          val nodes1 = nodes
wenzelm@44436
   240
            |> default_node name
wenzelm@48927
   241
            |> fold default_node imports;
wenzelm@44436
   242
          val nodes2 = nodes1
wenzelm@50862
   243
            |> String_Graph.Keys.fold
wenzelm@50862
   244
                (fn dep => String_Graph.del_edge (dep, name)) (String_Graph.imm_preds nodes1 name);
wenzelm@59086
   245
          val (nodes3, errors1) =
wenzelm@59086
   246
            (String_Graph.add_deps_acyclic (name, imports) nodes2, errors)
wenzelm@59086
   247
              handle String_Graph.CYCLES cs => (nodes2, errors @ map cycle_msg cs);
wenzelm@59715
   248
        in String_Graph.map_node name (set_header master header errors1) nodes3 end
wenzelm@47406
   249
    | Perspective perspective => update_node name (set_perspective perspective) nodes);
wenzelm@44444
   250
wenzelm@59086
   251
fun update_keywords name nodes =
wenzelm@59086
   252
  nodes |> String_Graph.map_node name (fn node =>
wenzelm@59086
   253
    if is_empty_node node then node
wenzelm@59086
   254
    else
wenzelm@59086
   255
      let
wenzelm@59715
   256
        val {master, header, errors} = get_header node;
wenzelm@59086
   257
        val imports_keywords = map_filter (get_keywords o get_node nodes o #1) (#imports header);
wenzelm@59086
   258
        val keywords =
wenzelm@59086
   259
          Library.foldl Keyword.merge_keywords (Session.get_keywords (), imports_keywords);
wenzelm@59086
   260
        val (keywords', errors') =
wenzelm@59086
   261
          (Keyword.add_keywords (#keywords header) keywords, errors)
wenzelm@59086
   262
            handle ERROR msg =>
wenzelm@59086
   263
              (keywords, if member (op =) errors msg then errors else errors @ [msg]);
wenzelm@59086
   264
      in
wenzelm@59086
   265
        node
wenzelm@59715
   266
        |> set_header master header errors'
wenzelm@59086
   267
        |> set_keywords (SOME keywords')
wenzelm@59086
   268
      end);
wenzelm@59086
   269
wenzelm@59086
   270
fun edit_keywords edits (Version nodes) =
wenzelm@59086
   271
  Version
wenzelm@59086
   272
    (fold update_keywords
wenzelm@59086
   273
      (String_Graph.all_succs nodes (map_filter (fn (a, Deps _) => SOME a | _ => NONE) edits))
wenzelm@59086
   274
      nodes);
wenzelm@59086
   275
wenzelm@44222
   276
fun put_node (name, node) (Version nodes) =
wenzelm@57616
   277
  let
wenzelm@57616
   278
    val nodes1 = update_node name (K node) nodes;
wenzelm@57616
   279
    val nodes2 =
wenzelm@57616
   280
      if String_Graph.is_maximal nodes1 name andalso is_empty_node node
wenzelm@57616
   281
      then String_Graph.del_node name nodes1
wenzelm@57616
   282
      else nodes1;
wenzelm@57616
   283
  in Version nodes2 end;
wenzelm@38418
   284
wenzelm@38150
   285
end;
wenzelm@38150
   286
wenzelm@38418
   287
wenzelm@52595
   288
wenzelm@52595
   289
(** main state -- document structure and execution process **)
wenzelm@52536
   290
wenzelm@56458
   291
type blob_digest = (string * string option) Exn.result;  (*file node name, raw digest*)
wenzelm@55798
   292
wenzelm@52774
   293
type execution =
wenzelm@52774
   294
 {version_id: Document_ID.version,  (*static version id*)
wenzelm@52774
   295
  execution_id: Document_ID.execution,  (*dynamic execution id*)
wenzelm@59193
   296
  delay_request: unit future};  (*pending event timer request*)
wenzelm@52604
   297
wenzelm@52774
   298
val no_execution: execution =
wenzelm@59193
   299
  {version_id = Document_ID.none,
wenzelm@59193
   300
   execution_id = Document_ID.none,
wenzelm@59193
   301
   delay_request = Future.value ()};
wenzelm@52774
   302
wenzelm@59193
   303
fun new_execution version_id delay_request : execution =
wenzelm@59193
   304
  {version_id = version_id,
wenzelm@59193
   305
   execution_id = Execution.start (),
wenzelm@59193
   306
   delay_request = delay_request};
wenzelm@52604
   307
wenzelm@38418
   308
abstype state = State of
wenzelm@52536
   309
 {versions: version Inttab.table,  (*version id -> document content*)
wenzelm@55798
   310
  blobs: (SHA1.digest * string list) Symtab.table,  (*raw digest -> digest, lines*)
wenzelm@59689
   311
  commands: (string * blob_digest list * int * Token.T list lazy) Inttab.table,
wenzelm@59689
   312
    (*command id -> name, inlined files, token index of files, command span*)
wenzelm@52536
   313
  execution: execution}  (*current execution process*)
wenzelm@38418
   314
with
wenzelm@38418
   315
wenzelm@54519
   316
fun make_state (versions, blobs, commands, execution) =
wenzelm@54519
   317
  State {versions = versions, blobs = blobs, commands = commands, execution = execution};
wenzelm@38418
   318
wenzelm@54519
   319
fun map_state f (State {versions, blobs, commands, execution}) =
wenzelm@54519
   320
  make_state (f (versions, blobs, commands, execution));
wenzelm@38418
   321
wenzelm@38418
   322
val init_state =
wenzelm@54519
   323
  make_state (Inttab.make [(Document_ID.none, empty_version)],
wenzelm@54519
   324
    Symtab.empty, Inttab.empty, no_execution);
wenzelm@38418
   325
wenzelm@38418
   326
wenzelm@38418
   327
(* document versions *)
wenzelm@38418
   328
wenzelm@52536
   329
fun define_version version_id version =
wenzelm@59193
   330
  map_state (fn (versions, blobs, commands, {delay_request, ...}) =>
wenzelm@47420
   331
    let
wenzelm@52536
   332
      val versions' = Inttab.update_new (version_id, version) versions
wenzelm@47420
   333
        handle Inttab.DUP dup => err_dup "document version" dup;
wenzelm@59193
   334
      val execution' = new_execution version_id delay_request;
wenzelm@54519
   335
    in (versions', blobs, commands, execution') end);
wenzelm@38418
   336
wenzelm@52536
   337
fun the_version (State {versions, ...}) version_id =
wenzelm@52536
   338
  (case Inttab.lookup versions version_id of
wenzelm@52536
   339
    NONE => err_undef "document version" version_id
wenzelm@38418
   340
  | SOME version => version);
wenzelm@38418
   341
wenzelm@52536
   342
fun delete_version version_id versions =
wenzelm@52536
   343
  Inttab.delete version_id versions
wenzelm@52536
   344
    handle Inttab.UNDEF _ => err_undef "document version" version_id;
wenzelm@44673
   345
wenzelm@38418
   346
wenzelm@54519
   347
(* inlined files *)
wenzelm@54519
   348
wenzelm@54519
   349
fun define_blob digest text =
wenzelm@54519
   350
  map_state (fn (versions, blobs, commands, execution) =>
wenzelm@57638
   351
    let val blobs' = Symtab.update (digest, (SHA1.fake digest, split_lines text)) blobs
wenzelm@54519
   352
    in (versions, blobs', commands, execution) end);
wenzelm@54519
   353
wenzelm@54519
   354
fun the_blob (State {blobs, ...}) digest =
wenzelm@54519
   355
  (case Symtab.lookup blobs digest of
wenzelm@54519
   356
    NONE => error ("Undefined blob: " ^ digest)
wenzelm@55788
   357
  | SOME content => content);
wenzelm@54519
   358
wenzelm@56447
   359
fun resolve_blob state (blob_digest: blob_digest) =
wenzelm@61077
   360
  blob_digest |> Exn.map_res (fn (file_node, raw_digest) =>
wenzelm@56458
   361
    (file_node, Option.map (the_blob state) raw_digest));
wenzelm@56447
   362
wenzelm@59685
   363
fun blob_reports pos (blob_digest: blob_digest) =
wenzelm@59685
   364
  (case blob_digest of Exn.Res (file_node, _) => [(pos, Markup.path file_node)] | _ => []);
wenzelm@59685
   365
wenzelm@54519
   366
wenzelm@38418
   367
(* commands *)
wenzelm@38418
   368
wenzelm@59704
   369
fun define_command command_id name blobs_digests blobs_index toks =
wenzelm@54519
   370
  map_state (fn (versions, blobs, commands, execution) =>
wenzelm@38418
   371
    let
wenzelm@52536
   372
      val id = Document_ID.print command_id;
wenzelm@52536
   373
      val span =
wenzelm@66168
   374
        Lazy.lazy_name "Document.define_command" (fn () =>
wenzelm@52536
   375
          Position.setmp_thread_data (Position.id_only id)
wenzelm@59685
   376
            (fn () =>
wenzelm@59685
   377
              let
wenzelm@59685
   378
                val (tokens, _) = fold_map Token.make toks (Position.id id);
wenzelm@59685
   379
                val _ =
wenzelm@59704
   380
                  if blobs_index < 0
wenzelm@59704
   381
                  then (*inlined errors*)
wenzelm@59704
   382
                    map_filter Exn.get_exn blobs_digests
wenzelm@59704
   383
                    |> List.app (Output.error_message o Runtime.exn_message)
wenzelm@59704
   384
                  else (*auxiliary files*)
wenzelm@60034
   385
                    let val pos = Token.pos_of (nth tokens blobs_index)
wenzelm@60034
   386
                    in Position.reports (maps (blob_reports pos) blobs_digests) end;
wenzelm@59685
   387
              in tokens end) ());
wenzelm@59685
   388
      val commands' =
wenzelm@59704
   389
        Inttab.update_new (command_id, (name, blobs_digests, blobs_index, span)) commands
wenzelm@59685
   390
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@47395
   391
      val _ =
wenzelm@52536
   392
        Position.setmp_thread_data (Position.id_only id)
wenzelm@50201
   393
          (fn () => Output.status (Markup.markup_only Markup.accepted)) ();
wenzelm@54519
   394
    in (versions, blobs, commands', execution) end);
wenzelm@38418
   395
wenzelm@52536
   396
fun the_command (State {commands, ...}) command_id =
wenzelm@52536
   397
  (case Inttab.lookup commands command_id of
wenzelm@52536
   398
    NONE => err_undef "command" command_id
wenzelm@44644
   399
  | SOME command => command);
wenzelm@38418
   400
wenzelm@52569
   401
val the_command_name = #1 oo the_command;
wenzelm@52569
   402
wenzelm@60880
   403
fun command_exec state node_name command_id =
wenzelm@60880
   404
  let
wenzelm@60880
   405
    val State {execution = {version_id, ...}, ...} = state;
wenzelm@60880
   406
    val node = get_node (nodes_of (the_version state version_id)) node_name;
wenzelm@60880
   407
  in the_entry node command_id end;
wenzelm@60880
   408
wenzelm@47420
   409
end;
wenzelm@38418
   410
wenzelm@52566
   411
wenzelm@52566
   412
(* remove_versions *)
wenzelm@52566
   413
wenzelm@54519
   414
fun remove_versions version_ids state = state |> map_state (fn (versions, _, _, execution) =>
wenzelm@47420
   415
  let
wenzelm@52536
   416
    val _ =
wenzelm@52536
   417
      member (op =) version_ids (#version_id execution) andalso
wenzelm@52536
   418
        error ("Attempt to remove execution version " ^ Document_ID.print (#version_id execution));
wenzelm@47343
   419
wenzelm@52536
   420
    val versions' = fold delete_version version_ids versions;
wenzelm@47420
   421
    val commands' =
wenzelm@47420
   422
      (versions', Inttab.empty) |->
wenzelm@47420
   423
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@50862
   424
          String_Graph.fold (fn (_, (node, _)) => node |>
wenzelm@52536
   425
            iterate_entries (fn ((_, command_id), _) =>
wenzelm@52536
   426
              SOME o Inttab.insert (K true) (command_id, the_command state command_id))));
wenzelm@54519
   427
    val blobs' =
wenzelm@54519
   428
      (commands', Symtab.empty) |->
wenzelm@59689
   429
        Inttab.fold (fn (_, (_, blobs, _, _)) => blobs |>
wenzelm@56458
   430
          fold (fn Exn.Res (_, SOME b) => Symtab.update (b, the_blob state b) | _ => I));
wenzelm@54519
   431
wenzelm@54519
   432
  in (versions', blobs', commands', execution) end);
wenzelm@38418
   433
wenzelm@38418
   434
wenzelm@52604
   435
(* document execution *)
wenzelm@47389
   436
wenzelm@60198
   437
fun make_required nodes =
wenzelm@60198
   438
  let
wenzelm@60198
   439
    fun all_preds P =
wenzelm@60198
   440
      String_Graph.fold (fn (a, (node, _)) => P node ? cons a) nodes []
wenzelm@60198
   441
      |> String_Graph.all_preds nodes
wenzelm@60198
   442
      |> Symtab.make_set;
wenzelm@60198
   443
wenzelm@60198
   444
    val all_visible = all_preds visible_node;
wenzelm@60198
   445
    val all_required = all_preds required_node;
wenzelm@60198
   446
  in
wenzelm@60198
   447
    Symtab.fold (fn (a, ()) =>
wenzelm@60198
   448
      exists (Symtab.defined all_visible) (String_Graph.immediate_succs nodes a) ?
wenzelm@60198
   449
        Symtab.update (a, ())) all_visible all_required
wenzelm@60198
   450
  end;
wenzelm@60198
   451
wenzelm@54519
   452
fun start_execution state = state |> map_state (fn (versions, blobs, commands, execution) =>
wenzelm@52796
   453
  timeit "Document.start_execution" (fn () =>
wenzelm@52796
   454
    let
wenzelm@59193
   455
      val {version_id, execution_id, delay_request} = execution;
wenzelm@52798
   456
wenzelm@52798
   457
      val delay = seconds (Options.default_real "editor_execution_delay");
wenzelm@52774
   458
wenzelm@52798
   459
      val _ = Future.cancel delay_request;
wenzelm@62826
   460
      val delay_request' = Event_Timer.future (Time.now () + delay);
wenzelm@52798
   461
wenzelm@59198
   462
      fun finished_import (name, (node, _)) =
wenzelm@65445
   463
        finished_result node orelse is_some (Thy_Info.lookup_theory name);
wenzelm@60198
   464
wenzelm@60198
   465
      val nodes = nodes_of (the_version state version_id);
wenzelm@60198
   466
      val required = make_required nodes;
wenzelm@59193
   467
      val _ =
wenzelm@60198
   468
        nodes |> String_Graph.schedule
wenzelm@52573
   469
          (fn deps => fn (name, node) =>
wenzelm@60198
   470
            if Symtab.defined required name orelse visible_node node orelse pending_result node then
wenzelm@52774
   471
              let
wenzelm@52775
   472
                fun body () =
wenzelm@59347
   473
                  (if forall finished_import deps then
wenzelm@59198
   474
                    iterate_entries (fn (_, opt_exec) => fn () =>
wenzelm@59198
   475
                      (case opt_exec of
wenzelm@59198
   476
                        SOME exec =>
wenzelm@59198
   477
                          if Execution.is_running execution_id
wenzelm@59198
   478
                          then SOME (Command.exec execution_id exec)
wenzelm@59198
   479
                          else NONE
wenzelm@59198
   480
                      | NONE => NONE)) node ()
wenzelm@59347
   481
                   else ())
wenzelm@62505
   482
                   handle exn => (Output.system_message (Runtime.exn_message exn); Exn.reraise exn);
wenzelm@52774
   483
                val future =
wenzelm@52774
   484
                  (singleton o Future.forks)
wenzelm@59193
   485
                   {name = "theory:" ^ name,
wenzelm@59193
   486
                    group = SOME (Future.new_group NONE),
wenzelm@59198
   487
                    deps = Future.task_of delay_request' :: maps (the_list o #2 o #2) deps,
wenzelm@59193
   488
                    pri = 0, interrupts = false} body;
wenzelm@59198
   489
              in (node, SOME (Future.task_of future)) end
wenzelm@59198
   490
            else (node, NONE));
wenzelm@52798
   491
      val execution' =
wenzelm@59193
   492
        {version_id = version_id, execution_id = execution_id, delay_request = delay_request'};
wenzelm@54519
   493
    in (versions, blobs, commands, execution') end));
wenzelm@47345
   494
wenzelm@47345
   495
wenzelm@47420
   496
wenzelm@47420
   497
(** document update **)
wenzelm@38418
   498
wenzelm@52566
   499
(* exec state assignment *)
wenzelm@52566
   500
wenzelm@52566
   501
type assign_update = Command.exec option Inttab.table;  (*command id -> exec*)
wenzelm@52566
   502
wenzelm@52566
   503
val assign_update_empty: assign_update = Inttab.empty;
wenzelm@52566
   504
fun assign_update_defined (tab: assign_update) command_id = Inttab.defined tab command_id;
wenzelm@52566
   505
fun assign_update_apply (tab: assign_update) node = Inttab.fold assign_entry tab node;
wenzelm@52566
   506
wenzelm@52566
   507
fun assign_update_new upd (tab: assign_update) =
wenzelm@52566
   508
  Inttab.update_new upd tab
wenzelm@52566
   509
    handle Inttab.DUP dup => err_dup "exec state assignment" dup;
wenzelm@52566
   510
wenzelm@52566
   511
fun assign_update_result (tab: assign_update) =
wenzelm@52566
   512
  Inttab.fold (fn (command_id, exec) => cons (command_id, Command.exec_ids exec)) tab [];
wenzelm@52566
   513
wenzelm@52566
   514
wenzelm@52566
   515
(* update *)
wenzelm@52566
   516
wenzelm@38418
   517
local
wenzelm@38418
   518
wenzelm@48927
   519
fun init_theory deps node span =
wenzelm@47335
   520
  let
wenzelm@54526
   521
    val master_dir = master_directory node;
wenzelm@54526
   522
    val header = read_header node span;
wenzelm@48927
   523
    val imports = #imports header;
wenzelm@59716
   524
wenzelm@59716
   525
    fun maybe_end_theory pos st =
wenzelm@59716
   526
      SOME (Toplevel.end_theory pos st)
wenzelm@59716
   527
        handle ERROR msg => (Output.error_message msg; NONE);
wenzelm@59716
   528
    val parents_reports =
wenzelm@59716
   529
      imports |> map_filter (fn (import, pos) =>
wenzelm@65445
   530
        (case Thy_Info.lookup_theory import of
wenzelm@59716
   531
          NONE =>
wenzelm@59716
   532
            maybe_end_theory pos
wenzelm@52536
   533
              (case get_result (snd (the (AList.lookup (op =) deps import))) of
wenzelm@52536
   534
                NONE => Toplevel.toplevel
wenzelm@59716
   535
              | SOME eval => Command.eval_result_state eval)
wenzelm@59716
   536
        | some => some)
wenzelm@59716
   537
        |> Option.map (fn thy => (thy, (pos, Theory.get_markup thy))));
wenzelm@59716
   538
wenzelm@59716
   539
    val parents =
wenzelm@60937
   540
      if null parents_reports then [Thy_Info.pure_theory ()] else map #1 parents_reports;
wenzelm@59716
   541
    val _ = Position.reports (map #2 parents_reports);
wenzelm@56208
   542
  in Resources.begin_theory master_dir header parents end;
wenzelm@47335
   543
wenzelm@62895
   544
fun check_ml_root node =
wenzelm@62932
   545
  if member (op =) Thy_Header.ml_roots (#1 (#name (#header (get_header node)))) then
wenzelm@62895
   546
    let
wenzelm@62895
   547
      val master_dir = master_directory node;
wenzelm@62895
   548
      val header = #header (get_header node);
wenzelm@62895
   549
      val parent = Thy_Info.get_theory Thy_Header.ml_bootstrapN;
wenzelm@62895
   550
    in SOME (Resources.begin_theory master_dir header [parent]) end
wenzelm@62895
   551
  else NONE;
wenzelm@62895
   552
wenzelm@47407
   553
fun check_theory full name node =
wenzelm@65445
   554
  is_some (Thy_Info.lookup_theory name) orelse
wenzelm@59715
   555
  null (#errors (get_header node)) andalso (not full orelse is_some (get_result node));
wenzelm@47335
   556
wenzelm@58923
   557
fun last_common keywords state node_required node0 node =
wenzelm@44482
   558
  let
wenzelm@44645
   559
    fun update_flags prev (visible, initial) =
wenzelm@44645
   560
      let
wenzelm@52569
   561
        val visible' = visible andalso prev <> visible_last node;
wenzelm@44645
   562
        val initial' = initial andalso
wenzelm@44645
   563
          (case prev of
wenzelm@44645
   564
            NONE => true
wenzelm@59735
   565
          | SOME command_id => the_command_name state command_id <> Thy_Header.theoryN);
wenzelm@44645
   566
      in (visible', initial') end;
wenzelm@52569
   567
wenzelm@52784
   568
    fun get_common ((prev, command_id), opt_exec) (_, ok, flags, assign_update) =
wenzelm@52784
   569
      if ok then
wenzelm@47630
   570
        let
wenzelm@52784
   571
          val flags' as (visible', _) = update_flags prev flags;
wenzelm@52784
   572
          val ok' =
wenzelm@52586
   573
            (case (lookup_entry node0 command_id, opt_exec) of
wenzelm@52784
   574
              (SOME (eval0, _), SOME (eval, _)) =>
wenzelm@52784
   575
                Command.eval_eq (eval0, eval) andalso
wenzelm@52784
   576
                  (visible' orelse node_required orelse Command.eval_running eval)
wenzelm@52586
   577
            | _ => false);
wenzelm@52784
   578
          val assign_update' = assign_update |> ok' ?
wenzelm@52569
   579
            (case opt_exec of
wenzelm@52569
   580
              SOME (eval, prints) =>
wenzelm@52569
   581
                let
wenzelm@52569
   582
                  val command_visible = visible_command node command_id;
wenzelm@52850
   583
                  val command_overlays = overlays node command_id;
wenzelm@52569
   584
                  val command_name = the_command_name state command_id;
wenzelm@52569
   585
                in
wenzelm@58923
   586
                  (case
wenzelm@58923
   587
                    Command.print command_visible command_overlays keywords command_name eval prints
wenzelm@58923
   588
                   of
wenzelm@52569
   589
                    SOME prints' => assign_update_new (command_id, SOME (eval, prints'))
wenzelm@52569
   590
                  | NONE => I)
wenzelm@52569
   591
                end
wenzelm@52569
   592
            | NONE => I);
wenzelm@52784
   593
        in SOME (prev, ok', flags', assign_update') end
wenzelm@47630
   594
      else NONE;
wenzelm@52784
   595
    val (common, ok, flags, assign_update') =
wenzelm@52569
   596
      iterate_entries get_common node (NONE, true, (true, true), assign_update_empty);
wenzelm@52569
   597
    val (common', flags') =
wenzelm@52784
   598
      if ok then
wenzelm@52569
   599
        let val last = Entries.get_after (get_entries node) common
wenzelm@52569
   600
        in (last, update_flags last flags) end
wenzelm@52569
   601
      else (common, flags);
wenzelm@52569
   602
  in (assign_update', common', flags') end;
wenzelm@44645
   603
wenzelm@62895
   604
fun illegal_init _ = error "Illegal theory header";
wenzelm@52534
   605
wenzelm@58923
   606
fun new_exec keywords state node proper_init command_id' (assign_update, command_exec, init) =
wenzelm@47407
   607
  if not proper_init andalso is_none init then NONE
wenzelm@44645
   608
  else
wenzelm@44645
   609
    let
wenzelm@52850
   610
      val command_visible = visible_command node command_id';
wenzelm@52850
   611
      val command_overlays = overlays node command_id';
wenzelm@59689
   612
      val (command_name, blob_digests, blobs_index, span0) = the_command state command_id';
wenzelm@56447
   613
      val blobs = map (resolve_blob state) blob_digests;
wenzelm@54519
   614
      val span = Lazy.force span0;
wenzelm@52566
   615
wenzelm@54519
   616
      val eval' =
wenzelm@66370
   617
        Command.eval keywords (master_directory node) (fn () => the_default illegal_init init span)
wenzelm@66370
   618
          (blobs, blobs_index) span (#1 (#2 command_exec));
wenzelm@58923
   619
      val prints' =
wenzelm@58923
   620
        perhaps (Command.print command_visible command_overlays keywords command_name eval') [];
wenzelm@52566
   621
      val exec' = (eval', prints');
wenzelm@44482
   622
wenzelm@52566
   623
      val assign_update' = assign_update_new (command_id', SOME exec') assign_update;
wenzelm@59735
   624
      val init' = if command_name = Thy_Header.theoryN then NONE else init;
wenzelm@66370
   625
    in SOME (assign_update', (command_id', exec'), init') end;
wenzelm@52602
   626
wenzelm@52655
   627
fun removed_execs node0 (command_id, exec_ids) =
wenzelm@52655
   628
  subtract (op =) exec_ids (Command.exec_ids (lookup_entry node0 command_id));
wenzelm@52602
   629
wenzelm@38418
   630
in
wenzelm@38418
   631
wenzelm@59056
   632
fun update old_version_id new_version_id edits state = Runtime.exn_trace_system (fn () =>
wenzelm@38418
   633
  let
wenzelm@52536
   634
    val old_version = the_version state old_version_id;
wenzelm@59086
   635
    val new_version =
wenzelm@59086
   636
      timeit "Document.edit_nodes"
wenzelm@59086
   637
        (fn () => old_version |> fold edit_nodes edits |> edit_keywords edits);
wenzelm@38418
   638
wenzelm@44544
   639
    val nodes = nodes_of new_version;
wenzelm@52776
   640
    val required = make_required nodes;
wenzelm@52776
   641
    val required0 = make_required (nodes_of old_version);
wenzelm@52772
   642
    val edited = fold (fn (name, _) => Symtab.update (name, ())) edits Symtab.empty;
wenzelm@44544
   643
wenzelm@47628
   644
    val updated = timeit "Document.update" (fn () =>
wenzelm@50862
   645
      nodes |> String_Graph.schedule
wenzelm@44199
   646
        (fn deps => fn (name, node) =>
wenzelm@47406
   647
          (singleton o Future.forks)
wenzelm@47406
   648
            {name = "Document.update", group = NONE,
wenzelm@52716
   649
              deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@59056
   650
            (fn () =>
wenzelm@59056
   651
              timeit ("Document.update " ^ name) (fn () =>
wenzelm@59056
   652
                Runtime.exn_trace_system (fn () =>
wenzelm@44482
   653
                  let
wenzelm@62895
   654
                    val ml_root = check_ml_root node;
wenzelm@59086
   655
                    val keywords = the_default (Session.get_keywords ()) (get_keywords node);
wenzelm@59056
   656
                    val imports = map (apsnd Future.join) deps;
wenzelm@59056
   657
                    val imports_result_changed = exists (#4 o #1 o #2) imports;
wenzelm@59056
   658
                    val node_required = Symtab.defined required name;
wenzelm@59056
   659
                  in
wenzelm@59056
   660
                    if Symtab.defined edited name orelse visible_node node orelse
wenzelm@59056
   661
                      imports_result_changed orelse Symtab.defined required0 name <> node_required
wenzelm@59056
   662
                    then
wenzelm@59056
   663
                      let
wenzelm@59056
   664
                        val node0 = node_of old_version name;
wenzelm@59056
   665
                        val init = init_theory imports node;
wenzelm@59056
   666
                        val proper_init =
wenzelm@62895
   667
                          is_some ml_root orelse
wenzelm@62895
   668
                            check_theory false name node andalso
wenzelm@62895
   669
                            forall (fn (name, (_, node)) => check_theory true name node) imports;
wenzelm@47406
   670
wenzelm@59056
   671
                        val (print_execs, common, (still_visible, initial)) =
wenzelm@59056
   672
                          if imports_result_changed then (assign_update_empty, NONE, (true, true))
wenzelm@59056
   673
                          else last_common keywords state node_required node0 node;
wenzelm@62895
   674
wenzelm@62895
   675
                        val common_command_exec =
wenzelm@62895
   676
                          (case common of
wenzelm@62895
   677
                            SOME id => (id, the_default Command.no_exec (the_entry node id))
wenzelm@62895
   678
                          | NONE => (Document_ID.none, Command.init_exec ml_root));
wenzelm@44479
   679
wenzelm@66370
   680
                        val (updated_execs, (command_id', exec'), _) =
wenzelm@59056
   681
                          (print_execs, common_command_exec, if initial then SOME init else NONE)
wenzelm@59056
   682
                          |> (still_visible orelse node_required) ?
wenzelm@59056
   683
                            iterate_entries_after common
wenzelm@59056
   684
                              (fn ((prev, id), _) => fn res =>
wenzelm@59056
   685
                                if not node_required andalso prev = visible_last node then NONE
wenzelm@59056
   686
                                else new_exec keywords state node proper_init id res) node;
wenzelm@44479
   687
wenzelm@59056
   688
                        val assigned_execs =
wenzelm@59056
   689
                          (node0, updated_execs) |-> iterate_entries_after common
wenzelm@59056
   690
                            (fn ((_, command_id0), exec0) => fn res =>
wenzelm@59056
   691
                              if is_none exec0 then NONE
wenzelm@59056
   692
                              else if assign_update_defined updated_execs command_id0 then SOME res
wenzelm@59056
   693
                              else SOME (assign_update_new (command_id0, NONE) res));
wenzelm@44480
   694
wenzelm@59056
   695
                        val last_exec =
wenzelm@59056
   696
                          if command_id' = Document_ID.none then NONE else SOME command_id';
wenzelm@59056
   697
                        val result =
wenzelm@59056
   698
                          if is_none last_exec orelse is_some (after_entry node last_exec) then NONE
wenzelm@66370
   699
                          else SOME (#1 exec');
wenzelm@44480
   700
wenzelm@59056
   701
                        val assign_update = assign_update_result assigned_execs;
wenzelm@59056
   702
                        val removed = maps (removed_execs node0) assign_update;
wenzelm@59056
   703
                        val _ = List.app Execution.cancel removed;
wenzelm@52602
   704
wenzelm@59056
   705
                        val node' = node
wenzelm@59056
   706
                          |> assign_update_apply assigned_execs
wenzelm@59056
   707
                          |> set_result result;
wenzelm@59056
   708
                        val assigned_node = SOME (name, node');
wenzelm@59056
   709
                        val result_changed = changed_result node0 node';
wenzelm@59056
   710
                      in ((removed, assign_update, assigned_node, result_changed), node') end
wenzelm@59056
   711
                    else (([], [], NONE, false), node)
wenzelm@59056
   712
                  end))))
wenzelm@47628
   713
      |> Future.joins |> map #1);
wenzelm@44476
   714
wenzelm@52655
   715
    val removed = maps #1 updated;
wenzelm@52655
   716
    val assign_update = maps #2 updated;
wenzelm@52602
   717
    val assigned_nodes = map_filter #3 updated;
wenzelm@52602
   718
wenzelm@44197
   719
    val state' = state
wenzelm@52602
   720
      |> define_version new_version_id (fold put_node assigned_nodes new_version);
wenzelm@52602
   721
wenzelm@59056
   722
  in (removed, assign_update, state') end);
wenzelm@38418
   723
wenzelm@38418
   724
end;
wenzelm@38418
   725
wenzelm@38418
   726
wenzelm@43713
   727
wenzelm@43713
   728
(** global state **)
wenzelm@43713
   729
wenzelm@52508
   730
val global_state = Synchronized.var "Document.global_state" init_state;
wenzelm@43713
   731
wenzelm@43713
   732
fun state () = Synchronized.value global_state;
wenzelm@43713
   733
val change_state = Synchronized.change global_state;
wenzelm@43713
   734
wenzelm@38418
   735
end;