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