src/Pure/PIDE/document.ML
author wenzelm
Wed Aug 24 17:16:48 2011 +0200 (2011-08-24 ago)
changeset 44445 364fd07398f5
parent 44444 33a5616a7571
child 44446 f9334afb6945
permissions -rw-r--r--
tuned pri: prefer purging of canceled execution;
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@38418
     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@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@44157
    31
  val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
wenzelm@38418
    32
  val execute: version_id -> state -> state
wenzelm@43713
    33
  val state: unit -> state
wenzelm@43713
    34
  val change_state: (state -> state) -> unit
wenzelm@38150
    35
end;
wenzelm@38150
    36
wenzelm@38150
    37
structure Document: DOCUMENT =
wenzelm@38150
    38
struct
wenzelm@38150
    39
wenzelm@38150
    40
(* unique identifiers *)
wenzelm@38150
    41
wenzelm@38363
    42
type id = int;
wenzelm@38373
    43
type version_id = id;
wenzelm@38363
    44
type command_id = id;
wenzelm@38373
    45
type exec_id = id;
wenzelm@38355
    46
wenzelm@38355
    47
val no_id = 0;
wenzelm@40449
    48
val new_id = Synchronized.counter ();
wenzelm@38418
    49
wenzelm@38414
    50
val parse_id = Markup.parse_int;
wenzelm@38414
    51
val print_id = Markup.print_int;
wenzelm@38150
    52
wenzelm@38418
    53
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
wenzelm@38418
    54
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
wenzelm@38150
    55
wenzelm@38418
    56
wenzelm@38418
    57
wenzelm@38418
    58
(** document structure **)
wenzelm@38418
    59
wenzelm@44185
    60
type node_header = (string * string list * (string * bool) list) Exn.result;
wenzelm@44386
    61
type perspective = (command_id -> bool) * command_id list; (*visible commands, canonical order*)
wenzelm@38448
    62
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    63
wenzelm@43668
    64
abstype node = Node of
wenzelm@44444
    65
 {touched: bool,
wenzelm@44444
    66
  header: node_header,
wenzelm@44386
    67
  perspective: perspective,
wenzelm@44180
    68
  entries: exec_id option Entries.T,  (*command entries with excecutions*)
wenzelm@44197
    69
  result: Toplevel.state lazy}
wenzelm@43668
    70
and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    71
with
wenzelm@38418
    72
wenzelm@44444
    73
fun make_node (touched, header, perspective, entries, result) =
wenzelm@44444
    74
  Node {touched = touched, header = header, perspective = perspective,
wenzelm@44444
    75
    entries = entries, result = result};
wenzelm@43668
    76
wenzelm@44444
    77
fun map_node f (Node {touched, header, perspective, entries, result}) =
wenzelm@44444
    78
  make_node (f (touched, header, perspective, entries, result));
wenzelm@38418
    79
wenzelm@44441
    80
fun make_perspective ids : perspective =
wenzelm@44441
    81
  (Inttab.defined (Inttab.make (map (rpair ()) ids)), ids);
wenzelm@44441
    82
wenzelm@44441
    83
val no_perspective = make_perspective [];
wenzelm@44386
    84
val no_print = Lazy.value ();
wenzelm@44386
    85
val no_result = Lazy.value Toplevel.toplevel;
wenzelm@44386
    86
wenzelm@44386
    87
val empty_node =
wenzelm@44444
    88
  make_node (false, Exn.Exn (ERROR "Bad theory header"), no_perspective, Entries.empty, no_result);
wenzelm@44386
    89
wenzelm@44386
    90
val clear_node =
wenzelm@44444
    91
  map_node (fn (_, header, _, _, _) => (false, header, no_perspective, Entries.empty, no_result));
wenzelm@44386
    92
wenzelm@44386
    93
wenzelm@44386
    94
(* basic components *)
wenzelm@44386
    95
wenzelm@44444
    96
fun get_touched (Node {touched, ...}) = touched;
wenzelm@44444
    97
fun set_touched touched =
wenzelm@44444
    98
  map_node (fn (_, header, perspective, entries, result) =>
wenzelm@44444
    99
    (touched, header, perspective, entries, result));
wenzelm@44444
   100
wenzelm@44180
   101
fun get_header (Node {header, ...}) = header;
wenzelm@44385
   102
fun set_header header =
wenzelm@44444
   103
  map_node (fn (touched, _, perspective, entries, result) =>
wenzelm@44444
   104
    (touched, header, perspective, entries, result));
wenzelm@44180
   105
wenzelm@44385
   106
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
   107
fun set_perspective ids =
wenzelm@44444
   108
  map_node (fn (touched, header, _, entries, result) =>
wenzelm@44444
   109
    (touched, header, make_perspective ids, entries, result));
wenzelm@44385
   110
wenzelm@44444
   111
fun map_entries f =
wenzelm@44444
   112
  map_node (fn (touched, header, perspective, entries, result) =>
wenzelm@44444
   113
    (touched, header, perspective, f entries, result));
wenzelm@43668
   114
fun fold_entries start f (Node {entries, ...}) = Entries.fold start f entries;
wenzelm@43668
   115
fun first_entry start P (Node {entries, ...}) = Entries.get_first start P entries;
wenzelm@43668
   116
wenzelm@44386
   117
fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force result);
wenzelm@44385
   118
fun set_result result =
wenzelm@44444
   119
  map_node (fn (touched, header, perspective, entries, _) =>
wenzelm@44444
   120
    (touched, header, perspective, entries, result));
wenzelm@44180
   121
wenzelm@44185
   122
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   123
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   124
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   125
wenzelm@38418
   126
wenzelm@38448
   127
(* node edits and associated executions *)
wenzelm@38150
   128
wenzelm@44157
   129
datatype node_edit =
wenzelm@44185
   130
  Clear |
wenzelm@44157
   131
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   132
  Header of node_header |
wenzelm@44385
   133
  Perspective of command_id list;
wenzelm@44157
   134
wenzelm@44156
   135
type edit = string * node_edit;
wenzelm@38448
   136
wenzelm@43668
   137
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   138
  (case Entries.lookup entries id of
wenzelm@38448
   139
    NONE => err_undef "command entry" id
wenzelm@38448
   140
  | SOME entry => entry);
wenzelm@38150
   141
wenzelm@43668
   142
fun update_entry (id, exec_id) =
wenzelm@43668
   143
  map_entries (Entries.update (id, SOME exec_id));
wenzelm@38418
   144
wenzelm@38448
   145
fun reset_after id entries =
wenzelm@38448
   146
  (case Entries.get_after entries id of
wenzelm@38448
   147
    NONE => entries
wenzelm@38448
   148
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   149
wenzelm@43668
   150
val edit_node = map_entries o fold
wenzelm@43668
   151
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   152
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   153
wenzelm@38418
   154
wenzelm@38418
   155
(* version operations *)
wenzelm@38418
   156
wenzelm@44185
   157
val empty_version = Version Graph.empty;
wenzelm@44185
   158
wenzelm@38418
   159
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   160
val node_of = get_node o nodes_of;
wenzelm@38418
   161
wenzelm@44444
   162
local
wenzelm@44444
   163
wenzelm@44185
   164
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   165
wenzelm@44444
   166
fun touch_node name nodes =
wenzelm@44444
   167
  fold (fn desc =>
wenzelm@44444
   168
      update_node desc (set_touched true) #>
wenzelm@44444
   169
      desc <> name ? update_node desc (map_entries (reset_after NONE)))
wenzelm@44202
   170
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   171
wenzelm@44444
   172
in
wenzelm@44444
   173
wenzelm@44157
   174
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   175
  Version
wenzelm@44436
   176
    (case node_edit of
wenzelm@44436
   177
      Clear =>
wenzelm@44436
   178
        nodes
wenzelm@44436
   179
        |> update_node name clear_node
wenzelm@44444
   180
        |> touch_node name
wenzelm@44436
   181
    | Edits edits =>
wenzelm@44436
   182
        nodes
wenzelm@44436
   183
        |> update_node name (edit_node edits)
wenzelm@44444
   184
        |> touch_node name
wenzelm@44436
   185
    | Header header =>
wenzelm@44436
   186
        let
wenzelm@44436
   187
          val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
wenzelm@44436
   188
          val nodes1 = nodes
wenzelm@44436
   189
            |> default_node name
wenzelm@44436
   190
            |> fold default_node parents;
wenzelm@44436
   191
          val nodes2 = nodes1
wenzelm@44436
   192
            |> Graph.Keys.fold
wenzelm@44436
   193
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@44436
   194
          val (header', nodes3) =
wenzelm@44436
   195
            (header, Graph.add_deps_acyclic (name, parents) nodes2)
wenzelm@44436
   196
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@44436
   197
        in Graph.map_node name (set_header header') nodes3 end
wenzelm@44444
   198
        |> touch_node name
wenzelm@44436
   199
    | Perspective perspective =>
wenzelm@44436
   200
        update_node name (set_perspective perspective) nodes);
wenzelm@38418
   201
wenzelm@44444
   202
end;
wenzelm@44444
   203
wenzelm@44222
   204
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   205
  Version (update_node name (K node) nodes);
wenzelm@38418
   206
wenzelm@38150
   207
end;
wenzelm@38150
   208
wenzelm@38418
   209
wenzelm@38418
   210
wenzelm@38418
   211
(** global state -- document structure and execution process **)
wenzelm@38418
   212
wenzelm@38418
   213
abstype state = State of
wenzelm@40398
   214
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@38421
   215
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@44386
   216
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   217
    (*exec_id -> command_id with eval/print process*)
wenzelm@44300
   218
  execution: Future.group}  (*global execution process*)
wenzelm@38418
   219
with
wenzelm@38418
   220
wenzelm@38418
   221
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   222
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   223
wenzelm@38418
   224
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   225
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   226
wenzelm@38418
   227
val init_state =
wenzelm@38418
   228
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38449
   229
    Inttab.make [(no_id, Future.value Toplevel.empty)],
wenzelm@44386
   230
    Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
wenzelm@44300
   231
    Future.new_group NONE);
wenzelm@38418
   232
wenzelm@38418
   233
wenzelm@38418
   234
(* document versions *)
wenzelm@38418
   235
wenzelm@38418
   236
fun define_version (id: version_id) version =
wenzelm@38418
   237
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   238
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   239
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   240
    in (versions', commands, execs, execution) end);
wenzelm@38418
   241
wenzelm@38418
   242
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   243
  (case Inttab.lookup versions id of
wenzelm@38418
   244
    NONE => err_undef "document version" id
wenzelm@38418
   245
  | SOME version => version);
wenzelm@38418
   246
wenzelm@38418
   247
wenzelm@38418
   248
(* commands *)
wenzelm@38418
   249
wenzelm@38418
   250
fun define_command (id: command_id) text =
wenzelm@38418
   251
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   252
    let
wenzelm@38418
   253
      val id_string = print_id id;
wenzelm@44302
   254
      val tr =
wenzelm@44302
   255
        Future.fork_pri 2 (fn () =>
wenzelm@44302
   256
          Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44302
   257
            (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
wenzelm@38418
   258
      val commands' =
wenzelm@38421
   259
        Inttab.update_new (id, tr) commands
wenzelm@38418
   260
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   261
    in (versions, commands', execs, execution) end);
wenzelm@38418
   262
wenzelm@38418
   263
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   264
  (case Inttab.lookup commands id of
wenzelm@38418
   265
    NONE => err_undef "command" id
wenzelm@38418
   266
  | SOME tr => tr);
wenzelm@38418
   267
wenzelm@38421
   268
fun join_commands (State {commands, ...}) =
wenzelm@38421
   269
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   270
wenzelm@38418
   271
wenzelm@38418
   272
(* command executions *)
wenzelm@38418
   273
wenzelm@44197
   274
fun define_exec (exec_id, exec) =
wenzelm@38418
   275
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   276
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   277
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   278
    in (versions, commands, execs', execution) end);
wenzelm@38418
   279
wenzelm@44197
   280
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   281
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   282
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   283
  | SOME exec => exec);
wenzelm@38418
   284
wenzelm@41634
   285
wenzelm@41634
   286
(* document execution *)
wenzelm@41634
   287
wenzelm@44299
   288
fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
wenzelm@41634
   289
wenzelm@38418
   290
end;
wenzelm@38418
   291
wenzelm@38418
   292
wenzelm@38418
   293
wenzelm@38888
   294
(* toplevel transactions *)
wenzelm@38888
   295
wenzelm@38888
   296
local
wenzelm@38888
   297
wenzelm@44440
   298
fun timing tr t =
wenzelm@44440
   299
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   300
wenzelm@38888
   301
fun proof_status tr st =
wenzelm@38888
   302
  (case try Toplevel.proof_of st of
wenzelm@38888
   303
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   304
  | NONE => ());
wenzelm@38888
   305
wenzelm@44386
   306
fun print_state tr st =
wenzelm@44386
   307
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   308
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   309
wenzelm@40390
   310
fun run int tr st =
wenzelm@40390
   311
  (case Toplevel.transition int tr st of
wenzelm@40390
   312
    SOME (st', NONE) => ([], SOME st')
wenzelm@44271
   313
  | SOME (_, SOME (exn, _)) =>
wenzelm@44271
   314
      (case ML_Compiler.exn_messages exn of
wenzelm@40390
   315
        [] => Exn.interrupt ()
wenzelm@40390
   316
      | errs => (errs, NONE))
wenzelm@44270
   317
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@40390
   318
wenzelm@38888
   319
in
wenzelm@38888
   320
wenzelm@44199
   321
fun run_command tr st =
wenzelm@44185
   322
  let
wenzelm@44199
   323
    val is_init = Toplevel.is_init tr;
wenzelm@44185
   324
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   325
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   326
wenzelm@44185
   327
    val start = Timing.start ();
wenzelm@44185
   328
    val (errs, result) =
wenzelm@44185
   329
      if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
wenzelm@44185
   330
      else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   331
    val _ = timing tr (Timing.result start);
wenzelm@44185
   332
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   333
  in
wenzelm@44185
   334
    (case result of
wenzelm@44386
   335
      NONE => (Toplevel.status tr Markup.failed; (st, no_print))
wenzelm@44185
   336
    | SOME st' =>
wenzelm@44185
   337
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   338
        proof_status tr st';
wenzelm@44386
   339
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   340
  end;
wenzelm@38888
   341
wenzelm@38888
   342
end;
wenzelm@38888
   343
wenzelm@38888
   344
wenzelm@38888
   345
wenzelm@38888
   346
wenzelm@38418
   347
(** editing **)
wenzelm@38418
   348
wenzelm@44436
   349
(* perspective *)
wenzelm@44436
   350
wenzelm@44436
   351
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   352
  let
wenzelm@44436
   353
    val old_version = the_version state old_id;
wenzelm@44436
   354
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   355
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   356
  in define_version new_id new_version state end;
wenzelm@44436
   357
wenzelm@44436
   358
wenzelm@38418
   359
(* edit *)
wenzelm@38418
   360
wenzelm@38418
   361
local
wenzelm@38418
   362
wenzelm@38448
   363
fun is_changed node' ((_, id), exec) =
wenzelm@38418
   364
  (case try (the_entry node') id of
wenzelm@38418
   365
    NONE => true
wenzelm@38448
   366
  | SOME exec' => exec' <> exec);
wenzelm@38418
   367
wenzelm@44441
   368
fun init_theory deps name node =
wenzelm@44435
   369
  let
wenzelm@44435
   370
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   371
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   372
    val (dir, files) =
wenzelm@44435
   373
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   374
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   375
wenzelm@44435
   376
    val parents =
wenzelm@44435
   377
      imports |> map (fn import =>
wenzelm@44441
   378
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   379
          SOME thy => thy
wenzelm@44435
   380
        | NONE =>
wenzelm@44435
   381
            get_theory (Position.file_only import)
wenzelm@44435
   382
              (#2 (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   383
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   384
wenzelm@44441
   385
fun new_exec state init command_id (assigns, execs, exec) =
wenzelm@38418
   386
  let
wenzelm@44441
   387
    val command = the_command state command_id;
wenzelm@38419
   388
    val exec_id' = new_id ();
wenzelm@44386
   389
    val exec' = exec |> Lazy.map (fn (st, _) =>
wenzelm@44441
   390
      let val tr =
wenzelm@44441
   391
        Future.join command
wenzelm@44441
   392
        |> Toplevel.modify_init init
wenzelm@44441
   393
        |> Toplevel.put_id (print_id exec_id');
wenzelm@44386
   394
      in run_command tr st end);
wenzelm@44386
   395
  in ((command_id, exec_id') :: assigns, (exec_id', (command_id, exec')) :: execs, exec') end;
wenzelm@38418
   396
wenzelm@38418
   397
in
wenzelm@38418
   398
wenzelm@44157
   399
fun edit (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   400
  let
wenzelm@38418
   401
    val old_version = the_version state old_id;
wenzelm@44180
   402
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   403
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   404
wenzelm@44197
   405
    val updates =
wenzelm@44197
   406
      nodes_of new_version |> Graph.schedule
wenzelm@44199
   407
        (fn deps => fn (name, node) =>
wenzelm@44444
   408
          if not (get_touched node) then Future.value (([], [], []), node)
wenzelm@44444
   409
          else
wenzelm@44444
   410
            (case first_entry NONE (is_changed (node_of old_version name)) node of
wenzelm@44444
   411
              NONE => Future.value (([], [], []), set_touched false node)
wenzelm@44444
   412
            | SOME ((prev, id), _) =>
wenzelm@44444
   413
                let
wenzelm@44444
   414
                  fun init () = init_theory deps name node;
wenzelm@44444
   415
                in
wenzelm@44444
   416
                  (singleton o Future.forks)
wenzelm@44444
   417
                    {name = "Document.edit", group = NONE,
wenzelm@44445
   418
                      deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44444
   419
                    (fn () =>
wenzelm@44444
   420
                      let
wenzelm@44444
   421
                        val prev_exec =
wenzelm@44444
   422
                          (case prev of
wenzelm@44444
   423
                            NONE => no_id
wenzelm@44444
   424
                          | SOME prev_id => the_default no_id (the_entry node prev_id));
wenzelm@44444
   425
                        val (assigns, execs, last_exec) =
wenzelm@44444
   426
                          fold_entries (SOME id) (new_exec state init o #2 o #1)
wenzelm@44444
   427
                            node ([], [], #2 (the_exec state prev_exec));
wenzelm@44444
   428
                        val node' = node
wenzelm@44444
   429
                          |> fold update_entry assigns
wenzelm@44444
   430
                          |> set_result (Lazy.map #1 last_exec)
wenzelm@44444
   431
                          |> set_touched false;
wenzelm@44444
   432
                      in ((assigns, execs, [(name, node')]), node') end)
wenzelm@44444
   433
                end))
wenzelm@44330
   434
      |> Future.joins |> map #1;
wenzelm@38418
   435
wenzelm@44197
   436
    val state' = state
wenzelm@44197
   437
      |> fold (fold define_exec o #2) updates
wenzelm@44197
   438
      |> define_version new_id (fold (fold put_node o #3) updates new_version);
wenzelm@38421
   439
wenzelm@44197
   440
  in (maps #1 (rev updates), state') end;
wenzelm@38418
   441
wenzelm@38418
   442
end;
wenzelm@38418
   443
wenzelm@38418
   444
wenzelm@38418
   445
(* execute *)
wenzelm@38418
   446
wenzelm@38418
   447
fun execute version_id state =
wenzelm@41673
   448
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   449
    let
wenzelm@38418
   450
      val version = the_version state version_id;
wenzelm@38418
   451
wenzelm@44386
   452
      fun force_exec _ NONE = ()
wenzelm@44386
   453
        | force_exec node (SOME exec_id) =
wenzelm@44386
   454
            let
wenzelm@44386
   455
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   456
              val (_, print) = Lazy.force exec;
wenzelm@44386
   457
              val _ =
wenzelm@44439
   458
                if #1 (get_perspective node) command_id
wenzelm@44386
   459
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   460
                else ();
wenzelm@44386
   461
            in () end;
wenzelm@38418
   462
wenzelm@44300
   463
      val execution = Future.new_group NONE;
wenzelm@44299
   464
      val _ =
wenzelm@44201
   465
        nodes_of version |> Graph.schedule
wenzelm@44201
   466
          (fn deps => fn (name, node) =>
wenzelm@44302
   467
            (singleton o Future.forks)
wenzelm@44302
   468
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
wenzelm@44302
   469
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = true}
wenzelm@44386
   470
              (fold_entries NONE (fn (_, exec) => fn () => force_exec node exec) node));
wenzelm@40520
   471
wenzelm@44299
   472
    in (versions, commands, execs, execution) end);
wenzelm@38418
   473
wenzelm@43713
   474
wenzelm@43713
   475
wenzelm@43713
   476
(** global state **)
wenzelm@43713
   477
wenzelm@43713
   478
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   479
wenzelm@43713
   480
fun state () = Synchronized.value global_state;
wenzelm@43713
   481
val change_state = Synchronized.change global_state;
wenzelm@43713
   482
wenzelm@38418
   483
end;
wenzelm@38418
   484