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