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