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