src/Pure/PIDE/command.ML
author wenzelm
Thu Aug 02 12:36:54 2012 +0200 (2012-08-02)
changeset 48646 91281e9472d8
parent 47395 e6261a493f04
child 48771 2ea997196d04
permissions -rw-r--r--
more official command specifications, including source position;
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@47341
     9
  type 'a memo
wenzelm@47341
    10
  val memo: (unit -> 'a) -> 'a memo
wenzelm@47341
    11
  val memo_value: 'a -> 'a memo
wenzelm@47342
    12
  val memo_eval: 'a memo -> 'a
wenzelm@47342
    13
  val memo_result: 'a memo -> 'a
wenzelm@47336
    14
  val run_command: Toplevel.transition -> Toplevel.state -> Toplevel.state * unit lazy
wenzelm@47336
    15
end;
wenzelm@47336
    16
wenzelm@47336
    17
structure Command: COMMAND =
wenzelm@47336
    18
struct
wenzelm@47336
    19
wenzelm@47341
    20
(* memo results *)
wenzelm@47341
    21
wenzelm@47341
    22
datatype 'a expr =
wenzelm@47341
    23
  Expr of unit -> 'a |
wenzelm@47341
    24
  Result of 'a Exn.result;
wenzelm@47341
    25
wenzelm@47341
    26
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    27
with
wenzelm@47341
    28
wenzelm@47341
    29
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
wenzelm@47341
    30
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    31
wenzelm@47342
    32
fun memo_eval (Memo v) =
wenzelm@47341
    33
  (case Synchronized.value v of
wenzelm@47341
    34
    Result res => res
wenzelm@47341
    35
  | _ =>
wenzelm@47341
    36
      Synchronized.guarded_access v
wenzelm@47341
    37
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    38
          | Expr e =>
wenzelm@47341
    39
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47342
    40
              in SOME (res, Result res) end))
wenzelm@47342
    41
  |> Exn.release;
wenzelm@47342
    42
wenzelm@47342
    43
fun memo_result (Memo v) =
wenzelm@47342
    44
  (case Synchronized.value v of
wenzelm@47342
    45
    Result res => Exn.release res
wenzelm@47342
    46
  | _ => raise Fail "Unfinished memo result");
wenzelm@47341
    47
wenzelm@47341
    48
end;
wenzelm@47341
    49
wenzelm@47341
    50
wenzelm@47341
    51
(* run command *)
wenzelm@47336
    52
wenzelm@47336
    53
local
wenzelm@47336
    54
wenzelm@47336
    55
fun run int tr st =
wenzelm@47336
    56
  (case Toplevel.transition int tr st of
wenzelm@47336
    57
    SOME (st', NONE) => ([], SOME st')
wenzelm@47336
    58
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@47336
    59
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@47336
    60
wenzelm@47336
    61
fun timing tr t =
wenzelm@47336
    62
  if Timing.is_relevant t then Toplevel.status tr (Isabelle_Markup.timing t) else ();
wenzelm@47336
    63
wenzelm@47336
    64
fun proof_status tr st =
wenzelm@47336
    65
  (case try Toplevel.proof_of st of
wenzelm@47336
    66
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
    67
  | NONE => ());
wenzelm@47336
    68
wenzelm@47336
    69
val no_print = Lazy.value ();
wenzelm@47336
    70
wenzelm@47336
    71
fun print_state tr st =
wenzelm@47336
    72
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@47336
    73
    (fn () => Toplevel.print_state false st);
wenzelm@47336
    74
wenzelm@47336
    75
in
wenzelm@47336
    76
wenzelm@47336
    77
fun run_command tr st =
wenzelm@47336
    78
  let
wenzelm@47336
    79
    val is_init = Toplevel.is_init tr;
wenzelm@47336
    80
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
    81
wenzelm@47336
    82
    val _ = Multithreading.interrupted ();
wenzelm@47336
    83
    val _ = Toplevel.status tr Isabelle_Markup.forked;
wenzelm@47336
    84
    val start = Timing.start ();
wenzelm@47336
    85
    val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@47336
    86
    val _ = timing tr (Timing.result start);
wenzelm@47336
    87
    val _ = Toplevel.status tr Isabelle_Markup.joined;
wenzelm@47336
    88
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@47336
    89
  in
wenzelm@47336
    90
    (case result of
wenzelm@47336
    91
      NONE =>
wenzelm@47336
    92
        let
wenzelm@47336
    93
          val _ = if null errs then Exn.interrupt () else ();
wenzelm@47336
    94
          val _ = Toplevel.status tr Isabelle_Markup.failed;
wenzelm@47336
    95
        in (st, no_print) end
wenzelm@47336
    96
    | SOME st' =>
wenzelm@47336
    97
        let
wenzelm@47336
    98
          val _ = Toplevel.status tr Isabelle_Markup.finished;
wenzelm@47336
    99
          val _ = proof_status tr st';
wenzelm@47336
   100
          val do_print =
wenzelm@47336
   101
            not is_init andalso
wenzelm@47336
   102
              (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@47336
   103
        in (st', if do_print then print_state tr st' else no_print) end)
wenzelm@47336
   104
  end;
wenzelm@47336
   105
wenzelm@47336
   106
end;
wenzelm@47336
   107
wenzelm@47336
   108
end;
wenzelm@47336
   109