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