summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-gadts/pr5985.ml
blob: d38e7e789d4dcc9d8470f7e1cbc8dffe961bc2cb (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
(* TEST
 expect;
*)

(* Report from Jeremy Yallop *)
module F (S : sig type 'a s end) = struct
  include S
  type _ t = T : 'a -> 'a s t
end;; (* fail *)
[%%expect{|
Line 3, characters 2-29:
3 |   type _ t = T : 'a -> 'a s t
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the GADT constructor
         T : 'a -> 'a s t
       the type variable 'a cannot be deduced from the type parameters.
|}];;
(*
module M = F (struct type 'a s = int end) ;;
let M.T x = M.T 3 in x = true;;
*)

(* Fix it using #-annotations *)
(*
module F (S : sig type #'a s end) = struct
  include S
  type _ t = T : 'a -> 'a s t
end;; (* syntax error *)
module M = F (struct type 'a s = int end) ;; (* fail *)
module M = F (struct type 'a s = new int end) ;; (* ok *)
let M.T x = M.T 3 in x = true;; (* fail *)
let M.T x = M.T 3 in x = 3;; (* ok *)
*)

(* Another version using OCaml 2.00 objects *)
module F(T:sig type 'a t end) = struct
  class ['a] c x =
    object constraint 'a = 'b T.t val x' : 'b = x method x = x' end
end;; (* fail *)
[%%expect{|
Lines 2-3, characters 2-67:
2 | ..class ['a] c x =
3 |     object constraint 'a = 'b T.t val x' : 'b = x method x = x' end
Error: In the definition
         type 'a c = < x : 'b > constraint 'a = 'b T.t
       the type variable 'b cannot be deduced from the type parameters.
|}];;

(* Another (more direct) instance using polymorphic variants *)
(* PR#6275 *)
type 'x t = A of 'a constraint 'x = [< `X of 'a ] ;; (* fail *)
let magic (x : int) : bool  =
  let A x = A x in
  x;; (* fail *)
[%%expect{|
Line 1, characters 0-49:
1 | type 'x t = A of 'a constraint 'x = [< `X of 'a ] ;; (* fail *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the definition
         type 'b t = A of 'a constraint 'b = [< `X of 'a ]
       the type variable 'a cannot be deduced from the type parameters.
|}];;

type 'a t = A : 'a -> [< `X of 'a ] t;; (* fail *)
[%%expect{|
Line 1, characters 0-37:
1 | type 'a t = A : 'a -> [< `X of 'a ] t;; (* fail *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the GADT constructor
         A : 'a -> [< `X of 'a ] t
       the type variable 'a cannot be deduced from the type parameters.
|}];;

(* It is not OK to allow modules exported by other compilation units *)
type (_,_) eq = Eq : ('a,'a) eq;;
let eq = Obj.magic Eq;;
let eq : (('a, 'b) Ephemeron.K1.t, ('c, 'd) Ephemeron.K1.t) eq = eq;;
type _ t = T : 'a -> ('a, 'b) Ephemeron.K1.t t;; (* fail *)
[%%expect{|
type (_, _) eq = Eq : ('a, 'a) eq
val eq : 'a = <poly>
val eq : (('a, 'b) Ephemeron.K1.t, ('c, 'd) Ephemeron.K1.t) eq = Eq
Line 4, characters 0-46:
4 | type _ t = T : 'a -> ('a, 'b) Ephemeron.K1.t t;; (* fail *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the GADT constructor
         T : 'a -> ('a, 'b) Ephemeron.K1.t t
       the type variable 'a cannot be deduced from the type parameters.
|}];;
(*
let castT (type a) (type b) (x : a t) (e: (a, b) eq) : b t =
  let Eq = e in (x : b t);;
let T (x : bool) = castT (T 3) eq;; (* we found a contradiction *)
*)

(* The following signature should not be accepted *)
module type S = sig
  type 'a s
  type _ t = T : 'a -> 'a s t
end;; (* fail *)
[%%expect{|
Line 3, characters 2-29:
3 |   type _ t = T : 'a -> 'a s t
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the GADT constructor
         T : 'a -> 'a s t
       the type variable 'a cannot be deduced from the type parameters.
|}];;
(* Otherwise we can write the following *)
module rec M : (S with type 'a s = unit) = M;;
[%%expect{|
Line 1, characters 16-17:
1 | module rec M : (S with type 'a s = unit) = M;;
                    ^
Error: Unbound module type S
|}];;
(* For the above reason, we cannot allow the abstract declaration
   of s and the definition of t to be in the same module, as
   we could create the signature using [module type of ...] *)


(* Another problem with variance *)
(*
module M = struct type 'a t = 'a -> unit end;;
module F(X:sig type #'a t end) =
  struct type +'a s = S of 'b constraint 'a = 'b X.t end;; (* fail *)
module N = F(M);;
let o = N.S (object end);;
let N.S o' = (o :> <m : int> M.t N.s);; (* unsound! *)
*)

(* And yet another *)
type 'a q = Q;;
type +'a t = 'b constraint 'a = 'b q;;
[%%expect{|
type 'a q = Q
Line 2, characters 0-36:
2 | type +'a t = 'b constraint 'a = 'b q;;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the definition
         type +'a t = 'b constraint 'a = 'b q
       the type variable 'b has a variance that
       cannot be deduced from the type parameters.
       It was expected to be unrestricted, but it is covariant.
|}];;
(* should fail: we do not know for sure the variance of Queue.t *)

type +'a t = T of 'a;;
type +'a s = 'b constraint 'a = 'b t;; (* ok *)
[%%expect{|
type 'a t = T of 'a
type +'a s = 'b constraint 'a = 'b t
|}];;
type -'a s = 'b constraint 'a = 'b t;; (* fail *)
[%%expect{|
Line 1, characters 0-36:
1 | type -'a s = 'b constraint 'a = 'b t;; (* fail *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the definition
         type -'a s = 'b constraint 'a = 'b t
       the type variable 'b has a variance that
       is not reflected by its occurrence in type parameters.
       It was expected to be contravariant, but it is covariant.
|}];;
type +'a u = 'a t;;
type 'a t = T of ('a -> 'a);;
type -'a s = 'b constraint 'a = 'b t;; (* ok *)
[%%expect{|
type 'a u = 'a t
type 'a t = T of ('a -> 'a)
type -'a s = 'b constraint 'a = 'b t
|}];;
type +'a s = 'b constraint 'a = 'b q t;; (* ok *)
[%%expect{|
type +'a s = 'b constraint 'a = 'b q t
|}];;
type +'a s = 'b constraint 'a = 'b t q;; (* fail *)
[%%expect{|
Line 1, characters 0-38:
1 | type +'a s = 'b constraint 'a = 'b t q;; (* fail *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the definition
         type +'a s = 'b constraint 'a = 'b t q
       the type variable 'b has a variance that
       cannot be deduced from the type parameters.
       It was expected to be unrestricted, but it is covariant.
|}];;


(* the problem from lablgtk2 *)
(*
module Gobject = struct
  type -'a obj
end
open Gobject;;

class virtual ['a] item_container =
 object
   constraint 'a = < as_item : [>`widget] obj; .. >
   method virtual add : 'a -> unit
 end;;
*)

(* Another variance anomaly, should not expand t in g before checking *)
type +'a t = unit constraint 'a = 'b list;;
type _ g = G : 'a -> 'a t g;; (* fail *)
[%%expect{|
type +'a t = unit constraint 'a = 'b list
Line 2, characters 0-27:
2 | type _ g = G : 'a -> 'a t g;; (* fail *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In the GADT constructor
         G : 'a list -> 'a list t g
       the type variable 'a cannot be deduced from the type parameters.
|}];;