35 structure CodegenNames: CODEGEN_NAMES = |
35 structure CodegenNames: CODEGEN_NAMES = |
36 struct |
36 struct |
37 |
37 |
38 (** purification **) |
38 (** purification **) |
39 |
39 |
40 val purify_name = |
40 fun purify_name upper_else_lower = |
41 let |
41 let |
42 fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'"; |
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; |
43 val is_junk = not o is_valid andf Symbol.not_eof; |
44 val junk = Scan.many is_junk; |
44 val junk = Scan.many is_junk; |
45 val scan_valids = Symbol.scanner "Malformed input" |
45 val scan_valids = Symbol.scanner "Malformed input" |
46 ((junk |-- |
46 ((junk |-- |
47 (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
47 (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
48 --| junk)) |
48 --| junk)) |
49 -- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::); |
49 -- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::); |
50 in explode #> scan_valids #> space_implode "_" end; |
50 fun upper_lower cs = if upper_else_lower then nth_map 0 Symbol.to_ascii_upper cs |
51 |
51 else (if forall Symbol.is_ascii_upper cs |
52 fun purify_op "op -->" = "implies" |
52 then map else nth_map 0) Symbol.to_ascii_lower cs; |
53 | purify_op "{}" = "empty" |
53 in |
54 | purify_op s = |
54 explode |
55 let |
55 #> scan_valids |
56 fun rep_op "+" = SOME "plus" |
56 #> space_implode "_" |
57 | rep_op "*" = SOME "times" |
57 #> explode |
58 | rep_op "&" = SOME "and" |
58 #> upper_lower |
59 | rep_op "|" = SOME "or" |
59 #> implode |
60 | rep_op "=" = SOME "eq" |
60 end; |
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; |
|
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 |
61 |
74 fun purify_var "" = "x" |
62 fun purify_var "" = "x" |
75 | purify_var v = (purify_name #> purify_lower) v; |
63 | purify_var v = purify_name false v; |
76 |
64 |
77 fun purify_tvar "" = "'a" |
65 fun purify_tvar "" = "'a" |
78 | purify_tvar v = |
66 | purify_tvar v = |
79 (unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v; |
67 (unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v; |
80 |
68 |
81 fun check_modulename mn = |
69 fun check_modulename mn = |
82 let |
70 let |
83 val mns = NameSpace.explode mn; |
71 val mns = NameSpace.explode mn; |
84 val mns' = map purify_upper mns; |
72 val mns' = map (purify_name true) mns; |
85 in |
73 in |
86 if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
74 if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
87 ^ "perhaps try " ^ quote (NameSpace.implode mns')) |
75 ^ "perhaps try " ^ quote (NameSpace.implode mns')) |
88 end; |
76 end; |
89 |
77 |
185 (fn c => "thyname_of_const: no such constant: " ^ quote c); |
173 (fn c => "thyname_of_const: no such constant: " ^ quote c); |
186 |
174 |
187 |
175 |
188 (* naming policies *) |
176 (* naming policies *) |
189 |
177 |
190 val purify_idf = purify_op #> purify_name; |
178 val purify_prefix = |
191 val purify_prefix = map (purify_idf #> purify_upper); |
|
192 val purify_base = purify_idf #> purify_lower; |
|
193 |
|
194 val dotify = |
|
195 explode |
179 explode |
196 (*should disappear as soon as hierarchical theory name spaces are available*) |
180 (*should disappear as soon as hierarchical theory name spaces are available*) |
197 #> Symbol.scanner "Malformed name" |
181 #> Symbol.scanner "Malformed name" |
198 (Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.not_eof)) |
182 (Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.not_eof)) |
199 #> implode; |
183 #> implode |
200 |
184 #> NameSpace.explode |
201 fun policy thy get_basename get_thyname name = |
185 #> map (purify_name true); |
202 let |
186 |
203 val prefix = (purify_prefix o NameSpace.explode o dotify o get_thyname thy) name; |
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 = |
|
200 let |
|
201 val prefix = (purify_prefix o get_thyname thy) name; |
204 val base = (purify_base o get_basename) name; |
202 val base = (purify_base o get_basename) name; |
205 in NameSpace.implode (prefix @ [base]) end; |
203 in NameSpace.implode (prefix @ [base]) end; |
206 |
204 |
207 fun class_policy thy = policy thy NameSpace.base thyname_of_class; |
205 fun class_policy thy = default_policy thy NameSpace.base thyname_of_class; |
208 fun classrel_policy thy = policy thy (fn (class1, class2) => |
206 fun classrel_policy thy = default_policy thy (fn (class1, class2) => |
209 NameSpace.base class1 ^ "_" ^ NameSpace.base class2) thyname_of_classrel; |
207 NameSpace.base class1 ^ "_" ^ NameSpace.base class2) thyname_of_classrel; |
210 fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco; |
208 fun tyco_policy thy = default_policy thy NameSpace.base thyname_of_tyco; |
211 fun instance_policy thy = policy thy (fn (class, tyco) => |
209 fun instance_policy thy = default_policy thy (fn (class, tyco) => |
212 NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
210 NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance; |
213 |
211 |
214 fun force_thyname thy (const as (c, opt_tyco)) = |
212 fun force_thyname thy (const as (c, opt_tyco)) = |
215 case CodegenData.get_datatype_of_constr thy const |
213 case CodegenData.get_datatype_of_constr thy const |
216 of SOME dtco => SOME (thyname_of_tyco thy dtco) |
214 of SOME dtco => SOME (thyname_of_tyco thy dtco) |
220 | NONE => SOME (thyname_of_class thy class)) |
218 | NONE => SOME (thyname_of_class thy class)) |
221 | NONE => NONE); |
219 | NONE => NONE); |
222 |
220 |
223 fun const_policy thy (const as (c, opt_tyco)) = |
221 fun const_policy thy (const as (c, opt_tyco)) = |
224 case force_thyname thy const |
222 case force_thyname thy const |
225 of NONE => policy thy NameSpace.base thyname_of_const c |
223 of NONE => default_policy thy NameSpace.base thyname_of_const c |
226 | SOME thyname => let |
224 | SOME thyname => let |
227 val prefix = (purify_prefix o NameSpace.explode o dotify) thyname; |
225 val prefix = purify_prefix thyname; |
228 val tycos = the_list opt_tyco; |
226 val tycos = the_list opt_tyco; |
229 val base = map (purify_base o NameSpace.base) (c :: tycos); |
227 val base = map (purify_base o NameSpace.base) (c :: tycos); |
230 in NameSpace.implode (prefix @ [space_implode "_" base]) end; |
228 in NameSpace.implode (prefix @ [space_implode "_" base]) end; |
231 |
229 |
232 |
230 |