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