src/Pure/PIDE/document.ML
author wenzelm
Thu Apr 05 22:33:52 2012 +0200 (2012-04-05)
changeset 47345 193251980a73
parent 47344 ca5eb90cc84a
child 47346 cd3ab7625519
permissions -rw-r--r--
more scalable execute/update: avoid redundant traversal of invisible/finished nodes;
tuned;
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@44478
     5
list of commands, with asynchronous read/eval/print processes.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@38363
    10
  type id = int
wenzelm@38373
    11
  type version_id = id
wenzelm@38363
    12
  type command_id = id
wenzelm@38373
    13
  type exec_id = id
wenzelm@38363
    14
  val no_id: id
wenzelm@38419
    15
  val new_id: unit -> id
wenzelm@38363
    16
  val parse_id: string -> id
wenzelm@38363
    17
  val print_id: id -> string
wenzelm@46938
    18
  type node_header = (string * Thy_Header.header) Exn.result
wenzelm@44157
    19
  datatype node_edit =
wenzelm@44185
    20
    Clear |
wenzelm@44157
    21
    Edits of (command_id option * command_id option) list |
wenzelm@44384
    22
    Header of node_header |
wenzelm@44385
    23
    Perspective of command_id list
wenzelm@44156
    24
  type edit = string * node_edit
wenzelm@38418
    25
  type state
wenzelm@38418
    26
  val init_state: state
wenzelm@44644
    27
  val define_command: command_id -> string -> string -> state -> state
wenzelm@47343
    28
  val discontinue_execution: state -> unit
wenzelm@44301
    29
  val cancel_execution: state -> Future.task list
wenzelm@47345
    30
  val execute: version_id -> state -> state
wenzelm@44436
    31
  val update_perspective: version_id -> version_id -> string -> command_id list -> state -> state
wenzelm@44481
    32
  val update: version_id -> version_id -> edit list -> state ->
wenzelm@44479
    33
    ((command_id * exec_id option) list * (string * command_id option) list) * state
wenzelm@44673
    34
  val remove_versions: version_id list -> state -> state
wenzelm@43713
    35
  val state: unit -> state
wenzelm@43713
    36
  val change_state: (state -> state) -> unit
wenzelm@38150
    37
end;
wenzelm@38150
    38
wenzelm@38150
    39
structure Document: DOCUMENT =
wenzelm@38150
    40
struct
wenzelm@38150
    41
wenzelm@38150
    42
(* unique identifiers *)
wenzelm@38150
    43
wenzelm@38363
    44
type id = int;
wenzelm@38373
    45
type version_id = id;
wenzelm@38363
    46
type command_id = id;
wenzelm@38373
    47
type exec_id = id;
wenzelm@38355
    48
wenzelm@38355
    49
val no_id = 0;
wenzelm@40449
    50
val new_id = Synchronized.counter ();
wenzelm@38418
    51
wenzelm@38414
    52
val parse_id = Markup.parse_int;
wenzelm@38414
    53
val print_id = Markup.print_int;
wenzelm@38150
    54
wenzelm@38418
    55
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
wenzelm@38418
    56
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
wenzelm@38150
    57
wenzelm@38418
    58
wenzelm@38418
    59
wenzelm@38418
    60
(** document structure **)
wenzelm@38418
    61
wenzelm@46938
    62
type node_header = (string * Thy_Header.header) Exn.result;
wenzelm@44476
    63
type perspective = (command_id -> bool) * command_id option; (*visible commands, last*)
wenzelm@38448
    64
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    65
wenzelm@47341
    66
type exec = (Toplevel.state * unit lazy) Command.memo;  (*eval/print process*)
wenzelm@47341
    67
val no_exec = Command.memo_value (Toplevel.toplevel, Lazy.value ());
wenzelm@44674
    68
wenzelm@43668
    69
abstype node = Node of
wenzelm@44444
    70
 {touched: bool,
wenzelm@44444
    71
  header: node_header,
wenzelm@44386
    72
  perspective: perspective,
wenzelm@47340
    73
  entries: (exec_id * exec) option Entries.T,  (*command entries with excecutions*)
wenzelm@44476
    74
  last_exec: command_id option,  (*last command with exec state assignment*)
wenzelm@47341
    75
  result: exec}
wenzelm@43668
    76
and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    77
with
wenzelm@38418
    78
wenzelm@44476
    79
fun make_node (touched, header, perspective, entries, last_exec, result) =
wenzelm@44444
    80
  Node {touched = touched, header = header, perspective = perspective,
wenzelm@44476
    81
    entries = entries, last_exec = last_exec, result = result};
wenzelm@43668
    82
wenzelm@44476
    83
fun map_node f (Node {touched, header, perspective, entries, last_exec, result}) =
wenzelm@44476
    84
  make_node (f (touched, header, perspective, entries, last_exec, result));
wenzelm@38418
    85
wenzelm@44476
    86
fun make_perspective command_ids : perspective =
wenzelm@44476
    87
  (Inttab.defined (Inttab.make (map (rpair ()) command_ids)), try List.last command_ids);
wenzelm@44441
    88
wenzelm@44476
    89
val no_header = Exn.Exn (ERROR "Bad theory header");
wenzelm@44441
    90
val no_perspective = make_perspective [];
wenzelm@44386
    91
wenzelm@47341
    92
val empty_node = make_node (false, no_header, no_perspective, Entries.empty, NONE, no_exec);
wenzelm@44476
    93
val clear_node = map_node (fn (_, header, _, _, _, _) =>
wenzelm@47341
    94
  (false, header, no_perspective, Entries.empty, NONE, no_exec));
wenzelm@44386
    95
wenzelm@44386
    96
wenzelm@44386
    97
(* basic components *)
wenzelm@44386
    98
wenzelm@44446
    99
fun is_touched (Node {touched, ...}) = touched;
wenzelm@44444
   100
fun set_touched touched =
wenzelm@44476
   101
  map_node (fn (_, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   102
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44444
   103
wenzelm@44180
   104
fun get_header (Node {header, ...}) = header;
wenzelm@44385
   105
fun set_header header =
wenzelm@44476
   106
  map_node (fn (touched, _, perspective, entries, last_exec, result) =>
wenzelm@44476
   107
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   108
wenzelm@44385
   109
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
   110
fun set_perspective ids =
wenzelm@44476
   111
  map_node (fn (touched, header, _, entries, last_exec, result) =>
wenzelm@44476
   112
    (touched, header, make_perspective ids, entries, last_exec, result));
wenzelm@44385
   113
wenzelm@47345
   114
val visible_command = #1 o get_perspective;
wenzelm@47345
   115
val visible_last = #2 o get_perspective;
wenzelm@47345
   116
val visible_node = is_some o visible_last
wenzelm@47345
   117
wenzelm@44444
   118
fun map_entries f =
wenzelm@44476
   119
  map_node (fn (touched, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   120
    (touched, header, perspective, f entries, last_exec, result));
wenzelm@44643
   121
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44645
   122
wenzelm@44645
   123
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@44645
   124
fun iterate_entries_after start f (Node {entries, ...}) =
wenzelm@44645
   125
  (case Entries.get_after entries start of
wenzelm@44645
   126
    NONE => I
wenzelm@44645
   127
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   128
wenzelm@44476
   129
fun get_last_exec (Node {last_exec, ...}) = last_exec;
wenzelm@44476
   130
fun set_last_exec last_exec =
wenzelm@44476
   131
  map_node (fn (touched, header, perspective, entries, _, result) =>
wenzelm@44476
   132
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44476
   133
wenzelm@47339
   134
fun get_result (Node {result, ...}) = result;
wenzelm@44385
   135
fun set_result result =
wenzelm@44476
   136
  map_node (fn (touched, header, perspective, entries, last_exec, _) =>
wenzelm@44476
   137
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   138
wenzelm@44185
   139
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   140
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   141
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   142
wenzelm@38418
   143
wenzelm@38448
   144
(* node edits and associated executions *)
wenzelm@38150
   145
wenzelm@44157
   146
datatype node_edit =
wenzelm@44185
   147
  Clear |
wenzelm@44157
   148
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   149
  Header of node_header |
wenzelm@44385
   150
  Perspective of command_id list;
wenzelm@44157
   151
wenzelm@44156
   152
type edit = string * node_edit;
wenzelm@38448
   153
wenzelm@44482
   154
fun after_entry (Node {entries, ...}) = Entries.get_after entries;
wenzelm@44479
   155
wenzelm@44480
   156
fun lookup_entry (Node {entries, ...}) id =
wenzelm@44480
   157
  (case Entries.lookup entries id of
wenzelm@44480
   158
    NONE => NONE
wenzelm@44480
   159
  | SOME (exec, _) => exec);
wenzelm@44480
   160
wenzelm@43668
   161
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   162
  (case Entries.lookup entries id of
wenzelm@38448
   163
    NONE => err_undef "command entry" id
wenzelm@44476
   164
  | SOME (exec, _) => exec);
wenzelm@38150
   165
wenzelm@47341
   166
fun the_default_entry node (SOME id) = (id, (the_default (no_id, no_exec) (the_entry node id)))
wenzelm@47341
   167
  | the_default_entry _ NONE = (no_id, (no_id, no_exec));
wenzelm@44476
   168
wenzelm@44482
   169
fun update_entry id exec =
wenzelm@44482
   170
  map_entries (Entries.update (id, exec));
wenzelm@44482
   171
wenzelm@44482
   172
fun reset_entry id node =
wenzelm@44482
   173
  if is_some (lookup_entry node id) then update_entry id NONE node else node;
wenzelm@38418
   174
wenzelm@38448
   175
fun reset_after id entries =
wenzelm@38448
   176
  (case Entries.get_after entries id of
wenzelm@38448
   177
    NONE => entries
wenzelm@38448
   178
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   179
wenzelm@43668
   180
val edit_node = map_entries o fold
wenzelm@43668
   181
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   182
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   183
wenzelm@38418
   184
wenzelm@38418
   185
(* version operations *)
wenzelm@38418
   186
wenzelm@44185
   187
val empty_version = Version Graph.empty;
wenzelm@44185
   188
wenzelm@38418
   189
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   190
val node_of = get_node o nodes_of;
wenzelm@38418
   191
wenzelm@44444
   192
local
wenzelm@44444
   193
wenzelm@44185
   194
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   195
wenzelm@44444
   196
fun touch_node name nodes =
wenzelm@44444
   197
  fold (fn desc =>
wenzelm@44643
   198
      update_node desc
wenzelm@44643
   199
        (set_touched true #>
wenzelm@47341
   200
          desc <> name ? (map_entries (reset_after NONE) #> set_result no_exec)))
wenzelm@44202
   201
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   202
wenzelm@44444
   203
in
wenzelm@44444
   204
wenzelm@44157
   205
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   206
  Version
wenzelm@44436
   207
    (case node_edit of
wenzelm@44436
   208
      Clear =>
wenzelm@44436
   209
        nodes
wenzelm@44436
   210
        |> update_node name clear_node
wenzelm@44444
   211
        |> touch_node name
wenzelm@44436
   212
    | Edits edits =>
wenzelm@44436
   213
        nodes
wenzelm@44436
   214
        |> update_node name (edit_node edits)
wenzelm@44444
   215
        |> touch_node name
wenzelm@44436
   216
    | Header header =>
wenzelm@44436
   217
        let
wenzelm@46938
   218
          val imports = (case header of Exn.Res (_, {imports, ...}) => imports | _ => []);
wenzelm@46945
   219
          val header' =
wenzelm@46958
   220
            ((case header of Exn.Res (_, h) => Thy_Header.define_keywords h | _ => ()); header)
wenzelm@46945
   221
              handle exn as ERROR _ => Exn.Exn exn;
wenzelm@44436
   222
          val nodes1 = nodes
wenzelm@44436
   223
            |> default_node name
wenzelm@46739
   224
            |> fold default_node imports;
wenzelm@44436
   225
          val nodes2 = nodes1
wenzelm@44436
   226
            |> Graph.Keys.fold
wenzelm@44436
   227
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@46945
   228
          val (header'', nodes3) =
wenzelm@46945
   229
            (header', Graph.add_deps_acyclic (name, imports) nodes2)
wenzelm@44436
   230
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@46945
   231
        in Graph.map_node name (set_header header'') nodes3 end
wenzelm@44444
   232
        |> touch_node name
wenzelm@44436
   233
    | Perspective perspective =>
wenzelm@47342
   234
        update_node name (set_perspective perspective #> set_touched true) nodes);
wenzelm@38418
   235
wenzelm@44444
   236
end;
wenzelm@44444
   237
wenzelm@44222
   238
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   239
  Version (update_node name (K node) nodes);
wenzelm@38418
   240
wenzelm@38150
   241
end;
wenzelm@38150
   242
wenzelm@38418
   243
wenzelm@38418
   244
wenzelm@38418
   245
(** global state -- document structure and execution process **)
wenzelm@38418
   246
wenzelm@38418
   247
abstype state = State of
wenzelm@40398
   248
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@47335
   249
  commands: (string * Token.T list lazy) Inttab.table,  (*command_id -> named span*)
wenzelm@47343
   250
  execution: version_id * Future.group * bool Unsynchronized.ref}  (*current execution process*)
wenzelm@38418
   251
with
wenzelm@38418
   252
wenzelm@44674
   253
fun make_state (versions, commands, execution) =
wenzelm@44674
   254
  State {versions = versions, commands = commands, execution = execution};
wenzelm@38418
   255
wenzelm@44674
   256
fun map_state f (State {versions, commands, execution}) =
wenzelm@44674
   257
  make_state (f (versions, commands, execution));
wenzelm@38418
   258
wenzelm@38418
   259
val init_state =
wenzelm@47343
   260
  make_state (Inttab.make [(no_id, empty_version)], Inttab.empty,
wenzelm@47343
   261
    (no_id, Future.new_group NONE, Unsynchronized.ref false));
wenzelm@38418
   262
wenzelm@38418
   263
wenzelm@38418
   264
(* document versions *)
wenzelm@38418
   265
wenzelm@38418
   266
fun define_version (id: version_id) version =
wenzelm@44674
   267
  map_state (fn (versions, commands, execution) =>
wenzelm@38418
   268
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   269
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@44674
   270
    in (versions', commands, execution) end);
wenzelm@38418
   271
wenzelm@38418
   272
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   273
  (case Inttab.lookup versions id of
wenzelm@38418
   274
    NONE => err_undef "document version" id
wenzelm@38418
   275
  | SOME version => version);
wenzelm@38418
   276
wenzelm@44673
   277
fun delete_version (id: version_id) versions = Inttab.delete id versions
wenzelm@44673
   278
  handle Inttab.UNDEF _ => err_undef "document version" id;
wenzelm@44673
   279
wenzelm@38418
   280
wenzelm@38418
   281
(* commands *)
wenzelm@38418
   282
wenzelm@44644
   283
fun define_command (id: command_id) name text =
wenzelm@44674
   284
  map_state (fn (versions, commands, execution) =>
wenzelm@38418
   285
    let
wenzelm@47336
   286
      val span = Lazy.lazy (fn () => Command.parse_command (print_id id) text);
wenzelm@38418
   287
      val commands' =
wenzelm@46945
   288
        Inttab.update_new (id, (name, span)) commands
wenzelm@38418
   289
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44674
   290
    in (versions, commands', execution) end);
wenzelm@38418
   291
wenzelm@38418
   292
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@44660
   293
  (case Inttab.lookup commands id of
wenzelm@38418
   294
    NONE => err_undef "command" id
wenzelm@44644
   295
  | SOME command => command);
wenzelm@38418
   296
wenzelm@38418
   297
wenzelm@41634
   298
(* document execution *)
wenzelm@41634
   299
wenzelm@47345
   300
fun discontinue_execution (State {execution = (_, _, running), ...}) = running := false;
wenzelm@47343
   301
wenzelm@47343
   302
fun cancel_execution (State {execution = (_, group, _), ...}) = Future.cancel_group group;
wenzelm@41634
   303
wenzelm@38418
   304
end;
wenzelm@38418
   305
wenzelm@38418
   306
wenzelm@38888
   307
wenzelm@47345
   308
(** execute **)
wenzelm@47345
   309
wenzelm@47345
   310
fun finished_theory node =
wenzelm@47345
   311
  (case Exn.capture Command.memo_result (get_result node) of
wenzelm@47345
   312
    Exn.Res (st, _) => can (Toplevel.end_theory Position.none) st
wenzelm@47345
   313
  | _ => false);
wenzelm@47345
   314
wenzelm@47345
   315
fun execute version_id state =
wenzelm@47345
   316
  state |> map_state (fn (versions, commands, _) =>
wenzelm@47345
   317
    let
wenzelm@47345
   318
      val version = the_version state version_id;
wenzelm@47345
   319
wenzelm@47345
   320
      val group = Future.new_group NONE;
wenzelm@47345
   321
      val running = Unsynchronized.ref true;
wenzelm@47345
   322
wenzelm@47345
   323
      fun run _ _ NONE = ()
wenzelm@47345
   324
        | run node command_id (SOME (_, exec)) =
wenzelm@47345
   325
            let
wenzelm@47345
   326
              val (_, print) = Command.memo_eval exec;
wenzelm@47345
   327
              val _ =
wenzelm@47345
   328
                if visible_command node command_id
wenzelm@47345
   329
                then ignore (Lazy.future Future.default_params print)
wenzelm@47345
   330
                else ();
wenzelm@47345
   331
            in () end;
wenzelm@47345
   332
wenzelm@47345
   333
      val _ =
wenzelm@47345
   334
        nodes_of version |> Graph.schedule
wenzelm@47345
   335
          (fn deps => fn (name, node) =>
wenzelm@47345
   336
            if not (visible_node node) andalso finished_theory node then
wenzelm@47345
   337
              Future.value ()
wenzelm@47345
   338
            else
wenzelm@47345
   339
              (singleton o Future.forks)
wenzelm@47345
   340
                {name = "theory:" ^ name, group = SOME (Future.new_group (SOME group)),
wenzelm@47345
   341
                  deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@47345
   342
                (fn () =>
wenzelm@47345
   343
                  iterate_entries (fn ((_, id), exec) => fn () =>
wenzelm@47345
   344
                    if ! running then SOME (run node id exec) else NONE) node ()));
wenzelm@47345
   345
wenzelm@47345
   346
    in (versions, commands, (version_id, group, running)) end);
wenzelm@47345
   347
wenzelm@47345
   348
wenzelm@47345
   349
wenzelm@47345
   350
wenzelm@44481
   351
(** update **)
wenzelm@38418
   352
wenzelm@44436
   353
(* perspective *)
wenzelm@44436
   354
wenzelm@44436
   355
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   356
  let
wenzelm@44436
   357
    val old_version = the_version state old_id;
wenzelm@44436
   358
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   359
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   360
  in define_version new_id new_version state end;
wenzelm@44436
   361
wenzelm@44436
   362
wenzelm@44481
   363
(* edits *)
wenzelm@38418
   364
wenzelm@38418
   365
local
wenzelm@38418
   366
wenzelm@47335
   367
fun make_required nodes =
wenzelm@47335
   368
  let
wenzelm@47335
   369
    val all_visible =
wenzelm@47345
   370
      Graph.fold (fn (a, (node, _)) => visible_node node ? cons a) nodes []
wenzelm@47345
   371
      |> Graph.all_preds nodes
wenzelm@47345
   372
      |> map (rpair ()) |> Symtab.make;
wenzelm@47345
   373
wenzelm@47335
   374
    val required =
wenzelm@47345
   375
      Symtab.fold (fn (a, ()) =>
wenzelm@47345
   376
        exists (Symtab.defined all_visible) (Graph.immediate_succs nodes a) ?
wenzelm@47345
   377
          Symtab.update (a, ())) all_visible Symtab.empty;
wenzelm@47335
   378
  in Symtab.defined required end;
wenzelm@47335
   379
wenzelm@47335
   380
fun init_theory deps node =
wenzelm@47335
   381
  let
wenzelm@47335
   382
    (* FIXME provide files via Scala layer, not master_dir *)
wenzelm@47335
   383
    val (dir, header) = Exn.release (get_header node);
wenzelm@47335
   384
    val master_dir =
wenzelm@47335
   385
      (case Url.explode dir of
wenzelm@47335
   386
        Url.File path => path
wenzelm@47335
   387
      | _ => Path.current);
wenzelm@47335
   388
    val parents =
wenzelm@47335
   389
      #imports header |> map (fn import =>
wenzelm@47335
   390
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@47335
   391
          SOME thy => thy
wenzelm@47335
   392
        | NONE =>
wenzelm@47339
   393
            Toplevel.end_theory (Position.file_only import)
wenzelm@47342
   394
              (fst (Command.memo_eval  (* FIXME memo_result !?! *)
wenzelm@47342
   395
                (get_result (snd (Future.join (the (AList.lookup (op =) deps import)))))))));
wenzelm@47335
   396
  in Thy_Load.begin_theory master_dir header parents end;
wenzelm@47335
   397
wenzelm@47335
   398
fun check_theory nodes name =
wenzelm@47335
   399
  is_some (Thy_Info.lookup_theory name) orelse  (* FIXME more robust!? *)
wenzelm@47335
   400
  is_some (Exn.get_res (get_header (get_node nodes name)));
wenzelm@47335
   401
wenzelm@44645
   402
fun last_common state last_visible node0 node =
wenzelm@44482
   403
  let
wenzelm@44645
   404
    fun update_flags prev (visible, initial) =
wenzelm@44645
   405
      let
wenzelm@44645
   406
        val visible' = visible andalso prev <> last_visible;
wenzelm@44645
   407
        val initial' = initial andalso
wenzelm@44645
   408
          (case prev of
wenzelm@44645
   409
            NONE => true
wenzelm@44645
   410
          | SOME id => not (Keyword.is_theory_begin (#1 (the_command state id))));
wenzelm@44645
   411
      in (visible', initial') end;
wenzelm@47340
   412
    fun get_common ((prev, id), opt_exec) (found, (_, flags)) =
wenzelm@44482
   413
      if found then NONE
wenzelm@44482
   414
      else
wenzelm@44674
   415
        let val found' =
wenzelm@47340
   416
          (case opt_exec of
wenzelm@47340
   417
            NONE => true
wenzelm@47340
   418
          | SOME (exec_id, exec) =>
wenzelm@47341
   419
              not (Command.memo_stable exec) orelse
wenzelm@47340
   420
              (case lookup_entry node0 id of
wenzelm@47340
   421
                NONE => true
wenzelm@47340
   422
              | SOME (exec_id0, _) => exec_id <> exec_id0));
wenzelm@44645
   423
        in SOME (found', (prev, update_flags prev flags)) end;
wenzelm@44645
   424
    val (found, (common, flags)) =
wenzelm@44645
   425
      iterate_entries get_common node (false, (NONE, (true, true)));
wenzelm@44645
   426
  in
wenzelm@44645
   427
    if found then (common, flags)
wenzelm@44645
   428
    else
wenzelm@44645
   429
      let val last = Entries.get_after (get_entries node) common
wenzelm@44645
   430
      in (last, update_flags last flags) end
wenzelm@44645
   431
  end;
wenzelm@44645
   432
wenzelm@44645
   433
fun illegal_init () = error "Illegal theory header after end of theory";
wenzelm@44482
   434
wenzelm@44674
   435
fun new_exec state bad_init command_id' (execs, command_exec, init) =
wenzelm@44645
   436
  if bad_init andalso is_none init then NONE
wenzelm@44645
   437
  else
wenzelm@44645
   438
    let
wenzelm@46945
   439
      val (name, span) = the_command state command_id' ||> Lazy.force;
wenzelm@44645
   440
      val (modify_init, init') =
wenzelm@44645
   441
        if Keyword.is_theory_begin name then
wenzelm@44645
   442
          (Toplevel.modify_init (the_default illegal_init init), NONE)
wenzelm@44645
   443
        else (I, init);
wenzelm@44660
   444
      val exec_id' = new_id ();
wenzelm@46876
   445
      val exec_id'_string = print_id exec_id';
wenzelm@46876
   446
      val tr =
wenzelm@46876
   447
        Position.setmp_thread_data (Position.id_only exec_id'_string)
wenzelm@46876
   448
          (fn () =>
wenzelm@46876
   449
            #1 (Outer_Syntax.read_span (#2 (Outer_Syntax.get_syntax ())) span)
wenzelm@46876
   450
            |> modify_init
wenzelm@46876
   451
            |> Toplevel.put_id exec_id'_string);
wenzelm@47341
   452
      val exec' = Command.memo (fn () =>
wenzelm@47344
   453
        let val (st, _) = Command.memo_eval (snd (snd command_exec));  (* FIXME memo_result !?! *)
wenzelm@47341
   454
        in Command.run_command (tr ()) st end);
wenzelm@44674
   455
      val command_exec' = (command_id', (exec_id', exec'));
wenzelm@44674
   456
    in SOME (command_exec' :: execs, command_exec', init') end;
wenzelm@44482
   457
wenzelm@38418
   458
in
wenzelm@38418
   459
wenzelm@44481
   460
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   461
  let
wenzelm@38418
   462
    val old_version = the_version state old_id;
wenzelm@44180
   463
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   464
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   465
wenzelm@44544
   466
    val nodes = nodes_of new_version;
wenzelm@44544
   467
    val is_required = make_required nodes;
wenzelm@44544
   468
wenzelm@44476
   469
    val updated =
wenzelm@44544
   470
      nodes |> Graph.schedule
wenzelm@44199
   471
        (fn deps => fn (name, node) =>
wenzelm@47345
   472
          if is_touched node orelse is_required name andalso not (finished_theory node) then
wenzelm@44479
   473
            let
wenzelm@44479
   474
              val node0 = node_of old_version name;
wenzelm@44979
   475
              fun init () = init_theory deps node;
wenzelm@44645
   476
              val bad_init =
wenzelm@44645
   477
                not (check_theory nodes name andalso forall (check_theory nodes o #1) deps);
wenzelm@44479
   478
            in
wenzelm@44482
   479
              (singleton o Future.forks)
wenzelm@44482
   480
                {name = "Document.update", group = NONE,
wenzelm@44482
   481
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44482
   482
                (fn () =>
wenzelm@44482
   483
                  let
wenzelm@44544
   484
                    val required = is_required name;
wenzelm@47345
   485
                    val last_visible = visible_last node;
wenzelm@44645
   486
                    val (common, (visible, initial)) = last_common state last_visible node0 node;
wenzelm@44674
   487
                    val common_command_exec = the_default_entry node common;
wenzelm@44479
   488
wenzelm@44674
   489
                    val (execs, (command_id, (_, exec)), _) =
wenzelm@44674
   490
                      ([], common_command_exec, if initial then SOME init else NONE) |>
wenzelm@44544
   491
                      (visible orelse required) ?
wenzelm@44645
   492
                        iterate_entries_after common
wenzelm@44482
   493
                          (fn ((prev, id), _) => fn res =>
wenzelm@44544
   494
                            if not required andalso prev = last_visible then NONE
wenzelm@44645
   495
                            else new_exec state bad_init id res) node;
wenzelm@44479
   496
wenzelm@44482
   497
                    val no_execs =
wenzelm@44645
   498
                      iterate_entries_after common
wenzelm@44482
   499
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   500
                          if is_none exec0 then NONE
wenzelm@44482
   501
                          else if exists (fn (_, (id, _)) => id0 = id) execs then SOME res
wenzelm@44482
   502
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   503
wenzelm@44674
   504
                    val last_exec = if command_id = no_id then NONE else SOME command_id;
wenzelm@44482
   505
                    val result =
wenzelm@47341
   506
                      if is_some (after_entry node last_exec) then no_exec
wenzelm@47341
   507
                      else exec;
wenzelm@44480
   508
wenzelm@44482
   509
                    val node' = node
wenzelm@44482
   510
                      |> fold reset_entry no_execs
wenzelm@44674
   511
                      |> fold (fn (id, exec) => update_entry id (SOME exec)) execs
wenzelm@44482
   512
                      |> set_last_exec last_exec
wenzelm@44482
   513
                      |> set_result result
wenzelm@44482
   514
                      |> set_touched false;
wenzelm@44482
   515
                  in ((no_execs, execs, [(name, node')]), node') end)
wenzelm@47340
   516
            end
wenzelm@47340
   517
          else Future.value (([], [], []), node))
wenzelm@44479
   518
      |> Future.joins |> map #1;
wenzelm@44476
   519
wenzelm@44479
   520
    val command_execs =
wenzelm@44479
   521
      map (rpair NONE) (maps #1 updated) @
wenzelm@44674
   522
      map (fn (command_id, (exec_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   523
    val updated_nodes = maps #3 updated;
wenzelm@44476
   524
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   525
wenzelm@44197
   526
    val state' = state
wenzelm@44476
   527
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   528
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   529
wenzelm@38418
   530
end;
wenzelm@38418
   531
wenzelm@38418
   532
wenzelm@44673
   533
(* remove versions *)
wenzelm@44673
   534
wenzelm@44674
   535
fun remove_versions ids state = state |> map_state (fn (versions, _, execution) =>
wenzelm@44673
   536
  let
wenzelm@44673
   537
    val _ = member (op =) ids (#1 execution) andalso
wenzelm@44673
   538
      error ("Attempt to remove execution version " ^ print_id (#1 execution));
wenzelm@44673
   539
wenzelm@44673
   540
    val versions' = fold delete_version ids versions;
wenzelm@44674
   541
    val commands' =
wenzelm@44675
   542
      (versions', Inttab.empty) |->
wenzelm@44673
   543
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@44673
   544
          Graph.fold (fn (_, (node, _)) => node |>
wenzelm@44674
   545
            iterate_entries (fn ((_, id), _) =>
wenzelm@44674
   546
              SOME o Inttab.insert (K true) (id, the_command state id))));
wenzelm@44674
   547
  in (versions', commands', execution) end);
wenzelm@38418
   548
wenzelm@43713
   549
wenzelm@43713
   550
wenzelm@43713
   551
(** global state **)
wenzelm@43713
   552
wenzelm@43713
   553
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   554
wenzelm@43713
   555
fun state () = Synchronized.value global_state;
wenzelm@43713
   556
val change_state = Synchronized.change global_state;
wenzelm@43713
   557
wenzelm@38418
   558
end;
wenzelm@38418
   559