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