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