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