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