20353
|
1 |
(* Title: Pure/Tools/codegen_names.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
|
5 |
Name policies for code generation: dissolving ad-hoc overloaded
|
|
6 |
constants, prefixing any name by corresponding theory name,
|
|
7 |
conversion to alphanumeric representation.
|
|
8 |
Mappings are incrementally cached.
|
|
9 |
*)
|
|
10 |
|
|
11 |
signature CODEGEN_NAMES =
|
|
12 |
sig
|
|
13 |
type tyco = string;
|
|
14 |
type const = string;
|
|
15 |
val class: theory -> class -> class
|
|
16 |
val class_rev: theory -> class -> class
|
|
17 |
val tyco: theory -> tyco -> tyco
|
|
18 |
val tyco_rev: theory -> tyco -> tyco
|
|
19 |
val instance: theory -> class * tyco -> string
|
|
20 |
val instance_rev: theory -> string -> class * tyco
|
|
21 |
val const: theory -> string * typ -> const
|
|
22 |
val const_rev: theory -> const -> string * typ
|
|
23 |
val force: (string * typ) * const -> theory -> theory
|
|
24 |
val read_const_typ: theory -> string -> string * typ
|
|
25 |
val read_const: theory -> string -> const
|
|
26 |
val purify_var: string -> string
|
|
27 |
end;
|
|
28 |
|
|
29 |
structure CodegenNames: CODEGEN_NAMES =
|
|
30 |
struct
|
|
31 |
|
|
32 |
|
|
33 |
(* theory data *)
|
|
34 |
|
|
35 |
type tyco = string;
|
|
36 |
type const = string;
|
|
37 |
val inst_ord = prod_ord fast_string_ord fast_string_ord;
|
|
38 |
val eq_inst = is_equal o inst_ord;
|
|
39 |
val const_ord = prod_ord fast_string_ord (list_ord Term.typ_ord);
|
|
40 |
val eq_const = is_equal o const_ord;
|
|
41 |
|
|
42 |
structure Insttab =
|
|
43 |
TableFun(
|
|
44 |
type key = class * tyco;
|
|
45 |
val ord = inst_ord;
|
|
46 |
);
|
|
47 |
|
|
48 |
structure Consttab =
|
|
49 |
TableFun(
|
|
50 |
type key = string * typ list;
|
|
51 |
val ord = const_ord;
|
|
52 |
);
|
|
53 |
|
|
54 |
datatype names = Names of {
|
|
55 |
class: class Symtab.table * class Symtab.table,
|
|
56 |
tyco: tyco Symtab.table * tyco Symtab.table,
|
|
57 |
instance: string Insttab.table * (class * tyco) Symtab.table,
|
|
58 |
const: const Consttab.table * (string * typ list) Symtab.table
|
|
59 |
}
|
|
60 |
|
|
61 |
val empty_names = Names {
|
|
62 |
class = (Symtab.empty, Symtab.empty),
|
|
63 |
tyco = (Symtab.empty, Symtab.empty),
|
|
64 |
instance = (Insttab.empty, Symtab.empty),
|
|
65 |
const = (Consttab.empty, Symtab.empty)
|
|
66 |
};
|
|
67 |
|
|
68 |
local
|
|
69 |
fun mk_names (class, tyco, instance, const) =
|
|
70 |
Names { class = class, tyco = tyco, instance = instance, const = const};
|
|
71 |
fun map_names f (Names { class, tyco, instance, const }) =
|
|
72 |
mk_names (f (class, tyco, instance, const));
|
|
73 |
val eq_string = op = : string * string -> bool;
|
|
74 |
in
|
|
75 |
fun merge_names (Names { class = (class1, classrev1), tyco = (tyco1, tycorev1),
|
|
76 |
instance = (instance1, instancerev1), const = (const1, constrev1) },
|
|
77 |
Names { class = (class2, classrev2), tyco = (tyco2, tycorev2),
|
|
78 |
instance = (instance2, instancerev2), const = (const2, constrev2) }) =
|
|
79 |
mk_names ((Symtab.merge eq_string (class1, class2), Symtab.merge eq_string (classrev1, classrev2)),
|
|
80 |
(Symtab.merge eq_string (tyco1, tyco2), Symtab.merge eq_string (tycorev1, tycorev2)),
|
|
81 |
(Insttab.merge eq_string (instance1, instance2), Symtab.merge eq_inst (instancerev1, instancerev2)),
|
|
82 |
(Consttab.merge eq_string (const1, const2), Symtab.merge eq_const (constrev1, constrev2)));
|
|
83 |
fun map_class f = map_names
|
|
84 |
(fn (class, tyco, inst, const) => (f class, tyco, inst, const));
|
|
85 |
fun map_tyco f = map_names
|
|
86 |
(fn (class, tyco, inst, const) => (class, f tyco, inst, const));
|
|
87 |
fun map_inst f = map_names
|
|
88 |
(fn (class, tyco, inst, const) => (class, tyco, f inst, const));
|
|
89 |
fun map_const f = map_names
|
|
90 |
(fn (class, tyco, inst, const) => (class, tyco, inst, f const));
|
|
91 |
|
|
92 |
end; (*local*)
|
|
93 |
|
|
94 |
structure CodegenNamesData = TheoryDataFun
|
|
95 |
(struct
|
|
96 |
val name = "Pure/codegen_names";
|
|
97 |
type T = names ref;
|
|
98 |
val empty = ref empty_names;
|
|
99 |
fun copy (ref names) = ref names;
|
|
100 |
val extend = copy;
|
|
101 |
fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2));
|
|
102 |
fun print _ _ = ();
|
|
103 |
end);
|
|
104 |
|
|
105 |
val _ = Context.add_setup CodegenNamesData.init;
|
|
106 |
|
|
107 |
|
|
108 |
(* forward lookup with cache update *)
|
|
109 |
|
|
110 |
fun get thy get_tabs get upd_names upd policy x =
|
|
111 |
let
|
|
112 |
val names_ref = CodegenNamesData.get thy
|
|
113 |
val (Names names) = ! names_ref;
|
|
114 |
fun mk_unique used name =
|
|
115 |
let
|
|
116 |
fun mk_name 0 = name
|
|
117 |
| mk_name i = name ^ "_" ^ string_of_int i
|
|
118 |
fun find_name i =
|
|
119 |
let
|
|
120 |
val name = mk_name i
|
|
121 |
in
|
|
122 |
if used name
|
|
123 |
then find_name (i+1)
|
|
124 |
else name
|
|
125 |
end;
|
|
126 |
val name = find_name 0;
|
|
127 |
in name end;
|
|
128 |
val tabs = get_tabs names;
|
|
129 |
fun declare name =
|
|
130 |
let
|
|
131 |
val names' = upd_names (K (upd (x, name) (fst tabs),
|
|
132 |
Symtab.update_new (name, x) (snd tabs))) (Names names)
|
|
133 |
in (names_ref := names'; name) end;
|
|
134 |
in case get (fst tabs) x
|
|
135 |
of SOME name => name
|
|
136 |
| NONE => let
|
|
137 |
val used = Symtab.defined (snd tabs);
|
|
138 |
val raw_name = policy thy x;
|
|
139 |
val name = mk_unique used raw_name;
|
|
140 |
in declare name end
|
|
141 |
end;
|
|
142 |
|
|
143 |
|
|
144 |
(* backward lookup *)
|
|
145 |
|
|
146 |
fun rev thy get_tabs errname name =
|
|
147 |
let
|
|
148 |
val names_ref = CodegenNamesData.get thy
|
|
149 |
val (Names names) = ! names_ref;
|
|
150 |
val tab = (snd o get_tabs) names;
|
|
151 |
in case Symtab.lookup tab name
|
|
152 |
of SOME x => x
|
|
153 |
| NONE => error ("no such " ^ errname ^ ": " ^ quote name)
|
|
154 |
end;
|
|
155 |
|
|
156 |
|
|
157 |
(* theory name lookup *)
|
|
158 |
|
|
159 |
fun thyname_of thy f errmsg x =
|
|
160 |
let
|
|
161 |
fun thy_of thy =
|
|
162 |
if f thy x then case get_first thy_of (Theory.parents_of thy)
|
|
163 |
of NONE => SOME thy
|
|
164 |
| thy => thy
|
|
165 |
else NONE;
|
|
166 |
in case thy_of thy
|
|
167 |
of SOME thy => Context.theory_name thy
|
|
168 |
| NONE => error (errmsg x) end;
|
|
169 |
|
|
170 |
fun thyname_of_class thy =
|
|
171 |
thyname_of thy (fn thy => member (op =) (Sign.classes thy))
|
|
172 |
(fn class => "thyname_of_class: no such class: " ^ quote class);
|
|
173 |
|
|
174 |
fun thyname_of_tyco thy =
|
|
175 |
thyname_of thy Sign.declared_tyname
|
|
176 |
(fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco);
|
|
177 |
|
|
178 |
fun thyname_of_instance thy =
|
|
179 |
let
|
|
180 |
fun test_instance thy (class, tyco) =
|
|
181 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class]
|
|
182 |
in thyname_of thy test_instance
|
|
183 |
(fn (class, tyco) => "thyname_of_instance: no such instance: " ^ quote class ^ ", " ^ quote tyco)
|
|
184 |
end;
|
|
185 |
|
|
186 |
fun thyname_of_const thy =
|
|
187 |
thyname_of thy Sign.declared_const
|
|
188 |
(fn c => "thyname_of_const: no such constant: " ^ quote c);
|
|
189 |
|
|
190 |
|
|
191 |
(* dissolving of ad-hoc overloading *)
|
|
192 |
|
|
193 |
fun typinst_of_typ thy (c, ty) =
|
|
194 |
let
|
|
195 |
fun disciplined _ [(Type (tyco, _))] =
|
|
196 |
[Type (tyco, map (fn v => TFree (v, Sign.defaultS thy)) (*for monotonicity*)
|
|
197 |
(Name.invents Name.context "'a" (Sign.arity_number thy tyco)))]
|
|
198 |
| disciplined sort _ =
|
|
199 |
[TFree ("'a", sort)];
|
|
200 |
fun ad_hoc c tys =
|
|
201 |
let
|
|
202 |
val def_tyinsts =
|
|
203 |
map (#lhs o snd) (Defs.specifications_of (Theory.defs_of thy) c);
|
|
204 |
val tyinst = find_first
|
|
205 |
(fn tys' => forall (Sign.typ_instance thy) (tys' ~~ tys)) def_tyinsts;
|
|
206 |
in case tyinst
|
|
207 |
of SOME tys => tys
|
|
208 |
| NONE => Consts.typargs (Sign.consts_of thy)
|
|
209 |
(c, (Logic.unvarifyT o Sign.the_const_type thy) c)
|
|
210 |
end;
|
|
211 |
val tyinsts = Consts.typargs (Sign.consts_of thy) (c, ty);
|
|
212 |
in if c = "op =" then (c, disciplined (Sign.defaultS thy) tyinsts)
|
|
213 |
else case AxClass.class_of_param thy c
|
|
214 |
of SOME class => (c, disciplined [class] tyinsts)
|
|
215 |
| _ => (c, ad_hoc c tyinsts)
|
|
216 |
end;
|
|
217 |
|
|
218 |
fun get_overl_def_module thy ("op =", [Type (tyco, _)]) =
|
|
219 |
SOME (thyname_of_tyco thy tyco)
|
|
220 |
| get_overl_def_module thy (c, tys) =
|
|
221 |
get_first (fn (_, { lhs, module, ...}) =>
|
|
222 |
if forall (Sign.typ_instance thy) (lhs ~~ tys) then SOME module else NONE)
|
|
223 |
(Defs.specifications_of (Theory.defs_of thy) c);
|
|
224 |
|
|
225 |
fun typ_of_typinst thy (c, tys) =
|
|
226 |
(c, Consts.instance (Sign.consts_of thy) (c, tys));
|
|
227 |
|
|
228 |
|
|
229 |
(* purification of identifiers *)
|
|
230 |
|
|
231 |
val purify_name =
|
|
232 |
let
|
|
233 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'";
|
|
234 |
val is_junk = not o is_valid andf Symbol.not_eof;
|
|
235 |
val junk = Scan.any is_junk;
|
|
236 |
val scan_valids = Symbol.scanner "Malformed input"
|
|
237 |
((junk |--
|
|
238 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.any is_valid >> implode)
|
|
239 |
--| junk))
|
|
240 |
-- Scan.repeat ((Scan.any1 is_valid >> implode) --| junk) >> op ::);
|
|
241 |
in explode #> scan_valids #> space_implode "_" end;
|
|
242 |
|
|
243 |
fun purify_op "0" = "zero"
|
|
244 |
| purify_op "1" = "one"
|
|
245 |
| purify_op s =
|
|
246 |
let
|
|
247 |
fun rep_op "+" = SOME "sum"
|
|
248 |
| rep_op "-" = SOME "diff"
|
|
249 |
| rep_op "*" = SOME "prod"
|
|
250 |
| rep_op "/" = SOME "quotient"
|
|
251 |
| rep_op "&" = SOME "conj"
|
|
252 |
| rep_op "|" = SOME "disj"
|
|
253 |
| rep_op "=" = SOME "eq"
|
|
254 |
| rep_op "~" = SOME "inv"
|
|
255 |
| rep_op "@" = SOME "append"
|
|
256 |
| rep_op s = NONE;
|
|
257 |
val scan_valids = Symbol.scanner "Malformed input"
|
|
258 |
(Scan.repeat (Scan.some rep_op || Scan.one (K true)));
|
|
259 |
in (explode #> scan_valids #> implode) s end;
|
|
260 |
|
|
261 |
val purify_lower = explode #> nth_map 0 Symbol.to_ascii_lower #> implode;
|
|
262 |
val purify_upper = explode #> nth_map 0 Symbol.to_ascii_upper #> implode;
|
|
263 |
|
|
264 |
fun purify_var v =
|
|
265 |
if nth (explode v) 0 = "'"
|
|
266 |
then "'" ^ (purify_name #> purify_lower #> unprefix "'") v
|
|
267 |
else (purify_name #> purify_lower) v;
|
|
268 |
|
|
269 |
val purify_idf = purify_op #> purify_name;
|
|
270 |
val purify_prefix = map (purify_idf #> purify_upper);
|
|
271 |
val purify_base = purify_idf #> purify_lower;
|
|
272 |
|
|
273 |
|
|
274 |
(* explicit given constant names with cache update *)
|
|
275 |
|
|
276 |
fun force (c_ty, name) thy =
|
|
277 |
let
|
|
278 |
val names = NameSpace.unpack name;
|
|
279 |
val (prefix, base) = split_last (NameSpace.unpack name);
|
|
280 |
val prefix' = purify_prefix prefix;
|
|
281 |
val base' = purify_base base;
|
|
282 |
val _ = if (base' = base) andalso forall (op =) (prefix' ~~ prefix)
|
|
283 |
then ()
|
|
284 |
else
|
|
285 |
error ("name violates naming conventions: " ^ quote name
|
|
286 |
^ "; perhaps try with " ^ quote (NameSpace.pack (prefix' @ [base'])))
|
|
287 |
val names_ref = CodegenNamesData.get thy;
|
|
288 |
val (Names names) = ! names_ref;
|
|
289 |
val (const, constrev) = #const names;
|
|
290 |
val c_tys as (c, _) = typinst_of_typ thy c_ty;
|
|
291 |
val _ = if Consttab.defined const c_tys
|
|
292 |
then error ("constant already named: " ^
|
|
293 |
quote (c ^ "::" ^ (Sign.string_of_typ thy o snd o typ_of_typinst thy) c_tys))
|
|
294 |
else ();
|
|
295 |
val _ = if Symtab.defined constrev name
|
|
296 |
then error ("name already given to constant: " ^ quote name)
|
|
297 |
else ();
|
|
298 |
val _ = names_ref := map_const (K (Consttab.update_new (c_tys, name) const,
|
|
299 |
Symtab.update_new (name, c_tys) constrev)) (Names names);
|
|
300 |
in
|
|
301 |
thy
|
|
302 |
end;
|
|
303 |
|
|
304 |
|
|
305 |
(* naming policies *)
|
|
306 |
|
|
307 |
fun policy thy get_basename get_thyname name =
|
|
308 |
let
|
|
309 |
val prefix = (purify_prefix o NameSpace.unpack o get_thyname thy) name;
|
|
310 |
val base = (purify_base o get_basename) name;
|
|
311 |
in NameSpace.pack (prefix @ [base]) end;
|
|
312 |
|
|
313 |
fun class_policy thy = policy thy NameSpace.base thyname_of_class;
|
|
314 |
fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco;
|
|
315 |
fun instance_policy thy = policy thy (fn (class, tyco) =>
|
|
316 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance;
|
|
317 |
|
|
318 |
fun const_policy thy (c, tys) =
|
|
319 |
case get_overl_def_module thy (c, tys)
|
|
320 |
of NONE => policy thy NameSpace.base thyname_of_const c
|
|
321 |
| SOME thyname => let
|
|
322 |
val prefix = (purify_prefix o NameSpace.unpack) thyname;
|
|
323 |
val base = (purify_base o NameSpace.base) c;
|
|
324 |
val tycos = map_filter (fn Type (tyco, _) => SOME tyco | _ => NONE) tys;
|
|
325 |
val base' = space_implode "_" (base :: tycos);
|
|
326 |
in NameSpace.pack (prefix @ [base']) end;
|
|
327 |
|
|
328 |
|
|
329 |
(* external interfaces *)
|
|
330 |
|
|
331 |
fun class thy =
|
|
332 |
get thy #class Symtab.lookup map_class Symtab.update class_policy;
|
|
333 |
fun tyco thy =
|
|
334 |
get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy;
|
|
335 |
fun instance thy =
|
|
336 |
get thy #instance Insttab.lookup map_inst Insttab.update instance_policy;
|
|
337 |
fun const thy =
|
|
338 |
get thy #const Consttab.lookup map_const Consttab.update const_policy
|
|
339 |
o typinst_of_typ thy;
|
|
340 |
|
|
341 |
fun class_rev thy = rev thy #class "class";
|
|
342 |
fun tyco_rev thy = rev thy #tyco "type constructor";
|
|
343 |
fun instance_rev thy = rev thy #instance "instance";
|
|
344 |
fun const_rev thy = rev thy #const "constant" #> typ_of_typinst thy;
|
|
345 |
|
|
346 |
|
|
347 |
(* reading constants as terms *)
|
|
348 |
|
|
349 |
fun read_const_typ thy raw_t =
|
|
350 |
let
|
|
351 |
val t = Sign.read_term thy raw_t
|
|
352 |
in case try dest_Const t
|
|
353 |
of SOME c_ty => c_ty
|
|
354 |
| NONE => error ("not a constant: " ^ Sign.string_of_term thy t)
|
|
355 |
end;
|
|
356 |
|
|
357 |
fun read_const thy =
|
|
358 |
const thy o read_const_typ thy;
|
|
359 |
|
|
360 |
|
|
361 |
(* outer syntax *)
|
|
362 |
|
|
363 |
local
|
|
364 |
|
|
365 |
structure P = OuterParse
|
|
366 |
and K = OuterKeyword
|
|
367 |
|
|
368 |
fun force_e (raw_c, name) thy =
|
|
369 |
force (read_const_typ thy raw_c, name) thy;
|
|
370 |
|
|
371 |
val constnameK = "code_constname";
|
|
372 |
|
|
373 |
val constnameP =
|
|
374 |
OuterSyntax.command constnameK "declare code name for constant" K.thy_decl (
|
|
375 |
P.term -- P.name
|
|
376 |
>> (fn (raw_c, name) =>
|
|
377 |
Toplevel.theory (force_e (raw_c, name)))
|
|
378 |
);
|
|
379 |
|
|
380 |
in
|
|
381 |
|
|
382 |
val _ = OuterSyntax.add_parsers [constnameP];
|
|
383 |
|
|
384 |
|
|
385 |
end; (*local*)
|
|
386 |
|
|
387 |
end;
|