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