author | wenzelm |
Fri, 29 Dec 2006 18:25:46 +0100 | |
changeset 21940 | fbd068dd4d29 |
parent 21902 | 8e5e2571c716 |
child 21944 | e877a5a78522 |
permissions | -rw-r--r-- |
21637 | 1 |
(* Title: Pure/ProofGeneral/pgip_types.ML |
2 |
ID: $Id$ |
|
3 |
Author: David Aspinall |
|
4 |
||
21940 | 5 |
PGIP abstraction: types and conversions. |
21637 | 6 |
*) |
7 |
||
21867 | 8 |
(* TODO: PGML *) |
21637 | 9 |
signature PGIPTYPES = |
10 |
sig |
|
21867 | 11 |
(* Object types: the types of values which can be manipulated externally. |
12 |
Ideally a list of other types would be configured as a parameter. *) |
|
13 |
datatype objtype = ObjFile | ObjTheory | ObjTheorem | ObjComment |
|
14 |
| ObjTerm | ObjType | ObjOther of string |
|
15 |
||
16 |
(* Names for values of objtypes (i.e. prover identifiers). Could be a parameter. |
|
17 |
Names for ObjFiles are URIs. *) |
|
18 |
type objname = string |
|
19 |
||
20 |
type idtable = { context: objname option, (* container's objname *) |
|
21 |
objtype: objtype, |
|
22 |
ids: objname list } |
|
23 |
||
21637 | 24 |
(* Types and values (used for preferences and dialogs) *) |
25 |
||
26 |
datatype pgiptype = Pgipnull | Pgipbool | Pgipint of int option * int option | Pgipnat |
|
27 |
| Pgipstring | Pgipconst of string | Pgipchoice of pgipdtype list |
|
28 |
||
29 |
and pgipdtype = Pgipdtype of string option * pgiptype (* type with opt. description *) |
|
30 |
||
31 |
type pgipval (* typed value *) |
|
32 |
||
33 |
(* URLs we can cope with *) |
|
34 |
type pgipurl |
|
35 |
||
36 |
(* Representation error in reading/writing PGIP *) |
|
37 |
exception PGIP of string |
|
38 |
||
39 |
||
40 |
(* Interface areas for message output *) |
|
41 |
datatype displayarea = Status | Message | Display | Other of string |
|
42 |
||
43 |
(* Kinds of message output (influence display behaviour) *) |
|
44 |
datatype messagecategory = Normal | Information | Tracing | Internal |
|
45 |
||
46 |
(* Error levels *) |
|
47 |
datatype fatality = Nonfatal | Fatal | Panic | Log | Debug |
|
48 |
||
49 |
(* File location *) |
|
50 |
type location = { descr: string option, |
|
51 |
url: pgipurl option, |
|
52 |
line: int option, |
|
53 |
column: int option, |
|
54 |
char: int option } |
|
55 |
||
56 |
(* Prover preference *) |
|
57 |
type preference = { name: string, |
|
58 |
descr: string option, |
|
59 |
default: string option, |
|
60 |
pgiptype: pgiptype } |
|
61 |
end |
|
62 |
||
63 |
signature PGIPTYPES_OPNS = |
|
64 |
sig |
|
65 |
include PGIPTYPES |
|
21867 | 66 |
|
67 |
(* Object types *) |
|
68 |
val name_of_objtype : objtype -> string |
|
69 |
val attrs_of_objtype : objtype -> XML.attributes |
|
70 |
val objtype_of_attrs : XML.attributes -> objtype (* raises PGIP *) |
|
71 |
val idtable_to_xml : idtable -> XML.tree |
|
21637 | 72 |
|
73 |
(* Values as XML strings *) |
|
74 |
val read_pgipint : (int option * int option) -> string -> int (* raises PGIP *) |
|
75 |
val read_pgipnat : string -> int (* raises PGIP *) |
|
76 |
val read_pgipbool : string -> bool (* raises PGIP *) |
|
77 |
val read_pgipstring : string -> string (* raises PGIP *) |
|
78 |
val int_to_pgstring : int -> string |
|
79 |
val bool_to_pgstring : bool -> string |
|
80 |
val string_to_pgstring : string -> string |
|
81 |
||
82 |
(* PGIP datatypes *) |
|
83 |
val pgiptype_to_xml : pgiptype -> XML.tree |
|
84 |
val read_pgval : pgiptype -> string -> pgipval (* raises PGIP *) |
|
85 |
val pgval_to_string : pgipval -> string |
|
86 |
||
87 |
val attrs_of_displayarea : displayarea -> XML.attributes |
|
88 |
val attrs_of_messagecategory : messagecategory -> XML.attributes |
|
89 |
val attrs_of_fatality : fatality -> XML.attributes |
|
90 |
val attrs_of_location : location -> XML.attributes |
|
91 |
val location_of_attrs : XML.attributes -> location (* raises PGIP *) |
|
92 |
||
93 |
val haspref : preference -> XML.tree |
|
94 |
||
95 |
val pgipurl_of_url : Url.T -> pgipurl (* raises PGIP *) |
|
96 |
val pgipurl_of_string : string -> pgipurl (* raises PGIP *) |
|
97 |
val pgipurl_of_path : Path.T -> pgipurl |
|
21867 | 98 |
val path_of_pgipurl : pgipurl -> Path.T |
21637 | 99 |
val attrs_of_pgipurl : pgipurl -> XML.attributes |
100 |
val pgipurl_of_attrs : XML.attributes -> pgipurl (* raises PGIP *) |
|
101 |
||
102 |
(* XML utils, only for PGIP code *) |
|
21867 | 103 |
val has_attr : string -> XML.attributes -> bool |
21637 | 104 |
val attr : string -> string -> XML.attributes |
105 |
val opt_attr : string -> string option -> XML.attributes |
|
106 |
val opt_attr_map : ('a -> string) -> string -> 'a option -> XML.attributes |
|
107 |
val get_attr : string -> XML.attributes -> string (* raises PGIP *) |
|
108 |
val get_attr_opt : string -> XML.attributes -> string option |
|
109 |
val get_attr_dflt : string -> string -> XML.attributes -> string |
|
110 |
end |
|
111 |
||
112 |
structure PgipTypes : PGIPTYPES_OPNS = |
|
113 |
struct |
|
114 |
exception PGIP of string |
|
115 |
||
21867 | 116 |
(** XML utils **) |
117 |
||
21902 | 118 |
fun has_attr (attr : string) attrs = AList.defined (op =) attrs attr |
21637 | 119 |
|
21902 | 120 |
fun get_attr_opt (attr : string) attrs = AList.lookup (op =) attrs attr |
21637 | 121 |
|
122 |
fun get_attr attr attrs = |
|
123 |
(case get_attr_opt attr attrs of |
|
124 |
SOME value => value |
|
125 |
| NONE => raise PGIP ("Missing attribute: " ^ attr)) |
|
126 |
||
127 |
fun get_attr_dflt attr dflt attrs = the_default dflt (get_attr_opt attr attrs) |
|
128 |
||
129 |
fun attr x y = [(x,y)] : XML.attributes |
|
130 |
||
131 |
fun opt_attr_map f attr_name opt_val = |
|
132 |
case opt_val of NONE => [] | SOME v => [(attr_name,f v)] |
|
133 |
(* or, if you've got function-itis: |
|
134 |
the_default [] (Option.map (single o (pair attr_name) o f) opt_val) |
|
135 |
*) |
|
136 |
||
21902 | 137 |
fun opt_attr attr_name = opt_attr_map I attr_name |
21637 | 138 |
|
21867 | 139 |
|
140 |
(** Objtypes **) |
|
141 |
||
142 |
datatype objtype = ObjFile | ObjTheory | ObjTheorem | ObjComment |
|
143 |
| ObjTerm | ObjType | ObjOther of string |
|
144 |
||
145 |
fun name_of_objtype obj = |
|
146 |
case obj of |
|
147 |
ObjFile => "file" |
|
148 |
| ObjTheory => "theory" |
|
149 |
| ObjTheorem => "theorem" |
|
150 |
| ObjComment => "comment" |
|
151 |
| ObjTerm => "term" |
|
152 |
| ObjType => "type" |
|
153 |
| ObjOther s => s |
|
154 |
||
21940 | 155 |
val attrs_of_objtype = attr "objtype" o name_of_objtype |
21867 | 156 |
|
157 |
fun objtype_of_attrs attrs = case get_attr "objtype" attrs of |
|
158 |
"file" => ObjFile |
|
159 |
| "theory" => ObjTheory |
|
160 |
| "theorem" => ObjTheorem |
|
161 |
| "comment" => ObjComment |
|
162 |
| "term" => ObjTerm |
|
163 |
| "type" => ObjType |
|
164 |
| s => ObjOther s (* where s mem other_objtypes_parameter *) |
|
165 |
||
166 |
type objname = string |
|
167 |
type idtable = { context: objname option, (* container's objname *) |
|
168 |
objtype: objtype, |
|
169 |
ids: objname list } |
|
170 |
||
171 |
fun idtable_to_xml {context, objtype, ids} = |
|
172 |
let |
|
173 |
val objtype_attrs = attrs_of_objtype objtype |
|
174 |
val context_attrs = opt_attr "context" context |
|
175 |
val ids_content = map (fn x => XML.Elem("identifier",[],[XML.Text x])) ids |
|
176 |
in |
|
177 |
XML.Elem ("idtable", |
|
178 |
objtype_attrs @ context_attrs, |
|
179 |
ids_content) |
|
180 |
end |
|
181 |
||
182 |
||
21637 | 183 |
(** Types and values **) |
184 |
||
185 |
(* readers and writers for values represented in XML strings *) |
|
186 |
||
187 |
fun read_pgipbool s = |
|
188 |
(case s of |
|
189 |
"false" => false |
|
190 |
| "true" => true |
|
191 |
| _ => raise PGIP ("Invalid boolean value: "^s)) |
|
192 |
||
193 |
local |
|
194 |
fun int_in_range (NONE,NONE) _ = true |
|
195 |
| int_in_range (SOME min,NONE) i = min<= i |
|
196 |
| int_in_range (NONE,SOME max) i = i<=max |
|
197 |
| int_in_range (SOME min,SOME max) i = min<= i andalso i<=max |
|
198 |
in |
|
199 |
fun read_pgipint range s = |
|
200 |
(case Syntax.read_int s of |
|
201 |
SOME i => if int_in_range range i then i |
|
202 |
else raise PGIP ("Out of range integer value: "^s) |
|
203 |
| NONE => raise PGIP ("Invalid integer value: "^s)) |
|
204 |
end; |
|
205 |
||
206 |
fun read_pgipnat s = |
|
207 |
(case Syntax.read_nat s of |
|
208 |
SOME i => i |
|
209 |
| NONE => raise PGIP ("Invalid natural number: "^s)) |
|
210 |
||
211 |
(* NB: we can maybe do without xml decode/encode here. *) |
|
212 |
fun read_pgipstring s = (* non-empty strings, XML escapes decoded *) |
|
213 |
(case XML.parse_string s of |
|
214 |
SOME s => s |
|
215 |
| NONE => raise PGIP ("Expected a non-empty string: "^s)) |
|
216 |
handle _ => raise PGIP ("Invalid XML string syntax: "^s) |
|
217 |
||
218 |
fun int_to_pgstring i = |
|
219 |
if i < 0 then "-" ^ string_of_int (~ i) else string_of_int i |
|
220 |
||
221 |
fun string_to_pgstring s = XML.text s |
|
222 |
||
223 |
fun bool_to_pgstring b = if b then "true" else "false" |
|
224 |
||
225 |
||
226 |
(* PGIP datatypes. |
|
227 |
||
228 |
This is a reflection of the type structure of PGIP configuration, |
|
229 |
which is meant for setting up user dialogs and preference settings. |
|
230 |
These are configured declaratively in XML, using a syntax for types |
|
231 |
and values which is like a (vastly cut down) form of XML Schema |
|
232 |
Datatypes. |
|
233 |
||
234 |
The prover needs to interpret the strings representing the typed |
|
235 |
values, at least for the types it expects from the dialogs it |
|
236 |
configures. Here we go further and construct a type-safe |
|
237 |
encapsulation of these values, which would be useful for more |
|
238 |
advanced cases (e.g. generating and processing forms). |
|
239 |
*) |
|
240 |
||
241 |
||
242 |
datatype pgiptype = |
|
243 |
Pgipnull (* unit type: unique element is empty string *) |
|
244 |
| Pgipbool (* booleans: "true" or "false" *) |
|
245 |
| Pgipint of int option * int option (* ranged integers, should be XSD canonical *) |
|
246 |
| Pgipnat (* naturals: non-negative integers (convenience) *) |
|
247 |
| Pgipstring (* non-empty strings *) |
|
248 |
| Pgipconst of string (* singleton type *) |
|
249 |
| Pgipchoice of pgipdtype list (* union type *) |
|
250 |
||
251 |
(* Compared with the PGIP schema, we push descriptions of types inside choices. *) |
|
252 |
||
253 |
and pgipdtype = Pgipdtype of string option * pgiptype |
|
254 |
||
255 |
datatype pgipuval = Pgvalnull | Pgvalbool of bool | Pgvalint of int | Pgvalnat of int |
|
256 |
| Pgvalstring of string | Pgvalconst of string |
|
257 |
||
258 |
type pgipval = pgiptype * pgipuval (* type-safe values *) |
|
259 |
||
260 |
fun pgipdtype_to_xml (desco,ty) = |
|
261 |
let |
|
262 |
val desc_attr = opt_attr "descr" desco |
|
263 |
||
264 |
val elt = case ty of |
|
265 |
Pgipnull => "pgipnull" |
|
266 |
| Pgipbool => "pgipbool" |
|
267 |
| Pgipint _ => "pgipint" |
|
268 |
| Pgipnat => "pgipint" |
|
269 |
| Pgipstring => "pgipstring" |
|
270 |
| Pgipconst _ => "pgipconst" |
|
271 |
| Pgipchoice _ => "pgipchoice" |
|
272 |
||
273 |
fun range_attr r v = attr r (int_to_pgstring v) |
|
274 |
||
275 |
val attrs = case ty of |
|
276 |
Pgipint (SOME min,SOME max) => (range_attr "min" min)@(range_attr "max" max) |
|
277 |
| Pgipint (SOME min,NONE) => (range_attr "min" min) |
|
278 |
| Pgipint (NONE,SOME max) => (range_attr "max" max) |
|
279 |
| Pgipnat => (range_attr "min" 0) |
|
280 |
| Pgipconst nm => attr "name" nm |
|
281 |
| _ => [] |
|
282 |
||
283 |
fun destpgipdtype (Pgipdtype x) = x |
|
284 |
||
285 |
val typargs = case ty of |
|
286 |
Pgipchoice ds => map destpgipdtype ds |
|
287 |
| _ => [] |
|
288 |
in |
|
289 |
XML.Elem (elt, (desc_attr @ attrs), (map pgipdtype_to_xml typargs)) |
|
290 |
end |
|
291 |
||
21940 | 292 |
val pgiptype_to_xml = pgipdtype_to_xml o pair NONE |
21637 | 293 |
|
294 |
fun read_pguval Pgipnull s = |
|
295 |
if s="" then Pgvalnull |
|
296 |
else raise PGIP ("Expecting empty string for null type, not: "^s) |
|
297 |
| read_pguval Pgipbool s = Pgvalbool (read_pgipbool s) |
|
298 |
| read_pguval (Pgipint range) s = Pgvalint (read_pgipint range s) |
|
299 |
| read_pguval Pgipnat s = Pgvalnat (read_pgipnat s) |
|
300 |
| read_pguval (Pgipconst c) s = |
|
301 |
if c=s then Pgvalconst c |
|
302 |
else raise PGIP ("Given string: "^s^" doesn't match expected string: "^c) |
|
303 |
| read_pguval Pgipstring s = |
|
304 |
if s<>"" then Pgvalstring s |
|
305 |
else raise PGIP ("Expecting non-empty string, empty string illegal.") |
|
306 |
| read_pguval (Pgipchoice tydescs) s = |
|
307 |
let |
|
308 |
(* Union type: match first in list *) |
|
21940 | 309 |
fun getty (Pgipdtype(_, ty)) = ty |
21637 | 310 |
val uval = get_first |
311 |
(fn ty => SOME (read_pguval ty s) handle PGIP _ => NONE) |
|
312 |
(map getty tydescs) |
|
313 |
in |
|
314 |
case uval of SOME pgv => pgv | NONE => raise PGIP ("Can't match string: "^s^ |
|
315 |
" against any allowed types.") |
|
316 |
end |
|
317 |
||
318 |
fun read_pgval ty s = (ty, read_pguval ty s) |
|
319 |
||
320 |
fun pgval_to_string (_, Pgvalnull) = "" |
|
21940 | 321 |
| pgval_to_string (_, Pgvalbool b) = bool_to_pgstring b |
322 |
| pgval_to_string (_, Pgvalnat n) = int_to_pgstring n |
|
323 |
| pgval_to_string (_, Pgvalint i) = int_to_pgstring i |
|
324 |
| pgval_to_string (_, Pgvalconst c) = string_to_pgstring c |
|
325 |
| pgval_to_string (_, Pgvalstring s) = string_to_pgstring s |
|
21637 | 326 |
|
327 |
||
21867 | 328 |
type pgipurl = Path.T (* URLs: only local files *) |
21637 | 329 |
|
330 |
datatype displayarea = Status | Message | Display | Other of string |
|
331 |
||
332 |
datatype messagecategory = Normal | Information | Tracing | Internal |
|
333 |
||
334 |
datatype fatality = Nonfatal | Fatal | Panic | Log | Debug |
|
335 |
||
336 |
type location = { descr: string option, |
|
337 |
url: pgipurl option, |
|
338 |
line: int option, |
|
339 |
column: int option, |
|
340 |
char: int option } |
|
341 |
||
342 |
||
343 |
||
344 |
(** Url operations **) |
|
345 |
||
21867 | 346 |
|
21637 | 347 |
fun pgipurl_of_string url = (* only handle file:/// or file://localhost/ *) |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21637
diff
changeset
|
348 |
case Url.explode url of |
21637 | 349 |
(Url.File path) => path |
350 |
| _ => raise PGIP ("Cannot access non-local URL " ^ url) |
|
351 |
||
352 |
fun pgipurl_of_path p = p |
|
353 |
||
21867 | 354 |
fun path_of_pgipurl p = p (* potentially raises PGIP *) |
355 |
||
21637 | 356 |
fun attrs_of_pgipurl purl = |
357 |
[("url", "file:" ^ (XML.text (File.platform_path (File.full_path purl))))] |
|
358 |
||
21940 | 359 |
val pgipurl_of_attrs = pgipurl_of_string o get_attr "url" |
21637 | 360 |
|
361 |
fun pgipurl_of_url (Url.File p) = p |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21637
diff
changeset
|
362 |
| pgipurl_of_url url = raise PGIP ("Cannot access non-local/non-file URL " ^ (Url.implode url)) |
21637 | 363 |
|
364 |
||
365 |
(** Messages and errors **) |
|
366 |
||
367 |
local |
|
368 |
fun string_of_area Status = "status" |
|
369 |
| string_of_area Message = "message" |
|
370 |
| string_of_area Display = "display" |
|
371 |
| string_of_area (Other s) = s |
|
372 |
||
373 |
fun string_of_msgcat Internal = "internal" |
|
374 |
| string_of_msgcat Information = "information" |
|
375 |
| string_of_msgcat Tracing = "tracing" |
|
376 |
| string_of_msgcat Normal = "normal" (* omitted in PGIP *) |
|
377 |
||
378 |
fun string_of_fatality Nonfatal = "nonfatal" |
|
379 |
| string_of_fatality Fatal = "fatal" |
|
380 |
| string_of_fatality Panic = "panic" |
|
381 |
| string_of_fatality Log = "log" |
|
382 |
| string_of_fatality Debug = "debug" |
|
383 |
in |
|
384 |
fun attrs_of_displayarea area = [("area", string_of_area area)] |
|
385 |
||
386 |
fun attrs_of_messagecategory msgcat = |
|
387 |
case msgcat of |
|
388 |
Normal => [] |
|
389 |
| _ => [("messagecategory", string_of_msgcat msgcat)] |
|
390 |
||
391 |
fun attrs_of_fatality fatality = [("fatality", string_of_fatality fatality)] |
|
392 |
||
393 |
fun attrs_of_location ({ descr, url, line, column, char }:location) = |
|
394 |
let |
|
21940 | 395 |
val descr = opt_attr "descr" descr |
396 |
val url = these (Option.map attrs_of_pgipurl url) |
|
21637 | 397 |
val line = opt_attr_map int_to_pgstring "line" line |
398 |
val column = opt_attr_map int_to_pgstring "column" column |
|
399 |
val char = opt_attr_map int_to_pgstring "char" char |
|
400 |
in |
|
401 |
descr @ url @ line @ column @ char |
|
402 |
end |
|
403 |
||
404 |
fun pgipint_of_string err s = |
|
405 |
case Syntax.read_int s of |
|
406 |
SOME i => i |
|
407 |
| NONE => raise PGIP ("Type error in " ^ err ^ ": expected integer.") |
|
408 |
||
409 |
fun location_of_attrs attrs = |
|
410 |
let |
|
411 |
val descr = get_attr_opt "descr" attrs |
|
412 |
val url = Option.map pgipurl_of_string (get_attr_opt "url" attrs) |
|
413 |
val line = Option.map (pgipint_of_string "location element line attribute") |
|
414 |
(get_attr_opt "line" attrs) |
|
415 |
val column = Option.map (pgipint_of_string "location element column attribute") |
|
416 |
(get_attr_opt "column" attrs) |
|
417 |
val char = Option.map (pgipint_of_string "location element char attribute") |
|
418 |
(get_attr_opt "char" attrs) |
|
419 |
in |
|
420 |
{descr=descr, url=url, line=line, column=column, char=char} |
|
421 |
end |
|
422 |
end |
|
423 |
||
424 |
(** Preferences **) |
|
425 |
||
426 |
type preference = { name: string, |
|
427 |
descr: string option, |
|
428 |
default: string option, |
|
429 |
pgiptype: pgiptype } |
|
430 |
||
431 |
fun haspref ({ name, descr, default, pgiptype}:preference) = |
|
432 |
XML.Elem ("haspref", |
|
433 |
attr "name" name @ |
|
434 |
opt_attr "descr" descr @ |
|
435 |
opt_attr "default" default, |
|
436 |
[pgiptype_to_xml pgiptype]) |
|
437 |
||
438 |
end |
|
439 |