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