src/Pure/PIDE/document.ML
author wenzelm
Fri Aug 26 21:27:58 2011 +0200 (2011-08-26)
changeset 44483 383a5b9efbd0
parent 44482 c7225307acf2
child 44544 da5f0af32c1b
permissions -rw-r--r--
tuned signature -- iterate subsumes both fold and get_first;
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@44197
    28
  val join_commands: state -> unit
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@44483
   115
fun iterate_entries start f (Node {entries, ...}) = Entries.iterate start f entries;
wenzelm@43668
   116
wenzelm@44476
   117
fun get_last_exec (Node {last_exec, ...}) = last_exec;
wenzelm@44476
   118
fun set_last_exec last_exec =
wenzelm@44476
   119
  map_node (fn (touched, header, perspective, entries, _, result) =>
wenzelm@44476
   120
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44476
   121
wenzelm@44386
   122
fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force result);
wenzelm@44385
   123
fun set_result result =
wenzelm@44476
   124
  map_node (fn (touched, header, perspective, entries, last_exec, _) =>
wenzelm@44476
   125
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   126
wenzelm@44185
   127
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   128
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   129
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   130
wenzelm@38418
   131
wenzelm@38448
   132
(* node edits and associated executions *)
wenzelm@38150
   133
wenzelm@44157
   134
datatype node_edit =
wenzelm@44185
   135
  Clear |
wenzelm@44157
   136
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   137
  Header of node_header |
wenzelm@44385
   138
  Perspective of command_id list;
wenzelm@44157
   139
wenzelm@44156
   140
type edit = string * node_edit;
wenzelm@38448
   141
wenzelm@44482
   142
fun after_entry (Node {entries, ...}) = Entries.get_after entries;
wenzelm@44479
   143
wenzelm@44480
   144
fun lookup_entry (Node {entries, ...}) id =
wenzelm@44480
   145
  (case Entries.lookup entries id of
wenzelm@44480
   146
    NONE => NONE
wenzelm@44480
   147
  | SOME (exec, _) => exec);
wenzelm@44480
   148
wenzelm@43668
   149
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   150
  (case Entries.lookup entries id of
wenzelm@38448
   151
    NONE => err_undef "command entry" id
wenzelm@44476
   152
  | SOME (exec, _) => exec);
wenzelm@38150
   153
wenzelm@44479
   154
fun the_default_entry node (SOME id) = the_default no_id (the_entry node id)
wenzelm@44479
   155
  | the_default_entry _ NONE = no_id;
wenzelm@44476
   156
wenzelm@44482
   157
fun update_entry id exec =
wenzelm@44482
   158
  map_entries (Entries.update (id, exec));
wenzelm@44482
   159
wenzelm@44482
   160
fun reset_entry id node =
wenzelm@44482
   161
  if is_some (lookup_entry node id) then update_entry id NONE node else node;
wenzelm@38418
   162
wenzelm@38448
   163
fun reset_after id entries =
wenzelm@38448
   164
  (case Entries.get_after entries id of
wenzelm@38448
   165
    NONE => entries
wenzelm@38448
   166
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   167
wenzelm@43668
   168
val edit_node = map_entries o fold
wenzelm@43668
   169
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   170
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   171
wenzelm@38418
   172
wenzelm@38418
   173
(* version operations *)
wenzelm@38418
   174
wenzelm@44185
   175
val empty_version = Version Graph.empty;
wenzelm@44185
   176
wenzelm@38418
   177
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   178
val node_of = get_node o nodes_of;
wenzelm@38418
   179
wenzelm@44444
   180
local
wenzelm@44444
   181
wenzelm@44185
   182
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   183
wenzelm@44444
   184
fun touch_node name nodes =
wenzelm@44444
   185
  fold (fn desc =>
wenzelm@44444
   186
      update_node desc (set_touched true) #>
wenzelm@44444
   187
      desc <> name ? update_node desc (map_entries (reset_after NONE)))
wenzelm@44202
   188
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   189
wenzelm@44444
   190
in
wenzelm@44444
   191
wenzelm@44157
   192
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   193
  Version
wenzelm@44436
   194
    (case node_edit of
wenzelm@44436
   195
      Clear =>
wenzelm@44436
   196
        nodes
wenzelm@44436
   197
        |> update_node name clear_node
wenzelm@44444
   198
        |> touch_node name
wenzelm@44436
   199
    | Edits edits =>
wenzelm@44436
   200
        nodes
wenzelm@44436
   201
        |> update_node name (edit_node edits)
wenzelm@44444
   202
        |> touch_node name
wenzelm@44436
   203
    | Header header =>
wenzelm@44436
   204
        let
wenzelm@44436
   205
          val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
wenzelm@44436
   206
          val nodes1 = nodes
wenzelm@44436
   207
            |> default_node name
wenzelm@44436
   208
            |> fold default_node parents;
wenzelm@44436
   209
          val nodes2 = nodes1
wenzelm@44436
   210
            |> Graph.Keys.fold
wenzelm@44436
   211
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@44436
   212
          val (header', nodes3) =
wenzelm@44436
   213
            (header, Graph.add_deps_acyclic (name, parents) nodes2)
wenzelm@44436
   214
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@44436
   215
        in Graph.map_node name (set_header header') nodes3 end
wenzelm@44444
   216
        |> touch_node name
wenzelm@44436
   217
    | Perspective perspective =>
wenzelm@44479
   218
        nodes
wenzelm@44479
   219
        |> update_node name (set_perspective perspective)
wenzelm@44479
   220
        |> touch_node name (* FIXME more precise!?! *));
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@38421
   235
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@44386
   236
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   237
    (*exec_id -> command_id with eval/print process*)
wenzelm@44300
   238
  execution: Future.group}  (*global execution process*)
wenzelm@38418
   239
with
wenzelm@38418
   240
wenzelm@38418
   241
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   242
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   243
wenzelm@38418
   244
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   245
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   246
wenzelm@38418
   247
val init_state =
wenzelm@38418
   248
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38449
   249
    Inttab.make [(no_id, Future.value Toplevel.empty)],
wenzelm@44386
   250
    Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
wenzelm@44300
   251
    Future.new_group NONE);
wenzelm@38418
   252
wenzelm@38418
   253
wenzelm@38418
   254
(* document versions *)
wenzelm@38418
   255
wenzelm@38418
   256
fun define_version (id: version_id) version =
wenzelm@38418
   257
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   258
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   259
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   260
    in (versions', commands, execs, execution) end);
wenzelm@38418
   261
wenzelm@38418
   262
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   263
  (case Inttab.lookup versions id of
wenzelm@38418
   264
    NONE => err_undef "document version" id
wenzelm@38418
   265
  | SOME version => version);
wenzelm@38418
   266
wenzelm@38418
   267
wenzelm@38418
   268
(* commands *)
wenzelm@38418
   269
wenzelm@38418
   270
fun define_command (id: command_id) text =
wenzelm@38418
   271
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   272
    let
wenzelm@38418
   273
      val id_string = print_id id;
wenzelm@44302
   274
      val tr =
wenzelm@44302
   275
        Future.fork_pri 2 (fn () =>
wenzelm@44302
   276
          Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44478
   277
            (fn () => Outer_Syntax.read_command (Position.id id_string) text) ());
wenzelm@38418
   278
      val commands' =
wenzelm@38421
   279
        Inttab.update_new (id, tr) commands
wenzelm@38418
   280
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   281
    in (versions, commands', execs, execution) end);
wenzelm@38418
   282
wenzelm@38418
   283
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   284
  (case Inttab.lookup commands id of
wenzelm@38418
   285
    NONE => err_undef "command" id
wenzelm@38418
   286
  | SOME tr => tr);
wenzelm@38418
   287
wenzelm@38421
   288
fun join_commands (State {commands, ...}) =
wenzelm@38421
   289
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   290
wenzelm@38418
   291
wenzelm@38418
   292
(* command executions *)
wenzelm@38418
   293
wenzelm@44197
   294
fun define_exec (exec_id, exec) =
wenzelm@38418
   295
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   296
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   297
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   298
    in (versions, commands, execs', execution) end);
wenzelm@38418
   299
wenzelm@44197
   300
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   301
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   302
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   303
  | SOME exec => exec);
wenzelm@38418
   304
wenzelm@41634
   305
wenzelm@41634
   306
(* document execution *)
wenzelm@41634
   307
wenzelm@44299
   308
fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
wenzelm@41634
   309
wenzelm@38418
   310
end;
wenzelm@38418
   311
wenzelm@38418
   312
wenzelm@38418
   313
wenzelm@38888
   314
(* toplevel transactions *)
wenzelm@38888
   315
wenzelm@38888
   316
local
wenzelm@38888
   317
wenzelm@44440
   318
fun timing tr t =
wenzelm@44440
   319
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   320
wenzelm@38888
   321
fun proof_status tr st =
wenzelm@38888
   322
  (case try Toplevel.proof_of st of
wenzelm@38888
   323
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   324
  | NONE => ());
wenzelm@38888
   325
wenzelm@44386
   326
fun print_state tr st =
wenzelm@44386
   327
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   328
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   329
wenzelm@40390
   330
fun run int tr st =
wenzelm@40390
   331
  (case Toplevel.transition int tr st of
wenzelm@40390
   332
    SOME (st', NONE) => ([], SOME st')
wenzelm@44271
   333
  | SOME (_, SOME (exn, _)) =>
wenzelm@44271
   334
      (case ML_Compiler.exn_messages exn of
wenzelm@40390
   335
        [] => Exn.interrupt ()
wenzelm@40390
   336
      | errs => (errs, 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@44185
   347
    val start = Timing.start ();
wenzelm@44185
   348
    val (errs, result) =
wenzelm@44185
   349
      if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
wenzelm@44185
   350
      else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   351
    val _ = timing tr (Timing.result start);
wenzelm@44185
   352
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   353
  in
wenzelm@44185
   354
    (case result of
wenzelm@44386
   355
      NONE => (Toplevel.status tr Markup.failed; (st, no_print))
wenzelm@44185
   356
    | SOME st' =>
wenzelm@44185
   357
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   358
        proof_status tr st';
wenzelm@44386
   359
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   360
  end;
wenzelm@38888
   361
wenzelm@38888
   362
end;
wenzelm@38888
   363
wenzelm@38888
   364
wenzelm@38888
   365
wenzelm@38888
   366
wenzelm@44481
   367
(** update **)
wenzelm@38418
   368
wenzelm@44436
   369
(* perspective *)
wenzelm@44436
   370
wenzelm@44436
   371
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   372
  let
wenzelm@44436
   373
    val old_version = the_version state old_id;
wenzelm@44436
   374
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   375
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   376
  in define_version new_id new_version state end;
wenzelm@44436
   377
wenzelm@44436
   378
wenzelm@44481
   379
(* edits *)
wenzelm@38418
   380
wenzelm@38418
   381
local
wenzelm@38418
   382
wenzelm@44482
   383
fun last_common last_visible node0 node =
wenzelm@44482
   384
  let
wenzelm@44482
   385
    fun get_common ((prev, id), exec) (found, (result, visible)) =
wenzelm@44482
   386
      if found then NONE
wenzelm@44482
   387
      else
wenzelm@44482
   388
        let val found' = is_none exec orelse exec <> lookup_entry node0 id
wenzelm@44482
   389
        in SOME (found', (prev, visible andalso prev <> last_visible)) end;
wenzelm@44483
   390
  in #2 (iterate_entries NONE get_common node (false, (NONE, true))) end;
wenzelm@44482
   391
wenzelm@44482
   392
fun new_exec state init command_id' (execs, exec) =
wenzelm@44482
   393
  let
wenzelm@44482
   394
    val command' = the_command state command_id';
wenzelm@44482
   395
    val exec_id' = new_id ();
wenzelm@44482
   396
    val exec' =
wenzelm@44482
   397
      snd exec |> Lazy.map (fn (st, _) =>
wenzelm@44482
   398
        let val tr =
wenzelm@44482
   399
          Future.join command'
wenzelm@44482
   400
          |> Toplevel.modify_init init
wenzelm@44482
   401
          |> Toplevel.put_id (print_id exec_id');
wenzelm@44482
   402
        in run_command tr st end)
wenzelm@44482
   403
      |> pair command_id';
wenzelm@44482
   404
  in ((exec_id', exec') :: execs, exec') end;
wenzelm@44482
   405
wenzelm@44441
   406
fun init_theory deps name node =
wenzelm@44435
   407
  let
wenzelm@44435
   408
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   409
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   410
    val (dir, files) =
wenzelm@44435
   411
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   412
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   413
wenzelm@44435
   414
    val parents =
wenzelm@44435
   415
      imports |> map (fn import =>
wenzelm@44441
   416
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   417
          SOME thy => thy
wenzelm@44435
   418
        | NONE =>
wenzelm@44435
   419
            get_theory (Position.file_only import)
wenzelm@44482
   420
              (snd (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   421
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   422
wenzelm@38418
   423
in
wenzelm@38418
   424
wenzelm@44481
   425
fun update (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   426
  let
wenzelm@38418
   427
    val old_version = the_version state old_id;
wenzelm@44180
   428
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   429
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   430
wenzelm@44476
   431
    val updated =
wenzelm@44197
   432
      nodes_of new_version |> Graph.schedule
wenzelm@44199
   433
        (fn deps => fn (name, node) =>
wenzelm@44479
   434
          if not (is_touched node) then Future.value (([], [], []), node)
wenzelm@44444
   435
          else
wenzelm@44479
   436
            let
wenzelm@44479
   437
              val node0 = node_of old_version name;
wenzelm@44479
   438
              fun init () = init_theory deps name node;
wenzelm@44479
   439
            in
wenzelm@44482
   440
              (singleton o Future.forks)
wenzelm@44482
   441
                {name = "Document.update", group = NONE,
wenzelm@44482
   442
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44482
   443
                (fn () =>
wenzelm@44482
   444
                  let
wenzelm@44482
   445
                    val last_visible = #2 (get_perspective node);
wenzelm@44482
   446
                    val (common, visible) = last_common last_visible node0 node;
wenzelm@44482
   447
                    val common_exec = the_exec state (the_default_entry node common);
wenzelm@44479
   448
wenzelm@44482
   449
                    val (execs, exec) = ([], common_exec) |>
wenzelm@44482
   450
                      visible ?
wenzelm@44483
   451
                        iterate_entries (after_entry node common)
wenzelm@44482
   452
                          (fn ((prev, id), _) => fn res =>
wenzelm@44482
   453
                            if prev = last_visible then NONE
wenzelm@44482
   454
                            else SOME (new_exec state init id res)) node;
wenzelm@44479
   455
wenzelm@44482
   456
                    val no_execs =
wenzelm@44483
   457
                      iterate_entries (after_entry node0 common)
wenzelm@44482
   458
                        (fn ((_, id0), exec0) => fn res =>
wenzelm@44482
   459
                          if is_none exec0 then NONE
wenzelm@44482
   460
                          else if exists (fn (_, (id, _)) => id0 = id) execs then SOME res
wenzelm@44482
   461
                          else SOME (id0 :: res)) node0 [];
wenzelm@44480
   462
wenzelm@44482
   463
                    val last_exec = if #1 exec = no_id then NONE else SOME (#1 exec);
wenzelm@44482
   464
                    val result =
wenzelm@44482
   465
                      if is_some (after_entry node last_exec) then no_result
wenzelm@44482
   466
                      else Lazy.map #1 (#2 exec);
wenzelm@44480
   467
wenzelm@44482
   468
                    val node' = node
wenzelm@44482
   469
                      |> fold reset_entry no_execs
wenzelm@44482
   470
                      |> fold (fn (exec_id, (id, _)) => update_entry id (SOME exec_id)) execs
wenzelm@44482
   471
                      |> set_last_exec last_exec
wenzelm@44482
   472
                      |> set_result result
wenzelm@44482
   473
                      |> set_touched false;
wenzelm@44482
   474
                  in ((no_execs, execs, [(name, node')]), node') end)
wenzelm@44479
   475
            end)
wenzelm@44479
   476
      |> Future.joins |> map #1;
wenzelm@44476
   477
wenzelm@44479
   478
    val command_execs =
wenzelm@44479
   479
      map (rpair NONE) (maps #1 updated) @
wenzelm@44479
   480
      map (fn (exec_id, (command_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   481
    val updated_nodes = maps #3 updated;
wenzelm@44476
   482
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   483
wenzelm@44197
   484
    val state' = state
wenzelm@44479
   485
      |> fold (fold define_exec o #2) updated
wenzelm@44476
   486
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   487
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   488
wenzelm@38418
   489
end;
wenzelm@38418
   490
wenzelm@38418
   491
wenzelm@38418
   492
(* execute *)
wenzelm@38418
   493
wenzelm@38418
   494
fun execute version_id state =
wenzelm@41673
   495
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   496
    let
wenzelm@38418
   497
      val version = the_version state version_id;
wenzelm@38418
   498
wenzelm@44386
   499
      fun force_exec _ NONE = ()
wenzelm@44386
   500
        | force_exec node (SOME exec_id) =
wenzelm@44386
   501
            let
wenzelm@44386
   502
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   503
              val (_, print) = Lazy.force exec;
wenzelm@44386
   504
              val _ =
wenzelm@44439
   505
                if #1 (get_perspective node) command_id
wenzelm@44386
   506
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   507
                else ();
wenzelm@44386
   508
            in () end;
wenzelm@38418
   509
wenzelm@44300
   510
      val execution = Future.new_group NONE;
wenzelm@44299
   511
      val _ =
wenzelm@44201
   512
        nodes_of version |> Graph.schedule
wenzelm@44201
   513
          (fn deps => fn (name, node) =>
wenzelm@44302
   514
            (singleton o Future.forks)
wenzelm@44302
   515
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
wenzelm@44302
   516
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = true}
wenzelm@44483
   517
              (iterate_entries NONE (fn (_, exec) => fn () => SOME (force_exec node exec)) node));
wenzelm@40520
   518
wenzelm@44299
   519
    in (versions, commands, execs, execution) end);
wenzelm@38418
   520
wenzelm@43713
   521
wenzelm@43713
   522
wenzelm@43713
   523
(** global state **)
wenzelm@43713
   524
wenzelm@43713
   525
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   526
wenzelm@43713
   527
fun state () = Synchronized.value global_state;
wenzelm@43713
   528
val change_state = Synchronized.change global_state;
wenzelm@43713
   529
wenzelm@38418
   530
end;
wenzelm@38418
   531