src/Pure/PIDE/document.ML
author wenzelm
Mon Aug 15 16:38:42 2011 +0200 (2011-08-15 ago)
changeset 44197 458573968568
parent 44196 3588f71abb50
child 44198 a41ea419de68
permissions -rw-r--r--
refined Document.edit: less stateful update via Graph.schedule;
clarified node result -- more direct get_theory;
clarified Document.joint_commands;
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@44197
    81
fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force 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
val node_names_of = Graph.keys o nodes_of;
wenzelm@38418
   127
wenzelm@44185
   128
fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
wenzelm@44180
   129
wenzelm@44157
   130
fun edit_nodes (name, node_edit) (Version nodes) =
wenzelm@44157
   131
  Version
wenzelm@44157
   132
    (case node_edit of
wenzelm@44185
   133
      Clear => update_node name clear_node nodes
wenzelm@44180
   134
    | Edits edits => update_node name (edit_node edits) nodes
wenzelm@44185
   135
    | Header header =>
wenzelm@44185
   136
        let
wenzelm@44185
   137
          val node = get_node nodes name;
wenzelm@44185
   138
          val nodes' = Graph.new_node (name, node) (remove_node name nodes);
wenzelm@44185
   139
          val parents =
wenzelm@44185
   140
            (case header of Exn.Res (_, parents, _) => parents | _ => [])
wenzelm@44185
   141
            |> filter (can (Graph.get_node nodes'));
wenzelm@44185
   142
          val (header', nodes'') =
wenzelm@44185
   143
            (header, Graph.add_deps_acyclic (name, parents) nodes')
wenzelm@44185
   144
              handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes');
wenzelm@44185
   145
        in Graph.map_node name (set_header header') nodes'' end);
wenzelm@38418
   146
wenzelm@44197
   147
fun put_node (name, node) (Version nodes) =
wenzelm@40481
   148
  Version (nodes
wenzelm@40481
   149
    |> Graph.default_node (name, empty_node)
wenzelm@40481
   150
    |> Graph.map_node name (K node));
wenzelm@38418
   151
wenzelm@38150
   152
end;
wenzelm@38150
   153
wenzelm@38418
   154
wenzelm@38418
   155
wenzelm@38418
   156
(** global state -- document structure and execution process **)
wenzelm@38418
   157
wenzelm@38418
   158
abstype state = State of
wenzelm@40398
   159
 {versions: version Inttab.table,  (*version_id -> document content*)
wenzelm@38421
   160
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@44196
   161
  execs: Toplevel.state lazy Inttab.table,  (*exec_id -> execution process*)
wenzelm@40398
   162
  execution: unit future list}  (*global execution process*)
wenzelm@38418
   163
with
wenzelm@38418
   164
wenzelm@38418
   165
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   166
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   167
wenzelm@38418
   168
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   169
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   170
wenzelm@38418
   171
val init_state =
wenzelm@38418
   172
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38449
   173
    Inttab.make [(no_id, Future.value Toplevel.empty)],
wenzelm@44197
   174
    Inttab.make [(no_id, empty_exec)],
wenzelm@38418
   175
    []);
wenzelm@38418
   176
wenzelm@38418
   177
wenzelm@38418
   178
(* document versions *)
wenzelm@38418
   179
wenzelm@38418
   180
fun define_version (id: version_id) version =
wenzelm@38418
   181
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   182
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   183
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   184
    in (versions', commands, execs, execution) end);
wenzelm@38418
   185
wenzelm@38418
   186
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   187
  (case Inttab.lookup versions id of
wenzelm@38418
   188
    NONE => err_undef "document version" id
wenzelm@38418
   189
  | SOME version => version);
wenzelm@38418
   190
wenzelm@38418
   191
wenzelm@38418
   192
(* commands *)
wenzelm@38418
   193
wenzelm@38418
   194
fun define_command (id: command_id) text =
wenzelm@38418
   195
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   196
    let
wenzelm@38418
   197
      val id_string = print_id id;
wenzelm@38421
   198
      val tr = Future.fork_pri 2 (fn () =>
wenzelm@38418
   199
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@38421
   200
          (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
wenzelm@38418
   201
      val commands' =
wenzelm@38421
   202
        Inttab.update_new (id, tr) commands
wenzelm@38418
   203
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   204
    in (versions, commands', execs, execution) end);
wenzelm@38418
   205
wenzelm@38418
   206
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   207
  (case Inttab.lookup commands id of
wenzelm@38418
   208
    NONE => err_undef "command" id
wenzelm@38418
   209
  | SOME tr => tr);
wenzelm@38418
   210
wenzelm@38421
   211
fun join_commands (State {commands, ...}) =
wenzelm@38421
   212
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   213
wenzelm@38418
   214
wenzelm@38418
   215
(* command executions *)
wenzelm@38418
   216
wenzelm@44197
   217
fun define_exec (exec_id, exec) =
wenzelm@38418
   218
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@44197
   219
    let val execs' = Inttab.update_new (exec_id, exec) execs
wenzelm@38418
   220
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   221
    in (versions, commands, execs', execution) end);
wenzelm@38418
   222
wenzelm@44197
   223
fun the_exec (State {execs, ...}) exec_id =
wenzelm@44197
   224
  (case Inttab.lookup execs exec_id of
wenzelm@44197
   225
    NONE => err_undef "command execution" exec_id
wenzelm@38418
   226
  | SOME exec => exec);
wenzelm@38418
   227
wenzelm@41634
   228
wenzelm@41634
   229
(* document execution *)
wenzelm@41634
   230
wenzelm@43666
   231
fun cancel_execution (State {execution, ...}) =
wenzelm@43666
   232
  (List.app Future.cancel execution;
wenzelm@43666
   233
    fn () => ignore (Future.join_results execution));
wenzelm@41634
   234
wenzelm@38418
   235
end;
wenzelm@38418
   236
wenzelm@38418
   237
wenzelm@38418
   238
wenzelm@38888
   239
(* toplevel transactions *)
wenzelm@38888
   240
wenzelm@38888
   241
local
wenzelm@38888
   242
wenzelm@40391
   243
fun timing tr t = Toplevel.status tr (Markup.timing t);
wenzelm@40391
   244
wenzelm@38888
   245
fun proof_status tr st =
wenzelm@38888
   246
  (case try Toplevel.proof_of st of
wenzelm@38888
   247
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@38888
   248
  | NONE => ());
wenzelm@38888
   249
wenzelm@38888
   250
fun async_state tr st =
wenzelm@40534
   251
  ignore
wenzelm@41673
   252
    (singleton
wenzelm@41674
   253
      (Future.forks {name = "Document.async_state",
wenzelm@44113
   254
        group = SOME (Task_Queue.new_group NONE), deps = [], pri = 0, interrupts = true})
wenzelm@40534
   255
      (fn () =>
wenzelm@40534
   256
        Toplevel.setmp_thread_position tr
wenzelm@40534
   257
          (fn () => Toplevel.print_state false st) ()));
wenzelm@38888
   258
wenzelm@40390
   259
fun run int tr st =
wenzelm@40390
   260
  (case Toplevel.transition int tr st of
wenzelm@40390
   261
    SOME (st', NONE) => ([], SOME st')
wenzelm@40390
   262
  | SOME (_, SOME exn_info) =>
wenzelm@40390
   263
      (case ML_Compiler.exn_messages (Runtime.EXCURSION_FAIL exn_info) of
wenzelm@40390
   264
        [] => Exn.interrupt ()
wenzelm@40390
   265
      | errs => (errs, NONE))
wenzelm@40390
   266
  | NONE => ([ML_Compiler.exn_message Runtime.TERMINATE], NONE));
wenzelm@40390
   267
wenzelm@38888
   268
in
wenzelm@38888
   269
wenzelm@44185
   270
fun run_command (node_name, node_header) raw_tr st =
wenzelm@44185
   271
  let
wenzelm@44185
   272
    val is_init = Toplevel.is_init raw_tr;
wenzelm@44185
   273
    val tr =
wenzelm@44185
   274
      if is_init then
wenzelm@44186
   275
        raw_tr |> Toplevel.modify_init (fn () =>
wenzelm@44185
   276
          let
wenzelm@44185
   277
            (* FIXME get theories from document state *)
wenzelm@44185
   278
            (* FIXME provide files via Scala layer *)
wenzelm@44185
   279
            val (name, imports, uses) = Exn.release node_header;
wenzelm@44186
   280
            val master = Path.dir (Path.explode node_name);
wenzelm@44185
   281
          in Thy_Info.toplevel_begin_theory master name imports (map (apfst Path.explode) uses) end)
wenzelm@44185
   282
      else raw_tr;
wenzelm@40534
   283
wenzelm@44185
   284
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@44185
   285
    val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
wenzelm@44185
   286
wenzelm@44185
   287
    val start = Timing.start ();
wenzelm@44185
   288
    val (errs, result) =
wenzelm@44185
   289
      if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
wenzelm@44185
   290
      else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@44185
   291
    val _ = timing tr (Timing.result start);
wenzelm@44185
   292
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@44185
   293
  in
wenzelm@44185
   294
    (case result of
wenzelm@44196
   295
      NONE => (Toplevel.status tr Markup.failed; st)
wenzelm@44185
   296
    | SOME st' =>
wenzelm@44185
   297
       (Toplevel.status tr Markup.finished;
wenzelm@44185
   298
        proof_status tr st';
wenzelm@44185
   299
        if do_print then async_state tr st' else ();
wenzelm@44196
   300
        st'))
wenzelm@44185
   301
  end;
wenzelm@38888
   302
wenzelm@38888
   303
end;
wenzelm@38888
   304
wenzelm@38888
   305
wenzelm@38888
   306
wenzelm@38888
   307
wenzelm@38418
   308
(** editing **)
wenzelm@38418
   309
wenzelm@38418
   310
(* edit *)
wenzelm@38418
   311
wenzelm@38418
   312
local
wenzelm@38418
   313
wenzelm@38448
   314
fun is_changed node' ((_, id), exec) =
wenzelm@38418
   315
  (case try (the_entry node') id of
wenzelm@38418
   316
    NONE => true
wenzelm@38448
   317
  | SOME exec' => exec' <> exec);
wenzelm@38418
   318
wenzelm@44197
   319
fun new_exec node_info (command, command_id) (assigns, execs, exec) =
wenzelm@38418
   320
  let
wenzelm@38419
   321
    val exec_id' = new_id ();
wenzelm@38418
   322
    val exec' =
wenzelm@38418
   323
      Lazy.lazy (fn () =>
wenzelm@40398
   324
        let
wenzelm@44197
   325
          val tr = Toplevel.put_id (print_id exec_id') (Future.join command); (* FIXME Future.join_finished !? *)
wenzelm@44197
   326
          val st = Lazy.force exec;  (* FIXME Lazy.force_finished !? *)
wenzelm@44197
   327
        in run_command node_info tr st end);
wenzelm@44197
   328
  in ((command_id, exec_id') :: assigns, (exec_id', exec') :: execs, exec') end;
wenzelm@38418
   329
wenzelm@38418
   330
in
wenzelm@38418
   331
wenzelm@44157
   332
fun edit (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   333
  let
wenzelm@38418
   334
    val old_version = the_version state old_id;
wenzelm@44180
   335
    val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
wenzelm@38418
   336
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   337
wenzelm@44197
   338
    (* FIXME futures *)
wenzelm@44197
   339
    val updates =
wenzelm@44197
   340
      nodes_of new_version |> Graph.schedule
wenzelm@44197
   341
        (fn _ => fn (name, node) =>
wenzelm@44197
   342
          (case first_entry NONE (is_changed (node_of old_version name)) node of
wenzelm@44197
   343
            NONE => (([], [], []), node)
wenzelm@44197
   344
          | SOME ((prev, id), _) =>
wenzelm@44197
   345
              let
wenzelm@44197
   346
                val prev_exec =
wenzelm@44197
   347
                  (case prev of
wenzelm@44197
   348
                    NONE => no_id
wenzelm@44197
   349
                  | SOME prev_id => the_default no_id (the_entry node prev_id));
wenzelm@44197
   350
                val (assigns, execs, result) =
wenzelm@44197
   351
                  fold_entries (SOME id)
wenzelm@44197
   352
                    (new_exec (name, get_header node) o `(the_command state) o #2 o #1)
wenzelm@44197
   353
                      node ([], [], the_exec state prev_exec);
wenzelm@44197
   354
                val node' = node |> fold update_entry assigns |> set_result result;
wenzelm@44197
   355
              in ((assigns, execs, [(name, node')]), node') end))
wenzelm@44197
   356
      |> map #1;
wenzelm@38418
   357
wenzelm@44197
   358
    val state' = state
wenzelm@44197
   359
      |> fold (fold define_exec o #2) updates
wenzelm@44197
   360
      |> define_version new_id (fold (fold put_node o #3) updates new_version);
wenzelm@38421
   361
wenzelm@44197
   362
  in (maps #1 (rev updates), state') end;
wenzelm@38418
   363
wenzelm@38418
   364
end;
wenzelm@38418
   365
wenzelm@38418
   366
wenzelm@38418
   367
(* execute *)
wenzelm@38418
   368
wenzelm@38418
   369
fun execute version_id state =
wenzelm@41673
   370
  state |> map_state (fn (versions, commands, execs, _) =>
wenzelm@38418
   371
    let
wenzelm@38418
   372
      val version = the_version state version_id;
wenzelm@38418
   373
wenzelm@38418
   374
      fun force_exec NONE = ()
wenzelm@38418
   375
        | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
wenzelm@38418
   376
wenzelm@44180
   377
      val execution' = (* FIXME Graph.schedule *)
wenzelm@44113
   378
        Future.forks
wenzelm@44113
   379
          {name = "Document.execute", group = NONE, deps = [], pri = 1, interrupts = true}
wenzelm@41673
   380
          [fn () =>
wenzelm@43666
   381
            node_names_of version |> List.app (fn name =>
wenzelm@43666
   382
              fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
wenzelm@44185
   383
                  (node_of version name) ())];
wenzelm@40520
   384
wenzelm@38418
   385
    in (versions, commands, execs, execution') end);
wenzelm@38418
   386
wenzelm@43713
   387
wenzelm@43713
   388
wenzelm@43713
   389
(** global state **)
wenzelm@43713
   390
wenzelm@43713
   391
val global_state = Synchronized.var "Document" init_state;
wenzelm@43713
   392
wenzelm@43713
   393
fun state () = Synchronized.value global_state;
wenzelm@43713
   394
val change_state = Synchronized.change global_state;
wenzelm@43713
   395
wenzelm@38418
   396
end;
wenzelm@38418
   397