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