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