src/Pure/PIDE/command.ML
author wenzelm
Wed Jul 03 23:02:00 2013 +0200 (2013-07-03 ago)
changeset 52516 b5b3c888df9f
parent 52515 0dcadc90550b
child 52526 d234cb6b60e3
permissions -rw-r--r--
more exception handling -- make print functions total;
     1 (*  Title:      Pure/PIDE/command.ML
     2     Author:     Makarius
     3 
     4 Prover command execution.
     5 *)
     6 
     7 signature COMMAND =
     8 sig
     9   type span = Token.T list
    10   val range: span -> Position.range
    11   val proper_range: span -> Position.range
    12   type 'a memo
    13   val memo: (unit -> 'a) -> 'a memo
    14   val memo_value: 'a -> 'a memo
    15   val memo_eval: 'a memo -> 'a
    16   val memo_result: 'a memo -> 'a
    17   val read: span -> Toplevel.transition
    18   val eval: span -> Toplevel.transition ->
    19     Toplevel.state * {malformed: bool} -> {failed: bool} * (Toplevel.state * {malformed: bool})
    20   type print = {name: string, pri: int, pr: unit lazy}
    21   val print: Toplevel.state -> Toplevel.transition -> Toplevel.state -> print list
    22   type print_function =
    23     {old_state: Toplevel.state, tr: Toplevel.transition, state: Toplevel.state} ->
    24       (unit -> unit) option
    25   val print_function: string -> int -> print_function -> unit
    26 end;
    27 
    28 structure Command: COMMAND =
    29 struct
    30 
    31 (* source *)
    32 
    33 type span = Token.T list;
    34 
    35 val range = Token.position_range_of;
    36 val proper_range = Token.position_range_of o #1 o take_suffix Token.is_improper;
    37 
    38 
    39 (* memo results *)
    40 
    41 datatype 'a expr =
    42   Expr of unit -> 'a |
    43   Result of 'a Exn.result;
    44 
    45 abstype 'a memo = Memo of 'a expr Synchronized.var
    46 with
    47 
    48 fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
    49 fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
    50 
    51 fun memo_eval (Memo v) =
    52   (case Synchronized.value v of
    53     Result res => res
    54   | _ =>
    55       Synchronized.guarded_access v
    56         (fn Result res => SOME (res, Result res)
    57           | Expr e =>
    58               let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
    59               in SOME (res, Result res) end))
    60   |> Exn.release;
    61 
    62 fun memo_result (Memo v) =
    63   (case Synchronized.value v of
    64     Result res => Exn.release res
    65   | _ => raise Fail "Unfinished memo result");
    66 
    67 end;
    68 
    69 
    70 (* read *)
    71 
    72 fun read span =
    73   let
    74     val outer_syntax = #2 (Outer_Syntax.get_syntax ());
    75     val command_reports = Outer_Syntax.command_reports outer_syntax;
    76 
    77     val proper_range = Position.set_range (proper_range span);
    78     val pos =
    79       (case find_first Token.is_command span of
    80         SOME tok => Token.position_of tok
    81       | NONE => proper_range);
    82 
    83     val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
    84     val _ = Position.reports_text (token_reports @ maps command_reports span);
    85   in
    86     if is_malformed then Toplevel.malformed pos "Malformed command syntax"
    87     else
    88       (case Outer_Syntax.read_spans outer_syntax span of
    89         [tr] =>
    90           if Keyword.is_control (Toplevel.name_of tr) then
    91             Toplevel.malformed pos "Illegal control command"
    92           else tr
    93       | [] => Toplevel.ignored (Position.set_range (range span))
    94       | _ => Toplevel.malformed proper_range "Exactly one command expected")
    95       handle ERROR msg => Toplevel.malformed proper_range msg
    96   end;
    97 
    98 
    99 (* eval *)
   100 
   101 local
   102 
   103 fun run int tr st =
   104   if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
   105     (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
   106       (fn () => Toplevel.command_exception int tr st); ([], SOME st))
   107   else Toplevel.command_errors int tr st;
   108 
   109 fun check_cmts span tr st' =
   110   Toplevel.setmp_thread_position tr
   111     (fn () =>
   112       Outer_Syntax.side_comments span |> maps (fn cmt =>
   113         (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
   114           handle exn => ML_Compiler.exn_messages_ids exn)) ();
   115 
   116 fun proof_status tr st =
   117   (case try Toplevel.proof_of st of
   118     SOME prf => Toplevel.status tr (Proof.status_markup prf)
   119   | NONE => ());
   120 
   121 in
   122 
   123 fun eval span tr (st, {malformed}) =
   124   if malformed then
   125     ({failed = true}, (Toplevel.toplevel, {malformed = malformed}))
   126   else
   127     let
   128       val malformed' = Toplevel.is_malformed tr;
   129       val is_init = Toplevel.is_init tr;
   130       val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   131 
   132       val _ = Multithreading.interrupted ();
   133       val _ = Toplevel.status tr Markup.running;
   134       val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
   135       val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
   136       val errs = errs1 @ errs2;
   137       val _ = Toplevel.status tr Markup.finished;
   138       val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
   139     in
   140       (case result of
   141         NONE =>
   142           let
   143             val _ = if null errs then Exn.interrupt () else ();
   144             val _ = Toplevel.status tr Markup.failed;
   145           in ({failed = true}, (st, {malformed = malformed'})) end
   146       | SOME st' =>
   147           let
   148             val _ = proof_status tr st';
   149           in ({failed = false}, (st', {malformed = malformed'})) end)
   150     end;
   151 
   152 end;
   153 
   154 
   155 (* print *)
   156 
   157 type print_function =
   158   {old_state: Toplevel.state, tr: Toplevel.transition, state: Toplevel.state} ->
   159     (unit -> unit) option;
   160 
   161 type print = {name: string, pri: int, pr: unit lazy};
   162 
   163 local
   164 
   165 val print_functions =
   166   Synchronized.var "Command.print_functions" ([]: (string * (int * print_function)) list);
   167 
   168 fun output_error tr exn =
   169   List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
   170 
   171 fun print_error tr f x =
   172   (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) f x
   173     handle exn => output_error tr exn;
   174 
   175 in
   176 
   177 fun print st tr st' =
   178   rev (Synchronized.value print_functions) |> map_filter (fn (name, (pri, f)) =>
   179     (case Exn.capture (Runtime.controlled_execution f) {old_state = st, tr = tr, state = st'} of
   180       Exn.Res NONE => NONE
   181     | Exn.Res (SOME pr) => SOME {name = name, pri = pri, pr = (Lazy.lazy o print_error tr) pr}
   182     | Exn.Exn exn => SOME {name = name, pri = pri, pr = Lazy.lazy (fn () => output_error tr exn)}));
   183 
   184 fun print_function name pri f =
   185   Synchronized.change print_functions (fn funs =>
   186    (if not (AList.defined (op =) funs name) then ()
   187     else warning ("Redefining command print function: " ^ quote name);
   188     AList.update (op =) (name, (pri, f)) funs));
   189 
   190 end;
   191 
   192 val _ = print_function "print_state" 0 (fn {tr, state, ...} =>
   193   let
   194     val is_init = Toplevel.is_init tr;
   195     val is_proof = Keyword.is_proof (Toplevel.name_of tr);
   196     val do_print =
   197       not is_init andalso
   198         (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof state));
   199   in if do_print then SOME (fn () => Toplevel.print_state false state) else NONE end);
   200 
   201 end;
   202