src/Pure/PIDE/document.ML
author wenzelm
Sat Sep 03 18:08:09 2011 +0200 (2011-09-03)
changeset 44673 2fa51ac191bc
parent 44660 90bab3febb6c
child 44674 bad4f9158c80
permissions -rw-r--r--
Document.remove_versions on ML side;
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@44185
    18
  type node_header = (string * string list * (string * bool) list) 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@44185
    61
type node_header = (string * string list * (string * bool) list) 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@43668
    65
abstype node = Node of
wenzelm@44444
    66
 {touched: bool,
wenzelm@44444
    67
  header: node_header,
wenzelm@44386
    68
  perspective: perspective,
wenzelm@44180
    69
  entries: exec_id option Entries.T,  (*command entries with excecutions*)
wenzelm@44476
    70
  last_exec: command_id option,  (*last command with exec state assignment*)
wenzelm@44197
    71
  result: Toplevel.state lazy}
wenzelm@43668
    72
and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    73
with
wenzelm@38418
    74
wenzelm@44476
    75
fun make_node (touched, header, perspective, entries, last_exec, result) =
wenzelm@44444
    76
  Node {touched = touched, header = header, perspective = perspective,
wenzelm@44476
    77
    entries = entries, last_exec = last_exec, result = result};
wenzelm@43668
    78
wenzelm@44476
    79
fun map_node f (Node {touched, header, perspective, entries, last_exec, result}) =
wenzelm@44476
    80
  make_node (f (touched, header, perspective, entries, last_exec, result));
wenzelm@38418
    81
wenzelm@44476
    82
fun make_perspective command_ids : perspective =
wenzelm@44476
    83
  (Inttab.defined (Inttab.make (map (rpair ()) command_ids)), try List.last command_ids);
wenzelm@44441
    84
wenzelm@44476
    85
val no_header = Exn.Exn (ERROR "Bad theory header");
wenzelm@44441
    86
val no_perspective = make_perspective [];
wenzelm@44386
    87
val no_print = Lazy.value ();
wenzelm@44386
    88
val no_result = Lazy.value Toplevel.toplevel;
wenzelm@44386
    89
wenzelm@44476
    90
val empty_node = make_node (false, no_header, no_perspective, Entries.empty, NONE, no_result);
wenzelm@44476
    91
val clear_node = map_node (fn (_, header, _, _, _, _) =>
wenzelm@44476
    92
  (false, header, no_perspective, Entries.empty, NONE, no_result));
wenzelm@44386
    93
wenzelm@44386
    94
wenzelm@44386
    95
(* basic components *)
wenzelm@44386
    96
wenzelm@44446
    97
fun is_touched (Node {touched, ...}) = touched;
wenzelm@44444
    98
fun set_touched touched =
wenzelm@44476
    99
  map_node (fn (_, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   100
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44444
   101
wenzelm@44180
   102
fun get_header (Node {header, ...}) = header;
wenzelm@44385
   103
fun set_header header =
wenzelm@44476
   104
  map_node (fn (touched, _, perspective, entries, last_exec, result) =>
wenzelm@44476
   105
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   106
wenzelm@44385
   107
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
   108
fun set_perspective ids =
wenzelm@44476
   109
  map_node (fn (touched, header, _, entries, last_exec, result) =>
wenzelm@44476
   110
    (touched, header, make_perspective ids, entries, last_exec, result));
wenzelm@44385
   111
wenzelm@44444
   112
fun map_entries f =
wenzelm@44476
   113
  map_node (fn (touched, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   114
    (touched, header, perspective, f entries, last_exec, result));
wenzelm@44643
   115
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44645
   116
wenzelm@44645
   117
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@44645
   118
fun iterate_entries_after start f (Node {entries, ...}) =
wenzelm@44645
   119
  (case Entries.get_after entries start of
wenzelm@44645
   120
    NONE => I
wenzelm@44645
   121
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   122
wenzelm@44476
   123
fun get_last_exec (Node {last_exec, ...}) = last_exec;
wenzelm@44476
   124
fun set_last_exec last_exec =
wenzelm@44476
   125
  map_node (fn (touched, header, perspective, entries, _, result) =>
wenzelm@44476
   126
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44476
   127
wenzelm@44386
   128
fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force result);
wenzelm@44385
   129
fun set_result result =
wenzelm@44476
   130
  map_node (fn (touched, header, perspective, entries, last_exec, _) =>
wenzelm@44476
   131
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   132
wenzelm@44185
   133
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   134
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   135
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   136
wenzelm@38418
   137
wenzelm@38448
   138
(* node edits and associated executions *)
wenzelm@38150
   139
wenzelm@44157
   140
datatype node_edit =
wenzelm@44185
   141
  Clear |
wenzelm@44157
   142
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   143
  Header of node_header |
wenzelm@44385
   144
  Perspective of command_id list;
wenzelm@44157
   145
wenzelm@44156
   146
type edit = string * node_edit;
wenzelm@38448
   147
wenzelm@44482
   148
fun after_entry (Node {entries, ...}) = Entries.get_after entries;
wenzelm@44479
   149
wenzelm@44480
   150
fun lookup_entry (Node {entries, ...}) id =
wenzelm@44480
   151
  (case Entries.lookup entries id of
wenzelm@44480
   152
    NONE => NONE
wenzelm@44480
   153
  | SOME (exec, _) => exec);
wenzelm@44480
   154
wenzelm@43668
   155
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   156
  (case Entries.lookup entries id of
wenzelm@38448
   157
    NONE => err_undef "command entry" id
wenzelm@44476
   158
  | SOME (exec, _) => exec);
wenzelm@38150
   159
wenzelm@44479
   160
fun the_default_entry node (SOME id) = the_default no_id (the_entry node id)
wenzelm@44479
   161
  | the_default_entry _ NONE = no_id;
wenzelm@44476
   162
wenzelm@44482
   163
fun update_entry id exec =
wenzelm@44482
   164
  map_entries (Entries.update (id, exec));
wenzelm@44482
   165
wenzelm@44482
   166
fun reset_entry id node =
wenzelm@44482
   167
  if is_some (lookup_entry node id) then update_entry id NONE node else node;
wenzelm@38418
   168
wenzelm@38448
   169
fun reset_after id entries =
wenzelm@38448
   170
  (case Entries.get_after entries id of
wenzelm@38448
   171
    NONE => entries
wenzelm@38448
   172
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   173
wenzelm@43668
   174
val edit_node = map_entries o fold
wenzelm@43668
   175
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   176
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   177
wenzelm@38418
   178
wenzelm@38418
   179
(* version operations *)
wenzelm@38418
   180
wenzelm@44185
   181
val empty_version = Version Graph.empty;
wenzelm@44185
   182
wenzelm@38418
   183
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   184
val node_of = get_node o nodes_of;
wenzelm@38418
   185
wenzelm@44444
   186
local
wenzelm@44444
   187
wenzelm@44185
   188
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   189
wenzelm@44444
   190
fun touch_node name nodes =
wenzelm@44444
   191
  fold (fn desc =>
wenzelm@44643
   192
      update_node desc
wenzelm@44643
   193
        (set_touched true #>
wenzelm@44643
   194
          desc <> name ? (map_entries (reset_after NONE) #> set_result no_result)))
wenzelm@44202
   195
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   196
wenzelm@44444
   197
in
wenzelm@44444
   198
wenzelm@44157
   199
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   200
  Version
wenzelm@44436
   201
    (case node_edit of
wenzelm@44436
   202
      Clear =>
wenzelm@44436
   203
        nodes
wenzelm@44436
   204
        |> update_node name clear_node
wenzelm@44444
   205
        |> touch_node name
wenzelm@44436
   206
    | Edits edits =>
wenzelm@44436
   207
        nodes
wenzelm@44436
   208
        |> update_node name (edit_node edits)
wenzelm@44444
   209
        |> touch_node name
wenzelm@44436
   210
    | Header header =>
wenzelm@44436
   211
        let
wenzelm@44436
   212
          val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
wenzelm@44436
   213
          val nodes1 = nodes
wenzelm@44436
   214
            |> default_node name
wenzelm@44436
   215
            |> fold default_node parents;
wenzelm@44436
   216
          val nodes2 = nodes1
wenzelm@44436
   217
            |> Graph.Keys.fold
wenzelm@44436
   218
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@44436
   219
          val (header', nodes3) =
wenzelm@44436
   220
            (header, Graph.add_deps_acyclic (name, parents) nodes2)
wenzelm@44436
   221
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@44436
   222
        in Graph.map_node name (set_header header') nodes3 end
wenzelm@44444
   223
        |> touch_node name
wenzelm@44436
   224
    | Perspective perspective =>
wenzelm@44544
   225
        update_node name (set_perspective perspective) nodes);
wenzelm@38418
   226
wenzelm@44444
   227
end;
wenzelm@44444
   228
wenzelm@44222
   229
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   230
  Version (update_node name (K node) nodes);
wenzelm@38418
   231
wenzelm@38150
   232
end;
wenzelm@38150
   233
wenzelm@38418
   234
wenzelm@38418
   235
wenzelm@38418
   236
(** global state -- document structure and execution process **)
wenzelm@38418
   237
wenzelm@38418
   238
abstype state = State of
wenzelm@40398
   239
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@44660
   240
  commands: (string * Toplevel.transition future) Inttab.table,  (*command_id -> name * transition*)
wenzelm@44386
   241
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   242
    (*exec_id -> command_id with eval/print process*)
wenzelm@44673
   243
  execution: version_id * Future.group}  (*current execution process*)
wenzelm@38418
   244
with
wenzelm@38418
   245
wenzelm@38418
   246
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   247
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   248
wenzelm@38418
   249
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   250
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   251
wenzelm@44673
   252
val empty_commands =
wenzelm@44673
   253
  Inttab.make [(no_id, (Toplevel.name_of Toplevel.empty, Future.value Toplevel.empty))];
wenzelm@44673
   254
val empty_execs = Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))];
wenzelm@44673
   255
val empty_execution = (no_id, Future.new_group NONE);
wenzelm@44673
   256
wenzelm@38418
   257
val init_state =
wenzelm@44673
   258
  make_state (Inttab.make [(no_id, empty_version)], empty_commands, empty_execs, empty_execution);
wenzelm@38418
   259
wenzelm@38418
   260
wenzelm@38418
   261
(* document versions *)
wenzelm@38418
   262
wenzelm@38418
   263
fun define_version (id: version_id) version =
wenzelm@38418
   264
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   265
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   266
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   267
    in (versions', commands, execs, execution) end);
wenzelm@38418
   268
wenzelm@38418
   269
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   270
  (case Inttab.lookup versions id of
wenzelm@38418
   271
    NONE => err_undef "document version" id
wenzelm@38418
   272
  | SOME version => version);
wenzelm@38418
   273
wenzelm@44673
   274
fun delete_version (id: version_id) versions = Inttab.delete id versions
wenzelm@44673
   275
  handle Inttab.UNDEF _ => err_undef "document version" id;
wenzelm@44673
   276
wenzelm@38418
   277
wenzelm@38418
   278
(* commands *)
wenzelm@38418
   279
wenzelm@44644
   280
fun define_command (id: command_id) name text =
wenzelm@44660
   281
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   282
    let
wenzelm@38418
   283
      val id_string = print_id id;
wenzelm@44660
   284
      val future =
wenzelm@44660
   285
        (singleton o Future.forks)
wenzelm@44660
   286
          {name = "Document.define_command", group = SOME (Future.new_group NONE),
wenzelm@44660
   287
            deps = [], pri = ~1, interrupts = false}
wenzelm@44660
   288
          (fn () =>
wenzelm@44660
   289
            Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44660
   290
              (fn () => Outer_Syntax.read_command (Position.id id_string) text) ());
wenzelm@38418
   291
      val commands' =
wenzelm@44660
   292
        Inttab.update_new (id, (name, future)) commands
wenzelm@38418
   293
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44660
   294
    in (versions, commands', execs, execution) end);
wenzelm@38418
   295
wenzelm@38418
   296
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@44660
   297
  (case Inttab.lookup commands id of
wenzelm@38418
   298
    NONE => err_undef "command" id
wenzelm@44644
   299
  | SOME command => command);
wenzelm@38418
   300
wenzelm@38418
   301
wenzelm@38418
   302
(* command executions *)
wenzelm@38418
   303
wenzelm@44197
   304
fun define_exec (exec_id, exec) =
wenzelm@38418
   305
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   306
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   307
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   308
    in (versions, commands, execs', execution) end);
wenzelm@38418
   309
wenzelm@44197
   310
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   311
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   312
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   313
  | SOME exec => exec);
wenzelm@38418
   314
wenzelm@41634
   315
wenzelm@41634
   316
(* document execution *)
wenzelm@41634
   317
wenzelm@44673
   318
fun cancel_execution (State {execution, ...}) = Future.cancel_group (#2 execution);
wenzelm@41634
   319
wenzelm@38418
   320
end;
wenzelm@38418
   321
wenzelm@38418
   322
wenzelm@38418
   323
wenzelm@38888
   324
(* toplevel transactions *)
wenzelm@38888
   325
wenzelm@38888
   326
local
wenzelm@38888
   327
wenzelm@44440
   328
fun timing tr t =
wenzelm@44440
   329
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   330
wenzelm@38888
   331
fun proof_status tr st =
wenzelm@38888
   332
  (case try Toplevel.proof_of st of
wenzelm@38888
   333
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   334
  | NONE => ());
wenzelm@38888
   335
wenzelm@44386
   336
fun print_state tr st =
wenzelm@44386
   337
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   338
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   339
wenzelm@40390
   340
fun run int tr st =
wenzelm@40390
   341
  (case Toplevel.transition int tr st of
wenzelm@40390
   342
    SOME (st', NONE) => ([], SOME st')
wenzelm@44614
   343
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@44270
   344
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@40390
   345
wenzelm@38888
   346
in
wenzelm@38888
   347
wenzelm@44199
   348
fun run_command tr st =
wenzelm@44185
   349
  let
wenzelm@44199
   350
    val is_init = Toplevel.is_init tr;
wenzelm@44185
   351
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   352
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   353
wenzelm@44614
   354
    val _ = Multithreading.interrupted ();
wenzelm@44611
   355
    val _ = Toplevel.status tr Markup.forked;
wenzelm@44185
   356
    val start = Timing.start ();
wenzelm@44645
   357
    val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   358
    val _ = timing tr (Timing.result start);
wenzelm@44611
   359
    val _ = Toplevel.status tr Markup.joined;
wenzelm@44185
   360
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   361
  in
wenzelm@44185
   362
    (case result of
wenzelm@44614
   363
      NONE =>
wenzelm@44614
   364
       (if null errs then Exn.interrupt () else ();
wenzelm@44614
   365
        Toplevel.status tr Markup.failed;
wenzelm@44614
   366
        (st, no_print))
wenzelm@44185
   367
    | SOME st' =>
wenzelm@44185
   368
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   369
        proof_status tr st';
wenzelm@44386
   370
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   371
  end;
wenzelm@38888
   372
wenzelm@38888
   373
end;
wenzelm@38888
   374
wenzelm@38888
   375
wenzelm@38888
   376
wenzelm@38888
   377
wenzelm@44481
   378
(** update **)
wenzelm@38418
   379
wenzelm@44436
   380
(* perspective *)
wenzelm@44436
   381
wenzelm@44436
   382
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   383
  let
wenzelm@44436
   384
    val old_version = the_version state old_id;
wenzelm@44436
   385
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   386
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   387
  in define_version new_id new_version state end;
wenzelm@44436
   388
wenzelm@44436
   389
wenzelm@44481
   390
(* edits *)
wenzelm@38418
   391
wenzelm@38418
   392
local
wenzelm@38418
   393
wenzelm@44645
   394
fun last_common state last_visible node0 node =
wenzelm@44482
   395
  let
wenzelm@44645
   396
    fun update_flags prev (visible, initial) =
wenzelm@44645
   397
      let
wenzelm@44645
   398
        val visible' = visible andalso prev <> last_visible;
wenzelm@44645
   399
        val initial' = initial andalso
wenzelm@44645
   400
          (case prev of
wenzelm@44645
   401
            NONE => true
wenzelm@44645
   402
          | SOME id => not (Keyword.is_theory_begin (#1 (the_command state id))));
wenzelm@44645
   403
      in (visible', initial') end;
wenzelm@44645
   404
    fun get_common ((prev, id), exec) (found, (_, flags)) =
wenzelm@44482
   405
      if found then NONE
wenzelm@44482
   406
      else
wenzelm@44482
   407
        let val found' = is_none exec orelse exec <> lookup_entry node0 id
wenzelm@44645
   408
        in SOME (found', (prev, update_flags prev flags)) end;
wenzelm@44645
   409
    val (found, (common, flags)) =
wenzelm@44645
   410
      iterate_entries get_common node (false, (NONE, (true, true)));
wenzelm@44645
   411
  in
wenzelm@44645
   412
    if found then (common, flags)
wenzelm@44645
   413
    else
wenzelm@44645
   414
      let val last = Entries.get_after (get_entries node) common
wenzelm@44645
   415
      in (last, update_flags last flags) end
wenzelm@44645
   416
  end;
wenzelm@44645
   417
wenzelm@44645
   418
fun illegal_init () = error "Illegal theory header after end of theory";
wenzelm@44482
   419
wenzelm@44645
   420
fun new_exec state bad_init command_id' (execs, exec, init) =
wenzelm@44645
   421
  if bad_init andalso is_none init then NONE
wenzelm@44645
   422
  else
wenzelm@44645
   423
    let
wenzelm@44660
   424
      val (name, tr0) = the_command state command_id' ||> Future.join;
wenzelm@44645
   425
      val (modify_init, init') =
wenzelm@44645
   426
        if Keyword.is_theory_begin name then
wenzelm@44645
   427
          (Toplevel.modify_init (the_default illegal_init init), NONE)
wenzelm@44645
   428
        else (I, init);
wenzelm@44660
   429
      val exec_id' = new_id ();
wenzelm@44660
   430
      val tr = tr0
wenzelm@44660
   431
        |> modify_init
wenzelm@44660
   432
        |> Toplevel.put_id (print_id exec_id');
wenzelm@44660
   433
      val exec' = (command_id', Lazy.map (fn (st, _) => run_command tr st) (snd exec));
wenzelm@44645
   434
    in SOME ((exec_id', exec') :: execs, exec', init') end;
wenzelm@44482
   435
wenzelm@44544
   436
fun make_required nodes =
wenzelm@44544
   437
  let
wenzelm@44544
   438
    val all_visible =
wenzelm@44544
   439
      Graph.fold (fn (a, (node, _)) => is_some (#2 (get_perspective node)) ? cons a) nodes []
wenzelm@44544
   440
      |> Graph.all_preds nodes;
wenzelm@44544
   441
    val required =
wenzelm@44544
   442
      fold (fn a => exists (fn b => Graph.is_edge nodes (a, b)) all_visible ? Symtab.update (a, ()))
wenzelm@44544
   443
        all_visible Symtab.empty;
wenzelm@44544
   444
  in Symtab.defined required end;
wenzelm@44544
   445
wenzelm@44645
   446
fun check_theory nodes name =
wenzelm@44645
   447
  is_some (Thy_Info.lookup_theory name) orelse  (* FIXME more robust!? *)
wenzelm@44645
   448
  is_some (Exn.get_res (get_header (get_node nodes name)));
wenzelm@44645
   449
wenzelm@44441
   450
fun init_theory deps name node =
wenzelm@44435
   451
  let
wenzelm@44435
   452
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   453
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   454
    val (dir, files) =
wenzelm@44435
   455
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   456
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   457
wenzelm@44435
   458
    val parents =
wenzelm@44435
   459
      imports |> map (fn import =>
wenzelm@44441
   460
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   461
          SOME thy => thy
wenzelm@44435
   462
        | NONE =>
wenzelm@44435
   463
            get_theory (Position.file_only import)
wenzelm@44482
   464
              (snd (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   465
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   466
wenzelm@38418
   467
in
wenzelm@38418
   468
wenzelm@44481
   469
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   470
  let
wenzelm@38418
   471
    val old_version = the_version state old_id;
wenzelm@44180
   472
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   473
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   474
wenzelm@44544
   475
    val nodes = nodes_of new_version;
wenzelm@44544
   476
    val is_required = make_required nodes;
wenzelm@44544
   477
wenzelm@44476
   478
    val updated =
wenzelm@44544
   479
      nodes |> Graph.schedule
wenzelm@44199
   480
        (fn deps => fn (name, node) =>
wenzelm@44544
   481
          if not (is_touched node orelse is_required name)
wenzelm@44544
   482
          then Future.value (([], [], []), node)
wenzelm@44444
   483
          else
wenzelm@44479
   484
            let
wenzelm@44479
   485
              val node0 = node_of old_version name;
wenzelm@44479
   486
              fun init () = init_theory deps name node;
wenzelm@44645
   487
              val bad_init =
wenzelm@44645
   488
                not (check_theory nodes name andalso forall (check_theory nodes o #1) deps);
wenzelm@44479
   489
            in
wenzelm@44482
   490
              (singleton o Future.forks)
wenzelm@44482
   491
                {name = "Document.update", group = NONE,
wenzelm@44482
   492
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44482
   493
                (fn () =>
wenzelm@44482
   494
                  let
wenzelm@44544
   495
                    val required = is_required name;
wenzelm@44482
   496
                    val last_visible = #2 (get_perspective node);
wenzelm@44645
   497
                    val (common, (visible, initial)) = last_common state last_visible node0 node;
wenzelm@44482
   498
                    val common_exec = the_exec state (the_default_entry node common);
wenzelm@44479
   499
wenzelm@44645
   500
                    val (execs, exec, _) =
wenzelm@44645
   501
                      ([], common_exec, if initial then SOME init else NONE) |>
wenzelm@44544
   502
                      (visible orelse required) ?
wenzelm@44645
   503
                        iterate_entries_after common
wenzelm@44482
   504
                          (fn ((prev, id), _) => fn res =>
wenzelm@44544
   505
                            if not required andalso prev = last_visible then NONE
wenzelm@44645
   506
                            else new_exec state bad_init id res) node;
wenzelm@44479
   507
wenzelm@44482
   508
                    val no_execs =
wenzelm@44645
   509
                      iterate_entries_after common
wenzelm@44482
   510
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   511
                          if is_none exec0 then NONE
wenzelm@44482
   512
                          else if exists (fn (_, (id, _)) => id0 = id) execs then SOME res
wenzelm@44482
   513
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   514
wenzelm@44482
   515
                    val last_exec = if #1 exec = no_id then NONE else SOME (#1 exec);
wenzelm@44482
   516
                    val result =
wenzelm@44482
   517
                      if is_some (after_entry node last_exec) then no_result
wenzelm@44482
   518
                      else Lazy.map #1 (#2 exec);
wenzelm@44480
   519
wenzelm@44482
   520
                    val node' = node
wenzelm@44482
   521
                      |> fold reset_entry no_execs
wenzelm@44482
   522
                      |> fold (fn (exec_id, (id, _)) => update_entry id (SOME exec_id)) execs
wenzelm@44482
   523
                      |> set_last_exec last_exec
wenzelm@44482
   524
                      |> set_result result
wenzelm@44482
   525
                      |> set_touched false;
wenzelm@44482
   526
                  in ((no_execs, execs, [(name, node')]), node') end)
wenzelm@44479
   527
            end)
wenzelm@44479
   528
      |> Future.joins |> map #1;
wenzelm@44476
   529
wenzelm@44479
   530
    val command_execs =
wenzelm@44479
   531
      map (rpair NONE) (maps #1 updated) @
wenzelm@44479
   532
      map (fn (exec_id, (command_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   533
    val updated_nodes = maps #3 updated;
wenzelm@44476
   534
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   535
wenzelm@44197
   536
    val state' = state
wenzelm@44479
   537
      |> fold (fold define_exec o #2) updated
wenzelm@44476
   538
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   539
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   540
wenzelm@38418
   541
end;
wenzelm@38418
   542
wenzelm@38418
   543
wenzelm@38418
   544
(* execute *)
wenzelm@38418
   545
wenzelm@38418
   546
fun execute version_id state =
wenzelm@41673
   547
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   548
    let
wenzelm@38418
   549
      val version = the_version state version_id;
wenzelm@38418
   550
wenzelm@44386
   551
      fun force_exec _ NONE = ()
wenzelm@44386
   552
        | force_exec node (SOME exec_id) =
wenzelm@44386
   553
            let
wenzelm@44386
   554
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   555
              val (_, print) = Lazy.force exec;
wenzelm@44386
   556
              val _ =
wenzelm@44439
   557
                if #1 (get_perspective node) command_id
wenzelm@44386
   558
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   559
                else ();
wenzelm@44386
   560
            in () end;
wenzelm@38418
   561
wenzelm@44673
   562
      val group = Future.new_group NONE;
wenzelm@44299
   563
      val _ =
wenzelm@44201
   564
        nodes_of version |> Graph.schedule
wenzelm@44201
   565
          (fn deps => fn (name, node) =>
wenzelm@44302
   566
            (singleton o Future.forks)
wenzelm@44673
   567
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME group)),
wenzelm@44614
   568
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@44645
   569
              (iterate_entries (fn (_, exec) => fn () => SOME (force_exec node exec)) node));
wenzelm@40520
   570
wenzelm@44673
   571
    in (versions, commands, execs, (version_id, group)) end);
wenzelm@44673
   572
wenzelm@44673
   573
wenzelm@44673
   574
(* remove versions *)
wenzelm@44673
   575
wenzelm@44673
   576
fun remove_versions ids state = state |> map_state (fn (versions, _, _, execution) =>
wenzelm@44673
   577
  let
wenzelm@44673
   578
    val _ = member (op =) ids (#1 execution) andalso
wenzelm@44673
   579
      error ("Attempt to remove execution version " ^ print_id (#1 execution));
wenzelm@44673
   580
wenzelm@44673
   581
    val versions' = fold delete_version ids versions;
wenzelm@44673
   582
    val (commands', execs') =
wenzelm@44673
   583
      (versions', (empty_commands, empty_execs)) |->
wenzelm@44673
   584
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@44673
   585
          Graph.fold (fn (_, (node, _)) => node |>
wenzelm@44673
   586
            iterate_entries (fn ((_, id), exec) => fn (commands, execs) =>
wenzelm@44673
   587
              let
wenzelm@44673
   588
                val commands' = Inttab.insert (K true) (id, the_command state id) commands;
wenzelm@44673
   589
                val execs' =
wenzelm@44673
   590
                  (case exec of
wenzelm@44673
   591
                    NONE => execs
wenzelm@44673
   592
                  | SOME exec_id => Inttab.insert (K true) (exec_id, the_exec state exec_id) execs);
wenzelm@44673
   593
              in SOME (commands', execs') end)));
wenzelm@44673
   594
  in (versions', commands', execs', execution) end);
wenzelm@38418
   595
wenzelm@43713
   596
wenzelm@43713
   597
wenzelm@43713
   598
(** global state **)
wenzelm@43713
   599
wenzelm@43713
   600
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   601
wenzelm@43713
   602
fun state () = Synchronized.value global_state;
wenzelm@43713
   603
val change_state = Synchronized.change global_state;
wenzelm@43713
   604
wenzelm@38418
   605
end;
wenzelm@38418
   606