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