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