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