src/Pure/PIDE/command.ML
author wenzelm
Tue Feb 26 19:44:26 2013 +0100 (2013-02-26 ago)
changeset 51284 59a03019f3bf
parent 51266 3007d0bc9cb1
child 51589 8ea0a5dd5a35
permissions -rw-r--r--
fork diagnostic commands (theory loader and PIDE interaction);
explicit id for load_thy, for more robust Goal.fork accounting and commit for each theory -- NB: use_thys nodes become subject to Position.is_reported like PIDE document transactions;
clarified Toplevel.command_exception vs. Toplevel.command_errors;
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@48771
     9
  val range: Token.T list -> Position.range
wenzelm@48771
    10
  val proper_range: Token.T list -> Position.range
wenzelm@47341
    11
  type 'a memo
wenzelm@47341
    12
  val memo: (unit -> 'a) -> 'a memo
wenzelm@47341
    13
  val memo_value: 'a -> 'a memo
wenzelm@47342
    14
  val memo_eval: 'a memo -> 'a
wenzelm@47342
    15
  val memo_result: 'a memo -> 'a
wenzelm@48918
    16
  val run_command: Toplevel.transition * Token.T list ->
wenzelm@48772
    17
    Toplevel.state * bool -> (Toplevel.state * bool) * unit lazy
wenzelm@47336
    18
end;
wenzelm@47336
    19
wenzelm@47336
    20
structure Command: COMMAND =
wenzelm@47336
    21
struct
wenzelm@47336
    22
wenzelm@48771
    23
(* span range *)
wenzelm@48771
    24
wenzelm@49866
    25
val range = Token.position_range_of;
wenzelm@51266
    26
val proper_range = Token.position_range_of o #1 o take_suffix Token.is_improper;
wenzelm@48771
    27
wenzelm@48771
    28
wenzelm@47341
    29
(* memo results *)
wenzelm@47341
    30
wenzelm@47341
    31
datatype 'a expr =
wenzelm@47341
    32
  Expr of 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@47341
    38
fun memo e = Memo (Synchronized.var "Command.memo" (Expr 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_eval (Memo v) =
wenzelm@47341
    42
  (case Synchronized.value v of
wenzelm@47341
    43
    Result res => res
wenzelm@47341
    44
  | _ =>
wenzelm@47341
    45
      Synchronized.guarded_access v
wenzelm@47341
    46
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    47
          | Expr e =>
wenzelm@47341
    48
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47342
    49
              in SOME (res, Result res) end))
wenzelm@47342
    50
  |> Exn.release;
wenzelm@47342
    51
wenzelm@47342
    52
fun memo_result (Memo v) =
wenzelm@47342
    53
  (case Synchronized.value v of
wenzelm@47342
    54
    Result res => Exn.release res
wenzelm@47342
    55
  | _ => raise Fail "Unfinished memo result");
wenzelm@47341
    56
wenzelm@47341
    57
end;
wenzelm@47341
    58
wenzelm@47341
    59
wenzelm@47341
    60
(* run command *)
wenzelm@47336
    61
wenzelm@47336
    62
local
wenzelm@47336
    63
wenzelm@47336
    64
fun run int tr st =
wenzelm@51284
    65
  if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@51284
    66
    Toplevel.setmp_thread_position tr (fn () =>
wenzelm@51284
    67
      (Goal.fork_name "Toplevel.diag" ~1
wenzelm@51284
    68
        (fn () => Toplevel.command_exception int tr st); ([], SOME st))) ()
wenzelm@51284
    69
  else Toplevel.command_errors int tr st;
wenzelm@47336
    70
wenzelm@48918
    71
fun check_cmts tr cmts st =
wenzelm@48918
    72
  Toplevel.setmp_thread_position tr
wenzelm@48918
    73
    (fn () => cmts
wenzelm@48918
    74
      |> maps (fn cmt =>
wenzelm@48918
    75
        (Thy_Output.check_text (Token.source_position_of cmt) st; [])
wenzelm@50914
    76
          handle exn => ML_Compiler.exn_messages_ids exn)) ();
wenzelm@48918
    77
wenzelm@47336
    78
fun timing tr t =
wenzelm@50201
    79
  if Timing.is_relevant t then Toplevel.status tr (Markup.timing t) else ();
wenzelm@47336
    80
wenzelm@47336
    81
fun proof_status tr st =
wenzelm@47336
    82
  (case try Toplevel.proof_of st of
wenzelm@47336
    83
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
    84
  | NONE => ());
wenzelm@47336
    85
wenzelm@47336
    86
val no_print = Lazy.value ();
wenzelm@47336
    87
wenzelm@47336
    88
fun print_state tr st =
wenzelm@47336
    89
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@47336
    90
    (fn () => Toplevel.print_state false st);
wenzelm@47336
    91
wenzelm@47336
    92
in
wenzelm@47336
    93
wenzelm@48918
    94
fun run_command (tr, cmts) (st, malformed) =
wenzelm@48772
    95
  if malformed then ((Toplevel.toplevel, malformed), no_print)
wenzelm@48772
    96
  else
wenzelm@48772
    97
    let
wenzelm@48772
    98
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
    99
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   100
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   101
wenzelm@48772
   102
      val _ = Multithreading.interrupted ();
wenzelm@50201
   103
      val _ = Toplevel.status tr Markup.running;
wenzelm@48772
   104
      val start = Timing.start ();
wenzelm@48918
   105
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@48918
   106
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts tr cmts st');
wenzelm@48918
   107
      val errs = errs1 @ errs2;
wenzelm@48772
   108
      val _ = timing tr (Timing.result start);
wenzelm@50201
   109
      val _ = Toplevel.status tr Markup.finished;
wenzelm@50914
   110
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   111
    in
wenzelm@48772
   112
      (case result of
wenzelm@48772
   113
        NONE =>
wenzelm@48772
   114
          let
wenzelm@48772
   115
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@50201
   116
            val _ = Toplevel.status tr Markup.failed;
wenzelm@48772
   117
          in ((st, malformed'), no_print) end
wenzelm@48772
   118
      | SOME st' =>
wenzelm@48772
   119
          let
wenzelm@48772
   120
            val _ = proof_status tr st';
wenzelm@48772
   121
            val do_print =
wenzelm@48772
   122
              not is_init andalso
wenzelm@48772
   123
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@48772
   124
          in ((st', malformed'), if do_print then print_state tr st' else no_print) end)
wenzelm@48772
   125
    end;
wenzelm@47336
   126
wenzelm@47336
   127
end;
wenzelm@47336
   128
wenzelm@47336
   129
end;
wenzelm@47336
   130