src/Pure/PIDE/document.ML
author wenzelm
Fri Sep 02 15:21:40 2011 +0200 (2011-09-02)
changeset 44645 5938beb84adc
parent 44644 317e4962dd0f
child 44660 90bab3febb6c
permissions -rw-r--r--
more precise iterate_entries_after if start refers to last entry;
do not assign exec states after bad theory init;
reject illegal theory header after end of theory;
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@44610
    28
  val join_commands: state -> state
wenzelm@44301
    29
  val cancel_execution: state -> Future.task list
wenzelm@44436
    30
  val update_perspective: version_id -> version_id -> string -> command_id list -> state -> state
wenzelm@44481
    31
  val update: version_id -> version_id -> edit list -> state ->
wenzelm@44479
    32
    ((command_id * exec_id option) list * (string * command_id option) list) * state
wenzelm@38418
    33
  val execute: version_id -> state -> state
wenzelm@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@44610
   240
  commands:
wenzelm@44644
   241
    (string * Toplevel.transition future) Inttab.table *  (*command_id -> name * transition*)
wenzelm@44610
   242
    Toplevel.transition future list,  (*recent commands to be joined*)
wenzelm@44386
   243
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   244
    (*exec_id -> command_id with eval/print process*)
wenzelm@44300
   245
  execution: Future.group}  (*global execution process*)
wenzelm@38418
   246
with
wenzelm@38418
   247
wenzelm@38418
   248
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   249
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   250
wenzelm@38418
   251
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   252
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   253
wenzelm@38418
   254
val init_state =
wenzelm@38418
   255
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@44644
   256
    (Inttab.make [(no_id, (Toplevel.name_of Toplevel.empty, Future.value Toplevel.empty))], []),
wenzelm@44386
   257
    Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
wenzelm@44300
   258
    Future.new_group NONE);
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@38418
   274
wenzelm@38418
   275
(* commands *)
wenzelm@38418
   276
wenzelm@44644
   277
fun define_command (id: command_id) name text =
wenzelm@44610
   278
  map_state (fn (versions, (commands, recent), execs, execution) =>
wenzelm@38418
   279
    let
wenzelm@38418
   280
      val id_string = print_id id;
wenzelm@44302
   281
      val tr =
wenzelm@44302
   282
        Future.fork_pri 2 (fn () =>
wenzelm@44302
   283
          Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44478
   284
            (fn () => Outer_Syntax.read_command (Position.id id_string) text) ());
wenzelm@38418
   285
      val commands' =
wenzelm@44644
   286
        Inttab.update_new (id, (name, tr)) commands
wenzelm@38418
   287
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44610
   288
    in (versions, (commands', tr :: recent), execs, execution) end);
wenzelm@38418
   289
wenzelm@38418
   290
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@44610
   291
  (case Inttab.lookup (#1 commands) id of
wenzelm@38418
   292
    NONE => err_undef "command" id
wenzelm@44644
   293
  | SOME command => command);
wenzelm@38418
   294
wenzelm@44610
   295
val join_commands =
wenzelm@44610
   296
    map_state (fn (versions, (commands, recent), execs, execution) =>
wenzelm@44610
   297
      (Future.join_results recent; (versions, (commands, []), execs, execution)));
wenzelm@38421
   298
wenzelm@38418
   299
wenzelm@38418
   300
(* command executions *)
wenzelm@38418
   301
wenzelm@44197
   302
fun define_exec (exec_id, exec) =
wenzelm@38418
   303
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   304
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   305
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   306
    in (versions, commands, execs', execution) end);
wenzelm@38418
   307
wenzelm@44197
   308
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   309
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   310
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   311
  | SOME exec => exec);
wenzelm@38418
   312
wenzelm@41634
   313
wenzelm@41634
   314
(* document execution *)
wenzelm@41634
   315
wenzelm@44299
   316
fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
wenzelm@41634
   317
wenzelm@38418
   318
end;
wenzelm@38418
   319
wenzelm@38418
   320
wenzelm@38418
   321
wenzelm@38888
   322
(* toplevel transactions *)
wenzelm@38888
   323
wenzelm@38888
   324
local
wenzelm@38888
   325
wenzelm@44440
   326
fun timing tr t =
wenzelm@44440
   327
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   328
wenzelm@38888
   329
fun proof_status tr st =
wenzelm@38888
   330
  (case try Toplevel.proof_of st of
wenzelm@38888
   331
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   332
  | NONE => ());
wenzelm@38888
   333
wenzelm@44386
   334
fun print_state tr st =
wenzelm@44386
   335
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   336
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   337
wenzelm@40390
   338
fun run int tr st =
wenzelm@40390
   339
  (case Toplevel.transition int tr st of
wenzelm@40390
   340
    SOME (st', NONE) => ([], SOME st')
wenzelm@44614
   341
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@44270
   342
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@40390
   343
wenzelm@38888
   344
in
wenzelm@38888
   345
wenzelm@44199
   346
fun run_command tr st =
wenzelm@44185
   347
  let
wenzelm@44199
   348
    val is_init = Toplevel.is_init tr;
wenzelm@44185
   349
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   350
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   351
wenzelm@44614
   352
    val _ = Multithreading.interrupted ();
wenzelm@44611
   353
    val _ = Toplevel.status tr Markup.forked;
wenzelm@44185
   354
    val start = Timing.start ();
wenzelm@44645
   355
    val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   356
    val _ = timing tr (Timing.result start);
wenzelm@44611
   357
    val _ = Toplevel.status tr Markup.joined;
wenzelm@44185
   358
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   359
  in
wenzelm@44185
   360
    (case result of
wenzelm@44614
   361
      NONE =>
wenzelm@44614
   362
       (if null errs then Exn.interrupt () else ();
wenzelm@44614
   363
        Toplevel.status tr Markup.failed;
wenzelm@44614
   364
        (st, no_print))
wenzelm@44185
   365
    | SOME st' =>
wenzelm@44185
   366
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   367
        proof_status tr st';
wenzelm@44386
   368
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   369
  end;
wenzelm@38888
   370
wenzelm@38888
   371
end;
wenzelm@38888
   372
wenzelm@38888
   373
wenzelm@38888
   374
wenzelm@38888
   375
wenzelm@44481
   376
(** update **)
wenzelm@38418
   377
wenzelm@44436
   378
(* perspective *)
wenzelm@44436
   379
wenzelm@44436
   380
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   381
  let
wenzelm@44436
   382
    val old_version = the_version state old_id;
wenzelm@44436
   383
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   384
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   385
  in define_version new_id new_version state end;
wenzelm@44436
   386
wenzelm@44436
   387
wenzelm@44481
   388
(* edits *)
wenzelm@38418
   389
wenzelm@38418
   390
local
wenzelm@38418
   391
wenzelm@44645
   392
fun last_common state last_visible node0 node =
wenzelm@44482
   393
  let
wenzelm@44645
   394
    fun update_flags prev (visible, initial) =
wenzelm@44645
   395
      let
wenzelm@44645
   396
        val visible' = visible andalso prev <> last_visible;
wenzelm@44645
   397
        val initial' = initial andalso
wenzelm@44645
   398
          (case prev of
wenzelm@44645
   399
            NONE => true
wenzelm@44645
   400
          | SOME id => not (Keyword.is_theory_begin (#1 (the_command state id))));
wenzelm@44645
   401
      in (visible', initial') end;
wenzelm@44645
   402
    fun get_common ((prev, id), exec) (found, (_, flags)) =
wenzelm@44482
   403
      if found then NONE
wenzelm@44482
   404
      else
wenzelm@44482
   405
        let val found' = is_none exec orelse exec <> lookup_entry node0 id
wenzelm@44645
   406
        in SOME (found', (prev, update_flags prev flags)) end;
wenzelm@44645
   407
    val (found, (common, flags)) =
wenzelm@44645
   408
      iterate_entries get_common node (false, (NONE, (true, true)));
wenzelm@44645
   409
  in
wenzelm@44645
   410
    if found then (common, flags)
wenzelm@44645
   411
    else
wenzelm@44645
   412
      let val last = Entries.get_after (get_entries node) common
wenzelm@44645
   413
      in (last, update_flags last flags) end
wenzelm@44645
   414
  end;
wenzelm@44645
   415
wenzelm@44645
   416
fun illegal_init () = error "Illegal theory header after end of theory";
wenzelm@44482
   417
wenzelm@44645
   418
fun new_exec state bad_init command_id' (execs, exec, init) =
wenzelm@44645
   419
  if bad_init andalso is_none init then NONE
wenzelm@44645
   420
  else
wenzelm@44645
   421
    let
wenzelm@44645
   422
      val (name, tr0) = the_command state command_id';
wenzelm@44645
   423
      val (modify_init, init') =
wenzelm@44645
   424
        if Keyword.is_theory_begin name then
wenzelm@44645
   425
          (Toplevel.modify_init (the_default illegal_init init), NONE)
wenzelm@44645
   426
        else (I, init);
wenzelm@44645
   427
        val exec_id' = new_id ();
wenzelm@44645
   428
      val exec' =
wenzelm@44645
   429
        snd exec |> Lazy.map (fn (st, _) =>
wenzelm@44645
   430
          let val tr =
wenzelm@44645
   431
            Future.join tr0
wenzelm@44645
   432
            |> modify_init
wenzelm@44645
   433
            |> Toplevel.put_id (print_id exec_id');
wenzelm@44645
   434
          in run_command tr st end)
wenzelm@44645
   435
        |> pair command_id';
wenzelm@44645
   436
    in SOME ((exec_id', exec') :: execs, exec', init') end;
wenzelm@44482
   437
wenzelm@44544
   438
fun make_required nodes =
wenzelm@44544
   439
  let
wenzelm@44544
   440
    val all_visible =
wenzelm@44544
   441
      Graph.fold (fn (a, (node, _)) => is_some (#2 (get_perspective node)) ? cons a) nodes []
wenzelm@44544
   442
      |> Graph.all_preds nodes;
wenzelm@44544
   443
    val required =
wenzelm@44544
   444
      fold (fn a => exists (fn b => Graph.is_edge nodes (a, b)) all_visible ? Symtab.update (a, ()))
wenzelm@44544
   445
        all_visible Symtab.empty;
wenzelm@44544
   446
  in Symtab.defined required end;
wenzelm@44544
   447
wenzelm@44645
   448
fun check_theory nodes name =
wenzelm@44645
   449
  is_some (Thy_Info.lookup_theory name) orelse  (* FIXME more robust!? *)
wenzelm@44645
   450
  is_some (Exn.get_res (get_header (get_node nodes name)));
wenzelm@44645
   451
wenzelm@44441
   452
fun init_theory deps name node =
wenzelm@44435
   453
  let
wenzelm@44435
   454
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   455
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   456
    val (dir, files) =
wenzelm@44435
   457
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   458
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   459
wenzelm@44435
   460
    val parents =
wenzelm@44435
   461
      imports |> map (fn import =>
wenzelm@44441
   462
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   463
          SOME thy => thy
wenzelm@44435
   464
        | NONE =>
wenzelm@44435
   465
            get_theory (Position.file_only import)
wenzelm@44482
   466
              (snd (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   467
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   468
wenzelm@38418
   469
in
wenzelm@38418
   470
wenzelm@44481
   471
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   472
  let
wenzelm@38418
   473
    val old_version = the_version state old_id;
wenzelm@44180
   474
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   475
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   476
wenzelm@44544
   477
    val nodes = nodes_of new_version;
wenzelm@44544
   478
    val is_required = make_required nodes;
wenzelm@44544
   479
wenzelm@44476
   480
    val updated =
wenzelm@44544
   481
      nodes |> Graph.schedule
wenzelm@44199
   482
        (fn deps => fn (name, node) =>
wenzelm@44544
   483
          if not (is_touched node orelse is_required name)
wenzelm@44544
   484
          then Future.value (([], [], []), node)
wenzelm@44444
   485
          else
wenzelm@44479
   486
            let
wenzelm@44479
   487
              val node0 = node_of old_version name;
wenzelm@44479
   488
              fun init () = init_theory deps name node;
wenzelm@44645
   489
              val bad_init =
wenzelm@44645
   490
                not (check_theory nodes name andalso forall (check_theory nodes o #1) deps);
wenzelm@44479
   491
            in
wenzelm@44482
   492
              (singleton o Future.forks)
wenzelm@44482
   493
                {name = "Document.update", group = NONE,
wenzelm@44482
   494
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44482
   495
                (fn () =>
wenzelm@44482
   496
                  let
wenzelm@44544
   497
                    val required = is_required name;
wenzelm@44482
   498
                    val last_visible = #2 (get_perspective node);
wenzelm@44645
   499
                    val (common, (visible, initial)) = last_common state last_visible node0 node;
wenzelm@44482
   500
                    val common_exec = the_exec state (the_default_entry node common);
wenzelm@44479
   501
wenzelm@44645
   502
                    val (execs, exec, _) =
wenzelm@44645
   503
                      ([], common_exec, if initial then SOME init else NONE) |>
wenzelm@44544
   504
                      (visible orelse required) ?
wenzelm@44645
   505
                        iterate_entries_after common
wenzelm@44482
   506
                          (fn ((prev, id), _) => fn res =>
wenzelm@44544
   507
                            if not required andalso prev = last_visible then NONE
wenzelm@44645
   508
                            else new_exec state bad_init id res) node;
wenzelm@44479
   509
wenzelm@44482
   510
                    val no_execs =
wenzelm@44645
   511
                      iterate_entries_after common
wenzelm@44482
   512
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   513
                          if is_none exec0 then NONE
wenzelm@44482
   514
                          else if exists (fn (_, (id, _)) => id0 = id) execs then SOME res
wenzelm@44482
   515
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   516
wenzelm@44482
   517
                    val last_exec = if #1 exec = no_id then NONE else SOME (#1 exec);
wenzelm@44482
   518
                    val result =
wenzelm@44482
   519
                      if is_some (after_entry node last_exec) then no_result
wenzelm@44482
   520
                      else Lazy.map #1 (#2 exec);
wenzelm@44480
   521
wenzelm@44482
   522
                    val node' = node
wenzelm@44482
   523
                      |> fold reset_entry no_execs
wenzelm@44482
   524
                      |> fold (fn (exec_id, (id, _)) => update_entry id (SOME exec_id)) execs
wenzelm@44482
   525
                      |> set_last_exec last_exec
wenzelm@44482
   526
                      |> set_result result
wenzelm@44482
   527
                      |> set_touched false;
wenzelm@44482
   528
                  in ((no_execs, execs, [(name, node')]), node') end)
wenzelm@44479
   529
            end)
wenzelm@44479
   530
      |> Future.joins |> map #1;
wenzelm@44476
   531
wenzelm@44479
   532
    val command_execs =
wenzelm@44479
   533
      map (rpair NONE) (maps #1 updated) @
wenzelm@44479
   534
      map (fn (exec_id, (command_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   535
    val updated_nodes = maps #3 updated;
wenzelm@44476
   536
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   537
wenzelm@44197
   538
    val state' = state
wenzelm@44479
   539
      |> fold (fold define_exec o #2) updated
wenzelm@44476
   540
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   541
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   542
wenzelm@38418
   543
end;
wenzelm@38418
   544
wenzelm@38418
   545
wenzelm@38418
   546
(* execute *)
wenzelm@38418
   547
wenzelm@38418
   548
fun execute version_id state =
wenzelm@41673
   549
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   550
    let
wenzelm@38418
   551
      val version = the_version state version_id;
wenzelm@38418
   552
wenzelm@44386
   553
      fun force_exec _ NONE = ()
wenzelm@44386
   554
        | force_exec node (SOME exec_id) =
wenzelm@44386
   555
            let
wenzelm@44386
   556
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   557
              val (_, print) = Lazy.force exec;
wenzelm@44386
   558
              val _ =
wenzelm@44439
   559
                if #1 (get_perspective node) command_id
wenzelm@44386
   560
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   561
                else ();
wenzelm@44386
   562
            in () end;
wenzelm@38418
   563
wenzelm@44300
   564
      val execution = Future.new_group NONE;
wenzelm@44299
   565
      val _ =
wenzelm@44201
   566
        nodes_of version |> Graph.schedule
wenzelm@44201
   567
          (fn deps => fn (name, node) =>
wenzelm@44302
   568
            (singleton o Future.forks)
wenzelm@44302
   569
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
wenzelm@44614
   570
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@44645
   571
              (iterate_entries (fn (_, exec) => fn () => SOME (force_exec node exec)) node));
wenzelm@40520
   572
wenzelm@44299
   573
    in (versions, commands, execs, execution) end);
wenzelm@38418
   574
wenzelm@43713
   575
wenzelm@43713
   576
wenzelm@43713
   577
(** global state **)
wenzelm@43713
   578
wenzelm@43713
   579
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   580
wenzelm@43713
   581
fun state () = Synchronized.value global_state;
wenzelm@43713
   582
val change_state = Synchronized.change global_state;
wenzelm@43713
   583
wenzelm@38418
   584
end;
wenzelm@38418
   585