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