src/Pure/PIDE/command.ML
author wenzelm
Thu Apr 05 11:58:46 2012 +0200 (2012-04-05 ago)
changeset 47341 00f6279bb67a
parent 47336 bed4b2738d8a
child 47342 7828c7b3c143
permissions -rw-r--r--
Command.memo including physical interrupts (unlike Lazy.lazy);
re-assign unstable exec, i.e. reset interrupted transaction;
node result as direct exec -- avoid potential interrupt instability of Lazy.map;
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@47336
     9
  val parse_command: string -> string -> Token.T list
wenzelm@47341
    10
  type 'a memo
wenzelm@47341
    11
  val memo: (unit -> 'a) -> 'a memo
wenzelm@47341
    12
  val memo_value: 'a -> 'a memo
wenzelm@47341
    13
  val memo_result: 'a memo -> 'a Exn.result
wenzelm@47341
    14
  val memo_stable: 'a memo -> bool
wenzelm@47336
    15
  val run_command: Toplevel.transition -> Toplevel.state -> Toplevel.state * unit lazy
wenzelm@47336
    16
end;
wenzelm@47336
    17
wenzelm@47336
    18
structure Command: COMMAND =
wenzelm@47336
    19
struct
wenzelm@47336
    20
wenzelm@47341
    21
(* parse command *)
wenzelm@47336
    22
wenzelm@47336
    23
fun parse_command id text =
wenzelm@47336
    24
  Position.setmp_thread_data (Position.id_only id)
wenzelm@47336
    25
    (fn () =>
wenzelm@47336
    26
      let
wenzelm@47336
    27
        val toks = Thy_Syntax.parse_tokens (#1 (Outer_Syntax.get_syntax ())) (Position.id id) text;
wenzelm@47336
    28
        val _ = Output.status (Markup.markup_only Isabelle_Markup.parsed);
wenzelm@47336
    29
      in toks end) ();
wenzelm@47336
    30
wenzelm@47336
    31
wenzelm@47341
    32
(* memo results *)
wenzelm@47341
    33
wenzelm@47341
    34
datatype 'a expr =
wenzelm@47341
    35
  Expr of unit -> 'a |
wenzelm@47341
    36
  Result of 'a Exn.result;
wenzelm@47341
    37
wenzelm@47341
    38
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    39
with
wenzelm@47341
    40
wenzelm@47341
    41
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
wenzelm@47341
    42
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    43
wenzelm@47341
    44
fun memo_result (Memo v) =
wenzelm@47341
    45
  (case Synchronized.value v of
wenzelm@47341
    46
    Result res => res
wenzelm@47341
    47
  | _ =>
wenzelm@47341
    48
      Synchronized.guarded_access v
wenzelm@47341
    49
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    50
          | Expr e =>
wenzelm@47341
    51
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47341
    52
              in SOME (res, Result res) end));
wenzelm@47341
    53
wenzelm@47341
    54
fun memo_stable (Memo v) =
wenzelm@47341
    55
  (case Synchronized.value v of
wenzelm@47341
    56
    Result (Exn.Exn exn) => not (Exn.is_interrupt exn)
wenzelm@47341
    57
  | _ => true);
wenzelm@47341
    58
wenzelm@47341
    59
end;
wenzelm@47341
    60
wenzelm@47341
    61
wenzelm@47341
    62
(* run command *)
wenzelm@47336
    63
wenzelm@47336
    64
local
wenzelm@47336
    65
wenzelm@47336
    66
fun run int tr st =
wenzelm@47336
    67
  (case Toplevel.transition int tr st of
wenzelm@47336
    68
    SOME (st', NONE) => ([], SOME st')
wenzelm@47336
    69
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@47336
    70
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@47336
    71
wenzelm@47336
    72
fun timing tr t =
wenzelm@47336
    73
  if Timing.is_relevant t then Toplevel.status tr (Isabelle_Markup.timing t) else ();
wenzelm@47336
    74
wenzelm@47336
    75
fun proof_status tr st =
wenzelm@47336
    76
  (case try Toplevel.proof_of st of
wenzelm@47336
    77
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
    78
  | NONE => ());
wenzelm@47336
    79
wenzelm@47336
    80
val no_print = Lazy.value ();
wenzelm@47336
    81
wenzelm@47336
    82
fun print_state tr st =
wenzelm@47336
    83
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@47336
    84
    (fn () => Toplevel.print_state false st);
wenzelm@47336
    85
wenzelm@47336
    86
in
wenzelm@47336
    87
wenzelm@47336
    88
fun run_command tr st =
wenzelm@47336
    89
  let
wenzelm@47336
    90
    val is_init = Toplevel.is_init tr;
wenzelm@47336
    91
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
    92
wenzelm@47336
    93
    val _ = Multithreading.interrupted ();
wenzelm@47336
    94
    val _ = Toplevel.status tr Isabelle_Markup.forked;
wenzelm@47336
    95
    val start = Timing.start ();
wenzelm@47336
    96
    val (errs, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@47336
    97
    val _ = timing tr (Timing.result start);
wenzelm@47336
    98
    val _ = Toplevel.status tr Isabelle_Markup.joined;
wenzelm@47336
    99
    val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@47336
   100
  in
wenzelm@47336
   101
    (case result of
wenzelm@47336
   102
      NONE =>
wenzelm@47336
   103
        let
wenzelm@47336
   104
          val _ = if null errs then Exn.interrupt () else ();
wenzelm@47336
   105
          val _ = Toplevel.status tr Isabelle_Markup.failed;
wenzelm@47336
   106
        in (st, no_print) end
wenzelm@47336
   107
    | SOME st' =>
wenzelm@47336
   108
        let
wenzelm@47336
   109
          val _ = Toplevel.status tr Isabelle_Markup.finished;
wenzelm@47336
   110
          val _ = proof_status tr st';
wenzelm@47336
   111
          val do_print =
wenzelm@47336
   112
            not is_init andalso
wenzelm@47336
   113
              (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@47336
   114
        in (st', if do_print then print_state tr st' else no_print) end)
wenzelm@47336
   115
  end;
wenzelm@47336
   116
wenzelm@47336
   117
end;
wenzelm@47336
   118
wenzelm@47336
   119
end;
wenzelm@47336
   120