File: variance.ml

package info (click to toggle)
ocaml 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,372 kB
  • sloc: ml: 370,196; ansic: 52,820; sh: 27,396; asm: 5,462; makefile: 3,679; python: 974; awk: 278; javascript: 273; perl: 59; fortran: 21; cs: 9
file content (267 lines) | stat: -rw-r--r-- 6,030 bytes parent folder | download | duplicates (3)
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
(* TEST
 flags = "-i-variance";
 expect;
*)

(* Syntax *)
type +'a t;;
type -'a t;;
type +-'a t;;
type -+'a t;;
type + 'a t;;
type - 'a t;;
type +- 'a t;;
type -+ 'a t;;
[%%expect{|
type +'a t
type -'a t
type +-'a t
type +-'a t
type +'a t
type -'a t
type +-'a t
type +-'a t
|}]
(* Expect doesn't support syntax errors
type + - 'a t
[%%expect]
*)

(* #8698 *)

(* Actually, this is not a bug *)
type +'a t = [> `Foo of 'a -> unit] as 'a;;
[%%expect{|
type !'a t = 'a constraint 'a = [> `Foo of 'a -> unit ]
|}, Principal{|
type +!'a t = 'a constraint 'a = [> `Foo of 'a -> unit ]
|}]

(* strengthening *)

type 'a t = (('a -> unit) -> unit);;
let tl = !(ref ([] : 'a t list));;
[%%expect{|
type +!'a t = ('a -> unit) -> unit
val tl : '_a t list = []
|}]

type 'a u = U of (('a -> unit) -> unit);;
let ul = !(ref ([] : 'a u list));;
[%%expect{|
type +!'a u = U of (('a -> unit) -> unit)
val ul : 'a u list = []
|}]

(* #11869 *)

module type s = sig type t end;;
type !'a t = (module s with type t = 'a);;
[%%expect{|
module type s = sig type t end
type !'a t = (module s with type t = 'a)
|}]

(* Composition *)
type -'a n
type +'a p
type !'a i

type +'a error_np = 'a n p;;
[%%expect{|
type -'a n
type +'a p
type !'a i
Line 5, characters 0-26:
5 | type +'a error_np = 'a n p;;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be covariant,
       but it is contravariant.
|}]


type +'a error_pn = 'a p n;;
[%%expect{|
Line 1, characters 0-26:
1 | type +'a error_pn = 'a p n;;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be covariant,
       but it is contravariant.
|}]

type -'a error_pp = 'a p p;;
[%%expect{|
Line 1, characters 0-26:
1 | type -'a error_pp = 'a p p;;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be contravariant,
       but it is covariant.
|}]

type -'a error_nn = 'a n n;;
[%%expect{|
Line 1, characters 0-26:
1 | type -'a error_nn = 'a n n;;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be contravariant,
       but it is covariant.
|}]

type !'a inj_in = 'a i n
[%%expect{|
Line 1, characters 0-24:
1 | type !'a inj_in = 'a i n
    ^^^^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be injective invariant,
       but it is invariant.
|}]

type !'a inj_in = 'a n i
[%%expect{|
Line 1, characters 0-24:
1 | type !'a inj_in = 'a n i
    ^^^^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be injective invariant,
       but it is invariant.
|}]

module Make_covariant(M: sig type 'a t end): sig
  type 'a i = 'a
  type +'a t = 'a i M.t
end = struct
  type 'a i = 'a
  type +'a t = 'a i M.t
end

module Positive_ref = Make_covariant(struct type 'a t = 'a ref end)
[%%expect {|
Line 6, characters 2-23:
6 |   type +'a t = 'a i M.t
      ^^^^^^^^^^^^^^^^^^^^^
Error: In this definition, expected parameter variances are not satisfied.
       The 1st type parameter was expected to be covariant,
       but it is invariant.
|}]


(* #14200 *)

module M : sig
  type 'a t = private string
  type foo = private int
  type bar = private int
  val foo : foo t
end = struct
  type 'a t = string
  type foo = private int
  type bar = private int
  let foo = "foo"
end;;
[%%expect{|
module M :
  sig
    type 'a t = private string
    type foo = private int
    type bar = private int
    val foo : foo t
  end
|}]

module Coerce : sig
  type +'a pos = private string
  type -'a neg = private string

  val p : 'a M.t -> 'a pos
  val pn : 'a pos -> 'a neg
  val n : 'a neg -> 'a M.t
end = struct
  type 'a pos = 'a M.t
  type 'a neg = 'a M.t

  let p = Fun.id
  let pn = Fun.id
  let n = Fun.id
end

let bar : M.bar M.t =
  M.foo
  |> Coerce.p
  |> (fun t -> (t :> int Coerce.pos))
  |> Coerce.pn
  |> (fun t -> (t :> M.bar Coerce.neg))
  |> Coerce.n ;;
[%%expect{|
Lines 8-15, characters 6-3:
 8 | ......struct
 9 |   type 'a pos = 'a M.t
10 |   type 'a neg = 'a M.t
11 |
12 |   let p = Fun.id
13 |   let pn = Fun.id
14 |   let n = Fun.id
15 | end
Error: Signature mismatch:
       Modules do not match:
         sig
           type 'a pos = 'a M.t
           type 'a neg = 'a M.t
           val p : 'a -> 'a
           val pn : 'a -> 'a
           val n : 'a -> 'a
         end
       is not included in
         sig
           type +'a pos = private string
           type -'a neg = private string
           val p : 'a M.t -> 'a pos
           val pn : 'a pos -> 'a neg
           val n : 'a neg -> 'a M.t
         end
       Type declarations do not match:
         type 'a pos = 'a M.t
       is not included in
         type +'a pos = private string
       Their variances do not agree.
|}]

type 'a priv = private int

module Bad : sig
  type +-'a t = private int
  val inj : 'a priv -> 'a t
  val prj : 'a t -> 'a priv
end = struct
  type 'a t = 'a priv
  let inj = Fun.id
  let prj = Fun.id
end
let cast x = x |> Bad.inj |> (fun x -> (x :> _ M.t)) |> Bad.prj;;
[%%expect{|
type 'a priv = private int
Lines 7-11, characters 6-3:
 7 | ......struct
 8 |   type 'a t = 'a priv
 9 |   let inj = Fun.id
10 |   let prj = Fun.id
11 | end
Error: Signature mismatch:
       Modules do not match:
         sig type 'a t = 'a priv val inj : 'a -> 'a val prj : 'a -> 'a end
       is not included in
         sig
           type +-'a t = private int
           val inj : 'a priv -> 'a t
           val prj : 'a t -> 'a priv
         end
       Type declarations do not match:
         type 'a t = 'a priv
       is not included in
         type +-'a t = private int
       Their variances do not agree.
|}]