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