src/Pure/PIDE/command.ML
author wenzelm
Fri Aug 24 12:35:39 2012 +0200 (2012-08-24 ago)
changeset 48918 6e5fd4585512
parent 48772 e46cd0d26481
child 49036 4680c4046814
permissions -rw-r--r--
check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
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@48771
    25
fun range [] = (Position.none, Position.none)
wenzelm@48771
    26
  | range toks =
wenzelm@48771
    27
      let
wenzelm@48771
    28
        val start_pos = Token.position_of (hd toks);
wenzelm@48771
    29
        val end_pos = Token.end_position_of (List.last toks);
wenzelm@48771
    30
      in (start_pos, end_pos) end;
wenzelm@48771
    31
wenzelm@48771
    32
val proper_range = range o #1 o take_suffix Token.is_space;
wenzelm@48771
    33
wenzelm@48771
    34
wenzelm@47341
    35
(* memo results *)
wenzelm@47341
    36
wenzelm@47341
    37
datatype 'a expr =
wenzelm@47341
    38
  Expr of unit -> 'a |
wenzelm@47341
    39
  Result of 'a Exn.result;
wenzelm@47341
    40
wenzelm@47341
    41
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    42
with
wenzelm@47341
    43
wenzelm@47341
    44
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
wenzelm@47341
    45
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    46
wenzelm@47342
    47
fun memo_eval (Memo v) =
wenzelm@47341
    48
  (case Synchronized.value v of
wenzelm@47341
    49
    Result res => res
wenzelm@47341
    50
  | _ =>
wenzelm@47341
    51
      Synchronized.guarded_access v
wenzelm@47341
    52
        (fn Result res => SOME (res, Result res)
wenzelm@47341
    53
          | Expr e =>
wenzelm@47341
    54
              let val res = Exn.capture e ();  (*memoing of physical interrupts!*)
wenzelm@47342
    55
              in SOME (res, Result res) end))
wenzelm@47342
    56
  |> Exn.release;
wenzelm@47342
    57
wenzelm@47342
    58
fun memo_result (Memo v) =
wenzelm@47342
    59
  (case Synchronized.value v of
wenzelm@47342
    60
    Result res => Exn.release res
wenzelm@47342
    61
  | _ => raise Fail "Unfinished memo result");
wenzelm@47341
    62
wenzelm@47341
    63
end;
wenzelm@47341
    64
wenzelm@47341
    65
wenzelm@47341
    66
(* run command *)
wenzelm@47336
    67
wenzelm@47336
    68
local
wenzelm@47336
    69
wenzelm@47336
    70
fun run int tr st =
wenzelm@47336
    71
  (case Toplevel.transition int tr st of
wenzelm@47336
    72
    SOME (st', NONE) => ([], SOME st')
wenzelm@47336
    73
  | SOME (_, SOME (exn, _)) => (ML_Compiler.exn_messages exn, NONE)
wenzelm@47336
    74
  | NONE => ([(serial (), ML_Compiler.exn_message Runtime.TERMINATE)], NONE));
wenzelm@47336
    75
wenzelm@48918
    76
fun check_cmts tr cmts st =
wenzelm@48918
    77
  Toplevel.setmp_thread_position tr
wenzelm@48918
    78
    (fn () => cmts
wenzelm@48918
    79
      |> maps (fn cmt =>
wenzelm@48918
    80
        (Thy_Output.check_text (Token.source_position_of cmt) st; [])
wenzelm@48918
    81
          handle exn => ML_Compiler.exn_messages exn)) ();
wenzelm@48918
    82
wenzelm@47336
    83
fun timing tr t =
wenzelm@47336
    84
  if Timing.is_relevant t then Toplevel.status tr (Isabelle_Markup.timing t) else ();
wenzelm@47336
    85
wenzelm@47336
    86
fun proof_status tr st =
wenzelm@47336
    87
  (case try Toplevel.proof_of st of
wenzelm@47336
    88
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
    89
  | NONE => ());
wenzelm@47336
    90
wenzelm@47336
    91
val no_print = Lazy.value ();
wenzelm@47336
    92
wenzelm@47336
    93
fun print_state tr st =
wenzelm@47336
    94
  (Lazy.lazy o Toplevel.setmp_thread_position tr)
wenzelm@47336
    95
    (fn () => Toplevel.print_state false st);
wenzelm@47336
    96
wenzelm@47336
    97
in
wenzelm@47336
    98
wenzelm@48918
    99
fun run_command (tr, cmts) (st, malformed) =
wenzelm@48772
   100
  if malformed then ((Toplevel.toplevel, malformed), no_print)
wenzelm@48772
   101
  else
wenzelm@48772
   102
    let
wenzelm@48772
   103
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   104
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   105
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   106
wenzelm@48772
   107
      val _ = Multithreading.interrupted ();
wenzelm@48772
   108
      val _ = Toplevel.status tr Isabelle_Markup.forked;
wenzelm@48772
   109
      val start = Timing.start ();
wenzelm@48918
   110
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@48918
   111
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts tr cmts st');
wenzelm@48918
   112
      val errs = errs1 @ errs2;
wenzelm@48772
   113
      val _ = timing tr (Timing.result start);
wenzelm@48772
   114
      val _ = Toplevel.status tr Isabelle_Markup.joined;
wenzelm@48772
   115
      val _ = List.app (Toplevel.error_msg tr) errs;
wenzelm@48772
   116
    in
wenzelm@48772
   117
      (case result of
wenzelm@48772
   118
        NONE =>
wenzelm@48772
   119
          let
wenzelm@48772
   120
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@48772
   121
            val _ = Toplevel.status tr Isabelle_Markup.failed;
wenzelm@48772
   122
          in ((st, malformed'), no_print) end
wenzelm@48772
   123
      | SOME st' =>
wenzelm@48772
   124
          let
wenzelm@48772
   125
            val _ = Toplevel.status tr Isabelle_Markup.finished;
wenzelm@48772
   126
            val _ = proof_status tr st';
wenzelm@48772
   127
            val do_print =
wenzelm@48772
   128
              not is_init andalso
wenzelm@48772
   129
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@48772
   130
          in ((st', malformed'), if do_print then print_state tr st' else no_print) end)
wenzelm@48772
   131
    end;
wenzelm@47336
   132
wenzelm@47336
   133
end;
wenzelm@47336
   134
wenzelm@47336
   135
end;
wenzelm@47336
   136