author | haftmann |
Sat, 10 Feb 2007 09:26:24 +0100 | |
changeset 22304 | ba3d6b76a627 |
parent 22197 | 461130ccfef4 |
child 22427 | 69ce2cb9e3e5 |
permissions | -rw-r--r-- |
20353 | 1 |
(* Title: Pure/Tools/codegen_names.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
21461 | 5 |
Naming policies for code generation: prefixing any name by corresponding theory name, |
22304 | 6 |
conversion to alphanumeric representation. |
20855 | 7 |
Mappings are incrementally cached. |
20353 | 8 |
*) |
9 |
||
10 |
signature CODEGEN_NAMES = |
|
11 |
sig |
|
22304 | 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 |
||
20386 | 20 |
type tyco = string |
21 |
type const = string |
|
20353 | 22 |
val class: theory -> class -> class |
20697 | 23 |
val class_rev: theory -> class -> class option |
21927 | 24 |
val classrel: theory -> class * class -> string |
25 |
val classrel_rev: theory -> string -> (class * class) option |
|
20353 | 26 |
val tyco: theory -> tyco -> tyco |
20697 | 27 |
val tyco_rev: theory -> tyco -> tyco option |
20353 | 28 |
val instance: theory -> class * tyco -> string |
20697 | 29 |
val instance_rev: theory -> string -> (class * tyco) option |
20585 | 30 |
val const: theory -> CodegenConsts.const -> const |
20697 | 31 |
val const_rev: theory -> const -> CodegenConsts.const option |
22034 | 32 |
val labelled_name: theory -> string -> string |
20353 | 33 |
end; |
34 |
||
35 |
structure CodegenNames: CODEGEN_NAMES = |
|
36 |
struct |
|
37 |
||
22034 | 38 |
(** purification **) |
39 |
||
40 |
val purify_name = |
|
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.not_eof; |
|
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 |
in explode #> scan_valids #> space_implode "_" end; |
|
51 |
||
52 |
fun purify_op "0" = "zero" |
|
53 |
| purify_op "1" = "one" |
|
54 |
| purify_op s = |
|
55 |
let |
|
56 |
fun rep_op "+" = SOME "sum" |
|
57 |
| rep_op "-" = SOME "diff" |
|
58 |
| rep_op "*" = SOME "prod" |
|
59 |
| rep_op "/" = SOME "quotient" |
|
60 |
| rep_op "&" = SOME "conj" |
|
61 |
| rep_op "|" = SOME "disj" |
|
62 |
| rep_op "=" = SOME "eq" |
|
63 |
| rep_op "~" = SOME "inv" |
|
64 |
| rep_op "@" = SOME "append" |
|
65 |
| rep_op s = NONE; |
|
66 |
val scan_valids = Symbol.scanner "Malformed input" |
|
67 |
(Scan.repeat (Scan.some rep_op || Scan.one Symbol.not_eof)); |
|
68 |
in (explode #> scan_valids #> implode) s end; |
|
69 |
||
70 |
val purify_lower = |
|
71 |
explode |
|
72 |
#> (fn cs => (if forall Symbol.is_ascii_upper cs |
|
73 |
then map else nth_map 0) Symbol.to_ascii_lower cs) |
|
74 |
#> implode; |
|
75 |
||
76 |
val purify_upper = explode #> nth_map 0 Symbol.to_ascii_upper #> implode; |
|
77 |
||
22304 | 78 |
fun purify_var "" = "x" |
79 |
| purify_var v = (purify_name #> purify_lower) v; |
|
80 |
||
81 |
fun purify_tvar "" = "'a" |
|
82 |
| purify_tvar v = |
|
83 |
(unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v; |
|
84 |
||
85 |
fun check_modulename mn = |
|
86 |
let |
|
87 |
val mns = NameSpace.explode mn; |
|
88 |
val mns' = map purify_upper mns; |
|
89 |
in |
|
90 |
if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
|
91 |
^ "perhaps try " ^ quote (NameSpace.implode mns')) |
|
92 |
end; |
|
93 |
||
94 |
||
95 |
(** variable name contexts **) |
|
96 |
||
97 |
type var_ctxt = string Symtab.table * Name.context; |
|
98 |
||
99 |
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty, |
|
100 |
Name.make_context names); |
|
101 |
||
102 |
fun intro_vars names (namemap, namectxt) = |
|
103 |
let |
|
104 |
val (names', namectxt') = Name.variants names namectxt; |
|
105 |
val namemap' = fold2 (curry Symtab.update) names names' namemap; |
|
106 |
in (namemap', namectxt') end; |
|
107 |
||
108 |
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name |
|
109 |
of SOME name' => name' |
|
110 |
| NONE => error ("Invalid name in context: " ^ quote name); |
|
111 |
||
112 |
||
113 |
(** identifier categories **) |
|
114 |
||
115 |
val idf_class = "class"; |
|
116 |
val idf_classrel = "clsrel" |
|
117 |
val idf_tyco = "tyco"; |
|
118 |
val idf_instance = "inst"; |
|
119 |
val idf_const = "const"; |
|
120 |
||
121 |
fun string_of_classrel (class, superclass) = class ^ " < " ^ superclass; |
|
122 |
fun string_of_instance (class, tyco) = tyco ^ " :: " ^ class; |
|
123 |
||
124 |
fun add_idf nsp name = |
|
125 |
NameSpace.append name nsp; |
|
126 |
||
127 |
fun dest_idf nsp name = |
|
128 |
if NameSpace.base name = nsp |
|
129 |
then SOME (NameSpace.qualifier name) |
|
130 |
else NONE; |
|
131 |
||
132 |
local |
|
133 |
||
134 |
val name_mapping = [ |
|
135 |
(idf_class, "class"), |
|
136 |
(idf_classrel, "subclass relation"), |
|
137 |
(idf_tyco, "type constructor"), |
|
138 |
(idf_instance, "instance"), |
|
139 |
(idf_const, "constant") |
|
140 |
] |
|
141 |
||
142 |
in |
|
143 |
||
144 |
val category_of = the o AList.lookup (op =) name_mapping o NameSpace.base; |
|
145 |
||
146 |
end; |
|
147 |
||
22034 | 148 |
|
149 |
(** global names (identifiers) **) |
|
20353 | 150 |
|
151 |
(* theory data *) |
|
152 |
||
153 |
type tyco = string; |
|
154 |
type const = string; |
|
21927 | 155 |
val string_pair_ord = prod_ord fast_string_ord fast_string_ord; |
156 |
val eq_string_pair = is_equal o string_pair_ord; |
|
20386 | 157 |
structure Consttab = CodegenConsts.Consttab; |
20353 | 158 |
|
21927 | 159 |
structure StringPairTab = |
20353 | 160 |
TableFun( |
21927 | 161 |
type key = string * string; |
162 |
val ord = string_pair_ord; |
|
20353 | 163 |
); |
164 |
||
165 |
datatype names = Names of { |
|
166 |
class: class Symtab.table * class Symtab.table, |
|
21927 | 167 |
classrel: string StringPairTab.table * (class * class) Symtab.table, |
20353 | 168 |
tyco: tyco Symtab.table * tyco Symtab.table, |
21927 | 169 |
instance: string StringPairTab.table * (class * tyco) Symtab.table, |
20353 | 170 |
const: const Consttab.table * (string * typ list) Symtab.table |
171 |
} |
|
172 |
||
173 |
val empty_names = Names { |
|
174 |
class = (Symtab.empty, Symtab.empty), |
|
21927 | 175 |
classrel = (StringPairTab.empty, Symtab.empty), |
20353 | 176 |
tyco = (Symtab.empty, Symtab.empty), |
21927 | 177 |
instance = (StringPairTab.empty, Symtab.empty), |
20353 | 178 |
const = (Consttab.empty, Symtab.empty) |
179 |
}; |
|
180 |
||
181 |
local |
|
21927 | 182 |
fun mk_names (class, classrel, tyco, instance, const) = |
183 |
Names { class = class, classrel = classrel, tyco = tyco, instance = instance, const = const}; |
|
184 |
fun map_names f (Names { class, classrel, tyco, instance, const }) = |
|
185 |
mk_names (f (class, classrel, tyco, instance, const)); |
|
20353 | 186 |
val eq_string = op = : string * string -> bool; |
187 |
in |
|
21927 | 188 |
fun merge_names (Names { class = (class1, classrev1), |
189 |
classrel = (classrel1, classrelrev1), tyco = (tyco1, tycorev1), |
|
20353 | 190 |
instance = (instance1, instancerev1), const = (const1, constrev1) }, |
21927 | 191 |
Names { class = (class2, classrev2), |
192 |
classrel = (classrel2, classrelrev2), tyco = (tyco2, tycorev2), |
|
20353 | 193 |
instance = (instance2, instancerev2), const = (const2, constrev2) }) = |
194 |
mk_names ((Symtab.merge eq_string (class1, class2), Symtab.merge eq_string (classrev1, classrev2)), |
|
21927 | 195 |
(StringPairTab.merge eq_string (classrel1, classrel2), Symtab.merge eq_string_pair (classrelrev1, classrelrev2)), |
20353 | 196 |
(Symtab.merge eq_string (tyco1, tyco2), Symtab.merge eq_string (tycorev1, tycorev2)), |
21927 | 197 |
(StringPairTab.merge eq_string (instance1, instance2), Symtab.merge eq_string_pair (instancerev1, instancerev2)), |
20386 | 198 |
(Consttab.merge eq_string (const1, const2), Symtab.merge CodegenConsts.eq_const (constrev1, constrev2))); |
20353 | 199 |
fun map_class f = map_names |
21927 | 200 |
(fn (class, classrel, tyco, inst, const) => (f class, classrel, tyco, inst, const)); |
201 |
fun map_classrel f = map_names |
|
202 |
(fn (class, classrel, tyco, inst, const) => (class, f classrel, tyco, inst, const)); |
|
20353 | 203 |
fun map_tyco f = map_names |
21927 | 204 |
(fn (class, classrel, tyco, inst, const) => (class, classrel, f tyco, inst, const)); |
22304 | 205 |
fun map_instance f = map_names |
21927 | 206 |
(fn (class, classrel, tyco, inst, const) => (class, classrel, tyco, f inst, const)); |
20353 | 207 |
fun map_const f = map_names |
21927 | 208 |
(fn (class, classrel, tyco, inst, const) => (class, classrel, tyco, inst, f const)); |
20353 | 209 |
|
210 |
end; (*local*) |
|
211 |
||
20456 | 212 |
structure CodeName = TheoryDataFun |
20353 | 213 |
(struct |
214 |
val name = "Pure/codegen_names"; |
|
215 |
type T = names ref; |
|
216 |
val empty = ref empty_names; |
|
217 |
fun copy (ref names) = ref names; |
|
218 |
val extend = copy; |
|
219 |
fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2)); |
|
220 |
fun print _ _ = (); |
|
221 |
end); |
|
222 |
||
20456 | 223 |
val _ = Context.add_setup CodeName.init; |
20353 | 224 |
|
225 |
||
226 |
(* forward lookup with cache update *) |
|
227 |
||
228 |
fun get thy get_tabs get upd_names upd policy x = |
|
229 |
let |
|
20456 | 230 |
val names_ref = CodeName.get thy |
20353 | 231 |
val (Names names) = ! names_ref; |
232 |
val tabs = get_tabs names; |
|
233 |
fun declare name = |
|
234 |
let |
|
235 |
val names' = upd_names (K (upd (x, name) (fst tabs), |
|
236 |
Symtab.update_new (name, x) (snd tabs))) (Names names) |
|
237 |
in (names_ref := names'; name) end; |
|
238 |
in case get (fst tabs) x |
|
239 |
of SOME name => name |
|
21338 | 240 |
| NONE => |
241 |
x |
|
242 |
|> policy thy |
|
243 |
|> Name.variant (Symtab.keys (snd tabs)) |
|
244 |
|> declare |
|
20353 | 245 |
end; |
246 |
||
247 |
||
248 |
(* backward lookup *) |
|
249 |
||
22304 | 250 |
fun rev thy get_tabs name = |
20353 | 251 |
let |
20456 | 252 |
val names_ref = CodeName.get thy |
20353 | 253 |
val (Names names) = ! names_ref; |
254 |
val tab = (snd o get_tabs) names; |
|
255 |
in case Symtab.lookup tab name |
|
256 |
of SOME x => x |
|
22304 | 257 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
20353 | 258 |
end; |
259 |
||
260 |
||
261 |
(* theory name lookup *) |
|
262 |
||
263 |
fun thyname_of thy f errmsg x = |
|
264 |
let |
|
265 |
fun thy_of thy = |
|
266 |
if f thy x then case get_first thy_of (Theory.parents_of thy) |
|
267 |
of NONE => SOME thy |
|
268 |
| thy => thy |
|
269 |
else NONE; |
|
270 |
in case thy_of thy |
|
271 |
of SOME thy => Context.theory_name thy |
|
272 |
| NONE => error (errmsg x) end; |
|
273 |
||
274 |
fun thyname_of_class thy = |
|
21931
314f9e2a442c
replaced Sign.classes by Sign.all_classes (topologically sorted);
wenzelm
parents:
21927
diff
changeset
|
275 |
thyname_of thy (fn thy => member (op =) (Sign.all_classes thy)) |
20353 | 276 |
(fn class => "thyname_of_class: no such class: " ^ quote class); |
277 |
||
21927 | 278 |
fun thyname_of_classrel thy = |
279 |
thyname_of thy (fn thy => fn (class1, class2) => Sign.subsort thy ([class1], [class2])) |
|
22304 | 280 |
(fn (class1, class2) => "thyname_of_classrel: no such subclass relation: " |
281 |
^ (quote o string_of_classrel) (class1, class2)); |
|
21927 | 282 |
|
20353 | 283 |
fun thyname_of_tyco thy = |
284 |
thyname_of thy Sign.declared_tyname |
|
285 |
(fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco); |
|
286 |
||
287 |
fun thyname_of_instance thy = |
|
288 |
let |
|
289 |
fun test_instance thy (class, tyco) = |
|
290 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
|
291 |
in thyname_of thy test_instance |
|
22304 | 292 |
(fn (class, tyco) => "thyname_of_instance: no such instance: " |
293 |
^ (quote o string_of_instance) (class, tyco)) |
|
20353 | 294 |
end; |
295 |
||
296 |
fun thyname_of_const thy = |
|
297 |
thyname_of thy Sign.declared_const |
|
298 |
(fn c => "thyname_of_const: no such constant: " ^ quote c); |
|
299 |
||
300 |
||
22034 | 301 |
(* naming policies *) |
20353 | 302 |
|
303 |
val purify_idf = purify_op #> purify_name; |
|
304 |
val purify_prefix = map (purify_idf #> purify_upper); |
|
305 |
val purify_base = purify_idf #> purify_lower; |
|
306 |
||
21927 | 307 |
val dotify = |
308 |
explode |
|
309 |
(*should disappear as soon as hierarchical theory name spaces are available*) |
|
310 |
#> Symbol.scanner "Malformed name" |
|
311 |
(Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.not_eof)) |
|
312 |
#> implode; |
|
313 |
||
20353 | 314 |
fun policy thy get_basename get_thyname name = |
315 |
let |
|
21927 | 316 |
val prefix = (purify_prefix o NameSpace.explode o dotify o get_thyname thy) name; |
20353 | 317 |
val base = (purify_base o get_basename) name; |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
318 |
in NameSpace.implode (prefix @ [base]) end; |
20353 | 319 |
|
320 |
fun class_policy thy = policy thy NameSpace.base thyname_of_class; |
|
21927 | 321 |
fun classrel_policy thy = policy thy (fn (class1, class2) => |
22075 | 322 |
NameSpace.base class1 ^ "_" ^ NameSpace.base class2) thyname_of_classrel; |
20353 | 323 |
fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco; |
324 |
fun instance_policy thy = policy thy (fn (class, tyco) => |
|
325 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
|
326 |
||
21461 | 327 |
fun force_thyname thy (const as (c, tys)) = |
20585 | 328 |
case AxClass.class_of_param thy c |
329 |
of SOME class => (case tys |
|
330 |
of [Type (tyco, _)] => SOME (thyname_of_instance thy (class, tyco)) |
|
21461 | 331 |
| _ => SOME (thyname_of_class thy class)) |
332 |
| NONE => (case CodegenData.get_datatype_of_constr thy const |
|
333 |
of SOME dtco => SOME (thyname_of_tyco thy dtco) |
|
334 |
| NONE => (case CodegenConsts.find_def thy const |
|
22185 | 335 |
of SOME (thyname, _) => SOME thyname |
21461 | 336 |
| NONE => NONE)); |
20386 | 337 |
|
20353 | 338 |
fun const_policy thy (c, tys) = |
20386 | 339 |
case force_thyname thy (c, tys) |
20353 | 340 |
of NONE => policy thy NameSpace.base thyname_of_const c |
341 |
| SOME thyname => let |
|
21927 | 342 |
val prefix = (purify_prefix o NameSpace.explode o dotify) thyname; |
20353 | 343 |
val tycos = map_filter (fn Type (tyco, _) => SOME tyco | _ => NONE) tys; |
20386 | 344 |
val base = map (purify_base o NameSpace.base) (c :: tycos); |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
345 |
in NameSpace.implode (prefix @ [space_implode "_" base]) end; |
20353 | 346 |
|
347 |
||
348 |
(* external interfaces *) |
|
349 |
||
350 |
fun class thy = |
|
20697 | 351 |
get thy #class Symtab.lookup map_class Symtab.update class_policy |
22304 | 352 |
#> add_idf idf_class; |
21927 | 353 |
fun classrel thy = |
354 |
get thy #classrel StringPairTab.lookup map_classrel StringPairTab.update classrel_policy |
|
22304 | 355 |
#> add_idf idf_classrel; |
20353 | 356 |
fun tyco thy = |
20697 | 357 |
get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy |
22304 | 358 |
#> add_idf idf_tyco; |
20353 | 359 |
fun instance thy = |
22304 | 360 |
get thy #instance StringPairTab.lookup map_instance StringPairTab.update instance_policy |
361 |
#> add_idf idf_instance; |
|
21915 | 362 |
fun const thy = |
363 |
CodegenConsts.norm thy |
|
364 |
#> get thy #const Consttab.lookup map_const Consttab.update const_policy |
|
22304 | 365 |
#> add_idf idf_const; |
20353 | 366 |
|
20697 | 367 |
fun class_rev thy = |
22304 | 368 |
dest_idf idf_class |
369 |
#> Option.map (rev thy #class); |
|
21927 | 370 |
fun classrel_rev thy = |
22304 | 371 |
dest_idf idf_classrel |
372 |
#> Option.map (rev thy #classrel); |
|
20697 | 373 |
fun tyco_rev thy = |
22304 | 374 |
dest_idf idf_tyco |
375 |
#> Option.map (rev thy #tyco); |
|
20697 | 376 |
fun instance_rev thy = |
22304 | 377 |
dest_idf idf_instance |
378 |
#> Option.map (rev thy #instance); |
|
21915 | 379 |
fun const_rev thy = |
22304 | 380 |
dest_idf idf_const |
381 |
#> Option.map (rev thy #const); |
|
382 |
||
383 |
local |
|
20353 | 384 |
|
22304 | 385 |
val f_mapping = [ |
386 |
(idf_class, class_rev), |
|
387 |
(idf_classrel, Option.map string_of_classrel oo classrel_rev), |
|
388 |
(idf_tyco, tyco_rev), |
|
389 |
(idf_instance, Option.map string_of_instance oo instance_rev), |
|
390 |
(idf_const, fn thy => Option.map (CodegenConsts.string_of_const thy) o const_rev thy) |
|
391 |
]; |
|
392 |
||
393 |
in |
|
394 |
||
395 |
fun labelled_name thy idf = |
|
22034 | 396 |
let |
22304 | 397 |
val category = category_of idf; |
398 |
val name = NameSpace.qualifier idf; |
|
399 |
val f = (the o AList.lookup (op =) f_mapping o NameSpace.base) idf |
|
400 |
in case f thy idf |
|
401 |
of SOME thing => category ^ " " ^ quote thing |
|
402 |
| NONE => error ("Unknown name: " ^ quote name) |
|
22034 | 403 |
end; |
404 |
||
22304 | 405 |
end; |
22034 | 406 |
|
20353 | 407 |
end; |