author | wenzelm |
Sun, 04 Feb 2007 22:02:14 +0100 | |
changeset 22234 | 52ba19aaa9c2 |
parent 22228 | 7c27195a4afc |
child 22243 | 0f24888c8591 |
permissions | -rw-r--r-- |
21637 | 1 |
(* Title: Pure/ProofGeneral/proof_general_pgip.ML |
2 |
ID: $Id$ |
|
3 |
Author: David Aspinall and Markus Wenzel |
|
4 |
||
5 |
Isabelle configuration for Proof General using PGIP protocol. |
|
21940 | 6 |
See http://proofgeneral.inf.ed.ac.uk/kit |
21637 | 7 |
*) |
8 |
||
9 |
signature PROOF_GENERAL_PGIP = |
|
10 |
sig |
|
21940 | 11 |
val init_pgip: bool -> unit (* main PGIP loop with true; fail with false *) |
21642 | 12 |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
13 |
(* These two are just to support the semi-PGIP Emacs mode *) |
21940 | 14 |
val init_pgip_channel: (string -> unit) -> unit |
15 |
val process_pgip: string -> unit |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
16 |
|
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
17 |
(* Yet more message functions... *) |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
18 |
val nonfatal_error : string -> unit (* recoverable error *) |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
19 |
val log_msg : string -> unit (* for internal log messages *) |
22163 | 20 |
val error_with_pos : PgipTypes.fatality -> Position.T -> string -> unit |
22159 | 21 |
|
22163 | 22 |
val get_currently_open_file : unit -> Path.T option (* interface focus *) |
21637 | 23 |
end |
24 |
||
25 |
structure ProofGeneralPgip : PROOF_GENERAL_PGIP = |
|
26 |
struct |
|
27 |
||
28 |
open Pgip; |
|
29 |
||
21949 | 30 |
|
21637 | 31 |
(* print modes *) |
32 |
||
33 |
val proof_generalN = "ProofGeneral"; (*token markup (colouring vars, etc.)*) |
|
34 |
val pgmlN = "PGML"; (*XML escapes and PGML symbol elements*) |
|
35 |
val pgmlatomsN = "PGMLatoms"; (*variable kind decorations*) |
|
36 |
val thm_depsN = "thm_deps"; (*meta-information about theorem deps*) |
|
37 |
||
38 |
||
39 |
(* text output: print modes for xsymbol and PGML *) |
|
40 |
||
41 |
local |
|
42 |
||
43 |
fun xsym_output "\\" = "\\\\" |
|
44 |
| xsym_output s = if Symbol.is_raw s then Symbol.decode_raw s else s; |
|
45 |
||
46 |
fun xsymbols_output s = |
|
21940 | 47 |
if Output.has_mode Symbol.xsymbolsN andalso exists_string (equal "\\") s then |
21637 | 48 |
let val syms = Symbol.explode s |
49 |
in (implode (map xsym_output syms), real (Symbol.length syms)) end |
|
50 |
else Symbol.default_output s; |
|
51 |
||
52 |
(* XML immediately rendered pretty printer. Take care not to double escape *) |
|
53 |
fun pgml_sym s = |
|
54 |
(case Symbol.decode s of |
|
55 |
Symbol.Char s => XML.text s |
|
21940 | 56 |
| Symbol.Sym sn => XML.element "sym" [("name", sn)] [XML.text s] |
21637 | 57 |
| Symbol.Ctrl sn => XML.element "ctrl" [("name", sn)] [XML.text s] (* FIXME: no such PGML! *) |
58 |
| Symbol.Raw s => s); |
|
59 |
||
60 |
fun pgml_output str = |
|
61 |
let val syms = Symbol.explode str |
|
62 |
in (implode (map pgml_sym syms), real (Symbol.length syms)) end; |
|
63 |
||
64 |
in |
|
65 |
||
66 |
fun setup_xsymbols_output () = |
|
21940 | 67 |
Output.add_mode Symbol.xsymbolsN |
21637 | 68 |
(xsymbols_output, K xsymbols_output, Symbol.default_indent, Symbol.encode_raw); |
69 |
||
70 |
fun setup_pgml_output () = |
|
71 |
Output.add_mode pgmlN |
|
72 |
(pgml_output, K pgml_output, Symbol.default_indent, Symbol.encode_raw); |
|
73 |
||
74 |
end; |
|
75 |
||
76 |
||
77 |
(* token translations *) |
|
78 |
||
79 |
local |
|
80 |
||
81 |
val class_tag = "class" |
|
82 |
val tfree_tag = "tfree" |
|
83 |
val tvar_tag = "tvar" |
|
84 |
val free_tag = "free" |
|
85 |
val bound_tag = "bound" |
|
86 |
val var_tag = "var" |
|
87 |
val skolem_tag = "skolem" |
|
88 |
||
89 |
fun xml_atom kind x = XML.element "atom" [("kind", kind)] [XML.text x]; |
|
90 |
||
21940 | 91 |
fun tagit kind x = |
21949 | 92 |
(xml_atom kind x, real (Symbol.length (Symbol.explode x))); |
21637 | 93 |
|
94 |
fun free_or_skolem x = |
|
95 |
(case try Name.dest_skolem x of |
|
96 |
NONE => tagit free_tag x |
|
97 |
| SOME x' => tagit skolem_tag x'); |
|
98 |
||
99 |
fun var_or_skolem s = |
|
100 |
(case Syntax.read_variable s of |
|
101 |
SOME (x, i) => |
|
102 |
(case try Name.dest_skolem x of |
|
103 |
NONE => tagit var_tag s |
|
104 |
| SOME x' => tagit skolem_tag |
|
105 |
(setmp show_question_marks true Syntax.string_of_vname (x', i))) |
|
106 |
| NONE => tagit var_tag s); |
|
107 |
||
108 |
val proof_general_trans = |
|
109 |
Syntax.tokentrans_mode proof_generalN |
|
110 |
[("class", tagit class_tag), |
|
111 |
("tfree", tagit tfree_tag), |
|
112 |
("tvar", tagit tvar_tag), |
|
113 |
("free", free_or_skolem), |
|
114 |
("bound", tagit bound_tag), |
|
115 |
("var", var_or_skolem)]; |
|
116 |
||
117 |
in |
|
118 |
||
119 |
val _ = Context.add_setup (Theory.add_tokentrfuns proof_general_trans); |
|
120 |
||
121 |
end; |
|
122 |
||
123 |
||
124 |
(** assembling and issuing PGIP packets **) |
|
125 |
||
126 |
val pgip_refid = ref NONE: string option ref; |
|
127 |
val pgip_refseq = ref NONE: int option ref; |
|
128 |
||
129 |
local |
|
130 |
val pgip_class = "pg" |
|
131 |
val pgip_tag = "Isabelle/Isar" |
|
132 |
val pgip_id = ref "" |
|
133 |
val pgip_seq = ref 0 |
|
134 |
fun pgip_serial () = inc pgip_seq |
|
135 |
||
136 |
fun assemble_pgips pgips = |
|
21940 | 137 |
Pgip { tag = SOME pgip_tag, |
138 |
class = pgip_class, |
|
139 |
seq = pgip_serial(), |
|
140 |
id = !pgip_id, |
|
141 |
destid = !pgip_refid, |
|
142 |
(* destid=refid since Isabelle only communicates back to sender *) |
|
143 |
refid = !pgip_refid, |
|
144 |
refseq = !pgip_refseq, |
|
145 |
content = pgips } |
|
21637 | 146 |
in |
147 |
||
148 |
fun init_pgip_session_id () = |
|
149 |
pgip_id := getenv "HOSTNAME" ^ "/" ^ getenv "USER" ^ "/" ^ |
|
150 |
getenv "ISABELLE_PID" ^ "/" ^ Time.toString (Time.now ()) |
|
151 |
||
152 |
fun matching_pgip_id id = (id = !pgip_id) |
|
153 |
||
21940 | 154 |
val output_xml_fn = ref writeln_default |
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
155 |
fun output_xml s = (!output_xml_fn) (XML.string_of_tree s); (* TODO: string buffer *) |
21637 | 156 |
|
21940 | 157 |
fun issue_pgip_rawtext str = |
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
158 |
output_xml (PgipOutput.output (assemble_pgips [XML.Rawtext str])) |
21637 | 159 |
|
160 |
fun issue_pgips pgipops = |
|
161 |
output_xml (PgipOutput.output (assemble_pgips (map PgipOutput.output pgipops))); |
|
162 |
||
163 |
fun issue_pgip pgipop = |
|
164 |
output_xml (PgipOutput.output (assemble_pgips [PgipOutput.output pgipop])); |
|
165 |
||
166 |
end; |
|
167 |
||
168 |
||
169 |
(** messages and notification **) |
|
170 |
||
171 |
local |
|
172 |
val delay_msgs = ref false (*true: accumulate messages*) |
|
173 |
val delayed_msgs = ref [] |
|
174 |
||
175 |
fun queue_or_issue pgip = |
|
21940 | 176 |
if ! delay_msgs then |
177 |
delayed_msgs := pgip :: ! delayed_msgs |
|
178 |
else issue_pgip pgip |
|
21637 | 179 |
in |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
180 |
(* Normal responses are messages for the user *) |
21940 | 181 |
fun normalmsg area cat urgent s = |
182 |
let |
|
183 |
val content = XML.Elem("pgmltext",[],[XML.Rawtext s]) |
|
184 |
val pgip = Normalresponse {area=area,messagecategory=cat, |
|
185 |
urgent=urgent,content=[content] } |
|
186 |
in |
|
187 |
queue_or_issue pgip |
|
188 |
end |
|
21637 | 189 |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
190 |
(* Error responses are error messages for the user, or system-level messages *) |
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
191 |
fun errormsg fatality s = |
21940 | 192 |
let |
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
193 |
val content = XML.Elem("pgmltext",[],[XML.Rawtext s]) |
21940 | 194 |
val pgip = Errorresponse {area=NONE,fatality=fatality, |
195 |
content=[content], |
|
196 |
location=NONE} |
|
197 |
in |
|
198 |
queue_or_issue pgip |
|
199 |
end |
|
21637 | 200 |
|
22159 | 201 |
(* Error responses with useful locations *) |
202 |
fun error_with_pos fatality pos s = |
|
203 |
let |
|
204 |
val content = XML.Elem("pgmltext",[],[XML.Rawtext s]) |
|
205 |
val pgip = Errorresponse {area=NONE,fatality=fatality, |
|
206 |
content=[content], |
|
207 |
location=SOME (PgipIsabelle.location_of_position pos)} |
|
208 |
in |
|
209 |
queue_or_issue pgip |
|
210 |
end |
|
211 |
||
21637 | 212 |
fun start_delay_msgs () = (set delay_msgs; delayed_msgs := []) |
213 |
fun end_delayed_msgs () = (reset delay_msgs; ! delayed_msgs) |
|
214 |
end; |
|
215 |
||
216 |
(* NB: all of the standard error/message functions now expect already-escaped text. |
|
217 |
FIXME: this may cause us problems now we're generating trees; on the other |
|
21940 | 218 |
hand the output functions were tuned some time ago, so it ought to be |
21637 | 219 |
enough to use Rawtext always above. *) |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
220 |
(* NB 2: all of standard functions print strings terminated with new lines, but we don't |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
221 |
add new lines explicitly in PGIP: they are left implicit. It means that PGIP messages |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
222 |
can't be written without newlines. *) |
21637 | 223 |
|
224 |
fun setup_messages () = |
|
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
225 |
(writeln_fn := (fn s => normalmsg Message Normal false s); |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
226 |
priority_fn := (fn s => normalmsg Message Normal true s); |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
227 |
tracing_fn := (fn s => normalmsg Message Tracing false s); |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
228 |
info_fn := (fn s => errormsg Info s); |
22001 | 229 |
warning_fn := (fn s => errormsg Warning s); |
22014
4b70cbd96007
removed Toplevel.print_exn hook -- existing error_msg_fn does the job;
wenzelm
parents:
22001
diff
changeset
|
230 |
error_fn := (fn s => errormsg Fatal s); |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
231 |
panic_fn := (fn s => errormsg Panic s); |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
232 |
debug_fn := (fn s => errormsg Debug s)); |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
233 |
|
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
234 |
fun nonfatal_error s = errormsg Nonfatal s; |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
235 |
fun log_msg s = errormsg Log s; |
21983
9fb029d1189b
Use Isar toplevel print_exn_fn for generating error responses instead of Output.error_msg.
aspinall
parents:
21972
diff
changeset
|
236 |
|
21637 | 237 |
|
238 |
(* immediate messages *) |
|
239 |
||
240 |
fun tell_clear_goals () = issue_pgip (Cleardisplay {area=Display}) |
|
241 |
fun tell_clear_response () = issue_pgip (Cleardisplay {area=Message}) |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
242 |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
243 |
fun tell_file_loaded completed path = |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
244 |
issue_pgip (Informfileloaded {url=PgipTypes.pgipurl_of_path path, |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
245 |
completed=completed}) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
246 |
fun tell_file_outdated completed path = |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
247 |
issue_pgip (Informfileoutdated {url=PgipTypes.pgipurl_of_path path, |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
248 |
completed=completed}) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
249 |
fun tell_file_retracted completed path = |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
250 |
issue_pgip (Informfileretracted {url=PgipTypes.pgipurl_of_path path, |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
251 |
completed=completed}) |
21637 | 252 |
|
253 |
||
254 |
||
255 |
(** theory / proof state output **) |
|
256 |
||
257 |
local |
|
258 |
||
259 |
fun statedisplay prts = |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
260 |
let |
21940 | 261 |
val display = Pretty.output (Pretty.chunks prts) |
22028 | 262 |
(* TODO: add extra PGML markup for allow proof state navigation *) |
21940 | 263 |
val statedisplay = XML.Elem("statedisplay",[], [XML.Rawtext display]) |
21637 | 264 |
in |
21940 | 265 |
issue_pgip (Proofstate {pgml=[XML.Elem("pgml",[],[statedisplay])]}) |
21637 | 266 |
end |
267 |
||
268 |
fun print_current_goals n m st = |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
269 |
case (Display.pretty_current_goals n m st) of |
22028 | 270 |
[] => tell_clear_goals() |
271 |
| prts => statedisplay prts |
|
21637 | 272 |
|
273 |
fun print_state b st = |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
274 |
case (Toplevel.pretty_state b st) of |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
275 |
[] => tell_clear_goals() |
22028 | 276 |
| prts => statedisplay prts |
21637 | 277 |
|
278 |
in |
|
279 |
||
280 |
fun setup_state () = |
|
281 |
(Display.print_current_goals_fn := print_current_goals; |
|
282 |
Toplevel.print_state_fn := print_state); |
|
283 |
||
284 |
end; |
|
285 |
||
286 |
||
287 |
(* theory loader actions *) |
|
288 |
||
289 |
local |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
290 |
(* da: TODO: PGIP has a completed flag so the prover can indicate to the |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
291 |
interface which files are busy performing a particular action. |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
292 |
To make use of this we need to adjust the hook in thy_info.ML |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
293 |
(may actually be difficult to tell the interface *which* action is in |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
294 |
progress, but we could add a generic "Lock" action which uses |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
295 |
informfileloaded: the broker/UI should not infer too much from incomplete |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
296 |
operations). |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
297 |
*) |
21637 | 298 |
fun trace_action action name = |
299 |
if action = ThyInfo.Update then |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
300 |
List.app (tell_file_loaded true) (ThyInfo.loaded_files name) |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
301 |
else if action = ThyInfo.Outdate then |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
302 |
List.app (tell_file_outdated true) (ThyInfo.loaded_files name) |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
303 |
else if action = ThyInfo.Remove then |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
304 |
List.app (tell_file_retracted true) (ThyInfo.loaded_files name) |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
305 |
else () |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
306 |
|
21637 | 307 |
|
308 |
in |
|
309 |
fun setup_thy_loader () = ThyInfo.add_hook trace_action; |
|
310 |
fun sync_thy_loader () = List.app (trace_action ThyInfo.Update) (ThyInfo.names ()); |
|
311 |
end; |
|
312 |
||
313 |
||
21949 | 314 |
(* get informed about files *) |
21637 | 315 |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
316 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base; |
21637 | 317 |
|
318 |
val inform_file_retracted = ThyInfo.if_known_thy ThyInfo.remove_thy o thy_name; |
|
319 |
val inform_file_processed = ThyInfo.if_known_thy ThyInfo.touch_child_thys o thy_name; |
|
320 |
||
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
321 |
fun proper_inform_file_processed path state = |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
322 |
let val name = thy_name path in |
21637 | 323 |
if Toplevel.is_toplevel state andalso ThyInfo.known_thy name then |
324 |
(ThyInfo.touch_child_thys name; |
|
325 |
ThyInfo.pretend_use_thy_only name handle ERROR msg => |
|
326 |
(warning msg; warning ("Failed to register theory: " ^ quote name); |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
327 |
tell_file_retracted true (Path.base path))) |
21637 | 328 |
else raise Toplevel.UNDEF |
329 |
end; |
|
330 |
||
331 |
||
332 |
(* restart top-level loop (keeps most state information) *) |
|
333 |
||
334 |
val welcome = priority o Session.welcome; |
|
335 |
||
336 |
fun restart () = |
|
337 |
(sync_thy_loader (); |
|
338 |
tell_clear_goals (); |
|
339 |
tell_clear_response (); |
|
340 |
welcome (); |
|
341 |
raise Toplevel.RESTART) |
|
342 |
||
343 |
||
344 |
(* theorem dependency output *) |
|
345 |
local |
|
346 |
||
347 |
val spaces_quote = space_implode " " o map quote; |
|
348 |
||
349 |
fun thm_deps_message (thms, deps) = |
|
21940 | 350 |
let |
351 |
val valuethms = XML.Elem("value",[("name", "thms")],[XML.Text thms]) |
|
352 |
val valuedeps = XML.Elem("value",[("name", "deps")],[XML.Text deps]) |
|
21637 | 353 |
in |
21940 | 354 |
issue_pgip (Metainforesponse {attrs=[("infotype", "isabelle_theorem_dependencies")], |
355 |
content=[valuethms,valuedeps]}) |
|
21637 | 356 |
end |
357 |
||
21969 | 358 |
fun tell_thm_deps ths = |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
359 |
if Output.has_mode thm_depsN then |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
360 |
let |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
361 |
val names = map PureThy.get_name_hint (filter PureThy.has_name_hint ths); |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
362 |
val deps = (Symtab.keys (fold Proofterm.thms_of_proof' |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
363 |
(map Thm.proof_of ths) Symtab.empty)) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
364 |
in |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
365 |
if null names orelse null deps then () |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
366 |
else thm_deps_message (spaces_quote names, spaces_quote deps) |
22225 | 367 |
end |
368 |
else () |
|
21637 | 369 |
|
370 |
in |
|
371 |
||
372 |
fun setup_present_hook () = |
|
373 |
Present.add_hook (fn _ => fn res => tell_thm_deps (maps #2 res)); |
|
374 |
||
375 |
end; |
|
376 |
||
377 |
(** lexicalstructure element with keywords (PGIP version of elisp keywords file) **) |
|
378 |
||
21940 | 379 |
fun lexicalstructure_keywords () = |
21637 | 380 |
let val commands = OuterSyntax.dest_keywords () |
21940 | 381 |
fun category_of k = if k mem commands then "major" else "minor" |
21637 | 382 |
(* NB: we might filter to only include words like elisp case (OuterSyntax.is_keyword). *) |
21940 | 383 |
fun keyword_elt (keyword,help,kind,_) = |
384 |
XML.Elem("keyword", [("word", keyword), ("category", category_of kind)], |
|
385 |
[XML.Elem("shorthelp", [], [XML.Text help])]) |
|
386 |
in |
|
21637 | 387 |
(* Also, note we don't call init_outer_syntax here to add interface commands, |
388 |
but they should never appear in scripts anyway so it shouldn't matter *) |
|
389 |
Lexicalstructure {content = map keyword_elt (OuterSyntax.dest_parsers()) } |
|
390 |
end |
|
391 |
||
392 |
(* TODO: we can issue a lexicalstructure/keyword when the syntax gets extended dynamically; |
|
393 |
hooks needed in outer_syntax.ML to do that. *) |
|
394 |
||
395 |
||
396 |
(* Configuration: GUI config, proverinfo messages *) |
|
397 |
||
398 |
local |
|
399 |
val isabellewww = "http://isabelle.in.tum.de/" |
|
400 |
val staticconfig = "~~/lib/ProofGeneral/pgip_isar.xml" |
|
21940 | 401 |
fun orenv v d = case getenv v of "" => d | s => s |
21637 | 402 |
fun config_file() = orenv "ISABELLE_PGIPCONFIG" staticconfig |
403 |
fun isabelle_www() = orenv "ISABELLE_HOMEPAGE" isabellewww |
|
404 |
in |
|
405 |
fun send_pgip_config () = |
|
406 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21655
diff
changeset
|
407 |
val path = Path.explode (config_file()) |
21940 | 408 |
val ex = File.exists path |
21637 | 409 |
|
21940 | 410 |
val wwwpage = |
411 |
(Url.explode (isabelle_www())) |
|
21969 | 412 |
handle ERROR _ => |
21940 | 413 |
(Output.panic |
414 |
("Error in URL in environment variable ISABELLE_HOMEPAGE."); |
|
415 |
Url.explode isabellewww) |
|
416 |
||
417 |
val proverinfo = |
|
21637 | 418 |
Proverinfo { name = "Isabelle", |
21940 | 419 |
version = version, |
420 |
instance = Session.name(), |
|
421 |
descr = "The Isabelle/Isar theorem prover", |
|
422 |
url = wwwpage, |
|
423 |
filenameextns = ".thy;" } |
|
21637 | 424 |
in |
21940 | 425 |
if ex then |
426 |
(issue_pgip proverinfo; |
|
427 |
issue_pgip_rawtext (File.read path); |
|
428 |
issue_pgip (lexicalstructure_keywords())) |
|
21637 | 429 |
else Output.panic ("PGIP configuration file \"" ^ config_file() ^ "\" not found") |
430 |
end; |
|
431 |
end |
|
432 |
||
433 |
||
22216
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
434 |
(* Preferences: tweak for PGIP interfaces *) |
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
435 |
|
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
436 |
val preferences = ref Preferences.preferences; |
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
437 |
|
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
438 |
fun setup_preferences_tweak() = |
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
439 |
preferences := |
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
440 |
(* PGIP uses markup for distinguishing variable types *) |
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
441 |
(!preferences |> Preferences.set_default ("show-question-marks","false") |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
442 |
|> Preferences.remove "show-question-marks"); |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
443 |
|
21637 | 444 |
|
445 |
||
446 |
(* Sending commands to Isar *) |
|
447 |
||
448 |
fun isarcmd s = |
|
449 |
s |> OuterSyntax.scan |> OuterSyntax.read |
|
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
450 |
(*|> map (Toplevel.position (Position.name "PGIP message") o #3)*) |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
451 |
|> map #3 |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
452 |
|> Toplevel.>>>; |
21637 | 453 |
|
21940 | 454 |
(* TODO: |
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
455 |
- apply a command given a transition function, e.g. IsarCmd.undo. |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
456 |
- fix position from path of currently open file [line numbers risk garbling though]. |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
457 |
*) |
21637 | 458 |
|
459 |
(* load an arbitrary file (must be .thy or .ML) *) |
|
460 |
||
461 |
fun use_thy_or_ml_file file = |
|
462 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21655
diff
changeset
|
463 |
val (path,extn) = Path.split_ext (Path.explode file) |
21637 | 464 |
in |
465 |
case extn of |
|
21940 | 466 |
"" => isarcmd ("use_thy " ^ quote (Path.implode path)) |
467 |
| "thy" => isarcmd ("use_thy " ^ quote (Path.implode path)) |
|
21637 | 468 |
| "ML" => isarcmd ("use " ^ quote file) |
22028 | 469 |
| other => error ("Don't know how to read a file with extension " ^ quote other) |
21637 | 470 |
end |
471 |
||
472 |
||
21867 | 473 |
(******* PGIP actions *******) |
21637 | 474 |
|
475 |
||
21940 | 476 |
(* Responses to each of the PGIP input commands. |
21637 | 477 |
These are programmed uniformly for extensibility. *) |
478 |
||
21940 | 479 |
fun askpgip (Askpgip vs) = |
21637 | 480 |
issue_pgip |
481 |
(Usespgip { version = PgipIsabelle.isabelle_pgip_version_supported, |
|
21940 | 482 |
pgipelems = PgipIsabelle.accepted_inputs }) |
21637 | 483 |
|
21940 | 484 |
fun askpgml (Askpgml vs) = |
21637 | 485 |
issue_pgip |
21940 | 486 |
(Usespgml { version = PgipIsabelle.isabelle_pgml_version_supported }) |
21637 | 487 |
|
21902 | 488 |
fun askprefs (Askprefs vs) = |
21940 | 489 |
let |
490 |
fun preference_of {name, descr, default, pgiptype, get, set } = |
|
491 |
{ name = name, descr = SOME descr, default = SOME default, |
|
492 |
pgiptype = pgiptype } |
|
21637 | 493 |
in |
21940 | 494 |
List.app (fn (prefcat, prefs) => |
495 |
issue_pgip (Hasprefs {prefcategory=SOME prefcat, |
|
496 |
prefs=map preference_of prefs})) |
|
22216
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
497 |
(!preferences) |
21940 | 498 |
end |
21637 | 499 |
|
21902 | 500 |
fun askconfig (Askconfig vs) = () (* TODO: add config response *) |
21637 | 501 |
|
502 |
local |
|
21940 | 503 |
fun lookuppref pref = |
504 |
case AList.lookup (op =) |
|
505 |
(map (fn p => (#name p,p)) |
|
22216
c3f5aa951a68
Tweak preferences for PGIP interfaces. Make <askids> return theory successors instead of parents (ideally should be content elements).
aspinall
parents:
22171
diff
changeset
|
506 |
(maps snd (!preferences))) pref of |
21940 | 507 |
NONE => error ("Unknown prover preference: " ^ quote pref) |
508 |
| SOME p => p |
|
21637 | 509 |
in |
21940 | 510 |
fun setpref (Setpref vs) = |
511 |
let |
|
512 |
val name = #name vs |
|
513 |
val value = #value vs |
|
514 |
val set = #set (lookuppref name) |
|
21637 | 515 |
in |
21940 | 516 |
set value |
21637 | 517 |
end |
518 |
||
21902 | 519 |
fun getpref (Getpref vs) = |
21940 | 520 |
let |
521 |
val name = #name vs |
|
522 |
val get = #get (lookuppref name) |
|
523 |
in |
|
21637 | 524 |
issue_pgip (Prefval {name=name, value=get ()}) |
525 |
end |
|
526 |
end |
|
527 |
||
528 |
fun proverinit vs = restart () |
|
529 |
||
530 |
fun proverexit vs = isarcmd "quit" |
|
531 |
||
532 |
fun startquiet vs = isarcmd "disable_pr" |
|
533 |
||
534 |
fun stopquiet vs = isarcmd "enable_pr" |
|
535 |
||
21940 | 536 |
fun pgmlsymbolson vs = |
21637 | 537 |
change print_mode (fn mode => |
21940 | 538 |
remove (op =) Symbol.xsymbolsN mode @ [Symbol.xsymbolsN]) |
21637 | 539 |
|
540 |
fun pgmlsymbolsoff vs = |
|
541 |
change print_mode (remove (op =) Symbol.xsymbolsN) |
|
542 |
||
21940 | 543 |
fun dostep (Dostep vs) = |
544 |
let |
|
545 |
val text = #text vs |
|
546 |
in |
|
547 |
isarcmd text |
|
21637 | 548 |
end |
549 |
||
21902 | 550 |
fun undostep (Undostep vs) = |
21940 | 551 |
let |
552 |
val times = #times vs |
|
553 |
in |
|
554 |
isarcmd ("undos_proof " ^ Int.toString times) |
|
21637 | 555 |
end |
556 |
||
557 |
fun redostep vs = isarcmd "redo" |
|
21940 | 558 |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
559 |
fun abortgoal vs = isarcmd "kill" (* was: ProofGeneral.kill_proof *) |
21637 | 560 |
|
561 |
||
21867 | 562 |
(*** PGIP identifier tables ***) |
563 |
||
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
564 |
(* TODO: these ones should be triggered by hooks after a |
22159 | 565 |
declaration addition/removal, to be sent automatically. *) |
21867 | 566 |
|
22159 | 567 |
fun addids t = issue_pgip (Addids {idtables = [t]}) |
568 |
fun delids t = issue_pgip (Delids {idtables = [t]}) |
|
21867 | 569 |
|
21940 | 570 |
fun askids (Askids vs) = |
21637 | 571 |
let |
21940 | 572 |
val url = #url vs (* ask for identifiers within a file *) |
573 |
val thyname = #thyname vs (* ask for identifiers within a theory *) |
|
574 |
val objtype = #objtype vs (* ask for identifiers of a particular type *) |
|
21867 | 575 |
|
21940 | 576 |
fun idtable ty ctx ids = {objtype=ty,context=ctx,ids=ids} |
21867 | 577 |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
578 |
fun setids t = issue_pgip (Setids {idtables = [t]}) |
22159 | 579 |
|
22225 | 580 |
(* fake one-level nested "subtheories" by picking apart names. *) |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
581 |
val thms_of_thy = |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
582 |
map fst o NameSpace.extern_table o PureThy.theorems_of o ThyInfo.get_theory; |
22225 | 583 |
val isnested_id = String.isSubstring NameSpace.separator |
584 |
val immed_thms_of_thy = filter_out isnested_id o thms_of_thy |
|
585 |
fun thy_prefix s = case space_explode NameSpace.separator s of |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
586 |
x::_::_ => SOME x (* String.find? *) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
587 |
| _ => NONE |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
588 |
fun subthys_of_thy s = |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
589 |
foldl (fn (NONE,xs) => xs | (SOME x,xs) => insert op= x xs) [] |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
590 |
(map thy_prefix (thms_of_thy s)) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
591 |
fun subthms_of_thy thy = |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
592 |
(case thy_prefix thy of |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
593 |
NONE => immed_thms_of_thy thy |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
594 |
| SOME prf => filter (String.isPrefix (unprefix (prf ^ NameSpace.separator) thy)) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
595 |
(thms_of_thy prf)) |
22225 | 596 |
val qualified_thms_of_thy = (* for global query with single response *) |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
597 |
map PureThy.get_name_hint o filter PureThy.has_name_hint o |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
598 |
map snd o PureThy.thms_of o ThyInfo.get_theory; |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
599 |
in |
21940 | 600 |
case (thyname,objtype) of |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
601 |
(NONE, NONE) => |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
602 |
setids (idtable ObjFile NONE (ThyInfo.names())) (*FIXME: uris*) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
603 |
| (NONE, SOME ObjFile) => |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
604 |
setids (idtable ObjFile NONE (ThyInfo.names())) (*FIXME: uris*) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
605 |
| (SOME fi, SOME ObjFile) => |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
606 |
setids (idtable ObjTheory (SOME fi) [fi]) (* TODO: check exists *) |
22225 | 607 |
| (NONE, SOME ObjTheory) => |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
608 |
setids (idtable ObjTheory NONE (ThyInfo.names())) |
22225 | 609 |
| (SOME thy, SOME ObjTheory) => |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
610 |
setids (idtable ObjTheory (SOME thy) (subthys_of_thy thy)) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
611 |
| (SOME thy, SOME ObjTheorem) => |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
612 |
setids (idtable ObjTheorem (SOME thy) (subthms_of_thy thy)) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
613 |
| (NONE, SOME ObjTheorem) => |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
614 |
(* A large query, but not unreasonable. ~5000 results for HOL.*) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
615 |
(* Several setids should be allowed, but Eclipse code is currently broken: |
22225 | 616 |
Library.seq (fn thy => setids (idtable ObjTheorem (SOME thy) (subthms_of_thy thy))) |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
617 |
(ThyInfo.names()) *) |
22225 | 618 |
setids (idtable ObjTheorem NONE (* this one gives ~7000 for HOL *) |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
619 |
(maps qualified_thms_of_thy (ThyInfo.names()))) |
22225 | 620 |
| _ => warning ("askids: ignored argument combination") |
21637 | 621 |
end |
622 |
||
22159 | 623 |
fun askrefs (Askrefs vs) = |
624 |
let |
|
625 |
val url = #url vs (* ask for references of a file (i.e. immediate pre-requisites) *) |
|
626 |
val thyname = #thyname vs (* ask for references of a theory (other theories) *) |
|
627 |
val objtype = #objtype vs (* ask for references of a particular type... *) |
|
628 |
val name = #name vs (* ... with this name *) |
|
629 |
||
630 |
fun idtable ty ctx ids = {objtype=ty,context=ctx,ids=ids} |
|
631 |
||
632 |
val thms_of_thy = map fst o PureThy.thms_of o ThyInfo.get_theory |
|
633 |
||
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
634 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base |
22159 | 635 |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
636 |
fun filerefs f = |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
637 |
let val thy = thy_name f |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
638 |
val (_,filerefs) = OuterSyntax.deps_thy thy true f (* (Path.unpack f); *) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
639 |
in |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
640 |
issue_pgip (Setrefs {url=url, thyname=NONE, objtype=SOME PgipTypes.ObjFile, |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
641 |
name=NONE, idtables=[], fileurls=filerefs}) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
642 |
end |
22159 | 643 |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
644 |
fun thyrefs thy = |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
645 |
let val ml_path = ThyLoad.ml_path thy |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
646 |
val (thyrefs,_) = OuterSyntax.deps_thy thy true ml_path (* (Path.unpack f); *) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
647 |
in |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
648 |
issue_pgip (Setrefs {url=url, thyname=thyname, objtype=SOME PgipTypes.ObjTheory, |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
649 |
name=NONE, idtables=[{context=NONE, objtype=PgipTypes.ObjTheory, |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
650 |
ids=thyrefs}], fileurls=[]}) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
651 |
end |
22159 | 652 |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
653 |
fun thmrefs thmname = |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
654 |
let |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
655 |
(* TODO: interim: this is probably not right. |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
656 |
What we want is mapping onto simple PGIP name/context model. *) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
657 |
val ctx = Toplevel.context_of (Toplevel.get_state()) (* NB: raises UNDEF *) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
658 |
val thy = Context.theory_of_proof ctx |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
659 |
val ths = [PureThy.get_thm thy (PureThy.Name thmname)] |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
660 |
val deps = filter_out (equal "") |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
661 |
(Symtab.keys (fold Proofterm.thms_of_proof |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
662 |
(map Thm.proof_of ths) Symtab.empty)) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
663 |
in |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
664 |
if null deps then () |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
665 |
else issue_pgip (Setrefs {url=url, thyname=thyname, name=name, |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
666 |
objtype=SOME PgipTypes.ObjTheorem, |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
667 |
idtables=[{context=NONE, objtype=PgipTypes.ObjTheorem, |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
668 |
ids=deps}], fileurls=[]}) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
669 |
end |
22159 | 670 |
in |
671 |
case (url,thyname,objtype,name) of |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
672 |
(SOME file, NONE, _, _) => filerefs file |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
673 |
| (_,SOME thy,_,_) => thyrefs thy |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
674 |
| (_,_,SOME PgipTypes.ObjTheorem,SOME thmname) => thmrefs thmname |
22159 | 675 |
| _ => error ("Unimplemented/invalid case of <askrefs>") |
676 |
end |
|
677 |
||
678 |
||
679 |
||
21637 | 680 |
local |
681 |
(* accumulate printed output in a single PGIP response (inside <pgmltext>) *) |
|
682 |
fun with_displaywrap pgipfn dispfn = |
|
683 |
let |
|
21940 | 684 |
val lines = ref ([]: string list); |
685 |
fun wlgrablines s = lines := s :: ! lines; |
|
21637 | 686 |
in |
21940 | 687 |
setmp writeln_fn wlgrablines dispfn (); |
688 |
issue_pgip (pgipfn (!lines)) |
|
21637 | 689 |
end; |
690 |
in |
|
21940 | 691 |
fun showid (Showid vs) = |
21637 | 692 |
let |
21940 | 693 |
val thyname = #thyname vs |
694 |
val objtype = #objtype vs |
|
695 |
val name = #name vs |
|
696 |
val topthy = Toplevel.theory_of o Toplevel.get_state |
|
21637 | 697 |
|
21940 | 698 |
fun idvalue objtype name strings = |
699 |
Idvalue { name=name, objtype=objtype, |
|
700 |
text=[XML.Elem("pgmltext",[],map XML.Text strings)] } |
|
21637 | 701 |
|
21940 | 702 |
fun pthm thy name = print_thm (get_thm thy (Name name)) |
703 |
in |
|
704 |
case (thyname, objtype) of |
|
705 |
(_,ObjTheory) => |
|
706 |
with_displaywrap (idvalue ObjTheory name) |
|
707 |
(fn ()=>(print_theory (ThyInfo.get_theory name))) |
|
708 |
| (SOME thy, ObjTheorem) => |
|
709 |
with_displaywrap (idvalue ObjTheorem name) |
|
710 |
(fn ()=>(pthm (ThyInfo.get_theory thy) name)) |
|
711 |
| (NONE, ObjTheorem) => |
|
712 |
with_displaywrap (idvalue ObjTheorem name) |
|
713 |
(fn ()=>pthm (topthy()) name) |
|
714 |
| (_, ot) => error ("Cannot show objects of type "^(PgipTypes.name_of_objtype ot)) |
|
21637 | 715 |
end |
716 |
||
21867 | 717 |
(*** Inspecting state ***) |
718 |
||
21940 | 719 |
(* The file which is currently being processed interactively. |
21637 | 720 |
In the pre-PGIP code, this was informed to Isabelle and the theory loader |
721 |
on completion, but that allows for circularity in case we read |
|
722 |
ourselves. So PGIP opens the filename at the start of a script. |
|
723 |
We ought to prevent problems by modifying the theory loader to know |
|
21940 | 724 |
about this special status, but for now we just keep a local reference. |
725 |
*) |
|
21637 | 726 |
|
727 |
val currently_open_file = ref (NONE : pgipurl option) |
|
728 |
||
22163 | 729 |
fun get_currently_open_file () = ! currently_open_file; |
730 |
||
21940 | 731 |
fun askguise vs = |
21637 | 732 |
(* The "guise" is the PGIP abstraction of the prover's state. |
733 |
The <informguise> message is merely used for consistency checking. *) |
|
21940 | 734 |
let |
735 |
val openfile = !currently_open_file |
|
21637 | 736 |
|
21940 | 737 |
val topthy = Toplevel.theory_of o Toplevel.get_state |
738 |
val topthy_name = Context.theory_name o topthy |
|
21637 | 739 |
|
21940 | 740 |
val opentheory = SOME (topthy_name()) handle Toplevel.UNDEF => NONE |
21637 | 741 |
|
21940 | 742 |
fun topproofpos () = try Toplevel.proof_position_of (Isar.state ()); |
743 |
val openproofpos = topproofpos() |
|
21637 | 744 |
in |
745 |
issue_pgip (Informguise { file = openfile, |
|
21940 | 746 |
theory = opentheory, |
747 |
(* would be nice to get thm name... *) |
|
748 |
theorem = NONE, |
|
749 |
proofpos = openproofpos }) |
|
21637 | 750 |
end |
751 |
||
21902 | 752 |
fun parsescript (Parsescript vs) = |
21637 | 753 |
let |
21940 | 754 |
val text = #text vs |
755 |
val systemdata = #systemdata vs |
|
756 |
val location = #location vs (* TODO: extract position *) |
|
21637 | 757 |
|
21867 | 758 |
val _ = start_delay_msgs () (* gather parsing errs/warns *) |
759 |
val doc = PgipParser.pgip_parser text |
|
21637 | 760 |
val errs = end_delayed_msgs () |
761 |
||
21940 | 762 |
val sysattrs = PgipTypes.opt_attr "systemdata" systemdata |
763 |
val locattrs = PgipTypes.attrs_of_location location |
|
21637 | 764 |
in |
765 |
issue_pgip (Parseresult { attrs= sysattrs@locattrs, |
|
21940 | 766 |
doc = doc, |
767 |
errs = map PgipOutput.output errs }) |
|
21637 | 768 |
end |
769 |
||
770 |
fun showproofstate vs = isarcmd "pr" |
|
771 |
||
21969 | 772 |
fun showctxt vs = isarcmd "print_context" |
21637 | 773 |
|
21902 | 774 |
fun searchtheorems (Searchtheorems vs) = |
21940 | 775 |
let |
776 |
val arg = #arg vs |
|
21637 | 777 |
in |
21969 | 778 |
isarcmd ("find_theorems " ^ arg) |
21637 | 779 |
end |
780 |
||
21940 | 781 |
fun setlinewidth (Setlinewidth vs) = |
782 |
let |
|
783 |
val width = #width vs |
|
21637 | 784 |
in |
21940 | 785 |
isarcmd ("pretty_setmargin " ^ Int.toString width) (* FIXME: conversion back/forth! *) |
21637 | 786 |
end |
787 |
||
21902 | 788 |
fun viewdoc (Viewdoc vs) = |
21940 | 789 |
let |
790 |
val arg = #arg vs |
|
791 |
in |
|
792 |
isarcmd ("print_" ^ arg) (* FIXME: isatool doc?. Return URLs, maybe? *) |
|
21637 | 793 |
end |
794 |
||
21867 | 795 |
(*** Theory ***) |
796 |
||
21902 | 797 |
fun doitem (Doitem vs) = |
21637 | 798 |
let |
21940 | 799 |
val text = #text vs |
21637 | 800 |
in |
21940 | 801 |
isarcmd text |
21637 | 802 |
end |
803 |
||
804 |
fun undoitem vs = |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
805 |
isarcmd "undo" |
21637 | 806 |
|
807 |
fun redoitem vs = |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
808 |
isarcmd "redo" |
21637 | 809 |
|
21940 | 810 |
fun aborttheory vs = |
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
811 |
isarcmd "kill" (* was: "init_toplevel" *) |
21637 | 812 |
|
21902 | 813 |
fun retracttheory (Retracttheory vs) = |
21940 | 814 |
let |
815 |
val thyname = #thyname vs |
|
21637 | 816 |
in |
21940 | 817 |
isarcmd ("kill_thy " ^ quote thyname) |
21637 | 818 |
end |
819 |
||
21867 | 820 |
|
821 |
(*** Files ***) |
|
822 |
||
823 |
(* Path management: we allow theory files to have dependencies in |
|
824 |
their own directory, but when we change directory for a new file we |
|
825 |
remove the path. Leaving it there can cause confusion with |
|
826 |
difference in batch mode. |
|
21940 | 827 |
NB: PGIP does not assume that the prover has a load path. |
21867 | 828 |
*) |
829 |
||
830 |
local |
|
831 |
val current_working_dir = ref (NONE : string option) |
|
832 |
in |
|
21940 | 833 |
fun changecwd_dir newdirpath = |
834 |
let |
|
21867 | 835 |
val newdir = File.platform_path newdirpath |
21940 | 836 |
in |
21867 | 837 |
(case (!current_working_dir) of |
838 |
NONE => () |
|
839 |
| SOME dir => ThyLoad.del_path dir; |
|
840 |
ThyLoad.add_path newdir; |
|
841 |
current_working_dir := SOME newdir) |
|
842 |
end |
|
843 |
end |
|
844 |
||
21940 | 845 |
fun changecwd (Changecwd vs) = |
846 |
let |
|
847 |
val url = #url vs |
|
848 |
val newdir = PgipTypes.path_of_pgipurl url |
|
21867 | 849 |
in |
21940 | 850 |
changecwd_dir url |
21867 | 851 |
end |
852 |
||
21902 | 853 |
fun openfile (Openfile vs) = |
21940 | 854 |
let |
21867 | 855 |
val url = #url vs |
856 |
val filepath = PgipTypes.path_of_pgipurl url |
|
857 |
val filedir = Path.dir filepath |
|
858 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base |
|
859 |
val openfile_retract = Output.no_warnings (ThyInfo.if_known_thy ThyInfo.remove_thy) o thy_name; |
|
860 |
in |
|
861 |
case !currently_open_file of |
|
22028 | 862 |
SOME f => raise PGIP ("<openfile> when a file is already open!\nCurrently open file: " ^ |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
863 |
PgipTypes.string_of_pgipurl url) |
21867 | 864 |
| NONE => (openfile_retract filepath; |
21940 | 865 |
changecwd_dir filedir; |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
866 |
priority ("Working in file: " ^ PgipTypes.string_of_pgipurl url); |
21940 | 867 |
currently_open_file := SOME url) |
21867 | 868 |
end |
869 |
||
870 |
fun closefile vs = |
|
871 |
case !currently_open_file of |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
872 |
SOME f => (proper_inform_file_processed f (Isar.state()); |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
873 |
priority ("Finished working in file: " ^ PgipTypes.string_of_pgipurl f); |
21867 | 874 |
currently_open_file := NONE) |
875 |
| NONE => raise PGIP ("<closefile> when no file is open!") |
|
876 |
||
21940 | 877 |
fun loadfile (Loadfile vs) = |
878 |
let |
|
879 |
val url = #url vs |
|
880 |
in |
|
22171
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
881 |
(* da: this doesn't seem to cause a problem, batch loading uses |
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
882 |
a different state context. Of course confusion is still possible, |
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
883 |
e.g. file loaded depends on open file which is not yet saved. *) |
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
884 |
(* case !currently_open_file of |
22028 | 885 |
SOME f => raise PGIP ("<loadfile> when a file is open!\nCurrently open file: " ^ |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
886 |
PgipTypes.string_of_pgipurl url) |
22171
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
887 |
| NONE => *) |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
888 |
use_thy_or_ml_file (File.platform_path url) |
21637 | 889 |
end |
890 |
||
891 |
fun abortfile vs = |
|
892 |
case !currently_open_file of |
|
893 |
SOME f => (isarcmd "init_toplevel"; |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
894 |
priority ("Aborted working in file: " ^ |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
895 |
PgipTypes.string_of_pgipurl f); |
21940 | 896 |
currently_open_file := NONE) |
21637 | 897 |
| NONE => raise PGIP ("<abortfile> when no file is open!") |
898 |
||
21940 | 899 |
fun retractfile (Retractfile vs) = |
900 |
let |
|
901 |
val url = #url vs |
|
21637 | 902 |
in |
21940 | 903 |
case !currently_open_file of |
21637 | 904 |
SOME f => raise PGIP ("<retractfile> when a file is open!") |
22028 | 905 |
| NONE => (priority ("Retracting file: " ^ PgipTypes.string_of_pgipurl url); |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
906 |
(* TODO: next should be in thy loader, here just for testing *) |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
907 |
let |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
908 |
val name = thy_name url |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
909 |
in List.app (tell_file_retracted false) (ThyInfo.loaded_files name) end; |
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
910 |
inform_file_retracted url) |
21637 | 911 |
end |
912 |
||
913 |
||
21867 | 914 |
(*** System ***) |
21637 | 915 |
|
21902 | 916 |
fun systemcmd (Systemcmd vs) = |
21940 | 917 |
let |
21637 | 918 |
val arg = #arg vs |
919 |
in |
|
920 |
isarcmd arg |
|
921 |
end |
|
922 |
||
923 |
exception PGIP_QUIT; |
|
924 |
fun quitpgip vs = raise PGIP_QUIT |
|
925 |
||
21902 | 926 |
fun process_input inp = case inp |
927 |
of Pgip.Askpgip _ => askpgip inp |
|
928 |
| Pgip.Askpgml _ => askpgml inp |
|
21940 | 929 |
| Pgip.Askprefs _ => askprefs inp |
21902 | 930 |
| Pgip.Askconfig _ => askconfig inp |
931 |
| Pgip.Getpref _ => getpref inp |
|
932 |
| Pgip.Setpref _ => setpref inp |
|
933 |
| Pgip.Proverinit _ => proverinit inp |
|
934 |
| Pgip.Proverexit _ => proverexit inp |
|
935 |
| Pgip.Startquiet _ => startquiet inp |
|
936 |
| Pgip.Stopquiet _ => stopquiet inp |
|
937 |
| Pgip.Pgmlsymbolson _ => pgmlsymbolson inp |
|
938 |
| Pgip.Pgmlsymbolsoff _ => pgmlsymbolsoff inp |
|
939 |
| Pgip.Dostep _ => dostep inp |
|
940 |
| Pgip.Undostep _ => undostep inp |
|
941 |
| Pgip.Redostep _ => redostep inp |
|
942 |
| Pgip.Forget _ => error "<forget> not implemented" |
|
943 |
| Pgip.Restoregoal _ => error "<restoregoal> not implemented" |
|
944 |
| Pgip.Abortgoal _ => abortgoal inp |
|
945 |
| Pgip.Askids _ => askids inp |
|
22159 | 946 |
| Pgip.Askrefs _ => askrefs inp |
21902 | 947 |
| Pgip.Showid _ => showid inp |
948 |
| Pgip.Askguise _ => askguise inp |
|
949 |
| Pgip.Parsescript _ => parsescript inp |
|
950 |
| Pgip.Showproofstate _ => showproofstate inp |
|
951 |
| Pgip.Showctxt _ => showctxt inp |
|
952 |
| Pgip.Searchtheorems _ => searchtheorems inp |
|
953 |
| Pgip.Setlinewidth _ => setlinewidth inp |
|
954 |
| Pgip.Viewdoc _ => viewdoc inp |
|
955 |
| Pgip.Doitem _ => doitem inp |
|
956 |
| Pgip.Undoitem _ => undoitem inp |
|
957 |
| Pgip.Redoitem _ => redoitem inp |
|
958 |
| Pgip.Aborttheory _ => aborttheory inp |
|
959 |
| Pgip.Retracttheory _ => retracttheory inp |
|
960 |
| Pgip.Loadfile _ => loadfile inp |
|
961 |
| Pgip.Openfile _ => openfile inp |
|
962 |
| Pgip.Closefile _ => closefile inp |
|
963 |
| Pgip.Abortfile _ => abortfile inp |
|
964 |
| Pgip.Retractfile _ => retractfile inp |
|
965 |
| Pgip.Changecwd _ => changecwd inp |
|
966 |
| Pgip.Systemcmd _ => systemcmd inp |
|
967 |
| Pgip.Quitpgip _ => quitpgip inp |
|
21637 | 968 |
|
969 |
||
21940 | 970 |
fun process_pgip_element pgipxml = |
21637 | 971 |
case pgipxml of |
21969 | 972 |
xml as (XML.Elem elem) => |
21940 | 973 |
(case Pgip.input elem of |
974 |
NONE => warning ("Unrecognized PGIP command, ignored: \n" ^ |
|
975 |
(XML.string_of_tree xml)) |
|
976 |
| SOME inp => (process_input inp)) (* errors later; packet discarded *) |
|
21969 | 977 |
| XML.Text t => ignored_text_warning t |
978 |
| XML.Rawtext t => ignored_text_warning t |
|
21637 | 979 |
and ignored_text_warning t = |
21940 | 980 |
if size (Symbol.strip_blanks t) > 0 then |
981 |
warning ("Ignored text in PGIP packet: \n" ^ t) |
|
21637 | 982 |
else () |
983 |
||
984 |
fun process_pgip_tree xml = |
|
985 |
(pgip_refid := NONE; |
|
986 |
pgip_refseq := NONE; |
|
987 |
(case xml of |
|
988 |
XML.Elem ("pgip", attrs, pgips) => |
|
989 |
(let |
|
990 |
val class = PgipTypes.get_attr "class" attrs |
|
991 |
val dest = PgipTypes.get_attr_opt "destid" attrs |
|
21940 | 992 |
val seq = PgipTypes.read_pgipnat (PgipTypes.get_attr "seq" attrs) |
21637 | 993 |
(* Respond to prover broadcasts, or messages for us. Ignore rest *) |
21940 | 994 |
val processit = |
995 |
case dest of |
|
21637 | 996 |
NONE => class = "pa" |
21940 | 997 |
| SOME id => matching_pgip_id id |
998 |
in if processit then |
|
999 |
(pgip_refid := PgipTypes.get_attr_opt "id" attrs; |
|
1000 |
pgip_refseq := SOME seq; |
|
1001 |
List.app process_pgip_element pgips; |
|
1002 |
(* return true to indicate <ready/> *) |
|
1003 |
true) |
|
1004 |
else |
|
1005 |
(* no response to ignored messages. *) |
|
1006 |
false |
|
21637 | 1007 |
end) |
1008 |
| _ => raise PGIP "Invalid PGIP packet received") |
|
1009 |
handle PGIP msg => |
|
1010 |
(Output.error_msg ((msg ^ "\nPGIP error occured in XML text below:\n") ^ |
|
21940 | 1011 |
(XML.string_of_tree xml)); |
1012 |
true)) |
|
21637 | 1013 |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1014 |
(* External input *) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1015 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1016 |
val process_pgip_plain = K () o process_pgip_tree o XML.tree_of_string |
21637 | 1017 |
|
1018 |
end |
|
1019 |
||
1020 |
||
1021 |
(* PGIP loop: process PGIP input only *) |
|
1022 |
||
1023 |
local |
|
1024 |
||
1025 |
exception XML_PARSE |
|
1026 |
||
1027 |
fun loop ready src = |
|
1028 |
let |
|
1029 |
val _ = if ready then issue_pgip (Ready ()) else () |
|
21969 | 1030 |
val pgipo = |
1031 |
(case try Source.get_single src of |
|
1032 |
SOME pgipo => pgipo |
|
1033 |
| NONE => raise XML_PARSE) |
|
21637 | 1034 |
in |
1035 |
case pgipo of |
|
1036 |
NONE => () |
|
1037 |
| SOME (pgip,src') => |
|
1038 |
let |
|
21940 | 1039 |
val ready' = (process_pgip_tree pgip) |
1040 |
handle e => (handler (e,SOME src'); true) |
|
21637 | 1041 |
in |
1042 |
loop ready' src' |
|
1043 |
end |
|
1044 |
end handle e => handler (e,SOME src) (* error in XML parse or Ready issue *) |
|
1045 |
||
1046 |
and handler (e,srco) = |
|
1047 |
case (e,srco) of |
|
1048 |
(XML_PARSE,SOME src) => |
|
1049 |
Output.panic "Invalid XML input, aborting" |
|
1050 |
| (PGIP_QUIT,_) => () |
|
21940 | 1051 |
| (Interrupt,SOME src) => |
1052 |
(Output.error_msg "Interrupt during PGIP processing"; loop true src) |
|
1053 |
| (Toplevel.UNDEF,SOME src) => |
|
1054 |
(Output.error_msg "No working context defined"; loop true src) |
|
1055 |
| (e,SOME src) => |
|
1056 |
(Output.error_msg (Toplevel.exn_message e); loop true src) |
|
21637 | 1057 |
| (_,NONE) => () |
1058 |
in |
|
1059 |
(* TODO: add socket interface *) |
|
1060 |
||
1061 |
val xmlP = XML.scan_comment_whspc |-- XML.parse_elem >> single |
|
1062 |
||
1063 |
val tty_src = Source.set_prompt "" (Source.source Symbol.stopper xmlP NONE Source.tty) |
|
1064 |
||
1065 |
fun pgip_toplevel x = loop true x |
|
1066 |
end |
|
1067 |
||
1068 |
||
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1069 |
local |
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1070 |
(* Extra command for embedding prover-control inside document (obscure/debug usage). *) |
21637 | 1071 |
|
22014
4b70cbd96007
removed Toplevel.print_exn hook -- existing error_msg_fn does the job;
wenzelm
parents:
22001
diff
changeset
|
1072 |
val process_pgipP = |
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1073 |
OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" OuterKeyword.control |
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1074 |
(OuterParse.text >> (Toplevel.no_timing oo |
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1075 |
(fn txt => Toplevel.imperative (fn () => process_pgip_plain txt)))); |
21637 | 1076 |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1077 |
in |
21637 | 1078 |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1079 |
fun init_outer_syntax () = OuterSyntax.add_parsers [process_pgipP]; |
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1080 |
|
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1081 |
end |
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1082 |
|
21637 | 1083 |
|
1084 |
||
1085 |
(* init *) |
|
1086 |
||
1087 |
val initialized = ref false; |
|
1088 |
||
21969 | 1089 |
fun init_pgip false = |
1090 |
Output.panic "No Proof General interface support for Isabelle/classic mode." |
|
1091 |
| init_pgip true = |
|
1092 |
(! initialized orelse |
|
1093 |
(setmp warning_fn (K ()) init_outer_syntax (); |
|
22014
4b70cbd96007
removed Toplevel.print_exn hook -- existing error_msg_fn does the job;
wenzelm
parents:
22001
diff
changeset
|
1094 |
PgipParser.init (); |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
1095 |
setup_preferences_tweak (); |
21969 | 1096 |
setup_xsymbols_output (); |
1097 |
setup_pgml_output (); |
|
1098 |
setup_messages (); |
|
1099 |
setup_state (); |
|
1100 |
setup_thy_loader (); |
|
1101 |
setup_present_hook (); |
|
1102 |
init_pgip_session_id (); |
|
1103 |
welcome (); |
|
1104 |
set initialized); |
|
1105 |
sync_thy_loader (); |
|
1106 |
change print_mode (cons proof_generalN o remove (op =) proof_generalN); |
|
1107 |
change print_mode (append [pgmlN, pgmlatomsN] o subtract (op =) [pgmlN, pgmlatomsN]); |
|
1108 |
pgip_toplevel tty_src); |
|
21637 | 1109 |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1110 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1111 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1112 |
(** Out-of-loop PGIP commands (for Emacs hybrid mode) **) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1113 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1114 |
local |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1115 |
val pgip_output_channel = ref writeln_default |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1116 |
in |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1117 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1118 |
(* Set recipient for PGIP results *) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1119 |
fun init_pgip_channel writefn = |
21940 | 1120 |
(init_pgip_session_id(); |
1121 |
pgip_output_channel := writefn) |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1122 |
|
21940 | 1123 |
(* Process a PGIP command. |
1124 |
This works for preferences but not generally guaranteed |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1125 |
because we haven't done full setup here (e.g., no pgml mode) *) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1126 |
fun process_pgip str = |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1127 |
setmp output_xml_fn (!pgip_output_channel) process_pgip_plain str |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1128 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1129 |
end |
21637 | 1130 |
|
1131 |
end; |