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