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