src/Pure/PIDE/document.ML
author wenzelm
Fri Aug 26 15:56:30 2011 +0200 (2011-08-26 ago)
changeset 44480 38c5b085fb1c
parent 44479 9a04e7502e22
child 44481 bb42bc831570
permissions -rw-r--r--
improved Document.edit: more accurate update_start and no_execs;
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@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@44480
   148
fun lookup_entry (Node {entries, ...}) id =
wenzelm@44480
   149
  (case Entries.lookup entries id of
wenzelm@44480
   150
    NONE => NONE
wenzelm@44480
   151
  | SOME (exec, _) => exec);
wenzelm@44480
   152
wenzelm@43668
   153
fun the_entry (Node {entries, ...}) id =
wenzelm@38448
   154
  (case Entries.lookup entries id of
wenzelm@38448
   155
    NONE => err_undef "command entry" id
wenzelm@44476
   156
  | SOME (exec, _) => exec);
wenzelm@38150
   157
wenzelm@44479
   158
fun the_default_entry node (SOME id) = the_default no_id (the_entry node id)
wenzelm@44479
   159
  | the_default_entry _ NONE = no_id;
wenzelm@44476
   160
wenzelm@44476
   161
fun update_entry id exec_id =
wenzelm@43668
   162
  map_entries (Entries.update (id, SOME exec_id));
wenzelm@38418
   163
wenzelm@38448
   164
fun reset_after id entries =
wenzelm@38448
   165
  (case Entries.get_after entries id of
wenzelm@38448
   166
    NONE => entries
wenzelm@38448
   167
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
   168
wenzelm@43668
   169
val edit_node = map_entries o fold
wenzelm@43668
   170
  (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
wenzelm@43668
   171
    | (id, NONE) => Entries.delete_after id #> reset_after id);
wenzelm@38418
   172
wenzelm@38418
   173
wenzelm@38418
   174
(* version operations *)
wenzelm@38418
   175
wenzelm@44185
   176
val empty_version = Version Graph.empty;
wenzelm@44185
   177
wenzelm@38418
   178
fun nodes_of (Version nodes) = nodes;
wenzelm@44185
   179
val node_of = get_node o nodes_of;
wenzelm@38418
   180
wenzelm@44444
   181
local
wenzelm@44444
   182
wenzelm@44185
   183
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   184
wenzelm@44444
   185
fun touch_node name nodes =
wenzelm@44444
   186
  fold (fn desc =>
wenzelm@44444
   187
      update_node desc (set_touched true) #>
wenzelm@44444
   188
      desc <> name ? update_node desc (map_entries (reset_after NONE)))
wenzelm@44202
   189
    (Graph.all_succs nodes [name]) nodes;
wenzelm@44202
   190
wenzelm@44444
   191
in
wenzelm@44444
   192
wenzelm@44157
   193
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   194
  Version
wenzelm@44436
   195
    (case node_edit of
wenzelm@44436
   196
      Clear =>
wenzelm@44436
   197
        nodes
wenzelm@44436
   198
        |> update_node name clear_node
wenzelm@44444
   199
        |> touch_node name
wenzelm@44436
   200
    | Edits edits =>
wenzelm@44436
   201
        nodes
wenzelm@44436
   202
        |> update_node name (edit_node edits)
wenzelm@44444
   203
        |> touch_node name
wenzelm@44436
   204
    | Header header =>
wenzelm@44436
   205
        let
wenzelm@44436
   206
          val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
wenzelm@44436
   207
          val nodes1 = nodes
wenzelm@44436
   208
            |> default_node name
wenzelm@44436
   209
            |> fold default_node parents;
wenzelm@44436
   210
          val nodes2 = nodes1
wenzelm@44436
   211
            |> Graph.Keys.fold
wenzelm@44436
   212
                (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
wenzelm@44436
   213
          val (header', nodes3) =
wenzelm@44436
   214
            (header, Graph.add_deps_acyclic (name, parents) nodes2)
wenzelm@44436
   215
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
wenzelm@44436
   216
        in Graph.map_node name (set_header header') nodes3 end
wenzelm@44444
   217
        |> touch_node name
wenzelm@44436
   218
    | Perspective perspective =>
wenzelm@44479
   219
        nodes
wenzelm@44479
   220
        |> update_node name (set_perspective perspective)
wenzelm@44479
   221
        |> touch_node name (* FIXME more precise!?! *));
wenzelm@38418
   222
wenzelm@44444
   223
end;
wenzelm@44444
   224
wenzelm@44222
   225
fun put_node (name, node) (Version nodes) =
wenzelm@44222
   226
  Version (update_node name (K node) nodes);
wenzelm@38418
   227
wenzelm@38150
   228
end;
wenzelm@38150
   229
wenzelm@38418
   230
wenzelm@38418
   231
wenzelm@38418
   232
(** global state -- document structure and execution process **)
wenzelm@38418
   233
wenzelm@38418
   234
abstype state = State of
wenzelm@40398
   235
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@38421
   236
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@44386
   237
  execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
wenzelm@44386
   238
    (*exec_id -> command_id with eval/print process*)
wenzelm@44300
   239
  execution: Future.group}  (*global execution process*)
wenzelm@38418
   240
with
wenzelm@38418
   241
wenzelm@38418
   242
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   243
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   244
wenzelm@38418
   245
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   246
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   247
wenzelm@38418
   248
val init_state =
wenzelm@38418
   249
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38449
   250
    Inttab.make [(no_id, Future.value Toplevel.empty)],
wenzelm@44386
   251
    Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
wenzelm@44300
   252
    Future.new_group NONE);
wenzelm@38418
   253
wenzelm@38418
   254
wenzelm@38418
   255
(* document versions *)
wenzelm@38418
   256
wenzelm@38418
   257
fun define_version (id: version_id) version =
wenzelm@38418
   258
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   259
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   260
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   261
    in (versions', commands, execs, execution) end);
wenzelm@38418
   262
wenzelm@38418
   263
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   264
  (case Inttab.lookup versions id of
wenzelm@38418
   265
    NONE => err_undef "document version" id
wenzelm@38418
   266
  | SOME version => version);
wenzelm@38418
   267
wenzelm@38418
   268
wenzelm@38418
   269
(* commands *)
wenzelm@38418
   270
wenzelm@38418
   271
fun define_command (id: command_id) text =
wenzelm@38418
   272
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   273
    let
wenzelm@38418
   274
      val id_string = print_id id;
wenzelm@44302
   275
      val tr =
wenzelm@44302
   276
        Future.fork_pri 2 (fn () =>
wenzelm@44302
   277
          Position.setmp_thread_data (Position.id_only id_string)
wenzelm@44478
   278
            (fn () => Outer_Syntax.read_command (Position.id id_string) text) ());
wenzelm@38418
   279
      val commands' =
wenzelm@38421
   280
        Inttab.update_new (id, tr) commands
wenzelm@38418
   281
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   282
    in (versions, commands', execs, execution) end);
wenzelm@38418
   283
wenzelm@38418
   284
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   285
  (case Inttab.lookup commands id of
wenzelm@38418
   286
    NONE => err_undef "command" id
wenzelm@38418
   287
  | SOME tr => tr);
wenzelm@38418
   288
wenzelm@38421
   289
fun join_commands (State {commands, ...}) =
wenzelm@38421
   290
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   291
wenzelm@38418
   292
wenzelm@38418
   293
(* command executions *)
wenzelm@38418
   294
wenzelm@44197
   295
fun define_exec (exec_id, exec) =
wenzelm@38418
   296
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   297
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   298
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   299
    in (versions, commands, execs', execution) end);
wenzelm@38418
   300
wenzelm@44197
   301
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   302
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   303
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   304
  | SOME exec => exec);
wenzelm@38418
   305
wenzelm@41634
   306
wenzelm@41634
   307
(* document execution *)
wenzelm@41634
   308
wenzelm@44299
   309
fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
wenzelm@41634
   310
wenzelm@38418
   311
end;
wenzelm@38418
   312
wenzelm@38418
   313
wenzelm@38418
   314
wenzelm@38888
   315
(* toplevel transactions *)
wenzelm@38888
   316
wenzelm@38888
   317
local
wenzelm@38888
   318
wenzelm@44440
   319
fun timing tr t =
wenzelm@44440
   320
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@40391
   321
wenzelm@38888
   322
fun proof_status tr st =
wenzelm@38888
   323
  (case try Toplevel.proof_of st of
wenzelm@38888
   324
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   325
  | NONE => ());
wenzelm@38888
   326
wenzelm@44386
   327
fun print_state tr st =
wenzelm@44386
   328
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@44386
   329
    (fn () => Toplevel.print_state false st);
wenzelm@38888
   330
wenzelm@40390
   331
fun run int tr st =
wenzelm@40390
   332
  (case Toplevel.transition int tr st of
wenzelm@40390
   333
    SOME (st', NONE) => ([], SOME st')
wenzelm@44271
   334
  | SOME (_, SOME (exn, _)) =>
wenzelm@44271
   335
      (case ML_Compiler.exn_messages exn of
wenzelm@40390
   336
        [] => Exn.interrupt ()
wenzelm@40390
   337
      | errs => (errs, NONE))
wenzelm@44270
   338
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@40390
   339
wenzelm@38888
   340
in
wenzelm@38888
   341
wenzelm@44199
   342
fun run_command tr st =
wenzelm@44185
   343
  let
wenzelm@44199
   344
    val is_init = Toplevel.is_init tr;
wenzelm@44185
   345
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   346
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   347
wenzelm@44185
   348
    val start = Timing.start ();
wenzelm@44185
   349
    val (errs, result) =
wenzelm@44185
   350
      if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
wenzelm@44185
   351
      else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   352
    val _ = timing tr (Timing.result start);
wenzelm@44185
   353
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   354
  in
wenzelm@44185
   355
    (case result of
wenzelm@44386
   356
      NONE => (Toplevel.status tr Markup.failed; (st, no_print))
wenzelm@44185
   357
    | SOME st' =>
wenzelm@44185
   358
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   359
        proof_status tr st';
wenzelm@44386
   360
        (st', if do_print then print_state tr st' else no_print)))
wenzelm@44185
   361
  end;
wenzelm@38888
   362
wenzelm@38888
   363
end;
wenzelm@38888
   364
wenzelm@38888
   365
wenzelm@38888
   366
wenzelm@38888
   367
wenzelm@38418
   368
(** editing **)
wenzelm@38418
   369
wenzelm@44436
   370
(* perspective *)
wenzelm@44436
   371
wenzelm@44436
   372
fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
wenzelm@44436
   373
  let
wenzelm@44436
   374
    val old_version = the_version state old_id;
wenzelm@44436
   375
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
wenzelm@44436
   376
    val new_version = edit_nodes (name, Perspective perspective) old_version;
wenzelm@44436
   377
  in define_version new_id new_version state end;
wenzelm@44436
   378
wenzelm@44436
   379
wenzelm@38418
   380
(* edit *)
wenzelm@38418
   381
wenzelm@38418
   382
local
wenzelm@38418
   383
wenzelm@44441
   384
fun init_theory deps name node =
wenzelm@44435
   385
  let
wenzelm@44435
   386
    val (thy_name, imports, uses) = Exn.release (get_header node);
wenzelm@44435
   387
    (* FIXME provide files via Scala layer *)
wenzelm@44435
   388
    val (dir, files) =
wenzelm@44435
   389
      if ML_System.platform_is_cygwin then (Path.current, [])
wenzelm@44435
   390
      else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
wenzelm@44435
   391
wenzelm@44435
   392
    val parents =
wenzelm@44435
   393
      imports |> map (fn import =>
wenzelm@44441
   394
        (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
wenzelm@44435
   395
          SOME thy => thy
wenzelm@44435
   396
        | NONE =>
wenzelm@44435
   397
            get_theory (Position.file_only import)
wenzelm@44435
   398
              (#2 (Future.join (the (AList.lookup (op =) deps import))))));
wenzelm@44435
   399
  in Thy_Load.begin_theory dir thy_name imports files parents end;
wenzelm@44435
   400
wenzelm@44480
   401
fun after_perspective node ((prev, _), _) = prev = #2 (get_perspective node);
wenzelm@44480
   402
fun needs_update node0 ((_, id), exec) = is_none exec orelse exec <> lookup_entry node0 id;
wenzelm@44480
   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@44480
   437
              | SOME (entry as ((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@44480
   443
                        val update_start =
wenzelm@44480
   444
                          Option.map (#2 o #1) (first_entry (SOME id) (needs_update node0) node);
wenzelm@44479
   445
wenzelm@44480
   446
                        val (execs, exec) =
wenzelm@44480
   447
                          ([], the_exec state (the_default_entry node prev))
wenzelm@44480
   448
                          |> not (after_perspective node entry) ?
wenzelm@44480
   449
                              fold_entries update_start
wenzelm@44480
   450
                                (fn entry1 as ((_, id1), _) => fn res =>
wenzelm@44480
   451
                                  if after_perspective node entry1 then NONE
wenzelm@44480
   452
                                  else SOME (new_exec state init id1 res)) node;
wenzelm@44479
   453
wenzelm@44479
   454
                        val node1 =
wenzelm@44476
   455
                          fold (fn (exec_id, (command_id, _)) => update_entry command_id exec_id)
wenzelm@44476
   456
                            execs node;
wenzelm@44479
   457
                        val last_exec = if #1 exec = no_id then NONE else SOME (#1 exec);
wenzelm@44476
   458
                        val result =
wenzelm@44479
   459
                          if is_some last_exec andalso is_none (next_entry node1 (the last_exec))
wenzelm@44479
   460
                          then Lazy.map #1 (#2 exec)
wenzelm@44476
   461
                          else no_result;
wenzelm@44479
   462
                        val node2 = node1
wenzelm@44479
   463
                          |> set_last_exec last_exec
wenzelm@44476
   464
                          |> set_result result
wenzelm@44444
   465
                          |> set_touched false;
wenzelm@44480
   466
wenzelm@44480
   467
                        val no_execs =
wenzelm@44480
   468
                          fold_entries update_start
wenzelm@44480
   469
                            (fn ((_, id0), exec0) => fn res =>
wenzelm@44480
   470
                              if is_none exec0 then NONE
wenzelm@44480
   471
                              else if is_some (lookup_entry node2 id0) then SOME res
wenzelm@44480
   472
                              else SOME (id0 :: res)) node0 []
wenzelm@44480
   473
                          handle Entries.UNDEFINED _ => [];
wenzelm@44480
   474
wenzelm@44479
   475
                      in ((no_execs, execs, [(name, node2)]), node2) end))
wenzelm@44479
   476
            end)
wenzelm@44479
   477
      |> Future.joins |> map #1;
wenzelm@44476
   478
wenzelm@44479
   479
    val command_execs =
wenzelm@44479
   480
      map (rpair NONE) (maps #1 updated) @
wenzelm@44479
   481
      map (fn (exec_id, (command_id, _)) => (command_id, SOME exec_id)) (maps #2 updated);
wenzelm@44479
   482
    val updated_nodes = maps #3 updated;
wenzelm@44476
   483
    val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
wenzelm@38418
   484
wenzelm@44197
   485
    val state' = state
wenzelm@44479
   486
      |> fold (fold define_exec o #2) updated
wenzelm@44476
   487
      |> define_version new_id (fold put_node updated_nodes new_version);
wenzelm@44479
   488
  in ((command_execs, last_execs), state') end;
wenzelm@38418
   489
wenzelm@38418
   490
end;
wenzelm@38418
   491
wenzelm@38418
   492
wenzelm@38418
   493
(* execute *)
wenzelm@38418
   494
wenzelm@38418
   495
fun execute version_id state =
wenzelm@41673
   496
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   497
    let
wenzelm@38418
   498
      val version = the_version state version_id;
wenzelm@38418
   499
wenzelm@44386
   500
      fun force_exec _ NONE = ()
wenzelm@44386
   501
        | force_exec node (SOME exec_id) =
wenzelm@44386
   502
            let
wenzelm@44386
   503
              val (command_id, exec) = the_exec state exec_id;
wenzelm@44386
   504
              val (_, print) = Lazy.force exec;
wenzelm@44386
   505
              val _ =
wenzelm@44439
   506
                if #1 (get_perspective node) command_id
wenzelm@44386
   507
                then ignore (Lazy.future Future.default_params print)
wenzelm@44386
   508
                else ();
wenzelm@44386
   509
            in () end;
wenzelm@38418
   510
wenzelm@44300
   511
      val execution = Future.new_group NONE;
wenzelm@44299
   512
      val _ =
wenzelm@44201
   513
        nodes_of version |> Graph.schedule
wenzelm@44201
   514
          (fn deps => fn (name, node) =>
wenzelm@44302
   515
            (singleton o Future.forks)
wenzelm@44302
   516
              {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
wenzelm@44302
   517
                deps = map (Future.task_of o #2) deps, pri = 1, interrupts = true}
wenzelm@44480
   518
              (fold_entries NONE (fn (_, exec) => fn () => SOME (force_exec node exec)) node));
wenzelm@40520
   519
wenzelm@44299
   520
    in (versions, commands, execs, execution) end);
wenzelm@38418
   521
wenzelm@43713
   522
wenzelm@43713
   523
wenzelm@43713
   524
(** global state **)
wenzelm@43713
   525
wenzelm@43713
   526
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   527
wenzelm@43713
   528
fun state () = Synchronized.value global_state;
wenzelm@43713
   529
val change_state = Synchronized.change global_state;
wenzelm@43713
   530
wenzelm@38418
   531
end;
wenzelm@38418
   532