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