src/Pure/PIDE/command.ML
author wenzelm
Wed, 03 Jul 2013 16:58:35 +0200
changeset 52510 a4a102237ded
parent 52509 2193d2c7f586
child 52511 d5d2093ff224
permissions -rw-r--r--
tuned signature;
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
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
     9
  type span = Token.T list
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    10
  val range: span -> Position.range
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    11
  val proper_range: span -> Position.range
47341
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    12
  type 'a memo
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    13
  val memo: (unit -> 'a) -> 'a memo
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    14
  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
    15
  val memo_eval: 'a memo -> 'a
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    16
  val memo_result: 'a memo -> 'a
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    17
  val read: span -> Toplevel.transition
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    18
  val eval: span -> Toplevel.transition ->
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    19
    Toplevel.state * {malformed: bool} -> {failed: bool} * (Toplevel.state * {malformed: bool})
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    20
  val no_print: unit lazy
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    21
  val print: Toplevel.transition -> Toplevel.state -> unit lazy
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    22
end;
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    23
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    24
structure Command: COMMAND =
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    25
struct
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    26
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    27
(* source *)
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    28
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    29
type span = Token.T list;
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
    30
49866
619acbd72664 more proof method text position information;
wenzelm
parents: 49036
diff changeset
    31
val range = Token.position_range_of;
51266
3007d0bc9cb1 unified Command.is_proper in ML with Scala (see also 123be08eed88);
wenzelm
parents: 50914
diff changeset
    32
val proper_range = Token.position_range_of o #1 o take_suffix Token.is_improper;
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
    33
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
    34
47341
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    35
(* memo results *)
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    36
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    37
datatype 'a expr =
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    38
  Expr of unit -> 'a |
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    39
  Result of 'a Exn.result;
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    40
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    41
abstype 'a memo = Memo of 'a expr Synchronized.var
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    42
with
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    43
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    44
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e));
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    45
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
    46
47342
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    47
fun memo_eval (Memo v) =
47341
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    48
  (case Synchronized.value v of
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    49
    Result res => res
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    50
  | _ =>
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    51
      Synchronized.guarded_access v
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    52
        (fn Result res => SOME (res, Result res)
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    53
          | Expr e =>
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    54
              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
    55
              in SOME (res, Result res) end))
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    56
  |> Exn.release;
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    57
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    58
fun memo_result (Memo v) =
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    59
  (case Synchronized.value v of
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    60
    Result res => Exn.release res
7828c7b3c143 more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents: 47341
diff changeset
    61
  | _ => raise Fail "Unfinished memo result");
47341
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    62
00f6279bb67a Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents: 47336
diff changeset
    63
end;
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
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    66
(* read *)
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    67
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    68
fun read span =
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    69
  let
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    70
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    71
    val command_reports = Outer_Syntax.command_reports outer_syntax;
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    72
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    73
    val proper_range = Position.set_range (proper_range span);
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    74
    val pos =
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    75
      (case find_first Token.is_command span of
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    76
        SOME tok => Token.position_of tok
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    77
      | NONE => proper_range);
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    78
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    79
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    80
    val _ = Position.reports_text (token_reports @ maps command_reports span);
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    81
  in
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    82
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    83
    else
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    84
      (case Outer_Syntax.read_spans outer_syntax span of
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    85
        [tr] =>
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    86
          if Keyword.is_control (Toplevel.name_of tr) then
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    87
            Toplevel.malformed pos "Illegal control command"
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    88
          else tr
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    89
      | [] => Toplevel.ignored (Position.set_range (range span))
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    90
      | _ => Toplevel.malformed proper_range "Exactly one command expected")
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    91
      handle ERROR msg => Toplevel.malformed proper_range msg
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    92
  end;
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    93
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    94
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
    95
(* eval *)
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    96
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    97
local
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    98
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
    99
fun run int tr st =
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51266
diff changeset
   100
  if Goal.future_enabled () andalso Keyword.is_diag (Toplevel.name_of tr) then
51605
eca8acb42e4a more explicit Goal.fork_params -- avoid implicit arguments via thread data;
wenzelm
parents: 51603
diff changeset
   101
    (Goal.fork_params {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
eca8acb42e4a more explicit Goal.fork_params -- avoid implicit arguments via thread data;
wenzelm
parents: 51603
diff changeset
   102
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51266
diff changeset
   103
  else Toplevel.command_errors int tr st;
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   104
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   105
fun check_cmts span tr st' =
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   106
  Toplevel.setmp_thread_position tr
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   107
    (fn () =>
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   108
      Outer_Syntax.side_comments span |> maps (fn cmt =>
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   109
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   110
          handle exn => ML_Compiler.exn_messages_ids exn)) ();
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   111
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   112
fun proof_status tr st =
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   113
  (case try Toplevel.proof_of st of
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   114
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   115
  | NONE => ());
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   116
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   117
in
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   118
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   119
fun eval span tr (st, {malformed}) =
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   120
  if malformed then
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   121
    ({failed = true}, (Toplevel.toplevel, {malformed = malformed}))
48772
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   122
  else
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   123
    let
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   124
      val malformed' = Toplevel.is_malformed tr;
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   125
      val is_init = Toplevel.is_init tr;
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   126
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   127
48772
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   128
      val _ = Multithreading.interrupted ();
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49866
diff changeset
   129
      val _ = Toplevel.status tr Markup.running;
48918
6e5fd4585512 check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents: 48772
diff changeset
   130
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   131
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
48918
6e5fd4585512 check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents: 48772
diff changeset
   132
      val errs = errs1 @ errs2;
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49866
diff changeset
   133
      val _ = Toplevel.status tr Markup.finished;
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
   134
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
48772
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   135
    in
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   136
      (case result of
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   137
        NONE =>
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   138
          let
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   139
            val _ = if null errs then Exn.interrupt () else ();
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49866
diff changeset
   140
            val _ = Toplevel.status tr Markup.failed;
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   141
          in ({failed = true}, (st, {malformed = malformed'})) end
48772
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   142
      | SOME st' =>
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   143
          let
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   144
            val _ = proof_status tr st';
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   145
          in ({failed = false}, (st', {malformed = malformed'})) end)
48772
e46cd0d26481 vacuous execution after first malformed command;
wenzelm
parents: 48771
diff changeset
   146
    end;
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   147
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   148
end;
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   149
52509
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   150
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   151
(* print *)
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   152
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   153
val no_print = Lazy.value ();
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   154
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   155
fun print tr st' =
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   156
  let
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   157
    val is_init = Toplevel.is_init tr;
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   158
    val is_proof = Keyword.is_proof (Toplevel.name_of tr);
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   159
    val do_print =
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   160
      not is_init andalso
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   161
        (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   162
  in
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   163
    if do_print then
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   164
      (Lazy.lazy o Toplevel.setmp_thread_position tr)
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   165
        (fn () => Toplevel.print_state false st')
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   166
    else no_print
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   167
  end;
2193d2c7f586 tuned signature;
wenzelm
parents: 51605
diff changeset
   168
47336
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   169
end;
bed4b2738d8a separate module for prover command execution;
wenzelm
parents:
diff changeset
   170