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