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