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