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