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