src/Pure/PIDE/document.ML
author wenzelm
Sun Aug 15 18:41:23 2010 +0200 (2010-08-15)
changeset 38418 9a7af64d71bb
parent 38414 49f1f657adc2
child 38419 f9dc924e54f8
permissions -rw-r--r--
more explicit / functional ML version of document model;
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@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@38418
    15
  val create_id: unit -> id
wenzelm@38363
    16
  val parse_id: string -> id
wenzelm@38363
    17
  val print_id: id -> string
wenzelm@38150
    18
  type edit = string * ((command_id * 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@38418
    41
  fun create_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@38418
    58
abstype entry = Entry of {next: command_id option, exec: exec_id option}
wenzelm@38418
    59
  and node = Node of entry Inttab.table  (*unique entries indexed by command_id, start with no_id*)
wenzelm@38418
    60
  and version = Version of node Graph.T  (*development graph wrt. static imports*)
wenzelm@38418
    61
with
wenzelm@38418
    62
wenzelm@38418
    63
wenzelm@38418
    64
(* command entries *)
wenzelm@38418
    65
wenzelm@38418
    66
fun make_entry next exec = Entry {next = next, exec = exec};
wenzelm@38418
    67
wenzelm@38418
    68
fun the_entry (Node entries) (id: command_id) =
wenzelm@38418
    69
  (case Inttab.lookup entries id of
wenzelm@38418
    70
    NONE => err_undef "command entry" id
wenzelm@38418
    71
  | SOME (Entry entry) => entry);
wenzelm@38418
    72
wenzelm@38418
    73
fun put_entry (id: command_id, entry: entry) (Node entries) =
wenzelm@38418
    74
  Node (Inttab.update (id, entry) entries);
wenzelm@38418
    75
wenzelm@38418
    76
fun put_entry_exec (id: command_id) exec node =
wenzelm@38418
    77
  let val {next, ...} = the_entry node id
wenzelm@38418
    78
  in put_entry (id, make_entry next exec) node end;
wenzelm@38418
    79
wenzelm@38418
    80
fun reset_entry_exec id = put_entry_exec id NONE;
wenzelm@38418
    81
fun set_entry_exec (id, exec_id) = put_entry_exec id (SOME exec_id);
wenzelm@38418
    82
wenzelm@38418
    83
wenzelm@38418
    84
(* iterate entries *)
wenzelm@38418
    85
wenzelm@38418
    86
fun fold_entries id0 f (node as Node entries) =
wenzelm@38418
    87
  let
wenzelm@38418
    88
    fun apply NONE x = x
wenzelm@38418
    89
      | apply (SOME id) x =
wenzelm@38418
    90
          let val entry = the_entry node id
wenzelm@38418
    91
          in apply (#next entry) (f (id, entry) x) end;
wenzelm@38418
    92
  in if Inttab.defined entries id0 then apply (SOME id0) else I end;
wenzelm@38418
    93
wenzelm@38418
    94
fun first_entry P node =
wenzelm@38418
    95
  let
wenzelm@38418
    96
    fun first _ NONE = NONE
wenzelm@38418
    97
      | first prev (SOME id) =
wenzelm@38418
    98
          let val entry = the_entry node id
wenzelm@38418
    99
          in if P (id, entry) then SOME (prev, id, entry) else first (SOME id) (#next entry) end;
wenzelm@38418
   100
  in first NONE (SOME no_id) end;
wenzelm@38418
   101
wenzelm@38418
   102
wenzelm@38418
   103
(* modify entries *)
wenzelm@38418
   104
wenzelm@38418
   105
fun insert_after (id: command_id) (id2: command_id) node =
wenzelm@38418
   106
  let val {next, exec} = the_entry node id in
wenzelm@38418
   107
    node
wenzelm@38418
   108
    |> put_entry (id, make_entry (SOME id2) exec)
wenzelm@38418
   109
    |> put_entry (id2, make_entry next NONE)
wenzelm@38418
   110
  end;
wenzelm@38418
   111
wenzelm@38418
   112
fun delete_after (id: command_id) node =
wenzelm@38418
   113
  let val {next, exec} = the_entry node id in
wenzelm@38418
   114
    (case next of
wenzelm@38418
   115
      NONE => error ("No next entry to delete: " ^ print_id id)
wenzelm@38418
   116
    | SOME id2 =>
wenzelm@38418
   117
        node |>
wenzelm@38418
   118
          (case #next (the_entry node id2) of
wenzelm@38418
   119
            NONE => put_entry (id, make_entry NONE exec)
wenzelm@38418
   120
          | SOME id3 => put_entry (id, make_entry (SOME id3) exec) #> reset_entry_exec id3))
wenzelm@38418
   121
  end;
wenzelm@38418
   122
wenzelm@38418
   123
wenzelm@38418
   124
(* node edits *)
wenzelm@38150
   125
wenzelm@38150
   126
type edit =
wenzelm@38150
   127
  string *  (*node name*)
wenzelm@38150
   128
  ((command_id * command_id option) list) option;  (*NONE: remove, SOME: insert/remove commands*)
wenzelm@38150
   129
wenzelm@38418
   130
val empty_node = Node (Inttab.make [(no_id, make_entry NONE (SOME no_id))]);
wenzelm@38418
   131
wenzelm@38418
   132
fun edit_node (id, SOME id2) = insert_after id id2
wenzelm@38418
   133
  | edit_node (id, NONE) = delete_after id;
wenzelm@38418
   134
wenzelm@38418
   135
wenzelm@38418
   136
(* version operations *)
wenzelm@38418
   137
wenzelm@38418
   138
fun nodes_of (Version nodes) = nodes;
wenzelm@38418
   139
val node_names_of = Graph.keys o nodes_of;
wenzelm@38418
   140
wenzelm@38418
   141
fun edit_nodes (name, SOME edits) (Version nodes) =
wenzelm@38418
   142
      Version (nodes
wenzelm@38418
   143
        |> Graph.default_node (name, empty_node)
wenzelm@38418
   144
        |> Graph.map_node name (fold edit_node edits))
wenzelm@38418
   145
  | edit_nodes (name, NONE) (Version nodes) = Version (Graph.del_node name nodes);
wenzelm@38418
   146
wenzelm@38418
   147
val empty_version = Version Graph.empty;
wenzelm@38418
   148
wenzelm@38418
   149
fun the_node version name =
wenzelm@38418
   150
  Graph.get_node (nodes_of version) name handle Graph.UNDEF _ => empty_node;
wenzelm@38418
   151
wenzelm@38418
   152
fun put_node name node (Version nodes) =
wenzelm@38418
   153
  Version (Graph.map_node name (K node) nodes);  (* FIXME Graph.UNDEF !? *)
wenzelm@38418
   154
wenzelm@38150
   155
end;
wenzelm@38150
   156
wenzelm@38418
   157
wenzelm@38418
   158
wenzelm@38418
   159
(** global state -- document structure and execution process **)
wenzelm@38418
   160
wenzelm@38418
   161
abstype state = State of
wenzelm@38418
   162
 {versions: version Inttab.table,                   (*version_id -> document content*)
wenzelm@38418
   163
  commands: Toplevel.transition Inttab.table,       (*command_id -> transition function*)
wenzelm@38418
   164
  execs: Toplevel.state option lazy Inttab.table,   (*exec_id -> execution process*)
wenzelm@38418
   165
  execution: unit future list}                      (*global execution process*)
wenzelm@38418
   166
with
wenzelm@38418
   167
wenzelm@38418
   168
fun make_state (versions, commands, execs, execution) =
wenzelm@38418
   169
  State {versions = versions, commands = commands, execs = execs, execution = execution};
wenzelm@38418
   170
wenzelm@38418
   171
fun map_state f (State {versions, commands, execs, execution}) =
wenzelm@38418
   172
  make_state (f (versions, commands, execs, execution));
wenzelm@38418
   173
wenzelm@38418
   174
val init_state =
wenzelm@38418
   175
  make_state (Inttab.make [(no_id, empty_version)],
wenzelm@38418
   176
    Inttab.make [(no_id, Toplevel.empty)],
wenzelm@38418
   177
    Inttab.make [(no_id, Lazy.value (SOME Toplevel.toplevel))],
wenzelm@38418
   178
    []);
wenzelm@38418
   179
wenzelm@38418
   180
wenzelm@38418
   181
(* document versions *)
wenzelm@38418
   182
wenzelm@38418
   183
fun define_version (id: version_id) version =
wenzelm@38418
   184
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   185
    let val versions' = Inttab.update_new (id, version) versions
wenzelm@38418
   186
      handle Inttab.DUP dup => err_dup "document version" dup
wenzelm@38418
   187
    in (versions', commands, execs, execution) end);
wenzelm@38418
   188
wenzelm@38418
   189
fun the_version (State {versions, ...}) (id: version_id) =
wenzelm@38418
   190
  (case Inttab.lookup versions id of
wenzelm@38418
   191
    NONE => err_undef "document version" id
wenzelm@38418
   192
  | SOME version => version);
wenzelm@38418
   193
wenzelm@38418
   194
wenzelm@38418
   195
(* commands *)
wenzelm@38418
   196
wenzelm@38418
   197
fun define_command (id: command_id) text =
wenzelm@38418
   198
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   199
    let
wenzelm@38418
   200
      val id_string = print_id id;
wenzelm@38418
   201
      val tr =
wenzelm@38418
   202
        Position.setmp_thread_data (Position.id_only id_string)
wenzelm@38418
   203
          (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ();
wenzelm@38418
   204
      val commands' =
wenzelm@38418
   205
        Inttab.update_new (id, Toplevel.put_id id_string tr) commands
wenzelm@38418
   206
          handle Inttab.DUP dup => err_dup "command" dup;
wenzelm@38418
   207
    in (versions, commands', execs, execution) end);
wenzelm@38418
   208
wenzelm@38418
   209
fun the_command (State {commands, ...}) (id: command_id) =
wenzelm@38418
   210
  (case Inttab.lookup commands id of
wenzelm@38418
   211
    NONE => err_undef "command" id
wenzelm@38418
   212
  | SOME tr => tr);
wenzelm@38418
   213
wenzelm@38418
   214
wenzelm@38418
   215
(* command executions *)
wenzelm@38418
   216
wenzelm@38418
   217
fun define_exec (id: exec_id) exec =
wenzelm@38418
   218
  map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   219
    let val execs' = Inttab.update_new (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@38418
   223
fun the_exec (State {execs, ...}) (id: exec_id) =
wenzelm@38418
   224
  (case Inttab.lookup execs id of
wenzelm@38418
   225
    NONE => err_undef "command execution" id
wenzelm@38418
   226
  | SOME exec => exec);
wenzelm@38418
   227
wenzelm@38418
   228
end;
wenzelm@38418
   229
wenzelm@38418
   230
wenzelm@38418
   231
wenzelm@38418
   232
(** editing **)
wenzelm@38418
   233
wenzelm@38418
   234
(* edit *)
wenzelm@38418
   235
wenzelm@38418
   236
local
wenzelm@38418
   237
wenzelm@38418
   238
fun is_changed node' (id, {next = _, exec}) =
wenzelm@38418
   239
  (case try (the_entry node') id of
wenzelm@38418
   240
    NONE => true
wenzelm@38418
   241
  | SOME {next = _, exec = exec'} => exec' <> exec);
wenzelm@38418
   242
wenzelm@38418
   243
fun new_exec name (id: command_id) (exec_id, updates, state) =
wenzelm@38418
   244
  let
wenzelm@38418
   245
    val exec = the_exec state exec_id;
wenzelm@38418
   246
    val exec_id' = create_id ();
wenzelm@38418
   247
    val tr = Toplevel.put_id (print_id exec_id') (the_command state id);
wenzelm@38418
   248
    val exec' =
wenzelm@38418
   249
      Lazy.lazy (fn () =>
wenzelm@38418
   250
        (case Lazy.force exec of
wenzelm@38418
   251
          NONE => NONE
wenzelm@38418
   252
        | SOME st => Toplevel.run_command name tr st));
wenzelm@38418
   253
    val state' = define_exec exec_id' exec' state;
wenzelm@38418
   254
  in (exec_id', (id, exec_id') :: updates, state') end;
wenzelm@38418
   255
wenzelm@38418
   256
in
wenzelm@38418
   257
wenzelm@38418
   258
fun edit (old_id: version_id) (new_id: version_id) edits state =
wenzelm@38418
   259
  let
wenzelm@38418
   260
    val old_version = the_version state old_id;
wenzelm@38418
   261
    val new_version = fold edit_nodes edits old_version;
wenzelm@38418
   262
wenzelm@38418
   263
    fun update_node name (rev_updates, version, st) =
wenzelm@38418
   264
      let val node = the_node version name in
wenzelm@38418
   265
        (case first_entry (is_changed (the_node old_version name)) node of
wenzelm@38418
   266
          NONE => (rev_updates, version, st)
wenzelm@38418
   267
        | SOME (prev, id, _) =>
wenzelm@38418
   268
            let
wenzelm@38418
   269
              val prev_exec = the (#exec (the_entry node (the prev)));
wenzelm@38418
   270
              val (_, rev_upds, st') =
wenzelm@38418
   271
                fold_entries id (new_exec name o #1) node (prev_exec, [], st);
wenzelm@38418
   272
              val node' = fold set_entry_exec rev_upds node;
wenzelm@38418
   273
            in (rev_upds @ rev_updates, put_node name node' version, st') end)
wenzelm@38418
   274
      end;
wenzelm@38418
   275
wenzelm@38418
   276
    (* FIXME proper node deps *)
wenzelm@38418
   277
    val (rev_updates, new_version', state') =
wenzelm@38418
   278
      fold update_node (node_names_of new_version) ([], new_version, state);
wenzelm@38418
   279
    val state'' = define_version new_id new_version' state';
wenzelm@38418
   280
  in (rev rev_updates, state'') end;
wenzelm@38418
   281
wenzelm@38418
   282
end;
wenzelm@38418
   283
wenzelm@38418
   284
wenzelm@38418
   285
(* execute *)
wenzelm@38418
   286
wenzelm@38418
   287
fun execute version_id state =
wenzelm@38418
   288
  state |> map_state (fn (versions, commands, execs, execution) =>
wenzelm@38418
   289
    let
wenzelm@38418
   290
      val version = the_version state version_id;
wenzelm@38418
   291
wenzelm@38418
   292
      fun force_exec NONE = ()
wenzelm@38418
   293
        | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
wenzelm@38418
   294
wenzelm@38418
   295
      val _ = List.app Future.cancel execution;
wenzelm@38418
   296
      fun await_cancellation () = uninterruptible (fn _ => Future.join_results) execution;
wenzelm@38418
   297
wenzelm@38418
   298
      val execution' = (* FIXME proper node deps *)
wenzelm@38418
   299
        node_names_of version |> map (fn name =>
wenzelm@38418
   300
          Future.fork_pri 1 (fn () =>
wenzelm@38418
   301
            (await_cancellation ();
wenzelm@38418
   302
              fold_entries no_id (fn (_, {exec, ...}) => fn () => force_exec exec)
wenzelm@38418
   303
                (the_node version name) ())));
wenzelm@38418
   304
    in (versions, commands, execs, execution') end);
wenzelm@38418
   305
wenzelm@38418
   306
end;
wenzelm@38418
   307