src/Pure/PIDE/document.ML
author wenzelm
Sun, 15 Aug 2010 20:13:07 +0200
changeset 38421 6cfc6fce7bfb
parent 38419 f9dc924e54f8
child 38448 62d16c415019
permissions -rw-r--r--
document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/PIDE/document.ML
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     3
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     4
Document as collection of named nodes, each consisting of an editable
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
     5
list of commands, associated with asynchronous execution process.
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     6
*)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     7
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     8
signature DOCUMENT =
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
     9
sig
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    10
  type id = int
38373
wenzelm
parents: 38363
diff changeset
    11
  type version_id = id
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    12
  type command_id = id
38373
wenzelm
parents: 38363
diff changeset
    13
  type exec_id = id
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    14
  val no_id: id
38419
f9dc924e54f8 renamed create_id to new_id;
wenzelm
parents: 38418
diff changeset
    15
  val new_id: unit -> id
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    16
  val parse_id: string -> id
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    17
  val print_id: id -> string
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    18
  type edit = string * ((command_id * command_id option) list) option
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    19
  type state
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    20
  val init_state: state
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    21
  val define_command: command_id -> string -> state -> state
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    22
  val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    23
  val execute: version_id -> state -> state
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    24
end;
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    25
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    26
structure Document: DOCUMENT =
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    27
struct
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    28
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    29
(* unique identifiers *)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    30
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    31
type id = int;
38373
wenzelm
parents: 38363
diff changeset
    32
type version_id = id;
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38355
diff changeset
    33
type command_id = id;
38373
wenzelm
parents: 38363
diff changeset
    34
type exec_id = id;
38355
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38150
diff changeset
    35
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38150
diff changeset
    36
val no_id = 0;
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    37
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    38
local
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    39
  val id_count = Synchronized.var "id" 0;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    40
in
38419
f9dc924e54f8 renamed create_id to new_id;
wenzelm
parents: 38418
diff changeset
    41
  fun new_id () =
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    42
    Synchronized.change_result id_count
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    43
      (fn i =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    44
        let val i' = i + 1
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    45
        in (i', i') end);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    46
end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    47
38414
49f1f657adc2 more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents: 38373
diff changeset
    48
val parse_id = Markup.parse_int;
49f1f657adc2 more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents: 38373
diff changeset
    49
val print_id = Markup.print_int;
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    50
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    51
fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    52
fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
    53
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    54
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    55
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    56
(** document structure **)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    57
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    58
abstype entry = Entry of {next: command_id option, exec: exec_id option}
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    59
  and node = Node of entry Inttab.table  (*unique entries indexed by command_id, start with no_id*)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    60
  and version = Version of node Graph.T  (*development graph wrt. static imports*)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    61
with
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    62
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    63
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    64
(* command entries *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    65
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    66
fun make_entry next exec = Entry {next = next, exec = exec};
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    67
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    68
fun the_entry (Node entries) (id: command_id) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    69
  (case Inttab.lookup entries id of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    70
    NONE => err_undef "command entry" id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    71
  | SOME (Entry entry) => entry);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    72
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    73
fun put_entry (id: command_id, entry: entry) (Node entries) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    74
  Node (Inttab.update (id, entry) entries);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    75
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    76
fun put_entry_exec (id: command_id) exec node =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    77
  let val {next, ...} = the_entry node id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    78
  in put_entry (id, make_entry next exec) node end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    79
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    80
fun reset_entry_exec id = put_entry_exec id NONE;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    81
fun set_entry_exec (id, exec_id) = put_entry_exec id (SOME exec_id);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    82
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    83
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    84
(* iterate entries *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    85
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    86
fun fold_entries id0 f (node as Node entries) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    87
  let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    88
    fun apply NONE x = x
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    89
      | apply (SOME id) x =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    90
          let val entry = the_entry node id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    91
          in apply (#next entry) (f (id, entry) x) end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    92
  in if Inttab.defined entries id0 then apply (SOME id0) else I end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    93
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    94
fun first_entry P node =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    95
  let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    96
    fun first _ NONE = NONE
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    97
      | first prev (SOME id) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    98
          let val entry = the_entry node id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
    99
          in if P (id, entry) then SOME (prev, id, entry) else first (SOME id) (#next entry) end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   100
  in first NONE (SOME no_id) end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   101
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   102
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   103
(* modify entries *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   104
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   105
fun insert_after (id: command_id) (id2: command_id) node =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   106
  let val {next, exec} = the_entry node id in
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   107
    node
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   108
    |> put_entry (id, make_entry (SOME id2) exec)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   109
    |> put_entry (id2, make_entry next NONE)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   110
  end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   111
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   112
fun delete_after (id: command_id) node =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   113
  let val {next, exec} = the_entry node id in
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   114
    (case next of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   115
      NONE => error ("No next entry to delete: " ^ print_id id)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   116
    | SOME id2 =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   117
        node |>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   118
          (case #next (the_entry node id2) of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   119
            NONE => put_entry (id, make_entry NONE exec)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   120
          | SOME id3 => put_entry (id, make_entry (SOME id3) exec) #> reset_entry_exec id3))
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   121
  end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   122
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   123
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   124
(* node edits *)
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   125
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   126
type edit =
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   127
  string *  (*node name*)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   128
  ((command_id * command_id option) list) option;  (*NONE: remove, SOME: insert/remove commands*)
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   129
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   130
val empty_node = Node (Inttab.make [(no_id, make_entry NONE (SOME no_id))]);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   131
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   132
fun edit_node (id, SOME id2) = insert_after id id2
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   133
  | edit_node (id, NONE) = delete_after id;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   134
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   135
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   136
(* version operations *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   137
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   138
fun nodes_of (Version nodes) = nodes;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   139
val node_names_of = Graph.keys o nodes_of;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   140
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   141
fun edit_nodes (name, SOME edits) (Version nodes) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   142
      Version (nodes
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   143
        |> Graph.default_node (name, empty_node)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   144
        |> Graph.map_node name (fold edit_node edits))
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   145
  | edit_nodes (name, NONE) (Version nodes) = Version (Graph.del_node name nodes);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   146
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   147
val empty_version = Version Graph.empty;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   148
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   149
fun the_node version name =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   150
  Graph.get_node (nodes_of version) name handle Graph.UNDEF _ => empty_node;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   151
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   152
fun put_node name node (Version nodes) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   153
  Version (Graph.map_node name (K node) nodes);  (* FIXME Graph.UNDEF !? *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   154
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   155
end;
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
diff changeset
   156
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   157
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   158
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   159
(** global state -- document structure and execution process **)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   160
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   161
abstype state = State of
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   162
 {versions: version Inttab.table,                     (*version_id -> document content*)
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   163
  commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   164
  execs: Toplevel.state option lazy Inttab.table,     (*exec_id -> execution process*)
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   165
  execution: unit future list}                        (*global execution process*)
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   166
with
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   167
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   168
fun make_state (versions, commands, execs, execution) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   169
  State {versions = versions, commands = commands, execs = execs, execution = execution};
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   170
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   171
fun map_state f (State {versions, commands, execs, execution}) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   172
  make_state (f (versions, commands, execs, execution));
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   173
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   174
val init_state =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   175
  make_state (Inttab.make [(no_id, empty_version)],
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   176
    Inttab.make [(no_id, Future.value Toplevel.empty)],
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   177
    Inttab.make [(no_id, Lazy.value (SOME Toplevel.toplevel))],
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   178
    []);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   179
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   180
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   181
(* document versions *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   182
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   183
fun define_version (id: version_id) version =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   184
  map_state (fn (versions, commands, execs, execution) =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   185
    let val versions' = Inttab.update_new (id, version) versions
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   186
      handle Inttab.DUP dup => err_dup "document version" dup
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   187
    in (versions', commands, execs, execution) end);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   188
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   189
fun the_version (State {versions, ...}) (id: version_id) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   190
  (case Inttab.lookup versions id of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   191
    NONE => err_undef "document version" id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   192
  | SOME version => version);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   193
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   194
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   195
(* commands *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   196
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   197
fun define_command (id: command_id) text =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   198
  map_state (fn (versions, commands, execs, execution) =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   199
    let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   200
      val id_string = print_id id;
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   201
      val tr = Future.fork_pri 2 (fn () =>
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   202
        Position.setmp_thread_data (Position.id_only id_string)
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   203
          (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   204
      val commands' =
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   205
        Inttab.update_new (id, tr) commands
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   206
          handle Inttab.DUP dup => err_dup "command" dup;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   207
    in (versions, commands', execs, execution) end);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   208
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   209
fun the_command (State {commands, ...}) (id: command_id) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   210
  (case Inttab.lookup commands id of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   211
    NONE => err_undef "command" id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   212
  | SOME tr => tr);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   213
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   214
fun join_commands (State {commands, ...}) =
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   215
  Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   216
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   217
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   218
(* command executions *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   219
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   220
fun define_exec (id: exec_id) exec =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   221
  map_state (fn (versions, commands, execs, execution) =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   222
    let val execs' = Inttab.update_new (id, exec) execs
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   223
      handle Inttab.DUP dup => err_dup "command execution" dup
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   224
    in (versions, commands, execs', execution) end);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   225
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   226
fun the_exec (State {execs, ...}) (id: exec_id) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   227
  (case Inttab.lookup execs id of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   228
    NONE => err_undef "command execution" id
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   229
  | SOME exec => exec);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   230
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   231
end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   232
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   233
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   234
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   235
(** editing **)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   236
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   237
(* edit *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   238
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   239
local
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   240
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   241
fun is_changed node' (id, {next = _, exec}) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   242
  (case try (the_entry node') id of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   243
    NONE => true
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   244
  | SOME {next = _, exec = exec'} => exec' <> exec);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   245
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   246
fun new_exec name (id: command_id) (exec_id, updates, state) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   247
  let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   248
    val exec = the_exec state exec_id;
38419
f9dc924e54f8 renamed create_id to new_id;
wenzelm
parents: 38418
diff changeset
   249
    val exec_id' = new_id ();
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   250
    val tr = the_command state id;
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   251
    val exec' =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   252
      Lazy.lazy (fn () =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   253
        (case Lazy.force exec of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   254
          NONE => NONE
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   255
        | SOME st =>
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   256
            let val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join tr)
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   257
            in Toplevel.run_command name exec_tr st end));
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   258
    val state' = define_exec exec_id' exec' state;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   259
  in (exec_id', (id, exec_id') :: updates, state') end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   260
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   261
in
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   262
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   263
fun edit (old_id: version_id) (new_id: version_id) edits state =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   264
  let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   265
    val old_version = the_version state old_id;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   266
    val new_version = fold edit_nodes edits old_version;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   267
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   268
    fun update_node name (rev_updates, version, st) =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   269
      let val node = the_node version name in
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   270
        (case first_entry (is_changed (the_node old_version name)) node of
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   271
          NONE => (rev_updates, version, st)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   272
        | SOME (prev, id, _) =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   273
            let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   274
              val prev_exec = the (#exec (the_entry node (the prev)));
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   275
              val (_, rev_upds, st') =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   276
                fold_entries id (new_exec name o #1) node (prev_exec, [], st);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   277
              val node' = fold set_entry_exec rev_upds node;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   278
            in (rev_upds @ rev_updates, put_node name node' version, st') end)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   279
      end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   280
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   281
    (* FIXME proper node deps *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   282
    val (rev_updates, new_version', state') =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   283
      fold update_node (node_names_of new_version) ([], new_version, state);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   284
    val state'' = define_version new_id new_version' state';
38421
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   285
6cfc6fce7bfb document commands: maintain transition as future (wrt. potentially slow Outer_Syntax.prepare_command), refrain from second Toplevel.put_id;
wenzelm
parents: 38419
diff changeset
   286
    val _ = join_commands state'';  (* FIXME async!? *)
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   287
  in (rev rev_updates, state'') end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   288
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   289
end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   290
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   291
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   292
(* execute *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   293
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   294
fun execute version_id state =
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   295
  state |> map_state (fn (versions, commands, execs, execution) =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   296
    let
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   297
      val version = the_version state version_id;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   298
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   299
      fun force_exec NONE = ()
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   300
        | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   301
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   302
      val _ = List.app Future.cancel execution;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   303
      fun await_cancellation () = uninterruptible (fn _ => Future.join_results) execution;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   304
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   305
      val execution' = (* FIXME proper node deps *)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   306
        node_names_of version |> map (fn name =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   307
          Future.fork_pri 1 (fn () =>
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   308
            (await_cancellation ();
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   309
              fold_entries no_id (fn (_, {exec, ...}) => fn () => force_exec exec)
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   310
                (the_node version name) ())));
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   311
    in (versions, commands, execs, execution') end);
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   312
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   313
end;
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38414
diff changeset
   314