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