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