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