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