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