1 (* Title: Pure/PIDE/document.ML
4 Document as collection of named nodes, each consisting of an editable
5 list of commands, associated with asynchronous execution process.
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
21 val cancel: state -> unit
22 val define_command: command_id -> string -> state -> state
23 val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
24 val execute: version_id -> state -> state
27 structure Document: DOCUMENT =
30 (* unique identifiers *)
38 val new_id = Synchronized.counter ();
40 val parse_id = Markup.parse_int;
41 val print_id = Markup.print_int;
43 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
44 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
48 (** document structure **)
50 structure Entries = Linear_Set(type key = command_id val ord = int_ord);
52 abstype node = Node of exec_id option Entries.T (*command entries with excecutions*)
53 and version = Version of node Graph.T (*development graph wrt. static imports*)
56 val empty_node = Node Entries.empty;
57 val empty_version = Version Graph.empty;
59 fun fold_entries start f (Node entries) = Entries.fold start f entries;
60 fun first_entry start P (Node entries) = Entries.get_first start P entries;
63 (* node edits and associated executions *)
67 (*NONE: remove node, SOME: insert/remove commands*)
68 ((command_id option * command_id option) list) option;
70 fun the_entry (Node entries) id =
71 (case Entries.lookup entries id of
72 NONE => err_undef "command entry" id
73 | SOME entry => entry);
75 fun update_entry (id, exec_id) (Node entries) =
76 Node (Entries.update (id, SOME exec_id) entries);
78 fun reset_after id entries =
79 (case Entries.get_after entries id of
81 | SOME next => Entries.update (next, NONE) entries);
83 fun edit_node (id, SOME id2) (Node entries) =
84 Node (Entries.insert_after id (id2, NONE) entries)
85 | edit_node (id, NONE) (Node entries) =
86 Node (entries |> Entries.delete_after id |> reset_after id);
89 (* version operations *)
91 fun nodes_of (Version nodes) = nodes;
92 val node_names_of = Graph.keys o nodes_of;
94 fun get_node version name = Graph.get_node (nodes_of version) name
95 handle Graph.UNDEF _ => empty_node;
97 fun edit_nodes (name, SOME edits) (Version nodes) =
99 |> Graph.default_node (name, empty_node)
100 |> Graph.map_node name (fold edit_node edits))
101 | edit_nodes (name, NONE) (Version nodes) =
102 Version (perhaps (try (Graph.del_node name)) nodes);
104 fun put_node name node (Version nodes) =
106 |> Graph.default_node (name, empty_node)
107 |> Graph.map_node name (K node));
113 (** global state -- document structure and execution process **)
115 abstype state = State of
116 {versions: version Inttab.table, (*version_id -> document content*)
117 commands: Toplevel.transition future Inttab.table, (*command_id -> transition (future parsing)*)
118 execs: (bool * Toplevel.state) lazy Inttab.table, (*exec_id -> execution process*)
119 execution: unit future list} (*global execution process*)
122 fun make_state (versions, commands, execs, execution) =
123 State {versions = versions, commands = commands, execs = execs, execution = execution};
125 fun map_state f (State {versions, commands, execs, execution}) =
126 make_state (f (versions, commands, execs, execution));
129 make_state (Inttab.make [(no_id, empty_version)],
130 Inttab.make [(no_id, Future.value Toplevel.empty)],
131 Inttab.make [(no_id, Lazy.value (true, Toplevel.toplevel))],
135 (* document versions *)
137 fun define_version (id: version_id) version =
138 map_state (fn (versions, commands, execs, execution) =>
139 let val versions' = Inttab.update_new (id, version) versions
140 handle Inttab.DUP dup => err_dup "document version" dup
141 in (versions', commands, execs, execution) end);
143 fun the_version (State {versions, ...}) (id: version_id) =
144 (case Inttab.lookup versions id of
145 NONE => err_undef "document version" id
146 | SOME version => version);
151 fun define_command (id: command_id) text =
152 map_state (fn (versions, commands, execs, execution) =>
154 val id_string = print_id id;
155 val tr = Future.fork_pri 2 (fn () =>
156 Position.setmp_thread_data (Position.id_only id_string)
157 (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
159 Inttab.update_new (id, tr) commands
160 handle Inttab.DUP dup => err_dup "command" dup;
161 in (versions, commands', execs, execution) end);
163 fun the_command (State {commands, ...}) (id: command_id) =
164 (case Inttab.lookup commands id of
165 NONE => err_undef "command" id
168 fun join_commands (State {commands, ...}) =
169 Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
172 (* command executions *)
174 fun define_exec (id: exec_id) exec =
175 map_state (fn (versions, commands, execs, execution) =>
176 let val execs' = Inttab.update_new (id, exec) execs
177 handle Inttab.DUP dup => err_dup "command execution" dup
178 in (versions, commands, execs', execution) end);
180 fun the_exec (State {execs, ...}) (id: exec_id) =
181 (case Inttab.lookup execs id of
182 NONE => err_undef "command execution" id
183 | SOME exec => exec);
186 (* document execution *)
188 fun cancel (State {execution, ...}) =
189 List.app Future.cancel execution;
191 fun await_cancellation (State {execution, ...}) =
192 ignore (Future.join_results execution);
198 (* toplevel transactions *)
202 fun timing tr t = Toplevel.status tr (Markup.timing t);
204 fun proof_status tr st =
205 (case try Toplevel.proof_of st of
206 SOME prf => Toplevel.status tr (Proof.status_markup prf)
209 fun async_state tr st =
212 (Future.forks {name = "Document.async_state",
213 group = SOME (Task_Queue.new_group NONE), deps = [], pri = 0})
215 Toplevel.setmp_thread_position tr
216 (fn () => Toplevel.print_state false st) ()));
219 (case Toplevel.transition int tr st of
220 SOME (st', NONE) => ([], SOME st')
221 | SOME (_, SOME exn_info) =>
222 (case ML_Compiler.exn_messages (Runtime.EXCURSION_FAIL exn_info) of
223 [] => Exn.interrupt ()
224 | errs => (errs, NONE))
225 | NONE => ([ML_Compiler.exn_message Runtime.TERMINATE], NONE));
229 fun run_command thy_name raw_tr st =
231 (case Toplevel.init_of raw_tr of
232 SOME name => Exn.capture (fn () =>
234 val path = Path.explode thy_name;
235 val _ = Thy_Header.consistent_name (Path.implode (Path.base path)) name;
236 in Toplevel.modify_master (SOME (Path.dir path)) raw_tr end) ()
237 | NONE => Exn.Result raw_tr) of
240 val is_init = is_some (Toplevel.init_of tr);
241 val is_proof = Keyword.is_proof (Toplevel.name_of tr);
242 val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
244 val start = Timing.start ();
246 if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
247 else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
248 val _ = timing tr (Timing.result start);
249 val _ = List.app (Toplevel.error_msg tr) errs;
252 NONE => (Toplevel.status tr Markup.failed; (false, st))
254 (Toplevel.status tr Markup.finished;
256 if do_print then async_state tr st' else ();
260 if Exn.is_interrupt exn then reraise exn
262 (Toplevel.error_msg raw_tr (ML_Compiler.exn_message exn);
263 Toplevel.status raw_tr Markup.failed;
264 (false, Toplevel.toplevel)));
277 fun is_changed node' ((_, id), exec) =
278 (case try (the_entry node') id of
280 | SOME exec' => exec' <> exec);
282 fun new_exec name (id: command_id) (exec_id, updates, state) =
284 val exec = the_exec state exec_id;
285 val exec_id' = new_id ();
286 val future_tr = the_command state id;
290 val st = #2 (Lazy.force exec);
291 val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join future_tr);
292 in run_command name exec_tr st end);
293 val state' = define_exec exec_id' exec' state;
294 in (exec_id', (id, exec_id') :: updates, state') end;
298 fun edit (old_id: version_id) (new_id: version_id) edits state =
300 val old_version = the_version state old_id;
301 val _ = Time.now (); (* FIXME odd workaround *)
302 val new_version = fold edit_nodes edits old_version;
304 fun update_node name (rev_updates, version, st) =
305 let val node = get_node version name in
306 (case first_entry NONE (is_changed (get_node old_version name)) node of
307 NONE => (rev_updates, version, st)
308 | SOME ((prev, id), _) =>
313 | SOME prev_id => the_default no_id (the_entry node prev_id));
314 val (_, rev_upds, st') =
315 fold_entries (SOME id) (new_exec name o #2 o #1) node (prev_exec, [], st);
316 val node' = fold update_entry rev_upds node;
317 in (rev_upds @ rev_updates, put_node name node' version, st') end)
320 (* FIXME proper node deps *)
321 val (rev_updates, new_version', state') =
322 fold update_node (node_names_of new_version) ([], new_version, state);
323 val state'' = define_version new_id new_version' state';
325 val _ = join_commands state''; (* FIXME async!? *)
326 in (rev rev_updates, state'') end;
333 fun execute version_id state =
334 state |> map_state (fn (versions, commands, execs, _) =>
336 val version = the_version state version_id;
338 fun force_exec NONE = ()
339 | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
341 val _ = cancel state;
343 val execution' = (* FIXME proper node deps *)
344 Future.forks {name = "Document.execute", group = NONE, deps = [], pri = 1}
347 val _ = await_cancellation state;
349 node_names_of version |> List.app (fn name =>
350 fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
351 (get_node version name) ());
354 val _ = await_cancellation state; (* FIXME async!? *)
356 in (versions, commands, execs, execution') end);