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