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