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