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