-
Notifications
You must be signed in to change notification settings - Fork 3
/
covariant.scala
339 lines (289 loc) · 10.9 KB
/
covariant.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
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
package com.thoughtworks.tryt
import scala.language.higherKinds
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import scalaz.Tags.Parallel
import scalaz.effect.{IO, LiftIO, MonadCatchIO, MonadIO}
import scalaz.{\/, _}
/** The namespace that contains the covariant [[TryT]]. */
object covariant {
private[tryt] trait OpacityTypes {
type TryT[F[+ _], +A]
def apply[F[+ _], A](run: F[Try[A]]): TryT[F, A]
def unwrap[F[+ _], A](tryT: TryT[F, A]): F[Try[A]]
}
@inline
private[tryt] val opacityTypes: OpacityTypes = new OpacityTypes {
type TryT[F[+ _], +A] = F[Try[A]]
override final def apply[F[+ _], A](tryT: F[Try[A]]): TryT[F, A] = tryT
override final def unwrap[F[+ _], A](tryT: TryT[F, A]): F[Try[A]] = tryT
}
object TryT extends TryTInstances0 {
private[thoughtworks] def unwrap[F[+ _], A](tryT: TryT[F, A]): F[Try[A]] = opacityTypes.unwrap(tryT)
/** @group Converters */
def unapply[F[+ _], A](tryT: TryT[F, A]): Some[F[Try[A]]] = Some(unwrap(tryT))
/** @group Converters */
def apply[F[+ _], A](tryT: F[Try[A]]): TryT[F, A] = opacityTypes.apply(tryT)
/** @group Type classes */
implicit final def tryTMonadCatchIORec[F[+ _]](
implicit F0: MonadIO[F],
B0: BindRec[F]): MonadCatchIO[TryT[F, ?]] with BindRec[TryT[F, ?]] = {
new MonadCatchIO[TryT[F, ?]] with TryTBindRec[F] with TryTLiftIO[F] with TryTMonadError[F] {
override implicit def B: BindRec[F] = B0
override implicit def F: MonadIO[F] = F0
}
}
}
private[tryt] sealed abstract class TryTInstances3 { this: TryT.type =>
/** @group Type classes */
implicit final def tryTMonadCatchIO[F[+ _]](implicit F0: MonadIO[F]): MonadCatchIO[TryT[F, ?]] = {
new MonadCatchIO[TryT[F, ?]] with TryTLiftIO[F] with TryTMonadError[F] {
override implicit def F: MonadIO[F] = F0
}
}
/** @group Type classes */
implicit final def tryTParallelApplicative[F[+ _]](
implicit F0: Applicative[Lambda[A => F[A] @@ Parallel]],
S0: Semigroup[Throwable]): Applicative[Lambda[A => TryT[F, A] @@ Parallel]] = {
new TryTParallelApplicative[F] {
override implicit def F: Applicative[Lambda[A => F[A] @@ Parallel]] = F0
override implicit def S: Semigroup[Throwable] = S0
}
}
}
private[tryt] sealed abstract class TryTInstances2 extends TryTInstances3 { this: TryT.type =>
/** @group Type classes */
implicit final def tryTBindRec[F[+ _]](
implicit F0: Monad[F],
B0: BindRec[F]): BindRec[TryT[F, ?]] with MonadError[TryT[F, ?], Throwable] = {
new TryTBindRec[F] with TryTMonadError[F] {
override implicit def B: BindRec[F] = B0
override implicit def F: Monad[F] = F0
}
}
}
private[tryt] sealed abstract class TryTInstances1 extends TryTInstances2 { this: TryT.type =>
/** @group Type classes */
implicit final def tryTMonadError[F[+ _]](implicit F0: Monad[F]): MonadError[TryT[F, ?], Throwable] = {
new TryTMonadError[F] {
implicit override def F: Monad[F] = F0
}
}
}
private[tryt] sealed abstract class TryTInstances0 extends TryTInstances1 { this: TryT.type =>
/** @group Type classes */
implicit final def tryTLiftIO[F[+ _]](implicit F0: LiftIO[F]): LiftIO[TryT[F, ?]] =
new TryTLiftIO[F] {
implicit override def F: LiftIO[F] = F0
}
/** @group Type classes */
implicit final def tryTFunctor[F[+ _]](implicit F0: Functor[F]): Functor[TryT[F, ?]] =
new TryTFunctor[F] {
implicit override def F: Functor[F] = F0
}
}
import opacityTypes._
private[tryt] trait TryTFunctor[F[+ _]] extends Functor[TryT[F, ?]] {
implicit protected def F: Functor[F]
override def map[A, B](fa: TryT[F, A])(f: A => B): TryT[F, B] = {
opacityTypes.apply(F.map(unwrap(fa)) { tryA =>
tryA.flatMap { a =>
Try(f(a))
}
})
}
}
private[tryt] trait TryTBind[F[+ _]] extends Bind[TryT[F, ?]] with TryTFunctor[F] {
implicit protected override def F: Monad[F]
override def bind[A, B](fa: TryT[F, A])(f: A => TryT[F, B]): TryT[F, B] = opacityTypes {
F.bind[Try[A], Try[B]](unwrap(fa)) {
case Failure(e) => F.point(Failure(e))
case Success(value) =>
unwrap(
try {
f(value)
} catch {
case NonFatal(e) =>
opacityTypes.apply[F, B](F.point(Failure(e)))
}
)
}
}
}
private[tryt] trait TryTBindRec[F[+ _]] extends BindRec[TryT[F, ?]] with TryTBind[F] {
implicit protected def F: Monad[F]
implicit protected def B: BindRec[F]
override def tailrecM[A, B](f: A => TryT[F, A \/ B])(a: A): TryT[F, B] = {
val fTryB: F[Try[B]] = B.tailrecM[A, Try[B]](a =>
Try(f(a)) match {
case Success(tryT) =>
F.map(opacityTypes.unwrap(tryT)) {
case Failure(e) =>
\/-(Failure(e))
case Success(\/-(b)) =>
\/-(Success(b))
case Success(left @ -\/(_)) =>
left
}
case Failure(e) =>
F.point(\/-(Failure(e)))
})(a)
opacityTypes(fTryB)
}
}
private[tryt] trait TryTLiftIO[F[+ _]] extends LiftIO[TryT[F, ?]] {
implicit protected def F: LiftIO[F]
override def liftIO[A](ioa: IO[A]): TryT[F, A] = {
opacityTypes.apply(F.liftIO(ioa.map(Try(_))))
}
}
private[tryt] trait TryTMonadError[F[+ _]] extends MonadError[TryT[F, ?], Throwable] with TryTBind[F] {
implicit protected override def F: Monad[F]
override def point[A](a: => A): TryT[F, A] = opacityTypes.apply(F.point(Try(a)))
override def raiseError[A](e: Throwable): TryT[F, A] = opacityTypes.apply[F, A](F.point(Failure(e)))
override def handleError[A](fa: TryT[F, A])(f: (Throwable) => TryT[F, A]): TryT[F, A] = opacityTypes {
F.bind(unwrap(fa)) {
case Failure(e) =>
unwrap(
try {
f(e)
} catch {
case NonFatal(nonFatal) => opacityTypes[F, A](F.point(Failure(nonFatal)))
}
)
case Success(value) => F.point(Success(value))
}
}
def except[A](fa: TryT[F, A])(handler: Throwable => TryT[F, A]): TryT[F, A] = {
handleError(fa)(handler)
}
}
private[tryt] trait TryTParallelApplicative[F[+ _]] extends Applicative[Lambda[A => TryT[F, A] @@ Parallel]] {
implicit protected def F: Applicative[Lambda[A => F[A] @@ Parallel]]
implicit protected def S: Semigroup[Throwable]
private type T[+A] = TryT[F, A]
private type P[A] = T[A] @@ Parallel
override def point[A](a: => A): P[A] =
Parallel(opacityTypes[F, A] {
Parallel.unwrap(F.point(Try(a)))
})
override def map[A, B](fa: P[A])(f: (A) => B): P[B] = {
Parallel(opacityTypes.apply(Parallel.unwrap(F.map(Parallel[F[Try[A]]](unwrap(Parallel.unwrap(fa)))) { tryA =>
tryA.flatMap { a =>
Try(f(a))
}
})))
}
override def ap[A, B](fa: => P[A])(f: => P[(A) => B]): P[B] = {
val fTryAP: F[Try[A]] @@ Parallel = try {
Parallel(opacityTypes.unwrap(Parallel.unwrap(fa)))
} catch {
case NonFatal(e) =>
F.point(Failure(e))
}
val fTryABP: F[Try[A => B]] @@ Parallel = try {
Parallel(opacityTypes.unwrap(Parallel.unwrap(f)): F[Try[A => B]])
} catch {
case NonFatal(e) =>
F.point(Failure(e))
}
import scalaz.syntax.all._
val fTryBP: F[Try[B]] @@ Parallel =
F.apply2(fTryAP, fTryABP) { (tryA: Try[A], tryAB: Try[A => B]) =>
tryA match {
case Success(a) =>
tryAB match {
case Success(ab) =>
try {
Success(ab(a))
} catch {
case NonFatal(nonfatal) => Failure(nonfatal)
}
case Failure(failure) => Failure(failure)
}
case Failure(failure) =>
tryAB match {
case Success(_) => Failure(failure)
case Failure(anotherFailure) =>
Failure(failure |+| anotherFailure)
}
}
}
Parallel(opacityTypes.apply(Parallel.unwrap(fTryBP)))
}
}
/** A monad transformer for exception handling.
*
* @see This `TryT` transfomer is similar to [[scalaz.EitherT]],
* except `TryT` handles exceptions thrown in callback functions passed to
* [[scalaz.Monad.map map]], [[scalaz.Monad.bind bind]] or [[scalaz.Monad.point point]].
*
* @example As a monad transformer, `TryT` should be used with another monadic data type, like [[scalaz.Name]].
*
* {{{
* import scalaz.Name
* import com.thoughtworks.tryt.covariant.TryT, TryT._
*
* type TryName[+A] = TryT[Name, A]
* }}}
*
* Given a `validate` function,
*
* {{{
* def validate(s: String): Int = s.toInt
* }}}
*
* when creating a `TryT`-transformed [[scalaz.Name]] from the `validate`,
*
* {{{
* import scalaz.syntax.all._
* val invalidTry: TryName[Int] = validate("invalid input").point[TryName]
* }}}
*
* then the exceptions thrown in `validate` call should be converted to a [[scala.util.Failure]];
*
* {{{
* import com.thoughtworks.tryt.covariant.TryT._
*
* val TryT(Name(failure)) = invalidTry
*
* import scala.util._
* failure should be(an[Failure[_]])
* }}}
*
* and when there is no exception thrown in `validate` call,
*
* {{{
* val validTry: TryName[Int] = validate("42").point[TryName]
* }}}
*
* then the result of `validate` call should be converted to a [[scala.util.Success]];
*
* {{{
* val TryT(Name(success)) = validTry
* success should be(Success(42))
* }}}
*
* and when the `TryT`-transformed [[scalaz.Name]] is built from a `for`-comprehension,
*
* {{{
* val invalidForComprehension: TryName[Int] = for {
* i <- validate("42").point[TryName]
* j <- validate("invalid input").point[TryName]
* } yield i + j
* }}}
*
* then the exceptions thrown in the `for`-comprehension should be converted to a [[scala.util.Failure]];
*
* {{{
* val TryT(Name(failure2)) = invalidTry
* failure2 should be(an[Failure[_]])
* }}}
*
*
* @note This `TryT` type is an opacity alias to `F[Try[A]]`.
* All type classes and helper functions for this `TryT` type are defined in the companion object [[TryT$ TryT]]
* @template
*/
type TryT[F[+ _], +A] = opacityTypes.TryT[F, A]
}