src/Pure/PIDE/command.ML
author wenzelm
Sat Jul 20 16:16:23 2013 +0200 (2013-07-20 ago)
changeset 52713 cd3ce844248f
parent 52657 42c14dba1daa
child 52761 909167fdd367
permissions -rw-r--r--
print_state at high priority -- fast and important;
wenzelm@47336
     1
(*  Title:      Pure/PIDE/command.ML
wenzelm@47336
     2
    Author:     Makarius
wenzelm@47336
     3
wenzelm@52534
     4
Prover command execution: read -- eval -- print.
wenzelm@47336
     5
*)
wenzelm@47336
     6
wenzelm@47336
     7
signature COMMAND =
wenzelm@47336
     8
sig
wenzelm@52600
     9
  val read: (unit -> theory) -> Token.T list -> Toplevel.transition
wenzelm@52600
    10
  type eval
wenzelm@52607
    11
  val eval_eq: eval * eval -> bool
wenzelm@52536
    12
  val eval_result_state: eval -> Toplevel.state
wenzelm@52607
    13
  val eval_stable: eval -> bool
wenzelm@52535
    14
  val eval: (unit -> theory) -> Token.T list -> eval -> eval
wenzelm@52600
    15
  type print
wenzelm@52570
    16
  val print: bool -> string -> eval -> print list -> print list option
wenzelm@52526
    17
  type print_fn = Toplevel.transition -> Toplevel.state -> unit
wenzelm@52647
    18
  val print_function: string ->
wenzelm@52651
    19
    ({command_name: string} ->
wenzelm@52651
    20
      {delay: Time.time, pri: int, persistent: bool, print_fn: print_fn} option) -> unit
wenzelm@52571
    21
  val no_print_function: string -> unit
wenzelm@52600
    22
  type exec = eval * print list
wenzelm@52600
    23
  val no_exec: exec
wenzelm@52600
    24
  val exec_ids: exec option -> Document_ID.exec list
wenzelm@52606
    25
  val exec: Document_ID.execution -> exec -> unit
wenzelm@47336
    26
end;
wenzelm@47336
    27
wenzelm@47336
    28
structure Command: COMMAND =
wenzelm@47336
    29
struct
wenzelm@47336
    30
wenzelm@52534
    31
(** memo results -- including physical interrupts! **)
wenzelm@47341
    32
wenzelm@47341
    33
datatype 'a expr =
wenzelm@52596
    34
  Expr of Document_ID.exec * (unit -> 'a) |
wenzelm@47341
    35
  Result of 'a Exn.result;
wenzelm@47341
    36
wenzelm@47341
    37
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    38
with
wenzelm@47341
    39
wenzelm@52596
    40
fun memo exec_id e = Memo (Synchronized.var "Command.memo" (Expr (exec_id, e)));
wenzelm@47341
    41
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    42
wenzelm@47342
    43
fun memo_result (Memo v) =
wenzelm@47342
    44
  (case Synchronized.value v of
wenzelm@52607
    45
    Expr (exec_id, _) => error ("Unfinished execution result: " ^ Document_ID.print exec_id)
wenzelm@52607
    46
  | Result res => Exn.release res);
wenzelm@47341
    47
wenzelm@52607
    48
fun memo_stable (Memo v) =
wenzelm@52607
    49
  (case Synchronized.value v of
wenzelm@52607
    50
   Expr _ => true
wenzelm@52607
    51
 | Result res => not (Exn.is_interrupt_exn res));
wenzelm@52607
    52
wenzelm@52656
    53
fun memo_finished (Memo v) =
wenzelm@52656
    54
  (case Synchronized.value v of
wenzelm@52656
    55
   Expr _ => false
wenzelm@52656
    56
 | Result res => not (Exn.is_interrupt_exn res));
wenzelm@52656
    57
wenzelm@52607
    58
fun memo_exec execution_id (Memo v) =
wenzelm@52609
    59
  Synchronized.guarded_access v
wenzelm@52609
    60
    (fn expr =>
wenzelm@52609
    61
      (case expr of
wenzelm@52657
    62
        Expr (exec_id, e) =>
wenzelm@52609
    63
          uninterruptible (fn restore_attributes => fn () =>
wenzelm@52609
    64
            if Execution.running execution_id exec_id then
wenzelm@52609
    65
              let
wenzelm@52609
    66
                val res = Exn.capture (restore_attributes e) ();
wenzelm@52609
    67
                val _ = Execution.finished exec_id;
wenzelm@52657
    68
              in SOME (Exn.is_interrupt_exn res, Result res) end
wenzelm@52657
    69
            else SOME (true, expr)) ()
wenzelm@52657
    70
      | Result _ => SOME (false, expr)))
wenzelm@52657
    71
  |> (fn true => Exn.interrupt () | false => ());
wenzelm@52596
    72
wenzelm@52607
    73
fun memo_fork params execution_id (Memo v) =
wenzelm@52596
    74
  (case Synchronized.value v of
wenzelm@52596
    75
    Result _ => ()
wenzelm@52607
    76
  | _ => ignore ((singleton o Future.forks) params (fn () => memo_exec execution_id (Memo v))));
wenzelm@52526
    77
wenzelm@47341
    78
end;
wenzelm@47341
    79
wenzelm@47341
    80
wenzelm@52607
    81
wenzelm@52536
    82
(** main phases of execution **)
wenzelm@52536
    83
wenzelm@52510
    84
(* read *)
wenzelm@52509
    85
wenzelm@52534
    86
fun read init span =
wenzelm@52510
    87
  let
wenzelm@52510
    88
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
    89
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
    90
wenzelm@52534
    91
    val proper_range =
wenzelm@52534
    92
      Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
wenzelm@52510
    93
    val pos =
wenzelm@52510
    94
      (case find_first Token.is_command span of
wenzelm@52510
    95
        SOME tok => Token.position_of tok
wenzelm@52510
    96
      | NONE => proper_range);
wenzelm@52509
    97
wenzelm@52510
    98
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
    99
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
   100
  in
wenzelm@52510
   101
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   102
    else
wenzelm@52510
   103
      (case Outer_Syntax.read_spans outer_syntax span of
wenzelm@52510
   104
        [tr] =>
wenzelm@52510
   105
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
   106
            Toplevel.malformed pos "Illegal control command"
wenzelm@52534
   107
          else Toplevel.modify_init init tr
wenzelm@52534
   108
      | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
wenzelm@52510
   109
      | _ => Toplevel.malformed proper_range "Exactly one command expected")
wenzelm@52510
   110
      handle ERROR msg => Toplevel.malformed proper_range msg
wenzelm@52510
   111
  end;
wenzelm@52509
   112
wenzelm@52509
   113
wenzelm@52509
   114
(* eval *)
wenzelm@47336
   115
wenzelm@52600
   116
type eval_state =
wenzelm@52600
   117
  {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@52600
   118
val init_eval_state =
wenzelm@52600
   119
  {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52600
   120
wenzelm@52600
   121
datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
wenzelm@52600
   122
wenzelm@52607
   123
fun eval_eq (Eval {exec_id, ...}, Eval {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   124
wenzelm@52600
   125
fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
wenzelm@52600
   126
val eval_result_state = #state o eval_result;
wenzelm@52600
   127
wenzelm@52607
   128
fun eval_stable (Eval {exec_id, eval_process}) =
wenzelm@52607
   129
  Goal.stable_futures exec_id andalso memo_stable eval_process;
wenzelm@52600
   130
wenzelm@47336
   131
local
wenzelm@47336
   132
wenzelm@47336
   133
fun run int tr st =
wenzelm@51284
   134
  if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@51605
   135
    (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   136
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   137
  else Toplevel.command_errors int tr st;
wenzelm@47336
   138
wenzelm@52510
   139
fun check_cmts span tr st' =
wenzelm@52510
   140
  Toplevel.setmp_thread_position tr
wenzelm@52510
   141
    (fn () =>
wenzelm@52510
   142
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   143
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52619
   144
          handle exn =>
wenzelm@52619
   145
            if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   146
            else ML_Compiler.exn_messages_ids exn)) ();
wenzelm@52510
   147
wenzelm@47336
   148
fun proof_status tr st =
wenzelm@47336
   149
  (case try Toplevel.proof_of st of
wenzelm@47336
   150
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
   151
  | NONE => ());
wenzelm@47336
   152
wenzelm@52534
   153
fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   154
  if malformed then
wenzelm@52526
   155
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   156
  else
wenzelm@48772
   157
    let
wenzelm@48772
   158
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   159
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   160
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   161
wenzelm@48772
   162
      val _ = Multithreading.interrupted ();
wenzelm@50201
   163
      val _ = Toplevel.status tr Markup.running;
wenzelm@48918
   164
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   165
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   166
      val errs = errs1 @ errs2;
wenzelm@50201
   167
      val _ = Toplevel.status tr Markup.finished;
wenzelm@50914
   168
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   169
    in
wenzelm@48772
   170
      (case result of
wenzelm@48772
   171
        NONE =>
wenzelm@48772
   172
          let
wenzelm@48772
   173
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@50201
   174
            val _ = Toplevel.status tr Markup.failed;
wenzelm@52526
   175
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   176
      | SOME st' =>
wenzelm@48772
   177
          let
wenzelm@48772
   178
            val _ = proof_status tr st';
wenzelm@52526
   179
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   180
    end;
wenzelm@47336
   181
wenzelm@52534
   182
in
wenzelm@52534
   183
wenzelm@52534
   184
fun eval init span eval0 =
wenzelm@52534
   185
  let
wenzelm@52534
   186
    val exec_id = Document_ID.make ();
wenzelm@52534
   187
    fun process () =
wenzelm@52534
   188
      let
wenzelm@52534
   189
        val tr =
wenzelm@52534
   190
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@52536
   191
            (fn () => read init span |> Toplevel.exec_id exec_id) ();
wenzelm@52534
   192
      in eval_state span tr (eval_result eval0) end;
wenzelm@52600
   193
  in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
wenzelm@52534
   194
wenzelm@47336
   195
end;
wenzelm@47336
   196
wenzelm@52509
   197
wenzelm@52509
   198
(* print *)
wenzelm@52509
   199
wenzelm@52600
   200
datatype print = Print of
wenzelm@52651
   201
 {name: string, delay: Time.time, pri: int, persistent: bool,
wenzelm@52600
   202
  exec_id: Document_ID.exec, print_process: unit memo};
wenzelm@52600
   203
wenzelm@52526
   204
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   205
wenzelm@52647
   206
type print_function =
wenzelm@52651
   207
  {command_name: string} ->
wenzelm@52651
   208
    {delay: Time.time, pri: int, persistent: bool, print_fn: print_fn} option;
wenzelm@52647
   209
wenzelm@52511
   210
local
wenzelm@52511
   211
wenzelm@52647
   212
val print_functions =
wenzelm@52647
   213
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   214
wenzelm@52570
   215
fun print_error tr e =
wenzelm@52619
   216
  (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) e ()
wenzelm@52619
   217
    handle exn =>
wenzelm@52619
   218
      if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   219
      else List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
wenzelm@52516
   220
wenzelm@52607
   221
fun print_eq (Print {exec_id, ...}, Print {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   222
wenzelm@52607
   223
fun print_stable (Print {exec_id, print_process, ...}) =
wenzelm@52607
   224
  Goal.stable_futures exec_id andalso memo_stable print_process;
wenzelm@52607
   225
wenzelm@52656
   226
fun print_finished (Print {exec_id, print_process, ...}) =
wenzelm@52656
   227
  Goal.stable_futures exec_id andalso memo_finished print_process;
wenzelm@52656
   228
wenzelm@52600
   229
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   230
wenzelm@52511
   231
in
wenzelm@52509
   232
wenzelm@52570
   233
fun print command_visible command_name eval old_prints =
wenzelm@52570
   234
  let
wenzelm@52647
   235
    fun new_print (name, get_pr) =
wenzelm@52570
   236
      let
wenzelm@52651
   237
        fun make_print strict {delay, pri, persistent, print_fn} =
wenzelm@52570
   238
          let
wenzelm@52570
   239
            val exec_id = Document_ID.make ();
wenzelm@52570
   240
            fun process () =
wenzelm@52570
   241
              let
wenzelm@52570
   242
                val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52570
   243
                val tr = Toplevel.exec_id exec_id command;
wenzelm@52570
   244
              in
wenzelm@52570
   245
                if failed andalso not strict then ()
wenzelm@52570
   246
                else print_error tr (fn () => print_fn tr st')
wenzelm@52570
   247
              end;
wenzelm@52572
   248
          in
wenzelm@52600
   249
           Print {
wenzelm@52651
   250
             name = name, delay = delay, pri = pri, persistent = persistent,
wenzelm@52600
   251
             exec_id = exec_id, print_process = memo exec_id process}
wenzelm@52572
   252
          end;
wenzelm@52570
   253
      in
wenzelm@52647
   254
        (case Exn.capture (Runtime.controlled_execution get_pr) {command_name = command_name} of
wenzelm@52570
   255
          Exn.Res NONE => NONE
wenzelm@52647
   256
        | Exn.Res (SOME pr) => SOME (make_print false pr)
wenzelm@52647
   257
        | Exn.Exn exn =>
wenzelm@52647
   258
            SOME (make_print true
wenzelm@52651
   259
              {delay = Time.zeroTime, pri = 0, persistent = false,
wenzelm@52651
   260
                print_fn = fn _ => fn _ => reraise exn}))
wenzelm@52570
   261
      end;
wenzelm@52570
   262
wenzelm@52570
   263
    val new_prints =
wenzelm@52570
   264
      if command_visible then
wenzelm@52570
   265
        rev (Synchronized.value print_functions) |> map_filter (fn pr =>
wenzelm@52600
   266
          (case find_first (fn Print {name, ...} => name = fst pr) old_prints of
wenzelm@52596
   267
            SOME print => if print_stable print then SOME print else new_print pr
wenzelm@52570
   268
          | NONE => new_print pr))
wenzelm@52656
   269
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   270
  in
wenzelm@52600
   271
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   272
  end;
wenzelm@52511
   273
wenzelm@52647
   274
fun print_function name f =
wenzelm@52511
   275
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   276
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   277
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   278
    AList.update (op =) (name, f) funs));
wenzelm@52511
   279
wenzelm@52571
   280
fun no_print_function name =
wenzelm@52571
   281
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   282
wenzelm@52511
   283
end;
wenzelm@52511
   284
wenzelm@52526
   285
val _ =
wenzelm@52647
   286
  print_function "print_state"
wenzelm@52651
   287
    (fn {command_name} =>
wenzelm@52713
   288
      SOME {delay = Time.zeroTime, pri = 1, persistent = true,
wenzelm@52651
   289
        print_fn = fn tr => fn st' =>
wenzelm@52651
   290
          let
wenzelm@52651
   291
            val is_init = Keyword.is_theory_begin command_name;
wenzelm@52651
   292
            val is_proof = Keyword.is_proof command_name;
wenzelm@52651
   293
            val do_print =
wenzelm@52651
   294
              not is_init andalso
wenzelm@52651
   295
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52651
   296
          in if do_print then Toplevel.print_state false st' else () end});
wenzelm@52509
   297
wenzelm@52532
   298
wenzelm@52600
   299
(* combined execution *)
wenzelm@52600
   300
wenzelm@52600
   301
type exec = eval * print list;
wenzelm@52600
   302
val no_exec: exec =
wenzelm@52600
   303
  (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
wenzelm@52532
   304
wenzelm@52600
   305
fun exec_ids NONE = []
wenzelm@52600
   306
  | exec_ids (SOME (Eval {exec_id, ...}, prints)) =
wenzelm@52600
   307
      exec_id :: map (fn Print {exec_id, ...} => exec_id) prints;
wenzelm@52600
   308
wenzelm@52600
   309
local
wenzelm@52600
   310
wenzelm@52651
   311
fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
wenzelm@52651
   312
  if Multithreading.enabled () then
wenzelm@52651
   313
    let
wenzelm@52651
   314
      val group = Future.worker_subgroup ();
wenzelm@52651
   315
      fun fork () =
wenzelm@52651
   316
        memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@52651
   317
          execution_id print_process;
wenzelm@52651
   318
    in
wenzelm@52651
   319
      if delay = Time.zeroTime then fork ()
wenzelm@52651
   320
      else ignore (Event_Timer.request (Time.+ (Time.now (), delay)) fork)
wenzelm@52651
   321
    end
wenzelm@52651
   322
  else memo_exec execution_id print_process;
wenzelm@52559
   323
wenzelm@52600
   324
in
wenzelm@52600
   325
wenzelm@52607
   326
fun exec execution_id (Eval {eval_process, ...}, prints) =
wenzelm@52607
   327
  (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
wenzelm@52532
   328
wenzelm@47336
   329
end;
wenzelm@47336
   330
wenzelm@52600
   331
end;
wenzelm@52600
   332