PageRenderTime 91ms CodeModel.GetById 56ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/main/scala/scalaz/WriterT.scala

http://github.com/scalaz/scalaz
Scala | 480 lines | 373 code | 102 blank | 5 comment | 0 complexity | 46166bc25e9a0d0fb490dd1d97557f2b MD5 | raw file
  1package scalaz
  2
  3import Id._
  4
  5final case class WriterT[W, F[_], A](run: F[(W, A)]) { self =>
  6  import WriterT._
  7
  8  def off: UnwriterT[F, W, A] =
  9    UnwriterT(run)
 10
 11  /** alias for `off` */
 12  def unary_- : UnwriterT[F, W, A] =
 13    UnwriterT(run)
 14
 15  def mapValue[X, B](f: ((W, A)) => (X, B))(implicit F: Functor[F]): WriterT[X, F, B] =
 16    writerT(F.map(run)(f))
 17
 18  def mapWritten[X](f: W => X)(implicit F: Functor[F]): WriterT[X, F, A] =
 19    mapValue(wa => (f(wa._1), wa._2))
 20
 21  def mapT[W2, G[_], B](f: F[(W, A)] => G[(W2, B)]): WriterT[W2, G, B] =
 22    WriterT(f(run))
 23
 24  def written(implicit F: Functor[F]): F[W] =
 25    F.map(run)(_._1)
 26
 27  def value(implicit F: Functor[F]): F[A] =
 28    F.map(run)(_._2)
 29
 30  def swap(implicit F: Functor[F]): WriterT[A, F, W] =
 31    mapValue(wa => (wa._2, wa._1))
 32
 33  def :++>(w: => W)(implicit F: Functor[F], W: Semigroup[W]): WriterT[W, F, A] =
 34    mapWritten(W.append(_, w))
 35
 36  def :++>>(f: A => W)(implicit F: Functor[F], W: Semigroup[W]): WriterT[W, F, A] =
 37    mapValue(wa => (W.append(wa._1, f(wa._2)), wa._2))
 38
 39  def <++:(w: W)(implicit F: Functor[F], W: Semigroup[W]): WriterT[W, F, A] =
 40    mapWritten(W.append(w, _))
 41
 42  def <<++:(f: A => W)(implicit F: Functor[F], s: Semigroup[W]): WriterT[W, F, A] =
 43    mapValue(wa => (s.append(f(wa._2), wa._1), wa._2))
 44
 45  def reset(implicit Z: Monoid[W], F: Functor[F]): WriterT[W, F, A] =
 46    mapWritten(_ => Z.zero)
 47
 48  def map[B](f: A => B)(implicit F: Functor[F]): WriterT[W, F, B] =
 49    writerT(F.map(run)(wa => (wa._1, f(wa._2))))
 50
 51  def mapF[B](f: A => F[B])(implicit F: Bind[F]): WriterT[W, F, B] = writerT {
 52    F.bind(run)(wa => F.map(f(wa._2))(a => (wa._1, a)))
 53  }
 54
 55  def ap[B](f: => WriterT[W, F, A => B])(implicit F: Apply[F], W: Semigroup[W]): WriterT[W, F, B] = writerT {
 56    F.apply2(f.run, run) {
 57      case ((w1, fab), (w2, a)) => (W.append(w1, w2), fab(a))
 58    }
 59  }
 60
 61  def flatMap[B](f: A => WriterT[W, F, B])(implicit F: Bind[F], s: Semigroup[W]): WriterT[W, F, B] =
 62    flatMapF(f.andThen(_.run))
 63
 64  def flatMapF[B](f: A => F[(W, B)])(implicit F: Bind[F], s: Semigroup[W]): WriterT[W, F, B] =
 65    writerT(F.bind(run){wa =>
 66      val z = f(wa._2)
 67      F.map(z)(wb => (s.append(wa._1, wb._1), wb._2))
 68    })
 69
 70  def traverse[G[_], B](f: A => G[B])(implicit G: Applicative[G], F: Traverse[F]): G[WriterT[W, F, B]] = {
 71    G.map(F.traverse(run){
 72      case (w, a) => G.map(f(a))(b => (w, b))
 73    })(WriterT(_))
 74  }
 75
 76  def foldRight[B](z: => B)(f: (A, => B) => B)(implicit F: Foldable[F]): B =
 77    F.foldr(run, z) { a => b =>
 78      f(a._2, b)
 79    }
 80
 81  def bimap[C, D](f: W => C, g: A => D)(implicit F: Functor[F]): WriterT[C, F, D] =
 82    writerT[F, C, D](F.map(run)({
 83      case (a, b) => (f(a), g(b))
 84    }))
 85
 86  def leftMap[C](f: W => C)(implicit F: Functor[F]): WriterT[C, F, A] =
 87    bimap(f, identity)
 88
 89  def bitraverse[G[_], C, D](f: W => G[C], g: A => G[D])(implicit G: Applicative[G], F: Traverse[F]): G[WriterT[C, F, D]] =
 90    G.map(F.traverse[G, (W, A), (C, D)](run) {
 91      case (a, b) => G.tuple2(f(a), g(b))
 92    })(writerT(_))
 93
 94  def rwst[R, S](implicit F: Functor[F]): ReaderWriterStateT[R, W, S, F, A] = ReaderWriterStateT(
 95    (r, s) => F.map(self.run) {
 96      case (w, a) => (w, a, s)
 97    }
 98  )
 99
100  def wpoint[G[_]](implicit F: Functor[F], P: Applicative[G]): WriterT[G[W], F, A] =
101    writerT(F.map(self.run) {
102      case (w, a) => (P.point(w), a)
103    })
104
105  def colocal[X](f: W => X)(implicit F: Functor[F]): WriterT[X, F, A] = mapWritten(f)
106}
107
108object WriterT extends WriterTInstances with WriterTFunctions
109
110sealed abstract class WriterTInstances16 {
111  implicit def writerTDivisible[F[_], W, A](implicit F0: Divisible[F]): Divisible[WriterT[W, F, *]] =
112    new WriterTDivisible[F, W] {
113      implicit def F: Divisible[F] = F0
114    }
115}
116
117sealed abstract class WriterTInstances15 extends WriterTInstances16 {
118  implicit def writerTMonoid[W, F[_], A](implicit M: Monoid[F[(W,A)]]): Monoid[WriterT[W, F, A]] =
119    new Monoid[WriterT[W, F, A]] {
120      def zero = WriterT(M.zero)
121      def append(a: WriterT[W, F, A], b: => WriterT[W, F, A]) = WriterT(M.append(a.run, b.run))
122    }
123
124  implicit def writerTPlus[W, F[_]](implicit F0: Plus[F]): Plus[WriterT[W, F, *]] =
125    new WriterTPlus[F, W] {
126      def F = F0
127    }
128
129  implicit def writerTDecidable[F[_], W, A](implicit F0: Decidable[F]): Decidable[WriterT[W, F, *]] =
130    new WriterTDecidable[F, W] {
131      implicit def F: Decidable[F] = F0
132    }
133}
134
135sealed abstract class WriterTInstances14 extends WriterTInstances15 {
136  implicit def writerFunctor[W]: Functor[Writer[W, *]] =
137    new WriterTFunctor[Id, W] {
138      implicit def F = idInstance
139    }
140
141  implicit def writerTPlusEmpty[W, F[_]](implicit F0: PlusEmpty[F]): PlusEmpty[WriterT[W, F, *]] =
142    new WriterTPlusEmpty[F, W] {
143      def F = F0
144    }
145}
146sealed abstract class WriterTInstances13 extends WriterTInstances14 {
147  implicit def writerTFunctor[W, F[_]](implicit F0: Functor[F]): Functor[WriterT[W, F, *]] =
148    new WriterTFunctor[F, W] {
149      implicit def F = F0
150    }
151}
152
153sealed abstract class WriterTInstances12 extends WriterTInstances13 {
154  implicit def writerBind[W](implicit W0: Semigroup[W]): Bind[Writer[W, *]] =
155    new WriterTBind[Id, W] {
156      implicit def F = idInstance
157      implicit def W = W0
158    }
159}
160
161sealed abstract class WriterTInstances11 extends WriterTInstances12 {
162  implicit def writerTBindRec[W, F[_]](implicit W0: Semigroup[W], F0: BindRec[F], F1: Applicative[F]): BindRec[WriterT[W, F, *]] =
163    new WriterTBindRec[F, W] {
164      implicit def F = F0
165      implicit def A = F1
166      implicit def W = W0
167    }
168}
169
170sealed abstract class WriterTInstances10 extends WriterTInstances11 {
171  implicit def writerTApply[W, F[_]](implicit W0: Semigroup[W], F0: Apply[F]): Apply[WriterT[W, F, *]] =
172    new WriterTApply[F, W] {
173      implicit def F = F0
174      implicit def W = W0
175    }
176}
177
178sealed abstract class WriterTInstances9 extends WriterTInstances10 {
179  implicit def writerTBind[W, F[_]](implicit W0: Semigroup[W], F0: Bind[F]): Bind[WriterT[W, F, *]] =
180    new WriterTBind[F, W] {
181      implicit def F = F0
182      implicit def W = W0
183    }
184}
185
186sealed abstract class WriterTInstances8 extends WriterTInstances9 {
187  implicit def writerTApplicative[W, F[_]](implicit W0: Monoid[W], F0: Applicative[F]): Applicative[WriterT[W, F, *]] =
188    new WriterTApplicative[F, W] {
189      implicit def F = F0
190      implicit def W = W0
191    }
192}
193
194sealed abstract class WriterTInstances7 extends WriterTInstances8 {
195  implicit def writerBindRec[W](implicit W0: Semigroup[W]): BindRec[Writer[W, *]] =
196    new WriterTBindRec[Id, W] {
197      implicit def F = idInstance
198      implicit def A = idInstance
199      implicit def W = W0
200    }
201}
202
203sealed abstract class WriterTInstances6 extends WriterTInstances7 {
204  implicit def writerMonad[W](implicit W0: Monoid[W]): Monad[Writer[W, *]] =
205    new WriterTMonad[Id, W] {
206      implicit def F = idInstance
207      implicit def W = W0
208    }
209}
210
211sealed abstract class WriterTInstance5 extends WriterTInstances6 {
212  implicit def writerTMonad[W, F[_]](implicit W0: Monoid[W], F0: Monad[F]): Monad[WriterT[W, F, *]] =
213    new WriterTMonad[F, W] {
214      implicit def F = F0
215      implicit def W = W0
216    }
217}
218
219sealed abstract class WriterTInstances4 extends WriterTInstance5 {
220  implicit def writerFoldable[W]: Foldable[Writer[W, *]] =
221    new WriterTFoldable[Id, W] {
222      implicit def F = idInstance
223    }
224  implicit def writerTEqual[F[_], W, A](implicit E: Equal[F[(W, A)]]): Equal[WriterT[W, F, A]] = E.contramap((_: WriterT[W, F, A]).run)
225
226  implicit def writerTMonadError[F[_], E, W](implicit F0: MonadError[F, E], W0: Monoid[W]): MonadError[WriterT[W, F, *], E] =
227    new WriterTMonadError[F, E, W] {
228      override def F = F0
229      override def W = W0
230    }
231}
232
233sealed abstract class WriterTInstances3 extends WriterTInstances4 {
234  implicit def writerTBifunctor[F[_]](implicit F0: Functor[F]): Bifunctor[WriterT[*, F, *]] =
235    new WriterTBifunctor[F] {
236      implicit def F = F0
237    }
238  implicit def writerTFoldable[W, F[_]](implicit F0: Foldable[F]): Foldable[WriterT[W, F, *]] =
239    new WriterTFoldable[F, W] {
240      implicit def F = F0
241    }
242  implicit def writerEqual[W, A](implicit E: Equal[(W, A)]): Equal[Writer[W, A]] = E.contramap((_: Writer[W, A]).run)
243
244  implicit def writerTMonadPlus[W, F[_]](implicit W0: Monoid[W], F0: MonadPlus[F]): MonadPlus[WriterT[W, F, *]] =
245    new WriterTMonadPlus[F, W] {
246      def F = F0
247      def W = W0
248    }
249}
250
251sealed abstract class WriterTInstances2 extends WriterTInstances3 {
252  implicit def writerComonad[W]: Comonad[Writer[W, *]] =
253    new WriterComonad[W] {
254      implicit def F = implicitly
255    }
256}
257
258sealed abstract class WriterTInstances1 extends WriterTInstances2 {
259  implicit val writerBitraverse: Bitraverse[Writer] =
260    new WriterTBitraverse[Id] {
261      implicit def F = idInstance
262    }
263  implicit def writerTTraverse[W, F[_]](implicit F0: Traverse[F]): Traverse[WriterT[W, F, *]] =
264    new WriterTTraverse[F, W] {
265      implicit def F = F0
266    }
267}
268
269sealed abstract class WriterTInstances0 extends WriterTInstances1 {
270  implicit def writerTBitraverse[F[_]](implicit F0: Traverse[F]): Bitraverse[WriterT[*, F, *]] =
271    new WriterTBitraverse[F] {
272      implicit def F = F0
273    }
274  implicit def writerTraverse[W]: Traverse[Writer[W, *]] =
275    new WriterTTraverse[Id, W] {
276      implicit def F = idInstance
277    }
278}
279
280sealed abstract class WriterTInstances extends WriterTInstances0 {
281  implicit def writerTMonadListen[W, F[_]](implicit F0: Monad[F], W0: Monoid[W]): MonadListen[WriterT[W, F, *], W] =
282    new WriterTMonadListen[F, W] {
283      implicit def F = F0
284      implicit def W = W0
285    }
286
287  implicit def writerTHoist[W](implicit W0: Monoid[W]): Hoist[λ[(α[_], β) => WriterT[W, α, β]]] =
288    new WriterTHoist[W] {
289      implicit def W = W0
290    }
291
292  implicit def writerTShow[F[_], W, A](implicit F0: Show[F[(W, A)]]): Show[WriterT[W, F, A]] =
293    Contravariant[Show].contramap(F0)(_.run)
294}
295
296trait WriterTFunctions {
297  def writerT[F[_], W, A](v: F[(W, A)]): WriterT[W, F, A] = WriterT(v)
298
299  def writerTU[FAB, AB, A0, B0](fab: FAB)(implicit
300                                          u1: Unapply[Functor, FAB]{type A = AB},
301                                          @deprecated("scala/bug#5075", "") u2: Unapply2[Bifunctor, AB]{type A = A0; type B = B0},
302                                          l: AB === (A0, B0)
303  ): WriterT[A0, u1.M, B0] = WriterT(l.subst[u1.M](u1(fab)))
304
305  def writer[W, A](v: (W, A)): Writer[W, A] =
306    writerT[Id, W, A](v)
307
308  def tell[W](w: W): Writer[W, Unit] = writer((w, ()))
309
310  def put[F[_], W, A](value: F[A])(w: W)(implicit F: Functor[F]): WriterT[W, F, A] =
311    WriterT(F.map(value)(a => (w, a)))
312
313  /** Puts the written value that is produced by applying the given function into a writer transformer and associates with `value` */
314  def putWith[F[_], W, A](value: F[A])(w: A => W)(implicit F: Functor[F]): WriterT[W, F, A] =
315    WriterT(F.map(value)(a => (w(a), a)))
316
317}
318
319//
320// Type class implementation traits
321//
322import WriterT.writerT
323
324private trait WriterTDivisible[F[_], W] extends Divisible[WriterT[W, F, *]] {
325  implicit def F: Divisible[F]
326
327  override def conquer[Z]: WriterT[W, F, Z] = WriterT(F.conquer)
328
329  override def divide2[A1, A2, Z](a1: => WriterT[W, F, A1], a2: => WriterT[W, F, A2])(f: Z => (A1, A2)): WriterT[W, F, Z] =
330    WriterT(
331      F.divide2[(W, A1), (W, A2), (W, Z)](a1.run, a2.run) ( (z: (W, Z)) =>
332        (z._1, f(z._2)) match {
333          case (w, (l, r)) => ((w, l), (w, r))
334        }
335      )
336    )
337}
338
339private trait WriterTDecidable[F[_], W] extends Decidable[WriterT[W, F, *]] with WriterTDivisible[F, W] {
340  implicit def F: Decidable[F]
341
342  override def choose2[Z, A1, A2](a1: => WriterT[W, F, A1], a2: => WriterT[W, F, A2])(f: Z => A1 \/ A2): WriterT[W, F, Z] =
343    WriterT(
344      F.choose2[(W, Z), (W, A1), (W, A2)](a1.run, a2.run)( (z: (W, Z)) =>
345        f(z._2).fold(l => -\/((z._1, l)), r => \/-((z._1, r)))
346      )
347    )
348}
349
350private trait WriterTPlus[F[_], W] extends Plus[WriterT[W, F, *]] {
351  def F: Plus[F]
352  override final def plus[A](a: WriterT[W, F, A], b: => WriterT[W, F, A]) =
353    WriterT(F.plus(a.run, b.run))
354}
355
356private trait WriterTPlusEmpty[F[_], W] extends PlusEmpty[WriterT[W, F, *]] with WriterTPlus[F, W] {
357  def F: PlusEmpty[F]
358
359  override final def empty[A] = WriterT(F.empty)
360}
361
362private trait WriterTFunctor[F[_], W] extends Functor[WriterT[W, F, *]] {
363  implicit def F: Functor[F]
364
365  override def map[A, B](fa: WriterT[W, F, A])(f: A => B) = fa map f
366}
367
368private trait WriterTApply[F[_], W] extends Apply[WriterT[W, F, *]] with WriterTFunctor[F, W] {
369  implicit def F: Apply[F]
370  implicit def W: Semigroup[W]
371
372  override def ap[A, B](fa: => WriterT[W, F, A])(f: => WriterT[W, F, A => B]) = fa ap f
373}
374
375private trait WriterTApplicative[F[_], W] extends Applicative[WriterT[W, F, *]] with WriterTApply[F, W] {
376  implicit def F: Applicative[F]
377  implicit def W: Monoid[W]
378  def point[A](a: => A) = writerT(F.point((W.zero, a)))
379}
380
381private trait WriterTBind[F[_], W] extends Bind[WriterT[W, F, *]] with WriterTApply[F, W] {
382  implicit def F: Bind[F]
383
384  override final def bind[A, B](fa: WriterT[W, F, A])(f: A => WriterT[W, F, B]) = fa flatMap f
385}
386
387private trait WriterTBindRec[F[_], W] extends BindRec[WriterT[W, F, *]] with WriterTBind[F, W] {
388  implicit def F: BindRec[F]
389  implicit def A: Applicative[F]
390
391  def tailrecM[A, B](a: A)(f: A => WriterT[W, F, A \/ B]): WriterT[W, F, B] = {
392    def go(t: (W, A)): F[(W, A) \/ (W, B)] =
393      F.map(f(t._2).run) {
394        case (w0, e) =>
395          val w1 = W.append(t._1, w0)
396          e.bimap((w1, _), (w1, _))
397      }
398
399    WriterT(F.bind(f(a).run) {
400      case (w, -\/(a0)) => F.tailrecM((w, a0))(go)
401      case (w, \/-(b)) => A.point((w, b))
402    })
403  }
404}
405
406private trait WriterTMonad[F[_], W] extends Monad[WriterT[W, F, *]] with WriterTApplicative[F, W] with WriterTBind[F, W] {
407  implicit def F: Monad[F]
408}
409
410private trait WriterTMonadPlus[F[_], W] extends MonadPlus[WriterT[W, F, *]] with WriterTMonad[F, W] with WriterTPlusEmpty[F, W] {
411  def F: MonadPlus[F]
412}
413
414private trait WriterTMonadError[F[_], E, W] extends MonadError[WriterT[W, F, *], E] with WriterTMonad[F, W] {
415  implicit def F: MonadError[F, E]
416
417  override def handleError[A](fa: WriterT[W, F, A])(f: E => WriterT[W, F, A]) =
418    WriterT[W, F, A](F.handleError(fa.run)(f(_).run))
419
420  override def raiseError[A](e: E) =
421    WriterT[W, F, A](F.raiseError[(W, A)](e))
422}
423
424private trait WriterTFoldable[F[_], W] extends Foldable.FromFoldr[WriterT[W, F, *]] {
425  implicit def F: Foldable[F]
426
427  override def foldRight[A, B](fa: WriterT[W, F, A], z: => B)(f: (A, => B) => B) = fa.foldRight(z)(f)
428}
429
430private trait WriterTTraverse[F[_], W] extends Traverse[WriterT[W, F, *]] with WriterTFoldable[F, W] {
431  implicit def F: Traverse[F]
432
433  def traverseImpl[G[_]: Applicative, A, B](fa: WriterT[W, F, A])(f: A => G[B]) = fa traverse f
434}
435
436private trait WriterTBifunctor[F[_]] extends Bifunctor[WriterT[*, F, *]] {
437  implicit def F: Functor[F]
438
439  override def bimap[A, B, C, D](fab: WriterT[A, F, B])(f: A => C, g: B => D) =
440    fab.bimap(f, g)
441}
442
443private trait WriterTBitraverse[F[_]] extends Bitraverse[WriterT[*, F, *]] with WriterTBifunctor[F] {
444  implicit def F: Traverse[F]
445
446  def bitraverseImpl[G[_]: Applicative, A, B, C, D](fab: WriterT[A, F, B])(f: A => G[C], g: B => G[D]) =
447    fab.bitraverse(f, g)
448}
449
450private trait WriterComonad[W] extends Comonad[Writer[W, *]] with WriterTFunctor[Id, W] {
451  def copoint[A](p: Writer[W, A]): A = p.value
452
453  override def cojoin[A](fa: Writer[W, A]): Writer[W, Writer[W, A]] =
454    Writer(fa.written, fa)
455
456  override def cobind[A, B](fa: Writer[W, A])(f: (Writer[W, A]) => B): Writer[W, B] =
457    Writer(fa.written, f(fa))
458}
459
460private trait WriterTHoist[W] extends Hoist[λ[(α[_], β) => WriterT[W, α, β]]] {
461  def liftM[M[_], B](mb: M[B])(implicit M: Monad[M]): WriterT[W, M, B] =
462    WriterT(M.map(mb)((W.zero, _)))
463
464  implicit def W: Monoid[W]
465
466  implicit def apply[M[_]: Monad]: Monad[WriterT[W, M, *]] = WriterT.writerTMonad
467
468  def hoist[M[_]: Monad, N[_]](f: M ~> N) =
469    λ[WriterT[W, M, *] ~> WriterT[W, N, *]](_ mapT f.apply)
470}
471
472private trait WriterTMonadListen[F[_], W] extends MonadListen[WriterT[W, F, *], W] with WriterTMonad[F, W] {
473  implicit def F: Monad[F]
474  implicit def W: Monoid[W]
475
476  def writer[A](w: W, v: A): WriterT[W, F, A] = WriterT.writerT(F.point((w, v)))
477
478  def listen[A](fa: WriterT[W, F, A]): WriterT[W, F, (A, W)] =
479    WriterT(F.map(fa.run){ case (w, a) => (w, (a, w)) })
480}