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