src/Pure/PIDE/command.ML
author wenzelm
Sun Aug 25 16:03:12 2013 +0200 (2013-08-25 ago)
changeset 53189 ee8b8dafef0e
parent 52999 1f09c98a3232
child 53192 04df1d236e1c
permissions -rw-r--r--
discontinued parallel_subproofs_saturation and related internal counters (superseded by parallel_subproofs_threshold and timing information);
simplified Goal.future_enabled;
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@52607
    11
  val eval_eq: eval * eval -> bool
wenzelm@52784
    12
  val eval_running: eval -> bool
wenzelm@52772
    13
  val eval_finished: eval -> bool
wenzelm@52536
    14
  val eval_result_state: eval -> Toplevel.state
wenzelm@52535
    15
  val eval: (unit -> theory) -> Token.T list -> eval -> eval
wenzelm@52600
    16
  type print
wenzelm@52850
    17
  val print: bool -> (string * string list) list -> string ->
wenzelm@52850
    18
    eval -> print list -> print list option
wenzelm@52526
    19
  type print_fn = Toplevel.transition -> Toplevel.state -> unit
wenzelm@52953
    20
  type print_function =
wenzelm@52953
    21
    {command_name: string, args: string list} ->
wenzelm@52953
    22
      {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option
wenzelm@52953
    23
  val print_function: string -> print_function -> unit
wenzelm@52571
    24
  val no_print_function: string -> unit
wenzelm@52600
    25
  type exec = eval * print list
wenzelm@52600
    26
  val no_exec: exec
wenzelm@52600
    27
  val exec_ids: exec option -> Document_ID.exec list
wenzelm@52606
    28
  val exec: Document_ID.execution -> exec -> unit
wenzelm@47336
    29
end;
wenzelm@47336
    30
wenzelm@47336
    31
structure Command: COMMAND =
wenzelm@47336
    32
struct
wenzelm@47336
    33
wenzelm@52775
    34
(** memo results **)
wenzelm@47341
    35
wenzelm@47341
    36
datatype 'a expr =
wenzelm@52596
    37
  Expr of Document_ID.exec * (unit -> 'a) |
wenzelm@47341
    38
  Result of 'a Exn.result;
wenzelm@47341
    39
wenzelm@47341
    40
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    41
with
wenzelm@47341
    42
wenzelm@52596
    43
fun memo exec_id e = Memo (Synchronized.var "Command.memo" (Expr (exec_id, e)));
wenzelm@47341
    44
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    45
wenzelm@47342
    46
fun memo_result (Memo v) =
wenzelm@47342
    47
  (case Synchronized.value v of
wenzelm@52607
    48
    Expr (exec_id, _) => error ("Unfinished execution result: " ^ Document_ID.print exec_id)
wenzelm@52607
    49
  | Result res => Exn.release res);
wenzelm@47341
    50
wenzelm@52656
    51
fun memo_finished (Memo v) =
wenzelm@52785
    52
  (case Synchronized.value v of Expr _ => false | Result _ => true);
wenzelm@52656
    53
wenzelm@52607
    54
fun memo_exec execution_id (Memo v) =
wenzelm@52774
    55
  Synchronized.timed_access v (K (SOME Time.zeroTime))
wenzelm@52609
    56
    (fn expr =>
wenzelm@52609
    57
      (case expr of
wenzelm@52775
    58
        Expr (exec_id, body) =>
wenzelm@52609
    59
          uninterruptible (fn restore_attributes => fn () =>
wenzelm@52609
    60
            if Execution.running execution_id exec_id then
wenzelm@52609
    61
              let
wenzelm@52775
    62
                val res =
wenzelm@52775
    63
                  (body
wenzelm@52775
    64
                    |> restore_attributes
wenzelm@52775
    65
                    |> Future.worker_nest "Command.memo_exec"
wenzelm@52775
    66
                    |> Exn.interruptible_capture) ();
wenzelm@52775
    67
              in SOME ((), Result res) end
wenzelm@52775
    68
            else SOME ((), expr)) ()
wenzelm@52775
    69
      | Result _ => SOME ((), expr)))
wenzelm@52775
    70
  |> (fn NONE => error "Conflicting command execution" | _ => ());
wenzelm@52596
    71
wenzelm@52607
    72
fun memo_fork params execution_id (Memo v) =
wenzelm@52596
    73
  (case Synchronized.value v of
wenzelm@52596
    74
    Result _ => ()
wenzelm@52607
    75
  | _ => ignore ((singleton o Future.forks) params (fn () => memo_exec execution_id (Memo v))));
wenzelm@52526
    76
wenzelm@47341
    77
end;
wenzelm@47341
    78
wenzelm@47341
    79
wenzelm@52607
    80
wenzelm@52536
    81
(** main phases of execution **)
wenzelm@52536
    82
wenzelm@52510
    83
(* read *)
wenzelm@52509
    84
wenzelm@52534
    85
fun read init span =
wenzelm@52510
    86
  let
wenzelm@52510
    87
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
    88
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
    89
wenzelm@52534
    90
    val proper_range =
wenzelm@52534
    91
      Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
wenzelm@52510
    92
    val pos =
wenzelm@52510
    93
      (case find_first Token.is_command span of
wenzelm@52510
    94
        SOME tok => Token.position_of tok
wenzelm@52510
    95
      | NONE => proper_range);
wenzelm@52509
    96
wenzelm@52510
    97
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
    98
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
    99
  in
wenzelm@52510
   100
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   101
    else
wenzelm@52510
   102
      (case Outer_Syntax.read_spans outer_syntax span of
wenzelm@52510
   103
        [tr] =>
wenzelm@52510
   104
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
   105
            Toplevel.malformed pos "Illegal control command"
wenzelm@52534
   106
          else Toplevel.modify_init init tr
wenzelm@52534
   107
      | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
wenzelm@52510
   108
      | _ => Toplevel.malformed proper_range "Exactly one command expected")
wenzelm@52510
   109
      handle ERROR msg => Toplevel.malformed proper_range msg
wenzelm@52510
   110
  end;
wenzelm@52509
   111
wenzelm@52509
   112
wenzelm@52509
   113
(* eval *)
wenzelm@47336
   114
wenzelm@52600
   115
type eval_state =
wenzelm@52600
   116
  {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@52600
   117
val init_eval_state =
wenzelm@52600
   118
  {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52600
   119
wenzelm@52600
   120
datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
wenzelm@52600
   121
wenzelm@52607
   122
fun eval_eq (Eval {exec_id, ...}, Eval {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   123
wenzelm@52784
   124
fun eval_running (Eval {exec_id, ...}) = Execution.is_running_exec exec_id;
wenzelm@52772
   125
fun eval_finished (Eval {eval_process, ...}) = memo_finished eval_process;
wenzelm@52772
   126
wenzelm@52600
   127
fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
wenzelm@52600
   128
val eval_result_state = #state o eval_result;
wenzelm@52600
   129
wenzelm@47336
   130
local
wenzelm@47336
   131
wenzelm@47336
   132
fun run int tr st =
wenzelm@53189
   133
  if Goal.future_enabled 1 andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@51605
   134
    (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   135
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   136
  else Toplevel.command_errors int tr st;
wenzelm@47336
   137
wenzelm@52510
   138
fun check_cmts span tr st' =
wenzelm@52510
   139
  Toplevel.setmp_thread_position tr
wenzelm@52510
   140
    (fn () =>
wenzelm@52510
   141
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   142
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52619
   143
          handle exn =>
wenzelm@52619
   144
            if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   145
            else ML_Compiler.exn_messages_ids exn)) ();
wenzelm@52510
   146
wenzelm@47336
   147
fun proof_status tr st =
wenzelm@47336
   148
  (case try Toplevel.proof_of st of
wenzelm@47336
   149
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
   150
  | NONE => ());
wenzelm@47336
   151
wenzelm@52534
   152
fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   153
  if malformed then
wenzelm@52526
   154
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   155
  else
wenzelm@48772
   156
    let
wenzelm@48772
   157
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   158
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   159
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   160
wenzelm@48772
   161
      val _ = Multithreading.interrupted ();
wenzelm@50201
   162
      val _ = Toplevel.status tr Markup.running;
wenzelm@48918
   163
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   164
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   165
      val errs = errs1 @ errs2;
wenzelm@50201
   166
      val _ = Toplevel.status tr Markup.finished;
wenzelm@50914
   167
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   168
    in
wenzelm@48772
   169
      (case result of
wenzelm@48772
   170
        NONE =>
wenzelm@48772
   171
          let
wenzelm@48772
   172
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@50201
   173
            val _ = Toplevel.status tr Markup.failed;
wenzelm@52526
   174
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   175
      | SOME st' =>
wenzelm@48772
   176
          let
wenzelm@48772
   177
            val _ = proof_status tr st';
wenzelm@52526
   178
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   179
    end;
wenzelm@47336
   180
wenzelm@52534
   181
in
wenzelm@52534
   182
wenzelm@52534
   183
fun eval init span eval0 =
wenzelm@52534
   184
  let
wenzelm@52534
   185
    val exec_id = Document_ID.make ();
wenzelm@52534
   186
    fun process () =
wenzelm@52534
   187
      let
wenzelm@52534
   188
        val tr =
wenzelm@52534
   189
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@52536
   190
            (fn () => read init span |> Toplevel.exec_id exec_id) ();
wenzelm@52534
   191
      in eval_state span tr (eval_result eval0) end;
wenzelm@52600
   192
  in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
wenzelm@52534
   193
wenzelm@47336
   194
end;
wenzelm@47336
   195
wenzelm@52509
   196
wenzelm@52509
   197
(* print *)
wenzelm@52509
   198
wenzelm@52600
   199
datatype print = Print of
wenzelm@52850
   200
 {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
wenzelm@52600
   201
  exec_id: Document_ID.exec, print_process: unit memo};
wenzelm@52600
   202
wenzelm@52526
   203
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   204
wenzelm@52647
   205
type print_function =
wenzelm@52850
   206
  {command_name: string, args: string list} ->
wenzelm@52953
   207
    {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
wenzelm@52647
   208
wenzelm@52511
   209
local
wenzelm@52511
   210
wenzelm@52647
   211
val print_functions =
wenzelm@52647
   212
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   213
wenzelm@52570
   214
fun print_error tr e =
wenzelm@52619
   215
  (Toplevel.setmp_thread_position tr o Runtime.controlled_execution) e ()
wenzelm@52619
   216
    handle exn =>
wenzelm@52619
   217
      if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   218
      else List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
wenzelm@52516
   219
wenzelm@52607
   220
fun print_eq (Print {exec_id, ...}, Print {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   221
wenzelm@52775
   222
fun print_finished (Print {print_process, ...}) = memo_finished print_process;
wenzelm@52656
   223
wenzelm@52600
   224
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   225
wenzelm@52853
   226
val overlay_ord = prod_ord string_ord (list_ord string_ord);
wenzelm@52853
   227
wenzelm@52511
   228
in
wenzelm@52509
   229
wenzelm@52850
   230
fun print command_visible command_overlays command_name eval old_prints =
wenzelm@52570
   231
  let
wenzelm@52850
   232
    val print_functions = Synchronized.value print_functions;
wenzelm@52850
   233
wenzelm@52999
   234
    fun make_print name args {delay, pri, persistent, strict, print_fn} =
wenzelm@52999
   235
      let
wenzelm@52999
   236
        val exec_id = Document_ID.make ();
wenzelm@52999
   237
        fun process () =
wenzelm@52999
   238
          let
wenzelm@52999
   239
            val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52999
   240
            val tr = Toplevel.exec_id exec_id command;
wenzelm@52999
   241
          in
wenzelm@52999
   242
            if failed andalso strict then ()
wenzelm@52999
   243
            else print_error tr (fn () => print_fn tr st')
wenzelm@52999
   244
          end;
wenzelm@52999
   245
      in
wenzelm@52999
   246
        Print {
wenzelm@52999
   247
          name = name, args = args, delay = delay, pri = pri, persistent = persistent,
wenzelm@52999
   248
          exec_id = exec_id, print_process = memo exec_id process}
wenzelm@52999
   249
      end;
wenzelm@52999
   250
wenzelm@52999
   251
    fun bad_print name args exn =
wenzelm@52999
   252
      make_print name args {delay = NONE, pri = 0, persistent = false,
wenzelm@52999
   253
        strict = false, print_fn = fn _ => fn _ => reraise exn};
wenzelm@52999
   254
wenzelm@52850
   255
    fun new_print name args get_pr =
wenzelm@52570
   256
      let
wenzelm@52850
   257
        val params = {command_name = command_name, args = args};
wenzelm@52570
   258
      in
wenzelm@52850
   259
        (case Exn.capture (Runtime.controlled_execution get_pr) params of
wenzelm@52570
   260
          Exn.Res NONE => NONE
wenzelm@52999
   261
        | Exn.Res (SOME pr) => SOME (make_print name args pr)
wenzelm@52999
   262
        | Exn.Exn exn => SOME (bad_print name args exn))
wenzelm@52570
   263
      end;
wenzelm@52570
   264
wenzelm@52850
   265
    fun get_print (a, b) =
wenzelm@52850
   266
      (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of
wenzelm@52850
   267
        NONE =>
wenzelm@52850
   268
          (case AList.lookup (op =) print_functions a of
wenzelm@52999
   269
            NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a)))
wenzelm@52850
   270
          | SOME get_pr => new_print a b get_pr)
wenzelm@52850
   271
      | some => some);
wenzelm@52850
   272
wenzelm@52570
   273
    val new_prints =
wenzelm@52570
   274
      if command_visible then
wenzelm@52853
   275
        fold (fn (a, _) => cons (a, [])) print_functions command_overlays
wenzelm@52853
   276
        |> sort_distinct overlay_ord
wenzelm@52850
   277
        |> map_filter get_print
wenzelm@52656
   278
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   279
  in
wenzelm@52600
   280
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   281
  end;
wenzelm@52511
   282
wenzelm@52647
   283
fun print_function name f =
wenzelm@52511
   284
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   285
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   286
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   287
    AList.update (op =) (name, f) funs));
wenzelm@52511
   288
wenzelm@52571
   289
fun no_print_function name =
wenzelm@52571
   290
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   291
wenzelm@52511
   292
end;
wenzelm@52511
   293
wenzelm@52526
   294
val _ =
wenzelm@52647
   295
  print_function "print_state"
wenzelm@52850
   296
    (fn {command_name, ...} =>
wenzelm@52953
   297
      SOME {delay = NONE, pri = 1, persistent = true, strict = true,
wenzelm@52651
   298
        print_fn = fn tr => fn st' =>
wenzelm@52651
   299
          let
wenzelm@52651
   300
            val is_init = Keyword.is_theory_begin command_name;
wenzelm@52651
   301
            val is_proof = Keyword.is_proof command_name;
wenzelm@52651
   302
            val do_print =
wenzelm@52651
   303
              not is_init andalso
wenzelm@52651
   304
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52651
   305
          in if do_print then Toplevel.print_state false st' else () end});
wenzelm@52509
   306
wenzelm@52532
   307
wenzelm@52600
   308
(* combined execution *)
wenzelm@52600
   309
wenzelm@52600
   310
type exec = eval * print list;
wenzelm@52600
   311
val no_exec: exec =
wenzelm@52600
   312
  (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
wenzelm@52532
   313
wenzelm@52600
   314
fun exec_ids NONE = []
wenzelm@52600
   315
  | exec_ids (SOME (Eval {exec_id, ...}, prints)) =
wenzelm@52600
   316
      exec_id :: map (fn Print {exec_id, ...} => exec_id) prints;
wenzelm@52600
   317
wenzelm@52600
   318
local
wenzelm@52600
   319
wenzelm@52651
   320
fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
wenzelm@52651
   321
  if Multithreading.enabled () then
wenzelm@52651
   322
    let
wenzelm@52651
   323
      val group = Future.worker_subgroup ();
wenzelm@52651
   324
      fun fork () =
wenzelm@52651
   325
        memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@52651
   326
          execution_id print_process;
wenzelm@52651
   327
    in
wenzelm@52762
   328
      (case delay of
wenzelm@52762
   329
        NONE => fork ()
wenzelm@52762
   330
      | SOME d => ignore (Event_Timer.request (Time.+ (Time.now (), d)) fork))
wenzelm@52651
   331
    end
wenzelm@52651
   332
  else memo_exec execution_id print_process;
wenzelm@52559
   333
wenzelm@52600
   334
in
wenzelm@52600
   335
wenzelm@52607
   336
fun exec execution_id (Eval {eval_process, ...}, prints) =
wenzelm@52607
   337
  (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
wenzelm@52532
   338
wenzelm@47336
   339
end;
wenzelm@47336
   340
wenzelm@52600
   341
end;
wenzelm@52600
   342