src/Pure/PIDE/document.ML
author wenzelm
Fri Aug 26 15:09:54 2011 +0200 (2011-08-26)
changeset 44479 9a04e7502e22
parent 44478 4fdb1009a370
child 44480 38c5b085fb1c
permissions -rw-r--r--
refined document state assignment: observe perspective, more explicit assignment message;
misc tuning and clarification;
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@44478
     5
list of commands, with asynchronous read/eval/print processes.
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@44476
    31
  val edit: version_id -> version_id -> edit list -> state ->
wenzelm@44479
    32
    ((command_id * exec_id option) list * (string * command_id option) list) * state
wenzelm@38418
    33
  val execute: version_id -> state -> state
wenzelm@43713
    34
  val state: unit -> state
wenzelm@43713
    35
  val change_state: (state -> state) -> unit
wenzelm@38150
    36
end;
wenzelm@38150
    37
wenzelm@38150
    38
structure Document: DOCUMENT =
wenzelm@38150
    39
struct
wenzelm@38150
    40
wenzelm@38150
    41
(* unique identifiers *)
wenzelm@38150
    42
wenzelm@38363
    43
type id = int;
wenzelm@38373
    44
type version_id = id;
wenzelm@38363
    45
type command_id = id;
wenzelm@38373
    46
type exec_id = id;
wenzelm@38355
    47
wenzelm@38355
    48
val no_id = 0;
wenzelm@40449
    49
val new_id = Synchronized.counter ();
wenzelm@38418
    50
wenzelm@38414
    51
val parse_id = Markup.parse_int;
wenzelm@38414
    52
val print_id = Markup.print_int;
wenzelm@38150
    53
wenzelm@38418
    54
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
wenzelm@38418
    55
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
wenzelm@38150
    56
wenzelm@38418
    57
wenzelm@38418
    58
wenzelm@38418
    59
(** document structure **)
wenzelm@38418
    60
wenzelm@44185
    61
type node_header = (string * string list * (string * bool) list) Exn.result;
wenzelm@44476
    62
type perspective = (command_id -> bool) * command_id option; (*visible commands, last*)
wenzelm@38448
    63
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    64
wenzelm@43668
    65
abstype node = Node of
wenzelm@44444
    66
 {touched: bool,
wenzelm@44444
    67
  header: node_header,
wenzelm@44386
    68
  perspective: perspective,
wenzelm@44180
    69
  entries: exec_id option Entries.T,  (*command entries with excecutions*)
wenzelm@44476
    70
  last_exec: command_id option,  (*last command with exec state assignment*)
wenzelm@44197
    71
  result: Toplevel.state lazy}
wenzelm@43668
    72
and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    73
with
wenzelm@38418
    74
wenzelm@44476
    75
fun make_node (touched, header, perspective, entries, last_exec, result) =
wenzelm@44444
    76
  Node {touched = touched, header = header, perspective = perspective,
wenzelm@44476
    77
    entries = entries, last_exec = last_exec, result = result};
wenzelm@43668
    78
wenzelm@44476
    79
fun map_node f (Node {touched, header, perspective, entries, last_exec, result}) =
wenzelm@44476
    80
  make_node (f (touched, header, perspective, entries, last_exec, result));
wenzelm@38418
    81
wenzelm@44476
    82
fun make_perspective command_ids : perspective =
wenzelm@44476
    83
  (Inttab.defined (Inttab.make (map (rpair ()) command_ids)), try List.last command_ids);
wenzelm@44441
    84
wenzelm@44476
    85
val no_header = Exn.Exn (ERROR "Bad theory header");
wenzelm@44441
    86
val no_perspective = make_perspective [];
wenzelm@44386
    87
val no_print = Lazy.value ();
wenzelm@44386
    88
val no_result = Lazy.value Toplevel.toplevel;
wenzelm@44386
    89
wenzelm@44476
    90
val empty_node = make_node (false, no_header, no_perspective, Entries.empty, NONE, no_result);
wenzelm@44476
    91
val clear_node = map_node (fn (_, header, _, _, _, _) =>
wenzelm@44476
    92
  (false, header, no_perspective, Entries.empty, NONE, no_result));
wenzelm@44386
    93
wenzelm@44386
    94
wenzelm@44386
    95
(* basic components *)
wenzelm@44386
    96
wenzelm@44446
    97
fun is_touched (Node {touched, ...}) = touched;
wenzelm@44444
    98
fun set_touched touched =
wenzelm@44476
    99
  map_node (fn (_, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   100
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44444
   101
wenzelm@44180
   102
fun get_header (Node {header, ...}) = header;
wenzelm@44385
   103
fun set_header header =
wenzelm@44476
   104
  map_node (fn (touched, _, perspective, entries, last_exec, result) =>
wenzelm@44476
   105
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   106
wenzelm@44385
   107
fun get_perspective (Node {perspective, ...}) = perspective;
wenzelm@44441
   108
fun set_perspective ids =
wenzelm@44476
   109
  map_node (fn (touched, header, _, entries, last_exec, result) =>
wenzelm@44476
   110
    (touched, header, make_perspective ids, entries, last_exec, result));
wenzelm@44385
   111
wenzelm@44444
   112
fun map_entries f =
wenzelm@44476
   113
  map_node (fn (touched, header, perspective, entries, last_exec, result) =>
wenzelm@44476
   114
    (touched, header, perspective, f entries, last_exec, result));
wenzelm@43668
   115
fun fold_entries start f (Node {entries, ...}) = Entries.fold start f entries;
wenzelm@43668
   116
fun first_entry start P (Node {entries, ...}) = Entries.get_first start P entries;
wenzelm@43668
   117
wenzelm@44476
   118
fun get_last_exec (Node {last_exec, ...}) = last_exec;
wenzelm@44476
   119
fun set_last_exec last_exec =
wenzelm@44476
   120
  map_node (fn (touched, header, perspective, entries, _, result) =>
wenzelm@44476
   121
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44476
   122
wenzelm@44386
   123
fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force result);
wenzelm@44385
   124
fun set_result result =
wenzelm@44476
   125
  map_node (fn (touched, header, perspective, entries, last_exec, _) =>
wenzelm@44476
   126
    (touched, header, perspective, entries, last_exec, result));
wenzelm@44180
   127
wenzelm@44185
   128
fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
wenzelm@44202
   129
fun default_node name = Graph.default_node (name, empty_node);
wenzelm@44202
   130
fun update_node name f = default_node name #> Graph.map_node name f;
wenzelm@38418
   131
wenzelm@38418
   132
wenzelm@38448
   133
(* node edits and associated executions *)
wenzelm@38150
   134
wenzelm@44157
   135
datatype node_edit =
wenzelm@44185
   136
  Clear |
wenzelm@44157
   137
  Edits of (command_id option * command_id option) list |
wenzelm@44384
   138
  Header of node_header |
wenzelm@44385
   139
  Perspective of command_id list;
wenzelm@44157
   140
wenzelm@44156
   141
type edit = string * node_edit;
wenzelm@38448
   142
wenzelm@44479
   143
fun next_entry (Node {entries, ...}) id =
wenzelm@44479
   144
  (case Entries.lookup entries id of
wenzelm@44479
   145
    NONE => err_undef "command entry" id
wenzelm@44479
   146
  | SOME (_, next) => next);
wenzelm@44479
   147
wenzelm@43668
   148
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   149
  (case Entries.lookup entries id of
wenzelm@38448
   150
    NONE => err_undef "command entry" id
wenzelm@44476
   151
  | SOME (exec, _) => exec);
wenzelm@38150
   152
wenzelm@44479
   153
fun the_default_entry node (SOME id) = the_default no_id (the_entry node id)
wenzelm@44479
   154
  | the_default_entry _ NONE = no_id;
wenzelm@44476
   155
wenzelm@44476
   156
fun update_entry id exec_id =
wenzelm@43668
   157
  map_entries (Entries.update (id, SOME exec_id));
wenzelm@38418
   158
wenzelm@38448
   159
fun reset_after id entries =
wenzelm@38448
   160
  (case Entries.get_after entries id of
wenzelm@38448
   161
    NONE => entries
wenzelm@38448
   162
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   163
wenzelm@43668
   164
val edit_node = map_entries o fold
wenzelm@43668
   165
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   166
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   167
wenzelm@38418
   168
wenzelm@38418
   169
(* version operations *)
wenzelm@38418
   170
wenzelm@44185
   171
val empty_version = Version Graph.empty;
wenzelm@44185
   172
wenzelm@38418
   173
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   174
val node_of = get_node o nodes_of;
wenzelm@38418
   175
wenzelm@44444
   176
local
wenzelm@44444
   177
wenzelm@44185
   178
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   179
wenzelm@44444
   180
fun touch_node name nodes =
wenzelm@44444
   181
  fold (fn desc =>
wenzelm@44444
   182
      update_node desc (set_touched true) #>
wenzelm@44444
   183
      desc <> name ? update_node desc (map_entries (reset_after NONE)))
wenzelm@44202
   184
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   185
wenzelm@44444
   186
in
wenzelm@44444
   187
wenzelm@44157
   188
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   189
  Version
wenzelm@44436
   190
    (case node_edit of
wenzelm@44436
   191
      Clear =>
wenzelm@44436
   192
        nodes
wenzelm@44436
   193
        |> update_node name clear_node
wenzelm@44444
   194
        |> touch_node name
wenzelm@44436
   195
    | Edits edits =>
wenzelm@44436
   196
        nodes
wenzelm@44436
   197
        |> update_node name (edit_node edits)
wenzelm@44444
   198
        |> touch_node name
wenzelm@44436
   199
    | Header header =>
wenzelm@44436
   200
        let
wenzelm@44436
   201
          val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
wenzelm@44436
   202
          val nodes1 = nodes
wenzelm@44436
   203
            |> default_node name
wenzelm@44436
   204
            |> fold default_node parents;
wenzelm@44436
   205
          val nodes2 = nodes1
wenzelm@44436
   206
            |> Graph.Keys.fold
wenzelm@44436
   207
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@44436
   208
          val (header', nodes3) =
wenzelm@44436
   209
            (header, Graph.add_deps_acyclic (name, parents) nodes2)
wenzelm@44436
   210
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@44436
   211
        in Graph.map_node name (set_header header') nodes3 end
wenzelm@44444
   212
        |> touch_node name
wenzelm@44436
   213
    | Perspective perspective =>
wenzelm@44479
   214
        nodes
wenzelm@44479
   215
        |> update_node name (set_perspective perspective)
wenzelm@44479
   216
        |> touch_node name (* FIXME more precise!?! *));
wenzelm@38418
   217
wenzelm@44444
   218
end;
wenzelm@44444
   219
wenzelm@44222
   220
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   221
  Version (update_node name (K node) nodes);
wenzelm@38418
   222
wenzelm@38150
   223
end;
wenzelm@38150
   224
wenzelm@38418
   225
wenzelm@38418
   226
wenzelm@38418
   227
(** global state -- document structure and execution process **)
wenzelm@38418
   228
wenzelm@38418
   229
abstype state = State of
wenzelm@40398
   230
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@38421
   231
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@44386
   232
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   233
    (*exec_id -> command_id with eval/print process*)
wenzelm@44300
   234
  execution: Future.group}  (*global execution process*)
wenzelm@38418
   235
with
wenzelm@38418
   236
wenzelm@38418
   237
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   238
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   239
wenzelm@38418
   240
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   241
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   242
wenzelm@38418
   243
val init_state =
wenzelm@38418
   244
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38449
   245
    Inttab.make [(no_id, Future.value Toplevel.empty)],
wenzelm@44386
   246
    Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
wenzelm@44300
   247
    Future.new_group NONE);
wenzelm@38418
   248
wenzelm@38418
   249
wenzelm@38418
   250
(* document versions *)
wenzelm@38418
   251
wenzelm@38418
   252
fun define_version (id: version_id) version =
wenzelm@38418
   253
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   254
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   255
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   256
    in (versions', commands, execs, execution) end);
wenzelm@38418
   257
wenzelm@38418
   258
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   259
  (case Inttab.lookup versions id of
wenzelm@38418
   260
    NONE => err_undef "document version" id
wenzelm@38418
   261
  | SOME version => version);
wenzelm@38418
   262
wenzelm@38418
   263
wenzelm@38418
   264
(* commands *)
wenzelm@38418
   265
wenzelm@38418
   266
fun define_command (id: command_id) text =
wenzelm@38418
   267
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   268
    let
wenzelm@38418
   269
      val id_string = print_id id;
wenzelm@44302
   270
      val tr =
wenzelm@44302
   271
        Future.fork_pri 2 (fn () =>
wenzelm@44302
   272
          Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44478
   273
            (fn () => Outer_Syntax.read_command (Position.id id_string) text) ());
wenzelm@38418
   274
      val commands' =
wenzelm@38421
   275
        Inttab.update_new (id, tr) commands
wenzelm@38418
   276
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   277
    in (versions, commands', execs, execution) end);
wenzelm@38418
   278
wenzelm@38418
   279
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   280
  (case Inttab.lookup commands id of
wenzelm@38418
   281
    NONE => err_undef "command" id
wenzelm@38418
   282
  | SOME tr => tr);
wenzelm@38418
   283
wenzelm@38421
   284
fun join_commands (State {commands, ...}) =
wenzelm@38421
   285
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   286
wenzelm@38418
   287
wenzelm@38418
   288
(* command executions *)
wenzelm@38418
   289
wenzelm@44197
   290
fun define_exec (exec_id, exec) =
wenzelm@38418
   291
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   292
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   293
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   294
    in (versions, commands, execs', execution) end);
wenzelm@38418
   295
wenzelm@44197
   296
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   297
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   298
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   299
  | SOME exec => exec);
wenzelm@38418
   300
wenzelm@41634
   301
wenzelm@41634
   302
(* document execution *)
wenzelm@41634
   303
wenzelm@44299
   304
fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
wenzelm@41634
   305
wenzelm@38418
   306
end;
wenzelm@38418
   307
wenzelm@38418
   308
wenzelm@38418
   309
wenzelm@38888
   310
(* toplevel transactions *)
wenzelm@38888
   311
wenzelm@38888
   312
local
wenzelm@38888
   313
wenzelm@44440
   314
fun timing tr t =
wenzelm@44440
   315
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   316
wenzelm@38888
   317
fun proof_status tr st =
wenzelm@38888
   318
  (case try Toplevel.proof_of st of
wenzelm@38888
   319
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   320
  | NONE => ());
wenzelm@38888
   321
wenzelm@44386
   322
fun print_state tr st =
wenzelm@44386
   323
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   324
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   325
wenzelm@40390
   326
fun run int tr st =
wenzelm@40390
   327
  (case Toplevel.transition int tr st of
wenzelm@40390
   328
    SOME (st', NONE) => ([], SOME st')
wenzelm@44271
   329
  | SOME (_, SOME (exn, _)) =>
wenzelm@44271
   330
      (case ML_Compiler.exn_messages exn of
wenzelm@40390
   331
        [] => Exn.interrupt ()
wenzelm@40390
   332
      | errs => (errs, NONE))
wenzelm@44270
   333
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@40390
   334
wenzelm@38888
   335
in
wenzelm@38888
   336
wenzelm@44199
   337
fun run_command tr st =
wenzelm@44185
   338
  let
wenzelm@44199
   339
    val is_init = Toplevel.is_init tr;
wenzelm@44185
   340
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   341
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   342
wenzelm@44185
   343
    val start = Timing.start ();
wenzelm@44185
   344
    val (errs, result) =
wenzelm@44185
   345
      if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
wenzelm@44185
   346
      else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   347
    val _ = timing tr (Timing.result start);
wenzelm@44185
   348
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   349
  in
wenzelm@44185
   350
    (case result of
wenzelm@44386
   351
      NONE => (Toplevel.status tr Markup.failed; (st, no_print))
wenzelm@44185
   352
    | SOME st' =>
wenzelm@44185
   353
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   354
        proof_status tr st';
wenzelm@44386
   355
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   356
  end;
wenzelm@38888
   357
wenzelm@38888
   358
end;
wenzelm@38888
   359
wenzelm@38888
   360
wenzelm@38888
   361
wenzelm@38888
   362
wenzelm@38418
   363
(** editing **)
wenzelm@38418
   364
wenzelm@44436
   365
(* perspective *)
wenzelm@44436
   366
wenzelm@44436
   367
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   368
  let
wenzelm@44436
   369
    val old_version = the_version state old_id;
wenzelm@44436
   370
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   371
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   372
  in define_version new_id new_version state end;
wenzelm@44436
   373
wenzelm@44436
   374
wenzelm@38418
   375
(* edit *)
wenzelm@38418
   376
wenzelm@38418
   377
local
wenzelm@38418
   378
wenzelm@44479
   379
fun after_perspective node ((prev, _), _) = #2 (get_perspective node) = prev;
wenzelm@44479
   380
wenzelm@44479
   381
fun needs_update node0 ((_, id), SOME exec) =
wenzelm@44479
   382
      (case try (the_entry node0) id of
wenzelm@44479
   383
        SOME (SOME exec0) => exec0 <> exec
wenzelm@44479
   384
      | _ => true)
wenzelm@44479
   385
  | needs_update _ _ = true;
wenzelm@38418
   386
wenzelm@44441
   387
fun init_theory deps name node =
wenzelm@44435
   388
  let
wenzelm@44435
   389
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   390
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   391
    val (dir, files) =
wenzelm@44435
   392
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   393
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   394
wenzelm@44435
   395
    val parents =
wenzelm@44435
   396
      imports |> map (fn import =>
wenzelm@44441
   397
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   398
          SOME thy => thy
wenzelm@44435
   399
        | NONE =>
wenzelm@44435
   400
            get_theory (Position.file_only import)
wenzelm@44435
   401
              (#2 (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   402
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   403
wenzelm@44476
   404
fun new_exec state init command_id' (execs, exec) =
wenzelm@38418
   405
  let
wenzelm@44476
   406
    val command' = the_command state command_id';
wenzelm@38419
   407
    val exec_id' = new_id ();
wenzelm@44476
   408
    val exec' =
wenzelm@44476
   409
      snd exec |> Lazy.map (fn (st, _) =>
wenzelm@44476
   410
        let val tr =
wenzelm@44476
   411
          Future.join command'
wenzelm@44476
   412
          |> Toplevel.modify_init init
wenzelm@44476
   413
          |> Toplevel.put_id (print_id exec_id');
wenzelm@44476
   414
        in run_command tr st end)
wenzelm@44476
   415
      |> pair command_id';
wenzelm@44476
   416
  in ((exec_id', exec') :: execs, exec') end;
wenzelm@38418
   417
wenzelm@38418
   418
in
wenzelm@38418
   419
wenzelm@44157
   420
fun edit (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   421
  let
wenzelm@38418
   422
    val old_version = the_version state old_id;
wenzelm@44180
   423
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@44222
   424
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   425
wenzelm@44476
   426
    val updated =
wenzelm@44197
   427
      nodes_of new_version |> Graph.schedule
wenzelm@44199
   428
        (fn deps => fn (name, node) =>
wenzelm@44479
   429
          if not (is_touched node) then Future.value (([], [], []), node)
wenzelm@44444
   430
          else
wenzelm@44479
   431
            let
wenzelm@44479
   432
              val node0 = node_of old_version name;
wenzelm@44479
   433
              fun init () = init_theory deps name node;
wenzelm@44479
   434
            in
wenzelm@44479
   435
              (case first_entry NONE (after_perspective node orf needs_update node0) node of
wenzelm@44479
   436
                NONE => Future.value (([], [], []), set_touched false node)
wenzelm@44479
   437
              | SOME ((prev, id), _) =>
wenzelm@44444
   438
                  (singleton o Future.forks)
wenzelm@44444
   439
                    {name = "Document.edit", group = NONE,
wenzelm@44445
   440
                      deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
wenzelm@44444
   441
                    (fn () =>
wenzelm@44444
   442
                      let
wenzelm@44479
   443
                        val (execs, exec) =
wenzelm@44479
   444
                          fold_entries (SOME id)
wenzelm@44479
   445
                            (fn entry1 as ((_, id1), _) => fn res =>
wenzelm@44479
   446
                              if after_perspective node entry1 then NONE
wenzelm@44479
   447
                              else SOME (new_exec state init id1 res))
wenzelm@44479
   448
                            node ([], the_exec state (the_default_entry node prev));
wenzelm@44479
   449
wenzelm@44479
   450
                        val no_execs =
wenzelm@44479
   451
                          if can (the_entry node0) id then
wenzelm@44479
   452
                            fold_entries (SOME id)
wenzelm@44479
   453
                              (fn ((_, id0), exec0) => fn res =>
wenzelm@44479
   454
                                if is_none exec0 then NONE
wenzelm@44479
   455
                                else if exists (fn (_, (id1, _)) => id0 = id1) execs then SOME res
wenzelm@44479
   456
                                else SOME (id0 :: res)) node0 []
wenzelm@44479
   457
                          else [];
wenzelm@44479
   458
wenzelm@44479
   459
                        val node1 =
wenzelm@44476
   460
                          fold (fn (exec_id, (command_id, _)) => update_entry command_id exec_id)
wenzelm@44476
   461
                            execs node;
wenzelm@44479
   462
                        val last_exec = if #1 exec = no_id then NONE else SOME (#1 exec);
wenzelm@44476
   463
                        val result =
wenzelm@44479
   464
                          if is_some last_exec andalso is_none (next_entry node1 (the last_exec))
wenzelm@44479
   465
                          then Lazy.map #1 (#2 exec)
wenzelm@44476
   466
                          else no_result;
wenzelm@44479
   467
                        val node2 = node1
wenzelm@44479
   468
                          |> set_last_exec last_exec
wenzelm@44476
   469
                          |> set_result result
wenzelm@44444
   470
                          |> set_touched false;
wenzelm@44479
   471
                      in ((no_execs, execs, [(name, node2)]), node2) end))
wenzelm@44479
   472
            end)
wenzelm@44479
   473
      |> Future.joins |> map #1;
wenzelm@44476
   474
wenzelm@44479
   475
    val command_execs =
wenzelm@44479
   476
      map (rpair NONE) (maps #1 updated) @
wenzelm@44479
   477
      map (fn (exec_id, (command_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   478
    val updated_nodes = maps #3 updated;
wenzelm@44476
   479
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   480
wenzelm@44197
   481
    val state' = state
wenzelm@44479
   482
      |> fold (fold define_exec o #2) updated
wenzelm@44476
   483
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   484
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   485
wenzelm@38418
   486
end;
wenzelm@38418
   487
wenzelm@38418
   488
wenzelm@38418
   489
(* execute *)
wenzelm@38418
   490
wenzelm@38418
   491
fun execute version_id state =
wenzelm@41673
   492
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   493
    let
wenzelm@38418
   494
      val version = the_version state version_id;
wenzelm@38418
   495
wenzelm@44386
   496
      fun force_exec _ NONE = ()
wenzelm@44386
   497
        | force_exec node (SOME exec_id) =
wenzelm@44386
   498
            let
wenzelm@44386
   499
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   500
              val (_, print) = Lazy.force exec;
wenzelm@44386
   501
              val _ =
wenzelm@44439
   502
                if #1 (get_perspective node) command_id
wenzelm@44386
   503
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   504
                else ();
wenzelm@44386
   505
            in () end;
wenzelm@38418
   506
wenzelm@44300
   507
      val execution = Future.new_group NONE;
wenzelm@44299
   508
      val _ =
wenzelm@44201
   509
        nodes_of version |> Graph.schedule
wenzelm@44201
   510
          (fn deps => fn (name, node) =>
wenzelm@44302
   511
            (singleton o Future.forks)
wenzelm@44302
   512
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
wenzelm@44302
   513
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = true}
wenzelm@44479
   514
              (fold_entries NONE
wenzelm@44479
   515
                (fn entry as (_, exec) => fn () => SOME (force_exec node exec)) node));
wenzelm@40520
   516
wenzelm@44299
   517
    in (versions, commands, execs, execution) end);
wenzelm@38418
   518
wenzelm@43713
   519
wenzelm@43713
   520
wenzelm@43713
   521
(** global state **)
wenzelm@43713
   522
wenzelm@43713
   523
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   524
wenzelm@43713
   525
fun state () = Synchronized.value global_state;
wenzelm@43713
   526
val change_state = Synchronized.change global_state;
wenzelm@43713
   527
wenzelm@38418
   528
end;
wenzelm@38418
   529