1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
9
(* $Id: nameops.ml 9433 2006-12-12 09:38:53Z herbelin $ *)
17
let pr_id id = str (string_of_id id)
19
let pr_name = function
20
| Anonymous -> str "_"
25
let code_of_0 = Char.code '0'
26
let code_of_9 = Char.code '9'
28
let cut_ident skip_quote s =
29
let s = string_of_id s in
30
let slen = String.length s in
31
(* [n'] is the position of the first non nullary digit *)
32
let rec numpart n n' =
34
(* ident made of _ and digits only [and ' if skip_quote]: don't cut it *)
37
let c = Char.code (String.get s (n-1)) in
38
if c = code_of_0 && n <> slen then
40
else if code_of_0 <= c && c <= code_of_9 then
42
else if skip_quote & (c = Char.code '\'' || c = Char.code '_') then
50
let numstart = cut_ident false s in
51
let s = string_of_id s in
52
let slen = String.length s in
53
if numstart = slen then
56
(String.sub s 0 numstart,
57
Some (int_of_string (String.sub s numstart (slen - numstart))))
59
let make_ident sa = function
61
let c = Char.code (String.get sa (String.length sa -1)) in
63
if c < code_of_0 or c > code_of_9 then sa ^ (string_of_int n)
64
else sa ^ "_" ^ (string_of_int n) in
66
| None -> id_of_string (String.copy sa)
69
let suffixstart = cut_ident true id in
70
id_of_string (String.sub (string_of_id id) 0 suffixstart)
72
(* Rem: semantics is a bit different, if an ident starts with toto00 then
73
after successive renamings it comes to toto09, then it goes on with toto10 *)
74
let lift_subscript id =
75
let id = string_of_id id in
76
let len = String.length id in
77
let rec add carrypos =
78
let c = id.[carrypos] in
85
let newid = String.copy id in
86
String.fill newid (carrypos+1) (len-1-carrypos) '0';
87
newid.[carrypos] <- Char.chr (Char.code c + 1);
92
if carrypos < len-1 then begin
93
String.fill newid (carrypos+1) (len-1-carrypos) '0';
94
newid.[carrypos+1] <- '1'
98
in id_of_string (add (len-1))
100
let has_subscript id =
101
let id = string_of_id id in
102
is_digit (id.[String.length id - 1])
104
let forget_subscript id =
105
let numstart = cut_ident false id in
106
let newid = String.make (numstart+1) '0' in
107
String.blit (string_of_id id) 0 newid 0 numstart;
110
let add_suffix id s = id_of_string (string_of_id id ^ s)
111
let add_prefix s id = id_of_string (s ^ string_of_id id)
113
let atompart_of_id id = fst (repr_ident id)
117
let lift_ident = lift_subscript
119
let next_ident_away id avoid =
120
if List.mem id avoid then
121
let id0 = if not (has_subscript id) then id else
122
(* Ce serait sans doute mieux avec quelque chose inspir� de
123
*** make_ident id (Some 0) *** mais �a brise la compatibilit�... *)
124
forget_subscript id in
125
let rec name_rec id =
126
if List.mem id avoid then name_rec (lift_ident id) else id in
130
let next_ident_away_from id avoid =
131
let rec name_rec id =
132
if List.mem id avoid then name_rec (lift_ident id) else id in
137
let out_name = function
139
| Anonymous -> failwith "out_name: expects a defined name"
141
let name_fold f na a =
151
let name_app f = function
152
| Name id -> Name (f id)
153
| Anonymous -> Anonymous
155
let name_fold_map f e = function
156
| Name id -> let (e,id) = f e id in (e,Name id)
157
| Anonymous -> e,Anonymous
159
let next_name_away_with_default default name l =
161
| Name str -> next_ident_away str l
162
| Anonymous -> next_ident_away (id_of_string default) l
164
let next_name_away = next_name_away_with_default "H"
166
let pr_lab l = str (string_of_label l)
168
let default_library = Names.initial_dir (* = ["Top"] *)
170
(*s Roots of the space of absolute names *)
171
let coq_root = id_of_string "Coq"
172
let default_root_prefix = make_dirpath []
176
let string_of_meta = string_of_int