src/Pure/PIDE/command.ML
author wenzelm
Sat Aug 11 18:05:41 2012 +0200 (2012-08-11 ago)
changeset 48771 2ea997196d04
parent 47395 e6261a493f04
child 48772 e46cd0d26481
permissions -rw-r--r--
clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
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@47336
    16
  val run_command: Toplevel.transition -> Toplevel.state -> Toplevel.state * unit lazy
wenzelm@47336
    17
end;
wenzelm@47336
    18
wenzelm@47336
    19
structure Command: COMMAND =
wenzelm@47336
    20
struct
wenzelm@47336
    21
wenzelm@48771
    22
(* span range *)
wenzelm@48771
    23
wenzelm@48771
    24
fun range [] = (Position.none, Position.none)
wenzelm@48771
    25
  | range toks =
wenzelm@48771
    26
      let
wenzelm@48771
    27
        val start_pos = Token.position_of (hd toks);
wenzelm@48771
    28
        val end_pos = Token.end_position_of (List.last toks);
wenzelm@48771
    29
      in (start_pos, end_pos) end;
wenzelm@48771
    30
wenzelm@48771
    31
val proper_range = range o #1 o take_suffix Token.is_space;
wenzelm@48771
    32
wenzelm@48771
    33
wenzelm@47341
    34
(* memo results *)
wenzelm@47341
    35
wenzelm@47341
    36
datatype 'a expr =
wenzelm@47341
    37
  Expr of unit -> 'a |
wenzelm@47341
    38
  Result of 'a Exn.result;
wenzelm@47341
    39
wenzelm@47341
    40
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    41
with
wenzelm@47341
    42
wenzelm@47341
    43
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
wenzelm@47341
    44
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    45
wenzelm@47342
    46
fun memo_eval (Memo v) =
wenzelm@47341
    47
  (case Synchronized.value v of
wenzelm@47341
    48
    Result res => res
wenzelm@47341
    49
  | _ =>
wenzelm@47341
    50
      Synchronized.guarded_access v
wenzelm@47341
    51
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    52
          | Expr e =>
wenzelm@47341
    53
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47342
    54
              in SOME (res, Result res) end))
wenzelm@47342
    55
  |> Exn.release;
wenzelm@47342
    56
wenzelm@47342
    57
fun memo_result (Memo v) =
wenzelm@47342
    58
  (case Synchronized.value v of
wenzelm@47342
    59
    Result res => Exn.release res
wenzelm@47342
    60
  | _ => raise Fail "Unfinished memo result");
wenzelm@47341
    61
wenzelm@47341
    62
end;
wenzelm@47341
    63
wenzelm@47341
    64
wenzelm@47341
    65
(* run command *)
wenzelm@47336
    66
wenzelm@47336
    67
local
wenzelm@47336
    68
wenzelm@47336
    69
fun run int tr st =
wenzelm@47336
    70
  (case Toplevel.transition int tr st of
wenzelm@47336
    71
    SOME (st', NONE) => ([], SOME st')
wenzelm@47336
    72
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@47336
    73
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@47336
    74
wenzelm@47336
    75
fun timing tr t =
wenzelm@47336
    76
  if Timing.is_relevant t then Toplevel.status tr (Isabelle_Markup.timing t) else ();
wenzelm@47336
    77
wenzelm@47336
    78
fun proof_status tr st =
wenzelm@47336
    79
  (case try Toplevel.proof_of st of
wenzelm@47336
    80
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
    81
  | NONE => ());
wenzelm@47336
    82
wenzelm@47336
    83
val no_print = Lazy.value ();
wenzelm@47336
    84
wenzelm@47336
    85
fun print_state tr st =
wenzelm@47336
    86
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@47336
    87
    (fn () => Toplevel.print_state false st);
wenzelm@47336
    88
wenzelm@47336
    89
in
wenzelm@47336
    90
wenzelm@47336
    91
fun run_command tr st =
wenzelm@47336
    92
  let
wenzelm@47336
    93
    val is_init = Toplevel.is_init tr;
wenzelm@47336
    94
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
    95
wenzelm@47336
    96
    val _ = Multithreading.interrupted ();
wenzelm@47336
    97
    val _ = Toplevel.status tr Isabelle_Markup.forked;
wenzelm@47336
    98
    val start = Timing.start ();
wenzelm@47336
    99
    val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@47336
   100
    val _ = timing tr (Timing.result start);
wenzelm@47336
   101
    val _ = Toplevel.status tr Isabelle_Markup.joined;
wenzelm@47336
   102
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@47336
   103
  in
wenzelm@47336
   104
    (case result of
wenzelm@47336
   105
      NONE =>
wenzelm@47336
   106
        let
wenzelm@47336
   107
          val _ = if null errs then Exn.interrupt () else ();
wenzelm@47336
   108
          val _ = Toplevel.status tr Isabelle_Markup.failed;
wenzelm@47336
   109
        in (st, no_print) end
wenzelm@47336
   110
    | SOME st' =>
wenzelm@47336
   111
        let
wenzelm@47336
   112
          val _ = Toplevel.status tr Isabelle_Markup.finished;
wenzelm@47336
   113
          val _ = proof_status tr st';
wenzelm@47336
   114
          val do_print =
wenzelm@47336
   115
            not is_init andalso
wenzelm@47336
   116
              (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@47336
   117
        in (st', if do_print then print_state tr st' else no_print) end)
wenzelm@47336
   118
  end;
wenzelm@47336
   119
wenzelm@47336
   120
end;
wenzelm@47336
   121
wenzelm@47336
   122
end;
wenzelm@47336
   123