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