11package cats
22
3- import cats .data .EitherT
3+ import cats .data ._
44
55trait ErrorControl [F [_], G [_], E ] extends Serializable {
6- val monadErrorF : MonadError [F , E ]
7- val monadG : Monad [G ]
6+ def monadErrorF : MonadError [F , E ]
7+ def monadG : Monad [G ]
88
99 def controlError [A ](fa : F [A ])(f : E => G [A ]): G [A ]
1010
1111 def accept [A ](ga : G [A ]): F [A ]
1212
13+ def control [A ,B ](fa : F [A ])(f : Either [E , A ] => G [B ]): G [B ] =
14+ monadG.flatMap(trial(fa))(f)
15+
1316 def trial [A ](fa : F [A ]): G [Either [E , A ]] =
1417 intercept(monadErrorF.map(fa)(Right (_): Either [E , A ]))(Left (_))
1518
@@ -35,4 +38,61 @@ object ErrorControl {
3538
3639 def apply [F [_], G [_], E ](implicit ev : ErrorControl [F , G , E ]): ErrorControl [F , G , E ] = ev
3740
41+
42+ implicit def catsErrorControlForStateT [F [_], G [_], S , E ]
43+ (implicit E : ErrorControl [F , G , E ]): ErrorControl [StateT [F , S , ? ], StateT [G , S , ? ], E ] =
44+ new ErrorControl [StateT [F , S , ? ], StateT [G , S , ? ], E ] {
45+ implicit val F : MonadError [F , E ] = E .monadErrorF
46+ implicit val G : Monad [G ] = E .monadG
47+
48+ val monadErrorF : MonadError [StateT [F , S , ? ], E ] = IndexedStateT .catsDataMonadErrorForIndexedStateT
49+ val monadG : Monad [StateT [G , S , ? ]] = IndexedStateT .catsDataMonadForIndexedStateT
50+
51+ def accept [A ](ga : StateT [G , S , A ]): StateT [F , S , A ] = ga.mapK(new (G ~> F ) {
52+ def apply [T ](ga : G [T ]): F [T ] = E .accept(ga)
53+ })
54+
55+ def controlError [A ](fa : StateT [F , S , A ])(f : E => StateT [G , S , A ]): StateT [G , S , A ] =
56+ IndexedStateT (s => E .controlError(fa.run(s))(e => f(e).run(s)))
57+
58+ }
59+
60+
61+ implicit def catsErrorControlForKleisli [F [_], G [_], R , E ]
62+ (implicit E : ErrorControl [F , G , E ]): ErrorControl [Kleisli [F , R , ? ], Kleisli [G , R , ? ], E ] =
63+ new ErrorControl [Kleisli [F , R , ? ], Kleisli [G , R , ? ], E ] {
64+ implicit val F : MonadError [F , E ] = E .monadErrorF
65+ implicit val G : Monad [G ] = E .monadG
66+
67+ val monadErrorF : MonadError [Kleisli [F , R , ? ], E ] = Kleisli .catsDataMonadErrorForKleisli
68+ val monadG : Monad [Kleisli [G , R , ? ]] = Kleisli .catsDataMonadForKleisli
69+
70+ def accept [A ](ga : Kleisli [G , R , A ]): Kleisli [F , R , A ] = ga.mapK(new (G ~> F ) {
71+ def apply [T ](ga : G [T ]): F [T ] = E .accept(ga)
72+ })
73+
74+ def controlError [A ](fa : Kleisli [F , R , A ])(f : E => Kleisli [G , R , A ]): Kleisli [G , R , A ] =
75+ Kleisli (r => E .controlError(fa.run(r))(e => f(e).run(r)))
76+
77+ }
78+
79+
80+ implicit def catsErrorControlForWriterT [F [_], G [_], L : Monoid , E ]
81+ (implicit M : ErrorControl [F , G , E ]): ErrorControl [WriterT [F , L , ? ], WriterT [G , L , ? ], E ] =
82+ new ErrorControl [WriterT [F , L , ? ], WriterT [G , L , ? ], E ] {
83+ implicit val F : MonadError [F , E ] = M .monadErrorF
84+ implicit val G : Monad [G ] = M .monadG
85+
86+ val monadErrorF : MonadError [WriterT [F , L , ? ], E ] = WriterT .catsDataMonadErrorForWriterT
87+ val monadG : Monad [WriterT [G , L , ? ]] = WriterT .catsDataMonadForWriterT
88+
89+ def accept [A ](ga : WriterT [G , L , A ]): WriterT [F , L , A ] = ga.mapK(new (G ~> F ) {
90+ def apply [T ](ga : G [T ]): F [T ] = M .accept(ga)
91+ })
92+
93+ def controlError [A ](fa : WriterT [F , L , A ])(f : E => WriterT [G , L , A ]): WriterT [G , L , A ] =
94+ WriterT (M .controlError(fa.run)(e => f(e).run))
95+
96+ }
97+
3898}
0 commit comments