src/Pure/PIDE/document.ML
author wenzelm
Wed Apr 11 11:44:21 2012 +0200 (2012-04-11)
changeset 47420 0dbe6c69eda2
parent 47415 c486ac962b89
child 47424 57ff63a52c53
permissions -rw-r--r--
just one dedicated execution per document version -- NB: non-monotonicity of cancel always requires fresh update;
explicit terminate_execution;
tuned source structure;
wenzelm@38150
     1
(*  Title:      Pure/PIDE/document.ML
wenzelm@38150
     2
    Author:     Makarius
wenzelm@38150
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@44478
     5
list of commands, with asynchronous read/eval/print processes.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@38363
    10
  type id = int
wenzelm@38373
    11
  type version_id = id
wenzelm@38363
    12
  type command_id = id
wenzelm@38373
    13
  type exec_id = id
wenzelm@38363
    14
  val no_id: id
wenzelm@38419
    15
  val new_id: unit -> id
wenzelm@38363
    16
  val parse_id: string -> id
wenzelm@38363
    17
  val print_id: id -> string
wenzelm@46938
    18
  type node_header = (string * Thy_Header.header) Exn.result
wenzelm@44157
    19
  datatype node_edit =
wenzelm@44185
    20
    Clear |
wenzelm@44157
    21
    Edits of (command_id option * command_id option) list |
wenzelm@44384
    22
    Header of node_header |
wenzelm@44385
    23
    Perspective of command_id list
wenzelm@44156
    24
  type edit = string * node_edit
wenzelm@38418
    25
  type state
wenzelm@38418
    26
  val init_state: state
wenzelm@44644
    27
  val define_command: command_id -> string -> string -> state -> state
wenzelm@47420
    28
  val remove_versions: version_id list -> state -> state
wenzelm@47343
    29
  val discontinue_execution: state -> unit
wenzelm@47404
    30
  val cancel_execution: state -> unit
wenzelm@47420
    31
  val start_execution: state -> unit
wenzelm@44481
    32
  val update: version_id -> version_id -> edit list -> state ->
wenzelm@47388
    33
    (command_id * exec_id option) list * state
wenzelm@43713
    34
  val state: unit -> state
wenzelm@43713
    35
  val change_state: (state -> state) -> unit
wenzelm@38150
    36
end;
wenzelm@38150
    37
wenzelm@38150
    38
structure Document: DOCUMENT =
wenzelm@38150
    39
struct
wenzelm@38150
    40
wenzelm@38150
    41
(* unique identifiers *)
wenzelm@38150
    42
wenzelm@38363
    43
type id = int;
wenzelm@38373
    44
type version_id = id;
wenzelm@38363
    45
type command_id = id;
wenzelm@38373
    46
type exec_id = id;
wenzelm@38355
    47
wenzelm@38355
    48
val no_id = 0;
wenzelm@40449
    49
val new_id = Synchronized.counter ();
wenzelm@38418
    50
wenzelm@38414
    51
val parse_id = Markup.parse_int;
wenzelm@38414
    52
val print_id = Markup.print_int;
wenzelm@38150
    53
wenzelm@38418
    54
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
wenzelm@38418
    55
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
wenzelm@38150
    56
wenzelm@38418
    57
wenzelm@38418
    58
wenzelm@38418
    59
(** document structure **)
wenzelm@38418
    60
wenzelm@46938
    61
type node_header = (string * Thy_Header.header) Exn.result;
wenzelm@47407
    62
type perspective = (command_id -> bool) * command_id option;
wenzelm@38448
    63
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    64
wenzelm@47341
    65
type exec = (Toplevel.state * unit lazy) Command.memo;  (*eval/print process*)
wenzelm@47341
    66
val no_exec = Command.memo_value (Toplevel.toplevel, Lazy.value ());
wenzelm@44674
    67
wenzelm@43668
    68
abstype node = Node of
wenzelm@47406
    69
 {header: node_header,
wenzelm@47407
    70
  perspective: perspective,  (*visible commands, last*)
wenzelm@47340
    71
  entries: (exec_id * exec) option Entries.T,  (*command entries with excecutions*)
wenzelm@47407
    72
  result: exec option}  (*result of last execution*)
wenzelm@43668
    73
and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    74
with
wenzelm@38418
    75
wenzelm@47406
    76
fun make_node (header, perspective, entries, result) =
wenzelm@47406
    77
  Node {header = header, perspective = perspective, entries = entries, result = result};
wenzelm@43668
    78
wenzelm@47406
    79
fun map_node f (Node {header, perspective, entries, result}) =
wenzelm@47406
    80
  make_node (f (header, perspective, entries, 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
wenzelm@47407
    88
val empty_node = make_node (no_header, no_perspective, Entries.empty, NONE);
wenzelm@47407
    89
val clear_node = map_node (fn (header, _, _, _) => (header, no_perspective, Entries.empty, NONE));
wenzelm@44386
    90
wenzelm@44386
    91
wenzelm@44386
    92
(* basic components *)
wenzelm@44386
    93
wenzelm@44180
    94
fun get_header (Node {header, ...}) = header;
wenzelm@44385
    95
fun set_header header =
wenzelm@47406
    96
  map_node (fn (_, perspective, entries, result) => (header, perspective, entries, result));
wenzelm@44180
    97
wenzelm@44385
    98
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
    99
fun set_perspective ids =
wenzelm@47406
   100
  map_node (fn (header, _, entries, result) => (header, make_perspective ids, entries, result));
wenzelm@44385
   101
wenzelm@47345
   102
val visible_command = #1 o get_perspective;
wenzelm@47345
   103
val visible_last = #2 o get_perspective;
wenzelm@47345
   104
val visible_node = is_some o visible_last
wenzelm@47345
   105
wenzelm@44444
   106
fun map_entries f =
wenzelm@47406
   107
  map_node (fn (header, perspective, entries, result) => (header, perspective, f entries, result));
wenzelm@44643
   108
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44645
   109
wenzelm@44645
   110
fun iterate_entries f = Entries.iterate NONE f o get_entries;
wenzelm@44645
   111
fun iterate_entries_after start f (Node {entries, ...}) =
wenzelm@44645
   112
  (case Entries.get_after entries start of
wenzelm@44645
   113
    NONE => I
wenzelm@44645
   114
  | SOME id => Entries.iterate (SOME id) f entries);
wenzelm@43668
   115
wenzelm@47339
   116
fun get_result (Node {result, ...}) = result;
wenzelm@44385
   117
fun set_result result =
wenzelm@47406
   118
  map_node (fn (header, perspective, entries, _) => (header, perspective, entries, result));
wenzelm@44180
   119
wenzelm@44185
   120
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   121
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   122
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   123
wenzelm@38418
   124
wenzelm@38448
   125
(* node edits and associated executions *)
wenzelm@38150
   126
wenzelm@44157
   127
datatype node_edit =
wenzelm@44185
   128
  Clear |
wenzelm@44157
   129
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   130
  Header of node_header |
wenzelm@44385
   131
  Perspective of command_id list;
wenzelm@44157
   132
wenzelm@44156
   133
type edit = string * node_edit;
wenzelm@38448
   134
wenzelm@44482
   135
fun after_entry (Node {entries, ...}) = Entries.get_after entries;
wenzelm@44479
   136
wenzelm@44480
   137
fun lookup_entry (Node {entries, ...}) id =
wenzelm@44480
   138
  (case Entries.lookup entries id of
wenzelm@44480
   139
    NONE => NONE
wenzelm@44480
   140
  | SOME (exec, _) => exec);
wenzelm@44480
   141
wenzelm@43668
   142
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   143
  (case Entries.lookup entries id of
wenzelm@38448
   144
    NONE => err_undef "command entry" id
wenzelm@44476
   145
  | SOME (exec, _) => exec);
wenzelm@38150
   146
wenzelm@47420
   147
fun the_default_entry node (SOME id) = (id, the_default (no_id, no_exec) (the_entry node id))
wenzelm@47341
   148
  | the_default_entry _ NONE = (no_id, (no_id, no_exec));
wenzelm@44476
   149
wenzelm@44482
   150
fun update_entry id exec =
wenzelm@44482
   151
  map_entries (Entries.update (id, exec));
wenzelm@44482
   152
wenzelm@44482
   153
fun reset_entry id node =
wenzelm@44482
   154
  if is_some (lookup_entry node id) then update_entry id NONE node else node;
wenzelm@38418
   155
wenzelm@38448
   156
fun reset_after id entries =
wenzelm@38448
   157
  (case Entries.get_after entries id of
wenzelm@38448
   158
    NONE => entries
wenzelm@38448
   159
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   160
wenzelm@43668
   161
val edit_node = map_entries o fold
wenzelm@43668
   162
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   163
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   164
wenzelm@38418
   165
wenzelm@38418
   166
(* version operations *)
wenzelm@38418
   167
wenzelm@44185
   168
val empty_version = Version Graph.empty;
wenzelm@44185
   169
wenzelm@38418
   170
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   171
val node_of = get_node o nodes_of;
wenzelm@38418
   172
wenzelm@44185
   173
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   174
wenzelm@44157
   175
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   176
  Version
wenzelm@44436
   177
    (case node_edit of
wenzelm@47406
   178
      Clear => update_node name clear_node nodes
wenzelm@47406
   179
    | Edits edits => update_node name (edit_node edits) nodes
wenzelm@44436
   180
    | Header header =>
wenzelm@44436
   181
        let
wenzelm@46938
   182
          val imports = (case header of Exn.Res (_, {imports, ...}) => imports | _ => []);
wenzelm@46945
   183
          val header' =
wenzelm@46958
   184
            ((case header of Exn.Res (_, h) => Thy_Header.define_keywords h | _ => ()); header)
wenzelm@46945
   185
              handle exn as ERROR _ => Exn.Exn exn;
wenzelm@44436
   186
          val nodes1 = nodes
wenzelm@44436
   187
            |> default_node name
wenzelm@46739
   188
            |> fold default_node imports;
wenzelm@44436
   189
          val nodes2 = nodes1
wenzelm@44436
   190
            |> Graph.Keys.fold
wenzelm@44436
   191
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@46945
   192
          val (header'', nodes3) =
wenzelm@46945
   193
            (header', Graph.add_deps_acyclic (name, imports) nodes2)
wenzelm@44436
   194
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@46945
   195
        in Graph.map_node name (set_header header'') nodes3 end
wenzelm@47406
   196
    | Perspective perspective => update_node name (set_perspective perspective) nodes);
wenzelm@44444
   197
wenzelm@44222
   198
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   199
  Version (update_node name (K node) nodes);
wenzelm@38418
   200
wenzelm@38150
   201
end;
wenzelm@38150
   202
wenzelm@38418
   203
wenzelm@38418
   204
wenzelm@47389
   205
(** main state -- document structure and execution process **)
wenzelm@38418
   206
wenzelm@38418
   207
abstype state = State of
wenzelm@40398
   208
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@47335
   209
  commands: (string * Token.T list lazy) Inttab.table,  (*command_id -> named span*)
wenzelm@47343
   210
  execution: version_id * Future.group * bool Unsynchronized.ref}  (*current execution process*)
wenzelm@38418
   211
with
wenzelm@38418
   212
wenzelm@44674
   213
fun make_state (versions, commands, execution) =
wenzelm@44674
   214
  State {versions = versions, commands = commands, execution = execution};
wenzelm@38418
   215
wenzelm@44674
   216
fun map_state f (State {versions, commands, execution}) =
wenzelm@44674
   217
  make_state (f (versions, commands, execution));
wenzelm@38418
   218
wenzelm@38418
   219
val init_state =
wenzelm@47343
   220
  make_state (Inttab.make [(no_id, empty_version)], Inttab.empty,
wenzelm@47343
   221
    (no_id, Future.new_group NONE, Unsynchronized.ref false));
wenzelm@38418
   222
wenzelm@47420
   223
fun execution_of (State {execution, ...}) = execution;
wenzelm@47420
   224
wenzelm@38418
   225
wenzelm@38418
   226
(* document versions *)
wenzelm@38418
   227
wenzelm@38418
   228
fun define_version (id: version_id) version =
wenzelm@47420
   229
  map_state (fn (versions, commands, _) =>
wenzelm@47420
   230
    let
wenzelm@47420
   231
      val versions' = Inttab.update_new (id, version) versions
wenzelm@47420
   232
        handle Inttab.DUP dup => err_dup "document version" dup;
wenzelm@47420
   233
      val execution' = (id, Future.new_group NONE, Unsynchronized.ref true);
wenzelm@47420
   234
    in (versions', commands, execution') end);
wenzelm@38418
   235
wenzelm@38418
   236
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   237
  (case Inttab.lookup versions id of
wenzelm@38418
   238
    NONE => err_undef "document version" id
wenzelm@38418
   239
  | SOME version => version);
wenzelm@38418
   240
wenzelm@44673
   241
fun delete_version (id: version_id) versions = Inttab.delete id versions
wenzelm@44673
   242
  handle Inttab.UNDEF _ => err_undef "document version" id;
wenzelm@44673
   243
wenzelm@38418
   244
wenzelm@38418
   245
(* commands *)
wenzelm@38418
   246
wenzelm@44644
   247
fun define_command (id: command_id) name text =
wenzelm@44674
   248
  map_state (fn (versions, commands, execution) =>
wenzelm@38418
   249
    let
wenzelm@47395
   250
      val id_string = print_id id;
wenzelm@47395
   251
      val span = Lazy.lazy (fn () =>
wenzelm@47395
   252
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@47395
   253
          (fn () =>
wenzelm@47395
   254
            Thy_Syntax.parse_tokens
wenzelm@47395
   255
              (#1 (Outer_Syntax.get_syntax ())) (Position.id id_string) text) ());
wenzelm@47395
   256
      val _ =
wenzelm@47395
   257
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@47395
   258
          (fn () => Output.status (Markup.markup_only Isabelle_Markup.accepted)) ();
wenzelm@38418
   259
      val commands' =
wenzelm@46945
   260
        Inttab.update_new (id, (name, span)) commands
wenzelm@38418
   261
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44674
   262
    in (versions, commands', execution) end);
wenzelm@38418
   263
wenzelm@38418
   264
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@44660
   265
  (case Inttab.lookup commands id of
wenzelm@38418
   266
    NONE => err_undef "command" id
wenzelm@44644
   267
  | SOME command => command);
wenzelm@38418
   268
wenzelm@47420
   269
end;
wenzelm@38418
   270
wenzelm@47420
   271
fun remove_versions ids state = state |> map_state (fn (versions, _, execution) =>
wenzelm@47420
   272
  let
wenzelm@47420
   273
    val _ = member (op =) ids (#1 execution) andalso
wenzelm@47420
   274
      error ("Attempt to remove execution version " ^ print_id (#1 execution));
wenzelm@47343
   275
wenzelm@47420
   276
    val versions' = fold delete_version ids versions;
wenzelm@47420
   277
    val commands' =
wenzelm@47420
   278
      (versions', Inttab.empty) |->
wenzelm@47420
   279
        Inttab.fold (fn (_, version) => nodes_of version |>
wenzelm@47420
   280
          Graph.fold (fn (_, (node, _)) => node |>
wenzelm@47420
   281
            iterate_entries (fn ((_, id), _) =>
wenzelm@47420
   282
              SOME o Inttab.insert (K true) (id, the_command state id))));
wenzelm@47420
   283
  in (versions', commands', execution) end);
wenzelm@38418
   284
wenzelm@38418
   285
wenzelm@38888
   286
wenzelm@47420
   287
(** document execution **)
wenzelm@47389
   288
wenzelm@47420
   289
val discontinue_execution = execution_of #> (fn (_, _, running) => running := false);
wenzelm@47389
   290
wenzelm@47420
   291
val cancel_execution = execution_of #> (fn (_, group, _) => Future.cancel_group group);
wenzelm@47345
   292
wenzelm@47420
   293
fun terminate_execution state =
wenzelm@47420
   294
  let
wenzelm@47420
   295
    val (_, group, _) = execution_of state;
wenzelm@47420
   296
    val _ = Future.cancel_group group;
wenzelm@47420
   297
  in Future.join_tasks (Future.group_tasks group) end;
wenzelm@47345
   298
wenzelm@47420
   299
fun start_execution state =
wenzelm@47420
   300
  let
wenzelm@47420
   301
    fun finished_theory node =
wenzelm@47420
   302
      (case Exn.capture (Command.memo_result o the) (get_result node) of
wenzelm@47420
   303
        Exn.Res (st, _) => can (Toplevel.end_theory Position.none) st
wenzelm@47420
   304
      | _ => false);
wenzelm@47345
   305
wenzelm@47420
   306
    fun run node command_id exec =
wenzelm@47420
   307
      let
wenzelm@47420
   308
        val (_, print) = Command.memo_eval exec;
wenzelm@47420
   309
        val _ =
wenzelm@47420
   310
          if visible_command node command_id
wenzelm@47420
   311
          then ignore (Lazy.future Future.default_params print)
wenzelm@47420
   312
          else ();
wenzelm@47420
   313
      in () end;
wenzelm@47345
   314
wenzelm@47420
   315
    val (version_id, group, running) = execution_of state;
wenzelm@47420
   316
    val _ =
wenzelm@47420
   317
      nodes_of (the_version state version_id) |> Graph.schedule
wenzelm@47420
   318
        (fn deps => fn (name, node) =>
wenzelm@47420
   319
          if not (visible_node node) andalso finished_theory node then
wenzelm@47420
   320
            Future.value ()
wenzelm@47420
   321
          else
wenzelm@47420
   322
            (singleton o Future.forks)
wenzelm@47420
   323
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME group)),
wenzelm@47420
   324
                deps = map (Future.task_of o #2) deps, pri = ~2, interrupts = false}
wenzelm@47420
   325
              (fn () =>
wenzelm@47420
   326
                iterate_entries (fn ((_, id), opt_exec) => fn () =>
wenzelm@47420
   327
                  (case opt_exec of
wenzelm@47420
   328
                    SOME (_, exec) => if ! running then SOME (run node id exec) else NONE
wenzelm@47420
   329
                  | NONE => NONE)) node ()));
wenzelm@47420
   330
  in () end;
wenzelm@47345
   331
wenzelm@47345
   332
wenzelm@47420
   333
wenzelm@47420
   334
(** document update **)
wenzelm@38418
   335
wenzelm@38418
   336
local
wenzelm@38418
   337
wenzelm@47389
   338
fun stable_finished_theory node =
wenzelm@47389
   339
  is_some (Exn.get_res (Exn.capture (fn () =>
wenzelm@47407
   340
    fst (Command.memo_result (the (get_result node)))
wenzelm@47389
   341
    |> Toplevel.end_theory Position.none
wenzelm@47389
   342
    |> Global_Theory.join_proofs) ()));
wenzelm@47389
   343
wenzelm@47389
   344
fun stable_command exec =
wenzelm@47389
   345
  (case Exn.capture Command.memo_result exec of
wenzelm@47389
   346
    Exn.Exn exn => not (Exn.is_interrupt exn)
wenzelm@47389
   347
  | Exn.Res (st, _) =>
wenzelm@47389
   348
      (case try Toplevel.theory_of st of
wenzelm@47389
   349
        NONE => true
wenzelm@47389
   350
      | SOME thy => is_some (Exn.get_res (Exn.capture Global_Theory.join_recent_proofs thy))));
wenzelm@47389
   351
wenzelm@47335
   352
fun make_required nodes =
wenzelm@47335
   353
  let
wenzelm@47335
   354
    val all_visible =
wenzelm@47345
   355
      Graph.fold (fn (a, (node, _)) => visible_node node ? cons a) nodes []
wenzelm@47345
   356
      |> Graph.all_preds nodes
wenzelm@47345
   357
      |> map (rpair ()) |> Symtab.make;
wenzelm@47345
   358
wenzelm@47335
   359
    val required =
wenzelm@47345
   360
      Symtab.fold (fn (a, ()) =>
wenzelm@47345
   361
        exists (Symtab.defined all_visible) (Graph.immediate_succs nodes a) ?
wenzelm@47345
   362
          Symtab.update (a, ())) all_visible Symtab.empty;
wenzelm@47335
   363
  in Symtab.defined required end;
wenzelm@47335
   364
wenzelm@47406
   365
fun init_theory imports node =
wenzelm@47335
   366
  let
wenzelm@47335
   367
    (* FIXME provide files via Scala layer, not master_dir *)
wenzelm@47335
   368
    val (dir, header) = Exn.release (get_header node);
wenzelm@47335
   369
    val master_dir =
wenzelm@47335
   370
      (case Url.explode dir of
wenzelm@47335
   371
        Url.File path => path
wenzelm@47335
   372
      | _ => Path.current);
wenzelm@47335
   373
    val parents =
wenzelm@47335
   374
      #imports header |> map (fn import =>
wenzelm@47407
   375
        (case Thy_Info.lookup_theory import of
wenzelm@47335
   376
          SOME thy => thy
wenzelm@47335
   377
        | NONE =>
wenzelm@47339
   378
            Toplevel.end_theory (Position.file_only import)
wenzelm@47407
   379
              (fst
wenzelm@47407
   380
                (Command.memo_result
wenzelm@47407
   381
                  (the_default no_exec
wenzelm@47407
   382
                    (get_result (snd (the (AList.lookup (op =) imports import)))))))));
wenzelm@47335
   383
  in Thy_Load.begin_theory master_dir header parents end;
wenzelm@47335
   384
wenzelm@47407
   385
fun check_theory full name node =
wenzelm@47407
   386
  is_some (Thy_Info.lookup_theory name) orelse
wenzelm@47407
   387
  is_some (Exn.get_res (get_header node)) andalso (not full orelse is_some (get_result node));
wenzelm@47335
   388
wenzelm@44645
   389
fun last_common state last_visible node0 node =
wenzelm@44482
   390
  let
wenzelm@44645
   391
    fun update_flags prev (visible, initial) =
wenzelm@44645
   392
      let
wenzelm@44645
   393
        val visible' = visible andalso prev <> last_visible;
wenzelm@44645
   394
        val initial' = initial andalso
wenzelm@44645
   395
          (case prev of
wenzelm@44645
   396
            NONE => true
wenzelm@44645
   397
          | SOME id => not (Keyword.is_theory_begin (#1 (the_command state id))));
wenzelm@44645
   398
      in (visible', initial') end;
wenzelm@47340
   399
    fun get_common ((prev, id), opt_exec) (found, (_, flags)) =
wenzelm@44482
   400
      if found then NONE
wenzelm@44482
   401
      else
wenzelm@44674
   402
        let val found' =
wenzelm@47340
   403
          (case opt_exec of
wenzelm@47340
   404
            NONE => true
wenzelm@47340
   405
          | SOME (exec_id, exec) =>
wenzelm@47389
   406
              not (stable_command exec) orelse
wenzelm@47340
   407
              (case lookup_entry node0 id of
wenzelm@47340
   408
                NONE => true
wenzelm@47340
   409
              | SOME (exec_id0, _) => exec_id <> exec_id0));
wenzelm@44645
   410
        in SOME (found', (prev, update_flags prev flags)) end;
wenzelm@44645
   411
    val (found, (common, flags)) =
wenzelm@44645
   412
      iterate_entries get_common node (false, (NONE, (true, true)));
wenzelm@44645
   413
  in
wenzelm@44645
   414
    if found then (common, flags)
wenzelm@44645
   415
    else
wenzelm@44645
   416
      let val last = Entries.get_after (get_entries node) common
wenzelm@44645
   417
      in (last, update_flags last flags) end
wenzelm@44645
   418
  end;
wenzelm@44645
   419
wenzelm@44645
   420
fun illegal_init () = error "Illegal theory header after end of theory";
wenzelm@44482
   421
wenzelm@47407
   422
fun new_exec state proper_init command_id' (execs, command_exec, init) =
wenzelm@47407
   423
  if not proper_init andalso is_none init then NONE
wenzelm@44645
   424
  else
wenzelm@44645
   425
    let
wenzelm@46945
   426
      val (name, span) = the_command state command_id' ||> Lazy.force;
wenzelm@44645
   427
      val (modify_init, init') =
wenzelm@44645
   428
        if Keyword.is_theory_begin name then
wenzelm@44645
   429
          (Toplevel.modify_init (the_default illegal_init init), NONE)
wenzelm@44645
   430
        else (I, init);
wenzelm@44660
   431
      val exec_id' = new_id ();
wenzelm@46876
   432
      val exec_id'_string = print_id exec_id';
wenzelm@46876
   433
      val tr =
wenzelm@46876
   434
        Position.setmp_thread_data (Position.id_only exec_id'_string)
wenzelm@46876
   435
          (fn () =>
wenzelm@46876
   436
            #1 (Outer_Syntax.read_span (#2 (Outer_Syntax.get_syntax ())) span)
wenzelm@46876
   437
            |> modify_init
wenzelm@46876
   438
            |> Toplevel.put_id exec_id'_string);
wenzelm@47406
   439
      val exec' =
wenzelm@47406
   440
        Command.memo (fn () =>
wenzelm@47406
   441
          Command.run_command (tr ()) (fst (Command.memo_result (snd (snd command_exec)))));
wenzelm@44674
   442
      val command_exec' = (command_id', (exec_id', exec'));
wenzelm@44674
   443
    in SOME (command_exec' :: execs, command_exec', init') end;
wenzelm@44482
   444
wenzelm@38418
   445
in
wenzelm@38418
   446
wenzelm@44481
   447
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   448
  let
wenzelm@38418
   449
    val old_version = the_version state old_id;
wenzelm@44180
   450
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   451
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   452
wenzelm@44544
   453
    val nodes = nodes_of new_version;
wenzelm@44544
   454
    val is_required = make_required nodes;
wenzelm@44544
   455
wenzelm@47420
   456
    val _ = terminate_execution state;
wenzelm@44476
   457
    val updated =
wenzelm@44544
   458
      nodes |> Graph.schedule
wenzelm@44199
   459
        (fn deps => fn (name, node) =>
wenzelm@47406
   460
          (singleton o Future.forks)
wenzelm@47406
   461
            {name = "Document.update", group = NONE,
wenzelm@47406
   462
              deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@47406
   463
            (fn () =>
wenzelm@47406
   464
              let
wenzelm@47406
   465
                val imports = map (apsnd Future.join) deps;
wenzelm@47406
   466
                val updated_imports = exists (is_some o #3 o #1 o #2) imports;
wenzelm@47406
   467
                val required = is_required name;
wenzelm@47406
   468
              in
wenzelm@47406
   469
                if updated_imports orelse AList.defined (op =) edits name orelse
wenzelm@47406
   470
                  required andalso not (stable_finished_theory node)
wenzelm@47406
   471
                then
wenzelm@44482
   472
                  let
wenzelm@47406
   473
                    val node0 = node_of old_version name;
wenzelm@47406
   474
                    fun init () = init_theory imports node;
wenzelm@47407
   475
                    val proper_init =
wenzelm@47407
   476
                      check_theory false name node andalso
wenzelm@47407
   477
                      forall (fn (name, (_, node)) => check_theory true name node) imports;
wenzelm@47406
   478
wenzelm@47345
   479
                    val last_visible = visible_last node;
wenzelm@47406
   480
                    val (common, (visible, initial)) =
wenzelm@47406
   481
                      if updated_imports then (NONE, (true, true))
wenzelm@47406
   482
                      else last_common state last_visible node0 node;
wenzelm@44674
   483
                    val common_command_exec = the_default_entry node common;
wenzelm@44479
   484
wenzelm@47405
   485
                    val (new_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@47407
   491
                            else new_exec state proper_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@47405
   497
                          else if exists (fn (_, (id, _)) => id0 = id) new_execs then SOME res
wenzelm@44482
   498
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   499
wenzelm@47405
   500
                    val last_exec = if command_id' = no_id then NONE else SOME command_id';
wenzelm@44482
   501
                    val result =
wenzelm@47407
   502
                      if is_some (after_entry node last_exec) then NONE
wenzelm@47407
   503
                      else SOME exec';
wenzelm@44480
   504
wenzelm@44482
   505
                    val node' = node
wenzelm@44482
   506
                      |> fold reset_entry no_execs
wenzelm@47405
   507
                      |> fold (fn (id, exec) => update_entry id (SOME exec)) new_execs
wenzelm@47406
   508
                      |> set_result result;
wenzelm@47406
   509
                    val updated_node =
wenzelm@47406
   510
                      if null no_execs andalso null new_execs then NONE
wenzelm@47406
   511
                      else SOME (name, node');
wenzelm@47406
   512
                  in ((no_execs, new_execs, updated_node), node') end
wenzelm@47406
   513
                else (([], [], NONE), node)
wenzelm@47406
   514
              end))
wenzelm@44479
   515
      |> Future.joins |> map #1;
wenzelm@44476
   516
wenzelm@44479
   517
    val command_execs =
wenzelm@44479
   518
      map (rpair NONE) (maps #1 updated) @
wenzelm@44674
   519
      map (fn (command_id, (exec_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@47406
   520
    val updated_nodes = map_filter #3 updated;
wenzelm@38418
   521
wenzelm@44197
   522
    val state' = state
wenzelm@44476
   523
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@47388
   524
  in (command_execs, state') end;
wenzelm@38418
   525
wenzelm@38418
   526
end;
wenzelm@38418
   527
wenzelm@38418
   528
wenzelm@43713
   529
wenzelm@43713
   530
(** global state **)
wenzelm@43713
   531
wenzelm@43713
   532
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   533
wenzelm@43713
   534
fun state () = Synchronized.value global_state;
wenzelm@43713
   535
val change_state = Synchronized.change global_state;
wenzelm@43713
   536
wenzelm@38418
   537
end;
wenzelm@38418
   538