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