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