summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-modules/pr7818.ml
blob: e6e03298a2334cbdd1129b1e4e431b1729045044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
(* TEST
 expect;
*)

(* cannot_alias.ml *)
module Termsig = struct
  module Term0 = struct
    module type S = sig
      module Id : sig end
    end
  end
  module Term = struct
    module type S = sig
      module Term0 : Term0.S
      module T = Term0
    end
  end
end;;
[%%expect{|
module Termsig :
  sig
    module Term0 : sig module type S = sig module Id : sig end end end
    module Term :
      sig module type S = sig module Term0 : Term0.S module T = Term0 end end
  end
|}]

module Make1 (T' : Termsig.Term.S) = struct
  module T = struct
    include T'.T
    let u = 1
  end
end;;
[%%expect{|
module Make1 :
  functor
    (T' : sig
            module Term0 : Termsig.Term0.S
            module T : sig module Id : sig end end
          end)
    -> sig module T : sig module Id : sig end val u : int end end
|}]

module Make2 (T' : Termsig.Term.S) = struct
  module T = struct
    include T'.T
    module Id2 = Id
    let u = 1
  end
end;;
[%%expect{|
module Make2 :
  functor
    (T' : sig
            module Term0 : Termsig.Term0.S
            module T : sig module Id : sig end end
          end)
    ->
    sig
      module T : sig module Id : sig end module Id2 = Id val u : int end
    end
|}]

module Make3 (T' : Termsig.Term.S) = struct
  module T = struct
    include T'.T
    module Id2 = Id
    let u = 1
    let u = 1
  end
end;;
[%%expect{|
module Make3 :
  functor
    (T' : sig
            module Term0 : Termsig.Term0.S
            module T : sig module Id : sig end end
          end)
    ->
    sig
      module T : sig module Id : sig end module Id2 = Id val u : int end
    end
|}]

(* cannot_alias2.ml *)
module type S = sig
  module Term0 : sig module Id : sig end end
  module T = Term0
end;;

module Make1 (T' : S)  = struct
  module Id = T'.T.Id
  module Id2 = Id
end;;
[%%expect{|
module type S =
  sig module Term0 : sig module Id : sig end end module T = Term0 end
module Make1 :
  functor
    (T' : sig
            module Term0 : sig module Id : sig end end
            module T : sig module Id : sig end end
          end)
    -> sig module Id : sig end module Id2 = Id end
|}]

module Make2 (T' : S) : sig module Id : sig end module Id2 = Id end
                        with module Id := T'.Term0.Id  = struct
  module Id = T'.T.Id
  module Id2 = Id
end;;
[%%expect{|
Lines 2-5, characters 57-3:
2 | .........................................................struct
3 |   module Id = T'.T.Id
4 |   module Id2 = Id
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig module Id : sig end module Id2 = Id end
       is not included in
         sig module Id2 = T'.Term0.Id end
       In module Id2:
       Module T'.Term0.Id cannot be aliased
|}]

module Make3 (T' : S) = struct
  module T = struct
    module Id = T'.T.Id
    module Id2 = Id
    let u = 1
    let u = 1
  end
end;;
[%%expect{|
module Make3 :
  functor
    (T' : sig
            module Term0 : sig module Id : sig end end
            module T : sig module Id : sig end end
          end)
    ->
    sig
      module T : sig module Id : sig end module Id2 = Id val u : int end
    end
|}]

(* unsoundness if Make1 returned an Id.x field *)
module M = Make1 (struct module Term0 =
  struct module Id = struct let x = "a" end end module T = Term0 end);;
M.Id.x;;
[%%expect{|
module M : sig module Id : sig end module Id2 = Id end
Line 3, characters 0-6:
3 | M.Id.x;;
    ^^^^^^
Error: Unbound value M.Id.x
|}]


(* cannot_alias3.ml *)
module MkT(X : sig end) = struct type t end
module type S = sig
  module Term0 : sig module Id : sig end end
  module T = Term0
  type t = MkT(T).t
end;;

module Make1 (T' : S)  = struct
  module Id = T'.T.Id
  module Id2 = Id
  type t = T'.t
end;;

module IS = struct
  module Term0 = struct module Id = struct let x = "a" end end
  module T = Term0
  type t = MkT(T).t
end;;

module M = Make1(IS);;
[%%expect{|
module MkT : functor (X : sig end) -> sig type t end
module type S =
  sig
    module Term0 : sig module Id : sig end end
    module T = Term0
    type t = MkT(T).t
  end
module Make1 :
  functor
    (T' : sig
            module Term0 : sig module Id : sig end end
            module T : sig module Id : sig end end
            type t = MkT(T).t
          end)
    -> sig module Id : sig end module Id2 = Id type t = T'.t end
module IS :
  sig
    module Term0 : sig module Id : sig val x : string end end
    module T = Term0
    type t = MkT(T).t
  end
module M : sig module Id : sig end module Id2 = Id type t = IS.t end
|}]


(* cannot_alias4.ml *)
(* Can be used to break module abstraction *)
(* Still sound ? *)
(* It seems to only work if Term0 and Term contain identical types *)
(* It may also be possible to do the same thing using
   Mtype.nondep_supertype anyway *)
type (_,_) eq = Eq : ('a,'a) eq
module MkT(X : Set.OrderedType) = Set.Make(X)
module type S = sig
  module Term0 : Set.OrderedType with type t = int
  module T = Term0
  type t = E of (MkT(T).t,MkT(T).t) eq
  type u = t = E of (MkT(Term0).t,MkT(T).t) eq
end;;
module F(X:S) = X;;
module rec M : S = M;;
module M' = F(M);;
module type S' = module type of M';;
module Asc = struct type t = int let compare x y = x - y end;;
module Desc = struct type t = int let compare x y = y - x end;;
module rec M1 : S' with module Term0 := Asc and module T := Desc = M1;;
(* And now we have a witness of MkT(Asc).t = MkT(Desc).t ... *)
let (E eq : M1.u) = (E Eq : M1.t);;
[%%expect{|
type (_, _) eq = Eq : ('a, 'a) eq
module MkT :
  functor (X : Set.OrderedType) ->
    sig
      type elt = X.t
      type t = Set.Make(X).t
      val empty : t
      val add : elt -> t -> t
      val singleton : elt -> t
      val remove : elt -> t -> t
      val union : t -> t -> t
      val inter : t -> t -> t
      val disjoint : t -> t -> bool
      val diff : t -> t -> t
      val cardinal : t -> int
      val elements : t -> elt list
      val min_elt : t -> elt
      val min_elt_opt : t -> elt option
      val max_elt : t -> elt
      val max_elt_opt : t -> elt option
      val choose : t -> elt
      val choose_opt : t -> elt option
      val find : elt -> t -> elt
      val find_opt : elt -> t -> elt option
      val find_first : (elt -> bool) -> t -> elt
      val find_first_opt : (elt -> bool) -> t -> elt option
      val find_last : (elt -> bool) -> t -> elt
      val find_last_opt : (elt -> bool) -> t -> elt option
      val iter : (elt -> unit) -> t -> unit
      val fold : (elt -> 'acc -> 'acc) -> t -> 'acc -> 'acc
      val map : (elt -> elt) -> t -> t
      val filter : (elt -> bool) -> t -> t
      val filter_map : (elt -> elt option) -> t -> t
      val partition : (elt -> bool) -> t -> t * t
      val split : elt -> t -> t * bool * t
      val is_empty : t -> bool
      val mem : elt -> t -> bool
      val equal : t -> t -> bool
      val compare : t -> t -> int
      val subset : t -> t -> bool
      val for_all : (elt -> bool) -> t -> bool
      val exists : (elt -> bool) -> t -> bool
      val to_list : t -> elt list
      val of_list : elt list -> t
      val to_seq_from : elt -> t -> elt Seq.t
      val to_seq : t -> elt Seq.t
      val to_rev_seq : t -> elt Seq.t
      val add_seq : elt Seq.t -> t -> t
      val of_seq : elt Seq.t -> t
    end
module type S =
  sig
    module Term0 : sig type t = int val compare : t -> t -> int end
    module T = Term0
    type t = E of (MkT(T).t, MkT(T).t) eq
    type u = t = E of (MkT(Term0).t, MkT(T).t) eq
  end
module F :
  functor
    (X : sig
           module Term0 : sig type t = int val compare : t -> t -> int end
           module T : sig type t = int val compare : t -> t -> int end
           type t = E of (MkT(T).t, MkT(T).t) eq
           type u = t = E of (MkT(Term0).t, MkT(T).t) eq
         end)
    ->
    sig
      module Term0 : sig type t = int val compare : t -> t -> int end
      module T : sig type t = int val compare : t -> t -> int end
      type t = X.t = E of (MkT(T).t, MkT(T).t) eq
      type u = t = E of (MkT(Term0).t, MkT(T).t) eq
    end
module rec M : S
module M' :
  sig
    module Term0 : sig type t = int val compare : t -> t -> int end
    module T : sig type t = int val compare : t -> t -> int end
    type t = M.t = E of (MkT(T).t, MkT(T).t) eq
    type u = t = E of (MkT(Term0).t, MkT(T).t) eq
  end
module type S' =
  sig
    module Term0 : sig type t = int val compare : t -> t -> int end
    module T : sig type t = int val compare : t -> t -> int end
    type t = M.t = E of (MkT(T).t, MkT(T).t) eq
    type u = t = E of (MkT(Term0).t, MkT(T).t) eq
  end
module Asc : sig type t = int val compare : int -> int -> int end
module Desc : sig type t = int val compare : int -> int -> int end
Line 15, characters 0-69:
15 | module rec M1 : S' with module Term0 := Asc and module T := Desc = M1;;
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This variant or record definition does not match that of type M.t
       Constructors do not match:
         E of (MkT(M.T).t, MkT(M.T).t) eq
       is not the same as:
         E of (MkT(Desc).t, MkT(Desc).t) eq
       The type (MkT(M.T).t, MkT(M.T).t) eq is not equal to the type
         (MkT(Desc).t, MkT(Desc).t) eq
       Type MkT(M.T).t = Set.Make(M.Term0).t is not equal to type
         MkT(Desc).t = Set.Make(Desc).t
|}]