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