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