-
Notifications
You must be signed in to change notification settings - Fork 515
/
SyncLaws.scala
148 lines (122 loc) · 4.34 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
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
/*
* Copyright (c) 2017-2019 The Typelevel Cats-effect Project Developers
*
* 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 BracketLaws[F, Throwable] with DeferLaws[F] {
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) = F.suspend {
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) = F.suspend {
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) = F.suspend {
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) = F.suspend {
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)))
def stackSafetyOnRepeatedLeftBinds(iterations: Int) = {
val result = (0 until iterations).foldLeft(F.delay(())) { (acc, _) =>
acc.flatMap(_ => F.delay(()))
}
result <-> F.pure(())
}
def stackSafetyOnRepeatedRightBinds(iterations: Int) = {
val result = (0 until iterations).foldRight(F.delay(())) { (_, acc) =>
F.delay(()).flatMap(_ => acc)
}
result <-> F.pure(())
}
def stackSafetyOnRepeatedAttempts(iterations: Int) = {
// Note this isn't enough to guarantee stack safety, unless
// coupled with `bindSuspendsEvaluation`
val result = (0 until iterations).foldLeft(F.delay(())) { (acc, _) =>
F.attempt(acc).map(_ => ())
}
result <-> F.pure(())
}
def stackSafetyOnRepeatedMaps(iterations: Int) = {
// Note this isn't enough to guarantee stack safety, unless
// coupled with `mapSuspendsEvaluation`
val result = (0 until iterations).foldLeft(F.delay(0)) { (acc, _) =>
F.map(acc)(_ + 1)
}
result <-> F.pure(iterations)
}
def stackSafetyOfBracketOnRepeatedLeftBinds(iterations: Int) = {
val result = (0 until iterations).foldRight(F.delay(())) { (_, acc) =>
acc.flatMap(_ => F.bracket(F.unit)(F.pure(_))(_ => F.unit))
}
result <-> F.pure(())
}
def stackSafetyOfBracketOnRepeatedRightBinds(iterations: Int) = {
val result = (0 until iterations).foldRight(F.delay(())) { (_, acc) =>
F.bracket(F.unit)(F.pure(_))(_ => F.unit).flatMap(_ => acc)
}
result <-> F.pure(())
}
def stackSafetyOfGuaranteeOnRepeatedLeftBinds(iterations: Int) = {
val result = (0 until iterations).foldRight(F.delay(())) { (_, acc) =>
acc.flatMap(_ => F.guarantee(F.unit)(F.unit))
}
result <-> F.pure(())
}
def stackSafetyOfGuaranteeOnRepeatedRightBinds(iterations: Int) = {
val result = (0 until iterations).foldRight(F.delay(())) { (_, acc) =>
F.guarantee(F.unit)(F.unit).flatMap(_ => acc)
}
result <-> F.pure(())
}
}
object SyncLaws {
def apply[F[_]](implicit F0: Sync[F]): SyncLaws[F] = new SyncLaws[F] {
val F = F0
}
}