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