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