author | wenzelm |
Tue, 22 Apr 2014 23:49:15 +0200 | |
changeset 56662 | f373fb77e0a4 |
parent 55949 | 4766342e8376 |
child 56811 | b66639331db5 |
permissions | -rw-r--r-- |
20089 | 1 |
(* Title: Pure/name.ML |
2 |
Author: Makarius |
|
3 |
||
28965 | 4 |
Names of basic logical entities (variables etc.). |
20089 | 5 |
*) |
6 |
||
7 |
signature NAME = |
|
8 |
sig |
|
24849 | 9 |
val uu: string |
43683 | 10 |
val uu_: string |
24849 | 11 |
val aT: string |
20089 | 12 |
val bound: int -> string |
13 |
val is_bound: string -> bool |
|
14 |
val internal: string -> string |
|
15 |
val dest_internal: string -> string |
|
55948 | 16 |
val is_internal: string -> bool |
55949 | 17 |
val reject_internal: string * Position.T list -> unit |
20089 | 18 |
val skolem: string -> string |
19 |
val dest_skolem: string -> string |
|
55948 | 20 |
val is_skolem: string -> bool |
55949 | 21 |
val reject_skolem: string * Position.T list -> unit |
20089 | 22 |
val clean_index: string * int -> string * int |
23 |
val clean: string -> string |
|
24 |
type context |
|
25 |
val context: context |
|
20158 | 26 |
val make_context: string list -> context |
20089 | 27 |
val declare: string -> context -> context |
20158 | 28 |
val is_declared: context -> string -> bool |
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
29 |
val invent: context -> string -> int -> string list |
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
30 |
val invent_names: context -> string -> 'a list -> (string * 'a) list |
20089 | 31 |
val invent_list: string list -> string -> int -> string list |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
32 |
val variant: string -> context -> string * context |
20089 | 33 |
val variant_list: string list -> string list -> string list |
31035 | 34 |
val desymbolize: bool -> string -> string |
20089 | 35 |
end; |
36 |
||
37 |
structure Name: NAME = |
|
38 |
struct |
|
39 |
||
24849 | 40 |
(** common defaults **) |
41 |
||
42 |
val uu = "uu"; |
|
43683 | 43 |
val uu_ = "uu_"; |
24849 | 44 |
val aT = "'a"; |
45 |
||
46 |
||
20089 | 47 |
|
48 |
(** special variable names **) |
|
49 |
||
50 |
(* encoded bounds *) |
|
51 |
||
52 |
(*names for numbered variables -- |
|
53 |
preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*) |
|
54 |
||
55 |
val small_int = Vector.tabulate (1000, fn i => |
|
56 |
let val leading = if i < 10 then "00" else if i < 100 then "0" else "" |
|
57 |
in ":" ^ leading ^ string_of_int i end); |
|
58 |
||
59 |
fun bound n = |
|
60 |
if n < 1000 then Vector.sub (small_int, n) |
|
61 |
else ":" ^ bound (n div 1000) ^ Vector.sub (small_int, n mod 1000); |
|
62 |
||
63 |
val is_bound = String.isPrefix ":"; |
|
64 |
||
65 |
||
55949 | 66 |
(* internal names -- NB: internal subsumes skolem *) |
20089 | 67 |
|
68 |
val internal = suffix "_"; |
|
69 |
val dest_internal = unsuffix "_"; |
|
55948 | 70 |
val is_internal = String.isSuffix "_"; |
55949 | 71 |
fun reject_internal (x, ps) = |
72 |
if is_internal x then error ("Bad name: " ^ quote x ^ Position.here_list ps) else (); |
|
20089 | 73 |
|
74 |
val skolem = suffix "__"; |
|
75 |
val dest_skolem = unsuffix "__"; |
|
55948 | 76 |
val is_skolem = String.isSuffix "__"; |
55949 | 77 |
fun reject_skolem (x, ps) = |
78 |
if is_skolem x then error ("Bad name: " ^ quote x ^ Position.here_list ps) else (); |
|
20089 | 79 |
|
20099 | 80 |
fun clean_index (x, i) = |
81 |
(case try dest_internal x of |
|
82 |
NONE => (x, i) |
|
83 |
| SOME x' => clean_index (x', i + 1)); |
|
20089 | 84 |
|
85 |
fun clean x = #1 (clean_index (x, 0)); |
|
86 |
||
87 |
||
88 |
||
89 |
(** generating fresh names **) |
|
90 |
||
91 |
(* context *) |
|
92 |
||
93 |
datatype context = |
|
94 |
Context of string option Symtab.table; (*declared names with latest renaming*) |
|
95 |
||
20099 | 96 |
fun declare x (Context tab) = |
97 |
Context (Symtab.default (clean x, NONE) tab); |
|
98 |
||
99 |
fun declare_renaming (x, x') (Context tab) = |
|
100 |
Context (Symtab.update (clean x, SOME (clean x')) tab); |
|
20089 | 101 |
|
20158 | 102 |
fun is_declared (Context tab) = Symtab.defined tab; |
20089 | 103 |
fun declared (Context tab) = Symtab.lookup tab; |
104 |
||
20121 | 105 |
val context = Context Symtab.empty |> fold declare ["", "'"]; |
20089 | 106 |
fun make_context used = fold declare used context; |
107 |
||
108 |
||
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
109 |
(* invent names *) |
20089 | 110 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
111 |
fun invent ctxt = |
20089 | 112 |
let |
113 |
fun invs _ 0 = [] |
|
114 |
| invs x n = |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
115 |
let val x' = Symbol.bump_string x |
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
116 |
in if is_declared ctxt x then invs x' n else x :: invs x' (n - 1) end; |
20099 | 117 |
in invs o clean end; |
20089 | 118 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
119 |
fun invent_names ctxt x xs = invent ctxt x (length xs) ~~ xs; |
20198 | 120 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
121 |
val invent_list = invent o make_context; |
20089 | 122 |
|
123 |
||
124 |
(* variants *) |
|
125 |
||
126 |
(*makes a variant of a name distinct from already used names in a |
|
127 |
context; preserves a suffix of underscores "_"*) |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
128 |
fun variant name ctxt = |
20089 | 129 |
let |
130 |
fun vary x = |
|
131 |
(case declared ctxt x of |
|
132 |
NONE => x |
|
133 |
| SOME x' => vary (Symbol.bump_string (the_default x x'))); |
|
134 |
||
20121 | 135 |
val (x, n) = clean_index (name, 0); |
20089 | 136 |
val (x', ctxt') = |
20158 | 137 |
if not (is_declared ctxt x) then (x, declare x ctxt) |
20089 | 138 |
else |
139 |
let |
|
140 |
val x0 = Symbol.bump_init x; |
|
141 |
val x' = vary x0; |
|
142 |
val ctxt' = ctxt |
|
21565 | 143 |
|> x0 <> x' ? declare_renaming (x0, x') |
20089 | 144 |
|> declare x'; |
145 |
in (x', ctxt') end; |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
146 |
in (x' ^ replicate_string n "_", ctxt') end; |
20089 | 147 |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
148 |
fun variant_list used names = #1 (make_context used |> fold_map variant names); |
20089 | 149 |
|
31013 | 150 |
|
31035 | 151 |
(* names conforming to typical requirements of identifiers in the world outside *) |
31013 | 152 |
|
31035 | 153 |
fun desymbolize upper "" = if upper then "X" else "x" |
154 |
| desymbolize upper s = |
|
31013 | 155 |
let |
31035 | 156 |
val xs as (x :: _) = Symbol.explode s; |
33547 | 157 |
val ys = |
158 |
if Symbol.is_ascii_letter x orelse Symbol.is_symbolic x then xs |
|
31013 | 159 |
else "x" :: xs; |
160 |
fun is_valid x = |
|
34307 | 161 |
Symbol.is_ascii_letter x orelse Symbol.is_ascii_digit x; |
31013 | 162 |
fun sep [] = [] |
163 |
| sep (xs as "_" :: _) = xs |
|
164 |
| sep xs = "_" :: xs; |
|
31035 | 165 |
fun desep ("_" :: xs) = xs |
166 |
| desep xs = xs; |
|
33547 | 167 |
fun desymb x xs = |
168 |
if is_valid x then x :: xs |
|
31013 | 169 |
else |
33547 | 170 |
(case Symbol.decode x of |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
34307
diff
changeset
|
171 |
Symbol.Sym name => "_" :: raw_explode name @ sep xs |
33547 | 172 |
| _ => sep xs); |
173 |
fun upper_lower cs = |
|
174 |
if upper then nth_map 0 Symbol.to_ascii_upper cs |
|
175 |
else |
|
176 |
(if forall Symbol.is_ascii_upper cs then map else nth_map 0) |
|
177 |
Symbol.to_ascii_lower cs; |
|
31035 | 178 |
in fold_rev desymb ys [] |> desep |> upper_lower |> implode end; |
31013 | 179 |
|
20089 | 180 |
end; |