author | wenzelm |
Mon, 02 Dec 2024 22:16:29 +0100 | |
changeset 81541 | 5335b1ca6233 |
parent 81521 | 1bfad73ab115 |
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 |
|
74411 | 26 |
val build_context: (context -> context) -> context |
20158 | 27 |
val make_context: string list -> context |
20089 | 28 |
val declare: string -> context -> context |
80601 | 29 |
val declare_renamed: string * string -> context -> context |
20158 | 30 |
val is_declared: context -> string -> bool |
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
31 |
val invent: context -> string -> int -> string list |
81513 | 32 |
val invent': string -> int -> context -> string list * context |
81507 | 33 |
val invent_global: string -> int -> string list |
34 |
val invent_global_types: int -> string list |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
35 |
val invent_names: context -> string -> 'a list -> (string * 'a) list |
81507 | 36 |
val invent_names_global: string -> 'a list -> (string * 'a) list |
81509 | 37 |
val invent_types: context -> 'a list -> (string * 'a) list |
81507 | 38 |
val invent_types_global: 'a list -> (string * 'a) list |
20089 | 39 |
val invent_list: string list -> string -> int -> string list |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
40 |
val variant: string -> context -> string * context |
80599 | 41 |
val variant_bound: string -> context -> string * context |
81517 | 42 |
val variant_names: context -> (string * 'a) list -> (string * 'a) list |
43 |
val variant_names_build: (context -> context) -> (string * 'a) list -> (string * 'a) list |
|
81521 | 44 |
val variants: context -> string list -> string list |
20089 | 45 |
val variant_list: string list -> string list -> string list |
56812 | 46 |
val enforce_case: bool -> string -> string |
56811 | 47 |
val desymbolize: bool option -> string -> string |
20089 | 48 |
end; |
49 |
||
50 |
structure Name: NAME = |
|
51 |
struct |
|
52 |
||
24849 | 53 |
(** common defaults **) |
54 |
||
55 |
val uu = "uu"; |
|
43683 | 56 |
val uu_ = "uu_"; |
24849 | 57 |
val aT = "'a"; |
58 |
||
59 |
||
20089 | 60 |
|
61 |
(** special variable names **) |
|
62 |
||
63 |
(* encoded bounds *) |
|
64 |
||
65 |
(*names for numbered variables -- |
|
66 |
preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*) |
|
67 |
||
68 |
val small_int = Vector.tabulate (1000, fn i => |
|
69 |
let val leading = if i < 10 then "00" else if i < 100 then "0" else "" |
|
70 |
in ":" ^ leading ^ string_of_int i end); |
|
71 |
||
72 |
fun bound n = |
|
77846
5ba68d3bd741
more operations, following Isabelle/ML conventions;
wenzelm
parents:
74411
diff
changeset
|
73 |
if n < 1000 then Vector.nth small_int n |
5ba68d3bd741
more operations, following Isabelle/ML conventions;
wenzelm
parents:
74411
diff
changeset
|
74 |
else ":" ^ bound (n div 1000) ^ Vector.nth small_int (n mod 1000); |
20089 | 75 |
|
76 |
val is_bound = String.isPrefix ":"; |
|
77 |
||
78 |
||
55949 | 79 |
(* internal names -- NB: internal subsumes skolem *) |
20089 | 80 |
|
81 |
val internal = suffix "_"; |
|
82 |
val dest_internal = unsuffix "_"; |
|
55948 | 83 |
val is_internal = String.isSuffix "_"; |
55949 | 84 |
fun reject_internal (x, ps) = |
85 |
if is_internal x then error ("Bad name: " ^ quote x ^ Position.here_list ps) else (); |
|
20089 | 86 |
|
87 |
val skolem = suffix "__"; |
|
88 |
val dest_skolem = unsuffix "__"; |
|
55948 | 89 |
val is_skolem = String.isSuffix "__"; |
55949 | 90 |
fun reject_skolem (x, ps) = |
91 |
if is_skolem x then error ("Bad name: " ^ quote x ^ Position.here_list ps) else (); |
|
20089 | 92 |
|
20099 | 93 |
fun clean_index (x, i) = |
94 |
(case try dest_internal x of |
|
95 |
NONE => (x, i) |
|
96 |
| SOME x' => clean_index (x', i + 1)); |
|
20089 | 97 |
|
98 |
fun clean x = #1 (clean_index (x, 0)); |
|
99 |
||
100 |
||
101 |
||
102 |
(** generating fresh names **) |
|
103 |
||
104 |
(* context *) |
|
105 |
||
106 |
datatype context = |
|
107 |
Context of string option Symtab.table; (*declared names with latest renaming*) |
|
108 |
||
20099 | 109 |
fun declare x (Context tab) = |
110 |
Context (Symtab.default (clean x, NONE) tab); |
|
111 |
||
112 |
fun declare_renaming (x, x') (Context tab) = |
|
113 |
Context (Symtab.update (clean x, SOME (clean x')) tab); |
|
20089 | 114 |
|
80601 | 115 |
fun declare_renamed (x, x') = |
116 |
clean x <> clean x' ? declare_renaming (x, x') #> declare x'; |
|
117 |
||
20158 | 118 |
fun is_declared (Context tab) = Symtab.defined tab; |
20089 | 119 |
fun declared (Context tab) = Symtab.lookup tab; |
120 |
||
20121 | 121 |
val context = Context Symtab.empty |> fold declare ["", "'"]; |
74411 | 122 |
|
123 |
fun build_context (f: context -> context) = f context; |
|
124 |
||
125 |
val make_context = build_context o fold declare; |
|
20089 | 126 |
|
127 |
||
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
128 |
(* invent names *) |
20089 | 129 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
130 |
fun invent ctxt = |
20089 | 131 |
let |
132 |
fun invs _ 0 = [] |
|
133 |
| invs x n = |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
134 |
let val x' = Symbol.bump_string x |
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
135 |
in if is_declared ctxt x then invs x' n else x :: invs x' (n - 1) end; |
20099 | 136 |
in invs o clean end; |
20089 | 137 |
|
81513 | 138 |
fun invent' x n ctxt = |
139 |
let val xs = invent ctxt x n |
|
140 |
in (xs, fold declare xs ctxt) end; |
|
141 |
||
81507 | 142 |
val invent_global = invent context; |
143 |
val invent_global_types = invent_global aT; |
|
144 |
||
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
145 |
fun invent_names ctxt x xs = invent ctxt x (length xs) ~~ xs; |
81507 | 146 |
fun invent_names_global x xs = invent_names context x xs; |
81509 | 147 |
|
148 |
fun invent_types ctxt xs = invent_names ctxt aT xs; |
|
149 |
fun invent_types_global xs = invent_types context xs; |
|
20198 | 150 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
151 |
val invent_list = invent o make_context; |
20089 | 152 |
|
153 |
||
154 |
(* variants *) |
|
155 |
||
156 |
(*makes a variant of a name distinct from already used names in a |
|
157 |
context; preserves a suffix of underscores "_"*) |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
158 |
fun variant name ctxt = |
20089 | 159 |
let |
160 |
fun vary x = |
|
161 |
(case declared ctxt x of |
|
162 |
NONE => x |
|
163 |
| SOME x' => vary (Symbol.bump_string (the_default x x'))); |
|
164 |
||
20121 | 165 |
val (x, n) = clean_index (name, 0); |
20089 | 166 |
val (x', ctxt') = |
20158 | 167 |
if not (is_declared ctxt x) then (x, declare x ctxt) |
20089 | 168 |
else |
169 |
let |
|
170 |
val x0 = Symbol.bump_init x; |
|
171 |
val x' = vary x0; |
|
80601 | 172 |
val ctxt' = ctxt |> declare_renamed (x0, x'); |
20089 | 173 |
in (x', ctxt') end; |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
174 |
in (x' ^ replicate_string n "_", ctxt') end; |
20089 | 175 |
|
80599 | 176 |
fun variant_bound name = variant (if is_bound name then "u" else name); |
177 |
||
81517 | 178 |
fun variant_names ctxt xs = #1 (fold_map (variant o fst) xs ctxt) ~~ map snd xs; |
179 |
fun variant_names_build f xs = variant_names (build_context f) xs; |
|
180 |
||
81521 | 181 |
fun variants ctxt xs = #1 (fold_map variant xs ctxt); |
182 |
val variant_list = variants o make_context; |
|
20089 | 183 |
|
31013 | 184 |
|
31035 | 185 |
(* names conforming to typical requirements of identifiers in the world outside *) |
31013 | 186 |
|
69137
90fce429e1bc
Jenkins: run ocaml_setup
Lars Hupel <lars.hupel@mytum.de>
parents:
56812
diff
changeset
|
187 |
fun enforce_case' false cs = |
56812 | 188 |
(if forall Symbol.is_ascii_upper cs then map else nth_map 0) Symbol.to_ascii_lower cs |
69137
90fce429e1bc
Jenkins: run ocaml_setup
Lars Hupel <lars.hupel@mytum.de>
parents:
56812
diff
changeset
|
189 |
| enforce_case' true cs = |
56812 | 190 |
nth_map 0 Symbol.to_ascii_upper cs; |
191 |
||
192 |
fun enforce_case upper = implode o enforce_case' upper o raw_explode; |
|
193 |
||
56811 | 194 |
fun desymbolize perhaps_upper "" = |
195 |
if the_default false perhaps_upper then "X" else "x" |
|
196 |
| desymbolize perhaps_upper s = |
|
31013 | 197 |
let |
31035 | 198 |
val xs as (x :: _) = Symbol.explode s; |
33547 | 199 |
val ys = |
200 |
if Symbol.is_ascii_letter x orelse Symbol.is_symbolic x then xs |
|
31013 | 201 |
else "x" :: xs; |
202 |
fun is_valid x = |
|
34307 | 203 |
Symbol.is_ascii_letter x orelse Symbol.is_ascii_digit x; |
31013 | 204 |
fun sep [] = [] |
205 |
| sep (xs as "_" :: _) = xs |
|
206 |
| sep xs = "_" :: xs; |
|
31035 | 207 |
fun desep ("_" :: xs) = xs |
208 |
| desep xs = xs; |
|
33547 | 209 |
fun desymb x xs = |
210 |
if is_valid x then x :: xs |
|
31013 | 211 |
else |
33547 | 212 |
(case Symbol.decode x of |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
34307
diff
changeset
|
213 |
Symbol.Sym name => "_" :: raw_explode name @ sep xs |
33547 | 214 |
| _ => sep xs); |
56812 | 215 |
val upper_lower = Option.map enforce_case' perhaps_upper |> the_default I; |
31035 | 216 |
in fold_rev desymb ys [] |> desep |> upper_lower |> implode end; |
31013 | 217 |
|
20089 | 218 |
end; |