src/Pure/PIDE/document.ML
author wenzelm
Sat, 02 Jul 2011 20:22:02 +0200
changeset 44519 9ef5479da29f
parent 43199 61879dc97e72
child 44541 7be2e51928cb
permissions -rw-r--r--
tuned;
     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 edit = string * ((command_id option * command_id option) list) option
    19   type state
    20   val init_state: state
    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
    25 end;
    26 
    27 structure Document: DOCUMENT =
    28 struct
    29 
    30 (* unique identifiers *)
    31 
    32 type id = int;
    33 type version_id = id;
    34 type command_id = id;
    35 type exec_id = id;
    36 
    37 val no_id = 0;
    38 val new_id = Synchronized.counter ();
    39 
    40 val parse_id = Markup.parse_int;
    41 val print_id = Markup.print_int;
    42 
    43 fun err_dup kind id = error ("Duplicate " ^ kind ^ ": " ^ print_id id);
    44 fun err_undef kind id = error ("Undefined " ^ kind ^ ": " ^ print_id id);
    45 
    46 
    47 
    48 (** document structure **)
    49 
    50 structure Entries = Linear_Set(type key = command_id val ord = int_ord);
    51 
    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*)
    54 with
    55 
    56 val empty_node = Node Entries.empty;
    57 val empty_version = Version Graph.empty;
    58 
    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;
    61 
    62 
    63 (* node edits and associated executions *)
    64 
    65 type edit =
    66   string *
    67   (*NONE: remove node, SOME: insert/remove commands*)
    68   ((command_id option * command_id option) list) option;
    69 
    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);
    74 
    75 fun update_entry (id, exec_id) (Node entries) =
    76   Node (Entries.update (id, SOME exec_id) entries);
    77 
    78 fun reset_after id entries =
    79   (case Entries.get_after entries id of
    80     NONE => entries
    81   | SOME next => Entries.update (next, NONE) entries);
    82 
    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);
    87 
    88 
    89 (* version operations *)
    90 
    91 fun nodes_of (Version nodes) = nodes;
    92 val node_names_of = Graph.keys o nodes_of;
    93 
    94 fun get_node version name = Graph.get_node (nodes_of version) name
    95   handle Graph.UNDEF _ => empty_node;
    96 
    97 fun edit_nodes (name, SOME edits) (Version nodes) =
    98       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);
   103 
   104 fun put_node name node (Version nodes) =
   105   Version (nodes
   106     |> Graph.default_node (name, empty_node)
   107     |> Graph.map_node name (K node));
   108 
   109 end;
   110 
   111 
   112 
   113 (** global state -- document structure and execution process **)
   114 
   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*)
   120 with
   121 
   122 fun make_state (versions, commands, execs, execution) =
   123   State {versions = versions, commands = commands, execs = execs, execution = execution};
   124 
   125 fun map_state f (State {versions, commands, execs, execution}) =
   126   make_state (f (versions, commands, execs, execution));
   127 
   128 val init_state =
   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))],
   132     []);
   133 
   134 
   135 (* document versions *)
   136 
   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);
   142 
   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);
   147 
   148 
   149 (* commands *)
   150 
   151 fun define_command (id: command_id) text =
   152   map_state (fn (versions, commands, execs, execution) =>
   153     let
   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) ());
   158       val commands' =
   159         Inttab.update_new (id, tr) commands
   160           handle Inttab.DUP dup => err_dup "command" dup;
   161     in (versions, commands', execs, execution) end);
   162 
   163 fun the_command (State {commands, ...}) (id: command_id) =
   164   (case Inttab.lookup commands id of
   165     NONE => err_undef "command" id
   166   | SOME tr => tr);
   167 
   168 fun join_commands (State {commands, ...}) =
   169   Inttab.fold (fn (_, tr) => fn () => ignore (Future.join_result tr)) commands ();
   170 
   171 
   172 (* command executions *)
   173 
   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);
   179 
   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);
   184 
   185 
   186 (* document execution *)
   187 
   188 fun cancel (State {execution, ...}) =
   189   List.app Future.cancel execution;
   190 
   191 fun await_cancellation (State {execution, ...}) =
   192   ignore (Future.join_results execution);
   193 
   194 end;
   195 
   196 
   197 
   198 (* toplevel transactions *)
   199 
   200 local
   201 
   202 fun timing tr t = Toplevel.status tr (Markup.timing t);
   203 
   204 fun proof_status tr st =
   205   (case try Toplevel.proof_of st of
   206     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   207   | NONE => ());
   208 
   209 fun async_state tr st =
   210   ignore
   211     (singleton
   212       (Future.forks {name = "Document.async_state",
   213         group = SOME (Task_Queue.new_group NONE), deps = [], pri = 0})
   214       (fn () =>
   215         Toplevel.setmp_thread_position tr
   216           (fn () => Toplevel.print_state false st) ()));
   217 
   218 fun run int tr 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));
   226 
   227 in
   228 
   229 fun run_command thy_name raw_tr st =
   230   (case
   231       (case Toplevel.init_of raw_tr of
   232         SOME name => Exn.capture (fn () =>
   233           let
   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
   238     Exn.Result tr =>
   239       let
   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);
   243 
   244         val start = Timing.start ();
   245         val (errs, result) =
   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;
   250         val res =
   251           (case result of
   252             NONE => (Toplevel.status tr Markup.failed; (false, st))
   253           | SOME st' =>
   254              (Toplevel.status tr Markup.finished;
   255               proof_status tr st';
   256               if do_print then async_state tr st' else ();
   257               (true, st')));
   258       in res end
   259   | Exn.Exn exn =>
   260       if Exn.is_interrupt exn then reraise exn
   261       else
   262        (Toplevel.error_msg raw_tr (ML_Compiler.exn_message exn);
   263         Toplevel.status raw_tr Markup.failed;
   264         (false, Toplevel.toplevel)));
   265 
   266 end;
   267 
   268 
   269 
   270 
   271 (** editing **)
   272 
   273 (* edit *)
   274 
   275 local
   276 
   277 fun is_changed node' ((_, id), exec) =
   278   (case try (the_entry node') id of
   279     NONE => true
   280   | SOME exec' => exec' <> exec);
   281 
   282 fun new_exec name (id: command_id) (exec_id, updates, state) =
   283   let
   284     val exec = the_exec state exec_id;
   285     val exec_id' = new_id ();
   286     val future_tr = the_command state id;
   287     val exec' =
   288       Lazy.lazy (fn () =>
   289         let
   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;
   295 
   296 in
   297 
   298 fun edit (old_id: version_id) (new_id: version_id) edits state =
   299   let
   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;
   303 
   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), _) =>
   309             let
   310               val prev_exec =
   311                 (case prev of
   312                   NONE => no_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)
   318       end;
   319 
   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';
   324 
   325     val _ = join_commands state'';  (* FIXME async!? *)
   326   in (rev rev_updates, state'') end;
   327 
   328 end;
   329 
   330 
   331 (* execute *)
   332 
   333 fun execute version_id state =
   334   state |> map_state (fn (versions, commands, execs, _) =>
   335     let
   336       val version = the_version state version_id;
   337 
   338       fun force_exec NONE = ()
   339         | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
   340 
   341       val _ = cancel state;
   342 
   343       val execution' = (* FIXME proper node deps *)
   344         Future.forks {name = "Document.execute", group = NONE, deps = [], pri = 1}
   345           [fn () =>
   346             let
   347               val _ = await_cancellation state;
   348               val _ =
   349                 node_names_of version |> List.app (fn name =>
   350                   fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
   351                       (get_node version name) ());
   352             in () end];
   353 
   354       val _ = await_cancellation state;  (* FIXME async!? *)
   355 
   356     in (versions, commands, execs, execution') end);
   357 
   358 end;
   359