|
1 | 1 | package kategory
|
2 | 2 |
|
3 |
| -import io.kotlintest.matchers.shouldBe |
4 | 3 | import io.kotlintest.properties.Gen
|
5 | 4 | import io.kotlintest.properties.forAll
|
6 | 5 |
|
7 | 6 | object MonadLaws {
|
8 | 7 |
|
9 |
| - inline fun <reified F> laws(M: Monad<F> = monad<F>()): List<Law> = |
10 |
| - ApplicativeLaws.laws(M) + listOf( |
11 |
| - Law("Monad Laws: left identity", { leftIdentity(M) }), |
12 |
| - Law("Monad Laws: right identity", { rightIdentity(M) }), |
13 |
| - Law("Monad Laws: kleisli left identity", { kleisliLeftIdentity(M) }), |
14 |
| - Law("Monad Laws: kleisli right identity", { kleisliRightIdentity(M) }), |
15 |
| - Law("Monad Laws: map / flatMap coherence", { mapFlatMapCoherence(M) }), |
16 |
| - Law("Monad / JVM: stack safe", { mapFlatMapCoherence(M) }) |
| 8 | + inline fun <reified F> laws(M: Monad<F> = monad<F>(), EQ: Eq<HK<F, Int>>): List<Law> = |
| 9 | + ApplicativeLaws.laws(M, EQ) + listOf( |
| 10 | + Law("Monad Laws: left identity", { leftIdentity(M, EQ) }), |
| 11 | + Law("Monad Laws: right identity", { rightIdentity(M, EQ) }), |
| 12 | + Law("Monad Laws: kleisli left identity", { kleisliLeftIdentity(M, EQ) }), |
| 13 | + Law("Monad Laws: kleisli right identity", { kleisliRightIdentity(M, EQ) }), |
| 14 | + Law("Monad Laws: map / flatMap coherence", { mapFlatMapCoherence(M, EQ) }), |
| 15 | + Law("Monad / JVM: stack safe", { stackSafety(5000, M) }) |
17 | 16 | )
|
18 | 17 |
|
19 |
| - inline fun <reified F> leftIdentity(M: Monad<F> = monad<F>()): Unit = |
| 18 | + inline fun <reified F> leftIdentity(M: Monad<F> = monad<F>(), EQ: Eq<HK<F, Int>>): Unit = |
20 | 19 | forAll(genFunctionAToB<Int, HK<F, Int>>(genApplicative(Gen.int(), M)), Gen.int(), { f: (Int) -> HK<F, Int>, a: Int ->
|
21 |
| - M.flatMap(M.pure(a), f) == f(a) |
| 20 | + M.flatMap(M.pure(a), f).equalUnderTheLaw(f(a), EQ) |
22 | 21 | })
|
23 | 22 |
|
24 |
| - inline fun <reified F> rightIdentity(M: Monad<F> = monad<F>()): Unit = |
| 23 | + inline fun <reified F> rightIdentity(M: Monad<F> = monad<F>(), EQ: Eq<HK<F, Int>>): Unit = |
25 | 24 | forAll(genApplicative(Gen.int(), M), { fa: HK<F, Int> ->
|
26 |
| - M.flatMap(fa, { M.pure(it) }) == fa |
| 25 | + M.flatMap(fa, { M.pure(it) }).equalUnderTheLaw(fa, EQ) |
27 | 26 | })
|
28 | 27 |
|
29 |
| - inline fun <reified F> kleisliLeftIdentity(M: Monad<F> = monad<F>()): Unit = |
| 28 | + inline fun <reified F> kleisliLeftIdentity(M: Monad<F> = monad<F>(), EQ: Eq<HK<F, Int>>): Unit = |
30 | 29 | forAll(genFunctionAToB<Int, HK<F, Int>>(genApplicative(Gen.int(), M)), Gen.int(), { f: (Int) -> HK<F, Int>, a: Int ->
|
31 |
| - (Kleisli({ n : Int -> M.pure(n)}, M) andThen Kleisli(f, M)).run(a) == f(a) |
| 30 | + (Kleisli({ n: Int -> M.pure(n) }, M) andThen Kleisli(f, M)).run(a).equalUnderTheLaw(f(a), EQ) |
32 | 31 | })
|
33 | 32 |
|
34 |
| - inline fun <reified F> kleisliRightIdentity(M: Monad<F> = monad<F>()): Unit = |
| 33 | + inline fun <reified F> kleisliRightIdentity(M: Monad<F> = monad<F>(), EQ: Eq<HK<F, Int>>): Unit = |
35 | 34 | forAll(genFunctionAToB<Int, HK<F, Int>>(genApplicative(Gen.int(), M)), Gen.int(), { f: (Int) -> HK<F, Int>, a: Int ->
|
36 |
| - (Kleisli(f, M) andThen Kleisli({ n : Int -> M.pure(n)}, M)).run(a) == f(a) |
| 35 | + (Kleisli(f, M) andThen Kleisli({ n: Int -> M.pure(n) }, M)).run(a).equalUnderTheLaw(f(a), EQ) |
37 | 36 | })
|
38 | 37 |
|
39 |
| - inline fun <reified F> mapFlatMapCoherence(M: Monad<F> = monad<F>()): Unit = |
| 38 | + inline fun <reified F> mapFlatMapCoherence(M: Monad<F> = monad<F>(), EQ: Eq<HK<F, Int>>): Unit = |
40 | 39 | forAll(genFunctionAToB<Int, Int>(Gen.int()), genApplicative(Gen.int(), M), { f: (Int) -> Int, fa: HK<F, Int> ->
|
41 |
| - M.flatMap(fa, { M.pure(f(it))}) == M.map(fa, f) |
| 40 | + M.flatMap(fa, { M.pure(f(it)) }).equalUnderTheLaw(M.map(fa, f), EQ) |
42 | 41 | })
|
43 | 42 |
|
44 |
| - inline fun <reified F> stackSafety(iterations : Int = 5000, M: Monad<F> = monad<F>()): Unit { |
45 |
| - val res = M.tailRecM(0, { i -> M.pure(if (i < iterations) Either.Left(i + 1) else Either.Right(i))}) |
46 |
| - res shouldBe iterations |
| 43 | + inline fun <reified F> stackSafety(iterations: Int = 5000, M: Monad<F> = monad<F>()): Unit { |
| 44 | + val res = M.tailRecM(0, { i -> M.pure(if (i < iterations) Either.Left(i + 1) else Either.Right(i)) }) |
| 45 | + res.equalUnderTheLaw(iterations) |
47 | 46 | }
|
48 |
| - |
49 | 47 | }
|
0 commit comments