src/Pure/PIDE/command.ML
author wenzelm
Fri Jul 05 17:09:28 2013 +0200 (2013-07-05 ago)
changeset 52533 a95440dcd59c
parent 52532 c81d76f7f63d
child 52534 341ae9cd4743
permissions -rw-r--r--
clarified type Command.eval;
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@52533
    22
  type eval = {exec_id: Document_ID.exec, eval: eval_state memo}
wenzelm@52526
    23
  val no_eval: eval
wenzelm@52533
    24
  val eval_result: eval -> eval_state
wenzelm@52526
    25
  val eval: span -> Toplevel.transition -> eval_state -> eval_state
wenzelm@52526
    26
  type print_fn = Toplevel.transition -> Toplevel.state -> unit
wenzelm@52530
    27
  type print = {name: string, pri: int, exec_id: Document_ID.exec, print: unit memo}
wenzelm@52530
    28
  val print: string -> eval -> print list
wenzelm@52526
    29
  val print_function: {name: string, pri: int} -> (string -> print_fn option) -> unit
wenzelm@52533
    30
  type exec = eval * print list
wenzelm@52532
    31
  val no_exec: exec
wenzelm@52532
    32
  val exec_ids: exec -> Document_ID.exec list
wenzelm@52532
    33
  val exec_result: exec -> eval_state
wenzelm@52532
    34
  val exec_run: exec -> unit
wenzelm@52533
    35
  val stable_eval: eval -> bool
wenzelm@52532
    36
  val stable_print: print -> bool
wenzelm@47336
    37
end;
wenzelm@47336
    38
wenzelm@47336
    39
structure Command: COMMAND =
wenzelm@47336
    40
struct
wenzelm@47336
    41
wenzelm@52509
    42
(* source *)
wenzelm@52509
    43
wenzelm@52509
    44
type span = Token.T list;
wenzelm@48771
    45
wenzelm@49866
    46
val range = Token.position_range_of;
wenzelm@51266
    47
val proper_range = Token.position_range_of o #1 o take_suffix Token.is_improper;
wenzelm@48771
    48
wenzelm@48771
    49
wenzelm@47341
    50
(* memo results *)
wenzelm@47341
    51
wenzelm@47341
    52
datatype 'a expr =
wenzelm@47341
    53
  Expr of unit -> 'a |
wenzelm@47341
    54
  Result of 'a Exn.result;
wenzelm@47341
    55
wenzelm@47341
    56
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    57
with
wenzelm@47341
    58
wenzelm@47341
    59
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
wenzelm@47341
    60
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    61
wenzelm@47342
    62
fun memo_eval (Memo v) =
wenzelm@47341
    63
  (case Synchronized.value v of
wenzelm@47341
    64
    Result res => res
wenzelm@47341
    65
  | _ =>
wenzelm@47341
    66
      Synchronized.guarded_access v
wenzelm@47341
    67
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    68
          | Expr e =>
wenzelm@47341
    69
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47342
    70
              in SOME (res, Result res) end))
wenzelm@47342
    71
  |> Exn.release;
wenzelm@47342
    72
wenzelm@52526
    73
fun memo_fork params (Memo v) =
wenzelm@52526
    74
  (case Synchronized.value v of
wenzelm@52526
    75
    Result _ => ()
wenzelm@52526
    76
  | _ => ignore ((singleton o Future.forks) params (fn () => memo_eval (Memo v))));
wenzelm@52526
    77
wenzelm@47342
    78
fun memo_result (Memo v) =
wenzelm@47342
    79
  (case Synchronized.value v of
wenzelm@47342
    80
    Result res => Exn.release res
wenzelm@47342
    81
  | _ => raise Fail "Unfinished memo result");
wenzelm@47341
    82
wenzelm@52526
    83
fun memo_stable (Memo v) =
wenzelm@52526
    84
  (case Synchronized.value v of
wenzelm@52526
    85
    Expr _ => true
wenzelm@52526
    86
  | Result res => not (Exn.is_interrupt_exn res));
wenzelm@52526
    87
wenzelm@47341
    88
end;
wenzelm@47341
    89
wenzelm@47341
    90
wenzelm@52532
    91
(** main phases: read -- eval -- print **)
wenzelm@52532
    92
wenzelm@52510
    93
(* read *)
wenzelm@52509
    94
wenzelm@52510
    95
fun read span =
wenzelm@52510
    96
  let
wenzelm@52510
    97
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
    98
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
    99
wenzelm@52510
   100
    val proper_range = Position.set_range (proper_range span);
wenzelm@52510
   101
    val pos =
wenzelm@52510
   102
      (case find_first Token.is_command span of
wenzelm@52510
   103
        SOME tok => Token.position_of tok
wenzelm@52510
   104
      | NONE => proper_range);
wenzelm@52509
   105
wenzelm@52510
   106
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
   107
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
   108
  in
wenzelm@52510
   109
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   110
    else
wenzelm@52510
   111
      (case Outer_Syntax.read_spans outer_syntax span of
wenzelm@52510
   112
        [tr] =>
wenzelm@52510
   113
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
   114
            Toplevel.malformed pos "Illegal control command"
wenzelm@52510
   115
          else tr
wenzelm@52510
   116
      | [] => Toplevel.ignored (Position.set_range (range span))
wenzelm@52510
   117
      | _ => Toplevel.malformed proper_range "Exactly one command expected")
wenzelm@52510
   118
      handle ERROR msg => Toplevel.malformed proper_range msg
wenzelm@52510
   119
  end;
wenzelm@52509
   120
wenzelm@52509
   121
wenzelm@52509
   122
(* eval *)
wenzelm@47336
   123
wenzelm@52526
   124
type eval_state =
wenzelm@52526
   125
  {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@52526
   126
val no_eval_state: eval_state =
wenzelm@52526
   127
  {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52526
   128
wenzelm@52533
   129
type eval = {exec_id: Document_ID.exec, eval: eval_state memo};
wenzelm@52533
   130
wenzelm@52533
   131
val no_eval: eval = {exec_id = Document_ID.none, eval = memo_value no_eval_state};
wenzelm@52533
   132
fun eval_result ({eval, ...}: eval) = memo_result eval;
wenzelm@52526
   133
wenzelm@47336
   134
local
wenzelm@47336
   135
wenzelm@47336
   136
fun run int tr st =
wenzelm@51284
   137
  if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@51605
   138
    (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   139
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   140
  else Toplevel.command_errors int tr st;
wenzelm@47336
   141
wenzelm@52510
   142
fun check_cmts span tr st' =
wenzelm@52510
   143
  Toplevel.setmp_thread_position tr
wenzelm@52510
   144
    (fn () =>
wenzelm@52510
   145
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   146
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52510
   147
          handle exn => 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@47336
   154
in
wenzelm@47336
   155
wenzelm@52526
   156
fun eval span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   157
  if malformed then
wenzelm@52526
   158
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   159
  else
wenzelm@48772
   160
    let
wenzelm@48772
   161
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   162
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   163
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   164
wenzelm@48772
   165
      val _ = Multithreading.interrupted ();
wenzelm@50201
   166
      val _ = Toplevel.status tr Markup.running;
wenzelm@48918
   167
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   168
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   169
      val errs = errs1 @ errs2;
wenzelm@50201
   170
      val _ = Toplevel.status tr Markup.finished;
wenzelm@50914
   171
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   172
    in
wenzelm@48772
   173
      (case result of
wenzelm@48772
   174
        NONE =>
wenzelm@48772
   175
          let
wenzelm@48772
   176
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@50201
   177
            val _ = Toplevel.status tr Markup.failed;
wenzelm@52526
   178
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   179
      | SOME st' =>
wenzelm@48772
   180
          let
wenzelm@48772
   181
            val _ = proof_status tr st';
wenzelm@52526
   182
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   183
    end;
wenzelm@47336
   184
wenzelm@47336
   185
end;
wenzelm@47336
   186
wenzelm@52509
   187
wenzelm@52509
   188
(* print *)
wenzelm@52509
   189
wenzelm@52530
   190
type print = {name: string, pri: int, exec_id: Document_ID.exec, print: unit memo};
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@52527
   214
          fun body () =
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@52527
   219
        in SOME {name = name, pri = pri, exec_id = exec_id, print = memo body} end
wenzelm@52526
   220
    | Exn.Exn exn =>
wenzelm@52527
   221
        let
wenzelm@52530
   222
          val exec_id = Document_ID.make ();
wenzelm@52527
   223
          fun body () =
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@52527
   228
        in SOME {name = name, pri = pri, exec_id = exec_id, print = memo body} 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@52533
   259
fun exec_result (({eval, ...}, _): exec) = memo_result eval;
wenzelm@52532
   260
wenzelm@52533
   261
fun exec_run (({eval, ...}, prints): exec) =
wenzelm@52532
   262
 (memo_eval eval;
wenzelm@52532
   263
  prints |> List.app (fn {name, pri, print, ...} =>
wenzelm@52532
   264
    memo_fork {name = name, group = NONE, deps = [], pri = pri, interrupts = true} print));
wenzelm@52532
   265
wenzelm@52532
   266
wenzelm@52533
   267
(* stable situations after cancellation *)
wenzelm@52532
   268
wenzelm@52532
   269
fun stable_goals exec_id =
wenzelm@52532
   270
  not (Par_Exn.is_interrupted (Future.join_results (Goal.peek_futures exec_id)));
wenzelm@52532
   271
wenzelm@52533
   272
fun stable_eval ({exec_id, eval}: eval) =
wenzelm@52532
   273
  stable_goals exec_id andalso memo_stable eval;
wenzelm@52532
   274
wenzelm@52532
   275
fun stable_print ({exec_id, print, ...}: print) =
wenzelm@52532
   276
  stable_goals exec_id andalso memo_stable print;
wenzelm@52532
   277
wenzelm@47336
   278
end;
wenzelm@47336
   279