src/Pure/PIDE/command.ML
author wenzelm
Mon Jul 15 10:42:12 2013 +0200 (2013-07-15 ago)
changeset 52657 42c14dba1daa
parent 52656 9437f440ef3f
child 52713 cd3ce844248f
permissions -rw-r--r--
tuned;
     1 (*  Title:      Pure/PIDE/command.ML
     2     Author:     Makarius
     3 
     4 Prover command execution: read -- eval -- print.
     5 *)
     6 
     7 signature COMMAND =
     8 sig
     9   val read: (unit -> theory) -> Token.T list -> Toplevel.transition
    10   type eval
    11   val eval_eq: eval * eval -> bool
    12   val eval_result_state: eval -> Toplevel.state
    13   val eval_stable: eval -> bool
    14   val eval: (unit -> theory) -> Token.T list -> eval -> eval
    15   type print
    16   val print: bool -> string -> eval -> print list -> print list option
    17   type print_fn = Toplevel.transition -> Toplevel.state -> unit
    18   val print_function: string ->
    19     ({command_name: string} ->
    20       {delay: Time.time, pri: int, persistent: bool, print_fn: print_fn} option) -> unit
    21   val no_print_function: string -> unit
    22   type exec = eval * print list
    23   val no_exec: exec
    24   val exec_ids: exec option -> Document_ID.exec list
    25   val exec: Document_ID.execution -> exec -> unit
    26 end;
    27 
    28 structure Command: COMMAND =
    29 struct
    30 
    31 (** memo results -- including physical interrupts! **)
    32 
    33 datatype 'a expr =
    34   Expr of Document_ID.exec * (unit -> 'a) |
    35   Result of 'a Exn.result;
    36 
    37 abstype 'a memo = Memo of 'a expr Synchronized.var
    38 with
    39 
    40 fun memo exec_id e = Memo (Synchronized.var "Command.memo" (Expr (exec_id, e)));
    41 fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
    42 
    43 fun memo_result (Memo v) =
    44   (case Synchronized.value v of
    45     Expr (exec_id, _) => error ("Unfinished execution result: " ^ Document_ID.print exec_id)
    46   | Result res => Exn.release res);
    47 
    48 fun memo_stable (Memo v) =
    49   (case Synchronized.value v of
    50    Expr _ => true
    51  | Result res => not (Exn.is_interrupt_exn res));
    52 
    53 fun memo_finished (Memo v) =
    54   (case Synchronized.value v of
    55    Expr _ => false
    56  | Result res => not (Exn.is_interrupt_exn res));
    57 
    58 fun memo_exec execution_id (Memo v) =
    59   Synchronized.guarded_access v
    60     (fn expr =>
    61       (case expr of
    62         Expr (exec_id, e) =>
    63           uninterruptible (fn restore_attributes => fn () =>
    64             if Execution.running execution_id exec_id then
    65               let
    66                 val res = Exn.capture (restore_attributes e) ();
    67                 val _ = Execution.finished exec_id;
    68               in SOME (Exn.is_interrupt_exn res, Result res) end
    69             else SOME (true, expr)) ()
    70       | Result _ => SOME (false, expr)))
    71   |> (fn true => Exn.interrupt () | false => ());
    72 
    73 fun memo_fork params execution_id (Memo v) =
    74   (case Synchronized.value v of
    75     Result _ => ()
    76   | _ => ignore ((singleton o Future.forks) params (fn () => memo_exec execution_id (Memo v))));
    77 
    78 end;
    79 
    80 
    81 
    82 (** main phases of execution **)
    83 
    84 (* read *)
    85 
    86 fun read init span =
    87   let
    88     val outer_syntax = #2 (Outer_Syntax.get_syntax ());
    89     val command_reports = Outer_Syntax.command_reports outer_syntax;
    90 
    91     val proper_range =
    92       Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
    93     val pos =
    94       (case find_first Token.is_command span of
    95         SOME tok => Token.position_of tok
    96       | NONE => proper_range);
    97 
    98     val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
    99     val _ = Position.reports_text (token_reports @ maps command_reports span);
   100   in
   101     if is_malformed then Toplevel.malformed pos "Malformed command syntax"
   102     else
   103       (case Outer_Syntax.read_spans outer_syntax span of
   104         [tr] =>
   105           if Keyword.is_control (Toplevel.name_of tr) then
   106             Toplevel.malformed pos "Illegal control command"
   107           else Toplevel.modify_init init tr
   108       | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
   109       | _ => Toplevel.malformed proper_range "Exactly one command expected")
   110       handle ERROR msg => Toplevel.malformed proper_range msg
   111   end;
   112 
   113 
   114 (* eval *)
   115 
   116 type eval_state =
   117   {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
   118 val init_eval_state =
   119   {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
   120 
   121 datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
   122 
   123 fun eval_eq (Eval {exec_id, ...}, Eval {exec_id = exec_id', ...}) = exec_id = exec_id';
   124 
   125 fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
   126 val eval_result_state = #state o eval_result;
   127 
   128 fun eval_stable (Eval {exec_id, eval_process}) =
   129   Goal.stable_futures exec_id andalso memo_stable eval_process;
   130 
   131 local
   132 
   133 fun run int tr st =
   134   if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
   135     (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
   136       (fn () => Toplevel.command_exception int tr st); ([], SOME st))
   137   else Toplevel.command_errors int tr st;
   138 
   139 fun check_cmts span tr st' =
   140   Toplevel.setmp_thread_position tr
   141     (fn () =>
   142       Outer_Syntax.side_comments span |> maps (fn cmt =>
   143         (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
   144           handle exn =>
   145             if Exn.is_interrupt exn then reraise exn
   146             else ML_Compiler.exn_messages_ids exn)) ();
   147 
   148 fun proof_status tr st =
   149   (case try Toplevel.proof_of st of
   150     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   151   | NONE => ());
   152 
   153 fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
   154   if malformed then
   155     {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
   156   else
   157     let
   158       val malformed' = Toplevel.is_malformed tr;
   159       val is_init = Toplevel.is_init tr;
   160       val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   161 
   162       val _ = Multithreading.interrupted ();
   163       val _ = Toplevel.status tr Markup.running;
   164       val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   165       val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
   166       val errs = errs1 @ errs2;
   167       val _ = Toplevel.status tr Markup.finished;
   168       val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
   169     in
   170       (case result of
   171         NONE =>
   172           let
   173             val _ = if null errs then Exn.interrupt () else ();
   174             val _ = Toplevel.status tr Markup.failed;
   175           in {failed = true, malformed = malformed', command = tr, state = st} end
   176       | SOME st' =>
   177           let
   178             val _ = proof_status tr st';
   179           in {failed = false, malformed = malformed', command = tr, state = st'} end)
   180     end;
   181 
   182 in
   183 
   184 fun eval init span eval0 =
   185   let
   186     val exec_id = Document_ID.make ();
   187     fun process () =
   188       let
   189         val tr =
   190           Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
   191             (fn () => read init span |> Toplevel.exec_id exec_id) ();
   192       in eval_state span tr (eval_result eval0) end;
   193   in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
   194 
   195 end;
   196 
   197 
   198 (* print *)
   199 
   200 datatype print = Print of
   201  {name: string, delay: Time.time, pri: int, persistent: bool,
   202   exec_id: Document_ID.exec, print_process: unit memo};
   203 
   204 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
   205 
   206 type print_function =
   207   {command_name: string} ->
   208     {delay: Time.time, pri: int, persistent: bool, print_fn: print_fn} option;
   209 
   210 local
   211 
   212 val print_functions =
   213   Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
   214 
   215 fun print_error tr e =
   216   (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) e ()
   217     handle exn =>
   218       if Exn.is_interrupt exn then reraise exn
   219       else List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
   220 
   221 fun print_eq (Print {exec_id, ...}, Print {exec_id = exec_id', ...}) = exec_id = exec_id';
   222 
   223 fun print_stable (Print {exec_id, print_process, ...}) =
   224   Goal.stable_futures exec_id andalso memo_stable print_process;
   225 
   226 fun print_finished (Print {exec_id, print_process, ...}) =
   227   Goal.stable_futures exec_id andalso memo_finished print_process;
   228 
   229 fun print_persistent (Print {persistent, ...}) = persistent;
   230 
   231 in
   232 
   233 fun print command_visible command_name eval old_prints =
   234   let
   235     fun new_print (name, get_pr) =
   236       let
   237         fun make_print strict {delay, pri, persistent, print_fn} =
   238           let
   239             val exec_id = Document_ID.make ();
   240             fun process () =
   241               let
   242                 val {failed, command, state = st', ...} = eval_result eval;
   243                 val tr = Toplevel.exec_id exec_id command;
   244               in
   245                 if failed andalso not strict then ()
   246                 else print_error tr (fn () => print_fn tr st')
   247               end;
   248           in
   249            Print {
   250              name = name, delay = delay, pri = pri, persistent = persistent,
   251              exec_id = exec_id, print_process = memo exec_id process}
   252           end;
   253       in
   254         (case Exn.capture (Runtime.controlled_execution get_pr) {command_name = command_name} of
   255           Exn.Res NONE => NONE
   256         | Exn.Res (SOME pr) => SOME (make_print false pr)
   257         | Exn.Exn exn =>
   258             SOME (make_print true
   259               {delay = Time.zeroTime, pri = 0, persistent = false,
   260                 print_fn = fn _ => fn _ => reraise exn}))
   261       end;
   262 
   263     val new_prints =
   264       if command_visible then
   265         rev (Synchronized.value print_functions) |> map_filter (fn pr =>
   266           (case find_first (fn Print {name, ...} => name = fst pr) old_prints of
   267             SOME print => if print_stable print then SOME print else new_print pr
   268           | NONE => new_print pr))
   269       else filter (fn print => print_finished print andalso print_persistent print) old_prints;
   270   in
   271     if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
   272   end;
   273 
   274 fun print_function name f =
   275   Synchronized.change print_functions (fn funs =>
   276    (if not (AList.defined (op =) funs name) then ()
   277     else warning ("Redefining command print function: " ^ quote name);
   278     AList.update (op =) (name, f) funs));
   279 
   280 fun no_print_function name =
   281   Synchronized.change print_functions (filter_out (equal name o #1));
   282 
   283 end;
   284 
   285 val _ =
   286   print_function "print_state"
   287     (fn {command_name} =>
   288       SOME {delay = Time.zeroTime, pri = 0, persistent = true,
   289         print_fn = fn tr => fn st' =>
   290           let
   291             val is_init = Keyword.is_theory_begin command_name;
   292             val is_proof = Keyword.is_proof command_name;
   293             val do_print =
   294               not is_init andalso
   295                 (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
   296           in if do_print then Toplevel.print_state false st' else () end});
   297 
   298 
   299 (* combined execution *)
   300 
   301 type exec = eval * print list;
   302 val no_exec: exec =
   303   (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
   304 
   305 fun exec_ids NONE = []
   306   | exec_ids (SOME (Eval {exec_id, ...}, prints)) =
   307       exec_id :: map (fn Print {exec_id, ...} => exec_id) prints;
   308 
   309 local
   310 
   311 fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
   312   if Multithreading.enabled () then
   313     let
   314       val group = Future.worker_subgroup ();
   315       fun fork () =
   316         memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
   317           execution_id print_process;
   318     in
   319       if delay = Time.zeroTime then fork ()
   320       else ignore (Event_Timer.request (Time.+ (Time.now (), delay)) fork)
   321     end
   322   else memo_exec execution_id print_process;
   323 
   324 in
   325 
   326 fun exec execution_id (Eval {eval_process, ...}, prints) =
   327   (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
   328 
   329 end;
   330 
   331 end;
   332