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