author | haftmann |
Sun, 22 Mar 2009 11:56:32 +0100 | |
changeset 30648 | 17365ef082f3 |
parent 30364 | 577edc39b501 |
child 31013 | 69a476d6fea6 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_name.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
4 |
Some code generator infrastructure concerning names. |
24219 | 5 |
*) |
6 |
||
7 |
signature CODE_NAME = |
|
8 |
sig |
|
9 |
val purify_var: string -> string |
|
10 |
val purify_tvar: string -> string |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
11 |
val purify_base: string -> string |
24219 | 12 |
val check_modulename: string -> string |
27103 | 13 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
14 |
val read_const_exprs: theory -> string list -> string list * string list |
24219 | 15 |
end; |
16 |
||
28054 | 17 |
structure Code_Name: CODE_NAME = |
24219 | 18 |
struct |
19 |
||
20 |
(** purification **) |
|
21 |
||
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
22 |
fun purify_name upper = |
24219 | 23 |
let |
24 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'"; |
|
25 |
val is_junk = not o is_valid andf Symbol.is_regular; |
|
26 |
val junk = Scan.many is_junk; |
|
27 |
val scan_valids = Symbol.scanner "Malformed input" |
|
28 |
((junk |-- |
|
29 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
|
30 |
--| junk)) |
|
25999 | 31 |
::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk)); |
25337 | 32 |
fun upper_lower cs = if upper then nth_map 0 Symbol.to_ascii_upper cs |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
33 |
else (if forall Symbol.is_ascii_upper cs |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
34 |
then map else nth_map 0) Symbol.to_ascii_lower cs; |
24219 | 35 |
in |
36 |
explode |
|
37 |
#> scan_valids |
|
38 |
#> space_implode "_" |
|
39 |
#> explode |
|
40 |
#> upper_lower |
|
41 |
#> implode |
|
42 |
end; |
|
43 |
||
44 |
fun purify_var "" = "x" |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
45 |
| purify_var v = purify_name false v; |
24219 | 46 |
|
47 |
fun purify_tvar "" = "'a" |
|
48 |
| purify_tvar v = |
|
49 |
(unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v; |
|
50 |
||
51 |
val purify_prefix = |
|
52 |
explode |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
53 |
(*FIMXE should disappear as soon as hierarchical theory name spaces are available*) |
24219 | 54 |
#> Symbol.scanner "Malformed name" |
55 |
(Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.is_regular)) |
|
56 |
#> implode |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30161
diff
changeset
|
57 |
#> Long_Name.explode |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
58 |
#> map (purify_name true); |
24219 | 59 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
60 |
(*FIMXE non-canonical function treating non-canonical names*) |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
61 |
fun purify_base "op &" = "and" |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
62 |
| purify_base "op |" = "or" |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
63 |
| purify_base "op -->" = "implies" |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
64 |
| purify_base "op :" = "member" |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
65 |
| purify_base "*" = "product" |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
66 |
| purify_base "+" = "sum" |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
67 |
| purify_base s = if String.isPrefix "op =" s |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
68 |
then "eq" ^ purify_name false s |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
69 |
else purify_name false s; |
24219 | 70 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
71 |
fun check_modulename mn = |
24219 | 72 |
let |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30161
diff
changeset
|
73 |
val mns = Long_Name.explode mn; |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30364
diff
changeset
|
74 |
val mns' = map (purify_name true) mns; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
75 |
in |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
76 |
if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n" |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30161
diff
changeset
|
77 |
^ "perhaps try " ^ quote (Long_Name.implode mns')) |
24219 | 78 |
end; |
79 |
||
80 |
||
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
81 |
(** misc **) |
24219 | 82 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
83 |
fun read_const_exprs thy = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
84 |
let |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
85 |
fun consts_of some_thyname = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
86 |
let |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
87 |
val thy' = case some_thyname |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
88 |
of SOME thyname => ThyInfo.the_theory thyname thy |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
89 |
| NONE => thy; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
90 |
val cs = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
91 |
((snd o #constants o Consts.dest o #consts o Sign.rep_sg) thy') []; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
92 |
fun belongs_here c = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
93 |
not (exists (fn thy'' => Sign.declared_const thy'' c) (Theory.parents_of thy')) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
94 |
in case some_thyname |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
95 |
of NONE => cs |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
96 |
| SOME thyname => filter belongs_here cs |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
97 |
end; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
98 |
fun read_const_expr "*" = ([], consts_of NONE) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
99 |
| read_const_expr s = if String.isSuffix ".*" s |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
100 |
then ([], consts_of (SOME (unsuffix ".*" s))) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
101 |
else ([Code_Unit.read_const thy s], []); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28346
diff
changeset
|
102 |
in pairself flat o split_list o map read_const_expr end; |
24219 | 103 |
|
104 |
end; |