author | aspinall |
Tue, 19 Dec 2006 17:35:33 +0100 | |
changeset 21886 | f1790ca921e1 |
parent 21880 | 0ce06e95982a |
child 21915 | 4e63c55f4cb4 |
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, |
20855 | 6 |
conversion to alphanumeric representation, shallow name spaces. |
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 |
20353 | 16 |
val tyco: theory -> tyco -> tyco |
20697 | 17 |
val tyco_rev: theory -> tyco -> tyco option |
20353 | 18 |
val instance: theory -> class * tyco -> string |
20697 | 19 |
val instance_rev: theory -> string -> (class * tyco) option |
20585 | 20 |
val const: theory -> CodegenConsts.const -> const |
20697 | 21 |
val const_rev: theory -> const -> CodegenConsts.const option |
20353 | 22 |
val purify_var: string -> string |
21014 | 23 |
val check_modulename: string -> string |
20697 | 24 |
val has_nsp: string -> string -> bool |
25 |
val nsp_class: string |
|
26 |
val nsp_tyco: string |
|
27 |
val nsp_inst: string |
|
28 |
val nsp_fun: string |
|
29 |
val nsp_dtco: string |
|
20353 | 30 |
end; |
31 |
||
32 |
structure CodegenNames: CODEGEN_NAMES = |
|
33 |
struct |
|
34 |
||
35 |
||
36 |
(* theory data *) |
|
37 |
||
38 |
type tyco = string; |
|
39 |
type const = string; |
|
40 |
val inst_ord = prod_ord fast_string_ord fast_string_ord; |
|
41 |
val eq_inst = is_equal o inst_ord; |
|
20386 | 42 |
structure Consttab = CodegenConsts.Consttab; |
20353 | 43 |
|
44 |
structure Insttab = |
|
45 |
TableFun( |
|
46 |
type key = class * tyco; |
|
47 |
val ord = inst_ord; |
|
48 |
); |
|
49 |
||
50 |
datatype names = Names of { |
|
51 |
class: class Symtab.table * class Symtab.table, |
|
52 |
tyco: tyco Symtab.table * tyco Symtab.table, |
|
53 |
instance: string Insttab.table * (class * tyco) Symtab.table, |
|
54 |
const: const Consttab.table * (string * typ list) Symtab.table |
|
55 |
} |
|
56 |
||
57 |
val empty_names = Names { |
|
58 |
class = (Symtab.empty, Symtab.empty), |
|
59 |
tyco = (Symtab.empty, Symtab.empty), |
|
60 |
instance = (Insttab.empty, Symtab.empty), |
|
61 |
const = (Consttab.empty, Symtab.empty) |
|
62 |
}; |
|
63 |
||
64 |
local |
|
65 |
fun mk_names (class, tyco, instance, const) = |
|
66 |
Names { class = class, tyco = tyco, instance = instance, const = const}; |
|
67 |
fun map_names f (Names { class, tyco, instance, const }) = |
|
68 |
mk_names (f (class, tyco, instance, const)); |
|
69 |
val eq_string = op = : string * string -> bool; |
|
70 |
in |
|
71 |
fun merge_names (Names { class = (class1, classrev1), tyco = (tyco1, tycorev1), |
|
72 |
instance = (instance1, instancerev1), const = (const1, constrev1) }, |
|
73 |
Names { class = (class2, classrev2), tyco = (tyco2, tycorev2), |
|
74 |
instance = (instance2, instancerev2), const = (const2, constrev2) }) = |
|
75 |
mk_names ((Symtab.merge eq_string (class1, class2), Symtab.merge eq_string (classrev1, classrev2)), |
|
76 |
(Symtab.merge eq_string (tyco1, tyco2), Symtab.merge eq_string (tycorev1, tycorev2)), |
|
77 |
(Insttab.merge eq_string (instance1, instance2), Symtab.merge eq_inst (instancerev1, instancerev2)), |
|
20386 | 78 |
(Consttab.merge eq_string (const1, const2), Symtab.merge CodegenConsts.eq_const (constrev1, constrev2))); |
20353 | 79 |
fun map_class f = map_names |
80 |
(fn (class, tyco, inst, const) => (f class, tyco, inst, const)); |
|
81 |
fun map_tyco f = map_names |
|
82 |
(fn (class, tyco, inst, const) => (class, f tyco, inst, const)); |
|
83 |
fun map_inst f = map_names |
|
84 |
(fn (class, tyco, inst, const) => (class, tyco, f inst, const)); |
|
85 |
fun map_const f = map_names |
|
86 |
(fn (class, tyco, inst, const) => (class, tyco, inst, f const)); |
|
87 |
||
88 |
end; (*local*) |
|
89 |
||
20456 | 90 |
structure CodeName = TheoryDataFun |
20353 | 91 |
(struct |
92 |
val name = "Pure/codegen_names"; |
|
93 |
type T = names ref; |
|
94 |
val empty = ref empty_names; |
|
95 |
fun copy (ref names) = ref names; |
|
96 |
val extend = copy; |
|
97 |
fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2)); |
|
98 |
fun print _ _ = (); |
|
99 |
end); |
|
100 |
||
20456 | 101 |
val _ = Context.add_setup CodeName.init; |
20353 | 102 |
|
103 |
||
104 |
(* forward lookup with cache update *) |
|
105 |
||
106 |
fun get thy get_tabs get upd_names upd policy x = |
|
107 |
let |
|
20456 | 108 |
val names_ref = CodeName.get thy |
20353 | 109 |
val (Names names) = ! names_ref; |
110 |
val tabs = get_tabs names; |
|
111 |
fun declare name = |
|
112 |
let |
|
113 |
val names' = upd_names (K (upd (x, name) (fst tabs), |
|
114 |
Symtab.update_new (name, x) (snd tabs))) (Names names) |
|
115 |
in (names_ref := names'; name) end; |
|
116 |
in case get (fst tabs) x |
|
117 |
of SOME name => name |
|
21338 | 118 |
| NONE => |
119 |
x |
|
120 |
|> policy thy |
|
121 |
|> Name.variant (Symtab.keys (snd tabs)) |
|
122 |
|> declare |
|
20353 | 123 |
end; |
124 |
||
125 |
||
126 |
(* backward lookup *) |
|
127 |
||
128 |
fun rev thy get_tabs errname name = |
|
129 |
let |
|
20456 | 130 |
val names_ref = CodeName.get thy |
20353 | 131 |
val (Names names) = ! names_ref; |
132 |
val tab = (snd o get_tabs) names; |
|
133 |
in case Symtab.lookup tab name |
|
134 |
of SOME x => x |
|
20389 | 135 |
| NONE => error ("No such " ^ errname ^ ": " ^ quote name) |
20353 | 136 |
end; |
137 |
||
138 |
||
139 |
(* theory name lookup *) |
|
140 |
||
141 |
fun thyname_of thy f errmsg x = |
|
142 |
let |
|
143 |
fun thy_of thy = |
|
144 |
if f thy x then case get_first thy_of (Theory.parents_of thy) |
|
145 |
of NONE => SOME thy |
|
146 |
| thy => thy |
|
147 |
else NONE; |
|
148 |
in case thy_of thy |
|
149 |
of SOME thy => Context.theory_name thy |
|
21880 | 150 |
|> explode |
151 |
(*should disappear as soon as hierarchical theory name spaces are available*) |
|
152 |
|> Symbol.scanner "Malformed name" |
|
153 |
(Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.not_eof)) |
|
154 |
|> implode |
|
20353 | 155 |
| NONE => error (errmsg x) end; |
156 |
||
157 |
fun thyname_of_class thy = |
|
158 |
thyname_of thy (fn thy => member (op =) (Sign.classes thy)) |
|
159 |
(fn class => "thyname_of_class: no such class: " ^ quote class); |
|
160 |
||
161 |
fun thyname_of_tyco thy = |
|
162 |
thyname_of thy Sign.declared_tyname |
|
163 |
(fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco); |
|
164 |
||
165 |
fun thyname_of_instance thy = |
|
166 |
let |
|
167 |
fun test_instance thy (class, tyco) = |
|
168 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
|
169 |
in thyname_of thy test_instance |
|
170 |
(fn (class, tyco) => "thyname_of_instance: no such instance: " ^ quote class ^ ", " ^ quote tyco) |
|
171 |
end; |
|
172 |
||
173 |
fun thyname_of_const thy = |
|
174 |
thyname_of thy Sign.declared_const |
|
175 |
(fn c => "thyname_of_const: no such constant: " ^ quote c); |
|
176 |
||
177 |
||
178 |
(* purification of identifiers *) |
|
179 |
||
180 |
val purify_name = |
|
181 |
let |
|
182 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'"; |
|
183 |
val is_junk = not o is_valid andf Symbol.not_eof; |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
184 |
val junk = Scan.many is_junk; |
20353 | 185 |
val scan_valids = Symbol.scanner "Malformed input" |
186 |
((junk |-- |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
187 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
20353 | 188 |
--| junk)) |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
189 |
-- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::); |
20353 | 190 |
in explode #> scan_valids #> space_implode "_" end; |
191 |
||
192 |
fun purify_op "0" = "zero" |
|
193 |
| purify_op "1" = "one" |
|
194 |
| purify_op s = |
|
195 |
let |
|
196 |
fun rep_op "+" = SOME "sum" |
|
197 |
| rep_op "-" = SOME "diff" |
|
198 |
| rep_op "*" = SOME "prod" |
|
199 |
| rep_op "/" = SOME "quotient" |
|
200 |
| rep_op "&" = SOME "conj" |
|
201 |
| rep_op "|" = SOME "disj" |
|
202 |
| rep_op "=" = SOME "eq" |
|
203 |
| rep_op "~" = SOME "inv" |
|
204 |
| rep_op "@" = SOME "append" |
|
205 |
| rep_op s = NONE; |
|
206 |
val scan_valids = Symbol.scanner "Malformed input" |
|
20386 | 207 |
(Scan.repeat (Scan.some rep_op || Scan.one Symbol.not_eof)); |
20353 | 208 |
in (explode #> scan_valids #> implode) s end; |
209 |
||
21014 | 210 |
val purify_lower = |
211 |
explode |
|
212 |
#> (fn cs => (if forall Symbol.is_ascii_upper cs |
|
213 |
then map else nth_map 0) Symbol.to_ascii_lower cs) |
|
214 |
#> implode; |
|
215 |
||
20353 | 216 |
val purify_upper = explode #> nth_map 0 Symbol.to_ascii_upper #> implode; |
217 |
||
21014 | 218 |
fun check_modulename mn = |
219 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
220 |
val mns = NameSpace.explode mn; |
21014 | 221 |
val mns' = map purify_upper mns; |
222 |
in |
|
21121 | 223 |
if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
224 |
^ "perhaps try " ^ quote (NameSpace.implode mns')) |
21014 | 225 |
end |
226 |
||
20389 | 227 |
fun purify_var "" = "x" |
228 |
| purify_var v = |
|
229 |
if nth (explode v) 0 = "'" |
|
230 |
then (unprefix "'" #> purify_name #> purify_lower #> prefix "'") v |
|
231 |
else (purify_name #> purify_lower) v; |
|
20353 | 232 |
|
233 |
val purify_idf = purify_op #> purify_name; |
|
234 |
val purify_prefix = map (purify_idf #> purify_upper); |
|
235 |
val purify_base = purify_idf #> purify_lower; |
|
236 |
||
237 |
||
238 |
(* naming policies *) |
|
239 |
||
240 |
fun policy thy get_basename get_thyname name = |
|
241 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
242 |
val prefix = (purify_prefix o NameSpace.explode o get_thyname thy) name; |
20353 | 243 |
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
|
244 |
in NameSpace.implode (prefix @ [base]) end; |
20353 | 245 |
|
246 |
fun class_policy thy = policy thy NameSpace.base thyname_of_class; |
|
247 |
fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco; |
|
248 |
fun instance_policy thy = policy thy (fn (class, tyco) => |
|
249 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
|
250 |
||
21461 | 251 |
fun force_thyname thy (const as (c, tys)) = |
20585 | 252 |
case AxClass.class_of_param thy c |
253 |
of SOME class => (case tys |
|
254 |
of [Type (tyco, _)] => SOME (thyname_of_instance thy (class, tyco)) |
|
21461 | 255 |
| _ => SOME (thyname_of_class thy class)) |
256 |
| NONE => (case CodegenData.get_datatype_of_constr thy const |
|
257 |
of SOME dtco => SOME (thyname_of_tyco thy dtco) |
|
258 |
| NONE => (case CodegenConsts.find_def thy const |
|
20585 | 259 |
of SOME ((thyname, _), _) => SOME thyname |
21461 | 260 |
| NONE => NONE)); |
20386 | 261 |
|
20353 | 262 |
fun const_policy thy (c, tys) = |
20386 | 263 |
case force_thyname thy (c, tys) |
20353 | 264 |
of NONE => policy thy NameSpace.base thyname_of_const c |
265 |
| SOME thyname => let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
266 |
val prefix = (purify_prefix o NameSpace.explode) thyname; |
20353 | 267 |
val tycos = map_filter (fn Type (tyco, _) => SOME tyco | _ => NONE) tys; |
20386 | 268 |
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
|
269 |
in NameSpace.implode (prefix @ [space_implode "_" base]) end; |
20353 | 270 |
|
271 |
||
20697 | 272 |
(* shallow name spaces *) |
273 |
||
274 |
val nsp_class = "class"; |
|
275 |
val nsp_tyco = "tyco"; |
|
276 |
val nsp_inst = "inst"; |
|
277 |
val nsp_fun = "fun"; |
|
278 |
val nsp_dtco = "dtco"; |
|
279 |
||
280 |
fun add_nsp shallow name = |
|
281 |
name |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
282 |
|> NameSpace.explode |
20697 | 283 |
|> split_last |
284 |
|> apsnd (single #> cons shallow) |
|
285 |
|> (op @) |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
286 |
|> NameSpace.implode; |
20697 | 287 |
|
288 |
fun dest_nsp nsp nspname = |
|
289 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
290 |
val xs = NameSpace.explode nspname; |
20697 | 291 |
val (ys, base) = split_last xs; |
292 |
val (module, shallow) = split_last ys; |
|
293 |
in |
|
294 |
if nsp = shallow |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21461
diff
changeset
|
295 |
then (SOME o NameSpace.implode) (module @ [base]) |
20697 | 296 |
else NONE |
297 |
end; |
|
298 |
||
299 |
val has_nsp = is_some oo dest_nsp; |
|
300 |
||
301 |
fun if_nsp nsp f idf = |
|
302 |
Option.map f (dest_nsp nsp idf); |
|
303 |
||
304 |
||
20353 | 305 |
(* external interfaces *) |
306 |
||
307 |
fun class thy = |
|
20697 | 308 |
get thy #class Symtab.lookup map_class Symtab.update class_policy |
309 |
#> add_nsp nsp_class; |
|
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 = |
20697 | 314 |
get thy #instance Insttab.lookup map_inst Insttab.update instance_policy |
315 |
#> add_nsp nsp_inst; |
|
316 |
fun const thy c_ty = case CodegenConsts.norm thy c_ty |
|
317 |
of (c_tys as (c, tys)) => add_nsp (if (is_some o CodegenData.get_datatype_of_constr thy) c_tys |
|
318 |
then nsp_dtco |
|
319 |
else nsp_fun) |
|
320 |
(get thy #const Consttab.lookup map_const Consttab.update const_policy c_tys); |
|
20353 | 321 |
|
20697 | 322 |
fun class_rev thy = |
323 |
dest_nsp nsp_class |
|
324 |
#> Option.map (rev thy #class "class"); |
|
325 |
fun tyco_rev thy = |
|
326 |
dest_nsp nsp_tyco |
|
327 |
#> Option.map (rev thy #tyco "type constructor"); |
|
328 |
fun instance_rev thy = |
|
329 |
dest_nsp nsp_inst |
|
330 |
#> Option.map (rev thy #instance "instance"); |
|
331 |
fun const_rev thy nspname = |
|
332 |
(case dest_nsp nsp_fun nspname |
|
333 |
of name as SOME _ => name |
|
334 |
| _ => (case dest_nsp nsp_dtco nspname |
|
335 |
of name as SOME _ => name |
|
21081 | 336 |
| _ => NONE)) |
20697 | 337 |
|> Option.map (rev thy #const "constant"); |
20353 | 338 |
|
339 |
end; |