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