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