author | wenzelm |
Wed, 25 Jul 2007 17:05:49 +0200 | |
changeset 23981 | 03b71bf91318 |
parent 23954 | bc85c552e82f |
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, |
22304 | 6 |
conversion to alphanumeric representation. |
20855 | 7 |
Mappings are incrementally cached. |
20353 | 8 |
*) |
9 |
||
10 |
signature CODEGEN_NAMES = |
|
11 |
sig |
|
22304 | 12 |
val purify_var: string -> string |
13 |
val purify_tvar: string -> string |
|
14 |
val check_modulename: string -> string |
|
15 |
type var_ctxt; |
|
16 |
val make_vars: string list -> var_ctxt; |
|
17 |
val intro_vars: string list -> var_ctxt -> var_ctxt; |
|
18 |
val lookup_var: var_ctxt -> string -> string; |
|
19 |
||
20386 | 20 |
type tyco = string |
21 |
type const = string |
|
20353 | 22 |
val class: theory -> class -> class |
20697 | 23 |
val class_rev: theory -> class -> class option |
21927 | 24 |
val classrel: theory -> class * class -> string |
25 |
val classrel_rev: theory -> string -> (class * class) option |
|
20353 | 26 |
val tyco: theory -> tyco -> tyco |
20697 | 27 |
val tyco_rev: theory -> tyco -> tyco option |
20353 | 28 |
val instance: theory -> class * tyco -> string |
20697 | 29 |
val instance_rev: theory -> string -> (class * tyco) option |
20585 | 30 |
val const: theory -> CodegenConsts.const -> const |
20697 | 31 |
val const_rev: theory -> const -> CodegenConsts.const option |
22034 | 32 |
val labelled_name: theory -> string -> string |
20353 | 33 |
end; |
34 |
||
35 |
structure CodegenNames: CODEGEN_NAMES = |
|
36 |
struct |
|
37 |
||
22034 | 38 |
(** purification **) |
39 |
||
23063 | 40 |
fun purify_name upper_else_lower = |
22034 | 41 |
let |
42 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'"; |
|
23784
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23229
diff
changeset
|
43 |
val is_junk = not o is_valid andf Symbol.is_regular; |
22034 | 44 |
val junk = Scan.many is_junk; |
45 |
val scan_valids = Symbol.scanner "Malformed input" |
|
46 |
((junk |-- |
|
47 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
|
48 |
--| junk)) |
|
49 |
-- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::); |
|
23063 | 50 |
fun upper_lower cs = if upper_else_lower then nth_map 0 Symbol.to_ascii_upper cs |
51 |
else (if forall Symbol.is_ascii_upper cs |
|
52 |
then map else nth_map 0) Symbol.to_ascii_lower cs; |
|
53 |
in |
|
54 |
explode |
|
55 |
#> scan_valids |
|
56 |
#> space_implode "_" |
|
57 |
#> explode |
|
58 |
#> upper_lower |
|
59 |
#> implode |
|
60 |
end; |
|
22034 | 61 |
|
22304 | 62 |
fun purify_var "" = "x" |
23063 | 63 |
| purify_var v = purify_name false v; |
22304 | 64 |
|
65 |
fun purify_tvar "" = "'a" |
|
66 |
| purify_tvar v = |
|
67 |
(unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v; |
|
68 |
||
69 |
fun check_modulename mn = |
|
70 |
let |
|
71 |
val mns = NameSpace.explode mn; |
|
23063 | 72 |
val mns' = map (purify_name true) mns; |
22304 | 73 |
in |
74 |
if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
|
75 |
^ "perhaps try " ^ quote (NameSpace.implode mns')) |
|
76 |
end; |
|
77 |
||
78 |
||
79 |
(** variable name contexts **) |
|
80 |
||
81 |
type var_ctxt = string Symtab.table * Name.context; |
|
82 |
||
83 |
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty, |
|
84 |
Name.make_context names); |
|
85 |
||
86 |
fun intro_vars names (namemap, namectxt) = |
|
87 |
let |
|
88 |
val (names', namectxt') = Name.variants names namectxt; |
|
89 |
val namemap' = fold2 (curry Symtab.update) names names' namemap; |
|
90 |
in (namemap', namectxt') end; |
|
91 |
||
92 |
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name |
|
93 |
of SOME name' => name' |
|
94 |
| NONE => error ("Invalid name in context: " ^ quote name); |
|
95 |
||
96 |
||
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
97 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
98 |
(** global names (identifiers) **) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
99 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
100 |
(* identifier categories *) |
22304 | 101 |
|
102 |
val idf_class = "class"; |
|
103 |
val idf_classrel = "clsrel" |
|
104 |
val idf_tyco = "tyco"; |
|
105 |
val idf_instance = "inst"; |
|
106 |
val idf_const = "const"; |
|
107 |
||
108 |
fun string_of_classrel (class, superclass) = class ^ " < " ^ superclass; |
|
109 |
fun string_of_instance (class, tyco) = tyco ^ " :: " ^ class; |
|
110 |
||
111 |
fun add_idf nsp name = |
|
112 |
NameSpace.append name nsp; |
|
113 |
||
114 |
fun dest_idf nsp name = |
|
115 |
if NameSpace.base name = nsp |
|
116 |
then SOME (NameSpace.qualifier name) |
|
117 |
else NONE; |
|
118 |
||
119 |
local |
|
120 |
||
121 |
val name_mapping = [ |
|
122 |
(idf_class, "class"), |
|
123 |
(idf_classrel, "subclass relation"), |
|
124 |
(idf_tyco, "type constructor"), |
|
125 |
(idf_instance, "instance"), |
|
126 |
(idf_const, "constant") |
|
127 |
] |
|
128 |
||
129 |
in |
|
130 |
||
131 |
val category_of = the o AList.lookup (op =) name_mapping o NameSpace.base; |
|
132 |
||
133 |
end; |
|
134 |
||
22034 | 135 |
|
20353 | 136 |
(* theory name lookup *) |
137 |
||
138 |
fun thyname_of thy f errmsg x = |
|
139 |
let |
|
140 |
fun thy_of thy = |
|
141 |
if f thy x then case get_first thy_of (Theory.parents_of thy) |
|
142 |
of NONE => SOME thy |
|
143 |
| thy => thy |
|
144 |
else NONE; |
|
145 |
in case thy_of thy |
|
146 |
of SOME thy => Context.theory_name thy |
|
147 |
| NONE => error (errmsg x) end; |
|
148 |
||
149 |
fun thyname_of_class thy = |
|
21931
314f9e2a442c
replaced Sign.classes by Sign.all_classes (topologically sorted);
wenzelm
parents:
21927
diff
changeset
|
150 |
thyname_of thy (fn thy => member (op =) (Sign.all_classes thy)) |
20353 | 151 |
(fn class => "thyname_of_class: no such class: " ^ quote class); |
152 |
||
21927 | 153 |
fun thyname_of_classrel thy = |
154 |
thyname_of thy (fn thy => fn (class1, class2) => Sign.subsort thy ([class1], [class2])) |
|
22304 | 155 |
(fn (class1, class2) => "thyname_of_classrel: no such subclass relation: " |
156 |
^ (quote o string_of_classrel) (class1, class2)); |
|
21927 | 157 |
|
20353 | 158 |
fun thyname_of_tyco thy = |
159 |
thyname_of thy Sign.declared_tyname |
|
160 |
(fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco); |
|
161 |
||
162 |
fun thyname_of_instance thy = |
|
163 |
let |
|
164 |
fun test_instance thy (class, tyco) = |
|
165 |
can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class] |
|
166 |
in thyname_of thy test_instance |
|
22304 | 167 |
(fn (class, tyco) => "thyname_of_instance: no such instance: " |
168 |
^ (quote o string_of_instance) (class, tyco)) |
|
20353 | 169 |
end; |
170 |
||
171 |
fun thyname_of_const thy = |
|
172 |
thyname_of thy Sign.declared_const |
|
173 |
(fn c => "thyname_of_const: no such constant: " ^ quote c); |
|
174 |
||
175 |
||
22034 | 176 |
(* naming policies *) |
20353 | 177 |
|
23063 | 178 |
val purify_prefix = |
21927 | 179 |
explode |
180 |
(*should disappear as soon as hierarchical theory name spaces are available*) |
|
181 |
#> Symbol.scanner "Malformed name" |
|
23784
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23229
diff
changeset
|
182 |
(Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.is_regular)) |
23063 | 183 |
#> implode |
184 |
#> NameSpace.explode |
|
185 |
#> map (purify_name true); |
|
21927 | 186 |
|
23063 | 187 |
fun purify_base "op =" = "eq" |
188 |
| purify_base "op &" = "and" |
|
189 |
| purify_base "op |" = "or" |
|
190 |
| purify_base "op -->" = "implies" |
|
191 |
| purify_base "{}" = "empty" |
|
192 |
| purify_base "op :" = "member" |
|
193 |
| purify_base "op Int" = "intersect" |
|
194 |
| purify_base "op Un" = "union" |
|
195 |
| purify_base "*" = "product" |
|
196 |
| purify_base "+" = "sum" |
|
197 |
| purify_base s = purify_name false s; |
|
198 |
||
199 |
fun default_policy thy get_basename get_thyname name = |
|
20353 | 200 |
let |
23063 | 201 |
val prefix = (purify_prefix o get_thyname thy) name; |
20353 | 202 |
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
|
203 |
in NameSpace.implode (prefix @ [base]) end; |
20353 | 204 |
|
23063 | 205 |
fun class_policy thy = default_policy thy NameSpace.base thyname_of_class; |
206 |
fun classrel_policy thy = default_policy thy (fn (class1, class2) => |
|
23954 | 207 |
NameSpace.base class2 ^ "_" ^ NameSpace.base class1) thyname_of_classrel; |
208 |
(*order fits nicely with composed projections*) |
|
23063 | 209 |
fun tyco_policy thy = default_policy thy NameSpace.base thyname_of_tyco; |
210 |
fun instance_policy thy = default_policy thy (fn (class, tyco) => |
|
20353 | 211 |
NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
212 |
||
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22463
diff
changeset
|
213 |
fun force_thyname thy (const as (c, opt_tyco)) = |
22807 | 214 |
case CodegenData.get_datatype_of_constr thy const |
21461 | 215 |
of SOME dtco => SOME (thyname_of_tyco thy dtco) |
22807 | 216 |
| NONE => (case AxClass.class_of_param thy c |
217 |
of SOME class => (case opt_tyco |
|
218 |
of SOME tyco => SOME (thyname_of_instance thy (class, tyco)) |
|
219 |
| NONE => SOME (thyname_of_class thy class)) |
|
220 |
| NONE => NONE); |
|
20386 | 221 |
|
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22463
diff
changeset
|
222 |
fun const_policy thy (const as (c, opt_tyco)) = |
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22463
diff
changeset
|
223 |
case force_thyname thy const |
23063 | 224 |
of NONE => default_policy thy NameSpace.base thyname_of_const c |
20353 | 225 |
| SOME thyname => let |
23063 | 226 |
val prefix = purify_prefix thyname; |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22463
diff
changeset
|
227 |
val tycos = the_list opt_tyco; |
20386 | 228 |
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
|
229 |
in NameSpace.implode (prefix @ [space_implode "_" base]) end; |
20353 | 230 |
|
231 |
||
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
232 |
(* theory and code data *) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
233 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
234 |
type tyco = string; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
235 |
type const = string; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
236 |
structure Consttab = CodegenConsts.Consttab; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
237 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
238 |
structure StringPairTab = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
239 |
TableFun( |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
240 |
type key = string * string; |
23229
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
241 |
val ord = prod_ord fast_string_ord fast_string_ord; |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
242 |
); |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
243 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
244 |
datatype names = Names of { |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
245 |
class: class Symtab.table * class Symtab.table, |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
246 |
classrel: string StringPairTab.table * (class * class) Symtab.table, |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
247 |
tyco: tyco Symtab.table * tyco Symtab.table, |
22463 | 248 |
instance: string StringPairTab.table * (class * tyco) Symtab.table |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
249 |
} |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
250 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
251 |
val empty_names = Names { |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
252 |
class = (Symtab.empty, Symtab.empty), |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
253 |
classrel = (StringPairTab.empty, Symtab.empty), |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
254 |
tyco = (Symtab.empty, Symtab.empty), |
22463 | 255 |
instance = (StringPairTab.empty, Symtab.empty) |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
256 |
}; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
257 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
258 |
local |
22463 | 259 |
fun mk_names (class, classrel, tyco, instance) = |
260 |
Names { class = class, classrel = classrel, tyco = tyco, instance = instance }; |
|
261 |
fun map_names f (Names { class, classrel, tyco, instance }) = |
|
262 |
mk_names (f (class, classrel, tyco, instance)); |
|
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
263 |
in |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
264 |
fun merge_names (Names { class = (class1, classrev1), |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
265 |
classrel = (classrel1, classrelrev1), tyco = (tyco1, tycorev1), |
22463 | 266 |
instance = (instance1, instancerev1) }, |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
267 |
Names { class = (class2, classrev2), |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
268 |
classrel = (classrel2, classrelrev2), tyco = (tyco2, tycorev2), |
22463 | 269 |
instance = (instance2, instancerev2) }) = |
23229
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
270 |
mk_names ((Symtab.merge (op =) (class1, class2), Symtab.merge (op =) (classrev1, classrev2)), |
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
271 |
(StringPairTab.merge (op =) (classrel1, classrel2), Symtab.merge (op =) (classrelrev1, classrelrev2)), |
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
272 |
(Symtab.merge (op =) (tyco1, tyco2), Symtab.merge (op =) (tycorev1, tycorev2)), |
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
273 |
(StringPairTab.merge (op =) (instance1, instance2), Symtab.merge (op =) (instancerev1, instancerev2))); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
274 |
fun map_class f = map_names |
22463 | 275 |
(fn (class, classrel, tyco, inst) => (f class, classrel, tyco, inst)); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
276 |
fun map_classrel f = map_names |
22463 | 277 |
(fn (class, classrel, tyco, inst) => (class, f classrel, tyco, inst)); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
278 |
fun map_tyco f = map_names |
22463 | 279 |
(fn (class, classrel, tyco, inst) => (class, classrel, f tyco, inst)); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
280 |
fun map_instance f = map_names |
22463 | 281 |
(fn (class, classrel, tyco, inst) => (class, classrel, tyco, f inst)); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
282 |
end; (*local*) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
283 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
284 |
structure CodeName = TheoryDataFun |
22846 | 285 |
( |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
286 |
type T = names ref; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
287 |
val empty = ref empty_names; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
288 |
fun copy (ref names) = ref names; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
289 |
val extend = copy; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
290 |
fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2)); |
22846 | 291 |
); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
292 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
293 |
structure ConstName = CodeDataFun |
22846 | 294 |
( |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22463
diff
changeset
|
295 |
type T = const Consttab.table * (string * string option) Symtab.table; |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
296 |
val empty = (Consttab.empty, Symtab.empty); |
23229
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
297 |
fun merge _ ((const1, constrev1), (const2, constrev2)) : T = |
492e2fd12767
monomorphic equality: let ML work out the details;
wenzelm
parents:
23136
diff
changeset
|
298 |
(Consttab.merge (op =) (const1, const2), |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
299 |
Symtab.merge CodegenConsts.eq_const (constrev1, constrev2)); |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
300 |
fun purge _ NONE _ = empty |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
301 |
| purge _ (SOME cs) (const, constrev) = (fold Consttab.delete_safe cs const, |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
302 |
fold Symtab.delete (map_filter (Consttab.lookup const) cs) constrev); |
22846 | 303 |
); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
304 |
|
23136 | 305 |
val _ = Context.add_setup (CodeName.init); |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
306 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
307 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
308 |
(* forward lookup with cache update *) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
309 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
310 |
fun get thy get_tabs get upd_names upd policy x = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
311 |
let |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
312 |
val names_ref = CodeName.get thy; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
313 |
val (Names names) = ! names_ref; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
314 |
val tabs = get_tabs names; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
315 |
fun declare name = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
316 |
let |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
317 |
val names' = upd_names (K (upd (x, name) (fst tabs), |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
318 |
Symtab.update_new (name, x) (snd tabs))) (Names names) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
319 |
in (names_ref := names'; name) end; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
320 |
in case get (fst tabs) x |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
321 |
of SOME name => name |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
322 |
| NONE => |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
323 |
x |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
324 |
|> policy thy |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
325 |
|> Name.variant (Symtab.keys (snd tabs)) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
326 |
|> declare |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
327 |
end; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
328 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
329 |
fun get_const thy const = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
330 |
let |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
331 |
val tabs = ConstName.get thy; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
332 |
fun declare name = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
333 |
let |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
334 |
val names' = (Consttab.update (const, name) (fst tabs), |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
335 |
Symtab.update_new (name, const) (snd tabs)) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
336 |
in (ConstName.change thy (K names'); name) end; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
337 |
in case Consttab.lookup (fst tabs) const |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
338 |
of SOME name => name |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
339 |
| NONE => |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
340 |
const |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
341 |
|> const_policy thy |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
342 |
|> Name.variant (Symtab.keys (snd tabs)) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
343 |
|> declare |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
344 |
end; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
345 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
346 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
347 |
(* backward lookup *) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
348 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
349 |
fun rev thy get_tabs name = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
350 |
let |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
351 |
val names_ref = CodeName.get thy |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
352 |
val (Names names) = ! names_ref; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
353 |
val tab = (snd o get_tabs) names; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
354 |
in case Symtab.lookup tab name |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
355 |
of SOME x => x |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
356 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
357 |
end; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
358 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
359 |
fun rev_const thy name = |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
360 |
let |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
361 |
val tab = snd (ConstName.get thy); |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
362 |
in case Symtab.lookup tab name |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
363 |
of SOME const => const |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
364 |
| NONE => error ("No such " ^ category_of name ^ ": " ^ quote name) |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
365 |
end; |
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
366 |
|
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
367 |
|
20353 | 368 |
(* external interfaces *) |
369 |
||
370 |
fun class thy = |
|
20697 | 371 |
get thy #class Symtab.lookup map_class Symtab.update class_policy |
22304 | 372 |
#> add_idf idf_class; |
21927 | 373 |
fun classrel thy = |
374 |
get thy #classrel StringPairTab.lookup map_classrel StringPairTab.update classrel_policy |
|
22304 | 375 |
#> add_idf idf_classrel; |
20353 | 376 |
fun tyco thy = |
20697 | 377 |
get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy |
22304 | 378 |
#> add_idf idf_tyco; |
20353 | 379 |
fun instance thy = |
22304 | 380 |
get thy #instance StringPairTab.lookup map_instance StringPairTab.update instance_policy |
381 |
#> add_idf idf_instance; |
|
21915 | 382 |
fun const thy = |
22554
d1499fff65d8
simplified constant representation in code generator
haftmann
parents:
22463
diff
changeset
|
383 |
get_const thy |
22304 | 384 |
#> add_idf idf_const; |
20353 | 385 |
|
20697 | 386 |
fun class_rev thy = |
22304 | 387 |
dest_idf idf_class |
388 |
#> Option.map (rev thy #class); |
|
21927 | 389 |
fun classrel_rev thy = |
22304 | 390 |
dest_idf idf_classrel |
391 |
#> Option.map (rev thy #classrel); |
|
20697 | 392 |
fun tyco_rev thy = |
22304 | 393 |
dest_idf idf_tyco |
394 |
#> Option.map (rev thy #tyco); |
|
20697 | 395 |
fun instance_rev thy = |
22304 | 396 |
dest_idf idf_instance |
397 |
#> Option.map (rev thy #instance); |
|
21915 | 398 |
fun const_rev thy = |
22304 | 399 |
dest_idf idf_const |
22427
69ce2cb9e3e5
constant names now dependent on executable content
haftmann
parents:
22304
diff
changeset
|
400 |
#> Option.map (rev_const thy); |
22304 | 401 |
|
402 |
local |
|
20353 | 403 |
|
22304 | 404 |
val f_mapping = [ |
405 |
(idf_class, class_rev), |
|
406 |
(idf_classrel, Option.map string_of_classrel oo classrel_rev), |
|
407 |
(idf_tyco, tyco_rev), |
|
408 |
(idf_instance, Option.map string_of_instance oo instance_rev), |
|
409 |
(idf_const, fn thy => Option.map (CodegenConsts.string_of_const thy) o const_rev thy) |
|
410 |
]; |
|
411 |
||
412 |
in |
|
413 |
||
414 |
fun labelled_name thy idf = |
|
22034 | 415 |
let |
22304 | 416 |
val category = category_of idf; |
417 |
val name = NameSpace.qualifier idf; |
|
418 |
val f = (the o AList.lookup (op =) f_mapping o NameSpace.base) idf |
|
419 |
in case f thy idf |
|
420 |
of SOME thing => category ^ " " ^ quote thing |
|
421 |
| NONE => error ("Unknown name: " ^ quote name) |
|
22034 | 422 |
end; |
423 |
||
22304 | 424 |
end; |
22034 | 425 |
|
20353 | 426 |
end; |