author | aspinall |
Wed, 24 Jan 2007 13:15:16 +0100 | |
changeset 22171 | 58f554f51f0d |
parent 22163 | a586b0af857e |
child 22216 | c3f5aa951a68 |
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 *) |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
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 |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
221 |
add new lines explicitly in PGIP: they are left implicit. It means that PGIP messages |
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 |
|
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
243 |
fun tell_file_loaded completed path = |
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, |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
245 |
completed=completed}) |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
246 |
fun tell_file_outdated completed path = |
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, |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
248 |
completed=completed}) |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
249 |
fun tell_file_retracted completed path = |
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, |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
251 |
completed=completed}) |
21637 | 252 |
|
253 |
||
254 |
||
255 |
(** theory / proof state output **) |
|
256 |
||
257 |
local |
|
258 |
||
259 |
fun statedisplay prts = |
|
22028 | 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 = |
|
22028 | 269 |
case (Display.pretty_current_goals n m st) of |
270 |
[] => tell_clear_goals() |
|
271 |
| prts => statedisplay prts |
|
21637 | 272 |
|
273 |
fun print_state b st = |
|
22028 | 274 |
case (Toplevel.pretty_state b st) of |
275 |
[] => tell_clear_goals() |
|
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 |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
290 |
(* da: TODO: PGIP has a completed flag so the prover can indicate to the |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
291 |
interface which files are busy performing a particular action. |
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 |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
293 |
(may actually be difficult to tell the interface *which* action is in |
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). |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
297 |
*) |
21637 | 298 |
fun trace_action action name = |
299 |
if action = ThyInfo.Update then |
|
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
300 |
List.app (tell_file_loaded 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
|
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 |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
304 |
List.app (tell_file_retracted 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
|
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 |
||
358 |
(* FIXME: check this uses non-transitive closure function here *) |
|
21969 | 359 |
fun tell_thm_deps ths = |
360 |
if Output.has_mode thm_depsN then |
|
361 |
let |
|
22159 | 362 |
val names = filter_out (equal "") (map PureThy.get_name_hint ths); (* da: HAS THIS NOW BECOME "??.unknown"? *) |
21969 | 363 |
val deps = filter_out (equal "") |
364 |
(Symtab.keys (fold Proofterm.thms_of_proof |
|
365 |
(map Thm.proof_of ths) Symtab.empty)); |
|
366 |
in |
|
367 |
if null names orelse null deps then () |
|
368 |
else thm_deps_message (spaces_quote names, spaces_quote deps) |
|
369 |
end |
|
370 |
else (); |
|
21637 | 371 |
|
372 |
in |
|
373 |
||
374 |
fun setup_present_hook () = |
|
375 |
Present.add_hook (fn _ => fn res => tell_thm_deps (maps #2 res)); |
|
376 |
||
377 |
end; |
|
378 |
||
379 |
(** lexicalstructure element with keywords (PGIP version of elisp keywords file) **) |
|
380 |
||
21940 | 381 |
fun lexicalstructure_keywords () = |
21637 | 382 |
let val commands = OuterSyntax.dest_keywords () |
21940 | 383 |
fun category_of k = if k mem commands then "major" else "minor" |
21637 | 384 |
(* NB: we might filter to only include words like elisp case (OuterSyntax.is_keyword). *) |
21940 | 385 |
fun keyword_elt (keyword,help,kind,_) = |
386 |
XML.Elem("keyword", [("word", keyword), ("category", category_of kind)], |
|
387 |
[XML.Elem("shorthelp", [], [XML.Text help])]) |
|
388 |
in |
|
21637 | 389 |
(* Also, note we don't call init_outer_syntax here to add interface commands, |
390 |
but they should never appear in scripts anyway so it shouldn't matter *) |
|
391 |
Lexicalstructure {content = map keyword_elt (OuterSyntax.dest_parsers()) } |
|
392 |
end |
|
393 |
||
394 |
(* TODO: we can issue a lexicalstructure/keyword when the syntax gets extended dynamically; |
|
395 |
hooks needed in outer_syntax.ML to do that. *) |
|
396 |
||
397 |
||
398 |
(* Configuration: GUI config, proverinfo messages *) |
|
399 |
||
400 |
local |
|
401 |
val isabellewww = "http://isabelle.in.tum.de/" |
|
402 |
val staticconfig = "~~/lib/ProofGeneral/pgip_isar.xml" |
|
21940 | 403 |
fun orenv v d = case getenv v of "" => d | s => s |
21637 | 404 |
fun config_file() = orenv "ISABELLE_PGIPCONFIG" staticconfig |
405 |
fun isabelle_www() = orenv "ISABELLE_HOMEPAGE" isabellewww |
|
406 |
in |
|
407 |
fun send_pgip_config () = |
|
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 = Path.explode (config_file()) |
21940 | 410 |
val ex = File.exists path |
21637 | 411 |
|
21940 | 412 |
val wwwpage = |
413 |
(Url.explode (isabelle_www())) |
|
21969 | 414 |
handle ERROR _ => |
21940 | 415 |
(Output.panic |
416 |
("Error in URL in environment variable ISABELLE_HOMEPAGE."); |
|
417 |
Url.explode isabellewww) |
|
418 |
||
419 |
val proverinfo = |
|
21637 | 420 |
Proverinfo { name = "Isabelle", |
21940 | 421 |
version = version, |
422 |
instance = Session.name(), |
|
423 |
descr = "The Isabelle/Isar theorem prover", |
|
424 |
url = wwwpage, |
|
425 |
filenameextns = ".thy;" } |
|
21637 | 426 |
in |
21940 | 427 |
if ex then |
428 |
(issue_pgip proverinfo; |
|
429 |
issue_pgip_rawtext (File.read path); |
|
430 |
issue_pgip (lexicalstructure_keywords())) |
|
21637 | 431 |
else Output.panic ("PGIP configuration file \"" ^ config_file() ^ "\" not found") |
432 |
end; |
|
433 |
end |
|
434 |
||
435 |
||
436 |
||
437 |
||
438 |
(* Sending commands to Isar *) |
|
439 |
||
440 |
fun isarcmd s = |
|
441 |
s |> OuterSyntax.scan |> OuterSyntax.read |
|
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
442 |
(*|> map (Toplevel.position (Position.name "PGIP message") o #3)*) |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
443 |
|> map #3 |
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
444 |
|> Toplevel.>>>; |
21637 | 445 |
|
21940 | 446 |
(* TODO: |
21885
5a11263bd8cf
Remove obsolete prefixes from error and warning messages.
aspinall
parents:
21867
diff
changeset
|
447 |
- 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
|
448 |
- 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
|
449 |
*) |
21637 | 450 |
|
451 |
(* load an arbitrary file (must be .thy or .ML) *) |
|
452 |
||
453 |
fun use_thy_or_ml_file file = |
|
454 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21655
diff
changeset
|
455 |
val (path,extn) = Path.split_ext (Path.explode file) |
21637 | 456 |
in |
457 |
case extn of |
|
21940 | 458 |
"" => isarcmd ("use_thy " ^ quote (Path.implode path)) |
459 |
| "thy" => isarcmd ("use_thy " ^ quote (Path.implode path)) |
|
21637 | 460 |
| "ML" => isarcmd ("use " ^ quote file) |
22028 | 461 |
| other => error ("Don't know how to read a file with extension " ^ quote other) |
21637 | 462 |
end |
463 |
||
464 |
||
21867 | 465 |
(******* PGIP actions *******) |
21637 | 466 |
|
467 |
||
21940 | 468 |
(* Responses to each of the PGIP input commands. |
21637 | 469 |
These are programmed uniformly for extensibility. *) |
470 |
||
21940 | 471 |
fun askpgip (Askpgip vs) = |
21637 | 472 |
issue_pgip |
473 |
(Usespgip { version = PgipIsabelle.isabelle_pgip_version_supported, |
|
21940 | 474 |
pgipelems = PgipIsabelle.accepted_inputs }) |
21637 | 475 |
|
21940 | 476 |
fun askpgml (Askpgml vs) = |
21637 | 477 |
issue_pgip |
21940 | 478 |
(Usespgml { version = PgipIsabelle.isabelle_pgml_version_supported }) |
21637 | 479 |
|
21902 | 480 |
fun askprefs (Askprefs vs) = |
21940 | 481 |
let |
482 |
fun preference_of {name, descr, default, pgiptype, get, set } = |
|
483 |
{ name = name, descr = SOME descr, default = SOME default, |
|
484 |
pgiptype = pgiptype } |
|
21637 | 485 |
in |
21940 | 486 |
List.app (fn (prefcat, prefs) => |
487 |
issue_pgip (Hasprefs {prefcategory=SOME prefcat, |
|
488 |
prefs=map preference_of prefs})) |
|
21637 | 489 |
Preferences.preferences |
21940 | 490 |
end |
21637 | 491 |
|
21902 | 492 |
fun askconfig (Askconfig vs) = () (* TODO: add config response *) |
21637 | 493 |
|
494 |
local |
|
21940 | 495 |
fun lookuppref pref = |
496 |
case AList.lookup (op =) |
|
497 |
(map (fn p => (#name p,p)) |
|
498 |
(maps snd Preferences.preferences)) pref of |
|
499 |
NONE => error ("Unknown prover preference: " ^ quote pref) |
|
500 |
| SOME p => p |
|
21637 | 501 |
in |
21940 | 502 |
fun setpref (Setpref vs) = |
503 |
let |
|
504 |
val name = #name vs |
|
505 |
val value = #value vs |
|
506 |
val set = #set (lookuppref name) |
|
21637 | 507 |
in |
21940 | 508 |
set value |
21637 | 509 |
end |
510 |
||
21902 | 511 |
fun getpref (Getpref vs) = |
21940 | 512 |
let |
513 |
val name = #name vs |
|
514 |
val get = #get (lookuppref name) |
|
515 |
in |
|
21637 | 516 |
issue_pgip (Prefval {name=name, value=get ()}) |
517 |
end |
|
518 |
end |
|
519 |
||
520 |
fun proverinit vs = restart () |
|
521 |
||
522 |
fun proverexit vs = isarcmd "quit" |
|
523 |
||
524 |
fun startquiet vs = isarcmd "disable_pr" |
|
525 |
||
526 |
fun stopquiet vs = isarcmd "enable_pr" |
|
527 |
||
21940 | 528 |
fun pgmlsymbolson vs = |
21637 | 529 |
change print_mode (fn mode => |
21940 | 530 |
remove (op =) Symbol.xsymbolsN mode @ [Symbol.xsymbolsN]) |
21637 | 531 |
|
532 |
fun pgmlsymbolsoff vs = |
|
533 |
change print_mode (remove (op =) Symbol.xsymbolsN) |
|
534 |
||
21940 | 535 |
fun dostep (Dostep vs) = |
536 |
let |
|
537 |
val text = #text vs |
|
538 |
in |
|
539 |
isarcmd text |
|
21637 | 540 |
end |
541 |
||
21902 | 542 |
fun undostep (Undostep vs) = |
21940 | 543 |
let |
544 |
val times = #times vs |
|
545 |
in |
|
546 |
isarcmd ("undos_proof " ^ Int.toString times) |
|
21637 | 547 |
end |
548 |
||
549 |
fun redostep vs = isarcmd "redo" |
|
21940 | 550 |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
551 |
fun abortgoal vs = isarcmd "kill" (* was: ProofGeneral.kill_proof *) |
21637 | 552 |
|
553 |
||
21867 | 554 |
(*** PGIP identifier tables ***) |
555 |
||
22159 | 556 |
(* TODO: these ones should be triggered by hooks after a |
557 |
declaration addition/removal, to be sent automatically. *) |
|
21867 | 558 |
|
22159 | 559 |
fun addids t = issue_pgip (Addids {idtables = [t]}) |
560 |
fun delids t = issue_pgip (Delids {idtables = [t]}) |
|
21867 | 561 |
|
21940 | 562 |
fun askids (Askids vs) = |
21637 | 563 |
let |
21940 | 564 |
val url = #url vs (* ask for identifiers within a file *) |
565 |
val thyname = #thyname vs (* ask for identifiers within a theory *) |
|
566 |
val objtype = #objtype vs (* ask for identifiers of a particular type *) |
|
21867 | 567 |
|
21940 | 568 |
fun idtable ty ctx ids = {objtype=ty,context=ctx,ids=ids} |
21867 | 569 |
|
22159 | 570 |
fun setids t = issue_pgip (Setids {idtables = [t]}) |
571 |
||
21940 | 572 |
val thms_of_thy = map fst o PureThy.thms_of o ThyInfo.get_theory |
573 |
in |
|
574 |
(* case (url_attr,thyname,objtype) of |
|
575 |
(NONE,NONE,NONE) => |
|
576 |
*) (* top-level: return *) |
|
21867 | 577 |
|
21940 | 578 |
(* TODO: add askids for "file" here, which returns single theory with same name *) |
21867 | 579 |
(* FIXME: objtypes on both sides *) |
21940 | 580 |
case (thyname,objtype) of |
21867 | 581 |
(* only files known in top context *) |
21940 | 582 |
(NONE, NONE) => setids (idtable ObjFile NONE (ThyInfo.names())) (*FIXME: uris?*) |
583 |
| (NONE, SOME ObjFile) => setids (idtable ObjFile NONE (ThyInfo.names())) (* ditto *) |
|
584 |
| (SOME fi, SOME ObjFile) => setids (idtable ObjTheory (SOME fi) [fi]) (* FIXME: lookup file *) |
|
585 |
| (NONE, SOME ObjTheory) => setids (idtable ObjTheory NONE (ThyInfo.names())) |
|
586 |
| (SOME thy, SOME ObjTheory) => setids (idtable ObjTheory (SOME thy) (ThyInfo.get_preds thy)) |
|
587 |
| (SOME thy, SOME ObjTheorem) => setids (idtable ObjTheorem (SOME thy) (thms_of_thy thy)) |
|
588 |
(* next case is both of above. FIXME: cleanup this *) |
|
589 |
| (SOME thy, NONE) => (setids (idtable ObjTheory (SOME thy) (ThyInfo.get_preds thy)); |
|
590 |
setids (idtable ObjTheorem (SOME thy) (thms_of_thy thy))) |
|
591 |
| (_, SOME ot) => error ("No objects of type "^(PgipTypes.name_of_objtype ot)^" are available here.") |
|
21637 | 592 |
end |
593 |
||
22159 | 594 |
fun askrefs (Askrefs vs) = |
595 |
let |
|
596 |
val url = #url vs (* ask for references of a file (i.e. immediate pre-requisites) *) |
|
597 |
val thyname = #thyname vs (* ask for references of a theory (other theories) *) |
|
598 |
val objtype = #objtype vs (* ask for references of a particular type... *) |
|
599 |
val name = #name vs (* ... with this name *) |
|
600 |
||
601 |
fun idtable ty ctx ids = {objtype=ty,context=ctx,ids=ids} |
|
602 |
||
603 |
val thms_of_thy = map fst o PureThy.thms_of o ThyInfo.get_theory |
|
604 |
||
605 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base |
|
606 |
||
607 |
fun filerefs f = |
|
608 |
let val thy = thy_name f |
|
609 |
val (_,filerefs) = OuterSyntax.deps_thy thy true f (* (Path.unpack f); *) |
|
610 |
in |
|
611 |
issue_pgip (Setrefs {url=url, thyname=NONE, objtype=SOME PgipTypes.ObjFile, |
|
612 |
name=NONE, idtables=[], fileurls=filerefs}) |
|
613 |
end |
|
614 |
||
615 |
fun thyrefs thy = |
|
616 |
let val ml_path = ThyLoad.ml_path thy |
|
617 |
val (thyrefs,_) = OuterSyntax.deps_thy thy true ml_path (* (Path.unpack f); *) |
|
618 |
in |
|
619 |
issue_pgip (Setrefs {url=url, thyname=thyname, objtype=SOME PgipTypes.ObjTheory, |
|
620 |
name=NONE, idtables=[{context=NONE, objtype=PgipTypes.ObjTheory, |
|
621 |
ids=thyrefs}], fileurls=[]}) |
|
622 |
end |
|
623 |
||
624 |
fun thmrefs thmname = |
|
625 |
let |
|
626 |
(* TODO: interim: this is probably not right. |
|
627 |
What we want is mapping onto simple PGIP name/context model. *) |
|
628 |
val ctx = Toplevel.context_of (Toplevel.get_state()) (* NB: raises UNDEF *) |
|
629 |
val thy = Context.theory_of_proof ctx |
|
630 |
val ths = [PureThy.get_thm thy (PureThy.Name thmname)] |
|
631 |
val deps = filter_out (equal "") |
|
632 |
(Symtab.keys (fold Proofterm.thms_of_proof |
|
633 |
(map Thm.proof_of ths) Symtab.empty)) |
|
634 |
in |
|
635 |
if null deps then () |
|
636 |
else issue_pgip (Setrefs {url=url, thyname=thyname, name=name, |
|
637 |
objtype=SOME PgipTypes.ObjTheorem, |
|
638 |
idtables=[{context=NONE, objtype=PgipTypes.ObjTheorem, |
|
639 |
ids=deps}], fileurls=[]}) |
|
640 |
end |
|
641 |
in |
|
642 |
case (url,thyname,objtype,name) of |
|
643 |
(SOME file, NONE, _, _) => filerefs file |
|
644 |
| (_,SOME thy,_,_) => thyrefs thy |
|
645 |
| (_,_,SOME PgipTypes.ObjTheorem,SOME thmname) => thmrefs thmname |
|
646 |
| _ => error ("Unimplemented/invalid case of <askrefs>") |
|
647 |
end |
|
648 |
||
649 |
||
650 |
||
21637 | 651 |
local |
652 |
(* accumulate printed output in a single PGIP response (inside <pgmltext>) *) |
|
653 |
fun with_displaywrap pgipfn dispfn = |
|
654 |
let |
|
21940 | 655 |
val lines = ref ([]: string list); |
656 |
fun wlgrablines s = lines := s :: ! lines; |
|
21637 | 657 |
in |
21940 | 658 |
setmp writeln_fn wlgrablines dispfn (); |
659 |
issue_pgip (pgipfn (!lines)) |
|
21637 | 660 |
end; |
661 |
in |
|
21940 | 662 |
fun showid (Showid vs) = |
21637 | 663 |
let |
21940 | 664 |
val thyname = #thyname vs |
665 |
val objtype = #objtype vs |
|
666 |
val name = #name vs |
|
667 |
val topthy = Toplevel.theory_of o Toplevel.get_state |
|
21637 | 668 |
|
21940 | 669 |
fun idvalue objtype name strings = |
670 |
Idvalue { name=name, objtype=objtype, |
|
671 |
text=[XML.Elem("pgmltext",[],map XML.Text strings)] } |
|
21637 | 672 |
|
21940 | 673 |
fun pthm thy name = print_thm (get_thm thy (Name name)) |
674 |
in |
|
675 |
case (thyname, objtype) of |
|
676 |
(_,ObjTheory) => |
|
677 |
with_displaywrap (idvalue ObjTheory name) |
|
678 |
(fn ()=>(print_theory (ThyInfo.get_theory name))) |
|
679 |
| (SOME thy, ObjTheorem) => |
|
680 |
with_displaywrap (idvalue ObjTheorem name) |
|
681 |
(fn ()=>(pthm (ThyInfo.get_theory thy) name)) |
|
682 |
| (NONE, ObjTheorem) => |
|
683 |
with_displaywrap (idvalue ObjTheorem name) |
|
684 |
(fn ()=>pthm (topthy()) name) |
|
685 |
| (_, ot) => error ("Cannot show objects of type "^(PgipTypes.name_of_objtype ot)) |
|
21637 | 686 |
end |
687 |
||
21867 | 688 |
(*** Inspecting state ***) |
689 |
||
21940 | 690 |
(* The file which is currently being processed interactively. |
21637 | 691 |
In the pre-PGIP code, this was informed to Isabelle and the theory loader |
692 |
on completion, but that allows for circularity in case we read |
|
693 |
ourselves. So PGIP opens the filename at the start of a script. |
|
694 |
We ought to prevent problems by modifying the theory loader to know |
|
21940 | 695 |
about this special status, but for now we just keep a local reference. |
696 |
*) |
|
21637 | 697 |
|
698 |
val currently_open_file = ref (NONE : pgipurl option) |
|
699 |
||
22163 | 700 |
fun get_currently_open_file () = ! currently_open_file; |
701 |
||
21940 | 702 |
fun askguise vs = |
21637 | 703 |
(* The "guise" is the PGIP abstraction of the prover's state. |
704 |
The <informguise> message is merely used for consistency checking. *) |
|
21940 | 705 |
let |
706 |
val openfile = !currently_open_file |
|
21637 | 707 |
|
21940 | 708 |
val topthy = Toplevel.theory_of o Toplevel.get_state |
709 |
val topthy_name = Context.theory_name o topthy |
|
21637 | 710 |
|
21940 | 711 |
val opentheory = SOME (topthy_name()) handle Toplevel.UNDEF => NONE |
21637 | 712 |
|
21940 | 713 |
fun topproofpos () = try Toplevel.proof_position_of (Isar.state ()); |
714 |
val openproofpos = topproofpos() |
|
21637 | 715 |
in |
716 |
issue_pgip (Informguise { file = openfile, |
|
21940 | 717 |
theory = opentheory, |
718 |
(* would be nice to get thm name... *) |
|
719 |
theorem = NONE, |
|
720 |
proofpos = openproofpos }) |
|
21637 | 721 |
end |
722 |
||
21902 | 723 |
fun parsescript (Parsescript vs) = |
21637 | 724 |
let |
21940 | 725 |
val text = #text vs |
726 |
val systemdata = #systemdata vs |
|
727 |
val location = #location vs (* TODO: extract position *) |
|
21637 | 728 |
|
21867 | 729 |
val _ = start_delay_msgs () (* gather parsing errs/warns *) |
730 |
val doc = PgipParser.pgip_parser text |
|
21637 | 731 |
val errs = end_delayed_msgs () |
732 |
||
21940 | 733 |
val sysattrs = PgipTypes.opt_attr "systemdata" systemdata |
734 |
val locattrs = PgipTypes.attrs_of_location location |
|
21637 | 735 |
in |
736 |
issue_pgip (Parseresult { attrs= sysattrs@locattrs, |
|
21940 | 737 |
doc = doc, |
738 |
errs = map PgipOutput.output errs }) |
|
21637 | 739 |
end |
740 |
||
741 |
fun showproofstate vs = isarcmd "pr" |
|
742 |
||
21969 | 743 |
fun showctxt vs = isarcmd "print_context" |
21637 | 744 |
|
21902 | 745 |
fun searchtheorems (Searchtheorems vs) = |
21940 | 746 |
let |
747 |
val arg = #arg vs |
|
21637 | 748 |
in |
21969 | 749 |
isarcmd ("find_theorems " ^ arg) |
21637 | 750 |
end |
751 |
||
21940 | 752 |
fun setlinewidth (Setlinewidth vs) = |
753 |
let |
|
754 |
val width = #width vs |
|
21637 | 755 |
in |
21940 | 756 |
isarcmd ("pretty_setmargin " ^ Int.toString width) (* FIXME: conversion back/forth! *) |
21637 | 757 |
end |
758 |
||
21902 | 759 |
fun viewdoc (Viewdoc vs) = |
21940 | 760 |
let |
761 |
val arg = #arg vs |
|
762 |
in |
|
763 |
isarcmd ("print_" ^ arg) (* FIXME: isatool doc?. Return URLs, maybe? *) |
|
21637 | 764 |
end |
765 |
||
21867 | 766 |
(*** Theory ***) |
767 |
||
21902 | 768 |
fun doitem (Doitem vs) = |
21637 | 769 |
let |
21940 | 770 |
val text = #text vs |
21637 | 771 |
in |
21940 | 772 |
isarcmd text |
21637 | 773 |
end |
774 |
||
775 |
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
|
776 |
isarcmd "undo" |
21637 | 777 |
|
778 |
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
|
779 |
isarcmd "redo" |
21637 | 780 |
|
21940 | 781 |
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
|
782 |
isarcmd "kill" (* was: "init_toplevel" *) |
21637 | 783 |
|
21902 | 784 |
fun retracttheory (Retracttheory vs) = |
21940 | 785 |
let |
786 |
val thyname = #thyname vs |
|
21637 | 787 |
in |
21940 | 788 |
isarcmd ("kill_thy " ^ quote thyname) |
21637 | 789 |
end |
790 |
||
21867 | 791 |
|
792 |
(*** Files ***) |
|
793 |
||
794 |
(* Path management: we allow theory files to have dependencies in |
|
795 |
their own directory, but when we change directory for a new file we |
|
796 |
remove the path. Leaving it there can cause confusion with |
|
797 |
difference in batch mode. |
|
21940 | 798 |
NB: PGIP does not assume that the prover has a load path. |
21867 | 799 |
*) |
800 |
||
801 |
local |
|
802 |
val current_working_dir = ref (NONE : string option) |
|
803 |
in |
|
21940 | 804 |
fun changecwd_dir newdirpath = |
805 |
let |
|
21867 | 806 |
val newdir = File.platform_path newdirpath |
21940 | 807 |
in |
21867 | 808 |
(case (!current_working_dir) of |
809 |
NONE => () |
|
810 |
| SOME dir => ThyLoad.del_path dir; |
|
811 |
ThyLoad.add_path newdir; |
|
812 |
current_working_dir := SOME newdir) |
|
813 |
end |
|
814 |
end |
|
815 |
||
21940 | 816 |
fun changecwd (Changecwd vs) = |
817 |
let |
|
818 |
val url = #url vs |
|
819 |
val newdir = PgipTypes.path_of_pgipurl url |
|
21867 | 820 |
in |
21940 | 821 |
changecwd_dir url |
21867 | 822 |
end |
823 |
||
21902 | 824 |
fun openfile (Openfile vs) = |
21940 | 825 |
let |
21867 | 826 |
val url = #url vs |
827 |
val filepath = PgipTypes.path_of_pgipurl url |
|
828 |
val filedir = Path.dir filepath |
|
829 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base |
|
830 |
val openfile_retract = Output.no_warnings (ThyInfo.if_known_thy ThyInfo.remove_thy) o thy_name; |
|
831 |
in |
|
832 |
case !currently_open_file of |
|
22028 | 833 |
SOME f => raise PGIP ("<openfile> when a file is already open!\nCurrently open file: " ^ |
834 |
PgipTypes.string_of_pgipurl url) |
|
21867 | 835 |
| NONE => (openfile_retract filepath; |
21940 | 836 |
changecwd_dir filedir; |
22028 | 837 |
priority ("Working in file: " ^ PgipTypes.string_of_pgipurl url); |
21940 | 838 |
currently_open_file := SOME url) |
21867 | 839 |
end |
840 |
||
841 |
fun closefile vs = |
|
842 |
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
|
843 |
SOME f => (proper_inform_file_processed f (Isar.state()); |
22028 | 844 |
priority ("Finished working in file: " ^ PgipTypes.string_of_pgipurl f); |
21867 | 845 |
currently_open_file := NONE) |
846 |
| NONE => raise PGIP ("<closefile> when no file is open!") |
|
847 |
||
21940 | 848 |
fun loadfile (Loadfile vs) = |
849 |
let |
|
850 |
val url = #url vs |
|
851 |
in |
|
22171
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
852 |
(* 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
|
853 |
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
|
854 |
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
|
855 |
(* case !currently_open_file of |
22028 | 856 |
SOME f => raise PGIP ("<loadfile> when a file is open!\nCurrently open file: " ^ |
857 |
PgipTypes.string_of_pgipurl url) |
|
22171
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
858 |
| NONE => *) |
58f554f51f0d
Let <loadfile> execute even while a file is open interactively.
aspinall
parents:
22163
diff
changeset
|
859 |
use_thy_or_ml_file (File.platform_path url) |
21637 | 860 |
end |
861 |
||
862 |
fun abortfile vs = |
|
863 |
case !currently_open_file of |
|
864 |
SOME f => (isarcmd "init_toplevel"; |
|
22028 | 865 |
priority ("Aborted working in file: " ^ |
866 |
PgipTypes.string_of_pgipurl f); |
|
21940 | 867 |
currently_open_file := NONE) |
21637 | 868 |
| NONE => raise PGIP ("<abortfile> when no file is open!") |
869 |
||
21940 | 870 |
fun retractfile (Retractfile vs) = |
871 |
let |
|
872 |
val url = #url vs |
|
21637 | 873 |
in |
21940 | 874 |
case !currently_open_file of |
21637 | 875 |
SOME f => raise PGIP ("<retractfile> when a file is open!") |
22028 | 876 |
| NONE => (priority ("Retracting file: " ^ PgipTypes.string_of_pgipurl url); |
22042
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
877 |
(* TODO: next should be in thy loader, here just for testing *) |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
878 |
let |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
879 |
val name = thy_name url |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
880 |
in List.app (tell_file_retracted false) (ThyInfo.loaded_files name) end; |
64f8f5433f30
Cleanup message model: add info fatality level. Add informfileoutdated and some use of completed flag.
aspinall
parents:
22028
diff
changeset
|
881 |
inform_file_retracted url) |
21637 | 882 |
end |
883 |
||
884 |
||
21867 | 885 |
(*** System ***) |
21637 | 886 |
|
21902 | 887 |
fun systemcmd (Systemcmd vs) = |
21940 | 888 |
let |
21637 | 889 |
val arg = #arg vs |
890 |
in |
|
891 |
isarcmd arg |
|
892 |
end |
|
893 |
||
894 |
exception PGIP_QUIT; |
|
895 |
fun quitpgip vs = raise PGIP_QUIT |
|
896 |
||
21902 | 897 |
fun process_input inp = case inp |
898 |
of Pgip.Askpgip _ => askpgip inp |
|
899 |
| Pgip.Askpgml _ => askpgml inp |
|
21940 | 900 |
| Pgip.Askprefs _ => askprefs inp |
21902 | 901 |
| Pgip.Askconfig _ => askconfig inp |
902 |
| Pgip.Getpref _ => getpref inp |
|
903 |
| Pgip.Setpref _ => setpref inp |
|
904 |
| Pgip.Proverinit _ => proverinit inp |
|
905 |
| Pgip.Proverexit _ => proverexit inp |
|
906 |
| Pgip.Startquiet _ => startquiet inp |
|
907 |
| Pgip.Stopquiet _ => stopquiet inp |
|
908 |
| Pgip.Pgmlsymbolson _ => pgmlsymbolson inp |
|
909 |
| Pgip.Pgmlsymbolsoff _ => pgmlsymbolsoff inp |
|
910 |
| Pgip.Dostep _ => dostep inp |
|
911 |
| Pgip.Undostep _ => undostep inp |
|
912 |
| Pgip.Redostep _ => redostep inp |
|
913 |
| Pgip.Forget _ => error "<forget> not implemented" |
|
914 |
| Pgip.Restoregoal _ => error "<restoregoal> not implemented" |
|
915 |
| Pgip.Abortgoal _ => abortgoal inp |
|
916 |
| Pgip.Askids _ => askids inp |
|
22159 | 917 |
| Pgip.Askrefs _ => askrefs inp |
21902 | 918 |
| Pgip.Showid _ => showid inp |
919 |
| Pgip.Askguise _ => askguise inp |
|
920 |
| Pgip.Parsescript _ => parsescript inp |
|
921 |
| Pgip.Showproofstate _ => showproofstate inp |
|
922 |
| Pgip.Showctxt _ => showctxt inp |
|
923 |
| Pgip.Searchtheorems _ => searchtheorems inp |
|
924 |
| Pgip.Setlinewidth _ => setlinewidth inp |
|
925 |
| Pgip.Viewdoc _ => viewdoc inp |
|
926 |
| Pgip.Doitem _ => doitem inp |
|
927 |
| Pgip.Undoitem _ => undoitem inp |
|
928 |
| Pgip.Redoitem _ => redoitem inp |
|
929 |
| Pgip.Aborttheory _ => aborttheory inp |
|
930 |
| Pgip.Retracttheory _ => retracttheory inp |
|
931 |
| Pgip.Loadfile _ => loadfile inp |
|
932 |
| Pgip.Openfile _ => openfile inp |
|
933 |
| Pgip.Closefile _ => closefile inp |
|
934 |
| Pgip.Abortfile _ => abortfile inp |
|
935 |
| Pgip.Retractfile _ => retractfile inp |
|
936 |
| Pgip.Changecwd _ => changecwd inp |
|
937 |
| Pgip.Systemcmd _ => systemcmd inp |
|
938 |
| Pgip.Quitpgip _ => quitpgip inp |
|
21637 | 939 |
|
940 |
||
21940 | 941 |
fun process_pgip_element pgipxml = |
21637 | 942 |
case pgipxml of |
21969 | 943 |
xml as (XML.Elem elem) => |
21940 | 944 |
(case Pgip.input elem of |
945 |
NONE => warning ("Unrecognized PGIP command, ignored: \n" ^ |
|
946 |
(XML.string_of_tree xml)) |
|
947 |
| SOME inp => (process_input inp)) (* errors later; packet discarded *) |
|
21969 | 948 |
| XML.Text t => ignored_text_warning t |
949 |
| XML.Rawtext t => ignored_text_warning t |
|
21637 | 950 |
and ignored_text_warning t = |
21940 | 951 |
if size (Symbol.strip_blanks t) > 0 then |
952 |
warning ("Ignored text in PGIP packet: \n" ^ t) |
|
21637 | 953 |
else () |
954 |
||
955 |
fun process_pgip_tree xml = |
|
956 |
(pgip_refid := NONE; |
|
957 |
pgip_refseq := NONE; |
|
958 |
(case xml of |
|
959 |
XML.Elem ("pgip", attrs, pgips) => |
|
960 |
(let |
|
961 |
val class = PgipTypes.get_attr "class" attrs |
|
962 |
val dest = PgipTypes.get_attr_opt "destid" attrs |
|
21940 | 963 |
val seq = PgipTypes.read_pgipnat (PgipTypes.get_attr "seq" attrs) |
21637 | 964 |
(* Respond to prover broadcasts, or messages for us. Ignore rest *) |
21940 | 965 |
val processit = |
966 |
case dest of |
|
21637 | 967 |
NONE => class = "pa" |
21940 | 968 |
| SOME id => matching_pgip_id id |
969 |
in if processit then |
|
970 |
(pgip_refid := PgipTypes.get_attr_opt "id" attrs; |
|
971 |
pgip_refseq := SOME seq; |
|
972 |
List.app process_pgip_element pgips; |
|
973 |
(* return true to indicate <ready/> *) |
|
974 |
true) |
|
975 |
else |
|
976 |
(* no response to ignored messages. *) |
|
977 |
false |
|
21637 | 978 |
end) |
979 |
| _ => raise PGIP "Invalid PGIP packet received") |
|
980 |
handle PGIP msg => |
|
981 |
(Output.error_msg ((msg ^ "\nPGIP error occured in XML text below:\n") ^ |
|
21940 | 982 |
(XML.string_of_tree xml)); |
983 |
true)) |
|
21637 | 984 |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
985 |
(* External input *) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
986 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
987 |
val process_pgip_plain = K () o process_pgip_tree o XML.tree_of_string |
21637 | 988 |
|
989 |
end |
|
990 |
||
991 |
||
992 |
(* PGIP loop: process PGIP input only *) |
|
993 |
||
994 |
local |
|
995 |
||
996 |
exception XML_PARSE |
|
997 |
||
998 |
fun loop ready src = |
|
999 |
let |
|
1000 |
val _ = if ready then issue_pgip (Ready ()) else () |
|
21969 | 1001 |
val pgipo = |
1002 |
(case try Source.get_single src of |
|
1003 |
SOME pgipo => pgipo |
|
1004 |
| NONE => raise XML_PARSE) |
|
21637 | 1005 |
in |
1006 |
case pgipo of |
|
1007 |
NONE => () |
|
1008 |
| SOME (pgip,src') => |
|
1009 |
let |
|
21940 | 1010 |
val ready' = (process_pgip_tree pgip) |
1011 |
handle e => (handler (e,SOME src'); true) |
|
21637 | 1012 |
in |
1013 |
loop ready' src' |
|
1014 |
end |
|
1015 |
end handle e => handler (e,SOME src) (* error in XML parse or Ready issue *) |
|
1016 |
||
1017 |
and handler (e,srco) = |
|
1018 |
case (e,srco) of |
|
1019 |
(XML_PARSE,SOME src) => |
|
1020 |
Output.panic "Invalid XML input, aborting" |
|
1021 |
| (PGIP_QUIT,_) => () |
|
21940 | 1022 |
| (Interrupt,SOME src) => |
1023 |
(Output.error_msg "Interrupt during PGIP processing"; loop true src) |
|
1024 |
| (Toplevel.UNDEF,SOME src) => |
|
1025 |
(Output.error_msg "No working context defined"; loop true src) |
|
1026 |
| (e,SOME src) => |
|
1027 |
(Output.error_msg (Toplevel.exn_message e); loop true src) |
|
21637 | 1028 |
| (_,NONE) => () |
1029 |
in |
|
1030 |
(* TODO: add socket interface *) |
|
1031 |
||
1032 |
val xmlP = XML.scan_comment_whspc |-- XML.parse_elem >> single |
|
1033 |
||
1034 |
val tty_src = Source.set_prompt "" (Source.source Symbol.stopper xmlP NONE Source.tty) |
|
1035 |
||
1036 |
fun pgip_toplevel x = loop true x |
|
1037 |
end |
|
1038 |
||
1039 |
||
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1040 |
local |
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1041 |
(* Extra command for embedding prover-control inside document (obscure/debug usage). *) |
21637 | 1042 |
|
22014
4b70cbd96007
removed Toplevel.print_exn hook -- existing error_msg_fn does the job;
wenzelm
parents:
22001
diff
changeset
|
1043 |
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
|
1044 |
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
|
1045 |
(OuterParse.text >> (Toplevel.no_timing oo |
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1046 |
(fn txt => Toplevel.imperative (fn () => process_pgip_plain txt)))); |
21637 | 1047 |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1048 |
in |
21637 | 1049 |
|
21972
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1050 |
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
|
1051 |
|
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1052 |
end |
1b68312c4cf0
Initialise parser at startup. Remove some obsolete ProofGeneral.XXX outer syntax, mapping PGIP commands directly to Isar.
aspinall
parents:
21970
diff
changeset
|
1053 |
|
21637 | 1054 |
|
1055 |
||
1056 |
(* init *) |
|
1057 |
||
1058 |
val initialized = ref false; |
|
1059 |
||
21969 | 1060 |
fun init_pgip false = |
1061 |
Output.panic "No Proof General interface support for Isabelle/classic mode." |
|
1062 |
| init_pgip true = |
|
1063 |
(! initialized orelse |
|
1064 |
(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
|
1065 |
PgipParser.init (); |
21969 | 1066 |
setup_xsymbols_output (); |
1067 |
setup_pgml_output (); |
|
1068 |
setup_messages (); |
|
1069 |
setup_state (); |
|
1070 |
setup_thy_loader (); |
|
1071 |
setup_present_hook (); |
|
1072 |
init_pgip_session_id (); |
|
1073 |
welcome (); |
|
1074 |
set initialized); |
|
1075 |
sync_thy_loader (); |
|
1076 |
change print_mode (cons proof_generalN o remove (op =) proof_generalN); |
|
1077 |
change print_mode (append [pgmlN, pgmlatomsN] o subtract (op =) [pgmlN, pgmlatomsN]); |
|
1078 |
pgip_toplevel tty_src); |
|
21637 | 1079 |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1080 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1081 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1082 |
(** Out-of-loop PGIP commands (for Emacs hybrid mode) **) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1083 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1084 |
local |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1085 |
val pgip_output_channel = ref writeln_default |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1086 |
in |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1087 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1088 |
(* Set recipient for PGIP results *) |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1089 |
fun init_pgip_channel writefn = |
21940 | 1090 |
(init_pgip_session_id(); |
1091 |
pgip_output_channel := writefn) |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1092 |
|
21940 | 1093 |
(* Process a PGIP command. |
1094 |
This works for preferences but not generally guaranteed |
|
21649
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1095 |
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
|
1096 |
fun process_pgip str = |
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1097 |
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
|
1098 |
|
40e6fdd26f82
Support PGIP communication for preferences in Emacs mode.
aspinall
parents:
21646
diff
changeset
|
1099 |
end |
21637 | 1100 |
|
1101 |
end; |
|
1102 |