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