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