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