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