src/Pure/PIDE/command.ML
author wenzelm
Wed Jul 03 16:58:35 2013 +0200 (2013-07-03 ago)
changeset 52510 a4a102237ded
parent 52509 2193d2c7f586
child 52511 d5d2093ff224
permissions -rw-r--r--
tuned signature;
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@52509
    20
  val no_print: unit lazy
wenzelm@52509
    21
  val print: Toplevel.transition -> Toplevel.state -> unit lazy
wenzelm@47336
    22
end;
wenzelm@47336
    23
wenzelm@47336
    24
structure Command: COMMAND =
wenzelm@47336
    25
struct
wenzelm@47336
    26
wenzelm@52509
    27
(* source *)
wenzelm@52509
    28
wenzelm@52509
    29
type span = Token.T list;
wenzelm@48771
    30
wenzelm@49866
    31
val range = Token.position_range_of;
wenzelm@51266
    32
val proper_range = Token.position_range_of o #1 o take_suffix Token.is_improper;
wenzelm@48771
    33
wenzelm@48771
    34
wenzelm@47341
    35
(* memo results *)
wenzelm@47341
    36
wenzelm@47341
    37
datatype 'a expr =
wenzelm@47341
    38
  Expr of unit -> 'a |
wenzelm@47341
    39
  Result of 'a Exn.result;
wenzelm@47341
    40
wenzelm@47341
    41
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    42
with
wenzelm@47341
    43
wenzelm@47341
    44
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
wenzelm@47341
    45
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    46
wenzelm@47342
    47
fun memo_eval (Memo v) =
wenzelm@47341
    48
  (case Synchronized.value v of
wenzelm@47341
    49
    Result res => res
wenzelm@47341
    50
  | _ =>
wenzelm@47341
    51
      Synchronized.guarded_access v
wenzelm@47341
    52
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    53
          | Expr e =>
wenzelm@47341
    54
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47342
    55
              in SOME (res, Result res) end))
wenzelm@47342
    56
  |> Exn.release;
wenzelm@47342
    57
wenzelm@47342
    58
fun memo_result (Memo v) =
wenzelm@47342
    59
  (case Synchronized.value v of
wenzelm@47342
    60
    Result res => Exn.release res
wenzelm@47342
    61
  | _ => raise Fail "Unfinished memo result");
wenzelm@47341
    62
wenzelm@47341
    63
end;
wenzelm@47341
    64
wenzelm@47341
    65
wenzelm@52510
    66
(* read *)
wenzelm@52509
    67
wenzelm@52510
    68
fun read span =
wenzelm@52510
    69
  let
wenzelm@52510
    70
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
    71
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
    72
wenzelm@52510
    73
    val proper_range = Position.set_range (proper_range span);
wenzelm@52510
    74
    val pos =
wenzelm@52510
    75
      (case find_first Token.is_command span of
wenzelm@52510
    76
        SOME tok => Token.position_of tok
wenzelm@52510
    77
      | NONE => proper_range);
wenzelm@52509
    78
wenzelm@52510
    79
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
    80
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
    81
  in
wenzelm@52510
    82
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
    83
    else
wenzelm@52510
    84
      (case Outer_Syntax.read_spans outer_syntax span of
wenzelm@52510
    85
        [tr] =>
wenzelm@52510
    86
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
    87
            Toplevel.malformed pos "Illegal control command"
wenzelm@52510
    88
          else tr
wenzelm@52510
    89
      | [] => Toplevel.ignored (Position.set_range (range span))
wenzelm@52510
    90
      | _ => Toplevel.malformed proper_range "Exactly one command expected")
wenzelm@52510
    91
      handle ERROR msg => Toplevel.malformed proper_range msg
wenzelm@52510
    92
  end;
wenzelm@52509
    93
wenzelm@52509
    94
wenzelm@52509
    95
(* eval *)
wenzelm@47336
    96
wenzelm@47336
    97
local
wenzelm@47336
    98
wenzelm@47336
    99
fun run int tr st =
wenzelm@51284
   100
  if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@51605
   101
    (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   102
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   103
  else Toplevel.command_errors int tr st;
wenzelm@47336
   104
wenzelm@52510
   105
fun check_cmts span tr st' =
wenzelm@52510
   106
  Toplevel.setmp_thread_position tr
wenzelm@52510
   107
    (fn () =>
wenzelm@52510
   108
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   109
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52510
   110
          handle exn => ML_Compiler.exn_messages_ids exn)) ();
wenzelm@52510
   111
wenzelm@47336
   112
fun proof_status tr st =
wenzelm@47336
   113
  (case try Toplevel.proof_of st of
wenzelm@47336
   114
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
   115
  | NONE => ());
wenzelm@47336
   116
wenzelm@47336
   117
in
wenzelm@47336
   118
wenzelm@52509
   119
fun eval span tr (st, {malformed}) =
wenzelm@52509
   120
  if malformed then
wenzelm@52509
   121
    ({failed = true}, (Toplevel.toplevel, {malformed = malformed}))
wenzelm@48772
   122
  else
wenzelm@48772
   123
    let
wenzelm@48772
   124
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   125
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   126
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   127
wenzelm@48772
   128
      val _ = Multithreading.interrupted ();
wenzelm@50201
   129
      val _ = Toplevel.status tr Markup.running;
wenzelm@48918
   130
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   131
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   132
      val errs = errs1 @ errs2;
wenzelm@50201
   133
      val _ = Toplevel.status tr Markup.finished;
wenzelm@50914
   134
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   135
    in
wenzelm@48772
   136
      (case result of
wenzelm@48772
   137
        NONE =>
wenzelm@48772
   138
          let
wenzelm@48772
   139
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@50201
   140
            val _ = Toplevel.status tr Markup.failed;
wenzelm@52509
   141
          in ({failed = true}, (st, {malformed = malformed'})) end
wenzelm@48772
   142
      | SOME st' =>
wenzelm@48772
   143
          let
wenzelm@48772
   144
            val _ = proof_status tr st';
wenzelm@52509
   145
          in ({failed = false}, (st', {malformed = malformed'})) end)
wenzelm@48772
   146
    end;
wenzelm@47336
   147
wenzelm@47336
   148
end;
wenzelm@47336
   149
wenzelm@52509
   150
wenzelm@52509
   151
(* print *)
wenzelm@52509
   152
wenzelm@52509
   153
val no_print = Lazy.value ();
wenzelm@52509
   154
wenzelm@52509
   155
fun print tr st' =
wenzelm@52509
   156
  let
wenzelm@52509
   157
    val is_init = Toplevel.is_init tr;
wenzelm@52509
   158
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@52509
   159
    val do_print =
wenzelm@52509
   160
      not is_init andalso
wenzelm@52509
   161
        (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52509
   162
  in
wenzelm@52509
   163
    if do_print then
wenzelm@52509
   164
      (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@52509
   165
        (fn () => Toplevel.print_state false st')
wenzelm@52509
   166
    else no_print
wenzelm@52509
   167
  end;
wenzelm@52509
   168
wenzelm@47336
   169
end;
wenzelm@47336
   170