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