src/Pure/PIDE/document.ML
author wenzelm
Mon Aug 15 14:54:36 2011 +0200 (2011-08-15)
changeset 44196 3588f71abb50
parent 44186 806f0ec1a43d
child 44197 458573968568
permissions -rw-r--r--
simplified exec: eliminated unused status flag;
     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   type edit = string * node_edit
    24   type state
    25   val init_state: state
    26   val get_theory: state -> version_id -> Position.T -> string -> theory
    27   val cancel_execution: state -> unit -> unit
    28   val define_command: command_id -> string -> state -> state
    29   val edit: version_id -> version_id -> edit list -> state -> (command_id * exec_id) list * state
    30   val execute: version_id -> state -> state
    31   val state: unit -> state
    32   val change_state: (state -> state) -> unit
    33 end;
    34 
    35 structure Document: DOCUMENT =
    36 struct
    37 
    38 (* unique identifiers *)
    39 
    40 type id = int;
    41 type version_id = id;
    42 type command_id = id;
    43 type exec_id = id;
    44 
    45 val no_id = 0;
    46 val new_id = Synchronized.counter ();
    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 type node_header = (string * string list * (string * bool) list) Exn.result;
    59 structure Entries = Linear_Set(type key = command_id val ord = int_ord);
    60 
    61 abstype node = Node of
    62  {header: node_header,
    63   entries: exec_id option Entries.T,  (*command entries with excecutions*)
    64   last: exec_id}  (*last entry with main result*)
    65 and version = Version of node Graph.T  (*development graph wrt. static imports*)
    66 with
    67 
    68 fun make_node (header, entries, last) =
    69   Node {header = header, entries = entries, last = last};
    70 
    71 fun map_node f (Node {header, entries, last}) =
    72   make_node (f (header, entries, last));
    73 
    74 fun get_header (Node {header, ...}) = header;
    75 fun set_header header = map_node (fn (_, entries, last) => (header, entries, last));
    76 
    77 fun map_entries f (Node {header, entries, last}) = make_node (header, f entries, last);
    78 fun fold_entries start f (Node {entries, ...}) = Entries.fold start f entries;
    79 fun first_entry start P (Node {entries, ...}) = Entries.get_first start P entries;
    80 
    81 fun get_last (Node {last, ...}) = last;
    82 fun set_last last = map_node (fn (header, entries, _) => (header, entries, last));
    83 
    84 val empty_node = make_node (Exn.Exn (ERROR "Bad theory header"), Entries.empty, no_id);
    85 val clear_node = map_node (fn (header, _, _) => (header, Entries.empty, no_id));
    86 
    87 fun get_node nodes name = Graph.get_node nodes name handle Graph.UNDEF _ => empty_node;
    88 fun remove_node name nodes = Graph.del_node name nodes handle Graph.UNDEF _ => nodes;
    89 fun update_node name f = Graph.default_node (name, empty_node) #> Graph.map_node name f;
    90 
    91 
    92 (* node edits and associated executions *)
    93 
    94 datatype node_edit =
    95   Clear |
    96   Edits of (command_id option * command_id option) list |
    97   Header of node_header;
    98 
    99 type edit = string * node_edit;
   100 
   101 fun the_entry (Node {entries, ...}) id =
   102   (case Entries.lookup entries id of
   103     NONE => err_undef "command entry" id
   104   | SOME entry => entry);
   105 
   106 fun update_entry (id, exec_id) =
   107   map_entries (Entries.update (id, SOME exec_id));
   108 
   109 fun reset_after id entries =
   110   (case Entries.get_after entries id of
   111     NONE => entries
   112   | SOME next => Entries.update (next, NONE) entries);
   113 
   114 val edit_node = map_entries o fold
   115   (fn (id, SOME id2) => Entries.insert_after id (id2, NONE)
   116     | (id, NONE) => Entries.delete_after id #> reset_after id);
   117 
   118 
   119 (* version operations *)
   120 
   121 val empty_version = Version Graph.empty;
   122 
   123 fun nodes_of (Version nodes) = nodes;
   124 val node_of = get_node o nodes_of;
   125 val node_names_of = Graph.keys o nodes_of;
   126 
   127 fun cycle_msg names = "Cyclic dependency of " ^ space_implode " via " (map quote names);
   128 
   129 fun edit_nodes (name, node_edit) (Version nodes) =
   130   Version
   131     (case node_edit of
   132       Clear => update_node name clear_node nodes
   133     | Edits edits => update_node name (edit_node edits) nodes
   134     | Header header =>
   135         let
   136           val node = get_node nodes name;
   137           val nodes' = Graph.new_node (name, node) (remove_node name nodes);
   138           val parents =
   139             (case header of Exn.Res (_, parents, _) => parents | _ => [])
   140             |> filter (can (Graph.get_node nodes'));
   141           val (header', nodes'') =
   142             (header, Graph.add_deps_acyclic (name, parents) nodes')
   143               handle Graph.CYCLES cs => (Exn.Exn (ERROR (cat_lines (map cycle_msg cs))), nodes');
   144         in Graph.map_node name (set_header header') nodes'' end);
   145 
   146 fun put_node name node (Version nodes) =
   147   Version (nodes
   148     |> Graph.default_node (name, empty_node)
   149     |> Graph.map_node name (K node));
   150 
   151 end;
   152 
   153 
   154 
   155 (** global state -- document structure and execution process **)
   156 
   157 abstype state = State of
   158  {versions: version Inttab.table,  (*version_id -> document content*)
   159   commands: Toplevel.transition future Inttab.table,  (*command_id -> transition (future parsing)*)
   160   execs: Toplevel.state lazy Inttab.table,  (*exec_id -> execution process*)
   161   execution: unit future list}  (*global execution process*)
   162 with
   163 
   164 fun make_state (versions, commands, execs, execution) =
   165   State {versions = versions, commands = commands, execs = execs, execution = execution};
   166 
   167 fun map_state f (State {versions, commands, execs, execution}) =
   168   make_state (f (versions, commands, execs, execution));
   169 
   170 val init_state =
   171   make_state (Inttab.make [(no_id, empty_version)],
   172     Inttab.make [(no_id, Future.value Toplevel.empty)],
   173     Inttab.make [(no_id, Lazy.value Toplevel.toplevel)],
   174     []);
   175 
   176 
   177 (* document versions *)
   178 
   179 fun define_version (id: version_id) version =
   180   map_state (fn (versions, commands, execs, execution) =>
   181     let val versions' = Inttab.update_new (id, version) versions
   182       handle Inttab.DUP dup => err_dup "document version" dup
   183     in (versions', commands, execs, execution) end);
   184 
   185 fun the_version (State {versions, ...}) (id: version_id) =
   186   (case Inttab.lookup versions id of
   187     NONE => err_undef "document version" id
   188   | SOME version => version);
   189 
   190 
   191 (* commands *)
   192 
   193 fun define_command (id: command_id) text =
   194   map_state (fn (versions, commands, execs, execution) =>
   195     let
   196       val id_string = print_id id;
   197       val tr = Future.fork_pri 2 (fn () =>
   198         Position.setmp_thread_data (Position.id_only id_string)
   199           (fn () => Outer_Syntax.prepare_command (Position.id id_string) text) ());
   200       val commands' =
   201         Inttab.update_new (id, tr) commands
   202           handle Inttab.DUP dup => err_dup "command" dup;
   203     in (versions, commands', execs, execution) end);
   204 
   205 fun the_command (State {commands, ...}) (id: command_id) =
   206   (case Inttab.lookup commands id of
   207     NONE => err_undef "command" id
   208   | SOME tr => tr);
   209 
   210 fun join_commands (State {commands, ...}) =
   211   Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
   212 
   213 
   214 (* command executions *)
   215 
   216 fun define_exec (id: exec_id) exec =
   217   map_state (fn (versions, commands, execs, execution) =>
   218     let val execs' = Inttab.update_new (id, exec) execs
   219       handle Inttab.DUP dup => err_dup "command execution" dup
   220     in (versions, commands, execs', execution) end);
   221 
   222 fun the_exec (State {execs, ...}) (id: exec_id) =
   223   (case Inttab.lookup execs id of
   224     NONE => err_undef "command execution" id
   225   | SOME exec => exec);
   226 
   227 fun get_theory state version_id pos name =
   228   let
   229     val last = get_last (node_of (the_version state version_id) name);
   230     val st =
   231       (case Lazy.peek (the_exec state last) of
   232         SOME result => Exn.release result
   233       | NONE => error ("Unfinished execution for theory " ^ quote name ^ Position.str_of pos));
   234   in Toplevel.end_theory pos st end;
   235 
   236 
   237 (* document execution *)
   238 
   239 fun cancel_execution (State {execution, ...}) =
   240   (List.app Future.cancel execution;
   241     fn () => ignore (Future.join_results execution));
   242 
   243 end;
   244 
   245 
   246 
   247 (* toplevel transactions *)
   248 
   249 local
   250 
   251 fun timing tr t = Toplevel.status tr (Markup.timing t);
   252 
   253 fun proof_status tr st =
   254   (case try Toplevel.proof_of st of
   255     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   256   | NONE => ());
   257 
   258 fun async_state tr st =
   259   ignore
   260     (singleton
   261       (Future.forks {name = "Document.async_state",
   262         group = SOME (Task_Queue.new_group NONE), deps = [], pri = 0, interrupts = true})
   263       (fn () =>
   264         Toplevel.setmp_thread_position tr
   265           (fn () => Toplevel.print_state false st) ()));
   266 
   267 fun run int tr st =
   268   (case Toplevel.transition int tr st of
   269     SOME (st', NONE) => ([], SOME st')
   270   | SOME (_, SOME exn_info) =>
   271       (case ML_Compiler.exn_messages (Runtime.EXCURSION_FAIL exn_info) of
   272         [] => Exn.interrupt ()
   273       | errs => (errs, NONE))
   274   | NONE => ([ML_Compiler.exn_message Runtime.TERMINATE], NONE));
   275 
   276 in
   277 
   278 fun run_command (node_name, node_header) raw_tr st =
   279   let
   280     val is_init = Toplevel.is_init raw_tr;
   281     val tr =
   282       if is_init then
   283         raw_tr |> Toplevel.modify_init (fn () =>
   284           let
   285             (* FIXME get theories from document state *)
   286             (* FIXME provide files via Scala layer *)
   287             val (name, imports, uses) = Exn.release node_header;
   288             val master = Path.dir (Path.explode node_name);
   289           in Thy_Info.toplevel_begin_theory master name imports (map (apfst Path.explode) uses) end)
   290       else raw_tr;
   291 
   292     val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   293     val do_print = not is_init andalso (Toplevel.print_of tr orelse is_proof);
   294 
   295     val start = Timing.start ();
   296     val (errs, result) =
   297       if Toplevel.is_toplevel st andalso not is_init then ([], SOME st)
   298       else run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   299     val _ = timing tr (Timing.result start);
   300     val _ = List.app (Toplevel.error_msg tr) errs;
   301   in
   302     (case result of
   303       NONE => (Toplevel.status tr Markup.failed; st)
   304     | SOME st' =>
   305        (Toplevel.status tr Markup.finished;
   306         proof_status tr st';
   307         if do_print then async_state tr st' else ();
   308         st'))
   309   end;
   310 
   311 end;
   312 
   313 
   314 
   315 
   316 (** editing **)
   317 
   318 (* edit *)
   319 
   320 local
   321 
   322 fun is_changed node' ((_, id), exec) =
   323   (case try (the_entry node') id of
   324     NONE => true
   325   | SOME exec' => exec' <> exec);
   326 
   327 fun new_exec node_info (id: command_id) (exec_id, updates, state) =
   328   let
   329     val exec = the_exec state exec_id;
   330     val exec_id' = new_id ();
   331     val future_tr = the_command state id;
   332     val exec' =
   333       Lazy.lazy (fn () =>
   334         let
   335           val st = Lazy.force exec;
   336           val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join future_tr);
   337         in run_command node_info exec_tr st end);
   338     val state' = define_exec exec_id' exec' state;
   339   in (exec_id', (id, exec_id') :: updates, state') end;
   340 
   341 in
   342 
   343 fun edit (old_id: version_id) (new_id: version_id) edits state =
   344   let
   345     val old_version = the_version state old_id;
   346     val _ = Time.now ();  (* FIXME odd workaround for polyml-5.4.0/x86_64 *)
   347     val new_version = fold edit_nodes edits old_version;
   348 
   349     fun update_node name (rev_updates, version, st) =
   350       let
   351         val node = node_of version name;
   352         val header = get_header node;
   353       in
   354         (case first_entry NONE (is_changed (node_of old_version name)) node of
   355           NONE => (rev_updates, version, st)
   356         | SOME ((prev, id), _) =>
   357             let
   358               val prev_exec =
   359                 (case prev of
   360                   NONE => no_id
   361                 | SOME prev_id => the_default no_id (the_entry node prev_id));
   362               val (last, rev_upds, st') =
   363                 fold_entries (SOME id) (new_exec (name, header) o #2 o #1) node (prev_exec, [], st);
   364               val node' = node |> fold update_entry rev_upds |> set_last last;
   365             in (rev_upds @ rev_updates, put_node name node' version, st') end)
   366       end;
   367 
   368     (* FIXME Graph.schedule *)
   369     val (rev_updates, new_version', state') =
   370       fold update_node (node_names_of new_version) ([], new_version, state);
   371     val state'' = define_version new_id new_version' state';
   372 
   373     val _ = join_commands state'';  (* FIXME async!? *)
   374   in (rev rev_updates, state'') end;
   375 
   376 end;
   377 
   378 
   379 (* execute *)
   380 
   381 fun execute version_id state =
   382   state |> map_state (fn (versions, commands, execs, _) =>
   383     let
   384       val version = the_version state version_id;
   385 
   386       fun force_exec NONE = ()
   387         | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
   388 
   389       val execution' = (* FIXME Graph.schedule *)
   390         Future.forks
   391           {name = "Document.execute", group = NONE, deps = [], pri = 1, interrupts = true}
   392           [fn () =>
   393             node_names_of version |> List.app (fn name =>
   394               fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
   395                   (node_of version name) ())];
   396 
   397     in (versions, commands, execs, execution') end);
   398 
   399 
   400 
   401 (** global state **)
   402 
   403 val global_state = Synchronized.var "Document" init_state;
   404 
   405 fun state () = Synchronized.value global_state;
   406 val change_state = Synchronized.change global_state;
   407 
   408 end;
   409