-
Notifications
You must be signed in to change notification settings - Fork 510
/
SyncLaws.scala
120 lines (98 loc) · 3.24 KB
/
SyncLaws.scala
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
/*
* Copyright 2017 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cats
package effect
package laws
import cats.implicits._
import cats.laws._
trait SyncLaws[F[_]] extends MonadErrorLaws[F, Throwable] {
implicit def F: Sync[F]
def delayConstantIsPure[A](a: A) =
F.delay(a) <-> F.pure(a)
def suspendConstantIsPureJoin[A](fa: F[A]) =
F.suspend(fa) <-> F.flatten(F.pure(fa))
def delayThrowIsRaiseError[A](e: Throwable) =
F.delay[A](throw e) <-> F.raiseError(e)
def suspendThrowIsRaiseError[A](e: Throwable) =
F.suspend[A](throw e) <-> F.raiseError(e)
def unsequencedDelayIsNoop[A](a: A, f: A => A) = {
var cur = a
val change = F delay { cur = f(cur) }
val _ = change
F.delay(cur) <-> F.pure(a)
}
def repeatedSyncEvaluationNotMemoized[A](a: A, f: A => A) = {
var cur = a
val change = F delay { cur = f(cur) }
val read = F.delay(cur)
change *> change *> read <-> F.pure(f(f(a)))
}
def propagateErrorsThroughBindSuspend[A](t: Throwable) = {
val fa = F.delay[A](throw t).flatMap(x => F.pure(x))
fa <-> F.raiseError(t)
}
def bindSuspendsEvaluation[A](fa: F[A], a1: A, f: (A, A) => A) = {
var state = a1
val evolve = F.flatMap(fa) { a2 =>
state = f(a1, a2)
F.pure(state)
}
// Observing `state` before and after `evolve`
F.map2(F.pure(state), evolve)(f) <-> F.map(fa)(a2 => f(a1, f(a1, a2)))
}
def mapSuspendsEvaluation[A](fa: F[A], a1: A, f: (A, A) => A) = {
var state = a1
val evolve = F.map(fa) { a2 =>
state = f(a1, a2)
state
}
// Observing `state` before and after `evolve`
F.map2(F.pure(state), evolve)(f) <-> F.map(fa)(a2 => f(a1, f(a1, a2)))
}
lazy val stackSafetyOnRepeatedLeftBinds = {
val result = (0 until 10000).foldLeft(F.delay(())) { (acc, _) =>
acc.flatMap(_ => F.delay(()))
}
result <-> F.pure(())
}
lazy val stackSafetyOnRepeatedRightBinds = {
val result = (0 until 10000).foldRight(F.delay(())) { (_, acc) =>
F.delay(()).flatMap(_ => acc)
}
result <-> F.pure(())
}
lazy val stackSafetyOnRepeatedAttempts = {
// Note this isn't enough to guarantee stack safety, unless
// coupled with `bindSuspendsEvaluation`
val result = (0 until 10000).foldLeft(F.delay(())) { (acc, _) =>
F.attempt(acc).map(_ => ())
}
result <-> F.pure(())
}
lazy val stackSafetyOnRepeatedMaps = {
// Note this isn't enough to guarantee stack safety, unless
// coupled with `mapSuspendsEvaluation`
val result = (0 until 10000).foldLeft(F.delay(0)) { (acc, _) =>
F.map(acc)(_ + 1)
}
result <-> F.pure(10000)
}
}
object SyncLaws {
def apply[F[_]](implicit F0: Sync[F]): SyncLaws[F] = new SyncLaws[F] {
val F = F0
}
}