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