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