src/Pure/PIDE/document.ML
author wenzelm
Thu Sep 01 23:08:42 2011 +0200 (2011-09-01 ago)
changeset 44643 9987ae55e17b
parent 44614 466ea227e00d
child 44644 317e4962dd0f
permissions -rw-r--r--
amended last_common, if that happens to the very last entry (important to load HOL/Auth, for example);
clarified touch_node: reset result explicitly;
wenzelm@38150
     1
(*  Title:      Pure/PIDE/document.ML
wenzelm@38150
     2
    Author:     Makarius
wenzelm@38150
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@44478
     5
list of commands, with asynchronous read/eval/print processes.
wenzelm@38150
     6
*)
wenzelm@38150
     7
wenzelm@38150
     8
signature DOCUMENT =
wenzelm@38150
     9
sig
wenzelm@38363
    10
  type id = int
wenzelm@38373
    11
  type version_id = id
wenzelm@38363
    12
  type command_id = id
wenzelm@38373
    13
  type exec_id = id
wenzelm@38363
    14
  val no_id: id
wenzelm@38419
    15
  val new_id: unit -> id
wenzelm@38363
    16
  val parse_id: string -> id
wenzelm@38363
    17
  val print_id: id -> string
wenzelm@44185
    18
  type node_header = (string * string list * (string * bool) list) Exn.result
wenzelm@44157
    19
  datatype node_edit =
wenzelm@44185
    20
    Clear |
wenzelm@44157
    21
    Edits of (command_id option * command_id option) list |
wenzelm@44384
    22
    Header of node_header |
wenzelm@44385
    23
    Perspective of command_id list
wenzelm@44156
    24
  type edit = string * node_edit
wenzelm@38418
    25
  type state
wenzelm@38418
    26
  val init_state: state
wenzelm@44441
    27
  val define_command: command_id -> string -> state -> state
wenzelm@44610
    28
  val join_commands: state -> state
wenzelm@44301
    29
  val cancel_execution: state -> Future.task list
wenzelm@44436
    30
  val update_perspective: version_id -> version_id -> string -> command_id list -> state -> state
wenzelm@44481
    31
  val update: version_id -> version_id -> edit list -> state ->
wenzelm@44479
    32
    ((command_id * exec_id option) list * (string * command_id option) list) * state
wenzelm@38418
    33
  val execute: version_id -> state -> state
wenzelm@43713
    34
  val state: unit -> state
wenzelm@43713
    35
  val change_state: (state -> state) -> unit
wenzelm@38150
    36
end;
wenzelm@38150
    37
wenzelm@38150
    38
structure Document: DOCUMENT =
wenzelm@38150
    39
struct
wenzelm@38150
    40
wenzelm@38150
    41
(* unique identifiers *)
wenzelm@38150
    42
wenzelm@38363
    43
type id = int;
wenzelm@38373
    44
type version_id = id;
wenzelm@38363
    45
type command_id = id;
wenzelm@38373
    46
type exec_id = id;
wenzelm@38355
    47
wenzelm@38355
    48
val no_id = 0;
wenzelm@40449
    49
val new_id = Synchronized.counter ();
wenzelm@38418
    50
wenzelm@38414
    51
val parse_id = Markup.parse_int;
wenzelm@38414
    52
val print_id = Markup.print_int;
wenzelm@38150
    53
wenzelm@38418
    54
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
wenzelm@38418
    55
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
wenzelm@38150
    56
wenzelm@38418
    57
wenzelm@38418
    58
wenzelm@38418
    59
(** document structure **)
wenzelm@38418
    60
wenzelm@44185
    61
type node_header = (string * string list * (string * bool) list) Exn.result;
wenzelm@44476
    62
type perspective = (command_id -> bool) * command_id option; (*visible commands, last*)
wenzelm@38448
    63
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    64
wenzelm@43668
    65
abstype node = Node of
wenzelm@44444
    66
 {touched: bool,
wenzelm@44444
    67
  header: node_header,
wenzelm@44386
    68
  perspective: perspective,
wenzelm@44180
    69
  entries: exec_id option Entries.T,  (*command entries with excecutions*)
wenzelm@44476
    70
  last_exec: command_id option,  (*last command with exec state assignment*)
wenzelm@44197
    71
  result: Toplevel.state lazy}
wenzelm@43668
    72
and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    73
with
wenzelm@38418
    74
wenzelm@44476
    75
fun make_node (touched, header, perspective, entries, last_exec, result) =
wenzelm@44444
    76
  Node {touched = touched, header = header, perspective = perspective,
wenzelm@44476
    77
    entries = entries, last_exec = last_exec, result = result};
wenzelm@43668
    78
wenzelm@44476
    79
fun map_node f (Node {touched, header, perspective, entries, last_exec, result}) =
wenzelm@44476
    80
  make_node (f (touched, header, perspective, entries, last_exec, result));
wenzelm@38418
    81
wenzelm@44476
    82
fun make_perspective command_ids : perspective =
wenzelm@44476
    83
  (Inttab.defined (Inttab.make (map (rpair ()) command_ids)), try List.last command_ids);
wenzelm@44441
    84
wenzelm@44476
    85
val no_header = Exn.Exn (ERROR "Bad theory header");
wenzelm@44441
    86
val no_perspective = make_perspective [];
wenzelm@44386
    87
val no_print = Lazy.value ();
wenzelm@44386
    88
val no_result = Lazy.value Toplevel.toplevel;
wenzelm@44386
    89
wenzelm@44476
    90
val empty_node = make_node (false, no_header, no_perspective, Entries.empty, NONE, no_result);
wenzelm@44476
    91
val clear_node = map_node (fn (_, header, _, _, _, _) =>
wenzelm@44476
    92
  (false, header, no_perspective, Entries.empty, NONE, no_result));
wenzelm@44386
    93
wenzelm@44386
    94
wenzelm@44386
    95
(* basic components *)
wenzelm@44386
    96
wenzelm@44446
    97
fun is_touched (Node {touched, ...}) = touched;
wenzelm@44444
    98
fun set_touched touched =
wenzelm@44476
    99
  map_node (fn (_, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   100
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44444
   101
wenzelm@44180
   102
fun get_header (Node {header, ...}) = header;
wenzelm@44385
   103
fun set_header header =
wenzelm@44476
   104
  map_node (fn (touched, _, perspective, entries, last_exec, result) =>
wenzelm@44476
   105
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   106
wenzelm@44385
   107
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
   108
fun set_perspective ids =
wenzelm@44476
   109
  map_node (fn (touched, header, _, entries, last_exec, result) =>
wenzelm@44476
   110
    (touched, header, make_perspective ids, entries, last_exec, result));
wenzelm@44385
   111
wenzelm@44444
   112
fun map_entries f =
wenzelm@44476
   113
  map_node (fn (touched, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   114
    (touched, header, perspective, f entries, last_exec, result));
wenzelm@44643
   115
fun get_entries (Node {entries, ...}) = entries;
wenzelm@44643
   116
fun iterate_entries start f = Entries.iterate start f o get_entries;
wenzelm@43668
   117
wenzelm@44476
   118
fun get_last_exec (Node {last_exec, ...}) = last_exec;
wenzelm@44476
   119
fun set_last_exec last_exec =
wenzelm@44476
   120
  map_node (fn (touched, header, perspective, entries, _, result) =>
wenzelm@44476
   121
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44476
   122
wenzelm@44386
   123
fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force result);
wenzelm@44385
   124
fun set_result result =
wenzelm@44476
   125
  map_node (fn (touched, header, perspective, entries, last_exec, _) =>
wenzelm@44476
   126
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   127
wenzelm@44185
   128
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   129
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   130
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   131
wenzelm@38418
   132
wenzelm@38448
   133
(* node edits and associated executions *)
wenzelm@38150
   134
wenzelm@44157
   135
datatype node_edit =
wenzelm@44185
   136
  Clear |
wenzelm@44157
   137
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   138
  Header of node_header |
wenzelm@44385
   139
  Perspective of command_id list;
wenzelm@44157
   140
wenzelm@44156
   141
type edit = string * node_edit;
wenzelm@38448
   142
wenzelm@44482
   143
fun after_entry (Node {entries, ...}) = Entries.get_after entries;
wenzelm@44479
   144
wenzelm@44480
   145
fun lookup_entry (Node {entries, ...}) id =
wenzelm@44480
   146
  (case Entries.lookup entries id of
wenzelm@44480
   147
    NONE => NONE
wenzelm@44480
   148
  | SOME (exec, _) => exec);
wenzelm@44480
   149
wenzelm@43668
   150
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   151
  (case Entries.lookup entries id of
wenzelm@38448
   152
    NONE => err_undef "command entry" id
wenzelm@44476
   153
  | SOME (exec, _) => exec);
wenzelm@38150
   154
wenzelm@44479
   155
fun the_default_entry node (SOME id) = the_default no_id (the_entry node id)
wenzelm@44479
   156
  | the_default_entry _ NONE = no_id;
wenzelm@44476
   157
wenzelm@44482
   158
fun update_entry id exec =
wenzelm@44482
   159
  map_entries (Entries.update (id, exec));
wenzelm@44482
   160
wenzelm@44482
   161
fun reset_entry id node =
wenzelm@44482
   162
  if is_some (lookup_entry node id) then update_entry id NONE node else node;
wenzelm@38418
   163
wenzelm@38448
   164
fun reset_after id entries =
wenzelm@38448
   165
  (case Entries.get_after entries id of
wenzelm@38448
   166
    NONE => entries
wenzelm@38448
   167
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   168
wenzelm@43668
   169
val edit_node = map_entries o fold
wenzelm@43668
   170
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   171
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   172
wenzelm@38418
   173
wenzelm@38418
   174
(* version operations *)
wenzelm@38418
   175
wenzelm@44185
   176
val empty_version = Version Graph.empty;
wenzelm@44185
   177
wenzelm@38418
   178
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   179
val node_of = get_node o nodes_of;
wenzelm@38418
   180
wenzelm@44444
   181
local
wenzelm@44444
   182
wenzelm@44185
   183
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   184
wenzelm@44444
   185
fun touch_node name nodes =
wenzelm@44444
   186
  fold (fn desc =>
wenzelm@44643
   187
      update_node desc
wenzelm@44643
   188
        (set_touched true #>
wenzelm@44643
   189
          desc <> name ? (map_entries (reset_after NONE) #> set_result no_result)))
wenzelm@44202
   190
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   191
wenzelm@44444
   192
in
wenzelm@44444
   193
wenzelm@44157
   194
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   195
  Version
wenzelm@44436
   196
    (case node_edit of
wenzelm@44436
   197
      Clear =>
wenzelm@44436
   198
        nodes
wenzelm@44436
   199
        |> update_node name clear_node
wenzelm@44444
   200
        |> touch_node name
wenzelm@44436
   201
    | Edits edits =>
wenzelm@44436
   202
        nodes
wenzelm@44436
   203
        |> update_node name (edit_node edits)
wenzelm@44444
   204
        |> touch_node name
wenzelm@44436
   205
    | Header header =>
wenzelm@44436
   206
        let
wenzelm@44436
   207
          val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
wenzelm@44436
   208
          val nodes1 = nodes
wenzelm@44436
   209
            |> default_node name
wenzelm@44436
   210
            |> fold default_node parents;
wenzelm@44436
   211
          val nodes2 = nodes1
wenzelm@44436
   212
            |> Graph.Keys.fold
wenzelm@44436
   213
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@44436
   214
          val (header', nodes3) =
wenzelm@44436
   215
            (header, Graph.add_deps_acyclic (name, parents) nodes2)
wenzelm@44436
   216
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@44436
   217
        in Graph.map_node name (set_header header') nodes3 end
wenzelm@44444
   218
        |> touch_node name
wenzelm@44436
   219
    | Perspective perspective =>
wenzelm@44544
   220
        update_node name (set_perspective perspective) nodes);
wenzelm@38418
   221
wenzelm@44444
   222
end;
wenzelm@44444
   223
wenzelm@44222
   224
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   225
  Version (update_node name (K node) nodes);
wenzelm@38418
   226
wenzelm@38150
   227
end;
wenzelm@38150
   228
wenzelm@38418
   229
wenzelm@38418
   230
wenzelm@38418
   231
(** global state -- document structure and execution process **)
wenzelm@38418
   232
wenzelm@38418
   233
abstype state = State of
wenzelm@40398
   234
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@44610
   235
  commands:
wenzelm@44610
   236
    Toplevel.transition future Inttab.table *  (*command_id -> transition (future parsing)*)
wenzelm@44610
   237
    Toplevel.transition future list,  (*recent commands to be joined*)
wenzelm@44386
   238
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   239
    (*exec_id -> command_id with eval/print process*)
wenzelm@44300
   240
  execution: Future.group}  (*global execution process*)
wenzelm@38418
   241
with
wenzelm@38418
   242
wenzelm@38418
   243
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   244
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   245
wenzelm@38418
   246
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   247
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   248
wenzelm@38418
   249
val init_state =
wenzelm@38418
   250
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@44610
   251
    (Inttab.make [(no_id, Future.value Toplevel.empty)], []),
wenzelm@44386
   252
    Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
wenzelm@44300
   253
    Future.new_group NONE);
wenzelm@38418
   254
wenzelm@38418
   255
wenzelm@38418
   256
(* document versions *)
wenzelm@38418
   257
wenzelm@38418
   258
fun define_version (id: version_id) version =
wenzelm@38418
   259
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   260
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   261
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   262
    in (versions', commands, execs, execution) end);
wenzelm@38418
   263
wenzelm@38418
   264
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   265
  (case Inttab.lookup versions id of
wenzelm@38418
   266
    NONE => err_undef "document version" id
wenzelm@38418
   267
  | SOME version => version);
wenzelm@38418
   268
wenzelm@38418
   269
wenzelm@38418
   270
(* commands *)
wenzelm@38418
   271
wenzelm@38418
   272
fun define_command (id: command_id) text =
wenzelm@44610
   273
  map_state (fn (versions, (commands, recent), execs, execution) =>
wenzelm@38418
   274
    let
wenzelm@38418
   275
      val id_string = print_id id;
wenzelm@44302
   276
      val tr =
wenzelm@44302
   277
        Future.fork_pri 2 (fn () =>
wenzelm@44302
   278
          Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44478
   279
            (fn () => Outer_Syntax.read_command (Position.id id_string) text) ());
wenzelm@38418
   280
      val commands' =
wenzelm@38421
   281
        Inttab.update_new (id, tr) commands
wenzelm@38418
   282
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@44610
   283
    in (versions, (commands', tr :: recent), execs, execution) end);
wenzelm@38418
   284
wenzelm@38418
   285
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@44610
   286
  (case Inttab.lookup (#1 commands) id of
wenzelm@38418
   287
    NONE => err_undef "command" id
wenzelm@38418
   288
  | SOME tr => tr);
wenzelm@38418
   289
wenzelm@44610
   290
val join_commands =
wenzelm@44610
   291
    map_state (fn (versions, (commands, recent), execs, execution) =>
wenzelm@44610
   292
      (Future.join_results recent; (versions, (commands, []), execs, execution)));
wenzelm@38421
   293
wenzelm@38418
   294
wenzelm@38418
   295
(* command executions *)
wenzelm@38418
   296
wenzelm@44197
   297
fun define_exec (exec_id, exec) =
wenzelm@38418
   298
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   299
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   300
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   301
    in (versions, commands, execs', execution) end);
wenzelm@38418
   302
wenzelm@44197
   303
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   304
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   305
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   306
  | SOME exec => exec);
wenzelm@38418
   307
wenzelm@41634
   308
wenzelm@41634
   309
(* document execution *)
wenzelm@41634
   310
wenzelm@44299
   311
fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
wenzelm@41634
   312
wenzelm@38418
   313
end;
wenzelm@38418
   314
wenzelm@38418
   315
wenzelm@38418
   316
wenzelm@38888
   317
(* toplevel transactions *)
wenzelm@38888
   318
wenzelm@38888
   319
local
wenzelm@38888
   320
wenzelm@44440
   321
fun timing tr t =
wenzelm@44440
   322
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   323
wenzelm@38888
   324
fun proof_status tr st =
wenzelm@38888
   325
  (case try Toplevel.proof_of st of
wenzelm@38888
   326
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   327
  | NONE => ());
wenzelm@38888
   328
wenzelm@44386
   329
fun print_state tr st =
wenzelm@44386
   330
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   331
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   332
wenzelm@40390
   333
fun run int tr st =
wenzelm@40390
   334
  (case Toplevel.transition int tr st of
wenzelm@40390
   335
    SOME (st', NONE) => ([], SOME st')
wenzelm@44614
   336
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@44270
   337
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@40390
   338
wenzelm@38888
   339
in
wenzelm@38888
   340
wenzelm@44199
   341
fun run_command tr st =
wenzelm@44185
   342
  let
wenzelm@44199
   343
    val is_init = Toplevel.is_init tr;
wenzelm@44185
   344
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   345
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   346
wenzelm@44614
   347
    val _ = Multithreading.interrupted ();
wenzelm@44611
   348
    val _ = Toplevel.status tr Markup.forked;
wenzelm@44185
   349
    val start = Timing.start ();
wenzelm@44185
   350
    val (errs, result) =
wenzelm@44185
   351
      if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
wenzelm@44185
   352
      else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   353
    val _ = timing tr (Timing.result start);
wenzelm@44611
   354
    val _ = Toplevel.status tr Markup.joined;
wenzelm@44185
   355
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   356
  in
wenzelm@44185
   357
    (case result of
wenzelm@44614
   358
      NONE =>
wenzelm@44614
   359
       (if null errs then Exn.interrupt () else ();
wenzelm@44614
   360
        Toplevel.status tr Markup.failed;
wenzelm@44614
   361
        (st, no_print))
wenzelm@44185
   362
    | SOME st' =>
wenzelm@44185
   363
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   364
        proof_status tr st';
wenzelm@44386
   365
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   366
  end;
wenzelm@38888
   367
wenzelm@38888
   368
end;
wenzelm@38888
   369
wenzelm@38888
   370
wenzelm@38888
   371
wenzelm@38888
   372
wenzelm@44481
   373
(** update **)
wenzelm@38418
   374
wenzelm@44436
   375
(* perspective *)
wenzelm@44436
   376
wenzelm@44436
   377
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   378
  let
wenzelm@44436
   379
    val old_version = the_version state old_id;
wenzelm@44436
   380
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   381
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   382
  in define_version new_id new_version state end;
wenzelm@44436
   383
wenzelm@44436
   384
wenzelm@44481
   385
(* edits *)
wenzelm@38418
   386
wenzelm@38418
   387
local
wenzelm@38418
   388
wenzelm@44482
   389
fun last_common last_visible node0 node =
wenzelm@44482
   390
  let
wenzelm@44544
   391
    fun get_common ((prev, id), exec) (found, (_, visible)) =
wenzelm@44482
   392
      if found then NONE
wenzelm@44482
   393
      else
wenzelm@44482
   394
        let val found' = is_none exec orelse exec <> lookup_entry node0 id
wenzelm@44482
   395
        in SOME (found', (prev, visible andalso prev <> last_visible)) end;
wenzelm@44643
   396
    val (found, (common, visible)) = iterate_entries NONE get_common node (false, (NONE, true));
wenzelm@44643
   397
    val common' = if found then common else Entries.get_after (get_entries node) common;
wenzelm@44643
   398
    val visible' = visible andalso common' <> last_visible;
wenzelm@44643
   399
  in (common', visible') end;
wenzelm@44482
   400
wenzelm@44482
   401
fun new_exec state init command_id' (execs, exec) =
wenzelm@44482
   402
  let
wenzelm@44482
   403
    val command' = the_command state command_id';
wenzelm@44482
   404
    val exec_id' = new_id ();
wenzelm@44482
   405
    val exec' =
wenzelm@44482
   406
      snd exec |> Lazy.map (fn (st, _) =>
wenzelm@44482
   407
        let val tr =
wenzelm@44482
   408
          Future.join command'
wenzelm@44482
   409
          |> Toplevel.modify_init init
wenzelm@44482
   410
          |> Toplevel.put_id (print_id exec_id');
wenzelm@44482
   411
        in run_command tr st end)
wenzelm@44482
   412
      |> pair command_id';
wenzelm@44482
   413
  in ((exec_id', exec') :: execs, exec') end;
wenzelm@44482
   414
wenzelm@44544
   415
fun make_required nodes =
wenzelm@44544
   416
  let
wenzelm@44544
   417
    val all_visible =
wenzelm@44544
   418
      Graph.fold (fn (a, (node, _)) => is_some (#2 (get_perspective node)) ? cons a) nodes []
wenzelm@44544
   419
      |> Graph.all_preds nodes;
wenzelm@44544
   420
    val required =
wenzelm@44544
   421
      fold (fn a => exists (fn b => Graph.is_edge nodes (a, b)) all_visible ? Symtab.update (a, ()))
wenzelm@44544
   422
        all_visible Symtab.empty;
wenzelm@44544
   423
  in Symtab.defined required end;
wenzelm@44544
   424
wenzelm@44441
   425
fun init_theory deps name node =
wenzelm@44435
   426
  let
wenzelm@44435
   427
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   428
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   429
    val (dir, files) =
wenzelm@44435
   430
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   431
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   432
wenzelm@44435
   433
    val parents =
wenzelm@44435
   434
      imports |> map (fn import =>
wenzelm@44441
   435
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   436
          SOME thy => thy
wenzelm@44435
   437
        | NONE =>
wenzelm@44435
   438
            get_theory (Position.file_only import)
wenzelm@44482
   439
              (snd (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   440
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   441
wenzelm@38418
   442
in
wenzelm@38418
   443
wenzelm@44481
   444
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   445
  let
wenzelm@38418
   446
    val old_version = the_version state old_id;
wenzelm@44180
   447
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   448
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   449
wenzelm@44544
   450
    val nodes = nodes_of new_version;
wenzelm@44544
   451
    val is_required = make_required nodes;
wenzelm@44544
   452
wenzelm@44476
   453
    val updated =
wenzelm@44544
   454
      nodes |> Graph.schedule
wenzelm@44199
   455
        (fn deps => fn (name, node) =>
wenzelm@44544
   456
          if not (is_touched node orelse is_required name)
wenzelm@44544
   457
          then Future.value (([], [], []), node)
wenzelm@44444
   458
          else
wenzelm@44479
   459
            let
wenzelm@44479
   460
              val node0 = node_of old_version name;
wenzelm@44479
   461
              fun init () = init_theory deps name node;
wenzelm@44479
   462
            in
wenzelm@44482
   463
              (singleton o Future.forks)
wenzelm@44482
   464
                {name = "Document.update", group = NONE,
wenzelm@44482
   465
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44482
   466
                (fn () =>
wenzelm@44482
   467
                  let
wenzelm@44544
   468
                    val required = is_required name;
wenzelm@44482
   469
                    val last_visible = #2 (get_perspective node);
wenzelm@44482
   470
                    val (common, visible) = last_common last_visible node0 node;
wenzelm@44482
   471
                    val common_exec = the_exec state (the_default_entry node common);
wenzelm@44479
   472
wenzelm@44482
   473
                    val (execs, exec) = ([], common_exec) |>
wenzelm@44544
   474
                      (visible orelse required) ?
wenzelm@44483
   475
                        iterate_entries (after_entry node common)
wenzelm@44482
   476
                          (fn ((prev, id), _) => fn res =>
wenzelm@44544
   477
                            if not required andalso prev = last_visible then NONE
wenzelm@44482
   478
                            else SOME (new_exec state init id res)) node;
wenzelm@44479
   479
wenzelm@44482
   480
                    val no_execs =
wenzelm@44483
   481
                      iterate_entries (after_entry node0 common)
wenzelm@44482
   482
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   483
                          if is_none exec0 then NONE
wenzelm@44482
   484
                          else if exists (fn (_, (id, _)) => id0 = id) execs then SOME res
wenzelm@44482
   485
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   486
wenzelm@44482
   487
                    val last_exec = if #1 exec = no_id then NONE else SOME (#1 exec);
wenzelm@44482
   488
                    val result =
wenzelm@44482
   489
                      if is_some (after_entry node last_exec) then no_result
wenzelm@44482
   490
                      else Lazy.map #1 (#2 exec);
wenzelm@44480
   491
wenzelm@44482
   492
                    val node' = node
wenzelm@44482
   493
                      |> fold reset_entry no_execs
wenzelm@44482
   494
                      |> fold (fn (exec_id, (id, _)) => update_entry id (SOME exec_id)) execs
wenzelm@44482
   495
                      |> set_last_exec last_exec
wenzelm@44482
   496
                      |> set_result result
wenzelm@44482
   497
                      |> set_touched false;
wenzelm@44482
   498
                  in ((no_execs, execs, [(name, node')]), node') end)
wenzelm@44479
   499
            end)
wenzelm@44479
   500
      |> Future.joins |> map #1;
wenzelm@44476
   501
wenzelm@44479
   502
    val command_execs =
wenzelm@44479
   503
      map (rpair NONE) (maps #1 updated) @
wenzelm@44479
   504
      map (fn (exec_id, (command_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   505
    val updated_nodes = maps #3 updated;
wenzelm@44476
   506
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   507
wenzelm@44197
   508
    val state' = state
wenzelm@44479
   509
      |> fold (fold define_exec o #2) updated
wenzelm@44476
   510
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   511
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   512
wenzelm@38418
   513
end;
wenzelm@38418
   514
wenzelm@38418
   515
wenzelm@38418
   516
(* execute *)
wenzelm@38418
   517
wenzelm@38418
   518
fun execute version_id state =
wenzelm@41673
   519
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   520
    let
wenzelm@38418
   521
      val version = the_version state version_id;
wenzelm@38418
   522
wenzelm@44386
   523
      fun force_exec _ NONE = ()
wenzelm@44386
   524
        | force_exec node (SOME exec_id) =
wenzelm@44386
   525
            let
wenzelm@44386
   526
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   527
              val (_, print) = Lazy.force exec;
wenzelm@44386
   528
              val _ =
wenzelm@44439
   529
                if #1 (get_perspective node) command_id
wenzelm@44386
   530
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   531
                else ();
wenzelm@44386
   532
            in () end;
wenzelm@38418
   533
wenzelm@44300
   534
      val execution = Future.new_group NONE;
wenzelm@44299
   535
      val _ =
wenzelm@44201
   536
        nodes_of version |> Graph.schedule
wenzelm@44201
   537
          (fn deps => fn (name, node) =>
wenzelm@44302
   538
            (singleton o Future.forks)
wenzelm@44302
   539
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
wenzelm@44614
   540
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = false}
wenzelm@44483
   541
              (iterate_entries NONE (fn (_, exec) => fn () => SOME (force_exec node exec)) node));
wenzelm@40520
   542
wenzelm@44299
   543
    in (versions, commands, execs, execution) end);
wenzelm@38418
   544
wenzelm@43713
   545
wenzelm@43713
   546
wenzelm@43713
   547
(** global state **)
wenzelm@43713
   548
wenzelm@43713
   549
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   550
wenzelm@43713
   551
fun state () = Synchronized.value global_state;
wenzelm@43713
   552
val change_state = Synchronized.change global_state;
wenzelm@43713
   553
wenzelm@38418
   554
end;
wenzelm@38418
   555