Debian Codegraph @

library/libnames.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Util
12 open Names
13
14 (**********************************************)
15
16 (*s Operations on dirpaths *)
17
18 let split_dirpath d = match DirPath.repr d with
19 | id :: l -> DirPath.make l, id
20 | _ -> failwith "split_dirpath"
21
22 let pop_dirpath p = match DirPath.repr p with
23 | _::l -> DirPath.make l
24 | [] -> failwith "pop_dirpath"
25
26 (* Pop the last n module idents *)
27 let pop_dirpath_n n dir = DirPath.make (List.skipn n (DirPath.repr dir))
28
29 let is_dirpath_prefix_of d1 d2 =
30 List.prefix_of Id.equal
31 (List.rev (DirPath.repr d1)) (List.rev (DirPath.repr d2))
32
33 let is_dirpath_suffix_of dir1 dir2 =
34 let dir1 = DirPath.repr dir1 in
35 let dir2 = DirPath.repr dir2 in
36 List.prefix_of Id.equal dir1 dir2
37
38 let drop_dirpath_prefix d1 d2 =
39 let d =
40 List.drop_prefix Id.equal
41 (List.rev (DirPath.repr d1)) (List.rev (DirPath.repr d2))
42 in
43 DirPath.make (List.rev d)
44
45 let append_dirpath d1 d2 = DirPath.make (DirPath.repr d2 @ DirPath.repr d1)
46
47 let add_dirpath_suffix p id = DirPath.make (id :: DirPath.repr p)
48
49 (* parsing *)
50 let parse_dir s =
51 let len = String.length s in
52 let rec decoupe_dirs dirs n =
53 if Int.equal n len && n > 0 then
54 CErrors.user_err Pp.(str @@ s ^ " is an invalid path.");
55 if n >= len then dirs else
56 let pos =
57 try
58 String.index_from s n '.'
59 with Not_found -> len
60 in
61 if Int.equal pos n then
62 CErrors.user_err Pp.(str @@ s ^ " is an invalid path.");
63 let dir = String.sub s n (pos-n) in
64 decoupe_dirs ((Id.of_string dir)::dirs) (pos+1)
65 in
66 decoupe_dirs [] 0
67
68 let dirpath_of_string s =
69 let path = match s with
70 | "" -> []
71 | _ -> parse_dir s
72 in
73 DirPath.make path
74
75 (** Absolute paths *)
76
77 type full_path = {
78 dirpath : DirPath.t;
79 basename : Id.t;
80 }
81
82 let dirpath sp = sp.dirpath
83 let basename sp = sp.basename
84
85 let dirpath_of_path { dirpath; basename } =
86 add_dirpath_suffix dirpath basename
87
88 let is_path_prefix_of p dp = is_dirpath_prefix_of (dirpath_of_path p) dp
89
90 let make_path pa id = { dirpath = pa; basename = id }
91
92 let make_path0 dp =
93 let dp, id = split_dirpath dp in
94 make_path dp id
95
96 let dummy_full_path = make_path0 DirPath.dummy
97
98 let add_path_suffix { dirpath = pa0; basename = id0 } id =
99 { dirpath = add_dirpath_suffix pa0 id0; basename = id }
100
101 let append_path ({ dirpath = dp; basename = id0 } as p) dp' =
102 if DirPath.is_empty dp' then p
103 else
104 let dp', id = split_dirpath dp' in
105 { dirpath = append_dirpath (add_dirpath_suffix dp id0) dp'; basename = id }
106
107 let path_pop_n_suffixes n ({ dirpath = dp; } as path) =
108 if n = 0 then path
109 else
110 let dp = pop_dirpath_n (n-1) dp in
111 make_path0 dp
112
113 let path_pop_suffix p = path_pop_n_suffixes 1 p
114
115 let repr_path { dirpath = pa; basename = id } = (pa,id)
116
117 let eq_full_path p1 p2 =
118 Id.equal p1.basename p2.basename &&
119 DirPath.equal p1.dirpath p2.dirpath
120
121 (* parsing and printing of section paths *)
122 let string_of_path sp =
123 let (sl,id) = repr_path sp in
124 match DirPath.repr sl with
125 | [] -> Id.to_string id
126 | _ -> (DirPath.to_string sl) ^ "." ^ (Id.to_string id)
127
128 let compare_full_path sp1 sp2 =
129 let (p1,id1) = repr_path sp1
130 and (p2,id2) = repr_path sp2 in
131 let p_bit = DirPath.compare p1 p2 in
132 if Int.equal p_bit 0 then Id.compare id1 id2 else p_bit
133
134 module SpOrdered =
135 struct
136 type t = full_path
137 let compare = compare_full_path
138 end
139
140 module Spmap = Map.Make(SpOrdered)
141
142 let path_of_string s =
143 try
144 let dir, id = split_dirpath (dirpath_of_string s) in
145 make_path dir id
146 with
147 | Invalid_argument _ -> invalid_arg "path_of_string"
148
149 let pr_path sp = Pp.str (string_of_path sp)
150
151 (*s qualified names *)
152 type qualid_r = full_path
153 type qualid = qualid_r CAst.t
154
155 let make_qualid ?loc pa id = CAst.make ?loc (make_path pa id)
156 let repr_qualid {CAst.v=qid} = repr_path qid
157
158 let qualid_eq qid1 qid2 = eq_full_path qid1.CAst.v qid2.CAst.v
159
160 let is_qualid_suffix_of_full_path
161 CAst.{v={dirpath=dirpath1;basename=basename1}} {dirpath=dirpath2;basename=basename2} =
162 let dir1 = DirPath.repr dirpath1 in
163 let dir2 = DirPath.repr dirpath2 in
164 Id.equal basename1 basename2 && List.prefix_of Id.equal dir1 dir2
165
166 let string_of_qualid qid = string_of_path qid.CAst.v
167 let pr_qualid qid = pr_path qid.CAst.v
168
169 let qualid_of_string ?loc s = CAst.make ?loc @@ path_of_string s
170
171 let qualid_of_path ?loc sp = CAst.make ?loc sp
172 let qualid_of_ident ?loc id = make_qualid ?loc DirPath.empty id
173 let qualid_of_dirpath ?loc dir =
174 let (l,a) = split_dirpath dir in
175 make_qualid ?loc l a
176
177 let qualid_of_lident lid = qualid_of_ident ?loc:lid.CAst.loc lid.CAst.v
178
179 let qualid_is_ident qid = DirPath.is_empty (dirpath qid.CAst.v)
180
181 let qualid_basename qid =
182 qid.CAst.v.basename
183
184 let qualid_path qid =
185 qid.CAst.v.dirpath
186
187 let idset_mem_qualid qid s =
188 qualid_is_ident qid && Id.Set.mem (qualid_basename qid) s
189
190 (* Default paths *)
191
192 (*s Roots of the space of absolute names *)
193 let rocq_init_string = "Corelib"
194 let rocq_init_root = Id.of_string rocq_init_string
195 let default_root_prefix = DirPath.empty