/
ApplicativeLawsCheck.fs
340 lines (294 loc) · 16.8 KB
/
ApplicativeLawsCheck.fs
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
334
335
336
337
338
339
namespace Funcy.Test
open System
open Funcy
open Funcy.Computations
open Persimmon
open Persimmon.Dried
open UseTestNameByReflection
module ApplicativeLawsCheck =
let funcId<'T> = Func<'T, 'T>(id)
let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x) // for implicit conversion in F#
module ApplicativeLawsInMaybe =
let pureMaybe = Maybe.Some
let ``Identity in Some<T>`` = Prop.forAll(Arb.int)(fun i ->
let v = Maybe.Some(i)
// pure id <*> v = v
v.Apply(pureMaybe funcId) = (v :> Maybe<int>)
)
let ``Identity in None<T>`` = Prop.forAll(Arb.int)(fun i ->
let v = Maybe<int>.None()
// pure id <*> v = v
v.Apply(pureMaybe funcId) = (v :> Maybe<int>)
)
let ``Composition in Maybe<T> 1`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f g i ->
let u = Maybe.Some(f)
let v = Maybe.Some(g)
let w = Maybe.Some(i)
let pointed = pureMaybe <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in Maybe<T> 2`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun g i ->
let u = Maybe.None()
let v = Maybe.Some(g)
let w = Maybe.Some(i)
let pointed = pureMaybe <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in Maybe<T> 3`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f i ->
let u = Maybe.Some(f)
let v = Maybe.None()
let w = Maybe.Some(i)
let pointed = pureMaybe <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in Maybe<T> 4`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int))(fun f g ->
let u = Maybe.Some(f)
let v = Maybe.Some(g)
let w = Maybe.None()
let pointed = pureMaybe <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Homomorphism in Maybe<T>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f x ->
// pure f <*> pure x = pure (f x)
(pureMaybe x).Apply(pureMaybe f) = (pureMaybe(f.Invoke(x)) :> Maybe<int>)
)
let ``Interchange in Maybe<T>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f y ->
// u <*> pure y = pure ($ y) <*> u
let u = Maybe.Some(f)
(pureMaybe y).Apply(u) = u.Apply(pureMaybe <| Func<Func<int, int>, int>(fun f_ -> f_.Invoke(y)))
)
let ``Applicative laws`` = property {
apply ``Identity in Some<T>``
apply ``Identity in None<T>``
apply ``Composition in Maybe<T> 1``
apply ``Composition in Maybe<T> 2``
apply ``Composition in Maybe<T> 3``
apply ``Composition in Maybe<T> 4``
apply ``Homomorphism in Maybe<T>``
apply ``Interchange in Maybe<T>``
}
module ApplicativeLawsInEither =
let pureEither<'TLeft, 'TRight> = Either<'TLeft, 'TRight>.Right
let ``Identity in Right<TLeft, TRight>`` = Prop.forAll(Arb.int)(fun i ->
let v = Either.Right(i)
// pure id <*> v = v
v.Apply(pureEither funcId) = (v :> Either<exn, int>)
)
let ``Identity in Left<TLeft, TRight>`` = Prop.forAll(Arb.string.NonNull)(fun s ->
let v = Either.Left(exn(s))
// pure id <*> v = v
v.Apply(pureEither funcId) = (v :> Either<exn, int>)
)
let ``Composition in Either<TLeft, TRight> 1`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f g i ->
let u = Either.Right(f)
let v = Either.Right(g)
let w = Either.Right(i)
let pointed = pureEither <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in Either<TLeft, TRight> 2`` = Prop.forAll(Arb.string.NonNull, Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun s g i ->
let u = Either.Left(exn(s))
let v = Either.Right(g)
let w = Either.Right(i)
let pointed = pureEither <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in Either<TLeft, TRight> 3`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.string.NonNull, Arb.int)(fun f s i ->
let u = Either.Right(f)
let v = Either.Left(exn(s))
let w = Either.Right(i)
let pointed = pureEither <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in Either<TLeft, TRight> 4`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.string)(fun f g s ->
let u = Either.Right(f)
let v = Either.Right(g)
let w = Either.Left(exn(s))
let pointed = pureEither <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Homomorphism in Either<TLeft, TRight>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f x ->
// pure f <*> pure x = pure (f x)
(pureEither x).Apply(pureEither f) = (pureEither(f.Invoke(x)) :> Either<exn, int>)
)
let ``Interchange in Either<TLeft, TRight>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f y ->
// u <*> pure y = pure ($ y) <*> u
let u = Either.Right(f)
(pureEither y).Apply(u) = u.Apply(pureEither <| Func<Func<int, int>, int>(fun f_ -> f_.Invoke(y)))
)
let ``Applicative laws`` = property {
apply ``Identity in Right<TLeft, TRight>``
apply ``Identity in Left<TLeft, TRight>``
apply ``Composition in Either<TLeft, TRight> 1``
apply ``Composition in Either<TLeft, TRight> 2``
apply ``Composition in Either<TLeft, TRight> 3``
apply ``Composition in Either<TLeft, TRight> 4``
apply ``Homomorphism in Either<TLeft, TRight>``
apply ``Interchange in Either<TLeft, TRight>``
}
module ApplicativeLawsInFuncyList =
let pureFList x = FuncyList.Construct([|x|])
let ``Identity in Cons<T>`` = Prop.forAll(Arb.int)(fun i ->
let v = FuncyList.Cons(i, FuncyList.Nil())
// pure id <*> v = v
v.Apply(pureFList funcId) = (v :> FuncyList<int>)
)
let ``Identity in Nil<T>`` = Prop.forAll(Arb.int)(fun i ->
let v = FuncyList<int>.Nil()
// pure id <*> v = v
v.Apply(pureFList funcId) = (v :> FuncyList<int>)
)
let ``Composition in FuncyList<T> 1`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f g i ->
let u = FuncyList.Cons(f, FuncyList.Nil())
let v = FuncyList.Construct([|g|])
let w = FuncyList.Cons(i, FuncyList.Nil())
let pointed = pureFList <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in FuncyList<T> 2`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.array(Arb.int).NonNull)(fun g a ->
let u = FuncyList.Nil()
let v = FuncyList.Cons(g, FuncyList.Nil())
let w = FuncyList.Construct(a)
let pointed = pureFList <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in FuncyList<T> 3`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f i ->
let u = FuncyList.Cons(f, FuncyList.Nil())
let v = FuncyList.Nil()
let w = FuncyList.Cons(i, FuncyList.Nil())
let pointed = pureFList <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in FuncyList<T> 4`` = Prop.forAll(Arb.array(Arb.systemFunc(CoArb.int, Arb.int)).NonNull, Arb.systemFunc(CoArb.int, Arb.int))(fun fs g ->
let u = FuncyList.Construct(fs)
let v = FuncyList.Construct([|g|])
let w = FuncyList.Nil()
let pointed = pureFList <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Homomorphism in FuncyList<T>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f x ->
// pure f <*> pure x = pure (f x)
(pureFList x).Apply(pureFList f) = pureFList(f.Invoke(x))
)
let ``Interchange in FuncyList<T>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f y ->
// u <*> pure y = pure ($ y) <*> u
let u = FuncyList.Cons(f, FuncyList.Nil())
(pureFList y).Apply(u) = u.Apply(pureFList <| Func<Func<int, int>, int>(fun f_ -> f_.Invoke(y)))
)
let ``Applicative laws`` = property {
apply ``Identity in Cons<T>``
apply ``Identity in Nil<T>``
apply ``Composition in FuncyList<T> 1``
apply ``Composition in FuncyList<T> 2``
apply ``Composition in FuncyList<T> 3``
apply ``Composition in FuncyList<T> 4``
apply ``Homomorphism in FuncyList<T>``
apply ``Interchange in FuncyList<T>``
}
module ApplicativeLawsInNonEmptyList =
let pureNEL x = NonEmptyList.Construct([x])
let ``Identity in NonEmptyList<T>`` = Prop.forAll(Arb.nonEmptyList(Arb.int))(fun ls ->
let v = NonEmptyList.Construct(ls)
// pure id <*> v = v
v.Apply(pureNEL funcId) = v
)
let ``Identity in ConsNEL<T>`` = Prop.forAll(Arb.int, Arb.int)(fun i j ->
let v = NonEmptyList.ConsNEL(i, NonEmptyList.Singleton(j))
// pure id <*> v = v
v.Apply(pureNEL funcId) = (v :> NonEmptyList<int>)
)
let ``Identity in Singleton<T>`` = Prop.forAll(Arb.int)(fun i ->
let v = NonEmptyList.Singleton(i)
// pure id <*> v = v
v.Apply(pureNEL funcId) = (v :> NonEmptyList<int>)
)
let ``Composition in NonEmptyList<T> 1`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.nonEmptyList(Arb.systemFunc(CoArb.int, Arb.int)), Arb.int, Arb.int)(fun f gs i j ->
let u = NonEmptyList.Singleton(f)
let v = NonEmptyList.Construct(gs)
let w = NonEmptyList.ConsNEL(i, NonEmptyList.Singleton(j))
let pointed = pureNEL <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in NonEmptyList<T> 2`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.nonEmptyList(Arb.int))(fun f g h ls ->
let u = NonEmptyList.ConsNEL(f, NonEmptyList.Singleton(g))
let v = NonEmptyList.Singleton(h)
let w = NonEmptyList.Construct(ls)
let pointed = pureNEL <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Composition in NonEmptyList<T> 3`` = Prop.forAll(Arb.nonEmptyList(Arb.systemFunc(CoArb.int, Arb.int)), Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun fs g h i ->
let u = NonEmptyList.Construct(fs)
let v = NonEmptyList.ConsNEL(g, NonEmptyList.Singleton(h))
let w = NonEmptyList.Singleton(i)
let pointed = pureNEL <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let arbFs = {
Gen = Gen.listOfLength 3 <| Arb.systemFunc(CoArb.int, Arb.int).Gen
Shrinker = Shrink.shrinkList <| Arb.systemFunc(CoArb.int, Arb.int).Shrinker
PrettyPrinter = Pretty.prettyList
}
let arbLs = {
Gen = Gen.listOfLength 3 <| Arb.int.Gen
Shrinker = Shrink.shrinkList <| Arb.int.Shrinker
PrettyPrinter = Pretty.prettyList
}
let ``Composition in NonEmptyList<T> 4`` = Prop.forAll(arbFs, arbFs, arbLs)(fun fs gs ls ->
let u = NonEmptyList.Construct(fs)
let v = NonEmptyList.Construct(gs)
let w = NonEmptyList.Construct(ls)
let pointed = pureNEL <|
(!> Currying.Curry(Func<Func<int, int>, Func<int, int>, Func<int, int>>(fun f_ g_ -> Composition.Compose(f_, g_))))
// pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
w.Apply(v.Apply(u.Apply(pointed))) = w.Apply(v).Apply(u)
)
let ``Homomorphism in NonEmptyList<T>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f x ->
// pure f <*> pure x = pure (f x)
(pureNEL x).Apply(pureNEL f) = pureNEL(f.Invoke(x))
)
let ``Interchange in NonEmptyList<T>`` = Prop.forAll(Arb.systemFunc(CoArb.int, Arb.int), Arb.systemFunc(CoArb.int, Arb.int), Arb.int)(fun f g y ->
// u <*> pure y = pure ($ y) <*> u
let u = NonEmptyList.ConsNEL(f, NonEmptyList.Singleton(g))
(pureNEL y).Apply(u) = u.Apply(pureNEL <| Func<Func<int, int>, int>(fun f_ -> f_.Invoke(y)))
)
let ``Applicative laws`` = property {
apply ``Identity in NonEmptyList<T>``
apply ``Identity in ConsNEL<T>``
apply ``Identity in Singleton<T>``
apply ``Composition in NonEmptyList<T> 1``
apply ``Composition in NonEmptyList<T> 2``
apply ``Composition in NonEmptyList<T> 3``
apply ``Composition in NonEmptyList<T> 4``
apply ``Homomorphism in NonEmptyList<T>``
apply ``Interchange in NonEmptyList<T>``
}