src/Pure/PIDE/document.ML
author wenzelm
Mon Jan 31 22:57:01 2011 +0100 (2011-01-31)
changeset 41673 1c191a39549f
parent 41672 2f70b1ddd09f
child 41674 7da257539a8d
permissions -rw-r--r--
support named tasks, for improved tracing;
     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 (hook, SOME id2) (Node entries) =
    84       Node (Entries.insert_after hook (id2, NONE) entries)
    85   | edit_node (hook, NONE) (Node entries) =
    86       Node (entries |> Entries.delete_after hook |> reset_after hook);
    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.bulk {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 = start_timing ();
   245         val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   246         val _ = timing tr (end_timing start);
   247         val _ = List.app (Toplevel.error_msg tr) errs;
   248         val res =
   249           (case result of
   250             NONE => (Toplevel.status tr Markup.failed; (false, st))
   251           | SOME st' =>
   252              (Toplevel.status tr Markup.finished;
   253               proof_status tr st';
   254               if do_print then async_state tr st' else ();
   255               (true, st')));
   256       in res end
   257   | Exn.Exn exn =>
   258       if Exn.is_interrupt exn then reraise exn
   259       else
   260        (Toplevel.error_msg raw_tr (ML_Compiler.exn_message exn);
   261         Toplevel.status raw_tr Markup.failed;
   262         (false, Toplevel.toplevel)));
   263 
   264 end;
   265 
   266 
   267 
   268 
   269 (** editing **)
   270 
   271 (* edit *)
   272 
   273 local
   274 
   275 fun is_changed node' ((_, id), exec) =
   276   (case try (the_entry node') id of
   277     NONE => true
   278   | SOME exec' => exec' <> exec);
   279 
   280 fun new_exec name (id: command_id) (exec_id, updates, state) =
   281   let
   282     val exec = the_exec state exec_id;
   283     val exec_id' = new_id ();
   284     val future_tr = the_command state id;
   285     val exec' =
   286       Lazy.lazy (fn () =>
   287         let
   288           val st = #2 (Lazy.force exec);
   289           val exec_tr = Toplevel.put_id (print_id exec_id') (Future.join future_tr);
   290         in run_command name exec_tr st end);
   291     val state' = define_exec exec_id' exec' state;
   292   in (exec_id', (id, exec_id') :: updates, state') end;
   293 
   294 in
   295 
   296 fun edit (old_id: version_id) (new_id: version_id) edits state =
   297   let
   298     val old_version = the_version state old_id;
   299     val _ = Time.now ();  (* FIXME odd workaround *)
   300     val new_version = fold edit_nodes edits old_version;
   301 
   302     fun update_node name (rev_updates, version, st) =
   303       let val node = get_node version name in
   304         (case first_entry NONE (is_changed (get_node old_version name)) node of
   305           NONE => (rev_updates, version, st)
   306         | SOME ((prev, id), _) =>
   307             let
   308               val prev_exec =
   309                 (case prev of
   310                   NONE => no_id
   311                 | SOME prev_id => the_default no_id (the_entry node prev_id));
   312               val (_, rev_upds, st') =
   313                 fold_entries (SOME id) (new_exec name o #2 o #1) node (prev_exec, [], st);
   314               val node' = fold update_entry rev_upds node;
   315             in (rev_upds @ rev_updates, put_node name node' version, st') end)
   316       end;
   317 
   318     (* FIXME proper node deps *)
   319     val (rev_updates, new_version', state') =
   320       fold update_node (node_names_of new_version) ([], new_version, state);
   321     val state'' = define_version new_id new_version' state';
   322 
   323     val _ = join_commands state'';  (* FIXME async!? *)
   324   in (rev rev_updates, state'') end;
   325 
   326 end;
   327 
   328 
   329 (* execute *)
   330 
   331 fun execute version_id state =
   332   state |> map_state (fn (versions, commands, execs, _) =>
   333     let
   334       val version = the_version state version_id;
   335 
   336       fun force_exec NONE = ()
   337         | force_exec (SOME exec_id) = ignore (Lazy.force (the_exec state exec_id));
   338 
   339       val _ = cancel state;
   340 
   341       val execution' = (* FIXME proper node deps *)
   342         Future.bulk {name = "Document.execute", group = NONE, deps = [], pri = 1}
   343           [fn () =>
   344             let
   345               val _ = await_cancellation state;
   346               val _ =
   347                 node_names_of version |> List.app (fn name =>
   348                   fold_entries NONE (fn (_, exec) => fn () => force_exec exec)
   349                       (get_node version name) ());
   350             in () end];
   351 
   352       val _ = await_cancellation state;  (* FIXME async!? *)
   353 
   354     in (versions, commands, execs, execution') end);
   355 
   356 end;
   357