src/Pure/PIDE/command.ML
author wenzelm
Fri, 05 Jul 2013 22:09:16 +0200
changeset 53672 b7badd371e4d
parent 53671 341ae9cd4743
child 53673 3a35ce87a55c
permissions -rw-r--r--
tuned signature -- eliminated pointless type synonym;
     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   type print_process
    12   type print = {name: string, pri: int, exec_id: Document_ID.exec, print_process: print_process}
    13   type exec = eval * print list
    14   val read: (unit -> theory) -> Token.T list -> Toplevel.transition
    15   val no_eval: eval
    16   val eval_result_state: eval -> Toplevel.state
    17   val eval: (unit -> theory) -> Token.T list -> eval -> eval
    18   val print: string -> eval -> print list
    19   type print_fn = Toplevel.transition -> Toplevel.state -> unit
    20   val print_function: {name: string, pri: int} -> (string -> print_fn option) -> unit
    21   val no_exec: exec
    22   val exec_ids: exec -> Document_ID.exec list
    23   val exec_run: exec -> unit
    24   val stable_eval: eval -> bool
    25   val stable_print: print -> bool
    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 **)
    74 
    75 type eval_state =
    76   {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
    77 type eval_process = eval_state memo;
    78 type eval = {exec_id: Document_ID.exec, eval_process: eval_process};
    79 
    80 type print_process = unit memo;
    81 type print = {name: string, pri: int, exec_id: Document_ID.exec, print_process: print_process};
    82 
    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 val no_eval_state: eval_state =
   117   {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
   118 
   119 val no_eval: eval = {exec_id = Document_ID.none, eval_process = memo_value no_eval_state};
   120 
   121 fun eval_result ({eval_process, ...}: eval) = memo_result eval_process;
   122 val eval_result_state = #state o eval_result;
   123 
   124 local
   125 
   126 fun run int tr st =
   127   if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
   128     (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
   129       (fn () => Toplevel.command_exception int tr st); ([], SOME st))
   130   else Toplevel.command_errors int tr st;
   131 
   132 fun check_cmts span tr st' =
   133   Toplevel.setmp_thread_position tr
   134     (fn () =>
   135       Outer_Syntax.side_comments span |> maps (fn cmt =>
   136         (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
   137           handle exn => ML_Compiler.exn_messages_ids exn)) ();
   138 
   139 fun proof_status tr st =
   140   (case try Toplevel.proof_of st of
   141     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   142   | NONE => ());
   143 
   144 fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
   145   if malformed then
   146     {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
   147   else
   148     let
   149       val malformed' = Toplevel.is_malformed tr;
   150       val is_init = Toplevel.is_init tr;
   151       val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   152 
   153       val _ = Multithreading.interrupted ();
   154       val _ = Toplevel.status tr Markup.running;
   155       val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   156       val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
   157       val errs = errs1 @ errs2;
   158       val _ = Toplevel.status tr Markup.finished;
   159       val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
   160     in
   161       (case result of
   162         NONE =>
   163           let
   164             val _ = if null errs then Exn.interrupt () else ();
   165             val _ = Toplevel.status tr Markup.failed;
   166           in {failed = true, malformed = malformed', command = tr, state = st} end
   167       | SOME st' =>
   168           let
   169             val _ = proof_status tr st';
   170           in {failed = false, malformed = malformed', command = tr, state = st'} end)
   171     end;
   172 
   173 in
   174 
   175 fun eval init span eval0 =
   176   let
   177     val exec_id = Document_ID.make ();
   178     fun process () =
   179       let
   180         val tr =
   181           Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
   182             (fn () => read init span |> Toplevel.put_id exec_id) ();
   183       in eval_state span tr (eval_result eval0) end;
   184   in {exec_id = exec_id, eval_process = memo process} end;
   185 
   186 end;
   187 
   188 
   189 (* print *)
   190 
   191 type print_fn = Toplevel.transition -> Toplevel.state -> unit;
   192 
   193 local
   194 
   195 type print_function = string * (int * (string -> print_fn option));
   196 val print_functions = Synchronized.var "Command.print_functions" ([]: print_function list);
   197 
   198 fun output_error tr exn =
   199   List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
   200 
   201 fun print_error tr f x =
   202   (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) f x
   203     handle exn => output_error tr exn;
   204 
   205 in
   206 
   207 fun print command_name eval =
   208   rev (Synchronized.value print_functions) |> map_filter (fn (name, (pri, get_print_fn)) =>
   209     (case Exn.capture (Runtime.controlled_execution get_print_fn) command_name of
   210       Exn.Res NONE => NONE
   211     | Exn.Res (SOME print_fn) =>
   212         let
   213           val exec_id = Document_ID.make ();
   214           fun process () =
   215             let
   216               val {failed, command, state = st', ...} = eval_result eval;
   217               val tr = Toplevel.put_id exec_id command;
   218             in if failed then () else print_error tr (fn () => print_fn tr st') () end;
   219         in SOME {name = name, pri = pri, exec_id = exec_id, print_process = memo process} end
   220     | Exn.Exn exn =>
   221         let
   222           val exec_id = Document_ID.make ();
   223           fun process () =
   224             let
   225               val {command, ...} = eval_result eval;
   226               val tr = Toplevel.put_id exec_id command;
   227             in output_error tr exn end;
   228         in SOME {name = name, pri = pri, exec_id = exec_id, print_process = memo process} end));
   229 
   230 fun print_function {name, pri} f =
   231   Synchronized.change print_functions (fn funs =>
   232    (if not (AList.defined (op =) funs name) then ()
   233     else warning ("Redefining command print function: " ^ quote name);
   234     AList.update (op =) (name, (pri, f)) funs));
   235 
   236 end;
   237 
   238 val _ =
   239   print_function {name = "print_state", pri = 0} (fn command_name => SOME (fn tr => fn st' =>
   240     let
   241       val is_init = Keyword.is_theory_begin command_name;
   242       val is_proof = Keyword.is_proof command_name;
   243       val do_print =
   244         not is_init andalso
   245           (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
   246     in if do_print then Toplevel.print_state false st' else () end));
   247 
   248 
   249 
   250 (** managed evaluation **)
   251 
   252 (* execution *)
   253 
   254 type exec = eval * print list;
   255 val no_exec: exec = (no_eval, []);
   256 
   257 fun exec_ids (({exec_id, ...}, prints): exec) = exec_id :: map #exec_id prints;
   258 
   259 fun exec_run (({eval_process, ...}, prints): exec) =
   260  (memo_eval eval_process;
   261   prints |> List.app (fn {name, pri, print_process, ...} =>
   262     memo_fork {name = name, group = NONE, deps = [], pri = pri, interrupts = true} print_process));
   263 
   264 
   265 (* stable situations after cancellation *)
   266 
   267 fun stable_goals exec_id =
   268   not (Par_Exn.is_interrupted (Future.join_results (Goal.peek_futures exec_id)));
   269 
   270 fun stable_eval ({exec_id, eval_process}: eval) =
   271   stable_goals exec_id andalso memo_stable eval_process;
   272 
   273 fun stable_print ({exec_id, print_process, ...}: print) =
   274   stable_goals exec_id andalso memo_stable print_process;
   275 
   276 end;
   277