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