src/Pure/PIDE/document.ML
author wenzelm
Tue Aug 17 15:10:49 2010 +0200 (2010-08-17)
changeset 38448 62d16c415019
parent 38421 6cfc6fce7bfb
child 38449 2eb6bad282b1
permissions -rw-r--r--
added functor Linear_Set, based on former adhoc structures in document.ML;
Linear_Set.delete_after (ML): actually delete table entries;
Scala Linear_Set: clarified exceptions, according to ML version;
simplified Document.node using Linear_Set;
ML Document.edit: refer to start via NONE instead of no_id, according to Scala version;
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@38448
    18
  type edit = string * ((command_id option * command_id option) list) option
wenzelm@38418
    19
  type state
wenzelm@38418
    20
  val init_state: state
wenzelm@38418
    21
  val define_command: command_id -> string -> state -> state
wenzelm@38418
    22
  val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
wenzelm@38418
    23
  val execute: version_id -> state -> state
wenzelm@38150
    24
end;
wenzelm@38150
    25
wenzelm@38150
    26
structure Document: DOCUMENT =
wenzelm@38150
    27
struct
wenzelm@38150
    28
wenzelm@38150
    29
(* unique identifiers *)
wenzelm@38150
    30
wenzelm@38363
    31
type id = int;
wenzelm@38373
    32
type version_id = id;
wenzelm@38363
    33
type command_id = id;
wenzelm@38373
    34
type exec_id = id;
wenzelm@38355
    35
wenzelm@38355
    36
val no_id = 0;
wenzelm@38150
    37
wenzelm@38418
    38
local
wenzelm@38418
    39
  val id_count = Synchronized.var "id" 0;
wenzelm@38418
    40
in
wenzelm@38419
    41
  fun new_id () =
wenzelm@38418
    42
    Synchronized.change_result id_count
wenzelm@38418
    43
      (fn i =>
wenzelm@38418
    44
        let val i' = i + 1
wenzelm@38418
    45
        in (i', i') end);
wenzelm@38418
    46
end;
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@38448
    58
structure Entries = Linear_Set(type key = command_id val ord = int_ord);
wenzelm@38448
    59
wenzelm@38448
    60
abstype node = Node of exec_id option Entries.T  (*command entries with excecutions*)
wenzelm@38418
    61
  and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    62
with
wenzelm@38418
    63
wenzelm@38448
    64
val empty_node = Node Entries.empty;
wenzelm@38448
    65
val empty_version = Version Graph.empty;
wenzelm@38418
    66
wenzelm@38448
    67
fun fold_entries start f (Node entries) = Entries.fold start f entries;
wenzelm@38448
    68
fun first_entry start P (Node entries) = Entries.get_first start P entries;
wenzelm@38418
    69
wenzelm@38418
    70
wenzelm@38448
    71
(* node edits and associated executions *)
wenzelm@38150
    72
wenzelm@38150
    73
type edit =
wenzelm@38448
    74
  string *
wenzelm@38448
    75
  (*NONE: remove node, SOME: insert/remove commands*)
wenzelm@38448
    76
  ((command_id option * command_id option) list) option;
wenzelm@38448
    77
wenzelm@38448
    78
fun the_entry (Node entries) id =
wenzelm@38448
    79
  (case Entries.lookup entries id of
wenzelm@38448
    80
    NONE => err_undef "command entry" id
wenzelm@38448
    81
  | SOME entry => entry);
wenzelm@38150
    82
wenzelm@38448
    83
fun update_entry (id, exec_id) (Node entries) =
wenzelm@38448
    84
  Node (Entries.update (id, SOME exec_id) entries);
wenzelm@38418
    85
wenzelm@38448
    86
fun reset_after id entries =
wenzelm@38448
    87
  (case Entries.get_after entries id of
wenzelm@38448
    88
    NONE => entries
wenzelm@38448
    89
  | SOME next => Entries.update (next, NONE) entries);
wenzelm@38448
    90
wenzelm@38448
    91
fun edit_node (hook, SOME id2) (Node entries) =
wenzelm@38448
    92
      Node (Entries.insert_after hook (id2, NONE) entries)
wenzelm@38448
    93
  | edit_node (hook, NONE) (Node entries) =
wenzelm@38448
    94
      Node (entries |> Entries.delete_after hook |> reset_after hook);
wenzelm@38418
    95
wenzelm@38418
    96
wenzelm@38418
    97
(* version operations *)
wenzelm@38418
    98
wenzelm@38418
    99
fun nodes_of (Version nodes) = nodes;
wenzelm@38418
   100
val node_names_of = Graph.keys o nodes_of;
wenzelm@38418
   101
wenzelm@38448
   102
fun get_node version name = Graph.get_node (nodes_of version) name
wenzelm@38448
   103
  handle Graph.UNDEF _ => empty_node;
wenzelm@38448
   104
wenzelm@38418
   105
fun edit_nodes (name, SOME edits) (Version nodes) =
wenzelm@38418
   106
      Version (nodes
wenzelm@38418
   107
        |> Graph.default_node (name, empty_node)
wenzelm@38418
   108
        |> Graph.map_node name (fold edit_node edits))
wenzelm@38448
   109
  | edit_nodes (name, NONE) (Version nodes) =
wenzelm@38448
   110
      Version (Graph.del_node name nodes);  (* FIXME Graph.UNDEF !? *)
wenzelm@38418
   111
wenzelm@38418
   112
fun put_node name node (Version nodes) =
wenzelm@38418
   113
  Version (Graph.map_node name (K node) nodes);  (* FIXME Graph.UNDEF !? *)
wenzelm@38418
   114
wenzelm@38150
   115
end;
wenzelm@38150
   116
wenzelm@38418
   117
wenzelm@38418
   118
wenzelm@38418
   119
(** global state -- document structure and execution process **)
wenzelm@38418
   120
wenzelm@38418
   121
abstype state = State of
wenzelm@38421
   122
 {versions: version Inttab.table,                     (*version_id -> document content*)
wenzelm@38421
   123
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
wenzelm@38421
   124
  execs: Toplevel.state option lazy Inttab.table,     (*exec_id -> execution process*)
wenzelm@38421
   125
  execution: unit future list}                        (*global execution process*)
wenzelm@38418
   126
with
wenzelm@38418
   127
wenzelm@38418
   128
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   129
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   130
wenzelm@38418
   131
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   132
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   133
wenzelm@38418
   134
val init_state =
wenzelm@38418
   135
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38448
   136
    Inttab.make [(no_id, Future.value Toplevel.empty)],          (* FIXME no_id !?? *)
wenzelm@38448
   137
    Inttab.make [(no_id, Lazy.value (SOME Toplevel.toplevel))],  (* FIXME no_id !?? *)
wenzelm@38418
   138
    []);
wenzelm@38418
   139
wenzelm@38418
   140
wenzelm@38418
   141
(* document versions *)
wenzelm@38418
   142
wenzelm@38418
   143
fun define_version (id: version_id) version =
wenzelm@38418
   144
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   145
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   146
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   147
    in (versions', commands, execs, execution) end);
wenzelm@38418
   148
wenzelm@38418
   149
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   150
  (case Inttab.lookup versions id of
wenzelm@38418
   151
    NONE => err_undef "document version" id
wenzelm@38418
   152
  | SOME version => version);
wenzelm@38418
   153
wenzelm@38418
   154
wenzelm@38418
   155
(* commands *)
wenzelm@38418
   156
wenzelm@38418
   157
fun define_command (id: command_id) text =
wenzelm@38418
   158
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   159
    let
wenzelm@38418
   160
      val id_string = print_id id;
wenzelm@38421
   161
      val tr = Future.fork_pri 2 (fn () =>
wenzelm@38418
   162
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@38421
   163
          (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
wenzelm@38418
   164
      val commands' =
wenzelm@38421
   165
        Inttab.update_new (id, tr) commands
wenzelm@38418
   166
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   167
    in (versions, commands', execs, execution) end);
wenzelm@38418
   168
wenzelm@38418
   169
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   170
  (case Inttab.lookup commands id of
wenzelm@38418
   171
    NONE => err_undef "command" id
wenzelm@38418
   172
  | SOME tr => tr);
wenzelm@38418
   173
wenzelm@38421
   174
fun join_commands (State {commands, ...}) =
wenzelm@38421
   175
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
wenzelm@38421
   176
wenzelm@38418
   177
wenzelm@38418
   178
(* command executions *)
wenzelm@38418
   179
wenzelm@38418
   180
fun define_exec (id: exec_id) exec =
wenzelm@38418
   181
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   182
    let val execs' = Inttab.update_new (id, exec) execs
wenzelm@38418
   183
      handle Inttab.DUP dup => err_dup "command execution" dup
wenzelm@38418
   184
    in (versions, commands, execs', execution) end);
wenzelm@38418
   185
wenzelm@38418
   186
fun the_exec (State {execs, ...}) (id: exec_id) =
wenzelm@38418
   187
  (case Inttab.lookup execs id of
wenzelm@38418
   188
    NONE => err_undef "command execution" id
wenzelm@38418
   189
  | SOME exec => exec);
wenzelm@38418
   190
wenzelm@38418
   191
end;
wenzelm@38418
   192
wenzelm@38418
   193
wenzelm@38418
   194
wenzelm@38418
   195
(** editing **)
wenzelm@38418
   196
wenzelm@38418
   197
(* edit *)
wenzelm@38418
   198
wenzelm@38418
   199
local
wenzelm@38418
   200
wenzelm@38448
   201
fun is_changed node' ((_, id), exec) =
wenzelm@38418
   202
  (case try (the_entry node') id of
wenzelm@38418
   203
    NONE => true
wenzelm@38448
   204
  | SOME exec' => exec' <> exec);
wenzelm@38418
   205
wenzelm@38418
   206
fun new_exec name (id: command_id) (exec_id, updates, state) =
wenzelm@38418
   207
  let
wenzelm@38418
   208
    val exec = the_exec state exec_id;
wenzelm@38419
   209
    val exec_id' = new_id ();
wenzelm@38421
   210
    val tr = the_command state id;
wenzelm@38418
   211
    val exec' =
wenzelm@38418
   212
      Lazy.lazy (fn () =>
wenzelm@38418
   213
        (case Lazy.force exec of
wenzelm@38418
   214
          NONE => NONE
wenzelm@38421
   215
        | SOME st =>
wenzelm@38421
   216
            let val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join tr)
wenzelm@38421
   217
            in Toplevel.run_command name exec_tr st end));
wenzelm@38418
   218
    val state' = define_exec exec_id' exec' state;
wenzelm@38418
   219
  in (exec_id', (id, exec_id') :: updates, state') end;
wenzelm@38418
   220
wenzelm@38418
   221
in
wenzelm@38418
   222
wenzelm@38418
   223
fun edit (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   224
  let
wenzelm@38418
   225
    val old_version = the_version state old_id;
wenzelm@38418
   226
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   227
wenzelm@38418
   228
    fun update_node name (rev_updates, version, st) =
wenzelm@38448
   229
      let val node = get_node version name in
wenzelm@38448
   230
        (case first_entry NONE (is_changed (get_node old_version name)) node of
wenzelm@38418
   231
          NONE => (rev_updates, version, st)
wenzelm@38448
   232
        | SOME ((prev, id), _) =>
wenzelm@38418
   233
            let
wenzelm@38448
   234
              val prev_exec =
wenzelm@38448
   235
                (case prev of
wenzelm@38448
   236
                  NONE => no_id
wenzelm@38448
   237
                | SOME prev_id => the_default no_id (the_entry node prev_id));
wenzelm@38418
   238
              val (_, rev_upds, st') =
wenzelm@38448
   239
                fold_entries (SOME id) (new_exec name o #2 o #1) node (prev_exec, [], st);
wenzelm@38448
   240
              val node' = fold update_entry rev_upds node;
wenzelm@38418
   241
            in (rev_upds @ rev_updates, put_node name node' version, st') end)
wenzelm@38418
   242
      end;
wenzelm@38418
   243
wenzelm@38418
   244
    (* FIXME proper node deps *)
wenzelm@38418
   245
    val (rev_updates, new_version', state') =
wenzelm@38418
   246
      fold update_node (node_names_of new_version) ([], new_version, state);
wenzelm@38418
   247
    val state'' = define_version new_id new_version' state';
wenzelm@38421
   248
wenzelm@38421
   249
    val _ = join_commands state'';  (* FIXME async!? *)
wenzelm@38418
   250
  in (rev rev_updates, state'') end;
wenzelm@38418
   251
wenzelm@38418
   252
end;
wenzelm@38418
   253
wenzelm@38418
   254
wenzelm@38418
   255
(* execute *)
wenzelm@38418
   256
wenzelm@38418
   257
fun execute version_id state =
wenzelm@38418
   258
  state |> map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   259
    let
wenzelm@38418
   260
      val version = the_version state version_id;
wenzelm@38418
   261
wenzelm@38418
   262
      fun force_exec NONE = ()
wenzelm@38418
   263
        | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
wenzelm@38418
   264
wenzelm@38418
   265
      val _ = List.app Future.cancel execution;
wenzelm@38418
   266
      fun await_cancellation () = uninterruptible (fn _ => Future.join_results) execution;
wenzelm@38418
   267
wenzelm@38418
   268
      val execution' = (* FIXME proper node deps *)
wenzelm@38418
   269
        node_names_of version |> map (fn name =>
wenzelm@38418
   270
          Future.fork_pri 1 (fn () =>
wenzelm@38418
   271
            (await_cancellation ();
wenzelm@38448
   272
              fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
wenzelm@38448
   273
                (get_node version name) ())));
wenzelm@38418
   274
    in (versions, commands, execs, execution') end);
wenzelm@38418
   275
wenzelm@38418
   276
end;
wenzelm@38418
   277