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