src/Pure/PIDE/document.ML
author wenzelm
Wed Apr 04 21:03:30 2012 +0200 (2012-04-04)
changeset 47339 79bd24497ffd
parent 47336 bed4b2738d8a
child 47340 9bbf7fd96bcd
permissions -rw-r--r--
tuned -- NB: get_theory still needs to apply Lazy.force due to interrupt instabilities;
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@44674
    65
type exec = exec_id * (Toplevel.state * unit lazy) lazy;  (*eval/print process*)
wenzelm@47336
    66
val no_exec = (no_id, Lazy.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@44674
    72
  entries: exec option Entries.T,  (*command entries with excecutions*)
wenzelm@44476
    73
  last_exec: command_id option,  (*last command with exec state assignment*)
wenzelm@44197
    74
  result: Toplevel.state lazy}
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
val no_result = Lazy.value Toplevel.toplevel;
wenzelm@44386
    91
wenzelm@44476
    92
val empty_node = make_node (false, no_header, no_perspective, Entries.empty, NONE, no_result);
wenzelm@44476
    93
val clear_node = map_node (fn (_, header, _, _, _, _) =>
wenzelm@44476
    94
  (false, header, no_perspective, Entries.empty, NONE, no_result));
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@44674
   162
fun the_default_entry node (SOME id) = (id, (the_default no_exec (the_entry node id)))
wenzelm@44674
   163
  | the_default_entry _ NONE = (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@44643
   196
          desc <> name ? (map_entries (reset_after NONE) #> set_result no_result)))
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@44544
   230
        update_node name (set_perspective perspective) 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@44673
   246
  execution: version_id * Future.group}  (*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@44675
   256
  make_state (Inttab.make [(no_id, empty_version)], Inttab.empty, (no_id, Future.new_group NONE));
wenzelm@38418
   257
wenzelm@38418
   258
wenzelm@38418
   259
(* document versions *)
wenzelm@38418
   260
wenzelm@38418
   261
fun define_version (id: version_id) version =
wenzelm@44674
   262
  map_state (fn (versions, commands, execution) =>
wenzelm@38418
   263
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   264
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@44674
   265
    in (versions', commands, execution) end);
wenzelm@38418
   266
wenzelm@38418
   267
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   268
  (case Inttab.lookup versions id of
wenzelm@38418
   269
    NONE => err_undef "document version" id
wenzelm@38418
   270
  | SOME version => version);
wenzelm@38418
   271
wenzelm@44673
   272
fun delete_version (id: version_id) versions = Inttab.delete id versions
wenzelm@44673
   273
  handle Inttab.UNDEF _ => err_undef "document version" id;
wenzelm@44673
   274
wenzelm@38418
   275
wenzelm@38418
   276
(* commands *)
wenzelm@38418
   277
wenzelm@44644
   278
fun define_command (id: command_id) name text =
wenzelm@44674
   279
  map_state (fn (versions, commands, execution) =>
wenzelm@38418
   280
    let
wenzelm@47336
   281
      val span = Lazy.lazy (fn () => Command.parse_command (print_id id) text);
wenzelm@38418
   282
      val commands' =
wenzelm@46945
   283
        Inttab.update_new (id, (name, span)) commands
wenzelm@38418
   284
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44674
   285
    in (versions, commands', execution) end);
wenzelm@38418
   286
wenzelm@38418
   287
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@44660
   288
  (case Inttab.lookup commands id of
wenzelm@38418
   289
    NONE => err_undef "command" id
wenzelm@44644
   290
  | SOME command => command);
wenzelm@38418
   291
wenzelm@38418
   292
wenzelm@41634
   293
(* document execution *)
wenzelm@41634
   294
wenzelm@44673
   295
fun cancel_execution (State {execution, ...}) = Future.cancel_group (#2 execution);
wenzelm@41634
   296
wenzelm@38418
   297
end;
wenzelm@38418
   298
wenzelm@38418
   299
wenzelm@38888
   300
wenzelm@44481
   301
(** update **)
wenzelm@38418
   302
wenzelm@44436
   303
(* perspective *)
wenzelm@44436
   304
wenzelm@44436
   305
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   306
  let
wenzelm@44436
   307
    val old_version = the_version state old_id;
wenzelm@44436
   308
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   309
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   310
  in define_version new_id new_version state end;
wenzelm@44436
   311
wenzelm@44436
   312
wenzelm@44481
   313
(* edits *)
wenzelm@38418
   314
wenzelm@38418
   315
local
wenzelm@38418
   316
wenzelm@47335
   317
fun make_required nodes =
wenzelm@47335
   318
  let
wenzelm@47335
   319
    val all_visible =
wenzelm@47335
   320
      Graph.fold (fn (a, (node, _)) => is_some (#2 (get_perspective node)) ? cons a) nodes []
wenzelm@47335
   321
      |> Graph.all_preds nodes;
wenzelm@47335
   322
    val required =
wenzelm@47335
   323
      fold (fn a => exists (fn b => Graph.is_edge nodes (a, b)) all_visible ? Symtab.update (a, ()))
wenzelm@47335
   324
        all_visible Symtab.empty;
wenzelm@47335
   325
  in Symtab.defined required end;
wenzelm@47335
   326
wenzelm@47335
   327
fun init_theory deps node =
wenzelm@47335
   328
  let
wenzelm@47335
   329
    (* FIXME provide files via Scala layer, not master_dir *)
wenzelm@47335
   330
    val (dir, header) = Exn.release (get_header node);
wenzelm@47335
   331
    val master_dir =
wenzelm@47335
   332
      (case Url.explode dir of
wenzelm@47335
   333
        Url.File path => path
wenzelm@47335
   334
      | _ => Path.current);
wenzelm@47335
   335
    val parents =
wenzelm@47335
   336
      #imports header |> map (fn import =>
wenzelm@47335
   337
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@47335
   338
          SOME thy => thy
wenzelm@47335
   339
        | NONE =>
wenzelm@47339
   340
            Toplevel.end_theory (Position.file_only import)
wenzelm@47339
   341
              (Lazy.force
wenzelm@47339
   342
                (get_result (snd (Future.join (the (AList.lookup (op =) deps import))))))));
wenzelm@47335
   343
  in Thy_Load.begin_theory master_dir header parents end;
wenzelm@47335
   344
wenzelm@47335
   345
fun check_theory nodes name =
wenzelm@47335
   346
  is_some (Thy_Info.lookup_theory name) orelse  (* FIXME more robust!? *)
wenzelm@47335
   347
  is_some (Exn.get_res (get_header (get_node nodes name)));
wenzelm@47335
   348
wenzelm@44645
   349
fun last_common state last_visible node0 node =
wenzelm@44482
   350
  let
wenzelm@44645
   351
    fun update_flags prev (visible, initial) =
wenzelm@44645
   352
      let
wenzelm@44645
   353
        val visible' = visible andalso prev <> last_visible;
wenzelm@44645
   354
        val initial' = initial andalso
wenzelm@44645
   355
          (case prev of
wenzelm@44645
   356
            NONE => true
wenzelm@44645
   357
          | SOME id => not (Keyword.is_theory_begin (#1 (the_command state id))));
wenzelm@44645
   358
      in (visible', initial') end;
wenzelm@44645
   359
    fun get_common ((prev, id), exec) (found, (_, flags)) =
wenzelm@44482
   360
      if found then NONE
wenzelm@44482
   361
      else
wenzelm@44674
   362
        let val found' =
wenzelm@44674
   363
          is_none exec orelse op <> (pairself (Option.map #1) (exec, lookup_entry node0 id));
wenzelm@44645
   364
        in SOME (found', (prev, update_flags prev flags)) end;
wenzelm@44645
   365
    val (found, (common, flags)) =
wenzelm@44645
   366
      iterate_entries get_common node (false, (NONE, (true, true)));
wenzelm@44645
   367
  in
wenzelm@44645
   368
    if found then (common, flags)
wenzelm@44645
   369
    else
wenzelm@44645
   370
      let val last = Entries.get_after (get_entries node) common
wenzelm@44645
   371
      in (last, update_flags last flags) end
wenzelm@44645
   372
  end;
wenzelm@44645
   373
wenzelm@44645
   374
fun illegal_init () = error "Illegal theory header after end of theory";
wenzelm@44482
   375
wenzelm@44674
   376
fun new_exec state bad_init command_id' (execs, command_exec, init) =
wenzelm@44645
   377
  if bad_init andalso is_none init then NONE
wenzelm@44645
   378
  else
wenzelm@44645
   379
    let
wenzelm@46945
   380
      val (name, span) = the_command state command_id' ||> Lazy.force;
wenzelm@44645
   381
      val (modify_init, init') =
wenzelm@44645
   382
        if Keyword.is_theory_begin name then
wenzelm@44645
   383
          (Toplevel.modify_init (the_default illegal_init init), NONE)
wenzelm@44645
   384
        else (I, init);
wenzelm@44660
   385
      val exec_id' = new_id ();
wenzelm@46876
   386
      val exec_id'_string = print_id exec_id';
wenzelm@46876
   387
      val tr =
wenzelm@46876
   388
        Position.setmp_thread_data (Position.id_only exec_id'_string)
wenzelm@46876
   389
          (fn () =>
wenzelm@46876
   390
            #1 (Outer_Syntax.read_span (#2 (Outer_Syntax.get_syntax ())) span)
wenzelm@46876
   391
            |> modify_init
wenzelm@46876
   392
            |> Toplevel.put_id exec_id'_string);
wenzelm@47336
   393
      val exec' =
wenzelm@47336
   394
        snd (snd command_exec) |> Lazy.map (fn (st, _) => Command.run_command (tr ()) st);
wenzelm@44674
   395
      val command_exec' = (command_id', (exec_id', exec'));
wenzelm@44674
   396
    in SOME (command_exec' :: execs, command_exec', init') end;
wenzelm@44482
   397
wenzelm@38418
   398
in
wenzelm@38418
   399
wenzelm@44481
   400
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   401
  let
wenzelm@38418
   402
    val old_version = the_version state old_id;
wenzelm@44180
   403
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   404
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   405
wenzelm@44544
   406
    val nodes = nodes_of new_version;
wenzelm@44544
   407
    val is_required = make_required nodes;
wenzelm@44544
   408
wenzelm@44476
   409
    val updated =
wenzelm@44544
   410
      nodes |> Graph.schedule
wenzelm@44199
   411
        (fn deps => fn (name, node) =>
wenzelm@44544
   412
          if not (is_touched node orelse is_required name)
wenzelm@44544
   413
          then Future.value (([], [], []), node)
wenzelm@44444
   414
          else
wenzelm@44479
   415
            let
wenzelm@44479
   416
              val node0 = node_of old_version name;
wenzelm@44979
   417
              fun init () = init_theory deps node;
wenzelm@44645
   418
              val bad_init =
wenzelm@44645
   419
                not (check_theory nodes name andalso forall (check_theory nodes o #1) deps);
wenzelm@44479
   420
            in
wenzelm@44482
   421
              (singleton o Future.forks)
wenzelm@44482
   422
                {name = "Document.update", group = NONE,
wenzelm@44482
   423
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44482
   424
                (fn () =>
wenzelm@44482
   425
                  let
wenzelm@44544
   426
                    val required = is_required name;
wenzelm@44482
   427
                    val last_visible = #2 (get_perspective node);
wenzelm@44645
   428
                    val (common, (visible, initial)) = last_common state last_visible node0 node;
wenzelm@44674
   429
                    val common_command_exec = the_default_entry node common;
wenzelm@44479
   430
wenzelm@44674
   431
                    val (execs, (command_id, (_, exec)), _) =
wenzelm@44674
   432
                      ([], common_command_exec, if initial then SOME init else NONE) |>
wenzelm@44544
   433
                      (visible orelse required) ?
wenzelm@44645
   434
                        iterate_entries_after common
wenzelm@44482
   435
                          (fn ((prev, id), _) => fn res =>
wenzelm@44544
   436
                            if not required andalso prev = last_visible then NONE
wenzelm@44645
   437
                            else new_exec state bad_init id res) node;
wenzelm@44479
   438
wenzelm@44482
   439
                    val no_execs =
wenzelm@44645
   440
                      iterate_entries_after common
wenzelm@44482
   441
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   442
                          if is_none exec0 then NONE
wenzelm@44482
   443
                          else if exists (fn (_, (id, _)) => id0 = id) execs then SOME res
wenzelm@44482
   444
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   445
wenzelm@44674
   446
                    val last_exec = if command_id = no_id then NONE else SOME command_id;
wenzelm@44482
   447
                    val result =
wenzelm@44482
   448
                      if is_some (after_entry node last_exec) then no_result
wenzelm@44674
   449
                      else Lazy.map #1 exec;
wenzelm@44480
   450
wenzelm@44482
   451
                    val node' = node
wenzelm@44482
   452
                      |> fold reset_entry no_execs
wenzelm@44674
   453
                      |> fold (fn (id, exec) => update_entry id (SOME exec)) execs
wenzelm@44482
   454
                      |> set_last_exec last_exec
wenzelm@44482
   455
                      |> set_result result
wenzelm@44482
   456
                      |> set_touched false;
wenzelm@44482
   457
                  in ((no_execs, execs, [(name, node')]), node') end)
wenzelm@44479
   458
            end)
wenzelm@44479
   459
      |> Future.joins |> map #1;
wenzelm@44476
   460
wenzelm@44479
   461
    val command_execs =
wenzelm@44479
   462
      map (rpair NONE) (maps #1 updated) @
wenzelm@44674
   463
      map (fn (command_id, (exec_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   464
    val updated_nodes = maps #3 updated;
wenzelm@44476
   465
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   466
wenzelm@44197
   467
    val state' = state
wenzelm@44476
   468
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   469
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   470
wenzelm@38418
   471
end;
wenzelm@38418
   472
wenzelm@38418
   473
wenzelm@38418
   474
(* execute *)
wenzelm@38418
   475
wenzelm@38418
   476
fun execute version_id state =
wenzelm@44674
   477
  state |> map_state (fn (versions, commands, _) =>
wenzelm@38418
   478
    let
wenzelm@38418
   479
      val version = the_version state version_id;
wenzelm@38418
   480
wenzelm@44674
   481
      fun force_exec _ _ NONE = ()
wenzelm@44674
   482
        | force_exec node command_id (SOME (_, exec)) =
wenzelm@44386
   483
            let
wenzelm@44386
   484
              val (_, print) = Lazy.force exec;
wenzelm@44386
   485
              val _ =
wenzelm@44439
   486
                if #1 (get_perspective node) command_id
wenzelm@44386
   487
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   488
                else ();
wenzelm@44386
   489
            in () end;
wenzelm@38418
   490
wenzelm@44673
   491
      val group = Future.new_group NONE;
wenzelm@44299
   492
      val _ =
wenzelm@44201
   493
        nodes_of version |> Graph.schedule
wenzelm@44201
   494
          (fn deps => fn (name, node) =>
wenzelm@44302
   495
            (singleton o Future.forks)
wenzelm@44673
   496
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME group)),
wenzelm@44614
   497
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@44674
   498
              (iterate_entries (fn ((_, id), exec) => fn () =>
wenzelm@44674
   499
                SOME (force_exec node id exec)) node));
wenzelm@40520
   500
wenzelm@44674
   501
    in (versions, commands, (version_id, group)) end);
wenzelm@44673
   502
wenzelm@44673
   503
wenzelm@44673
   504
(* remove versions *)
wenzelm@44673
   505
wenzelm@44674
   506
fun remove_versions ids state = state |> map_state (fn (versions, _, execution) =>
wenzelm@44673
   507
  let
wenzelm@44673
   508
    val _ = member (op =) ids (#1 execution) andalso
wenzelm@44673
   509
      error ("Attempt to remove execution version " ^ print_id (#1 execution));
wenzelm@44673
   510
wenzelm@44673
   511
    val versions' = fold delete_version ids versions;
wenzelm@44674
   512
    val commands' =
wenzelm@44675
   513
      (versions', Inttab.empty) |->
wenzelm@44673
   514
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@44673
   515
          Graph.fold (fn (_, (node, _)) => node |>
wenzelm@44674
   516
            iterate_entries (fn ((_, id), _) =>
wenzelm@44674
   517
              SOME o Inttab.insert (K true) (id, the_command state id))));
wenzelm@44674
   518
  in (versions', commands', execution) end);
wenzelm@38418
   519
wenzelm@43713
   520
wenzelm@43713
   521
wenzelm@43713
   522
(** global state **)
wenzelm@43713
   523
wenzelm@43713
   524
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   525
wenzelm@43713
   526
fun state () = Synchronized.value global_state;
wenzelm@43713
   527
val change_state = Synchronized.change global_state;
wenzelm@43713
   528
wenzelm@38418
   529
end;
wenzelm@38418
   530