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