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