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