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