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