author | haftmann |
Tue, 25 Sep 2007 12:16:14 +0200 | |
changeset 24702 | f875049a13a1 |
parent 24423 | ae9cd0e92423 |
child 24811 | 3bf788a0c49a |
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 |
||
102 |
val idf_class = "class"; |
|
103 |
val idf_classrel = "clsrel" |
|
104 |
val idf_tyco = "tyco"; |
|
105 |
val idf_instance = "inst"; |
|
106 |
val idf_const = "const"; |
|
107 |
||
108 |
fun string_of_classrel (class, superclass) = class ^ " < " ^ superclass; |
|
109 |
fun string_of_instance (class, tyco) = tyco ^ " :: " ^ class; |
|
110 |
||
111 |
fun add_idf nsp name = |
|
112 |
NameSpace.append name nsp; |
|
113 |
||
114 |
fun dest_idf nsp name = |
|
115 |
if NameSpace.base name = nsp |
|
116 |
then SOME (NameSpace.qualifier name) |
|
117 |
else NONE; |
|
118 |
||
119 |
local |
|
120 |
||
121 |
val name_mapping = [ |
|
122 |
(idf_class, "class"), |
|
123 |
(idf_classrel, "subclass relation"), |
|
124 |
(idf_tyco, "type constructor"), |
|
125 |
(idf_instance, "instance"), |
|
126 |
(idf_const, "constant") |
|
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_classrel thy = |
|
154 |
thyname_of thy (fn thy => fn (class1, class2) => Sign.subsort thy ([class1], [class2])) |
|
155 |
(fn (class1, class2) => "thyname_of_classrel: no such subclass relation: " |
|
156 |
^ (quote o string_of_classrel) (class1, class2)); |
|
157 |
||
158 |
fun thyname_of_tyco thy = |
|
159 |
thyname_of thy Sign.declared_tyname |
|
160 |
(fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco); |
|
161 |
||
162 |
fun thyname_of_instance thy = |
|
163 |
let |
|
164 |
fun test_instance thy (class, tyco) = |
|
165 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
|
166 |
in thyname_of thy test_instance |
|
167 |
(fn (class, tyco) => "thyname_of_instance: no such instance: " |
|
168 |
^ (quote o string_of_instance) (class, tyco)) |
|
169 |
end; |
|
170 |
||
171 |
fun thyname_of_const thy = |
|
172 |
thyname_of thy Sign.declared_const |
|
173 |
(fn c => "thyname_of_const: no such constant: " ^ quote c); |
|
174 |
||
175 |
||
176 |
(* naming policies *) |
|
177 |
||
178 |
val purify_prefix = |
|
179 |
explode |
|
180 |
(*should disappear as soon as hierarchical theory name spaces are available*) |
|
181 |
#> Symbol.scanner "Malformed name" |
|
182 |
(Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.is_regular)) |
|
183 |
#> implode |
|
184 |
#> NameSpace.explode |
|
185 |
#> map (purify_name true); |
|
186 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
187 |
fun purify_base "op &" = "and" |
24219 | 188 |
| purify_base "op |" = "or" |
189 |
| purify_base "op -->" = "implies" |
|
190 |
| purify_base "{}" = "empty" |
|
191 |
| purify_base "op :" = "member" |
|
192 |
| purify_base "op Int" = "intersect" |
|
193 |
| purify_base "op Un" = "union" |
|
194 |
| purify_base "*" = "product" |
|
195 |
| purify_base "+" = "sum" |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
196 |
| purify_base s = if String.isPrefix "op =" s |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
197 |
then "eq" ^ purify_name false s |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
198 |
else purify_name false s; |
24219 | 199 |
|
200 |
fun default_policy thy get_basename get_thyname name = |
|
201 |
let |
|
202 |
val prefix = (purify_prefix o get_thyname thy) name; |
|
203 |
val base = (purify_base o get_basename) name; |
|
204 |
in NameSpace.implode (prefix @ [base]) end; |
|
205 |
||
206 |
fun class_policy thy = default_policy thy NameSpace.base thyname_of_class; |
|
207 |
fun classrel_policy thy = default_policy thy (fn (class1, class2) => |
|
208 |
NameSpace.base class2 ^ "_" ^ NameSpace.base class1) thyname_of_classrel; |
|
209 |
(*order fits nicely with composed projections*) |
|
210 |
fun tyco_policy thy = default_policy thy NameSpace.base thyname_of_tyco; |
|
211 |
fun instance_policy thy = default_policy thy (fn (class, tyco) => |
|
212 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
|
213 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
214 |
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
|
215 |
of SOME dtco => SOME (thyname_of_tyco thy dtco) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
216 |
| NONE => (case AxClass.class_of_param thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
217 |
of SOME class => SOME (thyname_of_class thy class) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
218 |
| NONE => (case Class.param_const thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
219 |
of SOME (c, tyco) => SOME (thyname_of_instance thy |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
220 |
((the o AxClass.class_of_param thy) c, tyco)) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
221 |
| NONE => NONE)); |
24219 | 222 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
223 |
fun const_policy thy c = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
224 |
case force_thyname thy c |
24219 | 225 |
of NONE => default_policy thy NameSpace.base thyname_of_const c |
226 |
| SOME thyname => let |
|
227 |
val prefix = purify_prefix thyname; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
228 |
val base = (purify_base o NameSpace.base) c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
229 |
in NameSpace.implode (prefix @ [base]) end; |
24219 | 230 |
|
231 |
||
232 |
(* theory and code data *) |
|
233 |
||
234 |
type tyco = string; |
|
235 |
type const = string; |
|
236 |
||
237 |
structure StringPairTab = |
|
238 |
TableFun( |
|
239 |
type key = string * string; |
|
240 |
val ord = prod_ord fast_string_ord fast_string_ord; |
|
241 |
); |
|
242 |
||
243 |
datatype names = Names of { |
|
244 |
class: class Symtab.table * class Symtab.table, |
|
245 |
classrel: string StringPairTab.table * (class * class) Symtab.table, |
|
246 |
tyco: tyco Symtab.table * tyco Symtab.table, |
|
247 |
instance: string StringPairTab.table * (class * tyco) Symtab.table |
|
248 |
} |
|
249 |
||
250 |
val empty_names = Names { |
|
251 |
class = (Symtab.empty, Symtab.empty), |
|
252 |
classrel = (StringPairTab.empty, Symtab.empty), |
|
253 |
tyco = (Symtab.empty, Symtab.empty), |
|
254 |
instance = (StringPairTab.empty, Symtab.empty) |
|
255 |
}; |
|
256 |
||
257 |
local |
|
258 |
fun mk_names (class, classrel, tyco, instance) = |
|
259 |
Names { class = class, classrel = classrel, tyco = tyco, instance = instance }; |
|
260 |
fun map_names f (Names { class, classrel, tyco, instance }) = |
|
261 |
mk_names (f (class, classrel, tyco, instance)); |
|
262 |
in |
|
263 |
fun merge_names (Names { class = (class1, classrev1), |
|
264 |
classrel = (classrel1, classrelrev1), tyco = (tyco1, tycorev1), |
|
265 |
instance = (instance1, instancerev1) }, |
|
266 |
Names { class = (class2, classrev2), |
|
267 |
classrel = (classrel2, classrelrev2), tyco = (tyco2, tycorev2), |
|
268 |
instance = (instance2, instancerev2) }) = |
|
269 |
mk_names ((Symtab.merge (op =) (class1, class2), Symtab.merge (op =) (classrev1, classrev2)), |
|
270 |
(StringPairTab.merge (op =) (classrel1, classrel2), Symtab.merge (op =) (classrelrev1, classrelrev2)), |
|
271 |
(Symtab.merge (op =) (tyco1, tyco2), Symtab.merge (op =) (tycorev1, tycorev2)), |
|
272 |
(StringPairTab.merge (op =) (instance1, instance2), Symtab.merge (op =) (instancerev1, instancerev2))); |
|
273 |
fun map_class f = map_names |
|
274 |
(fn (class, classrel, tyco, inst) => (f class, classrel, tyco, inst)); |
|
275 |
fun map_classrel f = map_names |
|
276 |
(fn (class, classrel, tyco, inst) => (class, f classrel, tyco, inst)); |
|
277 |
fun map_tyco f = map_names |
|
278 |
(fn (class, classrel, tyco, inst) => (class, classrel, f tyco, inst)); |
|
279 |
fun map_instance f = map_names |
|
280 |
(fn (class, classrel, tyco, inst) => (class, classrel, tyco, f inst)); |
|
281 |
end; (*local*) |
|
282 |
||
283 |
structure CodeName = TheoryDataFun |
|
284 |
( |
|
285 |
type T = names ref; |
|
286 |
val empty = ref empty_names; |
|
287 |
fun copy (ref names) = ref names; |
|
288 |
val extend = copy; |
|
289 |
fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2)); |
|
290 |
); |
|
291 |
||
292 |
structure ConstName = CodeDataFun |
|
293 |
( |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
294 |
type T = const Symtab.table * string Symtab.table; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
295 |
val empty = (Symtab.empty, Symtab.empty); |
24219 | 296 |
fun merge _ ((const1, constrev1), (const2, constrev2)) : T = |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
297 |
(Symtab.merge (op =) (const1, const2), |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
298 |
Symtab.merge (op =) (constrev1, constrev2)); |
24219 | 299 |
fun purge _ NONE _ = empty |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
300 |
| purge _ (SOME cs) (const, constrev) = (fold Symtab.delete_safe cs const, |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
301 |
fold Symtab.delete_safe (map_filter (Symtab.lookup const) cs) constrev); |
24219 | 302 |
); |
303 |
||
304 |
val setup = CodeName.init; |
|
305 |
||
306 |
||
307 |
(* forward lookup with cache update *) |
|
308 |
||
309 |
fun get thy get_tabs get upd_names upd policy x = |
|
310 |
let |
|
311 |
val names_ref = CodeName.get thy; |
|
312 |
val (Names names) = ! names_ref; |
|
313 |
val tabs = get_tabs names; |
|
314 |
fun declare name = |
|
315 |
let |
|
316 |
val names' = upd_names (K (upd (x, name) (fst tabs), |
|
317 |
Symtab.update_new (name, x) (snd tabs))) (Names names) |
|
318 |
in (names_ref := names'; name) end; |
|
319 |
in case get (fst tabs) x |
|
320 |
of SOME name => name |
|
321 |
| NONE => |
|
322 |
x |
|
323 |
|> policy thy |
|
324 |
|> Name.variant (Symtab.keys (snd tabs)) |
|
325 |
|> declare |
|
326 |
end; |
|
327 |
||
328 |
fun get_const thy const = |
|
329 |
let |
|
330 |
val tabs = ConstName.get thy; |
|
331 |
fun declare name = |
|
332 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
333 |
val names' = (Symtab.update (const, name) (fst tabs), |
24219 | 334 |
Symtab.update_new (name, const) (snd tabs)) |
335 |
in (ConstName.change thy (K names'); name) end; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24219
diff
changeset
|
336 |
in case Symtab.lookup (fst tabs) const |
24219 | 337 |
of SOME name => name |
338 |
| NONE => |
|
339 |
const |
|
340 |
|> const_policy thy |
|
341 |
|> Name.variant (Symtab.keys (snd tabs)) |
|
342 |
|> declare |
|
343 |
end; |
|
344 |
||
345 |
||
346 |
(* backward lookup *) |
|
347 |
||
348 |
fun rev thy get_tabs name = |
|
349 |
let |
|
350 |
val names_ref = CodeName.get thy |
|
351 |
val (Names names) = ! names_ref; |
|
352 |
val tab = (snd o get_tabs) names; |
|
353 |
in case Symtab.lookup tab name |
|
354 |
of SOME x => x |
|
355 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
|
356 |
end; |
|
357 |
||
358 |
fun rev_const thy name = |
|
359 |
let |
|
360 |
val tab = snd (ConstName.get thy); |
|
361 |
in case Symtab.lookup tab name |
|
362 |
of SOME const => const |
|
363 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
|
364 |
end; |
|
365 |
||
366 |
||
367 |
(* external interfaces *) |
|
368 |
||
369 |
fun class thy = |
|
370 |
get thy #class Symtab.lookup map_class Symtab.update class_policy |
|
371 |
#> add_idf idf_class; |
|
372 |
fun classrel thy = |
|
373 |
get thy #classrel StringPairTab.lookup map_classrel StringPairTab.update classrel_policy |
|
374 |
#> add_idf idf_classrel; |
|
375 |
fun tyco thy = |
|
376 |
get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy |
|
377 |
#> add_idf idf_tyco; |
|
378 |
fun instance thy = |
|
379 |
get thy #instance StringPairTab.lookup map_instance StringPairTab.update instance_policy |
|
380 |
#> add_idf idf_instance; |
|
381 |
fun const thy = |
|
382 |
get_const thy |
|
383 |
#> add_idf idf_const; |
|
384 |
||
385 |
fun class_rev thy = |
|
386 |
dest_idf idf_class |
|
387 |
#> Option.map (rev thy #class); |
|
388 |
fun classrel_rev thy = |
|
389 |
dest_idf idf_classrel |
|
390 |
#> Option.map (rev thy #classrel); |
|
391 |
fun tyco_rev thy = |
|
392 |
dest_idf idf_tyco |
|
393 |
#> Option.map (rev thy #tyco); |
|
394 |
fun instance_rev thy = |
|
395 |
dest_idf idf_instance |
|
396 |
#> Option.map (rev thy #instance); |
|
397 |
fun const_rev thy = |
|
398 |
dest_idf idf_const |
|
399 |
#> Option.map (rev_const thy); |
|
400 |
||
401 |
local |
|
402 |
||
403 |
val f_mapping = [ |
|
404 |
(idf_class, class_rev), |
|
405 |
(idf_classrel, Option.map string_of_classrel oo classrel_rev), |
|
406 |
(idf_tyco, tyco_rev), |
|
407 |
(idf_instance, Option.map string_of_instance oo instance_rev), |
|
408 |
(idf_const, fn thy => Option.map (CodeUnit.string_of_const thy) o const_rev thy) |
|
409 |
]; |
|
410 |
||
411 |
in |
|
412 |
||
413 |
fun labelled_name thy idf = |
|
414 |
let |
|
415 |
val category = category_of idf; |
|
416 |
val name = NameSpace.qualifier idf; |
|
417 |
val f = (the o AList.lookup (op =) f_mapping o NameSpace.base) idf |
|
418 |
in case f thy idf |
|
419 |
of SOME thing => category ^ " " ^ quote thing |
|
420 |
| NONE => error ("Unknown name: " ^ quote name) |
|
421 |
end; |
|
422 |
||
423 |
end; |
|
424 |
||
425 |
end; |