typelevel / cats-mtl
Showing 32 of 102 files from the diff.
Other files ignored by Codecov
.gitignore has changed.
README.md has changed.
.scalafmt.conf has changed.
build.sbt has changed.

@@ -2,6 +2,7 @@
Loading
2 2
package mtl
3 3
4 4
import scala.util.control.NonFatal
5 +
import cats.data._
5 6
6 7
/**
7 8
  * `FunctorRaise[F, E]` expresses the ability to raise errors of type `E` in a functorial `F[_]` context.
@@ -42,14 +43,13 @@
Loading
42 43
  * }}}
43 44
  */
44 45
trait FunctorRaise[F[_], E] extends Serializable {
45 -
  val functor: Functor[F]
46 +
  def functor: Functor[F]
46 47
47 48
  def raise[A](e: E): F[A]
48 49
49 50
  def catchNonFatal[A](a: => A)(f: Throwable => E)(implicit A: Applicative[F]): F[A] = {
50 -
    try {
51 -
      A.pure(a)
52 -
    } catch {
51 +
    try A.pure(a)
52 +
    catch {
53 53
      case NonFatal(ex) => raise(f(ex))
54 54
    }
55 55
  }
@@ -58,15 +58,65 @@
Loading
58 58
    A.flatMap(fa)(a => if (predicate(a)) A.pure(a) else raise(error))
59 59
}
60 60
61 -
object FunctorRaise {
62 -
  def apply[F[_], E](implicit functorRaise: FunctorRaise[F, E]): FunctorRaise[F, E] = functorRaise
61 +
private[mtl] trait FunctorRaiseMonadPartialOrder[F[_], G[_], E] extends FunctorRaise[G, E] {
62 +
  val lift: MonadPartialOrder[F, G]
63 +
  val F: FunctorRaise[F, E]
64 +
65 +
  override def functor = lift.monadG
66 +
  override def raise[A](e: E) = lift(F.raise(e))
67 +
}
68 +
69 +
private[mtl] trait LowPriorityFunctorRaiseInstances {
70 +
  implicit def functorRaiseForMonadPartialOrder[F[_], G[_]: Functor, E](
71 +
      implicit F: FunctorRaise[F, E],
72 +
      lift: MonadPartialOrder[F, G]
73 +
  ): FunctorRaise[G, E] =
74 +
    new FunctorRaise[G, E] {
75 +
      val functor = Functor[G]
76 +
      def raise[A](e: E) = lift(F.raise(e))
77 +
    }
78 +
}
79 +
80 +
private[mtl] trait FunctorRaiseInstances extends LowPriorityFunctorRaiseInstances {
81 +
  implicit final def raiseEitherT[M[_], E](
82 +
      implicit M: Monad[M]): FunctorRaise[EitherTC[M, E]#l, E] =
83 +
    ApplicativeHandle.handleEitherT
84 +
85 +
  implicit final def raiseEither[E]: FunctorRaise[EitherC[E]#l, E] =
86 +
    ApplicativeHandle.handleEither
87 +
88 +
  implicit final def raiseOptionT[M[_]](
89 +
      implicit M: Monad[M]): FunctorRaise[OptionTC[M]#l, Unit] =
90 +
    ApplicativeHandle.handleOptionT
91 +
92 +
  implicit final def raiseOption[E]: FunctorRaise[Option, Unit] =
93 +
    ApplicativeHandle.handleOption
94 +
95 +
  implicit final def raiseValidated[E](
96 +
      implicit E: Semigroup[E]): FunctorRaise[Validated[E, ?], E] =
97 +
    ApplicativeHandle.handleValidated
98 +
99 +
  implicit final def raiseIor[E](implicit E: Semigroup[E]): FunctorRaise[Ior[E, *], E] =
100 +
    ApplicativeHandle.handleIor[E]
101 +
102 +
  implicit final def raiseIorT[F[_], E](
103 +
      implicit E: Semigroup[E],
104 +
      F: Monad[F]): FunctorRaise[IorT[F, E, *], E] =
105 +
    ApplicativeHandle.handleIorT[F, E]
106 +
107 +
}
108 +
109 +
object FunctorRaise extends FunctorRaiseInstances {
110 +
  def apply[F[_], E](implicit functorRaise: FunctorRaise[F, E]): FunctorRaise[F, E] =
111 +
    functorRaise
63 112
64 113
  def raise[F[_], E, A](e: E)(implicit raise: FunctorRaise[F, _ >: E]): F[A] =
65 114
    raise.raise(e)
66 115
67 116
  def raiseF[F[_]]: raiseFPartiallyApplied[F] = new raiseFPartiallyApplied[F]()
68 117
69 -
  final private[mtl] class raiseFPartiallyApplied[F[_]](val dummy: Boolean = false) extends AnyVal {
118 +
  final private[mtl] class raiseFPartiallyApplied[F[_]](val dummy: Boolean = false)
119 +
      extends AnyVal {
70 120
    @inline def apply[E, A](e: E)(implicit raise: FunctorRaise[F, _ >: E]): F[A] =
71 121
      raise.raise(e)
72 122
  }

@@ -13,14 +13,13 @@
Loading
13 13
14 14
  def laws: ApplicativeAskLaws[F, E] = ApplicativeAskLaws[F, E]
15 15
16 -
  def applicativeAsk[A: Arbitrary](implicit
17 -
                                   ArbFA: Arbitrary[F[A]],
18 -
                                   ArbE: Arbitrary[E],
19 -
                                   CogenA: Cogen[A],
20 -
                                   CogenE: Cogen[E],
21 -
                                   EqFU: Eq[F[E]],
22 -
                                   EqFA: Eq[F[A]]
23 -
                                  ): RuleSet = {
16 +
  def applicativeAsk[A: Arbitrary](
17 +
      implicit ArbFA: Arbitrary[F[A]],
18 +
      ArbE: Arbitrary[E],
19 +
      CogenA: Cogen[A],
20 +
      CogenE: Cogen[E],
21 +
      EqFU: Eq[F[E]],
22 +
      EqFA: Eq[F[A]]): RuleSet = {
24 23
    new DefaultRuleSet(
25 24
      name = "applicativeAsk",
26 25
      parent = None,
@@ -38,4 +37,3 @@
Loading
38 37
    }
39 38
  }
40 39
}
41 -

@@ -1,6 +1,9 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 +
import cats.data.{Kleisli, Reader, ReaderWriterStateT => RWST}
5 +
import cats.data.IndexedReaderWriterStateT
6 +
4 7
/**
5 8
  * `ApplicativeAsk[F, E]` lets you access an `E` value in the `F[_]` context.
6 9
  *
@@ -22,48 +25,77 @@
Loading
22 25
  *
23 26
  */
24 27
trait ApplicativeAsk[F[_], E] extends Serializable {
25 -
  val applicative: Applicative[F]
28 +
  def applicative: Applicative[F]
26 29
27 30
  def ask: F[E]
28 31
29 -
  def reader[A](f: E => A): F[A]
32 +
  def reader[A](f: E => A): F[A] = applicative.map(ask)(f)
30 33
}
31 34
32 -
object ApplicativeAsk {
35 +
private[mtl] trait ApplicativeAskForMonadPartialOrder[F[_], G[_], E]
36 +
    extends ApplicativeAsk[G, E] {
37 +
  val lift: MonadPartialOrder[F, G]
38 +
  val F: ApplicativeAsk[F, E]
39 +
40 +
  override def applicative = lift.monadG
41 +
  override def ask = lift(F.ask)
42 +
}
33 43
34 -
  def apply[F[_], E](implicit applicativeAsk: ApplicativeAsk[F, E]): ApplicativeAsk[F, E] = applicativeAsk
44 +
private[mtl] trait LowPriorityApplicativeAskInstances
45 +
    extends LowPriorityApplicativeAskInstancesCompat {
35 46
36 -
  def const[F[_]: Applicative, E](e: E): ApplicativeAsk[F, E] = new DefaultApplicativeAsk[F, E] {
37 -
    val applicative: Applicative[F] = Applicative[F]
38 -
    val ask: F[E] = applicative.pure(e)
39 -
  }
47 +
  implicit def applicativeAskForMonadPartialOrder[F[_], G[_], E](
48 +
      implicit lift0: MonadPartialOrder[F, G],
49 +
      F0: ApplicativeAsk[F, E]): ApplicativeAsk[G, E] =
50 +
    new ApplicativeAskForMonadPartialOrder[F, G, E] {
51 +
      val lift: MonadPartialOrder[F, G] = lift0
52 +
      val F: ApplicativeAsk[F, E] = F0
53 +
    }
54 +
}
55 +
56 +
private[mtl] trait ApplicativeAskInstances extends LowPriorityApplicativeAskInstances {
57 +
58 +
  implicit def applicativeAskForKleisli[F[_], E](
59 +
      implicit F: Applicative[F]): ApplicativeAsk[Kleisli[F, E, *], E] =
60 +
    ApplicativeLocal.baseApplicativeLocalForKleisli[F, E]
61 +
62 +
  implicit def applicativeAskForRWST[F[_], E, L, S](
63 +
      implicit F: Monad[F],
64 +
      L: Monoid[L]): ApplicativeAsk[RWST[F, E, L, S, *], E] =
65 +
    ApplicativeLocal.baseApplicativeLocalForRWST[F, E, L, S]
66 +
}
40 67
41 -
  def ask[F[_], E](implicit ask: ApplicativeAsk[F, E]): F[E] = {
68 +
object ApplicativeAsk extends ApplicativeAskInstances {
69 +
70 +
  def apply[F[_], E](implicit applicativeAsk: ApplicativeAsk[F, E]): ApplicativeAsk[F, E] =
71 +
    applicativeAsk
72 +
73 +
  def const[F[_]: Applicative, E](e: E): ApplicativeAsk[F, E] =
74 +
    new ApplicativeAsk[F, E] {
75 +
      val applicative: Applicative[F] = Applicative[F]
76 +
      val ask: F[E] = applicative.pure(e)
77 +
    }
78 +
79 +
  def ask[F[_], E](implicit ask: ApplicativeAsk[F, E]): F[E] =
42 80
    ask.ask
43 -
  }
44 81
45 82
  def askF[F[_]]: askFPartiallyApplied[F] = new askFPartiallyApplied[F]
46 83
47 -
  @inline final private[mtl] class askFPartiallyApplied[F[_]](val dummy: Boolean = false) extends AnyVal {
48 -
    @inline def apply[E]()(implicit ask: `ApplicativeAsk`[F, E]): F[E] = {
84 +
  @inline final private[mtl] class askFPartiallyApplied[F[_]](val dummy: Boolean = false)
85 +
      extends AnyVal {
86 +
    @inline def apply[E]()(implicit ask: `ApplicativeAsk`[F, E]): F[E] =
49 87
      ask.ask
50 -
    }
51 88
  }
52 89
53 -
  @inline final private[mtl] class readerFEPartiallyApplied[F[_], E](val dummy: Boolean = false) extends AnyVal {
54 -
    @inline def apply[A](f: E => A)(implicit ask: ApplicativeAsk[F, E]): F[A] = {
90 +
  @inline final private[mtl] class readerFEPartiallyApplied[F[_], E](val dummy: Boolean = false)
91 +
      extends AnyVal {
92 +
    @inline def apply[A](f: E => A)(implicit ask: ApplicativeAsk[F, E]): F[A] =
55 93
      ask.reader(f)
56 -
    }
57 94
  }
58 95
59 96
  def readerFE[F[_], E]: readerFEPartiallyApplied[F, E] = new readerFEPartiallyApplied[F, E]
60 97
61 -
  def reader[F[_], E, A](fun: E => A)(implicit ask: ApplicativeAsk[F, E]): F[A] = {
98 +
  def reader[F[_], E, A](fun: E => A)(implicit ask: ApplicativeAsk[F, E]): F[A] =
62 99
    ask.reader(fun)
63 -
  }
64 100
65 101
}
66 -
67 -
trait DefaultApplicativeAsk[F[_], E] extends ApplicativeAsk[F, E] {
68 -
  def reader[A](f: E => A): F[A] = applicative.map(ask)(f)
69 -
}

@@ -1,6 +1,19 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 +
import cats.data.{
5 +
  IndexedStateT,
6 +
  IndexedReaderWriterStateT,
7 +
  WriterT,
8 +
  Kleisli,
9 +
  StateT,
10 +
  OptionT,
11 +
  EitherT,
12 +
  IorT,
13 +
  ReaderWriterStateT => RWST
14 +
}
15 +
import cats.implicits._
16 +
4 17
trait ApplicativeCensor[F[_], L] extends FunctorListen[F, L] {
5 18
  val applicative: Applicative[F]
6 19
  val monoid: Monoid[L]
@@ -8,14 +21,134 @@
Loading
8 21
9 22
  def censor[A](fa: F[A])(f: L => L): F[A]
10 23
11 -
  def clear[A](fa: F[A]): F[A]
24 +
  def clear[A](fa: F[A]): F[A] = censor(fa)(_ => monoid.empty)
12 25
}
13 26
14 -
object ApplicativeCensor {
27 +
object ApplicativeCensor extends ApplicativeCensorInstances {
15 28
  def apply[F[_], L](implicit ev: ApplicativeCensor[F, L]): ApplicativeCensor[F, L] = ev
16 29
}
17 30
18 -
trait DefaultApplicativeCensor[F[_], L] extends ApplicativeCensor[F, L] with DefaultFunctorListen[F, L] {
19 -
  override def clear[A](fa: F[A]): F[A] =
20 -
    censor(fa)(_ => monoid.empty)
31 +
private[mtl] trait LowPriorityApplicativeCensorInstances {
32 +
  implicit final def inductiveApplicativeCensorWriterT[
33 +
      M[_]: Applicative,
34 +
      L0: Monoid,
35 +
      L: Monoid](implicit A: ApplicativeCensor[M, L]): ApplicativeCensor[WriterT[M, L0, *], L] =
36 +
    new FunctorListenInductiveWriterT[M, L0, L] with ApplicativeCensor[WriterT[M, L0, *], L] {
37 +
      val applicative: Applicative[WriterT[M, L0, *]] = WriterT.catsDataApplicativeForWriterT
38 +
      val monoid: Monoid[L] = Monoid[L]
39 +
40 +
      def censor[A](fa: WriterT[M, L0, A])(f: L => L): WriterT[M, L0, A] =
41 +
        WriterT(A.censor(fa.run)(f))
42 +
    }
43 +
44 +
  implicit final def inductiveApplicativeCensorRWST[M[_]: Monad, R, L0: Monoid, L: Monoid, S](
45 +
      implicit A: ApplicativeCensor[M, L]): ApplicativeCensor[RWST[M, R, L0, S, *], L] =
46 +
    new FunctorListenInductiveRWST[M, R, L0, L, S]
47 +
      with ApplicativeCensor[RWST[M, R, L0, S, *], L] {
48 +
      val applicative: Applicative[RWST[M, R, L0, S, *]] =
49 +
        IndexedReaderWriterStateT.catsDataMonadForRWST
50 +
      val monoid: Monoid[L] = Monoid[L]
51 +
52 +
      def censor[A](fa: RWST[M, R, L0, S, A])(f: L => L): RWST[M, R, L0, S, A] =
53 +
        RWST { case (r, s) => A.censor(fa.run(r, s))(f) }
54 +
    }
55 +
}
56 +
57 +
private[mtl] trait ApplicativeCensorInstances extends LowPriorityApplicativeCensorInstances {
58 +
  implicit final def applicativeCensorWriterT[M[_], L](
59 +
      implicit M: Applicative[M],
60 +
      L: Monoid[L]): ApplicativeCensor[WriterT[M, L, *], L] =
61 +
    new FunctorListenWriterT[M, L] with ApplicativeCensor[WriterT[M, L, *], L] {
62 +
      val applicative: Applicative[WriterT[M, L, *]] =
63 +
        cats.data.WriterT.catsDataApplicativeForWriterT[M, L]
64 +
65 +
      val monoid: Monoid[L] = L
66 +
67 +
      override def clear[A](fa: WriterT[M, L, A]): WriterT[M, L, A] =
68 +
        WriterT(fa.value.tupleLeft(L.empty))
69 +
70 +
      def censor[A](fa: WriterT[M, L, A])(f: L => L): WriterT[M, L, A] =
71 +
        WriterT(fa.run.map { case (l, a) => (f(l), a) })
72 +
73 +
    }
74 +
75 +
  implicit final def applicativeCensorRWST[M[_], R, L, S](
76 +
      implicit L: Monoid[L],
77 +
      M: Monad[M]): ApplicativeCensor[RWST[M, R, L, S, *], L] =
78 +
    new FunctorListenRWST[M, R, L, S] with ApplicativeCensor[RWST[M, R, L, S, *], L] {
79 +
      val applicative: Applicative[RWST[M, R, L, S, *]] =
80 +
        IndexedReaderWriterStateT.catsDataMonadForRWST
81 +
82 +
      val monoid: Monoid[L] = L
83 +
84 +
      def censor[A](faf: RWST[M, R, L, S, A])(f: L => L): RWST[M, R, L, S, A] =
85 +
        RWST((e, s) =>
86 +
          faf.run(e, s).map {
87 +
            case (l, s, a) => (f(l), s, a)
88 +
          })
89 +
90 +
    }
91 +
92 +
  implicit final def applicativeCensorKleisli[F[_], R, L](
93 +
      implicit L: Monoid[L],
94 +
      A: ApplicativeCensor[F, L],
95 +
      F: Applicative[F]): ApplicativeCensor[Kleisli[F, R, *], L] =
96 +
    new FunctorListenKleisli[F, R, L] with ApplicativeCensor[Kleisli[F, R, *], L] {
97 +
      val applicative: Applicative[Kleisli[F, R, *]] = Kleisli.catsDataApplicativeForKleisli
98 +
      val monoid: cats.Monoid[L] = L
99 +
100 +
      def censor[A](fa: Kleisli[F, R, A])(f: L => L): Kleisli[F, R, A] =
101 +
        Kleisli(r => A.censor(fa.run(r))(f))
102 +
    }
103 +
104 +
  implicit final def applicativeCensorStateT[F[_], S, L](
105 +
      implicit L: Monoid[L],
106 +
      A: ApplicativeCensor[F, L],
107 +
      F: Monad[F]): ApplicativeCensor[StateT[F, S, *], L] =
108 +
    new FunctorListenStateT[F, S, L] with ApplicativeCensor[StateT[F, S, *], L] {
109 +
      val applicative: Applicative[StateT[F, S, *]] =
110 +
        IndexedStateT.catsDataMonadForIndexedStateT
111 +
      val monoid: cats.Monoid[L] = L
112 +
113 +
      def censor[A](fa: StateT[F, S, A])(f: L => L): StateT[F, S, A] =
114 +
        StateT(s => A.censor(fa.run(s))(f))
115 +
    }
116 +
117 +
  implicit final def applicativeCensorEitherT[F[_], E, L](
118 +
      implicit L: Monoid[L],
119 +
      A: ApplicativeCensor[F, L],
120 +
      F: Monad[F]): ApplicativeCensor[EitherT[F, E, *], L] =
121 +
    new FunctorListenEitherT[F, E, L] with ApplicativeCensor[EitherT[F, E, *], L] {
122 +
      val applicative: Applicative[EitherT[F, E, *]] = EitherT.catsDataMonadErrorForEitherT
123 +
      val monoid: cats.Monoid[L] = L
124 +
125 +
      def censor[A](fa: EitherT[F, E, A])(f: L => L): EitherT[F, E, A] =
126 +
        EitherT(A.censor(fa.value)(f))
127 +
    }
128 +
129 +
  implicit final def applicativeCensorIorT[F[_], E, L](
130 +
      implicit L: Monoid[L],
131 +
      A: ApplicativeCensor[F, L],
132 +
      F: Monad[F],
133 +
      E: Semigroup[E]): ApplicativeCensor[IorT[F, E, *], L] =
134 +
    new FunctorListenIorT[F, E, L] with ApplicativeCensor[IorT[F, E, *], L] {
135 +
      val applicative: Applicative[IorT[F, E, *]] = IorT.catsDataMonadErrorForIorT
136 +
      val monoid: cats.Monoid[L] = L
137 +
138 +
      def censor[A](fa: IorT[F, E, A])(f: L => L): IorT[F, E, A] =
139 +
        IorT(A.censor(fa.value)(f))
140 +
    }
141 +
142 +
  implicit final def applicativeCensorOptionT[F[_], L](
143 +
      implicit L: Monoid[L],
144 +
      A: ApplicativeCensor[F, L],
145 +
      F: Monad[F]): ApplicativeCensor[OptionT[F, *], L] =
146 +
    new FunctorListenOptionT[F, L] with ApplicativeCensor[OptionT[F, *], L] {
147 +
      val applicative: Applicative[OptionT[F, *]] = OptionT.catsDataMonadForOptionT
148 +
      val monoid: cats.Monoid[L] = L
149 +
150 +
      def censor[A](fa: OptionT[F, A])(f: L => L): OptionT[F, A] =
151 +
        OptionT(A.censor(fa.value)(f))
152 +
    }
153 +
21 154
}

@@ -1,11 +1,13 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 +
import cats.data.{ReaderWriterStateT => RWST, StateT}
5 +
4 6
/**
5 7
  * `MonadState[F, S]` is the capability to access and modify a state value
6 8
  * from inside the `F[_]` context, using `set(s: S): F[Unit]` and `get: F[S]`.
7 9
  *
8 -
  * MonadState has four external laws:
10 +
 * MonadState has four external laws:
9 11
  * {{{
10 12
  * def getThenSetDoesNothing = {
11 13
  *   get >>= set <-> pure(())
@@ -21,32 +23,68 @@
Loading
21 23
  * }
22 24
  * }}}
23 25
  *
24 -
  * `MonadState` has two internal law:
26 +
 * `MonadState` has two internal law:
25 27
  * {{{
26 28
  * def modifyIsGetThenSet(f: S => S) = {
27 29
  *   modify(f) <-> (inspect(f) flatMap set)
28 30
  * }
29 31
  *
30 -
  * def inspectLaw[A](f: S => A) = {
32 +
 * def inspectLaw[A](f: S => A) = {
31 33
  *   inspect(f) <-> (get map f)
32 34
  * }
33 35
  * }}}
34 36
  *
35 -
  */
37 +
 */
36 38
trait MonadState[F[_], S] extends Serializable {
37 -
  val monad: Monad[F]
39 +
  def monad: Monad[F]
40 +
41 +
  def inspect[A](f: S => A): F[A] = monad.map(get)(f)
42 +
43 +
  def modify(f: S => S): F[Unit] = monad.flatMap(inspect(f))(set)
38 44
39 45
  def get: F[S]
40 46
41 47
  def set(s: S): F[Unit]
48 +
}
42 49
43 -
  def inspect[A](f: S => A): F[A]
44 -
45 -
  def modify(f: S => S): F[Unit]
50 +
private[mtl] trait LowPriorityMonadStateInstances {
51 +
52 +
  implicit def monadStateForPartialOrder[F[_], G[_], S](
53 +
      implicit liftF: MonadPartialOrder[
54 +
        F,
55 +
        G
56 +
      ], // NB don't make this the *second* parameter; it won't infer
57 +
      ms: MonadState[F, S]): MonadState[G, S] =
58 +
    new MonadState[G, S] {
59 +
      val monad = liftF.monadG
60 +
      def get: G[S] = liftF(ms.get)
61 +
      def set(s: S): G[Unit] = liftF(ms.set(s))
62 +
    }
46 63
}
47 64
65 +
private[mtl] trait MonadStateInstances extends LowPriorityMonadStateInstances {
66 +
67 +
  implicit def monadStateForStateT[F[_]: Monad, S]: MonadState[StateT[F, S, *], S] =
68 +
    new MonadState[StateT[F, S, *], S] {
69 +
      val monad = Monad[StateT[F, S, *]]
70 +
71 +
      def get = StateT.get[F, S]
72 +
73 +
      def set(s: S) = StateT.set[F, S](s)
74 +
    }
75 +
76 +
  implicit def monadStateForRWST[F[_]: Monad, E, L: Monoid, S]
77 +
      : MonadState[RWST[F, E, L, S, *], S] =
78 +
    new MonadState[RWST[F, E, L, S, *], S] {
79 +
      val monad = Monad[RWST[F, E, L, S, *]]
48 80
49 -
object MonadState {
81 +
      def get = RWST.get[F, E, L, S]
82 +
83 +
      def set(s: S) = RWST.set[F, E, L, S](s)
84 +
    }
85 +
}
86 +
87 +
object MonadState extends MonadStateInstances {
50 88
  def get[F[_], S](implicit ev: MonadState[F, S]): F[S] =
51 89
    ev.get
52 90
@@ -55,7 +93,8 @@
Loading
55 93
56 94
  def setF[F[_]]: setFPartiallyApplied[F] = new setFPartiallyApplied[F]
57 95
58 -
  final private[mtl] class setFPartiallyApplied[F[_]](val dummy: Boolean = false) extends AnyVal {
96 +
  final private[mtl] class setFPartiallyApplied[F[_]](val dummy: Boolean = false)
97 +
      extends AnyVal {
59 98
    @inline def apply[E, A](e: E)(implicit state: MonadState[F, E]): F[Unit] =
60 99
      state.set(e)
61 100
  }
@@ -68,11 +107,3 @@
Loading
68 107
69 108
  def apply[F[_], S](implicit monadState: MonadState[F, S]): MonadState[F, S] = monadState
70 109
}
71 -
72 -
73 -
trait DefaultMonadState[F[_], S] extends MonadState[F, S] {
74 -
75 -
  def inspect[A](f: S => A): F[A] = monad.map(get)(f)
76 -
77 -
  def modify(f: S => S): F[Unit] = monad.flatMap(inspect(f))(set)
78 -
}

@@ -5,51 +5,44 @@
Loading
5 5
import cats.data.Ior
6 6
7 7
trait ChronicleSyntax {
8 -
  implicit def toChronicleOps[F[_], A](fa: F[A]): ChronicleOps[F, A] = new ChronicleOps[F, A](fa)
8 +
  implicit def toChronicleOps[F[_], A](fa: F[A]): ChronicleOps[F, A] =
9 +
    new ChronicleOps[F, A](fa)
9 10
  implicit def toChronicleIdOps[E](e: E): ChronicleIdOps[E] = new ChronicleIdOps[E](e)
10 -
  implicit def toChronicleIorOps[A, E](ior: E Ior A): ChronicleIorOps[A, E] = new ChronicleIorOps[A, E](ior)
11 +
  implicit def toChronicleIorOps[A, E](ior: E Ior A): ChronicleIorOps[A, E] =
12 +
    new ChronicleIorOps[A, E](ior)
11 13
}
12 14
13 15
final class ChronicleOps[F[_], A](val fa: F[A]) extends AnyVal {
14 -
  def materialize[E](implicit chronicle: MonadChronicle[F, E]): F[E Ior A] = {
16 +
  def materialize[E](implicit chronicle: MonadChronicle[F, E]): F[E Ior A] =
15 17
    chronicle.materialize(fa)
16 -
  }
17 18
18 -
  def memento[E](implicit chronicle: MonadChronicle[F, E]): F[Either[E, A]] = {
19 +
  def memento[E](implicit chronicle: MonadChronicle[F, E]): F[Either[E, A]] =
19 20
    chronicle.memento(fa)
20 -
  }
21 21
22 -
  def absolve[E](a: => A)(implicit chronicle: MonadChronicle[F, E]): F[A] = {
22 +
  def absolve[E](a: => A)(implicit chronicle: MonadChronicle[F, E]): F[A] =
23 23
    chronicle.absolve(fa)(a)
24 -
  }
25 24
26 -
  def condemn[E](implicit chronicle: MonadChronicle[F, E]): F[A] = {
25 +
  def condemn[E](implicit chronicle: MonadChronicle[F, E]): F[A] =
27 26
    chronicle.condemn(fa)
28 -
  }
29 27
30 -
  def retcon[E](cc: E => E)(implicit chronicle: MonadChronicle[F, E]): F[A] = {
28 +
  def retcon[E](cc: E => E)(implicit chronicle: MonadChronicle[F, E]): F[A] =
31 29
    chronicle.retcon(fa)(cc)
32 -
  }
33 30
}
34 31
35 32
final class ChronicleIdOps[E](val e: E) extends AnyVal {
36 -
  def dictate[F[_]](implicit chronicle: MonadChronicle[F, E]): F[Unit] = {
37 -
      chronicle.dictate(e)
38 -
    }
33 +
  def dictate[F[_]](implicit chronicle: MonadChronicle[F, E]): F[Unit] =
34 +
    chronicle.dictate(e)
39 35
40 -
  def disclose[F[_], A](implicit chronicle: MonadChronicle[F, E], monoid: Monoid[A]): F[A] = {
41 -
      chronicle.disclose(e)
42 -
    }
36 +
  def disclose[F[_], A](implicit chronicle: MonadChronicle[F, E], monoid: Monoid[A]): F[A] =
37 +
    chronicle.disclose(e)
43 38
44 -
  def confess[F[_], A](implicit chronicle: MonadChronicle[F, E]): F[A] = {
45 -
      chronicle.confess(e)
46 -
    }
39 +
  def confess[F[_], A](implicit chronicle: MonadChronicle[F, E]): F[A] =
40 +
    chronicle.confess(e)
47 41
}
48 42
49 43
final class ChronicleIorOps[A, E](val ior: E Ior A) extends AnyVal {
50 -
  def chronicle[F[_]](implicit chronicle: MonadChronicle[F, E]): F[A] = {
44 +
  def chronicle[F[_]](implicit chronicle: MonadChronicle[F, E]): F[A] =
51 45
    chronicle.chronicle(ior)
52 -
  }
53 46
}
54 47
55 48
object chronicle extends ChronicleSyntax

@@ -11,9 +11,10 @@
Loading
11 11
  implicit val handleInstance: ApplicativeHandle[F, E]
12 12
  implicit val applicativeInstance: Applicative[F] = handleInstance.applicative
13 13
14 -
  import handleInstance.{handle, handleWith, attempt}
15 -
  import raiseInstance.{raise, catchNonFatal}
16 -
  import handleInstance.applicative._
14 +
  import handleInstance.{attempt, handle, handleWith}
15 +
  import raiseInstance.{catchNonFatal, raise}
16 +
17 +
  import applicativeInstance._
17 18
18 19
  // external laws:
19 20
  def raiseAndHandleWithIsFunctionApplication[A](e: E, f: E => F[A]): IsEq[F[A]] =
@@ -36,16 +37,16 @@
Loading
36 37
37 38
  // internal laws:
38 39
  def catchNonFatalDefault[A](a: A, f: Throwable => E): IsEq[F[A]] =
39 -
    catchNonFatal(a)(f) <-> (try {
40 -
      pure(a)
41 -
    } catch {
40 +
    catchNonFatal(a)(f) <-> (try pure(a)
41 +
    catch {
42 42
      case NonFatal(ex) => raise(f(ex))
43 43
    })
44 44
45 45
}
46 46
47 47
object ApplicativeHandleLaws {
48 -
  def apply[F[_], E](implicit instance0: ApplicativeHandle[F, E]): ApplicativeHandleLaws[F, E] = {
48 +
  def apply[F[_], E](
49 +
      implicit instance0: ApplicativeHandle[F, E]): ApplicativeHandleLaws[F, E] = {
49 50
    new ApplicativeHandleLaws[F, E] {
50 51
      lazy val handleInstance: ApplicativeHandle[F, E] = instance0
51 52
      override lazy val raiseInstance: FunctorRaise[F, E] = instance0

@@ -10,18 +10,15 @@
Loading
10 10
  implicit def F: FunctorListen[F, L]
11 11
12 12
  // external laws:
13 -
  def listenRespectsTell(l: L): IsEq[F[(Unit, L)]] = {
13 +
  def listenRespectsTell(l: L): IsEq[F[(Unit, L)]] =
14 14
    F.listen(F.tell(l)) <-> F.tell(l).as(((), l))
15 -
  }
16 15
17 -
  def listenAddsNoEffects[A](fa: F[A]): IsEq[F[A]] = {
16 +
  def listenAddsNoEffects[A](fa: F[A]): IsEq[F[A]] =
18 17
    F.listen(fa).map(_._1) <-> fa
19 -
  }
20 18
21 19
  // internal law:
22 -
  def listensIsListenThenMap[A, B](fa: F[A], f: L => B): IsEq[F[(A, B)]] = {
20 +
  def listensIsListenThenMap[A, B](fa: F[A], f: L => B): IsEq[F[(A, B)]] =
23 21
    F.listens(fa)(f) <-> F.listen(fa).map { case (a, l) => (a, f(l)) }
24 -
  }
25 22
}
26 23
27 24
object FunctorListenLaws {

@@ -1,6 +1,8 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 +
import cats.data.{ReaderWriterStateT => RWST, _}
5 +
4 6
/**
5 7
  * `ApplicativeLocal[F, E]` lets you alter the `E` value that is observed by an `F[A]` value
6 8
  * using `ask`; the modification can only be observed from within that `F[A]` value.
@@ -31,17 +33,115 @@
Loading
31 33
trait ApplicativeLocal[F[_], E] extends ApplicativeAsk[F, E] with Serializable {
32 34
  def local[A](f: E => E)(fa: F[A]): F[A]
33 35
34 -
  def scope[A](e: E)(fa: F[A]): F[A]
36 +
  def scope[A](e: E)(fa: F[A]): F[A] = local(_ => e)(fa)
35 37
}
36 38
37 -
object ApplicativeLocal {
38 -
  def apply[F[_], A](implicit local: ApplicativeLocal[F, A]): ApplicativeLocal[F, A] = local
39 +
private[mtl] trait LowPriorityApplicativeLocalInstances
40 +
    extends LowPriorityApplicativeLocalInstancesCompat {
41 +
  implicit def applicativeLocalForKleisli[F[_]: Monad, E, R](
42 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[Kleisli[F, R, *], E] =
43 +
    new ApplicativeLocal[Kleisli[F, R, *], E]
44 +
      with ApplicativeAskForMonadPartialOrder[F, Kleisli[F, R, *], E] {
45 +
      def local[A](f: E => E)(fa: Kleisli[F, R, A]): Kleisli[F, R, A] =
46 +
        Kleisli(r => F0.local(f)(fa.run(r)))
47 +
      val F: ApplicativeAsk[F, E] = F0
48 +
      val lift: MonadPartialOrder[F, Kleisli[F, R, *]] =
49 +
        MonadPartialOrder.monadPartialOrderForKleisli[F, R]
50 +
    }
51 +
}
52 +
53 +
private[mtl] trait ApplicativeLocalInstances extends LowPriorityApplicativeLocalInstances {
54 +
55 +
  implicit def baseApplicativeLocalForKleisli[F[_], E](
56 +
      implicit F: Applicative[F]): ApplicativeLocal[Kleisli[F, E, *], E] =
57 +
    new ApplicativeLocal[Kleisli[F, E, *], E] {
58 +
      def local[A](f: E => E)(fa: Kleisli[F, E, A]) = fa.local(f)
59 +
      val applicative = Applicative[Kleisli[F, E, *]]
60 +
      def ask = Kleisli.ask[F, E]
61 +
    }
62 +
63 +
  implicit def baseApplicativeLocalForRWST[F[_], E, L, S](
64 +
      implicit F: Monad[F],
65 +
      L: Monoid[L]): ApplicativeLocal[RWST[F, E, L, S, *], E] =
66 +
    new ApplicativeLocal[RWST[F, E, L, S, *], E] {
67 +
      def local[A](f: E => E)(fa: RWST[F, E, L, S, A]) = fa.local(f)
68 +
      val applicative = Applicative[RWST[F, E, L, S, *]]
69 +
      def ask = RWST.ask[F, E, L, S]
70 +
    }
71 +
72 +
  implicit def applicativeLocalForWriterT[F[_]: Monad, E, L: Monoid](
73 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[WriterT[F, L, *], E] =
74 +
    new ApplicativeLocal[WriterT[F, L, *], E]
75 +
      with ApplicativeAskForMonadPartialOrder[F, WriterT[F, L, *], E] {
76 +
      def local[A](f: E => E)(fa: WriterT[F, L, A]): WriterT[F, L, A] =
77 +
        WriterT(F0.local(f)(fa.run))
78 +
      val F: ApplicativeAsk[F, E] = F0
79 +
      val lift: MonadPartialOrder[F, WriterT[F, L, *]] =
80 +
        MonadPartialOrder.monadPartialOrderForWriterT[F, L]
81 +
    }
39 82
40 -
  def local[F[_], E, A](f: E => E)(fa: F[A])(implicit local: ApplicativeLocal[F, E]): F[A] = local.local(f)(fa)
83 +
  implicit def applicativeLocalForRWST[F[_]: Monad, E, R, L: Monoid, S](
84 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[RWST[F, R, L, S, *], E] =
85 +
    new ApplicativeLocal[RWST[F, R, L, S, *], E]
86 +
      with ApplicativeAskForMonadPartialOrder[F, RWST[F, R, L, S, *], E] {
87 +
      def local[A](f: E => E)(fa: RWST[F, R, L, S, A]): RWST[F, R, L, S, A] =
88 +
        RWST((r, s) => F0.local(f)(fa.run(r, s)))
89 +
      val F: ApplicativeAsk[F, E] = F0
90 +
      val lift: MonadPartialOrder[F, RWST[F, R, L, S, *]] =
91 +
        MonadPartialOrder.monadPartialOrderForRWST[F, R, L, S]
92 +
    }
41 93
42 -
  def scope[F[_], E, A](e: E)(fa: F[A])(implicit local: ApplicativeLocal[F, E]): F[A] = local.scope(e)(fa)
94 +
  implicit def applicativeLocalForStateT[F[_]: Monad, E, S](
95 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[StateT[F, S, *], E] =
96 +
    new ApplicativeLocal[StateT[F, S, *], E]
97 +
      with ApplicativeAskForMonadPartialOrder[F, StateT[F, S, *], E] {
98 +
      def local[A](f: E => E)(fa: StateT[F, S, A]): StateT[F, S, A] =
99 +
        StateT(s => F0.local(f)(fa.run(s)))
100 +
      val F: ApplicativeAsk[F, E] = F0
101 +
      val lift: MonadPartialOrder[F, StateT[F, S, *]] =
102 +
        MonadPartialOrder.monadPartialOrderForStateT[F, S]
103 +
    }
104 +
105 +
  implicit def applicativeLocalForEitherT[F[_]: Monad, E, E2](
106 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[EitherT[F, E2, *], E] =
107 +
    new ApplicativeLocal[EitherT[F, E2, *], E]
108 +
      with ApplicativeAskForMonadPartialOrder[F, EitherT[F, E2, *], E] {
109 +
      def local[A](f: E => E)(fa: EitherT[F, E2, A]): EitherT[F, E2, A] =
110 +
        EitherT(F0.local(f)(fa.value))
111 +
      val F: ApplicativeAsk[F, E] = F0
112 +
      val lift: MonadPartialOrder[F, EitherT[F, E2, *]] =
113 +
        MonadPartialOrder.monadPartialOrderForEitherT[F, E2]
114 +
    }
115 +
116 +
  implicit def applicativeLocalForOptionT[F[_]: Monad, E](
117 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[OptionT[F, *], E] =
118 +
    new ApplicativeLocal[OptionT[F, *], E]
119 +
      with ApplicativeAskForMonadPartialOrder[F, OptionT[F, *], E] {
120 +
      def local[A](f: E => E)(fa: OptionT[F, A]): OptionT[F, A] =
121 +
        OptionT(F0.local(f)(fa.value))
122 +
      val F: ApplicativeAsk[F, E] = F0
123 +
      val lift: MonadPartialOrder[F, OptionT[F, *]] =
124 +
        MonadPartialOrder.monadPartialOrderForOptionT[F]
125 +
    }
126 +
127 +
  implicit def applicativeLocalForIorT[F[_]: Monad, E, E2: Semigroup](
128 +
      implicit F0: ApplicativeLocal[F, E]): ApplicativeLocal[IorT[F, E2, *], E] =
129 +
    new ApplicativeLocal[IorT[F, E2, *], E]
130 +
      with ApplicativeAskForMonadPartialOrder[F, IorT[F, E2, *], E] {
131 +
      def local[A](f: E => E)(fa: IorT[F, E2, A]): IorT[F, E2, A] =
132 +
        IorT(F0.local(f)(fa.value))
133 +
      val F: ApplicativeAsk[F, E] = F0
134 +
      val lift: MonadPartialOrder[F, IorT[F, E2, *]] =
135 +
        MonadPartialOrder.monadPartialOrderForIorT[F, E2]
136 +
    }
43 137
}
44 138
45 -
trait DefaultApplicativeLocal[F[_], E] extends DefaultApplicativeAsk[F, E] with ApplicativeLocal[F, E]{
46 -
  def scope[A](e: E)(fa: F[A]): F[A] = local(_ => e)(fa)
139 +
object ApplicativeLocal extends ApplicativeLocalInstances {
140 +
  def apply[F[_], A](implicit local: ApplicativeLocal[F, A]): ApplicativeLocal[F, A] = local
141 +
142 +
  def local[F[_], E, A](f: E => E)(fa: F[A])(implicit local: ApplicativeLocal[F, E]): F[A] =
143 +
    local.local(f)(fa)
144 +
145 +
  def scope[F[_], E, A](e: E)(fa: F[A])(implicit local: ApplicativeLocal[F, E]): F[A] =
146 +
    local.scope(e)(fa)
47 147
}

@@ -11,13 +11,12 @@
Loading
11 11
trait FunctorTellTests[F[_], L] extends Laws {
12 12
  def laws: FunctorTellLaws[F, L]
13 13
14 -
  def functorTell[A: Arbitrary](implicit
15 -
                                    ArbFA: Arbitrary[F[A]],
16 -
                                    ArbL: Arbitrary[L],
17 -
                                    CogenA: Cogen[A],
18 -
                                    EqFU: Eq[F[Unit]],
19 -
                                    EqFA: Eq[F[A]]
20 -
                                   ): RuleSet = {
14 +
  def functorTell[A: Arbitrary](
15 +
      implicit ArbFA: Arbitrary[F[A]],
16 +
      ArbL: Arbitrary[L],
17 +
      CogenA: Cogen[A],
18 +
      EqFU: Eq[F[Unit]],
19 +
      EqFA: Eq[F[A]]): RuleSet = {
21 20
    new DefaultRuleSet(
22 21
      name = "functorTell",
23 22
      parent = None,

@@ -7,7 +7,8 @@
Loading
7 7
}
8 8
9 9
final class RaiseOps[E](val e: E) extends AnyVal {
10 -
  def raise[F[_], A](implicit functorRaise: FunctorRaise[F, _ >: E]): F[A] = functorRaise.raise(e)
10 +
  def raise[F[_], A](implicit functorRaise: FunctorRaise[F, _ >: E]): F[A] =
11 +
    functorRaise.raise(e)
11 12
}
12 13
13 14
object raise extends RaiseSyntax

@@ -7,29 +7,26 @@
Loading
7 7
8 8
trait ApplicativeLocalLaws[F[_], E] extends ApplicativeAskLaws[F, E] {
9 9
  implicit val localInstance: ApplicativeLocal[F, E]
10 +
  override implicit val applicative = localInstance.applicative
10 11
11 12
  import localInstance.{local, scope}
12 13
  import askInstance.ask
13 -
  import askInstance.applicative._
14 +
  import applicative._
14 15
  import cats.syntax.apply._
15 16
16 17
  // external laws:
17 -
  def askReflectsLocal(f: E => E): IsEq[F[E]] = {
18 +
  def askReflectsLocal(f: E => E): IsEq[F[E]] =
18 19
    local(f)(ask) <-> map(ask)(f)
19 -
  }
20 20
21 -
  def localPureIsPure[A](a: A, f: E => E): IsEq[F[A]] = {
21 +
  def localPureIsPure[A](a: A, f: E => E): IsEq[F[A]] =
22 22
    local(f)(pure(a)) <-> pure(a)
23 -
  }
24 23
25 -
  def localDistributesOverAp[A, B](fa: F[A], ff: F[A => B], f: E => E): IsEq[F[B]] = {
26 -
    local(f)(ff ap fa) <-> (local(f)(ff) ap local(f)(fa))
27 -
  }
24 +
  def localDistributesOverAp[A, B](fa: F[A], ff: F[A => B], f: E => E): IsEq[F[B]] =
25 +
    local(f)(applicative.ap(ff)(fa)) <-> applicative.ap(local(f)(ff))(local(f)(fa))
28 26
29 27
  // internal law:
30 -
  def scopeIsLocalConst[A](fa: F[A], e: E): IsEq[F[A]] = {
28 +
  def scopeIsLocalConst[A](fa: F[A], e: E): IsEq[F[A]] =
31 29
    scope(e)(fa) <-> local(_ => e)(fa)
32 -
  }
33 30
34 31
}
35 32

@@ -16,30 +16,26 @@
Loading
16 16
  import monad.pure
17 17
18 18
  // external laws:
19 -
  def getThenSetDoesNothing: IsEq[F[Unit]] = {
19 +
  def getThenSetDoesNothing: IsEq[F[Unit]] =
20 20
    (get >>= set) <-> pure(())
21 -
  }
22 21
23 -
  def setThenGetReturnsSetted(s: S): IsEq[F[S]] = {
22 +
  def setThenGetReturnsSetted(s: S): IsEq[F[S]] =
24 23
    (set(s) *> get) <-> (set(s) *> pure(s))
25 -
  }
26 24
27 -
  def setThenSetSetsLast(s1: S, s2: S): IsEq[F[Unit]] = {
25 +
  def setThenSetSetsLast(s1: S, s2: S): IsEq[F[Unit]] =
28 26
    set(s1) *> set(s2) <-> set(s2)
29 -
  }
30 27
31 -
  def getThenGetGetsOnce: IsEq[F[S]] = {
28 +
  def getThenGetGetsOnce: IsEq[F[S]] =
32 29
    get *> get <-> get
33 -
  }
34 30
35 31
  // internal law:
36 -
  def modifyIsGetThenSet(f: S => S): IsEq[F[Unit]] = {
32 +
  def modifyIsGetThenSet(f: S => S): IsEq[F[Unit]] =
37 33
    modify(f) <-> ((get map f) flatMap set)
38 -
  }
39 34
}
40 35
41 36
object MonadStateLaws {
42 -
  def apply[F[_], S](implicit instance0: MonadState[F, S]): MonadStateLaws[F, S] = new MonadStateLaws[F, S] {
43 -
    override lazy val stateInstance: MonadState[F, S] = instance0
44 -
  }
37 +
  def apply[F[_], S](implicit instance0: MonadState[F, S]): MonadStateLaws[F, S] =
38 +
    new MonadStateLaws[F, S] {
39 +
      override lazy val stateInstance: MonadState[F, S] = instance0
40 +
    }
45 41
}

@@ -11,13 +11,11 @@
Loading
11 11
  implicit def functor(implicit F: FunctorTell[F, L]): Functor[F] = F.functor
12 12
13 13
  // internal laws:
14 -
  def writerIsTellAndMap[A](a: A, l: L): IsEq[F[A]] = {
14 +
  def writerIsTellAndMap[A](a: A, l: L): IsEq[F[A]] =
15 15
    F.tell(l).as(a) <-> F.writer(a, l)
16 -
  }
17 16
18 -
  def tupleIsWriterFlipped[A](a: A, l: L): IsEq[F[A]] = {
17 +
  def tupleIsWriterFlipped[A](a: A, l: L): IsEq[F[A]] =
19 18
    F.writer(a, l) <-> F.tuple((l, a))
20 -
  }
21 19
22 20
}
23 21

@@ -1,31 +1,166 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 -
import cats.data.Ior
4 +
import cats.data.{ReaderWriterStateT => RWST, IndexedReaderWriterStateT => IndexedRWST, _}
5 +
import cats.implicits._
6 +
import cats.data.Ior.Both
5 7
6 8
trait MonadChronicle[F[_], E] extends Serializable {
7 9
  val monad: Monad[F]
8 10
9 11
  def dictate(c: E): F[Unit]
10 12
11 -
  def disclose[A](c: E)(implicit M: Monoid[A]): F[A]
13 +
  def disclose[A](c: E)(implicit M: Monoid[A]): F[A] = monad.as(dictate(c), M.empty)
12 14
13 15
  def confess[A](c: E): F[A]
14 16
15 17
  def materialize[A](fa: F[A]): F[E Ior A]
16 18
17 -
  def memento[A](fa: F[A]): F[Either[E, A]]
18 -
19 -
  def absolve[A](fa: F[A])(a: => A): F[A]
20 -
21 -
  def condemn[A](fa: F[A]): F[A]
22 -
23 -
  def retcon[A](fa: F[A])(cc: E => E): F[A]
19 +
  def memento[A](fa: F[A]): F[Either[E, A]] =
20 +
    monad.flatMap(materialize(fa)) {
21 +
      case Ior.Left(e) => monad.pure(Left(e))
22 +
      case Ior.Right(a) => monad.pure(Right(a))
23 +
      case Ior.Both(e, a) => monad.as(dictate(e), Right(a))
24 +
    }
25 +
26 +
  def absolve[A](fa: F[A])(a: => A): F[A] =
27 +
    monad.map(materialize(fa)) {
28 +
      case Ior.Left(_) => a
29 +
      case Ior.Right(a0) => a0
30 +
      case Ior.Both(_, a0) => a0
31 +
    }
32 +
33 +
  def condemn[A](fa: F[A]): F[A] =
34 +
    monad.flatMap(materialize(fa)) {
35 +
      case Ior.Left(e) => confess(e)
36 +
      case Ior.Right(a) => monad.pure(a)
37 +
      case Ior.Both(e, _) => confess(e)
38 +
    }
39 +
40 +
  def retcon[A](fa: F[A])(cc: E => E): F[A] =
41 +
    monad.flatMap(materialize(fa)) {
42 +
      case Ior.Left(e) => confess(cc(e))
43 +
      case Ior.Right(a) => monad.pure(a)
44 +
      case Ior.Both(e, a) => monad.as(dictate(cc(e)), a)
45 +
    }
46 +
47 +
  def chronicle[A](ior: E Ior A): F[A] =
48 +
    ior match {
49 +
      case Ior.Left(e) => confess(e)
50 +
      case Ior.Right(a) => monad.pure(a)
51 +
      case Ior.Both(e, a) => monad.as(dictate(e), a)
52 +
    }
53 +
}
24 54
25 -
  def chronicle[A](ior: E Ior A): F[A]
55 +
private[mtl] trait MonadChronicleInstances {
56 +
57 +
  implicit def monadChronicleForIorT[F[_], E: Semigroup](
58 +
      implicit F: Monad[F]): MonadChronicle[IorT[F, E, *], E] =
59 +
    new MonadChronicle[IorT[F, E, *], E] {
60 +
      override val monad: Monad[IorT[F, E, *]] = IorT.catsDataMonadErrorForIorT
61 +
62 +
      override def dictate(c: E): IorT[F, E, Unit] = IorT.bothT[F](c, ())
63 +
64 +
      override def confess[A](c: E): IorT[F, E, A] = IorT.leftT[F, A](c)
65 +
66 +
      override def materialize[A](fa: IorT[F, E, A]): IorT[F, E, E Ior A] =
67 +
        IorT[F, E, E Ior A] {
68 +
          F.map(fa.value) {
69 +
            case Ior.Left(e) => Ior.right(Ior.left(e))
70 +
            case Ior.Right(a) => Ior.right(Ior.right(a))
71 +
            case Ior.Both(e, a) => Ior.right(Ior.both(e, a))
72 +
          }
73 +
        }
74 +
    }
75 +
76 +
  implicit final def chronicleIor[E](implicit S: Semigroup[E]): MonadChronicle[Ior[E, *], E] =
77 +
    new MonadChronicle[Ior[E, *], E] {
78 +
      override val monad: Monad[Ior[E, *]] = Ior.catsDataMonadErrorForIor
79 +
80 +
      override def dictate(c: E): Ior[E, Unit] = Ior.both(c, ())
81 +
82 +
      override def confess[A](c: E): Ior[E, A] = Ior.left(c)
83 +
84 +
      override def materialize[A](fa: Ior[E, A]): Ior[E, Ior[E, A]] =
85 +
        fa match {
86 +
          case Ior.Left(e) => Ior.right(Ior.left(e))
87 +
          case Ior.Right(a) => Ior.right(Ior.right(a))
88 +
          case Ior.Both(e, a) => Ior.right(Ior.both(e, a))
89 +
        }
90 +
    }
91 +
92 +
  implicit def monadChronicleForWriterT[F[_]: Monad, E, L: Monoid](
93 +
      implicit F: MonadChronicle[F, E]): MonadChronicle[WriterT[F, L, *], E] =
94 +
    new MonadChronicle[WriterT[F, L, *], E] {
95 +
      val monad: Monad[WriterT[F, L, *]] = WriterT.catsDataMonadForWriterT[F, L]
96 +
      def confess[A](c: E): WriterT[F, L, A] = WriterT.liftF(F.confess[A](c))
97 +
      def dictate(c: E): WriterT[F, L, Unit] = WriterT.liftF(F.dictate(c))
98 +
      def materialize[A](fa: WriterT[F, L, A]): WriterT[F, L, Ior[E, A]] =
99 +
        WriterT(F.materialize(fa.run).map(_.sequence))
100 +
    }
101 +
102 +
  implicit def monadChronicleForEitherT[F[_]: Monad, E, E2](
103 +
      implicit F: MonadChronicle[F, E]): MonadChronicle[EitherT[F, E2, *], E] =
104 +
    new MonadChronicle[EitherT[F, E2, *], E] {
105 +
      val monad: Monad[EitherT[F, E2, *]] = EitherT.catsDataMonadErrorForEitherT[F, E2]
106 +
      def confess[A](c: E): EitherT[F, E2, A] = EitherT.liftF(F.confess[A](c))
107 +
      def dictate(c: E): EitherT[F, E2, Unit] = EitherT.liftF(F.dictate(c))
108 +
      def materialize[A](fa: EitherT[F, E2, A]): EitherT[F, E2, Ior[E, A]] =
109 +
        EitherT(F.materialize(fa.value).map(_.sequence))
110 +
    }
111 +
112 +
  implicit def monadChronicleForKleisli[F[_]: Monad, E, R](
113 +
      implicit F: MonadChronicle[F, E]): MonadChronicle[Kleisli[F, R, *], E] =
114 +
    new MonadChronicle[Kleisli[F, R, *], E] {
115 +
      val monad: Monad[Kleisli[F, R, *]] = Kleisli.catsDataMonadForKleisli[F, R]
116 +
      def confess[A](c: E): Kleisli[F, R, A] = Kleisli.liftF(F.confess[A](c))
117 +
      def dictate(c: E): Kleisli[F, R, Unit] = Kleisli.liftF(F.dictate(c))
118 +
      def materialize[A](fa: Kleisli[F, R, A]): Kleisli[F, R, Ior[E, A]] =
119 +
        Kleisli(r => F.materialize(fa.run(r)))
120 +
    }
121 +
122 +
  implicit def monadChronicleForStateT[F[_]: Monad, E, S](
123 +
      implicit F: MonadChronicle[F, E]): MonadChronicle[StateT[F, S, *], E] =
124 +
    new MonadChronicle[StateT[F, S, *], E] {
125 +
      val monad: Monad[StateT[F, S, *]] = IndexedStateT.catsDataMonadForIndexedStateT[F, S]
126 +
      def confess[A](c: E): StateT[F, S, A] = StateT.liftF(F.confess[A](c))
127 +
      def dictate(c: E): StateT[F, S, Unit] = StateT.liftF(F.dictate(c))
128 +
      def materialize[A](fa: StateT[F, S, A]): StateT[F, S, Ior[E, A]] =
129 +
        StateT(s =>
130 +
          F.materialize(fa.run(s)).map {
131 +
            case Both(e, (s2, a)) => (s2, Both(e, a))
132 +
            case Ior.Left(e) => (s, Ior.Left(e))
133 +
            case Ior.Right((s2, a)) => (s2, Ior.Right(a))
134 +
          })
135 +
    }
136 +
137 +
  implicit def monadChronicleForOptionT[F[_]: Monad, E](
138 +
      implicit F: MonadChronicle[F, E]): MonadChronicle[OptionT[F, *], E] =
139 +
    new MonadChronicle[OptionT[F, *], E] {
140 +
      val monad: Monad[OptionT[F, *]] = OptionT.catsDataMonadForOptionT[F]
141 +
      def confess[A](c: E): OptionT[F, A] = OptionT.liftF(F.confess[A](c))
142 +
      def dictate(c: E): OptionT[F, Unit] = OptionT.liftF(F.dictate(c))
143 +
      def materialize[A](fa: OptionT[F, A]): OptionT[F, Ior[E, A]] =
144 +
        OptionT(F.materialize(fa.value).map(_.sequence))
145 +
    }
146 +
147 +
  implicit def monadChronicleForRWST[F[_]: Monad, E, R, L: Monoid, S](
148 +
      implicit F: MonadChronicle[F, E]): MonadChronicle[RWST[F, R, L, S, *], E] =
149 +
    new MonadChronicle[RWST[F, R, L, S, *], E] {
150 +
      val monad: Monad[RWST[F, R, L, S, *]] = IndexedRWST.catsDataMonadForRWST[F, R, L, S]
151 +
      def confess[A](c: E): RWST[F, R, L, S, A] = RWST.liftF(F.confess[A](c))
152 +
      def dictate(c: E): RWST[F, R, L, S, Unit] = RWST.liftF(F.dictate(c))
153 +
      def materialize[A](fa: RWST[F, R, L, S, A]): RWST[F, R, L, S, Ior[E, A]] =
154 +
        RWST((r, s) =>
155 +
          F.materialize(fa.run(r, s)).map {
156 +
            case Both(e, (l, s2, a)) => (l, s2, Both(e, a))
157 +
            case Ior.Left(e) => (Monoid[L].empty, s, Ior.Left(e))
158 +
            case Ior.Right((l, s2, a)) => (l, s2, Ior.Right(a))
159 +
          })
160 +
    }
26 161
}
27 162
28 -
object MonadChronicle {
163 +
object MonadChronicle extends MonadChronicleInstances {
29 164
  def dictate[F[_], E](e: E)(implicit ev: MonadChronicle[F, E]): F[Unit] = ev.dictate(e)
30 165
31 166
  def disclose[F[_], A, E](c: E)(implicit ev: MonadChronicle[F, E], m: Monoid[A]): F[A] =
@@ -42,37 +177,3 @@
Loading
42 177
  def apply[F[_], E](implicit ev: MonadChronicle[F, E]): MonadChronicle[F, E] =
43 178
    ev
44 179
}
45 -
46 -
trait DefaultMonadChronicle[F[_], E] extends MonadChronicle[F, E] {
47 -
  override def disclose[A](c: E)(implicit M: Monoid[A]): F[A] = monad.as(dictate(c), M.empty)
48 -
49 -
  override def memento[A](fa: F[A]): F[Either[E, A]] = monad.flatMap(materialize(fa)) {
50 -
    case Ior.Left(e)    => monad.pure(Left(e))
51 -
    case Ior.Right(a)   => monad.pure(Right(a))
52 -
    case Ior.Both(e, a) => monad.as(dictate(e), Right(a))
53 -
  }
54 -
55 -
  override def absolve[A](fa: F[A])(a: => A): F[A] = monad.map(materialize(fa)) {
56 -
    case Ior.Left(_)     => a
57 -
    case Ior.Right(a0)   => a0
58 -
    case Ior.Both(_, a0) => a0
59 -
  }
60 -
61 -
  override def condemn[A](fa: F[A]): F[A] = monad.flatMap(materialize(fa)) {
62 -
    case Ior.Left(e)    => confess(e)
63 -
    case Ior.Right(a)   => monad.pure(a)
64 -
    case Ior.Both(e, _) => confess(e)
65 -
  }
66 -
67 -
  override def retcon[A](fa: F[A])(cc: E => E): F[A] = monad.flatMap(materialize(fa)) {
68 -
    case Ior.Left(e)    => confess(cc(e))
69 -
    case Ior.Right(a)   => monad.pure(a)
70 -
    case Ior.Both(e, a) => monad.as(dictate(cc(e)), a)
71 -
  }
72 -
73 -
  override def chronicle[A](ior: E Ior A): F[A] = ior match {
74 -
    case Ior.Left(e)    => confess(e)
75 -
    case Ior.Right(a)   => monad.pure(a)
76 -
    case Ior.Both(e, a) => monad.as(dictate(e), a)
77 -
  }
78 -
}

@@ -7,9 +7,10 @@
Loading
7 7
}
8 8
9 9
final class LocalOps[F[_], A](val fa: F[A]) extends AnyVal {
10 -
  def local[E](f: E => E)(implicit applicativeLocal: ApplicativeLocal[F, E]): F[A] = applicativeLocal.local(f)(fa)
11 -
  def scope[E](e: E)(implicit applicativeLocal: ApplicativeLocal[F, E]): F[A] = applicativeLocal.scope(e)(fa)
10 +
  def local[E](f: E => E)(implicit applicativeLocal: ApplicativeLocal[F, E]): F[A] =
11 +
    applicativeLocal.local(f)(fa)
12 +
  def scope[E](e: E)(implicit applicativeLocal: ApplicativeLocal[F, E]): F[A] =
13 +
    applicativeLocal.scope(e)(fa)
12 14
}
13 15
14 16
object local extends LocalSyntax
15 -

@@ -0,0 +1,80 @@
Loading
1 +
package cats
2 +
package mtl
3 +
4 +
import cats.data._
5 +
6 +
/**
7 +
  * Encapsulates the notion of a monad, G, which contains all of
8 +
  * the effects of some other monad, F. This means that any effect
9 +
  * of type F[A] can be lifted to G[A], such that both F and G
10 +
  * form monads and the lifting distributes over flatMap and pure.
11 +
  *
12 +
 * Original idea by Kris Nuttycombe.
13 +
  */
14 +
trait MonadPartialOrder[F[_], G[_]] extends (F ~> G) {
15 +
  def monadF: Monad[F]
16 +
  def monadG: Monad[G]
17 +
}
18 +
19 +
private[mtl] trait MonadPartialOrderInstances {
20 +
21 +
  implicit def monadPartialOrderForStateT[F[_], S](
22 +
      implicit F: Monad[F]): MonadPartialOrder[F, StateT[F, S, *]] =
23 +
    new MonadPartialOrder[F, StateT[F, S, *]] {
24 +
      val monadF = F
25 +
      val monadG = IndexedStateT.catsDataMonadForIndexedStateT[F, S]
26 +
      def apply[A](fa: F[A]) = StateT.liftF(fa)
27 +
    }
28 +
29 +
  implicit def monadPartialOrderForKleisli[F[_], R](
30 +
      implicit F: Monad[F]): MonadPartialOrder[F, Kleisli[F, R, *]] =
31 +
    new MonadPartialOrder[F, Kleisli[F, R, *]] {
32 +
      val monadF = F
33 +
      val monadG = Kleisli.catsDataMonadForKleisli[F, R]
34 +
      def apply[A](fa: F[A]) = Kleisli.liftF(fa)
35 +
    }
36 +
37 +
  implicit def monadPartialOrderForEitherT[F[_], E](
38 +
      implicit F: Monad[F]): MonadPartialOrder[F, EitherT[F, E, *]] =
39 +
    new MonadPartialOrder[F, EitherT[F, E, *]] {
40 +
      val monadF = F
41 +
      val monadG = EitherT.catsDataMonadErrorForEitherT[F, E]
42 +
      def apply[A](fa: F[A]) = EitherT.liftF(fa)
43 +
    }
44 +
45 +
  implicit def monadPartialOrderForWriterT[F[_], L: Monoid](
46 +
      implicit F: Monad[F]): MonadPartialOrder[F, WriterT[F, L, *]] =
47 +
    new MonadPartialOrder[F, WriterT[F, L, *]] {
48 +
      val monadF = F
49 +
      val monadG = WriterT.catsDataMonadForWriterT[F, L]
50 +
      def apply[A](fa: F[A]) = WriterT.liftF(fa)
51 +
    }
52 +
53 +
  implicit def monadPartialOrderForOptionT[F[_]](
54 +
      implicit F: Monad[F]): MonadPartialOrder[F, OptionT[F, *]] =
55 +
    new MonadPartialOrder[F, OptionT[F, *]] {
56 +
      val monadF = F
57 +
      val monadG = OptionT.catsDataMonadForOptionT[F]
58 +
      def apply[A](fa: F[A]) = OptionT.liftF(fa)
59 +
    }
60 +
61 +
  implicit def monadPartialOrderForRWST[F[_], E, L: Monoid, S](
62 +
      implicit F: Monad[F]): MonadPartialOrder[F, RWST[F, E, L, S, *]] =
63 +
    new MonadPartialOrder[F, RWST[F, E, L, S, *]] {
64 +
      val monadF = F
65 +
      val monadG = IndexedReaderWriterStateT.catsDataMonadForRWST[F, E, L, S]
66 +
      def apply[A](fa: F[A]) = RWST.liftF(fa)
67 +
    }
68 +
69 +
  implicit def monadPartialOrderForIorT[F[_], E: Semigroup](
70 +
      implicit F: Monad[F]): MonadPartialOrder[F, IorT[F, E, *]] =
71 +
    new MonadPartialOrder[F, IorT[F, E, *]] {
72 +
      def apply[A](fa: F[A]): IorT[F, E, A] = IorT.liftF(fa)
73 +
      val monadF: Monad[F] = F
74 +
      val monadG: Monad[IorT[F, E, *]] = IorT.catsDataMonadErrorForIorT[F, E]
75 +
    }
76 +
}
77 +
78 +
object MonadPartialOrder extends MonadPartialOrderInstances {
79 +
  def apply[F[_], G[_]](implicit mpo: MonadPartialOrder[F, G]): MonadPartialOrder[F, G] = mpo
80 +
}

@@ -13,13 +13,12 @@
Loading
13 13
14 14
  def laws: MonadStateLaws[F, S] = MonadStateLaws[F, S]
15 15
16 -
  def monadState[A: Arbitrary](implicit
17 -
                               ArbFA: Arbitrary[F[A]],
18 -
                               ArbS: Arbitrary[S],
19 -
                               CogenS: Cogen[S],
20 -
                               EqFU: Eq[F[Unit]],
21 -
                               EqFS: Eq[F[S]]
22 -
                              ): RuleSet = {
16 +
  def monadState[A: Arbitrary](
17 +
      implicit ArbFA: Arbitrary[F[A]],
18 +
      ArbS: Arbitrary[S],
19 +
      CogenS: Cogen[S],
20 +
      EqFU: Eq[F[Unit]],
21 +
      EqFS: Eq[F[S]]): RuleSet = {
23 22
    new DefaultRuleSet(
24 23
      name = "monadState",
25 24
      parent = None,

@@ -10,16 +10,15 @@
Loading
10 10
trait FunctorListenTests[F[_], L] extends FunctorTellTests[F, L] {
11 11
  def laws: FunctorListenLaws[F, L]
12 12
13 -
  def functorListen[A: Arbitrary, B: Arbitrary](implicit
14 -
                                                    ArbFA: Arbitrary[F[A]],
15 -
                                                    ArbL: Arbitrary[L],
16 -
                                                    CogenA: Cogen[A],
17 -
                                                    CogenL: Cogen[L],
18 -
                                                    EqFU: Eq[F[Unit]],
19 -
                                                    EqFA: Eq[F[A]],
20 -
                                                    EqFAB: Eq[F[(A, B)]],
21 -
                                                    EqFUL: Eq[F[(Unit, L)]]
22 -
                                                   ): RuleSet = {
13 +
  def functorListen[A: Arbitrary, B: Arbitrary](
14 +
      implicit ArbFA: Arbitrary[F[A]],
15 +
      ArbL: Arbitrary[L],
16 +
      CogenA: Cogen[A],
17 +
      CogenL: Cogen[L],
18 +
      EqFU: Eq[F[Unit]],
19 +
      EqFA: Eq[F[A]],
20 +
      EqFAB: Eq[F[(A, B)]],
21 +
      EqFUL: Eq[F[(Unit, L)]]): RuleSet = {
23 22
    new DefaultRuleSet(
24 23
      name = "functorListen",
25 24
      parent = Some(functorTell[A]),

@@ -30,7 +30,8 @@
Loading
30 30
}
31 31
32 32
object ApplicativeCensorLaws {
33 -
  def apply[F[_], L](implicit instance0: ApplicativeCensor[F, L]): ApplicativeCensorLaws[F, L] = {
33 +
  def apply[F[_], L](
34 +
      implicit instance0: ApplicativeCensor[F, L]): ApplicativeCensorLaws[F, L] = {
34 35
    new ApplicativeCensorLaws[F, L] {
35 36
      def F: ApplicativeCensor[F, L] = instance0
36 37
    }

@@ -1,22 +1,26 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 +
import cats.data.{EitherT, StateT, IorT, Kleisli, OptionT, ReaderWriterStateT => RWST, WriterT}
5 +
import cats.implicits._
6 +
import cats.data.Ior
7 +
4 8
/**
5 9
  * `FunctorListen[F, L]` is a function `F[A] => F[(A, L)]` which exposes some state
6 10
  * that is contained in all `F[A]` values, and can be modified using `tell`.
7 11
  *
8 -
  * `FunctorListen` has two external laws:
12 +
 * `FunctorListen` has two external laws:
9 13
  * {{{
10 14
  * def listenRespectsTell(l: L) = {
11 15
  *   listen(tell(l)) <-> tell(l).as(((), l))
12 16
  * }
13 17
  *
14 -
  * def listenAddsNoEffects(fa: F[A]) = {
18 +
 * def listenAddsNoEffects(fa: F[A]) = {
15 19
  *   listen(fa).map(_._1) <-> fa
16 20
  * }
17 21
  * }}}
18 22
  *
19 -
  * `FunctorListen` has one internal law:
23 +
 * `FunctorListen` has one internal law:
20 24
  * {{{
21 25
  * def listensIsListenThenMap(fa: F[A], f: L => B) = {
22 26
  *   listens(fa)(f) <-> listen(fa).map { case (a, l) => (a, f(l)) }
@@ -24,19 +28,166 @@
Loading
24 28
  * }}}
25 29
  */
26 30
trait FunctorListen[F[_], L] extends FunctorTell[F, L] with Serializable {
31 +
27 32
  def listen[A](fa: F[A]): F[(A, L)]
28 33
29 -
  def listens[A, B](fa: F[A])(f: L => B): F[(A, B)]
34 +
  def listens[A, B](fa: F[A])(f: L => B): F[(A, B)] =
35 +
    functor.map(listen[A](fa)) {
36 +
      case (a, l) => (a, f(l))
37 +
    }
30 38
}
31 39
32 -
object FunctorListen {
33 -
  def apply[F[_], L](implicit listen: FunctorListen[F, L]): FunctorListen[F, L] = listen
34 -
  def listen[F[_], L, A](fa: F[A])(implicit ev: FunctorListen[F, L]): F[(A, L)] = ev.listen(fa)
35 -
  def listens[F[_], L, A, B](fa: F[A])(f: L => B)(implicit ev: FunctorListen[F, L]): F[(A, B)] = ev.listens(fa)(f)
40 +
private[mtl] abstract class FunctorListenWriterT[F[_]: Applicative, L]
41 +
    extends FunctorListen[WriterT[F, L, *], L] {
42 +
  def functor = Functor[WriterT[F, L, *]]
43 +
  def tell(l: L) = WriterT.tell[F, L](l)
44 +
  def listen[A](fa: WriterT[F, L, A]) = fa.listen
45 +
}
46 +
47 +
private[mtl] abstract class FunctorListenEitherT[F[_]: Functor, E, L](
48 +
    implicit F: FunctorListen[F, L])
49 +
    extends FunctorListen[EitherT[F, E, *], L] {
50 +
  def functor = Functor[EitherT[F, E, *]]
51 +
  def tell(l: L) = EitherT.liftF(F.tell(l))
52 +
  def listen[A](fa: EitherT[F, E, A]) =
53 +
    EitherT(F.listen(fa.value) map { case (e, l) => e.tupleRight(l) })
54 +
}
55 +
56 +
private[mtl] abstract class FunctorListenKleisli[F[_]: Functor, R, L](
57 +
    implicit F: FunctorListen[F, L])
58 +
    extends FunctorListen[Kleisli[F, R, *], L] {
59 +
  def functor = Functor[Kleisli[F, R, *]]
60 +
  def tell(l: L) = Kleisli.liftF(F.tell(l))
61 +
  def listen[A](fa: Kleisli[F, R, A]) =
62 +
    Kleisli(a => F.listen(fa.run(a)))
63 +
}
64 +
65 +
private[mtl] abstract class FunctorListenStateT[F[_]: Applicative, S, L](
66 +
    implicit F: FunctorListen[F, L])
67 +
    extends FunctorListen[StateT[F, S, *], L] {
68 +
  def functor = Functor[StateT[F, S, *]]
69 +
  def tell(l: L) = StateT.liftF(F.tell(l))
70 +
  def listen[A](fa: StateT[F, S, A]) =
71 +
    StateT applyF {
72 +
      fa.runF map { f => (s: S) =>
73 +
        F.listen(f(s)) map {
74 +
          case ((s, a), l) =>
75 +
            (s, (a, l))
76 +
        }
77 +
      }
78 +
    }
79 +
}
80 +
81 +
private[mtl] abstract class FunctorListenOptionT[F[_]: Functor, L](
82 +
    implicit F: FunctorListen[F, L])
83 +
    extends FunctorListen[OptionT[F, *], L] {
84 +
  def functor = Functor[OptionT[F, *]]
85 +
  def tell(l: L) = OptionT.liftF(F.tell(l))
86 +
  def listen[A](fa: OptionT[F, A]) =
87 +
    OptionT(F.listen(fa.value) map { case (opt, l) => opt.tupleRight(l) })
88 +
}
89 +
90 +
private[mtl] abstract class FunctorListenIorT[F[_]: Applicative, E, L](
91 +
    implicit F: FunctorListen[F, L])
92 +
    extends FunctorListen[IorT[F, E, *], L] {
93 +
  def functor = Functor[IorT[F, E, *]]
94 +
  def tell(l: L) = IorT.liftF(F.tell(l))
95 +
  def listen[A](fa: IorT[F, E, A]): IorT[F, E, (A, L)] =
96 +
    IorT(F.listen(fa.value).map {
97 +
      case (Ior.Both(e, a), l) => Ior.both(e, (a, l))
98 +
      case (Ior.Left(e), _) => Ior.left(e)
99 +
      case (Ior.Right(a), l) => Ior.right((a, l))
100 +
    })
101 +
}
102 +
103 +
private[mtl] abstract class FunctorListenRWST[F[_]: Applicative, E, L, S]
104 +
    extends FunctorListen[RWST[F, E, L, S, *], L] {
105 +
  def functor = Functor[RWST[F, E, L, S, *]]
106 +
  def tell(l: L) = RWST.tell(l)
107 +
  def listen[A](fa: RWST[F, E, L, S, A]) =
108 +
    RWST applyF {
109 +
      fa.runF map { f => (e: E, s: S) =>
110 +
        f(e, s) map {
111 +
          case (l, s, a) => (l, s, (a, l))
112 +
        }
113 +
      }
114 +
    }
115 +
}
116 +
117 +
private[mtl] abstract class FunctorListenInductiveWriterT[F[_]: Applicative, L0: Monoid, L](
118 +
    implicit F: FunctorListen[F, L])
119 +
    extends FunctorListen[WriterT[F, L0, *], L] {
120 +
  def functor = Functor[WriterT[F, L0, *]]
121 +
  def tell(l: L) = WriterT.liftF(F.tell(l))
122 +
  def listen[A](fa: WriterT[F, L0, A]) =
123 +
    WriterT(F.listen(fa.run) map { case ((l0, a), l) => (l0, (a, l)) })
36 124
}
37 125
38 -
trait DefaultFunctorListen[F[_], L] extends DefaultFunctorTell[F, L] with FunctorListen[F, L] {
39 -
  def listens[A, B](fa: F[A])(f: L => B): F[(A, B)] = functor.map(listen[A](fa)) {
40 -
    case (a, l) => (a, f(l))
41 -
  }
126 +
private[mtl] abstract class FunctorListenInductiveRWST[F[_]: Applicative, E, L0: Monoid, L, S](
127 +
    implicit F: FunctorListen[F, L])
128 +
    extends FunctorListen[RWST[F, E, L0, S, *], L] {
129 +
  def functor = Functor[RWST[F, E, L0, S, *]]
130 +
  def tell(l: L) =
131 +
    RWST.liftF[F, E, L0, S, Unit](F.tell(l))
132 +
133 +
  def listen[A](fa: RWST[F, E, L0, S, A]) =
134 +
    RWST applyF {
135 +
      fa.runF map { f => (e: E, s: S) =>
136 +
        F.listen(f(e, s)) map {
137 +
          case ((l0, s, a), l) =>
138 +
            (l0, s, (a, l))
139 +
        }
140 +
      }
141 +
    }
142 +
}
143 +
144 +
private[mtl] trait LowPriorityFunctorListenInstances {
145 +
146 +
  implicit def inductiveFunctorListenForWriterT[F[_]: Applicative, L, L0: Monoid](
147 +
      implicit F: FunctorListen[F, L]): FunctorListen[WriterT[F, L0, *], L] =
148 +
    new FunctorListenInductiveWriterT[F, L0, L] {}
149 +
150 +
  implicit def inductiveFunctorListenForRWST[F[_]: Applicative, E, L0: Monoid, S, L](
151 +
      implicit F: FunctorListen[F, L]): FunctorListen[RWST[F, E, L0, S, *], L] =
152 +
    new FunctorListenInductiveRWST[F, E, L0, L, S] {}
153 +
}
154 +
155 +
private[mtl] trait FunctorListenInstances
156 +
    extends LowPriorityFunctorListenInstances
157 +
    with LowPriorityFunctorListenInstancesCompat {
158 +
159 +
  implicit def functorListenForEitherT[F[_]: Functor, E, L](
160 +
      implicit F: FunctorListen[F, L]): FunctorListen[EitherT[F, E, *], L] =
161 +
    new FunctorListenEitherT[F, E, L] {}
162 +
163 +
  implicit def functorListenForKleisli[F[_]: Functor, R, L](
164 +
      implicit F: FunctorListen[F, L]): FunctorListen[Kleisli[F, R, *], L] =
165 +
    new FunctorListenKleisli[F, R, L] {}
166 +
167 +
  implicit def functorListenForOptionT[F[_]: Functor, L](
168 +
      implicit F: FunctorListen[F, L]): FunctorListen[OptionT[F, *], L] =
169 +
    new FunctorListenOptionT[F, L] {}
170 +
171 +
  implicit def functorListenForStateT[F[_]: Applicative, S, L](
172 +
      implicit F: FunctorListen[F, L]): FunctorListen[StateT[F, S, *], L] =
173 +
    new FunctorListenStateT[F, S, L] {}
174 +
175 +
  implicit def baseFunctorListenForWriterT[F[_]: Applicative, L]
176 +
      : FunctorListen[WriterT[F, L, *], L] =
177 +
    new FunctorListenWriterT[F, L] {}
178 +
179 +
  implicit def functorListenForIorT[F[_]: Applicative, K, L](
180 +
      implicit F: FunctorListen[F, K]): FunctorListen[IorT[F, L, *], K] =
181 +
    new FunctorListenIorT[F, L, K] {}
182 +
183 +
  implicit def baseFunctorListenForRWST[F[_]: Applicative, E, L, S]
184 +
      : FunctorListen[RWST[F, E, L, S, *], L] =
185 +
    new FunctorListenRWST[F, E, L, S] {}
186 +
}
187 +
188 +
object FunctorListen extends FunctorListenInstances {
189 +
  def apply[F[_], L](implicit listen: FunctorListen[F, L]): FunctorListen[F, L] = listen
190 +
  def listen[F[_], L, A](fa: F[A])(implicit ev: FunctorListen[F, L]): F[(A, L)] = ev.listen(fa)
191 +
  def listens[F[_], L, A, B](fa: F[A])(f: L => B)(implicit ev: FunctorListen[F, L]): F[(A, B)] =
192 +
    ev.listens(fa)(f)
42 193
}

@@ -12,13 +12,12 @@
Loading
12 12
  implicit val raiseInstance: FunctorRaise[F, E]
13 13
  def laws: FunctorRaiseLaws[F, E] = FunctorRaiseLaws[F, E]
14 14
15 -
  def functorRaise[A: Arbitrary](implicit
16 -
                                ArbFA: Arbitrary[F[A]],
17 -
                                ArbE: Arbitrary[E],
18 -
                                CogenA: Cogen[A],
19 -
                                EqFU: Eq[F[Unit]],
20 -
                                EqFA: Eq[F[A]]
21 -
                               ): RuleSet = {
15 +
  def functorRaise[A: Arbitrary](
16 +
      implicit ArbFA: Arbitrary[F[A]],
17 +
      ArbE: Arbitrary[E],
18 +
      CogenA: Cogen[A],
19 +
      EqFU: Eq[F[Unit]],
20 +
      EqFA: Eq[F[A]]): RuleSet = {
22 21
    new DefaultRuleSet(
23 22
      name = "functorRaise",
24 23
      parent = None,

@@ -3,7 +3,7 @@
Loading
3 3
package syntax
4 4
5 5
trait AskSyntax {
6 -
  implicit def toReaderOps[E, A](fun: E => A): ReaderOps[E, A]  = new ReaderOps(fun)
6 +
  implicit def toReaderOps[E, A](fun: E => A): ReaderOps[E, A] = new ReaderOps(fun)
7 7
}
8 8
9 9
final class ReaderOps[E, A](val fun: E => A) extends AnyVal {

@@ -7,6 +7,7 @@
Loading
7 7
import cats.syntax.functor._
8 8
import cats.syntax.apply._
9 9
import cats.syntax.semigroup._
10 +
10 11
/**
11 12
  * Created by Yuval.Itzchakov on 20/07/2018.
12 13
  */
@@ -17,53 +18,43 @@
Loading
17 18
  import chronicleInstance._
18 19
  import monad._
19 20
20 -
  def dictateThenMaterializeIsBoth(e: E): IsEq[F[E Ior Unit]] = {
21 +
  def dictateThenMaterializeIsBoth(e: E): IsEq[F[E Ior Unit]] =
21 22
    materialize(dictate(e)) <-> pure(Ior.Both(e, ()))
22 -
  }
23 23
24 -
  def confessThenMaterializeIsLeft[A](e: E): IsEq[F[E Ior A]] = {
24 +
  def confessThenMaterializeIsLeft[A](e: E): IsEq[F[E Ior A]] =
25 25
    materialize[A](confess(e)) <-> pure(Ior.Left(e))
26 -
  }
27 26
28 -
  def pureThenMaterializeIsRight[A](a: A): IsEq[F[E Ior A]] = {
27 +
  def pureThenMaterializeIsRight[A](a: A): IsEq[F[E Ior A]] =
29 28
    materialize[A](pure(a)) <-> pure(Ior.Right(a))
30 -
  }
31 29
32 -
  def confessThenAbsolveIsPure[A](a: A, e: E): IsEq[F[A]] = {
30 +
  def confessThenAbsolveIsPure[A](a: A, e: E): IsEq[F[A]] =
33 31
    absolve(confess[A](e))(a) <-> pure(a)
34 -
  }
35 32
36 -
  def dictateThenCondemIsConfess[A](e: E): IsEq[F[Unit]] = {
33 +
  def dictateThenCondemIsConfess[A](e: E): IsEq[F[Unit]] =
37 34
    condemn(dictate(e)) <-> confess(e)
38 -
  }
39 35
40 -
  def confessThenMementoIsLeft[A](e: E): IsEq[F[Either[E, A]]] = {
36 +
  def confessThenMementoIsLeft[A](e: E): IsEq[F[Either[E, A]]] =
41 37
    memento[A](confess(e)) <-> pure(Left(e))
42 -
  }
43 38
44 -
  def dictateThenMementoIsDictateRightUnit(e: E): IsEq[F[Either[E, Unit]]] = {
39 +
  def dictateThenMementoIsDictateRightUnit(e: E): IsEq[F[Either[E, Unit]]] =
45 40
    memento(dictate(e)) <-> dictate(e).map(_ => Right(()))
46 -
  }
47 41
48 -
  def confessThenRetconIsConfess[A](f: E => E, e: E): IsEq[F[A]] = {
42 +
  def confessThenRetconIsConfess[A](f: E => E, e: E): IsEq[F[A]] =
49 43
    retcon(confess[A](e))(f) <-> confess[A](f(e))
50 -
  }
51 44
52 -
  def dictateThenRetconIsDictate[A](f: E => E, e: E): IsEq[F[Unit]] = {
45 +
  def dictateThenRetconIsDictate[A](f: E => E, e: E): IsEq[F[Unit]] =
53 46
    retcon(dictate(e))(f) <-> dictate(f(e))
54 -
  }
55 47
56 -
  def pureThenRetconIsPure[A](f: E => E, a: A): IsEq[F[A]] = {
48 +
  def pureThenRetconIsPure[A](f: E => E, a: A): IsEq[F[A]] =
57 49
    retcon(pure(a))(f) <-> pure(a)
58 -
  }
59 50
60 -
  def dictateSharkDictateIsDictateSemigroup(e0: E, e: E)(implicit ev: Semigroup[E]): IsEq[F[Unit]] = {
51 +
  def dictateSharkDictateIsDictateSemigroup(e0: E, e: E)(
52 +
      implicit ev: Semigroup[E]): IsEq[F[Unit]] =
61 53
    dictate(e0) *> dictate(e) <-> dictate(e0 |+| e)
62 -
  }
63 54
64 -
  def dictateSharkConfessIsConfessSemigroup[A](e0: E, e: E)(implicit ev: Semigroup[E]): IsEq[F[A]] = {
55 +
  def dictateSharkConfessIsConfessSemigroup[A](e0: E, e: E)(
56 +
      implicit ev: Semigroup[E]): IsEq[F[A]] =
65 57
    dictate(e0) *> confess[A](e) <-> confess[A](e0 |+| e)
66 -
  }
67 58
}
68 59
69 60
object MonadChronicleLaws {

@@ -14,18 +14,19 @@
Loading
14 14
15 15
  def laws: MonadChronicleLaws[F, E] = MonadChronicleLaws[F, E]
16 16
17 -
  def monadChronicle[A: Arbitrary](implicit ArbFA: Arbitrary[F[A]],
18 -
                                   ArbE: Arbitrary[E],
19 -
                                   CogenA: Cogen[A],
20 -
                                   CogenE: Cogen[E],
21 -
                                   EqFU: Eq[F[E]],
22 -
                                   EqFA: Eq[F[A]],
23 -
                                   EqFUnit: Eq[F[Unit]],
24 -
                                   EqFEIorUnit: Eq[F[E Ior Unit]],
25 -
                                   EqFEUnit: Eq[F[Either[E, Unit]]],
26 -
                                   EqFIor: Eq[F[E Ior A]],
27 -
                                   EqFEither: Eq[F[Either[E, A]]],
28 -
                                   SemigroupE: Semigroup[E]): RuleSet = {
17 +
  def monadChronicle[A: Arbitrary](
18 +
      implicit ArbFA: Arbitrary[F[A]],
19 +
      ArbE: Arbitrary[E],
20 +
      CogenA: Cogen[A],
21 +
      CogenE: Cogen[E],
22 +
      EqFU: Eq[F[E]],
23 +
      EqFA: Eq[F[A]],
24 +
      EqFUnit: Eq[F[Unit]],
25 +
      EqFEIorUnit: Eq[F[E Ior Unit]],
26 +
      EqFEUnit: Eq[F[Either[E, Unit]]],
27 +
      EqFIor: Eq[F[E Ior A]],
28 +
      EqFEither: Eq[F[Either[E, A]]],
29 +
      SemigroupE: Semigroup[E]): RuleSet = {
29 30
    new DefaultRuleSet(
30 31
      name = "monadChronicle",
31 32
      parent = None,
@@ -35,12 +36,15 @@
Loading
35 36
      "confess then retcon is confess" -> ∀(laws.confessThenRetconIsConfess[A] _),
36 37
      "dictate then condem is confess" -> ∀(laws.dictateThenCondemIsConfess[A] _),
37 38
      "dictate then materialize is both" -> ∀(laws.dictateThenMaterializeIsBoth _),
38 -
      "dictate then memento is dictate right unit" -> ∀(laws.dictateThenMementoIsDictateRightUnit _),
39 +
      "dictate then memento is dictate right unit" -> ∀(
40 +
        laws.dictateThenMementoIsDictateRightUnit _),
39 41
      "dictate then retcon is dictate" -> ∀(laws.dictateThenRetconIsDictate[A] _),
40 42
      "pure then materialize is right" -> ∀(laws.pureThenMaterializeIsRight[A] _),
41 43
      "pure then retcon is pure" -> ∀(laws.pureThenRetconIsPure[A] _),
42 -
      "dictate shark dictate is dictate semigroup" -> ∀(laws.dictateSharkDictateIsDictateSemigroup _),
43 -
      "dictate shark confess is confess semigroup" -> ∀(laws.dictateSharkConfessIsConfessSemigroup[A] _)
44 +
      "dictate shark dictate is dictate semigroup" -> ∀(
45 +
        laws.dictateSharkDictateIsDictateSemigroup _),
46 +
      "dictate shark confess is confess semigroup" -> ∀(
47 +
        laws.dictateSharkConfessIsConfessSemigroup[A] _)
44 48
    )
45 49
  }
46 50
}

@@ -10,16 +10,15 @@
Loading
10 10
trait ApplicativeCensorTests[F[_], L] extends FunctorListenTests[F, L] {
11 11
  def laws: ApplicativeCensorLaws[F, L]
12 12
13 -
  def applicativeCensor[A: Arbitrary, B: Arbitrary](implicit
14 -
                                                    ArbFA: Arbitrary[F[A]],
15 -
                                                    ArbL: Arbitrary[L],
16 -
                                                    CogenA: Cogen[A],
17 -
                                                    CogenL: Cogen[L],
18 -
                                                    EqFU: Eq[F[Unit]],
19 -
                                                    EqFA: Eq[F[A]],
20 -
                                                    EqFAB: Eq[F[(A, B)]],
21 -
                                                    EqFUL: Eq[F[(Unit, L)]]
22 -
                                                 ): RuleSet = {
13 +
  def applicativeCensor[A: Arbitrary, B: Arbitrary](
14 +
      implicit ArbFA: Arbitrary[F[A]],
15 +
      ArbL: Arbitrary[L],
16 +
      CogenA: Cogen[A],
17 +
      CogenL: Cogen[L],
18 +
      EqFU: Eq[F[Unit]],
19 +
      EqFA: Eq[F[A]],
20 +
      EqFAB: Eq[F[(A, B)]],
21 +
      EqFUL: Eq[F[(Unit, L)]]): RuleSet = {
23 22
    new DefaultRuleSet(
24 23
      name = "applicativeCensor",
25 24
      parent = Some(functorListen[A, B]),
@@ -34,7 +33,8 @@
Loading
34 33
}
35 34
36 35
object ApplicativeCensorTests {
37 -
  def apply[F[_], L](implicit instance0: ApplicativeCensor[F, L]): ApplicativeCensorTests[F, L] = {
36 +
  def apply[F[_], L](
37 +
      implicit instance0: ApplicativeCensor[F, L]): ApplicativeCensorTests[F, L] = {
38 38
    new ApplicativeCensorTests[F, L] {
39 39
      override def laws: ApplicativeCensorLaws[F, L] = ApplicativeCensorLaws[F, L]
40 40
    }

@@ -12,22 +12,22 @@
Loading
12 12
13 13
  override def laws: ApplicativeHandleLaws[F, E] = ApplicativeHandleLaws[F, E]
14 14
15 -
  def applicativeHandle[A: Arbitrary](implicit
16 -
                                        ArbFA: Arbitrary[F[A]],
17 -
                                        ArbEE: Arbitrary[E => E],
18 -
                                        ArbE: Arbitrary[E],
19 -
                                        CogenA: Cogen[A],
20 -
                                        CogenE: Cogen[E],
21 -
                                        EqFU: Eq[F[E]],
22 -
                                        EqFA: Eq[F[A]],
23 -
                                        EqFUnit: Eq[F[Unit]],
24 -
                                        EqEitherA: Eq[F[Either[E, A]]],
25 -
                                        EqEitherUnit: Eq[F[Either[E, Unit]]]
26 -
                                       ): RuleSet = {
15 +
  def applicativeHandle[A: Arbitrary](
16 +
      implicit ArbFA: Arbitrary[F[A]],
17 +
      ArbEE: Arbitrary[E => E],
18 +
      ArbE: Arbitrary[E],
19 +
      CogenA: Cogen[A],
20 +
      CogenE: Cogen[E],
21 +
      EqFU: Eq[F[E]],
22 +
      EqFA: Eq[F[A]],
23 +
      EqFUnit: Eq[F[Unit]],
24 +
      EqEitherA: Eq[F[Either[E, A]]],
25 +
      EqEitherUnit: Eq[F[Either[E, Unit]]]): RuleSet = {
27 26
    new DefaultRuleSet(
28 27
      name = "applicativeHandle",
29 28
      parent = Some(functorRaise[A]),
30 -
      "raise and handleWith is function application" -> ∀(laws.raiseAndHandleWithIsFunctionApplication[A]  _),
29 +
      "raise and handleWith is function application" -> ∀(
30 +
        laws.raiseAndHandleWithIsFunctionApplication[A] _),
31 31
      "raise and handle is pure and function application" -> ∀(laws.raiseAndHandleIsPure[A] _),
32 32
      "pure and handleWith is pure" -> ∀(laws.handleWithPureIsPure[A] _),
33 33
      "pure and handle is pure" -> ∀(laws.handlePureIsPure[A] _),
@@ -40,7 +40,8 @@
Loading
40 40
}
41 41
42 42
object ApplicativeHandleTests {
43 -
  def apply[F[_], E](implicit instance0: ApplicativeHandle[F, E]): ApplicativeHandleTests[F, E] = {
43 +
  def apply[F[_], E](
44 +
      implicit instance0: ApplicativeHandle[F, E]): ApplicativeHandleTests[F, E] = {
44 45
    new ApplicativeHandleTests[F, E] {
45 46
      override lazy val handleInstance: ApplicativeHandle[F, E] = instance0
46 47
      override lazy val raiseInstance: FunctorRaise[F, E] = instance0

@@ -7,13 +7,11 @@
Loading
7 7
}
8 8
9 9
final class ListenOps[F[_], A](val fa: F[A]) extends AnyVal {
10 -
  def listen[L](implicit listen: FunctorListen[F, L]): F[(A, L)] = {
10 +
  def listen[L](implicit listen: FunctorListen[F, L]): F[(A, L)] =
11 11
    listen.listen(fa)
12 -
  }
13 12
14 -
  def listens[L, B](f: L => B)(implicit listen: FunctorListen[F, L]): F[(A, B)] = {
13 +
  def listens[L, B](f: L => B)(implicit listen: FunctorListen[F, L]): F[(A, B)] =
15 14
    listen.listens(fa)(f)
16 -
  }
17 15
18 16
}
19 17

@@ -13,14 +13,12 @@
Loading
13 13
  implicit val applicative: Applicative[F] = askInstance.applicative
14 14
15 15
  // external law:
16 -
  def askAddsNoEffects[A](fa: F[A]): IsEq[F[A]] = {
16 +
  def askAddsNoEffects[A](fa: F[A]): IsEq[F[A]] =
17 17
    ask *> fa <-> fa
18 -
  }
19 18
20 19
  // internal law:
21 -
  def readerIsAskAndMap[A](f: E => A): IsEq[F[A]] = {
20 +
  def readerIsAskAndMap[A](f: E => A): IsEq[F[A]] =
22 21
    ask.map(f) <-> reader(f)
23 -
  }
24 22
}
25 23
26 24
object ApplicativeAskLaws {

@@ -1,34 +1,208 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 -
import cats.data.EitherT
4 +
import cats.data._
5 5
6 6
trait ApplicativeHandle[F[_], E] extends FunctorRaise[F, E] with Serializable {
7 -
  val applicative: Applicative[F]
7 +
  def applicative: Applicative[F]
8 8
9 9
  def handleWith[A](fa: F[A])(f: E => F[A]): F[A]
10 10
11 -
  def attempt[A](fa: F[A]): F[Either[E, A]]
11 +
  def attempt[A](fa: F[A]): F[Either[E, A]] =
12 +
    handleWith(applicative.map(fa)(Right(_): Either[E, A]))(e => applicative.pure(Left(e)))
12 13
13 -
  def attemptT[A](fa: F[A]): EitherT[F, E, A]
14 +
  def attemptT[A](fa: F[A]): EitherT[F, E, A] =
15 +
    EitherT(attempt(fa))
14 16
15 -
  def handle[A](fa: F[A])(f: E => A): F[A]
17 +
  def handle[A](fa: F[A])(f: E => A): F[A] =
18 +
    handleWith(fa)(e => applicative.pure(f(e)))
16 19
}
17 20
21 +
private[mtl] trait ApplicativeHandleInstances {
18 22
23 +
  implicit final def handleEitherT[M[_], E](
24 +
      implicit M: Monad[M]): ApplicativeHandle[EitherTC[M, E]#l, E] = {
25 +
    new ApplicativeHandle[EitherTC[M, E]#l, E] {
26 +
      val applicative: Applicative[EitherTC[M, E]#l] = EitherT.catsDataMonadErrorForEitherT(M)
19 27
20 -
object ApplicativeHandle {
21 -
  def apply[F[_], E](implicit ev: ApplicativeHandle[F, E]): ApplicativeHandle[F, E] = ev
22 -
}
28 +
      val functor: Functor[EitherTC[M, E]#l] = EitherT.catsDataFunctorForEitherT(M)
23 29
30 +
      def raise[A](e: E): EitherT[M, E, A] = EitherT(M.pure(Left(e)))
24 31
25 -
trait DefaultApplicativeHandle[F[_], E] extends ApplicativeHandle[F, E] {
26 -
  def attempt[A](fa: F[A]): F[Either[E, A]] =
27 -
    handleWith(applicative.map(fa)(Right(_): Either[E, A]))(e => applicative.pure(Left(e)))
32 +
      def handleWith[A](fa: EitherT[M, E, A])(f: E => EitherT[M, E, A]): EitherT[M, E, A] =
33 +
        EitherT(M.flatMap(fa.value) {
34 +
          case Left(e) => f(e).value
35 +
          case r @ Right(_) => M.pure(r)
36 +
        })
37 +
    }
38 +
  }
28 39
29 -
  def attemptT[A](fa: F[A]): EitherT[F, E, A] =
30 -
    EitherT(attempt(fa))
40 +
  implicit final def handleEither[E]: ApplicativeHandle[EitherC[E]#l, E] = {
41 +
    new ApplicativeHandle[EitherC[E]#l, E] {
42 +
      val applicative: Applicative[EitherC[E]#l] =
43 +
        cats.instances.either.catsStdInstancesForEither[E]
31 44
32 -
  def handle[A](fa: F[A])(f: E => A): F[A] =
33 -
    handleWith(fa)(e => applicative.pure(f(e)))
45 +
      val functor: Functor[EitherC[E]#l] = cats.instances.either.catsStdInstancesForEither
46 +
47 +
      def raise[A](e: E): Either[E, A] = Left(e)
48 +
49 +
      def handleWith[A](fa: Either[E, A])(f: E => Either[E, A]): Either[E, A] =
50 +
        fa match {
51 +
          case Left(e) => f(e)
52 +
          case r @ Right(_) => r
53 +
        }
54 +
    }
55 +
  }
56 +
57 +
  implicit final def handleOptionT[M[_]](
58 +
      implicit M: Monad[M]): ApplicativeHandle[OptionTC[M]#l, Unit] = {
59 +
    new ApplicativeHandle[OptionTC[M]#l, Unit] {
60 +
      val applicative: Applicative[OptionTC[M]#l] = OptionT.catsDataMonadForOptionT(M)
61 +
62 +
      val functor: Functor[OptionTC[M]#l] = OptionT.catsDataFunctorForOptionT(M)
63 +
64 +
      def raise[A](e: Unit): OptionT[M, A] = OptionT(M.pure(None))
65 +
66 +
      def handleWith[A](fa: OptionT[M, A])(f: Unit => OptionT[M, A]): OptionT[M, A] =
67 +
        OptionT(M.flatMap(fa.value) {
68 +
          case None => f(()).value
69 +
          case s @ Some(_) => M.pure(s)
70 +
        })
71 +
    }
72 +
  }
73 +
74 +
  implicit final def handleOption[E]: ApplicativeHandle[Option, Unit] = {
75 +
    new ApplicativeHandle[Option, Unit] {
76 +
      val applicative: Applicative[Option] = cats.instances.option.catsStdInstancesForOption
77 +
78 +
      val functor: Functor[Option] = cats.instances.option.catsStdInstancesForOption
79 +
80 +
      def raise[A](e: Unit): Option[A] = None
81 +
82 +
      def handleWith[A](fa: Option[A])(f: Unit => Option[A]): Option[A] =
83 +
        fa match {
84 +
          case None => f(())
85 +
          case s @ Some(_) => s
86 +
        }
87 +
    }
88 +
  }
89 +
90 +
  implicit final def handleValidated[E](
91 +
      implicit E: Semigroup[E]): ApplicativeHandle[Validated[E, ?], E] =
92 +
    new ApplicativeHandle[Validated[E, ?], E] {
93 +
      val applicative: Applicative[Validated[E, ?]] =
94 +
        Validated.catsDataApplicativeErrorForValidated[E]
95 +
96 +
      val functor: Functor[Validated[E, ?]] = Validated.catsDataApplicativeErrorForValidated[E]
97 +
98 +
      def raise[A](e: E): Validated[E, A] = Validated.Invalid(e)
99 +
100 +
      def handleWith[A](fa: Validated[E, A])(f: E => Validated[E, A]): Validated[E, A] =
101 +
        fa match {
102 +
          case Validated.Invalid(e) => f(e)
103 +
          case v @ Validated.Valid(_) => v
104 +
        }
105 +
    }
106 +
107 +
  implicit final def handleIorT[F[_], E](
108 +
      implicit E: Semigroup[E],
109 +
      F: Monad[F]): ApplicativeHandle[IorT[F, E, *], E] =
110 +
    new ApplicativeHandle[IorT[F, E, *], E] {
111 +
      val applicative: Applicative[IorT[F, E, ?]] = IorT.catsDataMonadErrorForIorT[F, E]
112 +
113 +
      val functor: Functor[IorT[F, E, ?]] = IorT.catsDataMonadErrorForIorT[F, E]
114 +
115 +
      def raise[A](e: E): IorT[F, E, A] = IorT.leftT(e)
116 +
117 +
      def handleWith[A](fa: IorT[F, E, A])(f: E => IorT[F, E, A]): IorT[F, E, A] =
118 +
        IorT(F.flatMap(fa.value) {
119 +
          case Ior.Left(e) => f(e).value
120 +
          case e @ _ => F.pure(e)
121 +
        })
122 +
    }
123 +
124 +
  implicit final def handleIor[E](implicit E: Semigroup[E]): ApplicativeHandle[Ior[E, *], E] =
125 +
    new ApplicativeHandle[Ior[E, *], E] {
126 +
      val applicative: Applicative[Ior[E, ?]] = Ior.catsDataMonadErrorForIor[E]
127 +
128 +
      val functor: Functor[Ior[E, ?]] = Ior.catsDataMonadErrorForIor[E]
129 +
130 +
      def raise[A](e: E): Ior[E, A] = Ior.Left(e)
131 +
132 +
      def handleWith[A](fa: Ior[E, A])(f: E => Ior[E, A]): Ior[E, A] =
133 +
        fa match {
134 +
          case Ior.Left(e) => f(e)
135 +
          case _ => fa
136 +
        }
137 +
    }
138 +
139 +
  implicit final def applicativeHandleKleisli[F[_], E, R](
140 +
      implicit F0: ApplicativeHandle[F, E],
141 +
      M: Monad[F]): ApplicativeHandle[Kleisli[F, R, *], E] =
142 +
    new ApplicativeHandle[Kleisli[F, R, *], E]
143 +
      with FunctorRaiseMonadPartialOrder[F, Kleisli[F, R, *], E] {
144 +
      val applicative: Applicative[Kleisli[F, R, *]] = Kleisli.catsDataMonadForKleisli[F, R]
145 +
146 +
      val F: FunctorRaise[F, E] = F0
147 +
      val lift: MonadPartialOrder[F, Kleisli[F, R, *]] =
148 +
        MonadPartialOrder.monadPartialOrderForKleisli[F, R]
149 +
150 +
      def handleWith[A](fa: Kleisli[F, R, A])(f: E => Kleisli[F, R, A]): Kleisli[F, R, A] =
151 +
        Kleisli(r => F0.handleWith(fa.run(r))(e => f(e).run(r)))
152 +
    }
153 +
154 +
  implicit final def applicativeHandleWriterT[F[_], E, L](
155 +
      implicit F0: ApplicativeHandle[F, E],
156 +
      M: Monad[F],
157 +
      L: Monoid[L]): ApplicativeHandle[WriterT[F, L, *], E] =
158 +
    new ApplicativeHandle[WriterT[F, L, *], E]
159 +
      with FunctorRaiseMonadPartialOrder[F, WriterT[F, L, *], E] {
160 +
      val applicative: Applicative[WriterT[F, L, *]] =
161 +
        WriterT.catsDataApplicativeForWriterT[F, L]
162 +
163 +
      val F: FunctorRaise[F, E] = F0
164 +
      val lift: MonadPartialOrder[F, WriterT[F, L, *]] =
165 +
        MonadPartialOrder.monadPartialOrderForWriterT[F, L]
166 +
167 +
      def handleWith[A](fa: WriterT[F, L, A])(f: E => WriterT[F, L, A]): WriterT[F, L, A] =
168 +
        WriterT(F0.handleWith(fa.run)(e => f(e).run))
169 +
    }
170 +
171 +
  implicit final def applicativeHandleStateT[F[_], E, S](
172 +
      implicit F0: ApplicativeHandle[F, E],
173 +
      M: Monad[F]): ApplicativeHandle[StateT[F, S, *], E] =
174 +
    new ApplicativeHandle[StateT[F, S, *], E]
175 +
      with FunctorRaiseMonadPartialOrder[F, StateT[F, S, *], E] {
176 +
      val applicative: Applicative[StateT[F, S, *]] =
177 +
        IndexedStateT.catsDataMonadForIndexedStateT[F, S]
178 +
179 +
      val F: FunctorRaise[F, E] = F0
180 +
      val lift: MonadPartialOrder[F, StateT[F, S, *]] =
181 +
        MonadPartialOrder.monadPartialOrderForStateT[F, S]
182 +
183 +
      def handleWith[A](fa: StateT[F, S, A])(f: E => StateT[F, S, A]): StateT[F, S, A] =
184 +
        StateT(s => F0.handleWith(fa.run(s))(e => f(e).run(s)))
185 +
    }
186 +
187 +
  implicit final def applicativeHandleRWST[F[_], E, R, L, S](
188 +
      implicit F0: ApplicativeHandle[F, E],
189 +
      M: Monad[F],
190 +
      L: Monoid[L]): ApplicativeHandle[RWST[F, R, L, S, *], E] =
191 +
    new ApplicativeHandle[RWST[F, R, L, S, *], E]
192 +
      with FunctorRaiseMonadPartialOrder[F, RWST[F, R, L, S, *], E] {
193 +
      val applicative: Applicative[RWST[F, R, L, S, *]] =
194 +
        IndexedReaderWriterStateT.catsDataMonadForRWST
195 +
196 +
      val F: FunctorRaise[F, E] = F0
197 +
      val lift: MonadPartialOrder[F, RWST[F, R, L, S, *]] =
198 +
        MonadPartialOrder.monadPartialOrderForRWST
199 +
200 +
      def handleWith[A](fa: RWST[F, R, L, S, A])(
201 +
          f: E => RWST[F, R, L, S, A]): RWST[F, R, L, S, A] =
202 +
        RWST { case (r, s) => F0.handleWith(fa.run(r, s))(e => f(e).run(r, s)) }
203 +
    }
204 +
}
205 +
206 +
object ApplicativeHandle extends ApplicativeHandleInstances {
207 +
  def apply[F[_], E](implicit ev: ApplicativeHandle[F, E]): ApplicativeHandle[F, E] = ev
34 208
}

@@ -1,50 +1,74 @@
Loading
1 1
package cats
2 2
package mtl
3 3
4 +
import cats.data.WriterT
5 +
4 6
/**
5 7
  * `FunctorTell[F, L]` is the ability to "log" values `L` inside a context `F[_]`, as an effect.
6 8
  *
7 -
  * `FunctorTell` has no external laws.
9 +
 * `FunctorTell` has no external laws.
8 10
  *
9 -
  * `FunctorTell` has one internal law:
11 +
 * `FunctorTell` has one internal law:
10 12
  * {{{
11 13
  * def writerIsTellAndMap(a: A, l: L) = {
12 14
  *   (tell(l) as a) <-> writer(a, l)
13 15
  * }
14 16
  *
15 -
  * def tupleIsWriterFlipped(a: A, l: L) = {
17 +
 * def tupleIsWriterFlipped(a: A, l: L) = {
16 18
  *   writer(a, l) <-> tuple((l, a))
17 19
  * }
18 20
  * }}}
19 21
  */
20 22
trait FunctorTell[F[_], L] extends Serializable {
21 -
  val functor: Functor[F]
23 +
  def functor: Functor[F]
22 24
23 25
  def tell(l: L): F[Unit]
24 26
25 -
  def writer[A](a: A, l: L): F[A]
27 +
  def writer[A](a: A, l: L): F[A] = functor.as(tell(l), a)
26 28
27 -
  def tuple[A](ta: (L, A)): F[A]
29 +
  def tuple[A](ta: (L, A)): F[A] = writer(ta._2, ta._1)
28 30
}
29 31
30 -
object FunctorTell {
31 -
  def apply[F[_], L](implicit tell: FunctorTell[F, L]): FunctorTell[F, L] = tell
32 -
  def tell[F[_], L](l: L)(implicit tell: FunctorTell[F, L]): F[Unit] = tell.tell(l)
32 +
private[mtl] trait FunctorTellMonadPartialOrder[F[_], G[_], L] extends FunctorTell[G, L] {
33 +
  val lift: MonadPartialOrder[F, G]
34 +
  val F: FunctorTell[F, L]
33 35
34 -
  def tellF[F[_]]: tellFPartiallyApplied[F] = new tellFPartiallyApplied[F]
36 +
  override val functor = lift.monadG
37 +
  override def tell(l: L) = lift(F.tell(l))
38 +
}
35 39
36 -
  final private[mtl] class tellFPartiallyApplied[F[_]](val dummy: Boolean = false) extends AnyVal {
37 -
    @inline def apply[L](l: L)(implicit tell: FunctorTell[F, L]): F[Unit] = {
38 -
      tell.tell(l)
39 -
    }
40 -
  }
40 +
private[mtl] trait LowPriorityFunctorTellInstances {
41 41
42 -
  abstract class FunctorTellTemplate[F[_], L](implicit override val functor: Functor[F]) extends FunctorTell[F, L]
42 +
  implicit def functorTellForPartialOrder[F[_], G[_], L](
43 +
      implicit lift0: MonadPartialOrder[F, G],
44 +
      F0: FunctorTell[F, L]): FunctorTell[G, L] =
45 +
    new FunctorTellMonadPartialOrder[F, G, L] {
46 +
      val lift: MonadPartialOrder[F, G] = lift0
47 +
      val F: FunctorTell[F, L] = F0
48 +
    }
43 49
}
44 50
45 -
trait DefaultFunctorTell[F[_], L] extends FunctorTell[F, L] {
46 -
  def writer[A](a: A, l: L): F[A] = functor.as(tell(l), a)
51 +
private[mtl] trait FunctorTellInstances
52 +
    extends LowPriorityFunctorTellInstances
53 +
    with LowPriorityFunctorTellInstancesCompat {
47 54
48 -
  def tuple[A](ta: (L, A)): F[A] = writer(ta._2, ta._1)
55 +
  implicit def functorTellForWriterT[F[_]: Applicative, L: Monoid]
56 +
      : FunctorTell[WriterT[F, L, *], L] =
57 +
    new FunctorTell[WriterT[F, L, *], L] {
58 +
      val functor = Functor[WriterT[F, L, *]]
59 +
      def tell(l: L) = WriterT.tell[F, L](l)
60 +
    }
49 61
}
50 62
63 +
object FunctorTell extends FunctorTellInstances {
64 +
  def apply[F[_], L](implicit tell: FunctorTell[F, L]): FunctorTell[F, L] = tell
65 +
  def tell[F[_], L](l: L)(implicit tell: FunctorTell[F, L]): F[Unit] = tell.tell(l)
66 +
67 +
  def tellF[F[_]]: tellFPartiallyApplied[F] = new tellFPartiallyApplied[F]
68 +
69 +
  final private[mtl] class tellFPartiallyApplied[F[_]](val dummy: Boolean = false)
70 +
      extends AnyVal {
71 +
    @inline def apply[L](l: L)(implicit tell: FunctorTell[F, L]): F[Unit] =
72 +
      tell.tell(l)
73 +
  }
74 +
}

@@ -12,17 +12,16 @@
Loading
12 12
13 13
  override def laws: ApplicativeLocalLaws[F, E] = ApplicativeLocalLaws[F, E]
14 14
15 -
  def applicativeLocal[A: Arbitrary, B](implicit
16 -
                                        ArbFA: Arbitrary[F[A]],
17 -
                                        ArbFAB: Arbitrary[F[A => B]],
18 -
                                        ArbEE: Arbitrary[E => E],
19 -
                                        ArbE: Arbitrary[E],
20 -
                                        CogenA: Cogen[A],
21 -
                                        CogenE: Cogen[E],
22 -
                                        EqFU: Eq[F[E]],
23 -
                                        EqFA: Eq[F[A]],
24 -
                                        EqFB: Eq[F[B]]
25 -
                                       ): RuleSet = {
15 +
  def applicativeLocal[A: Arbitrary, B](
16 +
      implicit ArbFA: Arbitrary[F[A]],
17 +
      ArbFAB: Arbitrary[F[A => B]],
18 +
      ArbEE: Arbitrary[E => E],
19 +
      ArbE: Arbitrary[E],
20 +
      CogenA: Cogen[A],
21 +
      CogenE: Cogen[E],
22 +
      EqFU: Eq[F[E]],
23 +
      EqFA: Eq[F[A]],
24 +
      EqFB: Eq[F[B]]): RuleSet = {
26 25
    new DefaultRuleSet(
27 26
      name = "applicativeLocal",
28 27
      parent = Some(applicativeAsk[A]),
@@ -36,7 +35,8 @@
Loading
36 35
}
37 36
38 37
object ApplicativeLocalTests {
39 -
  def apply[F[_], E](implicit instance0: ApplicativeLocal[F, E]): ApplicativeLocalTests[F, E] = {
38 +
  def apply[F[_], E](
39 +
      implicit instance0: ApplicativeLocal[F, E]): ApplicativeLocalTests[F, E] = {
40 40
    new ApplicativeLocalTests[F, E] {
41 41
      override lazy val localInstance: ApplicativeLocal[F, E] = instance0
42 42
      override lazy val askInstance: ApplicativeAsk[F, E] = instance0
Files Coverage
core/src/main/scala/cats/mtl 65.29%
laws/src/main/scala/cats/mtl/laws 90.67%
Project Totals (36 files) 72.71%
706.2
TRAVIS_JDK_VERSION=openjdk8
TRAVIS_OS_NAME=linux
706.1
TRAVIS_JDK_VERSION=openjdk8
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading