src/Pure/PIDE/document.ML
author wenzelm
Thu Aug 25 16:44:06 2011 +0200 (2011-08-25 ago)
changeset 44476 e8a87398f35d
parent 44446 f9334afb6945
child 44478 4fdb1009a370
permissions -rw-r--r--
propagate information about last command with exec state assignment through document model;
     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 node_header = (string * string list * (string * bool) list) Exn.result
    19   datatype node_edit =
    20     Clear |
    21     Edits of (command_id option * command_id option) list |
    22     Header of node_header |
    23     Perspective of command_id list
    24   type edit = string * node_edit
    25   type state
    26   val init_state: state
    27   val define_command: command_id -> string -> state -> state
    28   val join_commands: state -> unit
    29   val cancel_execution: state -> Future.task list
    30   val update_perspective: version_id -> version_id -> string -> command_id list -> state -> state
    31   val edit: version_id -> version_id -> edit list -> state ->
    32     ((command_id * exec_id) list * (string * command_id option) list) * state
    33   val execute: version_id -> state -> state
    34   val state: unit -> state
    35   val change_state: (state -> state) -> unit
    36 end;
    37 
    38 structure Document: DOCUMENT =
    39 struct
    40 
    41 (* unique identifiers *)
    42 
    43 type id = int;
    44 type version_id = id;
    45 type command_id = id;
    46 type exec_id = id;
    47 
    48 val no_id = 0;
    49 val new_id = Synchronized.counter ();
    50 
    51 val parse_id = Markup.parse_int;
    52 val print_id = Markup.print_int;
    53 
    54 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
    55 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
    56 
    57 
    58 
    59 (** document structure **)
    60 
    61 type node_header = (string * string list * (string * bool) list) Exn.result;
    62 type perspective = (command_id -> bool) * command_id option; (*visible commands, last*)
    63 structure Entries = Linear_Set(type key = command_id val ord = int_ord);
    64 
    65 abstype node = Node of
    66  {touched: bool,
    67   header: node_header,
    68   perspective: perspective,
    69   entries: exec_id option Entries.T,  (*command entries with excecutions*)
    70   last_exec: command_id option,  (*last command with exec state assignment*)
    71   result: Toplevel.state lazy}
    72 and version = Version of node Graph.T  (*development graph wrt. static imports*)
    73 with
    74 
    75 fun make_node (touched, header, perspective, entries, last_exec, result) =
    76   Node {touched = touched, header = header, perspective = perspective,
    77     entries = entries, last_exec = last_exec, result = result};
    78 
    79 fun map_node f (Node {touched, header, perspective, entries, last_exec, result}) =
    80   make_node (f (touched, header, perspective, entries, last_exec, result));
    81 
    82 fun make_perspective command_ids : perspective =
    83   (Inttab.defined (Inttab.make (map (rpair ()) command_ids)), try List.last command_ids);
    84 
    85 val no_header = Exn.Exn (ERROR "Bad theory header");
    86 val no_perspective = make_perspective [];
    87 val no_print = Lazy.value ();
    88 val no_result = Lazy.value Toplevel.toplevel;
    89 
    90 val empty_node = make_node (false, no_header, no_perspective, Entries.empty, NONE, no_result);
    91 val clear_node = map_node (fn (_, header, _, _, _, _) =>
    92   (false, header, no_perspective, Entries.empty, NONE, no_result));
    93 
    94 
    95 (* basic components *)
    96 
    97 fun is_touched (Node {touched, ...}) = touched;
    98 fun set_touched touched =
    99   map_node (fn (_, header, perspective, entries, last_exec, result) =>
   100     (touched, header, perspective, entries, last_exec, result));
   101 
   102 fun get_header (Node {header, ...}) = header;
   103 fun set_header header =
   104   map_node (fn (touched, _, perspective, entries, last_exec, result) =>
   105     (touched, header, perspective, entries, last_exec, result));
   106 
   107 fun get_perspective (Node {perspective, ...}) = perspective;
   108 fun set_perspective ids =
   109   map_node (fn (touched, header, _, entries, last_exec, result) =>
   110     (touched, header, make_perspective ids, entries, last_exec, result));
   111 
   112 fun map_entries f =
   113   map_node (fn (touched, header, perspective, entries, last_exec, result) =>
   114     (touched, header, perspective, f entries, last_exec, result));
   115 fun fold_entries start f (Node {entries, ...}) = Entries.fold start f entries;
   116 fun first_entry start P (Node {entries, ...}) = Entries.get_first start P entries;
   117 
   118 fun get_last_exec (Node {last_exec, ...}) = last_exec;
   119 fun set_last_exec last_exec =
   120   map_node (fn (touched, header, perspective, entries, _, result) =>
   121     (touched, header, perspective, entries, last_exec, result));
   122 
   123 fun get_theory pos (Node {result, ...}) = Toplevel.end_theory pos (Lazy.force result);
   124 fun set_result result =
   125   map_node (fn (touched, header, perspective, entries, last_exec, _) =>
   126     (touched, header, perspective, entries, last_exec, result));
   127 
   128 fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
   129 fun default_node name = Graph.default_node (name, empty_node);
   130 fun update_node name f = default_node name #> Graph.map_node name f;
   131 
   132 
   133 (* node edits and associated executions *)
   134 
   135 datatype node_edit =
   136   Clear |
   137   Edits of (command_id option * command_id option) list |
   138   Header of node_header |
   139   Perspective of command_id list;
   140 
   141 type edit = string * node_edit;
   142 
   143 fun the_entry (Node {entries, ...}) id =
   144   (case Entries.lookup entries id of
   145     NONE => err_undef "command entry" id
   146   | SOME (exec, _) => exec);
   147 
   148 fun is_last_entry (Node {entries, ...}) id =
   149   (case Entries.lookup entries id of
   150     SOME (_, SOME _) => false
   151   | _ => true);
   152 
   153 fun update_entry id exec_id =
   154   map_entries (Entries.update (id, SOME exec_id));
   155 
   156 fun reset_after id entries =
   157   (case Entries.get_after entries id of
   158     NONE => entries
   159   | SOME next => Entries.update (next, NONE) entries);
   160 
   161 val edit_node = map_entries o fold
   162   (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
   163     | (id, NONE) => Entries.delete_after id #> reset_after id);
   164 
   165 
   166 (* version operations *)
   167 
   168 val empty_version = Version Graph.empty;
   169 
   170 fun nodes_of (Version nodes) = nodes;
   171 val node_of = get_node o nodes_of;
   172 
   173 local
   174 
   175 fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
   176 
   177 fun touch_node name nodes =
   178   fold (fn desc =>
   179       update_node desc (set_touched true) #>
   180       desc <> name ? update_node desc (map_entries (reset_after NONE)))
   181     (Graph.all_succs nodes [name]) nodes;
   182 
   183 in
   184 
   185 fun edit_nodes (name, node_edit) (Version nodes) =
   186   Version
   187     (case node_edit of
   188       Clear =>
   189         nodes
   190         |> update_node name clear_node
   191         |> touch_node name
   192     | Edits edits =>
   193         nodes
   194         |> update_node name (edit_node edits)
   195         |> touch_node name
   196     | Header header =>
   197         let
   198           val parents = (case header of Exn.Res (_, parents, _) => parents | _ => []);
   199           val nodes1 = nodes
   200             |> default_node name
   201             |> fold default_node parents;
   202           val nodes2 = nodes1
   203             |> Graph.Keys.fold
   204                 (fn dep => Graph.del_edge (dep, name)) (Graph.imm_preds nodes1 name);
   205           val (header', nodes3) =
   206             (header, Graph.add_deps_acyclic (name, parents) nodes2)
   207               handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes2);
   208         in Graph.map_node name (set_header header') nodes3 end
   209         |> touch_node name
   210     | Perspective perspective =>
   211         update_node name (set_perspective perspective) nodes);
   212 
   213 end;
   214 
   215 fun put_node (name, node) (Version nodes) =
   216   Version (update_node name (K node) nodes);
   217 
   218 end;
   219 
   220 
   221 
   222 (** global state -- document structure and execution process **)
   223 
   224 abstype state = State of
   225  {versions: version Inttab.table,  (*version_id -> document content*)
   226   commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
   227   execs: (command_id * (Toplevel.state * unit lazy) lazy) Inttab.table,
   228     (*exec_id -> command_id with eval/print process*)
   229   execution: Future.group}  (*global execution process*)
   230 with
   231 
   232 fun make_state (versions, commands, execs, execution) =
   233   State {versions = versions, commands = commands, execs = execs, execution = execution};
   234 
   235 fun map_state f (State {versions, commands, execs, execution}) =
   236   make_state (f (versions, commands, execs, execution));
   237 
   238 val init_state =
   239   make_state (Inttab.make [(no_id, empty_version)],
   240     Inttab.make [(no_id, Future.value Toplevel.empty)],
   241     Inttab.make [(no_id, (no_id, Lazy.value (Toplevel.toplevel, no_print)))],
   242     Future.new_group NONE);
   243 
   244 
   245 (* document versions *)
   246 
   247 fun define_version (id: version_id) version =
   248   map_state (fn (versions, commands, execs, execution) =>
   249     let val versions' = Inttab.update_new (id, version) versions
   250       handle Inttab.DUP dup => err_dup "document version" dup
   251     in (versions', commands, execs, execution) end);
   252 
   253 fun the_version (State {versions, ...}) (id: version_id) =
   254   (case Inttab.lookup versions id of
   255     NONE => err_undef "document version" id
   256   | SOME version => version);
   257 
   258 
   259 (* commands *)
   260 
   261 fun define_command (id: command_id) text =
   262   map_state (fn (versions, commands, execs, execution) =>
   263     let
   264       val id_string = print_id id;
   265       val tr =
   266         Future.fork_pri 2 (fn () =>
   267           Position.setmp_thread_data (Position.id_only id_string)
   268             (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
   269       val commands' =
   270         Inttab.update_new (id, tr) commands
   271           handle Inttab.DUP dup => err_dup "command" dup;
   272     in (versions, commands', execs, execution) end);
   273 
   274 fun the_command (State {commands, ...}) (id: command_id) =
   275   (case Inttab.lookup commands id of
   276     NONE => err_undef "command" id
   277   | SOME tr => tr);
   278 
   279 fun join_commands (State {commands, ...}) =
   280   Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
   281 
   282 
   283 (* command executions *)
   284 
   285 fun define_exec (exec_id, exec) =
   286   map_state (fn (versions, commands, execs, execution) =>
   287     let val execs' = Inttab.update_new (exec_id, exec) execs
   288       handle Inttab.DUP dup => err_dup "command execution" dup
   289     in (versions, commands, execs', execution) end);
   290 
   291 fun the_exec (State {execs, ...}) exec_id =
   292   (case Inttab.lookup execs exec_id of
   293     NONE => err_undef "command execution" exec_id
   294   | SOME exec => exec);
   295 
   296 
   297 (* document execution *)
   298 
   299 fun cancel_execution (State {execution, ...}) = Future.cancel_group execution;
   300 
   301 end;
   302 
   303 
   304 
   305 (* toplevel transactions *)
   306 
   307 local
   308 
   309 fun timing tr t =
   310   if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
   311 
   312 fun proof_status tr st =
   313   (case try Toplevel.proof_of st of
   314     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   315   | NONE => ());
   316 
   317 fun print_state tr st =
   318   (Lazy.lazy o Toplevel.setmp_thread_position tr)
   319     (fn () => Toplevel.print_state false st);
   320 
   321 fun run int tr st =
   322   (case Toplevel.transition int tr st of
   323     SOME (st', NONE) => ([], SOME st')
   324   | SOME (_, SOME (exn, _)) =>
   325       (case ML_Compiler.exn_messages exn of
   326         [] => Exn.interrupt ()
   327       | errs => (errs, NONE))
   328   | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
   329 
   330 in
   331 
   332 fun run_command tr st =
   333   let
   334     val is_init = Toplevel.is_init tr;
   335     val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   336     val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
   337 
   338     val start = Timing.start ();
   339     val (errs, result) =
   340       if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
   341       else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   342     val _ = timing tr (Timing.result start);
   343     val _ = List.app (Toplevel.error_msg tr) errs;
   344   in
   345     (case result of
   346       NONE => (Toplevel.status tr Markup.failed; (st, no_print))
   347     | SOME st' =>
   348        (Toplevel.status tr Markup.finished;
   349         proof_status tr st';
   350         (st', if do_print then print_state tr st' else no_print)))
   351   end;
   352 
   353 end;
   354 
   355 
   356 
   357 
   358 (** editing **)
   359 
   360 (* perspective *)
   361 
   362 fun update_perspective (old_id: version_id) (new_id: version_id) name perspective state =
   363   let
   364     val old_version = the_version state old_id;
   365     val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 -- needed? *)
   366     val new_version = edit_nodes (name, Perspective perspective) old_version;
   367   in define_version new_id new_version state end;
   368 
   369 
   370 (* edit *)
   371 
   372 local
   373 
   374 fun is_changed node' ((_, id), exec) =
   375   (case try (the_entry node') id of
   376     NONE => true
   377   | SOME exec' => exec' <> exec);
   378 
   379 fun init_theory deps name node =
   380   let
   381     val (thy_name, imports, uses) = Exn.release (get_header node);
   382     (* FIXME provide files via Scala layer *)
   383     val (dir, files) =
   384       if ML_System.platform_is_cygwin then (Path.current, [])
   385       else (Path.dir (Path.explode name), map (apfst Path.explode) uses);
   386 
   387     val parents =
   388       imports |> map (fn import =>
   389         (case Thy_Info.lookup_theory import of  (* FIXME more robust!? *)
   390           SOME thy => thy
   391         | NONE =>
   392             get_theory (Position.file_only import)
   393               (#2 (Future.join (the (AList.lookup (op =) deps import))))));
   394   in Thy_Load.begin_theory dir thy_name imports files parents end;
   395 
   396 fun new_exec state init command_id' (execs, exec) =
   397   let
   398     val command' = the_command state command_id';
   399     val exec_id' = new_id ();
   400     val exec' =
   401       snd exec |> Lazy.map (fn (st, _) =>
   402         let val tr =
   403           Future.join command'
   404           |> Toplevel.modify_init init
   405           |> Toplevel.put_id (print_id exec_id');
   406         in run_command tr st end)
   407       |> pair command_id';
   408   in ((exec_id', exec') :: execs, exec') end;
   409 
   410 in
   411 
   412 fun edit (old_id: version_id) (new_id: version_id) edits state =
   413   let
   414     val old_version = the_version state old_id;
   415     val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
   416     val new_version = fold edit_nodes edits old_version;
   417 
   418     val updated =
   419       nodes_of new_version |> Graph.schedule
   420         (fn deps => fn (name, node) =>
   421           if not (is_touched node) then Future.value (([], []), node)
   422           else
   423             (case first_entry NONE (is_changed (node_of old_version name)) node of
   424               NONE => Future.value (([], []), set_touched false node)
   425             | SOME ((prev, id), _) =>
   426                 let
   427                   fun init () = init_theory deps name node;
   428                 in
   429                   (singleton o Future.forks)
   430                     {name = "Document.edit", group = NONE,
   431                       deps = map (Future.task_of o #2) deps, pri = 0, interrupts = false}
   432                     (fn () =>
   433                       let
   434                         val prev_exec =
   435                           (case prev of
   436                             NONE => no_id
   437                           | SOME prev_id => the_default no_id (the_entry node prev_id));
   438                         val (execs, last_exec as (last_id, _)) =
   439                           fold_entries (SOME id) (new_exec state init o #2 o #1)
   440                             node ([], the_exec state prev_exec);
   441                         val node' =
   442                           fold (fn (exec_id, (command_id, _)) => update_entry command_id exec_id)
   443                             execs node;
   444                         val result =
   445                           if is_last_entry node' last_id
   446                           then Lazy.map #1 (#2 last_exec)
   447                           else no_result;
   448                         val node'' = node'
   449                           |> set_last_exec (if last_id = no_id then NONE else SOME last_id)
   450                           |> set_result result
   451                           |> set_touched false;
   452                       in ((execs, [(name, node'')]), node'') end)
   453                 end))
   454       |> Future.joins |> map #1 |> rev;  (* FIXME why rev? *)
   455 
   456     val updated_nodes = maps #2 updated;
   457     val assignment = map (fn (exec_id, (command_id, _)) => (command_id, exec_id)) (maps #1 updated);
   458     val last_execs = map (fn (name, node) => (name, get_last_exec node)) updated_nodes;
   459 
   460     val state' = state
   461       |> fold (fold define_exec o #1) updated
   462       |> define_version new_id (fold put_node updated_nodes new_version);
   463   in ((assignment, last_execs), state') end;
   464 
   465 end;
   466 
   467 
   468 (* execute *)
   469 
   470 fun execute version_id state =
   471   state |> map_state (fn (versions, commands, execs, _) =>
   472     let
   473       val version = the_version state version_id;
   474 
   475       fun force_exec _ NONE = ()
   476         | force_exec node (SOME exec_id) =
   477             let
   478               val (command_id, exec) = the_exec state exec_id;
   479               val (_, print) = Lazy.force exec;
   480               val _ =
   481                 if #1 (get_perspective node) command_id
   482                 then ignore (Lazy.future Future.default_params print)
   483                 else ();
   484             in () end;
   485 
   486       val execution = Future.new_group NONE;
   487       val _ =
   488         nodes_of version |> Graph.schedule
   489           (fn deps => fn (name, node) =>
   490             (singleton o Future.forks)
   491               {name = "theory:" ^ name, group = SOME (Future.new_group (SOME execution)),
   492                 deps = map (Future.task_of o #2) deps, pri = 1, interrupts = true}
   493               (fold_entries NONE (fn (_, exec) => fn () => force_exec node exec) node));
   494 
   495     in (versions, commands, execs, execution) end);
   496 
   497 
   498 
   499 (** global state **)
   500 
   501 val global_state = Synchronized.var "Document" init_state;
   502 
   503 fun state () = Synchronized.value global_state;
   504 val change_state = Synchronized.change global_state;
   505 
   506 end;
   507