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