1
(* this lib should not depend on core *)
12
if n = 0 then loop xs ys
20
val compare : t -> t -> int
21
val equal : t -> t -> bool
22
val next : string -> t
24
val name : t -> string
31
let compare a b = Pervasives.compare (a.code : int) b.code
32
let equal a b = Pervasives.(=) (a.code : int) b.code
34
let next name = let code = !uid in incr uid; {code; name}
35
let hash a = Hashtbl.hash a.code
37
let static = next "static"
45
let rec compare k1 k2 =
46
if k1 == k2 then 0 else
47
let cmp = Uid.compare k1.uid k2.uid in
48
if cmp <> 0 then cmp else
49
List.compare ~cmp:compare k1.params k2.params
50
let equal a b = compare a b = 0
51
let hash = (Hashtbl.hash : t -> int)
52
let static = { uid = Uid.static ; params = [] }
56
type 'a typename = 'a t
60
let name t = Uid.name t.Key.uid
61
let static = Key.static
63
let create ?(name="Typename.create") () = { Key.uid = Uid.next name ; params = [] }
66
(* The argument for Obj.magic here is the same as the one in core/type_equal *)
68
let same (type a) (type b) (nm1 : a t) (nm2 : b t) = Key.compare nm1 nm2 = 0
70
let same_witness (type a) (type b) (nm1 : a t) (nm2 : b t) =
71
if Key.compare nm1 nm2 = 0
72
then Some (Obj.magic Type_equal.refl : (a, b) Type_equal.t)
75
let same_witness_exn (type a) (type b) (nm1 : a t) (nm2 : b t) =
76
if Key.compare nm1 nm2 = 0
77
then (Obj.magic Type_equal.refl : (a, b) Type_equal.t)
78
else failwith "Typename.same_witness_exn"
84
val typename_of_t : t typename
89
val typename_of_t : 'a typename -> 'a t typename
97
-> ('a, 'b) t typename
106
-> ('a, 'b, 'c) t typename
110
type ('a, 'b, 'c, 'd) t
116
-> ('a, 'b, 'c, 'd) t typename
120
type ('a, 'b, 'c, 'd, 'e) t
127
-> ('a, 'b, 'c, 'd, 'e) t typename
130
module Make0 (X : Named_intf.S0) = struct
131
let uid = Uid.next X.name
132
let typename_of_t = { Key.uid ; params = [] }
135
module Make1 (X : Named_intf.S1) = struct
136
let uid = Uid.next X.name
137
let typename_of_t a = { Key.uid ; params = [ a ] }
140
module Make2 (X : Named_intf.S2) = struct
141
let uid = Uid.next X.name
142
let typename_of_t a b = { Key.uid ; params = [ a ; b ] }
145
module Make3 (X : Named_intf.S3) = struct
146
let uid = Uid.next X.name
147
let typename_of_t a b c = { Key.uid ; params = [ a ; b ; c ] }
150
module Make4 (X : Named_intf.S4) = struct
151
let uid = Uid.next X.name
152
let typename_of_t a b c d = { Key.uid ; params = [ a ; b ; c ; d ] }
155
module Make5 (X : Named_intf.S5) = struct
156
let uid = Uid.next X.name
157
let typename_of_t a b c d e = { Key.uid ; params = [ a ; b ; c ; d ; e ] }
160
module Key_table = Hashtbl.Make (Key)
162
module Table (X : sig
166
type data = Data : 'a t * 'a X.t -> data
167
type t = data Key_table.t
169
let create int = Key_table.create int
171
let mem table name = Key_table.mem table (key name)
173
let set table name data =
174
Key_table.replace table (key name) (Data (name, data))
176
let find (type a) table (name : a typename) =
178
try Some (Key_table.find table (key name))
179
with Not_found -> None
183
| Some (Data (name', data)) ->
184
(fun (type b) (name' : b typename) (data : b X.t) ->
185
let Type_equal.T = (same_witness_exn name' name : (b, a) Type_equal.t) in
190
let fail uid_a uid_b =
192
Printf.sprintf "Typename.Same_witness_exn %S %S" (Uid.name uid_a) (Uid.name uid_b)
196
module Same_witness_exn_1 (A : S1) (B : S1) = struct
197
type t = { eq : 'a. ('a A.t, 'a B.t) Type_equal.t }
200
let uid_a = uid (A.typename_of_t static) in
201
let uid_b = uid (B.typename_of_t static) in
202
if Uid.equal uid_a uid_b
203
then { eq = Obj.magic Type_equal.refl }
204
else fail uid_a uid_b
207
module Same_witness_exn_2 (A : S2) (B : S2) = struct
209
eq : 'a 'b. ( ('a, 'b) A.t,
210
('a, 'b) B.t ) Type_equal.t
214
let uid_a = uid (A.typename_of_t static static) in
215
let uid_b = uid (B.typename_of_t static static) in
216
if Uid.equal uid_a uid_b
217
then { eq = Obj.magic Type_equal.refl }
218
else fail uid_a uid_b
221
module Same_witness_exn_3 (A : S3) (B : S3) = struct
223
eq : 'a 'b 'c. ( ('a, 'b, 'c) A.t,
224
('a, 'b, 'c) B.t ) Type_equal.t
228
let uid_a = uid (A.typename_of_t static static static) in
229
let uid_b = uid (B.typename_of_t static static static) in
230
if Uid.equal uid_a uid_b
231
then { eq = Obj.magic Type_equal.refl }
232
else fail uid_a uid_b
235
module Same_witness_exn_4 (A : S4) (B : S4) = struct
237
eq : 'a 'b 'c 'd. ( ('a, 'b, 'c, 'd) A.t,
238
('a, 'b, 'c, 'd) B.t ) Type_equal.t
242
let uid_a = uid (A.typename_of_t static static static static) in
243
let uid_b = uid (B.typename_of_t static static static static) in
244
if Uid.equal uid_a uid_b
245
then { eq = Obj.magic Type_equal.refl }
246
else fail uid_a uid_b
249
module Same_witness_exn_5 (A : S5) (B : S5) = struct
251
eq : 'a 'b 'c 'd 'e. ( ('a, 'b, 'c, 'd, 'e) A.t,
252
('a, 'b, 'c, 'd, 'e) B.t ) Type_equal.t
256
let uid_a = uid (A.typename_of_t static static static static static) in
257
let uid_b = uid (B.typename_of_t static static static static static) in
258
if Uid.equal uid_a uid_b
259
then { eq = Obj.magic Type_equal.refl }
260
else fail uid_a uid_b