author | wenzelm |
Wed, 03 Jul 2013 17:50:47 +0200 | |
changeset 52511 | d5d2093ff224 |
parent 52510 | a4a102237ded |
child 52515 | 0dcadc90550b |
permissions | -rw-r--r-- |
47336 | 1 |
(* Title: Pure/PIDE/command.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Prover command execution. |
|
5 |
*) |
|
6 |
||
7 |
signature COMMAND = |
|
8 |
sig |
|
52509 | 9 |
type span = Token.T list |
10 |
val range: span -> Position.range |
|
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 | 17 |
val read: span -> Toplevel.transition |
52509 | 18 |
val eval: span -> Toplevel.transition -> |
19 |
Toplevel.state * {malformed: bool} -> {failed: bool} * (Toplevel.state * {malformed: bool}) |
|
52511 | 20 |
val print: Toplevel.transition -> Toplevel.state -> (string * unit lazy) list |
21 |
val print_function: string -> |
|
22 |
({tr: Toplevel.transition, state: Toplevel.state} -> (unit -> unit) option) -> unit |
|
47336 | 23 |
end; |
24 |
||
25 |
structure Command: COMMAND = |
|
26 |
struct |
|
27 |
||
52509 | 28 |
(* source *) |
29 |
||
30 |
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
|
31 |
|
49866 | 32 |
val range = Token.position_range_of; |
51266
3007d0bc9cb1
unified Command.is_proper in ML with Scala (see also 123be08eed88);
wenzelm
parents:
50914
diff
changeset
|
33 |
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
|
34 |
|
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
|
35 |
|
47341
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
36 |
(* memo results *) |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
37 |
|
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
38 |
datatype 'a expr = |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
39 |
Expr of unit -> 'a | |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
40 |
Result of 'a Exn.result; |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
41 |
|
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
42 |
abstype 'a memo = Memo of 'a expr Synchronized.var |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
43 |
with |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
44 |
|
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
45 |
fun memo e = Memo (Synchronized.var "Command.memo" (Expr e)); |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
46 |
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
|
47 |
|
47342
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
48 |
fun memo_eval (Memo v) = |
47341
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
49 |
(case Synchronized.value v of |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
50 |
Result res => res |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
51 |
| _ => |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
52 |
Synchronized.guarded_access v |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
53 |
(fn Result res => SOME (res, Result res) |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
54 |
| Expr e => |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
55 |
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
|
56 |
in SOME (res, Result res) end)) |
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
57 |
|> Exn.release; |
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
58 |
|
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
59 |
fun memo_result (Memo v) = |
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
60 |
(case Synchronized.value v of |
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
61 |
Result res => Exn.release res |
7828c7b3c143
more explicit memo_eval vs. memo_result, to enforce bottom-up execution;
wenzelm
parents:
47341
diff
changeset
|
62 |
| _ => raise Fail "Unfinished memo result"); |
47341
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 |
end; |
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
65 |
|
00f6279bb67a
Command.memo including physical interrupts (unlike Lazy.lazy);
wenzelm
parents:
47336
diff
changeset
|
66 |
|
52510 | 67 |
(* read *) |
52509 | 68 |
|
52510 | 69 |
fun read span = |
70 |
let |
|
71 |
val outer_syntax = #2 (Outer_Syntax.get_syntax ()); |
|
72 |
val command_reports = Outer_Syntax.command_reports outer_syntax; |
|
52509 | 73 |
|
52510 | 74 |
val proper_range = Position.set_range (proper_range span); |
75 |
val pos = |
|
76 |
(case find_first Token.is_command span of |
|
77 |
SOME tok => Token.position_of tok |
|
78 |
| NONE => proper_range); |
|
52509 | 79 |
|
52510 | 80 |
val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span; |
81 |
val _ = Position.reports_text (token_reports @ maps command_reports span); |
|
82 |
in |
|
83 |
if is_malformed then Toplevel.malformed pos "Malformed command syntax" |
|
84 |
else |
|
85 |
(case Outer_Syntax.read_spans outer_syntax span of |
|
86 |
[tr] => |
|
87 |
if Keyword.is_control (Toplevel.name_of tr) then |
|
88 |
Toplevel.malformed pos "Illegal control command" |
|
89 |
else tr |
|
90 |
| [] => Toplevel.ignored (Position.set_range (range span)) |
|
91 |
| _ => Toplevel.malformed proper_range "Exactly one command expected") |
|
92 |
handle ERROR msg => Toplevel.malformed proper_range msg |
|
93 |
end; |
|
52509 | 94 |
|
95 |
||
96 |
(* eval *) |
|
47336 | 97 |
|
98 |
local |
|
99 |
||
100 |
fun run int tr st = |
|
51284
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51266
diff
changeset
|
101 |
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
|
102 |
(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
|
103 |
(fn () => Toplevel.command_exception int tr st); ([], SOME st)) |
51284
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51266
diff
changeset
|
104 |
else Toplevel.command_errors int tr st; |
47336 | 105 |
|
52510 | 106 |
fun check_cmts span tr st' = |
107 |
Toplevel.setmp_thread_position tr |
|
108 |
(fn () => |
|
109 |
Outer_Syntax.side_comments span |> maps (fn cmt => |
|
110 |
(Thy_Output.check_text (Token.source_position_of cmt) st'; []) |
|
111 |
handle exn => ML_Compiler.exn_messages_ids exn)) (); |
|
112 |
||
47336 | 113 |
fun proof_status tr st = |
114 |
(case try Toplevel.proof_of st of |
|
115 |
SOME prf => Toplevel.status tr (Proof.status_markup prf) |
|
116 |
| NONE => ()); |
|
117 |
||
118 |
in |
|
119 |
||
52509 | 120 |
fun eval span tr (st, {malformed}) = |
121 |
if malformed then |
|
122 |
({failed = true}, (Toplevel.toplevel, {malformed = malformed})) |
|
48772 | 123 |
else |
124 |
let |
|
125 |
val malformed' = Toplevel.is_malformed tr; |
|
126 |
val is_init = Toplevel.is_init tr; |
|
127 |
val is_proof = Keyword.is_proof (Toplevel.name_of tr); |
|
47336 | 128 |
|
48772 | 129 |
val _ = Multithreading.interrupted (); |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49866
diff
changeset
|
130 |
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
|
131 |
val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st; |
52509 | 132 |
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
|
133 |
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
|
134 |
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
|
135 |
val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs; |
48772 | 136 |
in |
137 |
(case result of |
|
138 |
NONE => |
|
139 |
let |
|
140 |
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
|
141 |
val _ = Toplevel.status tr Markup.failed; |
52509 | 142 |
in ({failed = true}, (st, {malformed = malformed'})) end |
48772 | 143 |
| SOME st' => |
144 |
let |
|
145 |
val _ = proof_status tr st'; |
|
52509 | 146 |
in ({failed = false}, (st', {malformed = malformed'})) end) |
48772 | 147 |
end; |
47336 | 148 |
|
149 |
end; |
|
150 |
||
52509 | 151 |
|
152 |
(* print *) |
|
153 |
||
52511 | 154 |
local |
155 |
||
156 |
type print_function = |
|
157 |
{tr: Toplevel.transition, state: Toplevel.state} -> (unit -> unit) option; |
|
158 |
||
159 |
val print_functions = |
|
160 |
Synchronized.var "Command.print_functions" ([]: (string * print_function) list); |
|
161 |
||
162 |
in |
|
52509 | 163 |
|
164 |
fun print tr st' = |
|
52511 | 165 |
rev (Synchronized.value print_functions) |> map_filter (fn (name, f) => |
166 |
(case f {tr = tr, state = st'} of |
|
167 |
SOME pr => SOME (name, (Lazy.lazy o Toplevel.setmp_thread_position tr) pr) |
|
168 |
| NONE => NONE)); |
|
169 |
||
170 |
fun print_function name f = |
|
171 |
Synchronized.change print_functions (fn funs => |
|
172 |
(if not (AList.defined (op =) funs name) then () |
|
173 |
else warning ("Redefining command print function: " ^ quote name); |
|
174 |
AList.update (op =) (name, f) funs)); |
|
175 |
||
176 |
end; |
|
177 |
||
178 |
val _ = print_function "print_state" (fn {tr, state} => |
|
52509 | 179 |
let |
180 |
val is_init = Toplevel.is_init tr; |
|
181 |
val is_proof = Keyword.is_proof (Toplevel.name_of tr); |
|
182 |
val do_print = |
|
183 |
not is_init andalso |
|
52511 | 184 |
(Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof state)); |
185 |
in if do_print then SOME (fn () => Toplevel.print_state false state) else NONE end); |
|
52509 | 186 |
|
47336 | 187 |
end; |
188 |