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