author | haftmann |
Tue, 02 Oct 2007 07:59:55 +0200 | |
changeset 24811 | 3bf788a0c49a |
parent 24423 | ae9cd0e92423 |
child 24840 | 01b14b37eca3 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_name.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Naming policies for code generation: prefixing any name by corresponding |
|
6 |
theory name, conversion to alphanumeric representation. |
|
7 |
Mappings are incrementally cached. |
|
8 |
*) |
|
9 |
||
10 |
signature CODE_NAME = |
|
11 |
sig |
|
12 |
val purify_var: string -> string |
|
13 |
val purify_tvar: string -> string |
|
14 |
val check_modulename: string -> string |
|
15 |
type var_ctxt; |
|
16 |
val make_vars: string list -> var_ctxt; |
|
17 |
val intro_vars: string list -> var_ctxt -> var_ctxt; |
|
18 |
val lookup_var: var_ctxt -> string -> string; |
|
19 |
||
20 |
val class: theory -> class -> class |
|
21 |
val class_rev: theory -> class -> class option |
|
22 |
val classrel: theory -> class * class -> string |
|
23 |
val classrel_rev: theory -> string -> (class * class) option |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
24 |
val tyco: theory -> string -> string |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
25 |
val tyco_rev: theory -> string -> string option |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
26 |
val instance: theory -> class * string -> string |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
27 |
val instance_rev: theory -> string -> (class * string) option |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
28 |
val const: theory -> string -> string |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
29 |
val const_rev: theory -> string -> string option |
24219 | 30 |
val labelled_name: theory -> string -> string |
31 |
||
32 |
val setup: theory -> theory |
|
33 |
end; |
|
34 |
||
35 |
structure CodeName: CODE_NAME = |
|
36 |
struct |
|
37 |
||
38 |
(** purification **) |
|
39 |
||
40 |
fun purify_name upper_else_lower = |
|
41 |
let |
|
42 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'"; |
|
43 |
val is_junk = not o is_valid andf Symbol.is_regular; |
|
44 |
val junk = Scan.many is_junk; |
|
45 |
val scan_valids = Symbol.scanner "Malformed input" |
|
46 |
((junk |-- |
|
47 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
|
48 |
--| junk)) |
|
49 |
-- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::); |
|
50 |
fun upper_lower cs = if upper_else_lower then nth_map 0 Symbol.to_ascii_upper cs |
|
51 |
else (if forall Symbol.is_ascii_upper cs |
|
52 |
then map else nth_map 0) Symbol.to_ascii_lower cs; |
|
53 |
in |
|
54 |
explode |
|
55 |
#> scan_valids |
|
56 |
#> space_implode "_" |
|
57 |
#> explode |
|
58 |
#> upper_lower |
|
59 |
#> implode |
|
60 |
end; |
|
61 |
||
62 |
fun purify_var "" = "x" |
|
63 |
| purify_var v = purify_name false v; |
|
64 |
||
65 |
fun purify_tvar "" = "'a" |
|
66 |
| purify_tvar v = |
|
67 |
(unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v; |
|
68 |
||
69 |
fun check_modulename mn = |
|
70 |
let |
|
71 |
val mns = NameSpace.explode mn; |
|
72 |
val mns' = map (purify_name true) mns; |
|
73 |
in |
|
74 |
if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
|
75 |
^ "perhaps try " ^ quote (NameSpace.implode mns')) |
|
76 |
end; |
|
77 |
||
78 |
||
79 |
(** variable name contexts **) |
|
80 |
||
81 |
type var_ctxt = string Symtab.table * Name.context; |
|
82 |
||
83 |
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty, |
|
84 |
Name.make_context names); |
|
85 |
||
86 |
fun intro_vars names (namemap, namectxt) = |
|
87 |
let |
|
88 |
val (names', namectxt') = Name.variants names namectxt; |
|
89 |
val namemap' = fold2 (curry Symtab.update) names names' namemap; |
|
90 |
in (namemap', namectxt') end; |
|
91 |
||
92 |
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name |
|
93 |
of SOME name' => name' |
|
94 |
| NONE => error ("Invalid name in context: " ^ quote name); |
|
95 |
||
96 |
||
97 |
||
98 |
(** global names (identifiers) **) |
|
99 |
||
100 |
(* identifier categories *) |
|
101 |
||
24811 | 102 |
val suffix_class = "class"; |
103 |
val suffix_classrel = "clsrel" |
|
104 |
val suffix_tyco = "tyco"; |
|
105 |
val suffix_instance = "inst"; |
|
106 |
val suffix_const = "const"; |
|
24219 | 107 |
|
108 |
fun string_of_classrel (class, superclass) = class ^ " < " ^ superclass; |
|
109 |
fun string_of_instance (class, tyco) = tyco ^ " :: " ^ class; |
|
110 |
||
24811 | 111 |
fun add_suffix nsp name = |
24219 | 112 |
NameSpace.append name nsp; |
113 |
||
24811 | 114 |
fun dest_suffix nsp name = |
24219 | 115 |
if NameSpace.base name = nsp |
116 |
then SOME (NameSpace.qualifier name) |
|
117 |
else NONE; |
|
118 |
||
119 |
local |
|
120 |
||
121 |
val name_mapping = [ |
|
24811 | 122 |
(suffix_class, "class"), |
123 |
(suffix_classrel, "subclass relation"), |
|
124 |
(suffix_tyco, "type constructor"), |
|
125 |
(suffix_instance, "instance"), |
|
126 |
(suffix_const, "constant") |
|
24219 | 127 |
] |
128 |
||
129 |
in |
|
130 |
||
131 |
val category_of = the o AList.lookup (op =) name_mapping o NameSpace.base; |
|
132 |
||
133 |
end; |
|
134 |
||
135 |
||
136 |
(* theory name lookup *) |
|
137 |
||
138 |
fun thyname_of thy f errmsg x = |
|
139 |
let |
|
140 |
fun thy_of thy = |
|
141 |
if f thy x then case get_first thy_of (Theory.parents_of thy) |
|
142 |
of NONE => SOME thy |
|
143 |
| thy => thy |
|
144 |
else NONE; |
|
145 |
in case thy_of thy |
|
146 |
of SOME thy => Context.theory_name thy |
|
147 |
| NONE => error (errmsg x) end; |
|
148 |
||
149 |
fun thyname_of_class thy = |
|
150 |
thyname_of thy (fn thy => member (op =) (Sign.all_classes thy)) |
|
151 |
(fn class => "thyname_of_class: no such class: " ^ quote class); |
|
152 |
||
153 |
fun thyname_of_tyco thy = |
|
154 |
thyname_of thy Sign.declared_tyname |
|
155 |
(fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco); |
|
156 |
||
157 |
fun thyname_of_instance thy = |
|
158 |
let |
|
159 |
fun test_instance thy (class, tyco) = |
|
160 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
|
161 |
in thyname_of thy test_instance |
|
162 |
(fn (class, tyco) => "thyname_of_instance: no such instance: " |
|
163 |
^ (quote o string_of_instance) (class, tyco)) |
|
164 |
end; |
|
165 |
||
166 |
fun thyname_of_const thy = |
|
167 |
thyname_of thy Sign.declared_const |
|
168 |
(fn c => "thyname_of_const: no such constant: " ^ quote c); |
|
169 |
||
170 |
||
171 |
(* naming policies *) |
|
172 |
||
173 |
val purify_prefix = |
|
174 |
explode |
|
175 |
(*should disappear as soon as hierarchical theory name spaces are available*) |
|
176 |
#> Symbol.scanner "Malformed name" |
|
177 |
(Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.is_regular)) |
|
178 |
#> implode |
|
179 |
#> NameSpace.explode |
|
180 |
#> map (purify_name true); |
|
181 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
182 |
fun purify_base "op &" = "and" |
24219 | 183 |
| purify_base "op |" = "or" |
184 |
| purify_base "op -->" = "implies" |
|
185 |
| purify_base "{}" = "empty" |
|
186 |
| purify_base "op :" = "member" |
|
187 |
| purify_base "op Int" = "intersect" |
|
188 |
| purify_base "op Un" = "union" |
|
189 |
| purify_base "*" = "product" |
|
190 |
| purify_base "+" = "sum" |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
191 |
| purify_base s = if String.isPrefix "op =" s |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
192 |
then "eq" ^ purify_name false s |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
193 |
else purify_name false s; |
24219 | 194 |
|
195 |
fun default_policy thy get_basename get_thyname name = |
|
196 |
let |
|
197 |
val prefix = (purify_prefix o get_thyname thy) name; |
|
198 |
val base = (purify_base o get_basename) name; |
|
199 |
in NameSpace.implode (prefix @ [base]) end; |
|
200 |
||
201 |
fun class_policy thy = default_policy thy NameSpace.base thyname_of_class; |
|
202 |
fun classrel_policy thy = default_policy thy (fn (class1, class2) => |
|
24811 | 203 |
NameSpace.base class2 ^ "_" ^ NameSpace.base class1) (fn thy => thyname_of_class thy o fst); |
24219 | 204 |
(*order fits nicely with composed projections*) |
205 |
fun tyco_policy thy = default_policy thy NameSpace.base thyname_of_tyco; |
|
206 |
fun instance_policy thy = default_policy thy (fn (class, tyco) => |
|
207 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
|
208 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
209 |
fun force_thyname thy c = case Code.get_datatype_of_constr thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
210 |
of SOME dtco => SOME (thyname_of_tyco thy dtco) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
211 |
| NONE => (case AxClass.class_of_param thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
212 |
of SOME class => SOME (thyname_of_class thy class) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
213 |
| NONE => (case Class.param_const thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
214 |
of SOME (c, tyco) => SOME (thyname_of_instance thy |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
215 |
((the o AxClass.class_of_param thy) c, tyco)) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
216 |
| NONE => NONE)); |
24219 | 217 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
218 |
fun const_policy thy c = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
219 |
case force_thyname thy c |
24219 | 220 |
of NONE => default_policy thy NameSpace.base thyname_of_const c |
221 |
| SOME thyname => let |
|
222 |
val prefix = purify_prefix thyname; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
223 |
val base = (purify_base o NameSpace.base) c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
224 |
in NameSpace.implode (prefix @ [base]) end; |
24219 | 225 |
|
226 |
||
227 |
(* theory and code data *) |
|
228 |
||
229 |
type tyco = string; |
|
230 |
type const = string; |
|
231 |
||
232 |
structure StringPairTab = |
|
233 |
TableFun( |
|
234 |
type key = string * string; |
|
235 |
val ord = prod_ord fast_string_ord fast_string_ord; |
|
236 |
); |
|
237 |
||
238 |
datatype names = Names of { |
|
239 |
class: class Symtab.table * class Symtab.table, |
|
240 |
classrel: string StringPairTab.table * (class * class) Symtab.table, |
|
241 |
tyco: tyco Symtab.table * tyco Symtab.table, |
|
242 |
instance: string StringPairTab.table * (class * tyco) Symtab.table |
|
243 |
} |
|
244 |
||
245 |
val empty_names = Names { |
|
246 |
class = (Symtab.empty, Symtab.empty), |
|
247 |
classrel = (StringPairTab.empty, Symtab.empty), |
|
248 |
tyco = (Symtab.empty, Symtab.empty), |
|
249 |
instance = (StringPairTab.empty, Symtab.empty) |
|
250 |
}; |
|
251 |
||
252 |
local |
|
253 |
fun mk_names (class, classrel, tyco, instance) = |
|
254 |
Names { class = class, classrel = classrel, tyco = tyco, instance = instance }; |
|
255 |
fun map_names f (Names { class, classrel, tyco, instance }) = |
|
256 |
mk_names (f (class, classrel, tyco, instance)); |
|
257 |
in |
|
258 |
fun merge_names (Names { class = (class1, classrev1), |
|
259 |
classrel = (classrel1, classrelrev1), tyco = (tyco1, tycorev1), |
|
260 |
instance = (instance1, instancerev1) }, |
|
261 |
Names { class = (class2, classrev2), |
|
262 |
classrel = (classrel2, classrelrev2), tyco = (tyco2, tycorev2), |
|
263 |
instance = (instance2, instancerev2) }) = |
|
264 |
mk_names ((Symtab.merge (op =) (class1, class2), Symtab.merge (op =) (classrev1, classrev2)), |
|
265 |
(StringPairTab.merge (op =) (classrel1, classrel2), Symtab.merge (op =) (classrelrev1, classrelrev2)), |
|
266 |
(Symtab.merge (op =) (tyco1, tyco2), Symtab.merge (op =) (tycorev1, tycorev2)), |
|
267 |
(StringPairTab.merge (op =) (instance1, instance2), Symtab.merge (op =) (instancerev1, instancerev2))); |
|
268 |
fun map_class f = map_names |
|
269 |
(fn (class, classrel, tyco, inst) => (f class, classrel, tyco, inst)); |
|
270 |
fun map_classrel f = map_names |
|
271 |
(fn (class, classrel, tyco, inst) => (class, f classrel, tyco, inst)); |
|
272 |
fun map_tyco f = map_names |
|
273 |
(fn (class, classrel, tyco, inst) => (class, classrel, f tyco, inst)); |
|
274 |
fun map_instance f = map_names |
|
275 |
(fn (class, classrel, tyco, inst) => (class, classrel, tyco, f inst)); |
|
276 |
end; (*local*) |
|
277 |
||
278 |
structure CodeName = TheoryDataFun |
|
279 |
( |
|
280 |
type T = names ref; |
|
281 |
val empty = ref empty_names; |
|
282 |
fun copy (ref names) = ref names; |
|
283 |
val extend = copy; |
|
284 |
fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2)); |
|
285 |
); |
|
286 |
||
287 |
structure ConstName = CodeDataFun |
|
288 |
( |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
289 |
type T = const Symtab.table * string Symtab.table; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
290 |
val empty = (Symtab.empty, Symtab.empty); |
24219 | 291 |
fun merge _ ((const1, constrev1), (const2, constrev2)) : T = |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
292 |
(Symtab.merge (op =) (const1, const2), |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
293 |
Symtab.merge (op =) (constrev1, constrev2)); |
24219 | 294 |
fun purge _ NONE _ = empty |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
295 |
| purge _ (SOME cs) (const, constrev) = (fold Symtab.delete_safe cs const, |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
296 |
fold Symtab.delete_safe (map_filter (Symtab.lookup const) cs) constrev); |
24219 | 297 |
); |
298 |
||
299 |
val setup = CodeName.init; |
|
300 |
||
301 |
||
302 |
(* forward lookup with cache update *) |
|
303 |
||
304 |
fun get thy get_tabs get upd_names upd policy x = |
|
305 |
let |
|
306 |
val names_ref = CodeName.get thy; |
|
307 |
val (Names names) = ! names_ref; |
|
308 |
val tabs = get_tabs names; |
|
309 |
fun declare name = |
|
310 |
let |
|
311 |
val names' = upd_names (K (upd (x, name) (fst tabs), |
|
312 |
Symtab.update_new (name, x) (snd tabs))) (Names names) |
|
313 |
in (names_ref := names'; name) end; |
|
314 |
in case get (fst tabs) x |
|
315 |
of SOME name => name |
|
316 |
| NONE => |
|
317 |
x |
|
318 |
|> policy thy |
|
319 |
|> Name.variant (Symtab.keys (snd tabs)) |
|
320 |
|> declare |
|
321 |
end; |
|
322 |
||
323 |
fun get_const thy const = |
|
324 |
let |
|
325 |
val tabs = ConstName.get thy; |
|
326 |
fun declare name = |
|
327 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
328 |
val names' = (Symtab.update (const, name) (fst tabs), |
24219 | 329 |
Symtab.update_new (name, const) (snd tabs)) |
330 |
in (ConstName.change thy (K names'); name) end; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
331 |
in case Symtab.lookup (fst tabs) const |
24219 | 332 |
of SOME name => name |
333 |
| NONE => |
|
334 |
const |
|
335 |
|> const_policy thy |
|
336 |
|> Name.variant (Symtab.keys (snd tabs)) |
|
337 |
|> declare |
|
338 |
end; |
|
339 |
||
340 |
||
341 |
(* backward lookup *) |
|
342 |
||
343 |
fun rev thy get_tabs name = |
|
344 |
let |
|
345 |
val names_ref = CodeName.get thy |
|
346 |
val (Names names) = ! names_ref; |
|
347 |
val tab = (snd o get_tabs) names; |
|
348 |
in case Symtab.lookup tab name |
|
349 |
of SOME x => x |
|
350 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
|
351 |
end; |
|
352 |
||
353 |
fun rev_const thy name = |
|
354 |
let |
|
355 |
val tab = snd (ConstName.get thy); |
|
356 |
in case Symtab.lookup tab name |
|
357 |
of SOME const => const |
|
358 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
|
359 |
end; |
|
360 |
||
361 |
||
362 |
(* external interfaces *) |
|
363 |
||
364 |
fun class thy = |
|
365 |
get thy #class Symtab.lookup map_class Symtab.update class_policy |
|
24811 | 366 |
#> add_suffix suffix_class; |
24219 | 367 |
fun classrel thy = |
368 |
get thy #classrel StringPairTab.lookup map_classrel StringPairTab.update classrel_policy |
|
24811 | 369 |
#> add_suffix suffix_classrel; |
24219 | 370 |
fun tyco thy = |
371 |
get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy |
|
24811 | 372 |
#> add_suffix suffix_tyco; |
24219 | 373 |
fun instance thy = |
374 |
get thy #instance StringPairTab.lookup map_instance StringPairTab.update instance_policy |
|
24811 | 375 |
#> add_suffix suffix_instance; |
24219 | 376 |
fun const thy = |
377 |
get_const thy |
|
24811 | 378 |
#> add_suffix suffix_const; |
24219 | 379 |
|
380 |
fun class_rev thy = |
|
24811 | 381 |
dest_suffix suffix_class |
24219 | 382 |
#> Option.map (rev thy #class); |
383 |
fun classrel_rev thy = |
|
24811 | 384 |
dest_suffix suffix_classrel |
24219 | 385 |
#> Option.map (rev thy #classrel); |
386 |
fun tyco_rev thy = |
|
24811 | 387 |
dest_suffix suffix_tyco |
24219 | 388 |
#> Option.map (rev thy #tyco); |
389 |
fun instance_rev thy = |
|
24811 | 390 |
dest_suffix suffix_instance |
24219 | 391 |
#> Option.map (rev thy #instance); |
392 |
fun const_rev thy = |
|
24811 | 393 |
dest_suffix suffix_const |
24219 | 394 |
#> Option.map (rev_const thy); |
395 |
||
396 |
local |
|
397 |
||
398 |
val f_mapping = [ |
|
24811 | 399 |
(suffix_class, class_rev), |
400 |
(suffix_classrel, Option.map string_of_classrel oo classrel_rev), |
|
401 |
(suffix_tyco, tyco_rev), |
|
402 |
(suffix_instance, Option.map string_of_instance oo instance_rev), |
|
403 |
(suffix_const, fn thy => Option.map (CodeUnit.string_of_const thy) o const_rev thy) |
|
24219 | 404 |
]; |
405 |
||
406 |
in |
|
407 |
||
24811 | 408 |
fun labelled_name thy suffix_name = |
24219 | 409 |
let |
24811 | 410 |
val category = category_of suffix_name; |
411 |
val name = NameSpace.qualifier suffix_name; |
|
412 |
val suffix = NameSpace.base suffix_name |
|
413 |
in case (the o AList.lookup (op =) f_mapping) suffix thy suffix_name |
|
24219 | 414 |
of SOME thing => category ^ " " ^ quote thing |
415 |
| NONE => error ("Unknown name: " ^ quote name) |
|
416 |
end; |
|
417 |
||
418 |
end; |
|
419 |
||
420 |
end; |