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