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