@@ -1,6 +1,6 @@
package scalaz

sealed trait IndexedContsT[W[+_], M[+_], +R, -O, +A] {
sealed trait IndexedContsT[W[_], M[_], R, O, A] {
def run(wamo: W[A => M[O]]): M[R]

def apply(wamo: W[A => M[O]]): M[R] = run(wamo)
@@ -43,17 +43,17 @@ sealed trait IndexedContsT[W[+_], M[+_], +R, -O, +A] {
}

object IndexedContsT extends IndexedContsTFunctions with IndexedContsTInstances {
def apply[W[+_], M[+_], R, O, A](f: W[A => M[O]] => M[R]): IndexedContsT[W, M, R, O, A] = new IndexedContsT[W, M, R, O, A] {
def apply[W[_], M[_], R, O, A](f: W[A => M[O]] => M[R]): IndexedContsT[W, M, R, O, A] = new IndexedContsT[W, M, R, O, A] {
def run(wamo: W[A => M[O]]): M[R] = f(wamo)
}
}

trait IndexedContsTFunctions {
def point[W[+_], M[+_], R, A](a: => A)(implicit W: Comonad[W]): ContsT[W, M, R, A] = ContsT { k => W.copoint(k)(a) }
def point[W[_], M[_], R, A](a: => A)(implicit W: Comonad[W]): ContsT[W, M, R, A] = ContsT { k => W.copoint(k)(a) }

def liftM[W[+_], M[+_], R, O, A](a: => M[A])(implicit W: Comonad[W], M: Bind[M]): ContsT[W, M, R, A] = ContsT { k => M.bind(a)(W.copoint(k)) }
def liftM[W[_], M[_], R, O, A](a: => M[A])(implicit W: Comonad[W], M: Bind[M]): ContsT[W, M, R, A] = ContsT { k => M.bind(a)(W.copoint(k)) }

def shift[W[+_], M[+_], I, R, J, O, A](f: (A => IndexedContsT[W, M, I, I, O]) => IndexedContsT[W, M, R, J, J])(implicit W: Comonad[W], WA: Applicative[W], M: Monad[M]): IndexedContsT[W, M, R, O, A] =
def shift[W[_], M[_], I, R, J, O, A](f: (A => IndexedContsT[W, M, I, I, O]) => IndexedContsT[W, M, R, J, J])(implicit W: Comonad[W], WA: Applicative[W], M: Monad[M]): IndexedContsT[W, M, R, O, A] =
IndexedContsT { k0 =>
(f { a =>
IndexedContsT { k1 =>
@@ -62,82 +62,82 @@ trait IndexedContsTFunctions {
}).run_
}

def reset[W[+_], M[+_], R, O, A](v: IndexedContsT[W, M, A, O, O])(implicit W: Comonad[W], WA: Applicative[W], M: Monad[M]): IndexedContsT[W, M, R, R, A] =
def reset[W[_], M[_], R, O, A](v: IndexedContsT[W, M, A, O, O])(implicit W: Comonad[W], WA: Applicative[W], M: Monad[M]): IndexedContsT[W, M, R, R, A] =
IndexedContsT { k =>
M.bind(v.run_)(W.copoint(k))
}

def callCC[W[+_], M[+_], R, O, A](f: (A => IndexedContsT[W, M, Any, O, Nothing]) => IndexedContsT[W, M, R, O, A])(implicit W: Comonad[W]): IndexedContsT[W, M, R, O, A] =
def callCC[W[_], M[_], R, O, A, B](f: (A => IndexedContsT[W, M, O, O, B]) => IndexedContsT[W, M, R, O, A])(implicit W: Comonad[W]): IndexedContsT[W, M, R, O, A] =
IndexedContsT { k =>
(f { a =>
IndexedContsT { u =>
IndexedContsT[W, M, O, O, B] { u =>
W.copoint(k)(a)
}
}).run(k)
}
}

trait IndexedContsTInstances0 {
implicit def IndexedContsTFunctorRight[W[+_], M[+_], R, O](implicit W0: Functor[W]): Functor[({type f[+a]=IndexedContsT[W, M, R, O, a]})#f] = new IndexedContsTFunctorRight[W, M, R, O] {
implicit def IndexedContsTFunctorRight[W[_], M[_], R, O](implicit W0: Functor[W]): Functor[({type f[a]=IndexedContsT[W, M, R, O, a]})#f] = new IndexedContsTFunctorRight[W, M, R, O] {
implicit val W: Functor[W] = W0
}
}

trait IndexedContsTInstances extends IndexedContsTInstances0 {
implicit def IndexedContsTFunctorLeft[W[+_], M[+_], O, A](implicit M0: Functor[M]): Functor[({type f[+r]=IndexedContsT[W, M, r, O, A]})#f] = new IndexedContsTFunctorLeft[W, M, O, A] {
implicit def IndexedContsTFunctorLeft[W[_], M[_], O, A](implicit M0: Functor[M]): Functor[({type f[r]=IndexedContsT[W, M, r, O, A]})#f] = new IndexedContsTFunctorLeft[W, M, O, A] {
implicit val M: Functor[M] = M0
}

implicit def IndexedContsTContravariant[W[+_], M[+_], R, A](implicit W0: Functor[W], M0: Functor[M]): Contravariant[({type f[-o]=IndexedContsT[W, M, R, o, A]})#f] = new IndexedContsTContravariant[W, M, R, A] {
implicit def IndexedContsTContravariant[W[_], M[_], R, A](implicit W0: Functor[W], M0: Functor[M]): Contravariant[({type f[o]=IndexedContsT[W, M, R, o, A]})#f] = new IndexedContsTContravariant[W, M, R, A] {
implicit val W: Functor[W] = W0
implicit val M: Functor[M] = M0
}

implicit def IndexedContsTBifunctor[W[+_], M[+_], O](implicit W0: Functor[W], M0: Functor[M]): Bifunctor[({type f[+r, +a]=IndexedContsT[W, M, r, O, a]})#f] = new IndexedContsTBifunctor[W, M, O] {
implicit def IndexedContsTBifunctor[W[_], M[_], O](implicit W0: Functor[W], M0: Functor[M]): Bifunctor[({type f[r, a]=IndexedContsT[W, M, r, O, a]})#f] = new IndexedContsTBifunctor[W, M, O] {
implicit val W: Functor[W] = W0
implicit val M: Functor[M] = M0
}

implicit def ContsTMonad[W[+_], M[+_], R](implicit W0: Comonad[W]): Monad[({type f[+a]=ContsT[W, M, R, a]})#f] = new ContsTMonad[W, M, R] {
implicit def ContsTMonad[W[_], M[_], R](implicit W0: Comonad[W]): Monad[({type f[a]=ContsT[W, M, R, a]})#f] = new ContsTMonad[W, M, R] {
implicit val W: Comonad[W] = W0
}
}

private[scalaz] sealed trait IndexedContsTFunctorLeft[W[+_], M[+_], O, A0] extends Functor[({type f[+r]=IndexedContsT[W, M, r, O, A0]})#f] {
private[scalaz] sealed trait IndexedContsTFunctorLeft[W[_], M[_], O, A0] extends Functor[({type f[r]=IndexedContsT[W, M, r, O, A0]})#f] {
implicit val M: Functor[M]

def map[A, B](fa: IndexedContsT[W, M, A, O, A0])(f: A => B): IndexedContsT[W, M, B, O, A0] = fa.imap(f)
}

private[scalaz] sealed trait IndexedContsTFunctorRight[W[+_], M[+_], R, O] extends Functor[({type f[+a]=IndexedContsT[W, M, R, O, a]})#f] {
private[scalaz] sealed trait IndexedContsTFunctorRight[W[_], M[_], R, O] extends Functor[({type f[a]=IndexedContsT[W, M, R, O, a]})#f] {
implicit val W: Functor[W]

override def map[A, B](fa: IndexedContsT[W, M, R, O, A])(f: A => B): IndexedContsT[W, M, R, O, B] = fa.map(f)
}

private[scalaz] sealed trait IndexedContsTContravariant[W[+_], M[+_], R, A0] extends Contravariant[({type f[-o]=IndexedContsT[W, M, R, o, A0]})#f] {
private[scalaz] sealed trait IndexedContsTContravariant[W[_], M[_], R, A0] extends Contravariant[({type f[o]=IndexedContsT[W, M, R, o, A0]})#f] {
implicit val W: Functor[W]
implicit val M: Functor[M]

def contramap[A, B](fa: IndexedContsT[W, M, R, A, A0])(f: B => A): IndexedContsT[W, M, R, B, A0] = fa.contramap(f)
}

private[scalaz] sealed trait IndexedContsTBifunctor[W[+_], M[+_], O] extends Bifunctor[({type f[+r, +a]=IndexedContsT[W, M, r, O, a]})#f] {
private[scalaz] sealed trait IndexedContsTBifunctor[W[_], M[_], O] extends Bifunctor[({type f[r, a]=IndexedContsT[W, M, r, O, a]})#f] {
implicit val W: Functor[W]
implicit val M: Functor[M]

def bimap[A, B, C, D](fab: IndexedContsT[W, M, A, O, B])(f: A => C, g: B => D): IndexedContsT[W, M, C, O, D] = fab.bimap(f, g)

override def leftFunctor[X]: Functor[({type f[+r]=IndexedContsT[W, M, r, O, X]})#f] = IndexedContsT.IndexedContsTFunctorLeft
override def leftFunctor[X]: Functor[({type f[r]=IndexedContsT[W, M, r, O, X]})#f] = IndexedContsT.IndexedContsTFunctorLeft

override def leftMap[A, B, C](fa: IndexedContsT[W, M, A, O, B])(f: A => C): IndexedContsT[W, M, C, O, B] = fa.imap(f)

override def rightFunctor[X]: Functor[({type f[+a]=IndexedContsT[W, M, X, O, a]})#f] = IndexedContsT.IndexedContsTFunctorRight
override def rightFunctor[X]: Functor[({type f[a]=IndexedContsT[W, M, X, O, a]})#f] = IndexedContsT.IndexedContsTFunctorRight

override def rightMap[A, B, D](fa: IndexedContsT[W, M, A, O, B])(f: B => D): IndexedContsT[W, M, A, O, D] = fa.map(f)
}

private[scalaz] sealed trait ContsTMonad[W[+_], M[+_], R] extends Monad[({type f[+a]=ContsT[W, M, R, a]})#f] with IndexedContsTFunctorRight[W, M, R, R] {
private[scalaz] sealed trait ContsTMonad[W[_], M[_], R] extends Monad[({type f[a]=ContsT[W, M, R, a]})#f] with IndexedContsTFunctorRight[W, M, R, R] {
implicit val W: Comonad[W]

def point[A](a: => A): ContsT[W, M, R, A] = IndexedContsT.point(a)
@@ -1,12 +1,11 @@
package scalaz

import Id._
import annotation.unchecked.uncheckedVariance

/**
* Represents a function `A => M[B]`.
*/
sealed trait Kleisli[M[+_], -A, +B] { self =>
sealed trait Kleisli[M[_], A, B] { self =>
def run(a: A): M[B]

import Kleisli._
@@ -29,15 +28,15 @@ sealed trait Kleisli[M[+_], -A, +B] { self =>

def composeK[C](k: C => M[A])(implicit b: Bind[M]): Kleisli[M, C, B] = this <==< k

def traverse[F[_], AA <: A, BB >: B](f: F[AA])(implicit M: Applicative[M], F: Traverse[F]): M[F[BB]] =
def traverse[F[_]](f: F[A])(implicit M: Applicative[M], F: Traverse[F]): M[F[B]] =
F.traverse(f)(Kleisli.this(_))

def =<<[AA <: A](a: M[AA])(implicit m: Bind[M]): M[B] = m.bind(a)(run _)

def map[C](f: B => C)(implicit M: Functor[M]): Kleisli[M, A, C] =
kleisli(a => M.map(run(a))(f))

def mapK[N[+_], C](f: M[B] => N[C]): Kleisli[N, A, C] =
def mapK[N[_], C](f: M[B] => N[C]): Kleisli[N, A, C] =
kleisli(a => f(run(a)))

def flatMapK[C](f: B => M[C])(implicit M: Bind[M]): Kleisli[M, A, C] =
@@ -46,16 +45,16 @@ sealed trait Kleisli[M[+_], -A, +B] { self =>
def flatMap[C, AA <: A](f: B => Kleisli[M, AA, C])(implicit M: Bind[M]): Kleisli[M, AA, C] =
kleisli((r: AA) => M.bind[B, C](run(r))(((b: B) => f(b).run(r))))

def lift[L[+_]: Applicative]: Kleisli[({type λ[+α]=L[M[α]]})#λ, A, B] = new Kleisli[({type λ[+α]=L[M[α]]})#λ, A, B] {
def lift[L[_]: Applicative]: Kleisli[({type λ[α]=L[M[α]]})#λ, A, B] = new Kleisli[({type λ[α]=L[M[α]]})#λ, A, B] {
def run(a: A) = Applicative[L].point(self(a))
}

import Liskov._
def unlift[N[+_], FF[+_], AA <: A, BB >: B](implicit M: Comonad[N], ev: this.type <~< Kleisli[({type λ[+α] = N[FF[α]]})#λ, AA, BB]): Kleisli[FF, AA, BB] = new Kleisli[FF, AA, BB] {
def unlift[N[_], FF[_], AA <: A, BB >: B](implicit M: Comonad[N], ev: this.type <~< Kleisli[({type λ[α] = N[FF[α]]})#λ, AA, BB]): Kleisli[FF, AA, BB] = new Kleisli[FF, AA, BB] {
def run(a: AA) = Comonad[N].copoint(ev(self) run a)
}

def unliftId[N[+_], AA <: A, BB >: B](implicit M: Comonad[N], ev: this.type <~< Kleisli[({type λ[+α] = N[α]})#λ, AA, BB]): Reader[AA, BB] =
def unliftId[N[_], AA <: A, BB >: B](implicit M: Comonad[N], ev: this.type <~< Kleisli[({type λ[α] = N[α]})#λ, AA, BB]): Reader[AA, BB] =
unlift[N, Id, AA, BB]

def rwst[W, S](implicit M: Functor[M], W: Monoid[W]): ReaderWriterStateT[M, A, W, S, B] = ReaderWriterStateT(
@@ -67,8 +66,8 @@ sealed trait Kleisli[M[+_], -A, +B] { self =>
def state[AA <: A, BB >: B](implicit M: Functor[M]): StateT[M, AA, BB] =
StateT(a => M.map(run(a))((a, _)))

def liftMK[T[_[_], +_]](implicit T: MonadTrans[T], M: Monad[M]): Kleisli[({type l[+a] = T[M, a]})#l, A, B] =
mapK[({type l[+a] = T[M, a]})#l, B](ma => T.liftM(ma))
def liftMK[T[_[_], _]](implicit T: MonadTrans[T], M: Monad[M]): Kleisli[({type l[a] = T[M, a]})#l, A, B] =
mapK[({type l[a] = T[M, a]})#l, B](ma => T.liftM(ma))

def local[AA](f: AA => A): Kleisli[M, AA, B] = kleisli(f andThen run)
}
@@ -77,48 +76,48 @@ sealed trait Kleisli[M[+_], -A, +B] { self =>
// Prioritized Implicits for type class instances
//
trait KleisliInstances8 {
implicit def kleisliFunctor[F[+_], R](implicit F0: Functor[F]): Functor[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliFunctor[F, R] {
implicit def kleisliFunctor[F[_], R](implicit F0: Functor[F]): Functor[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliFunctor[F, R] {
implicit def F: Functor[F] = F0
}
}

trait KleisliInstances7 extends KleisliInstances8 {

implicit def kleisliApply[F[+_], R](implicit F0: Apply[F]): Apply[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliApply[F, R] {
implicit def kleisliApply[F[_], R](implicit F0: Apply[F]): Apply[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliApply[F, R] {
implicit def F: Apply[F] = F0
}

implicit def kleisliDistributive[F[+_], R](implicit F0: Distributive[F]): Distributive[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliDistributive[F, R] {
implicit def kleisliDistributive[F[_], R](implicit F0: Distributive[F]): Distributive[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliDistributive[F, R] {
implicit def F: Distributive[F] = F0
}
}

trait KleisliInstances6 extends KleisliInstances7 {
implicit def kleisliApplicative[F[+_], R](implicit F0: Applicative[F]): Applicative[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliApplicative[F, R] {
implicit def kleisliApplicative[F[_], R](implicit F0: Applicative[F]): Applicative[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliApplicative[F, R] {
implicit def F: Applicative[F] = F0
}
implicit def kleisliPlus[F[+_], A](implicit F0: Plus[F]) = new KleisliPlus[F, A] {
implicit def kleisliPlus[F[_], A](implicit F0: Plus[F]) = new KleisliPlus[F, A] {
implicit def F = F0
}
}

trait KleisliInstances5 extends KleisliInstances6 {
implicit def kleisliApplicativePlus[F[+_], R](implicit F0: ApplicativePlus[F]): ApplicativePlus[({type λ[α] = Kleisli[F, R, α]})#λ] = new ApplicativePlus[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R] with KleisliPlusEmpty[F, R] {
implicit def kleisliApplicativePlus[F[_], R](implicit F0: ApplicativePlus[F]): ApplicativePlus[({type λ[α] = Kleisli[F, R, α]})#λ] = new ApplicativePlus[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R] with KleisliPlusEmpty[F, R] {
implicit def F: ApplicativePlus[F] = F0
}
implicit def kleisliSemigroup[F[+_], A, B](implicit FB0: Semigroup[F[B]]) = new KleisliSemigroup[F, A, B] {
implicit def kleisliSemigroup[F[_], A, B](implicit FB0: Semigroup[F[B]]) = new KleisliSemigroup[F, A, B] {
implicit def FB = FB0
}
}

trait KleisliInstances4 extends KleisliInstances5 {
implicit def kleisliMonadPlus[F[+_], A](implicit F0: MonadPlus[F]) = new KleisliMonadPlus[F, A] {
implicit def kleisliMonadPlus[F[_], A](implicit F0: MonadPlus[F]) = new KleisliMonadPlus[F, A] {
implicit def F = F0
}
}

trait KleisliInstances3 extends KleisliInstances4 {
implicit def kleisliMonadReader[F[+_], R](implicit F0: Monad[F]) = new KleisliMonadReader[F, R] {
implicit def kleisliMonadReader[F[_], R](implicit F0: Monad[F]) = new KleisliMonadReader[F, R] {
implicit def F: Monad[F] = F0
}
}
@@ -137,40 +136,40 @@ trait KleisliInstances0 extends KleisliInstances1 {
}

trait KleisliInstances extends KleisliInstances0 {
implicit def kleisliArrow[F[+_]](implicit F0: Monad[F]) = new KleisliArrow[F] {
implicit def kleisliArrow[F[_]](implicit F0: Monad[F]) = new KleisliArrow[F] {
implicit def F: Monad[F] = F0
}

implicit def kleisliContravariant[F[+_], A] = new KleisliContravariant[F, A] {}
implicit def kleisliContravariant[F[_], A] = new KleisliContravariant[F, A] {}

implicit def kleisliIdMonadReader[R] = kleisliMonadReader[Id, R]

implicit def kleisliMonoid[F[+_], A, B](implicit FB0: Monoid[F[B]]) = new KleisliMonoid[F, A, B] {
implicit def kleisliMonoid[F[_], A, B](implicit FB0: Monoid[F[B]]) = new KleisliMonoid[F, A, B] {
implicit def FB = FB0
}
implicit def kleisliPlusEmpty[F[+_], A](implicit F0: PlusEmpty[F]) = new KleisliPlusEmpty[F, A] {
implicit def kleisliPlusEmpty[F[_], A](implicit F0: PlusEmpty[F]) = new KleisliPlusEmpty[F, A] {
implicit def F = F0
}
implicit def kleisliMonadTrans[R]: Hoist[({type λ[α[+_], β] = Kleisli[α, R, β]})#λ] = new KleisliHoist[R] {}
implicit def kleisliMonadTrans[R]: Hoist[({type λ[α[_], β] = Kleisli[α, R, β]})#λ] = new KleisliHoist[R] {}
}

trait KleisliFunctions {
/**Construct a Kleisli from a Function1 */
def kleisli[M[+_], A, B](f: A => M[B]): Kleisli[M, A, B] = new Kleisli[M, A, B] {
def kleisli[M[_], A, B](f: A => M[B]): Kleisli[M, A, B] = new Kleisli[M, A, B] {
def run(a: A) = f(a)
}

/**Implicitly unwrap the Function1 represented by the Kleisli */
implicit def kleisliFn[M[+_], A, B](k: Kleisli[M, A, B]): A => M[B] = (a: A) => k.run(a)
implicit def kleisliFn[M[_], A, B](k: Kleisli[M, A, B]): A => M[B] = (a: A) => k.run(a)

/**Pure Kleisli arrow */
def ask[M[+_] : Monad, A]: Kleisli[M, A, A] = kleisli(a => Monad[M].point(a))
def ask[M[_] : Monad, A]: Kleisli[M, A, A] = kleisli(a => Monad[M].point(a))

def local[M[+_] : Monad, A, R](f: R => R)(fa: Kleisli[M, R, A]): Kleisli[M, R, A] = fa local f
def local[M[_] : Monad, A, R](f: R => R)(fa: Kleisli[M, R, A]): Kleisli[M, R, A] = fa local f
}

object Kleisli extends KleisliFunctions with KleisliInstances {
def apply[M[+_], A, B](f: A => M[B]): Kleisli[M, A, B] = kleisli(f)
def apply[M[_], A, B](f: A => M[B]): Kleisli[M, A, B] = kleisli(f)
}

//
@@ -183,65 +182,65 @@ import Kleisli.kleisli
// * -> *
//

private[scalaz] trait KleisliFunctor[F[+_], R] extends Functor[({type λ[α] = Kleisli[F, R, α]})#λ] {
private[scalaz] trait KleisliFunctor[F[_], R] extends Functor[({type λ[α] = Kleisli[F, R, α]})#λ] {
implicit def F: Functor[F]

override def map[A, B](fa: Kleisli[F, R, A])(f: A => B): Kleisli[F, R, B] = fa map f
}

private[scalaz] trait KleisliApply[F[+_], R] extends Apply[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliFunctor[F, R] {
private[scalaz] trait KleisliApply[F[_], R] extends Apply[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliFunctor[F, R] {
implicit def F: Apply[F]
override def ap[A, B](fa: => Kleisli[F, R, A])(f: => Kleisli[F, R, A => B]): Kleisli[F, R, B] = Kleisli[F, R, B](r => F.ap(fa(r))(f(r)))
}

private[scalaz] trait KleisliDistributive[F[+_], R] extends Distributive[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliFunctor[F, R] {
private[scalaz] trait KleisliDistributive[F[_], R] extends Distributive[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliFunctor[F, R] {
implicit def F: Distributive[F]

override def distributeImpl[G[_]: Functor, A, B](a: G[A])(f: A => Kleisli[F, R, B]): Kleisli[F, R, G[B]] =
Kleisli(r => F.distribute(a)(f(_) run r))
}

private[scalaz] trait KleisliApplicative[F[+_], R] extends Applicative[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApply[F, R] {
private[scalaz] trait KleisliApplicative[F[_], R] extends Applicative[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApply[F, R] {
implicit def F: Applicative[F]
def point[A](a: => A): Kleisli[F, R, A] = kleisli((r: R) => F.point(a))
}

private[scalaz] trait KleisliMonad[F[+_], R] extends Monad[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R] {
private[scalaz] trait KleisliMonad[F[_], R] extends Monad[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R] {
implicit def F: Monad[F]
def bind[A, B](fa: Kleisli[F, R, A])(f: A => Kleisli[F, R, B]): Kleisli[F, R, B] = fa flatMap f
}

private[scalaz] trait KleisliMonadReader[F[+_], R] extends MonadReader[({type f[s, a] = Kleisli[F, s, a]})#f, R] with KleisliApplicative[F, R] with KleisliMonad[F, R] {
private[scalaz] trait KleisliMonadReader[F[_], R] extends MonadReader[({type f[s, a] = Kleisli[F, s, a]})#f, R] with KleisliApplicative[F, R] with KleisliMonad[F, R] {
implicit def F: Monad[F]

def ask: Kleisli[F, R, R] = Kleisli[F, R, R](r => F.point(r))
def local[A](f: R => R)(fa: Kleisli[F, R, A]): Kleisli[F, R, A] = Kleisli[F, R, A](r => fa.run(f(r)))
}

private[scalaz] trait KleisliHoist[R] extends Hoist[({type λ[α[+_], β] = Kleisli[α, R, β]})#λ] {
def hoist[M[+_]: Monad, N[+_]](f: M ~> N): ({type f[x] = Kleisli[M, R, x]})#f ~> ({type f[x] = Kleisli[N, R, x]})#f =
private[scalaz] trait KleisliHoist[R] extends Hoist[({type λ[α[_], β] = Kleisli[α, R, β]})#λ] {
def hoist[M[_]: Monad, N[_]](f: M ~> N): ({type f[x] = Kleisli[M, R, x]})#f ~> ({type f[x] = Kleisli[N, R, x]})#f =
new (({type f[x] = Kleisli[M, R, x]})#f ~> ({type f[x] = Kleisli[N, R, x]})#f) {
def apply[A](m: Kleisli[M, R, A]): Kleisli[N, R, A] = Kleisli[N, R, A](r => f(m(r)))
}

def liftM[G[+_] : Monad, A](a: G[A]): Kleisli[G, R, A] = Kleisli(_ => a)
def liftM[G[_] : Monad, A](a: G[A]): Kleisli[G, R, A] = Kleisli(_ => a)

implicit def apply[G[+_] : Monad]: Monad[({type λ[α] = Kleisli[G, R, α]})#λ] = Kleisli.kleisliMonadReader
implicit def apply[G[_] : Monad]: Monad[({type λ[α] = Kleisli[G, R, α]})#λ] = Kleisli.kleisliMonadReader
}

private[scalaz] trait KleisliMonadPlus[F[+_], R] extends MonadPlus[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliPlusEmpty[F, R] with KleisliMonad[F, R] {
private[scalaz] trait KleisliMonadPlus[F[_], R] extends MonadPlus[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliPlusEmpty[F, R] with KleisliMonad[F, R] {
implicit def F: MonadPlus[F]
}

private[scalaz] trait KleisliContravariant[F[+_], X] extends Contravariant[({type λ[α] = Kleisli[F, α, X]})#λ] {
private[scalaz] trait KleisliContravariant[F[_], X] extends Contravariant[({type λ[α] = Kleisli[F, α, X]})#λ] {
def contramap[A, B](fa: Kleisli[F, A, X])(f: B => A) = fa local f
}

//
// (* *) -> *
//

private[scalaz] trait KleisliArrow[F[+_]]
private[scalaz] trait KleisliArrow[F[_]]
extends Arrow[({type λ[α, β] = Kleisli[F, α, β]})#λ]
with Choice[({type λ[α, β] = Kleisli[F, α, β]})#λ] {

@@ -270,22 +269,22 @@ private[scalaz] trait KleisliArrow[F[+_]]
}
}

private[scalaz] trait KleisliSemigroup[F[+_], A, B] extends Semigroup[Kleisli[F, A, B]] {
private[scalaz] trait KleisliSemigroup[F[_], A, B] extends Semigroup[Kleisli[F, A, B]] {
implicit def FB: Semigroup[F[B]]
def append(f1: Kleisli[F, A, B], f2: => Kleisli[F, A, B]) = Kleisli[F, A, B](a => FB.append(f1.run(a), f2.run(a)))
}

private[scalaz] trait KleisliMonoid[F[+_], A, B] extends Monoid[Kleisli[F, A, B]] with KleisliSemigroup[F, A, B] {
private[scalaz] trait KleisliMonoid[F[_], A, B] extends Monoid[Kleisli[F, A, B]] with KleisliSemigroup[F, A, B] {
implicit def FB: Monoid[F[B]]
def zero = Kleisli[F, A, B](a => FB.zero)
}

private[scalaz] trait KleisliPlus[F[+_], A] extends Plus[({type λ[+α]=Kleisli[F, A, α]})#λ] {
private[scalaz] trait KleisliPlus[F[_], A] extends Plus[({type λ[α]=Kleisli[F, A, α]})#λ] {
implicit def F: Plus[F]
def plus[B](f1: Kleisli[F, A, B], f2: => Kleisli[F, A, B]) = Kleisli[F, A, B](a => F.plus[B](f1.run(a), f2.run(a)))
}

private[scalaz] trait KleisliPlusEmpty[F[+_], A] extends PlusEmpty[({type λ[+α]=Kleisli[F, A, α]})#λ] with KleisliPlus[F, A] {
private[scalaz] trait KleisliPlusEmpty[F[_], A] extends PlusEmpty[({type λ[α]=Kleisli[F, A, α]})#λ] with KleisliPlus[F, A] {
implicit def F: PlusEmpty[F]
def empty[B] = Kleisli[F, A, B](a => F.empty[B])
}
@@ -1,6 +1,6 @@
package scalaz

sealed trait LazyEitherT[F[+_], +A, +B] {
sealed trait LazyEitherT[F[_], A, B] {
def run: F[LazyEither[A, B]]

import LazyEither._
@@ -21,7 +21,7 @@ sealed trait LazyEitherT[F[+_], +A, +B] {
def swap(implicit F: Functor[F]): F[LazyEither[B, A]] =
F.map(run)(_.swap)

def getOrElse[BB >: B](default: => BB)(implicit F: Functor[F]): F[BB] =
def getOrElse(default: => B)(implicit F: Functor[F]): F[B] =
F.map(run)(_ getOrElse default)

def exists(f: (=> B) => Boolean)(implicit F: Functor[F]): F[Boolean] =
@@ -30,7 +30,7 @@ sealed trait LazyEitherT[F[+_], +A, +B] {
def forall(f: (=> B) => Boolean)(implicit F: Functor[F]): F[Boolean] =
F.map(run)(_ forall f)

def orElse[AA >: A, BB >: B](x: => LazyEitherT[F, AA, BB])(implicit m: Bind[F]): LazyEitherT[F, AA, BB] = {
def orElse(x: => LazyEitherT[F, A, B])(implicit m: Bind[F]): LazyEitherT[F, A, B] = {
val g = run
LazyEitherT(m.bind(g)(_.fold(
_ => x.run
@@ -56,7 +56,7 @@ sealed trait LazyEitherT[F[+_], +A, +B] {
def foreach(f: (=> B) => Unit)(implicit e: Each[F]): Unit =
e.each(run)(_ foreach f)

def flatMap[AA >: A, C](f: (=> B) => LazyEitherT[F, AA, C])(implicit M: Monad[F]): LazyEitherT[F, AA, C] =
def flatMap[C](f: (=> B) => LazyEitherT[F, A, C])(implicit M: Monad[F]): LazyEitherT[F, A, C] =
lazyEitherT(M.bind(run)(_.fold(a => M.point(lazyLeft[C](a)), b => f(b).run)))

def bimap[C, D](f: (=> A) => C, g: (=> B) => D)(implicit F: Functor[F]): LazyEitherT[F, C, D] =
@@ -71,7 +71,7 @@ sealed trait LazyEitherT[F[+_], +A, +B] {
Applicative[G].map(F.traverse(run)(Bitraverse[LazyEither].bitraverseF(f, g)))(LazyEitherT(_: F[LazyEither[C, D]]))
}

def traverse[G[_], AA >: A, C](f: B => G[C])(implicit F: Traverse[F], G: Applicative[G]): G[LazyEitherT[F, AA, C]] = {
def traverse[G[_], C](f: B => G[C])(implicit F: Traverse[F], G: Applicative[G]): G[LazyEitherT[F, A, C]] = {
G.map(F.traverse(run)(o => LazyEither.lazyEitherInstance[A].traverse(o)(f)))(LazyEitherT(_))
}

@@ -83,9 +83,9 @@ sealed trait LazyEitherT[F[+_], +A, +B] {
* disjunction. Because it runs my `F` even when `f`'s `\/` fails,
* it is not consistent with `ap`.
*/
def app[AA >: A, C](f: => LazyEitherT[F, AA, B => C])(implicit F: Apply[F]): LazyEitherT[F, AA, C] = {
def app[C](f: => LazyEitherT[F, A, B => C])(implicit F: Apply[F]): LazyEitherT[F, A, C] = {
// TODO check laziness
LazyEitherT[F, AA, C](F.apply2(f.run, run)((ff: LazyEither[AA, B => C], aa: LazyEither[A, B]) => LazyEither.lazyEitherInstance[AA].ap(aa)(ff)))
LazyEitherT[F, A, C](F.apply2(f.run, run)((ff: LazyEither[A, B => C], aa: LazyEither[A, B]) => LazyEither.lazyEitherInstance[A].ap(aa)(ff)))
}

def left: LeftProjectionT[F, A, B] = new LazyEitherT.LeftProjectionT[F, A, B]() {
@@ -94,16 +94,16 @@ sealed trait LazyEitherT[F[+_], +A, +B] {
}

object LazyEitherT extends LazyEitherTFunctions with LazyEitherTInstances {
def apply[F[+_], A, B](a: F[LazyEither[A, B]]): LazyEitherT[F, A, B] =
def apply[F[_], A, B](a: F[LazyEither[A, B]]): LazyEitherT[F, A, B] =
lazyEitherT(a)

sealed trait LeftProjectionT[F[+_], +A, +B] {
sealed trait LeftProjectionT[F[_], A, B] {
def lazyEitherT: LazyEitherT[F, A, B]

import OptionT._
import LazyOptionT._

def getOrElse[AA >: A](default: => AA)(implicit F: Functor[F]): F[AA] =
def getOrElse(default: => A)(implicit F: Functor[F]): F[A] =
F.map(lazyEitherT.run)(_.left getOrElse default)

def exists(f: (=> A) => Boolean)(implicit F: Functor[F]): F[Boolean] =
@@ -112,7 +112,7 @@ object LazyEitherT extends LazyEitherTFunctions with LazyEitherTInstances {
def forall(f: (=> A) => Boolean)(implicit F: Functor[F]): F[Boolean] =
F.map(lazyEitherT.run)(_.left forall f)

def orElse[AA >: A, BB >: B](x: => LazyEitherT[F, AA, BB])(implicit m: Bind[F]): LazyEitherT[F, AA, BB] = {
def orElse(x: => LazyEitherT[F, A, B])(implicit m: Bind[F]): LazyEitherT[F, A, B] = {
val g = lazyEitherT.run
LazyEitherT(m.bind(g)((z: LazyEither[A, B]) => z.fold(
_ => g
@@ -138,87 +138,87 @@ object LazyEitherT extends LazyEitherTFunctions with LazyEitherTInstances {
def foreach(f: (=> A) => Unit)(implicit F: Each[F]): Unit =
F.each(lazyEitherT.run)(_.left foreach f)

def flatMap[BB >: B, C](f: (=> A) => LazyEitherT[F, C, BB])(implicit M: Monad[F]): LazyEitherT[F, C, BB] =
def flatMap[C](f: (=> A) => LazyEitherT[F, C, B])(implicit M: Monad[F]): LazyEitherT[F, C, B] =
LazyEitherT(M.bind(lazyEitherT.run)(_.fold(a => f(a).run, b => M.point(LazyEither.lazyRight[C](b)))))
}

}

trait LazyEitherTInstances1 {
implicit def lazyEitherTFunctor[F[+_], L](implicit F0: Functor[F]) = new LazyEitherTFunctor[F, L] {
implicit def lazyEitherTFunctor[F[_], L](implicit F0: Functor[F]) = new LazyEitherTFunctor[F, L] {
implicit def F = F0
}
implicit def lazyEitherTLeftProjectionFunctor[F[+_], L](implicit F0: Functor[F]) = new IsomorphismFunctor[({type λ[α] = LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α] = LazyEitherT[F, L, α]})#λ] {
implicit def lazyEitherTLeftProjectionFunctor[F[_], L](implicit F0: Functor[F]) = new IsomorphismFunctor[({type λ[α] = LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α] = LazyEitherT[F, L, α]})#λ] {
implicit def G = lazyEitherTFunctor[F, L]
def iso = LazyEitherT.lazyEitherTLeftProjectionEIso2[F, L]
}
}

trait LazyEitherTInstances0 extends LazyEitherTInstances1 {
implicit def lazyEitherTBifunctor[F[+_]](implicit F0: Functor[F]) = new LazyEitherTBifunctor[F] {
implicit def lazyEitherTBifunctor[F[_]](implicit F0: Functor[F]) = new LazyEitherTBifunctor[F] {
implicit def F = F0
}
implicit def lazyEitherTLeftProjectionBifunctor[F[+_]](implicit F0: Functor[F]) = new IsomorphismBifunctor[({type λ[α, β]=LazyEitherT.LeftProjectionT[F, α, β]})#λ, ({type λ[α, β]=LazyEitherT[F, α, β]})#λ] {
implicit def lazyEitherTLeftProjectionBifunctor[F[_]](implicit F0: Functor[F]) = new IsomorphismBifunctor[({type λ[α, β]=LazyEitherT.LeftProjectionT[F, α, β]})#λ, ({type λ[α, β]=LazyEitherT[F, α, β]})#λ] {
implicit def G = lazyEitherTBifunctor[F]
def iso = LazyEitherT.lazyEitherTLeftProjectionIso2[F]
}

implicit def lazyEitherTMonad[F[+_], L](implicit F0: Monad[F]) = new LazyEitherTMonad[F, L] {
implicit def lazyEitherTMonad[F[_], L](implicit F0: Monad[F]) = new LazyEitherTMonad[F, L] {
implicit def F = F0
}
implicit def lazyEitherTLeftProjectionMonad[F[+_], L](implicit F0: Monad[F]) = new IsomorphismMonad[({type λ[α] = LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α] = LazyEitherT[F, L, α]})#λ] {
implicit def lazyEitherTLeftProjectionMonad[F[_], L](implicit F0: Monad[F]) = new IsomorphismMonad[({type λ[α] = LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α] = LazyEitherT[F, L, α]})#λ] {
implicit def G = lazyEitherTMonad[F, L]
def iso = LazyEitherT.lazyEitherTLeftProjectionEIso2[F, L]
}
implicit def lazyEitherTFoldable[F[+_], L](implicit F0: Foldable[F]) = new LazyEitherTFoldable[F, L] {
implicit def lazyEitherTFoldable[F[_], L](implicit F0: Foldable[F]) = new LazyEitherTFoldable[F, L] {
implicit def F = F0
}
implicit def lazyEitherTLeftProjectionFoldable[F[+_], L](implicit F0: Foldable[F]) = new IsomorphismFoldable[({type λ[α] = LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α] = LazyEitherT[F, L, α]})#λ] {
implicit def lazyEitherTLeftProjectionFoldable[F[_], L](implicit F0: Foldable[F]) = new IsomorphismFoldable[({type λ[α] = LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α] = LazyEitherT[F, L, α]})#λ] {
implicit def G = lazyEitherTFoldable[F, L]
def iso = LazyEitherT.lazyEitherTLeftProjectionEIso2[F, L]
}
}

// TODO more instances
trait LazyEitherTInstances extends LazyEitherTInstances0 {
implicit def lazyEitherTBitraverse[F[+_]](implicit F0: Traverse[F]) = new LazyEitherTBitraverse[F] {
implicit def lazyEitherTBitraverse[F[_]](implicit F0: Traverse[F]) = new LazyEitherTBitraverse[F] {
implicit def F = F0
}
implicit def lazyEitherTLeftProjectionBitraverse[F[+_]](implicit F0: Traverse[F]) = new IsomorphismBitraverse[({type λ[α, β] = LazyEitherT.LeftProjectionT[F, α, β]})#λ, ({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
implicit def lazyEitherTLeftProjectionBitraverse[F[_]](implicit F0: Traverse[F]) = new IsomorphismBitraverse[({type λ[α, β] = LazyEitherT.LeftProjectionT[F, α, β]})#λ, ({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
implicit def G = lazyEitherTBitraverse[F]
def iso = LazyEitherT.lazyEitherTLeftProjectionIso2[F]
}

implicit def lazyEitherTTraverse[F[+_], L](implicit F0: Traverse[F]) = new LazyEitherTTraverse[F, L] {
implicit def lazyEitherTTraverse[F[_], L](implicit F0: Traverse[F]) = new LazyEitherTTraverse[F, L] {
implicit def F = F0
}

implicit def lazyEitherTLeftProjectionTraverse[F[+_], L](implicit F0: Traverse[F]) = new IsomorphismTraverse[({type λ[α]=LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α]=LazyEitherT[F, L, α]})#λ] {
implicit def lazyEitherTLeftProjectionTraverse[F[_], L](implicit F0: Traverse[F]) = new IsomorphismTraverse[({type λ[α]=LazyEitherT.LeftProjectionT[F, L, α]})#λ, ({type λ[α]=LazyEitherT[F, L, α]})#λ] {
implicit def G = lazyEitherTTraverse[F, L]
def iso = LazyEitherT.lazyEitherTLeftProjectionEIso2[F, L]
}
}

trait LazyEitherTFunctions {
def lazyEitherT[F[+_], A, B](a: F[LazyEither[A, B]]): LazyEitherT[F, A, B] = new LazyEitherT[F, A, B] {
def lazyEitherT[F[_], A, B](a: F[LazyEither[A, B]]): LazyEitherT[F, A, B] = new LazyEitherT[F, A, B] {
val run = a
}

import LazyEither._

def lazyLeftT[F[+_], A, B](a: => A)(implicit p: Applicative[F]): LazyEitherT[F, A, B] =
def lazyLeftT[F[_], A, B](a: => A)(implicit p: Applicative[F]): LazyEitherT[F, A, B] =
lazyEitherT(p.point(lazyLeft(a)))

def lazyRightT[F[+_], A, B](b: => B)(implicit p: Applicative[F]): LazyEitherT[F, A, B] =
def lazyRightT[F[_], A, B](b: => B)(implicit p: Applicative[F]): LazyEitherT[F, A, B] =
lazyEitherT(p.point(lazyRight(b)))

import Isomorphism.{IsoFunctorTemplate, IsoBifunctorTemplate}

implicit def lazyEitherTLeftProjectionEIso2[F[+_], E] = new IsoFunctorTemplate[({type λ[α] = LazyEitherT.LeftProjectionT[F, E, α]})#λ, ({type λ[α] = LazyEitherT[F, E, α]})#λ] {
implicit def lazyEitherTLeftProjectionEIso2[F[_], E] = new IsoFunctorTemplate[({type λ[α] = LazyEitherT.LeftProjectionT[F, E, α]})#λ, ({type λ[α] = LazyEitherT[F, E, α]})#λ] {
def to[A](fa: LazyEitherT.LeftProjectionT[F, E, A]): LazyEitherT[F, E, A] = fa.lazyEitherT
def from[A](ga: LazyEitherT[F, E, A]): LazyEitherT.LeftProjectionT[F, E, A] = ga.left
}
implicit def lazyEitherTLeftProjectionIso2[F[+_]] = new IsoBifunctorTemplate[({type λ[α, β] = LazyEitherT.LeftProjectionT[F, α, β]})#λ, ({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
implicit def lazyEitherTLeftProjectionIso2[F[_]] = new IsoBifunctorTemplate[({type λ[α, β] = LazyEitherT.LeftProjectionT[F, α, β]})#λ, ({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
def to[A, B](fa: LazyEitherT.LeftProjectionT[F, A, B]): LazyEitherT[F, A, B] = fa.lazyEitherT
def from[A, B](ga: LazyEitherT[F, A, B]): LazyEitherT.LeftProjectionT[F, A, B] = ga.left
}
@@ -228,42 +228,42 @@ trait LazyEitherTFunctions {
// Type class implementation traits
//

private[scalaz] trait LazyEitherTFunctor[F[+_], E] extends Functor[({type λ[α]=LazyEitherT[F, E, α]})#λ] {
private[scalaz] trait LazyEitherTFunctor[F[_], E] extends Functor[({type λ[α]=LazyEitherT[F, E, α]})#λ] {
implicit def F: Functor[F]

override def map[A, B](fa: LazyEitherT[F, E, A])(f: A => B): LazyEitherT[F, E, B] = fa map (a => f(a))
}

private[scalaz] trait LazyEitherTMonad[F[+_], E] extends Monad[({type λ[α]=LazyEitherT[F, E, α]})#λ] with LazyEitherTFunctor[F, E] {
private[scalaz] trait LazyEitherTMonad[F[_], E] extends Monad[({type λ[α]=LazyEitherT[F, E, α]})#λ] with LazyEitherTFunctor[F, E] {
implicit def F: Monad[F]

def point[A](a: => A): LazyEitherT[F, E, A] = LazyEitherT.lazyRightT(a)

def bind[A, B](fa: LazyEitherT[F, E, A])(f: A => LazyEitherT[F, E, B]): LazyEitherT[F, E, B] = fa flatMap (a => f(a))
}

private[scalaz] trait LazyEitherTFoldable[F[+_], E] extends Foldable.FromFoldr[({type λ[α]=LazyEitherT[F, E, α]})#λ] {
private[scalaz] trait LazyEitherTFoldable[F[_], E] extends Foldable.FromFoldr[({type λ[α]=LazyEitherT[F, E, α]})#λ] {
implicit def F: Foldable[F]

def foldRight[A, B](fa: LazyEitherT[F, E, A], z: => B)(f: (A, => B) => B): B = fa.foldRight(z)(f)
}

private[scalaz] trait LazyEitherTTraverse[F[+_], E] extends Traverse[({type λ[α]=LazyEitherT[F, E, α]})#λ] with LazyEitherTFoldable[F, E] {
private[scalaz] trait LazyEitherTTraverse[F[_], E] extends Traverse[({type λ[α]=LazyEitherT[F, E, α]})#λ] with LazyEitherTFoldable[F, E] {
implicit def F: Traverse[F]

def traverseImpl[G[_]: Applicative, A, B](fa: LazyEitherT[F, E, A])(f: A => G[B]): G[LazyEitherT[F, E, B]] = fa traverse f

override def foldRight[A, B](fa: LazyEitherT[F, E, A], z: => B)(f: (A, => B) => B): B = fa.foldRight(z)(f)
}

private[scalaz] trait LazyEitherTBifunctor[F[+_]] extends Bifunctor[({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
private[scalaz] trait LazyEitherTBifunctor[F[_]] extends Bifunctor[({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
implicit def F: Functor[F]

def bimap[A, B, C, D](fab: LazyEitherT[F, A, B])(f: A => C, g: B => D) =
fab.map(x => g(x)).left.map(x => f(x))
}

private[scalaz] trait LazyEitherTBitraverse[F[+_]] extends Bitraverse[({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
private[scalaz] trait LazyEitherTBitraverse[F[_]] extends Bitraverse[({type λ[α, β] = LazyEitherT[F, α, β]})#λ] {
implicit def F: Traverse[F]

def bitraverseImpl[G[_]: Applicative, A, B, C, D](fab: LazyEitherT[F, A, B])(f: A => G[C], g: B => G[D]): G[LazyEitherT[F, C, D]] =
@@ -1,6 +1,6 @@
package scalaz

sealed trait LazyOptionT[F[+_], +A] {
sealed trait LazyOptionT[F[_], A] {
def run: F[LazyOption[A]]

import LazyOption._
@@ -17,7 +17,7 @@ sealed trait LazyOptionT[F[+_], +A] {
def isEmpty(implicit F: Functor[F]): F[Boolean] =
F.map(run)(_.isEmpty)

def getOrElse[AA >: A](default: => AA)(implicit F: Functor[F]): F[AA] =
def getOrElse(default: => A)(implicit F: Functor[F]): F[A] =
F.map(run)(_.getOrElse(default))

def exists(f: (=> A) => Boolean)(implicit F: Functor[F]): F[Boolean] =
@@ -41,7 +41,7 @@ sealed trait LazyOptionT[F[+_], +A] {
def toLeft[X](right: => X)(implicit F: Functor[F]): EitherT[F, A, X] =
eitherT(F.map(run)(_.fold(z => -\/(z), \/-(right))))

def orElse[AA >: A](a: => LazyOption[AA])(implicit F: Functor[F]): LazyOptionT[F, AA] =
def orElse(a: => LazyOption[A])(implicit F: Functor[F]): LazyOptionT[F, A] =
lazyOptionT(F.map(LazyOptionT.this.run)(_.orElse(a)))

def map[B](f: (=> A) => B)(implicit F: Functor[F]): LazyOptionT[F, B] =
@@ -62,7 +62,7 @@ sealed trait LazyOptionT[F[+_], +A] {
}

object LazyOptionT extends LazyOptionTFunctions with LazyOptionTInstances {
def apply[F[+_], A](r: F[LazyOption[A]]): LazyOptionT[F, A] =
def apply[F[_], A](r: F[LazyOption[A]]): LazyOptionT[F, A] =
lazyOptionT(r)
}

@@ -71,48 +71,48 @@ object LazyOptionT extends LazyOptionTFunctions with LazyOptionTInstances {
//

trait LazyOptionTInstances2 {
implicit def lazyOptionTFunctor[F[+_]](implicit F0: Functor[F]): Functor[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTFunctor[F] {
implicit def lazyOptionTFunctor[F[_]](implicit F0: Functor[F]): Functor[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTFunctor[F] {
implicit def F: Functor[F] = F0
}
}

trait LazyOptionTInstances1 extends LazyOptionTInstances2 {
@deprecated("Monad[F] will be required, and this instance removed, in 7.1",
"7.0.5")
def lazyOptionTApply[F[+_]](implicit F0: Apply[F]): Apply[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTApply[F] {
def lazyOptionTApply[F[_]](implicit F0: Apply[F]): Apply[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTApply[F] {
implicit def F: Apply[F] = F0
}
}

trait LazyOptionTInstances0 extends LazyOptionTInstances1 {
@deprecated("Monad[F] will be required, and this instance removed, in 7.1",
"7.0.5")
def lazyOptionTApplicative[F[+_]](implicit F0: Applicative[F]): Applicative[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTApplicative[F] {
def lazyOptionTApplicative[F[_]](implicit F0: Applicative[F]): Applicative[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTApplicative[F] {
implicit def F: Applicative[F] = F0
}
implicit def lazyOptionEqual[F[+_], A](implicit FA: Equal[F[LazyOption[A]]]): Equal[LazyOptionT[F, A]] = Equal.equalBy((_: LazyOptionT[F, A]).run)
implicit def lazyOptionEqual[F[_], A](implicit FA: Equal[F[LazyOption[A]]]): Equal[LazyOptionT[F, A]] = Equal.equalBy((_: LazyOptionT[F, A]).run)
}

trait LazyOptionTInstances extends LazyOptionTInstances0 {
implicit def lazyOptionTMonadTrans: Hoist[LazyOptionT] = new LazyOptionTHoist {}

implicit def lazyOptionTMonad[F[+_]](implicit F0: Monad[F]): Monad[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTMonad[F] {
implicit def lazyOptionTMonad[F[_]](implicit F0: Monad[F]): Monad[({type λ[α] = LazyOptionT[F, α]})#λ] = new LazyOptionTMonad[F] {
implicit def F: Monad[F] = F0
}
implicit def lazyOptionOrder[F[+_], A](implicit FA: Order[F[LazyOption[A]]]): Order[LazyOptionT[F, A]] = Order.orderBy((_: LazyOptionT[F, A]).run)
implicit def lazyOptionOrder[F[_], A](implicit FA: Order[F[LazyOption[A]]]): Order[LazyOptionT[F, A]] = Order.orderBy((_: LazyOptionT[F, A]).run)
}

trait LazyOptionTFunctions {
def lazyOptionT[F[+_], A](r: F[LazyOption[A]]): LazyOptionT[F, A] = new LazyOptionT[F, A] {
def lazyOptionT[F[_], A](r: F[LazyOption[A]]): LazyOptionT[F, A] = new LazyOptionT[F, A] {
val run = r
}

import LazyOption._

def lazySomeT[F[+_], A](a: => A)(implicit F: Applicative[F]): LazyOptionT[F, A] =
def lazySomeT[F[_], A](a: => A)(implicit F: Applicative[F]): LazyOptionT[F, A] =
lazyOptionT(F.point(lazySome(a)))

def lazyNoneT[F[+_], A](implicit F: Applicative[F]): LazyOptionT[F, A] =
def lazyNoneT[F[_], A](implicit F: Applicative[F]): LazyOptionT[F, A] =
lazyOptionT(F.point(lazyNone[A]))
}

@@ -121,25 +121,25 @@ trait LazyOptionTFunctions {
// Implementation traits for type class instances
//

private[scalaz] trait LazyOptionTFunctor[F[+_]] extends Functor[({type λ[α] = LazyOptionT[F, α]})#λ] {
private[scalaz] trait LazyOptionTFunctor[F[_]] extends Functor[({type λ[α] = LazyOptionT[F, α]})#λ] {
implicit def F: Functor[F]

override def map[A, B](fa: LazyOptionT[F, A])(f: A => B): LazyOptionT[F, B] = fa map (a => f(a))
}

private[scalaz] trait LazyOptionTApply[F[+_]] extends Apply[({type λ[α] = LazyOptionT[F, α]})#λ] with LazyOptionTFunctor[F] {
private[scalaz] trait LazyOptionTApply[F[_]] extends Apply[({type λ[α] = LazyOptionT[F, α]})#λ] with LazyOptionTFunctor[F] {
implicit def F: Apply[F]

override def ap[A, B](fa: => LazyOptionT[F, A])(f: => LazyOptionT[F, A => B]): LazyOptionT[F, B] =
LazyOptionT(F.apply2(f.run, fa.run)({ case (ff, aa) => LazyOption.lazyOptionInstance.ap(aa)(ff) }))
}

private[scalaz] trait LazyOptionTApplicative[F[+_]] extends Applicative[({type λ[α] = LazyOptionT[F, α]})#λ] with LazyOptionTApply[F] {
private[scalaz] trait LazyOptionTApplicative[F[_]] extends Applicative[({type λ[α] = LazyOptionT[F, α]})#λ] with LazyOptionTApply[F] {
implicit def F: Applicative[F]
def point[A](a: => A): LazyOptionT[F, A] = LazyOptionT[F, A](F.point(LazyOption.lazySome(a)))
}

private[scalaz] trait LazyOptionTMonad[F[+_]] extends Monad[({type λ[α] = LazyOptionT[F, α]})#λ] with LazyOptionTApplicative[F] {
private[scalaz] trait LazyOptionTMonad[F[_]] extends Monad[({type λ[α] = LazyOptionT[F, α]})#λ] with LazyOptionTApplicative[F] {
implicit def F: Monad[F]

override def ap[A, B](fa: => LazyOptionT[F, A])(f: => LazyOptionT[F, A => B]): LazyOptionT[F, B] =
@@ -150,12 +150,12 @@ private[scalaz] trait LazyOptionTMonad[F[+_]] extends Monad[({type λ[α] = Lazy
}

private[scalaz] trait LazyOptionTHoist extends Hoist[LazyOptionT] {
def liftM[G[+_], A](a: G[A])(implicit G: Monad[G]): LazyOptionT[G, A] =
def liftM[G[_], A](a: G[A])(implicit G: Monad[G]): LazyOptionT[G, A] =
LazyOptionT[G, A](G.map[A, LazyOption[A]](a)((a: A) => LazyOption.lazySome(a)))

def hoist[M[+_]: Monad, N[+_]](f: M ~> N) = new (({type f[x] = LazyOptionT[M, x]})#f ~> ({type f[x] = LazyOptionT[N, x]})#f) {
def hoist[M[_]: Monad, N[_]](f: M ~> N) = new (({type f[x] = LazyOptionT[M, x]})#f ~> ({type f[x] = LazyOptionT[N, x]})#f) {
def apply[A](fa: LazyOptionT[M, A]): LazyOptionT[N, A] = LazyOptionT(f.apply(fa.run))
}

implicit def apply[G[+_] : Monad]: Monad[({type λ[α] = LazyOptionT[G, α]})#λ] = LazyOptionT.lazyOptionTMonad[G]
implicit def apply[G[_] : Monad]: Monad[({type λ[α] = LazyOptionT[G, α]})#λ] = LazyOptionT.lazyOptionTMonad[G]
}
@@ -19,7 +19,7 @@ import Id._
* @tparam B1 The initial type of the field
* @tparam B2 The final type of the field
*/
sealed trait LensFamily[-A1, +A2, +B1, -B2] {
sealed trait LensFamily[A1, A2, B1, B2] {
def run(a: A1): IndexedStore[B1, B2, A2]

def apply(a: A1): IndexedStore[B1, B2, A2] =
@@ -59,12 +59,12 @@ sealed trait LensFamily[-A1, +A2, +B1, -B2] {
mod(f, _)

/** Modify the value viewed through the lens, returning a functor `X` full of results. */
def modf[X[+_]](f: B1 => X[B2], a: A1)(implicit XF: Functor[X]): X[A2] = {
def modf[X[_]](f: B1 => X[B2], a: A1)(implicit XF: Functor[X]): X[A2] = {
val c = run(a)
XF.map(f(c.pos))(c put _)
}

def =>>=[X[+_]](f: B1 => X[B2])(implicit XF: Functor[X]): A1 => X[A2] =
def =>>=[X[_]](f: B1 => X[B2])(implicit XF: Functor[X]): A1 => X[A2] =
modf(f, _)

/** Modify the value viewed through the lens, returning a `C` on the side. */
@@ -74,16 +74,16 @@ sealed trait LensFamily[-A1, +A2, +B1, -B2] {
}

/** Modify the portion of the state viewed through the lens and return its new value. */
def mods[B <: B2](f: B1 => B): IndexedState[A1, A2, B] =
def mods(f: B1 => B2): IndexedState[A1, A2, B2] =
IndexedState(a => {
val c = run(a)
val b = f(c.pos)
(c put b, b)
})

/** Modify the portion of the state viewed through the lens and return its new value. */
def %=[B <: B2](f: B1 => B): IndexedState[A1, A2, B] =
mods[B](f)
def %=(f: B1 => B2): IndexedState[A1, A2, B2] =
mods(f)

/** Modify the portion of the state viewed through the lens and return its old value.
* @since 7.0.2
@@ -102,12 +102,12 @@ sealed trait LensFamily[-A1, +A2, +B1, -B2] {
modo(f)

/** Set the portion of the state viewed through the lens and return its new value. */
def assign[B <: B2](b: => B): IndexedState[A1, A2, B] =
mods[B](_ => b)
def assign(b: => B2): IndexedState[A1, A2, B2] =
mods(_ => b)

/** Set the portion of the state viewed through the lens and return its new value. */
def :=[B <: B2](b: => B): IndexedState[A1, A2, B] =
assign[B](b)
def :=(b: => B2): IndexedState[A1, A2, B2] =
assign(b)

/** Set the portion of the state viewed through the lens and return its old value.
* @since 7.0.2
@@ -138,28 +138,27 @@ sealed trait LensFamily[-A1, +A2, +B1, -B2] {
lifts(s)

/** Map the function `f` over the lens as a state action. */
def map[C, A <: A1](f: B1 => C): State[A, C] =
def map[C](f: B1 => C): State[A1, C] =
State(a => (a, f(get(a))))

/** Map the function `f` over the value under the lens, as a state action. */
def >-[C, A <: A1](f: B1 => C): State[A, C] = map(f)
def >-[C](f: B1 => C): State[A1, C] = map(f)

/** Bind the function `f` over the value under the lens, as a state action. */
def flatMap[C, X1 <: A1, X2 >: A2](f: B1 => IndexedState[X1, X2, C]): IndexedState[X1, X2, C] =
def flatMap[C](f: B1 => IndexedState[A1, A2, C]): IndexedState[A1, A2, C] =
IndexedState(a => f(get(a))(a))

/** Bind the function `f` over the value under the lens, as a state action. */
def >>-[C, X1 <: A1, X2 >: A2](f: B1 => IndexedState[X1, X2, C]): IndexedState[X1, X2, C] =
flatMap[C, X1, X2](f)
def >>-[C](f: B1 => IndexedState[A1, A2, C]): IndexedState[A1, A2, C] =
flatMap[C](f)

/** Sequence the monadic action of looking through the lens to occur before the state action `f`. */
def ->>-[C, X1 <: A1, X2 >: A2](f: => IndexedState[X1, X2, C]): IndexedState[X1, X2, C] =
def ->>-[C](f: => IndexedState[A1, A2, C]): IndexedState[A1, A2, C] =
flatMap(_ => f)

import annotation.unchecked._
/** Contravariantly mapping the state of a state monad through a lens is a natural transformation */
def liftsNT: ({type m[x] = IndexedState[B1 @uncheckedVariance,B2 @uncheckedVariance,x]})#m ~> ({type n[x] = IndexedState[A1 @uncheckedVariance,A2 @uncheckedVariance,x]})#n =
new (({type m[x] = IndexedState[B1 @uncheckedVariance,B2 @uncheckedVariance,x]})#m ~> ({type n[x] = IndexedState[A1 @uncheckedVariance,A2 @uncheckedVariance,x]})#n) {
def liftsNT: ({type m[x] = IndexedState[B1,B2,x]})#m ~> ({type n[x] = IndexedState[A1,A2,x]})#n =
new (({type m[x] = IndexedState[B1,B2,x]})#m ~> ({type n[x] = IndexedState[A1,A2,x]})#n) {
def apply[C](s : IndexedState[B1,B2,C]): IndexedState[A1,A2,C] = IndexedState[A1,A2,C](a => modp(s(_), a))
}

@@ -181,7 +180,7 @@ sealed trait LensFamily[-A1, +A2, +B1, -B2] {
def >=>[C1, C2](that: LensFamily[B1, B2, C1, C2]): LensFamily[A1, A2, C1, C2] = andThen(that)

/** Two lenses that view a value of the same type can be joined */
def sum[C1, C2, B1m >: B1, B2m <: B2](that: => LensFamily[C1, C2, B1m, B2m]): LensFamily[A1 \/ C1, A2 \/ C2, B1m, B2m] =
def sum[C1, C2](that: => LensFamily[C1, C2, B1, B2]): LensFamily[A1 \/ C1, A2 \/ C2, B1, B2] =
lensFamily{
case -\/(a) =>
run(a) map (-\/(_))
@@ -190,7 +189,7 @@ sealed trait LensFamily[-A1, +A2, +B1, -B2] {
}

/** Alias for `sum` */
def |||[C1, C2, A1m <: A1, A2m >: A2, B1m >: B1, B2m <: B2](that: => LensFamily[C1, C2, B1m, B2m]): LensFamily[A1 \/ C1, A2 \/ C2, B1m, B2m] = sum(that)
def |||[C1, C2](that: => LensFamily[C1, C2, B1, B2]): LensFamily[A1 \/ C1, A2 \/ C2, B1, B2] = sum(that)

/** Two disjoint lenses can be paired */
def product[C1, C2, D1, D2](that: LensFamily[C1, C2, D1, D2]): LensFamily[(A1, C1), (A2, C2), (B1, D1), (B2, D2)] =
@@ -441,7 +440,7 @@ trait LensInstances extends LensInstances0 {

type SetLens[S, K] = SetLensFamily[S, S, K]
val SetLens: SetLensFamily.type = SetLensFamily
case class SetLensFamily[-S1, +S2, K](lens: LensFamily[S1, S2, Set[K], Set[K]]) {
case class SetLensFamily[S1, S2, K](lens: LensFamily[S1, S2, Set[K], Set[K]]) {
/** Setting the value of this lens will change whether or not it is present in the set */
def contains(key: K) = lensFamilyg[S1, S2, Boolean, Boolean](
s => b => lens.mod(m => if (b) m + key else m - key, s): Id[S2]
@@ -483,7 +482,7 @@ trait LensInstances extends LensInstances0 {
type MapLens[S, K, V] = MapLensFamily[S, S, K, V]
val MapLens: MapLensFamily.type = MapLensFamily
/** A lens that views an immutable Map type can provide a mutable.Map-like API via State */
case class MapLensFamily[-S1, +S2, K, V](lens: LensFamily[S1, S2, Map[K, V], Map[K, V]]) {
case class MapLensFamily[S1, S2, K, V](lens: LensFamily[S1, S2, Map[K, V], Map[K, V]]) {
/** Allows both viewing and setting the value of a member of the map */
def member(k: K): LensFamily[S1, S2, Option[V], Option[V]] = lensFamilyg[S1, S2, Option[V], Option[V]](
s => opt => lens.mod((m: Map[K, V]) => (opt match {
@@ -4,7 +4,7 @@ package scalaz
* ListT monad transformer.
*/

sealed case class ListT[M[+_], +A](underlying: M[List[A]]){
sealed case class ListT[M[_], A](underlying: M[List[A]]){
def uncons(implicit M: Applicative[M]): M[Option[(A, ListT[M, A])]] = {
M.map(underlying){list =>
list match {
@@ -14,7 +14,7 @@ sealed case class ListT[M[+_], +A](underlying: M[List[A]]){
}
}

def ::[AA >: A](a: AA)(implicit M: Functor[M]) : ListT[M, AA] = new ListT(M.map(underlying)(list => a :: list))
def ::(a: A)(implicit M: Functor[M]) : ListT[M, A] = new ListT(M.map(underlying)(list => a :: list))

def isEmpty(implicit M: Functor[M]) : M[Boolean] = M.map(underlying)(_.isEmpty)

@@ -66,59 +66,59 @@ sealed case class ListT[M[+_], +A](underlying: M[List[A]]){
//

trait ListTInstances2 {
implicit def listTFunctor[F[+_]](implicit F0: Functor[F]): Functor[({type λ[α] = ListT[F, α]})#λ] = new ListTFunctor[F]{
implicit def listTFunctor[F[_]](implicit F0: Functor[F]): Functor[({type λ[α] = ListT[F, α]})#λ] = new ListTFunctor[F]{
implicit def F: Functor[F] = F0
}

implicit def listTSemigroup[F[+_], A](implicit F0: Bind[F]): Semigroup[ListT[F, A]] = new ListTSemigroup[F, A]{
implicit def listTSemigroup[F[_], A](implicit F0: Bind[F]): Semigroup[ListT[F, A]] = new ListTSemigroup[F, A]{
implicit def F: Bind[F] = F0
}
}

trait ListTInstances1 extends ListTInstances2 {

implicit def listTMonoid[F[+_], A](implicit F0: Monad[F]): Monoid[ListT[F, A]] = new ListTMonoid[F, A] {
implicit def listTMonoid[F[_], A](implicit F0: Monad[F]): Monoid[ListT[F, A]] = new ListTMonoid[F, A] {
implicit def F: Monad[F] = F0
}
}

trait ListTInstances extends ListTInstances1 {
implicit def listTMonadPlus[F[+_]](implicit F0: Monad[F]): MonadPlus[({type λ[α] = ListT[F, α]})#λ] = new ListTMonadPlus[F] {
implicit def listTMonadPlus[F[_]](implicit F0: Monad[F]): MonadPlus[({type λ[α] = ListT[F, α]})#λ] = new ListTMonadPlus[F] {
implicit def F: Monad[F] = F0
}
implicit def listTEqual[F[+_], A](implicit E: Equal[F[List[A]]], F: Monad[F]): Equal[ListT[F, A]] = E.contramap((_: ListT[F, A]).toList)
implicit def listTShow[F[+_], A](implicit E: Show[F[List[A]]], F: Monad[F]): Show[ListT[F, A]] = Contravariant[Show].contramap(E)((_: ListT[F, A]).toList)
implicit def listTEqual[F[_], A](implicit E: Equal[F[List[A]]], F: Monad[F]): Equal[ListT[F, A]] = E.contramap((_: ListT[F, A]).toList)
implicit def listTShow[F[_], A](implicit E: Show[F[List[A]]], F: Monad[F]): Show[ListT[F, A]] = Contravariant[Show].contramap(E)((_: ListT[F, A]).toList)

implicit def listTHoist: Hoist[ListT] = new ListTHoist {}
}

object ListT extends ListTInstances {
def empty[M[+_], A](implicit M: Applicative[M]): ListT[M, A] = new ListT[M, A](M.point(Nil))
def empty[M[_], A](implicit M: Applicative[M]): ListT[M, A] = new ListT[M, A](M.point(Nil))

def fromList[M[+_], A](mas: M[List[A]]): ListT[M, A] = new ListT(mas)
def fromList[M[_], A](mas: M[List[A]]): ListT[M, A] = new ListT(mas)
}

//
// Implementation traits for type class instances
//

private[scalaz] trait ListTFunctor[F[+_]] extends Functor[({type λ[α] = ListT[F, α]})#λ] {
private[scalaz] trait ListTFunctor[F[_]] extends Functor[({type λ[α] = ListT[F, α]})#λ] {
implicit def F: Functor[F]
override def map[A, B](fa: ListT[F, A])(f: A => B): ListT[F, B] = fa map f
}

private[scalaz] trait ListTSemigroup[F[+_], A] extends Semigroup[ListT[F, A]] {
private[scalaz] trait ListTSemigroup[F[_], A] extends Semigroup[ListT[F, A]] {
implicit def F: Bind[F]
def append(f1: ListT[F, A], f2: => ListT[F, A]): ListT[F, A] = f1 ++ f2
}

private[scalaz] trait ListTMonoid[F[+_], A] extends Monoid[ListT[F, A]] with ListTSemigroup[F, A] {
private[scalaz] trait ListTMonoid[F[_], A] extends Monoid[ListT[F, A]] with ListTSemigroup[F, A] {
implicit def F: Monad[F]

def zero: ListT[F, A] = ListT.empty[F, A]
}

private[scalaz] trait ListTMonadPlus[F[+_]] extends MonadPlus[({type λ[α] = ListT[F, α]})#λ] with ListTFunctor[F] {
private[scalaz] trait ListTMonadPlus[F[_]] extends MonadPlus[({type λ[α] = ListT[F, α]})#λ] with ListTFunctor[F] {
implicit def F: Monad[F]

def bind[A, B](fa: ListT[F, A])(f: A => ListT[F, B]): ListT[F, B] = fa flatMap f
@@ -133,11 +133,11 @@ private[scalaz] trait ListTMonadPlus[F[+_]] extends MonadPlus[({type λ[α] = Li
private[scalaz] trait ListTHoist extends Hoist[ListT] {
import ListT._

implicit def apply[G[+_] : Monad]: Monad[({type λ[α] = ListT[G, α]})#λ] = listTMonadPlus[G]
implicit def apply[G[_] : Monad]: Monad[({type λ[α] = ListT[G, α]})#λ] = listTMonadPlus[G]

def liftM[G[+_], A](a: G[A])(implicit G: Monad[G]): ListT[G, A] = fromList(G.map(a)(entry => entry :: Nil))
def liftM[G[_], A](a: G[A])(implicit G: Monad[G]): ListT[G, A] = fromList(G.map(a)(entry => entry :: Nil))

def hoist[M[+_], N[+_]](f: M ~> N)(implicit M: Monad[M]): ({type f[x] = ListT[M, x]})#f ~> ({type f[x] = ListT[N, x]})#f =
def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]): ({type f[x] = ListT[M, x]})#f ~> ({type f[x] = ListT[N, x]})#f =
new (({type f[x] = ListT[M, x]})#f ~> ({type f[x] = ListT[N, x]})#f) {
def apply[A](a: ListT[M, A]): ListT[N, A] = fromList(f(a.underlying))
}
@@ -5,7 +5,7 @@ import std.option.{optionInstance, none, some}
/**
* OptionT monad transformer.
*/
final case class OptionT[F[+_], +A](run: F[Option[A]]) {
final case class OptionT[F[_], A](run: F[Option[A]]) {
self =>

def map[B](f: A => B)(implicit F: Functor[F]): OptionT[F, B] = new OptionT[F, B](mapO(_ map f))
@@ -60,13 +60,13 @@ final case class OptionT[F[+_], +A](run: F[Option[A]]) {
case Some(a) => some(a)
}

def getOrElse[AA >: A](default: => AA)(implicit F: Functor[F]): F[AA] = mapO(_.getOrElse(default))
def getOrElse(default: => A)(implicit F: Functor[F]): F[A] = mapO(_.getOrElse(default))

def exists(f: A => Boolean)(implicit F: Functor[F]): F[Boolean] = mapO(_.exists(f))

def forall(f: A => Boolean)(implicit F: Functor[F]): F[Boolean] = mapO(_.forall(f))

def orElse[AA >: A](a: => OptionT[F, AA])(implicit F: Monad[F]): OptionT[F, AA] =
def orElse(a: => OptionT[F, A])(implicit F: Monad[F]): OptionT[F, A] =
OptionT(F.bind(run) {
case None => a.run
case x@Some(_) => F.point(x)
@@ -86,56 +86,56 @@ final case class OptionT[F[+_], +A](run: F[Option[A]]) {
//

trait OptionTInstances3 {
implicit def optionTFunctor[F[+_]](implicit F0: Functor[F]): Functor[({type λ[α] = OptionT[F, α]})#λ] = new OptionTFunctor[F] {
implicit def optionTFunctor[F[_]](implicit F0: Functor[F]): Functor[({type λ[α] = OptionT[F, α]})#λ] = new OptionTFunctor[F] {
implicit def F: Functor[F] = F0
}
}

trait OptionTInstances2 extends OptionTInstances3 {
@deprecated("Monad[F] will be required, and this instance removed, in 7.1",
"7.0.5")
def optionTApply[F[+_]](implicit F0: Apply[F]): Apply[({type λ[α] = OptionT[F, α]})#λ] = new OptionTApply[F] {
def optionTApply[F[_]](implicit F0: Apply[F]): Apply[({type λ[α] = OptionT[F, α]})#λ] = new OptionTApply[F] {
implicit def F: Apply[F] = F0
}
}

trait OptionTInstances1 extends OptionTInstances2 {
implicit def optionTFoldable[F[+_]](implicit F0: Foldable[F]): Foldable[({type λ[α] = OptionT[F, α]})#λ] = new OptionTFoldable[F] {
implicit def optionTFoldable[F[_]](implicit F0: Foldable[F]): Foldable[({type λ[α] = OptionT[F, α]})#λ] = new OptionTFoldable[F] {
implicit def F: Foldable[F] = F0
}
@deprecated("Monad[F] will be required, and this instance removed, in 7.1",
"7.0.5")
def optionTApplicative[F[+_]](implicit F0: Applicative[F]): Applicative[({type λ[α] = OptionT[F, α]})#λ] = new OptionTApplicative[F] {
def optionTApplicative[F[_]](implicit F0: Applicative[F]): Applicative[({type λ[α] = OptionT[F, α]})#λ] = new OptionTApplicative[F] {
implicit def F: Applicative[F] = F0
}
}

trait OptionTInstances0 extends OptionTInstances1 {
implicit def optionTMonadPlus[F[+_]](implicit F0: Monad[F]): MonadPlus[({type λ[α] = OptionT[F, α]})#λ] = new OptionTMonadPlus[F] {
implicit def optionTMonadPlus[F[_]](implicit F0: Monad[F]): MonadPlus[({type λ[α] = OptionT[F, α]})#λ] = new OptionTMonadPlus[F] {
implicit def F: Monad[F] = F0
}
}

trait OptionTInstances extends OptionTInstances0 {
implicit def optionTMonadTrans: Hoist[OptionT] = new OptionTHoist {}

implicit def optionTTraverse[F[+_]](implicit F0: Traverse[F]): Traverse[({type λ[α] = OptionT[F, α]})#λ] = new OptionTTraverse[F] {
implicit def optionTTraverse[F[_]](implicit F0: Traverse[F]): Traverse[({type λ[α] = OptionT[F, α]})#λ] = new OptionTTraverse[F] {
implicit def F: Traverse[F] = F0
}

implicit def optionTEqual[F[+_], A](implicit F0: Equal[F[Option[A]]]): Equal[OptionT[F, A]] = F0.contramap((_: OptionT[F, A]).run)
implicit def optionTEqual[F[_], A](implicit F0: Equal[F[Option[A]]]): Equal[OptionT[F, A]] = F0.contramap((_: OptionT[F, A]).run)
}

trait OptionTFunctions {
def optionT[M[+_]] = new (({type λ[α] = M[Option[α]]})#λ ~> ({type λ[α] = OptionT[M, α]})#λ) {
def optionT[M[_]] = new (({type λ[α] = M[Option[α]]})#λ ~> ({type λ[α] = OptionT[M, α]})#λ) {
def apply[A](a: M[Option[A]]) = new OptionT[M, A](a)
}

def monadTell[F[+_, +_], W, A](implicit MT0: MonadTell[F, W]) = new OptionTMonadTell[F, W] {
def monadTell[F[_, _], W, A](implicit MT0: MonadTell[F, W]) = new OptionTMonadTell[F, W] {
def MT = MT0
}

def monadListen[F[+_, +_], W, A](implicit ML0: MonadListen[F, W]) = new OptionTMonadListen[F, W] {
def monadListen[F[_, _], W, A](implicit ML0: MonadListen[F, W]) = new OptionTMonadListen[F, W] {
def MT = ML0
}
}
@@ -146,25 +146,25 @@ object OptionT extends OptionTFunctions with OptionTInstances
// Implementation traits for type class instances
//

private[scalaz] trait OptionTFunctor[F[+_]] extends Functor[({type λ[α] = OptionT[F, α]})#λ] {
private[scalaz] trait OptionTFunctor[F[_]] extends Functor[({type λ[α] = OptionT[F, α]})#λ] {
implicit def F: Functor[F]

override def map[A, B](fa: OptionT[F, A])(f: A => B): OptionT[F, B] = fa map f
}

private[scalaz] trait OptionTApply[F[+_]] extends Apply[({type λ[α] = OptionT[F, α]})#λ] with OptionTFunctor[F] {
private[scalaz] trait OptionTApply[F[_]] extends Apply[({type λ[α] = OptionT[F, α]})#λ] with OptionTFunctor[F] {
implicit def F: Apply[F]

override def ap[A, B](fa: => OptionT[F, A])(f: => OptionT[F, A => B]): OptionT[F, B] = fa app f
}

private[scalaz] trait OptionTApplicative[F[+_]] extends Applicative[({type λ[α] = OptionT[F, α]})#λ] with OptionTApply[F] {
private[scalaz] trait OptionTApplicative[F[_]] extends Applicative[({type λ[α] = OptionT[F, α]})#λ] with OptionTApply[F] {
implicit def F: Applicative[F]

def point[A](a: => A): OptionT[F, A] = OptionT[F, A](F.point(some(a)))
}

private[scalaz] trait OptionTMonad[F[+_]] extends Monad[({type λ[α] = OptionT[F, α]})#λ] with OptionTApplicative[F] {
private[scalaz] trait OptionTMonad[F[_]] extends Monad[({type λ[α] = OptionT[F, α]})#λ] with OptionTApplicative[F] {
implicit def F: Monad[F]

override def ap[A, B](fa: => OptionT[F, A])(f: => OptionT[F, A => B]): OptionT[F, B] =
@@ -177,60 +177,60 @@ private[scalaz] trait OptionTMonad[F[+_]] extends Monad[({type λ[α] = OptionT[

}

private[scalaz] trait OptionTFoldable[F[+_]] extends Foldable.FromFoldr[({type λ[α] = OptionT[F, α]})#λ] {
private[scalaz] trait OptionTFoldable[F[_]] extends Foldable.FromFoldr[({type λ[α] = OptionT[F, α]})#λ] {
implicit def F: Foldable[F]

override def foldRight[A, B](fa: OptionT[F, A], z: => B)(f: (A, => B) => B): B = fa.foldRight(z)(f)
}

private[scalaz] trait OptionTTraverse[F[+_]] extends Traverse[({type λ[α] = OptionT[F, α]})#λ] with OptionTFoldable[F] with OptionTFunctor[F]{
private[scalaz] trait OptionTTraverse[F[_]] extends Traverse[({type λ[α] = OptionT[F, α]})#λ] with OptionTFoldable[F] with OptionTFunctor[F]{
implicit def F: Traverse[F]

def traverseImpl[G[_] : Applicative, A, B](fa: OptionT[F, A])(f: A => G[B]): G[OptionT[F, B]] = fa traverse f
}

private[scalaz] trait OptionTHoist extends Hoist[OptionT] {
def liftM[G[+_], A](a: G[A])(implicit G: Monad[G]): OptionT[G, A] =
def liftM[G[_], A](a: G[A])(implicit G: Monad[G]): OptionT[G, A] =
OptionT[G, A](G.map[A, Option[A]](a)((a: A) => some(a)))

def hoist[M[+_]: Monad, N[+_]](f: M ~> N) = new (({type f[x] = OptionT[M, x]})#f ~> ({type f[x] = OptionT[N, x]})#f) {
def hoist[M[_]: Monad, N[_]](f: M ~> N) = new (({type f[x] = OptionT[M, x]})#f ~> ({type f[x] = OptionT[N, x]})#f) {
def apply[A](fa: OptionT[M, A]): OptionT[N, A] = OptionT(f.apply(fa.run))
}

implicit def apply[G[+_] : Monad]: Monad[({type λ[α] = OptionT[G, α]})#λ] = OptionT.optionTMonadPlus[G]
implicit def apply[G[_] : Monad]: Monad[({type λ[α] = OptionT[G, α]})#λ] = OptionT.optionTMonadPlus[G]
}

private[scalaz] trait OptionTMonadPlus[F[+_]] extends MonadPlus[({type λ[α] = OptionT[F, α]})#λ] with OptionTMonad[F] {
private[scalaz] trait OptionTMonadPlus[F[_]] extends MonadPlus[({type λ[α] = OptionT[F, α]})#λ] with OptionTMonad[F] {
implicit def F: Monad[F]

def empty[A]: OptionT[F, A] = OptionT(F point none[A])
def plus[A](a: OptionT[F, A], b: => OptionT[F, A]): OptionT[F, A] = a orElse b
}

private[scalaz] trait OptionTMonadTell[F[+_, +_], W] extends MonadTell[({ type λ[α, +β] = OptionT[({ type f[+x] = F[α, x] })#f, β] })#λ, W] with OptionTMonad[({ type λ[+α] = F[W, α] })#λ] with OptionTHoist {
private[scalaz] trait OptionTMonadTell[F[_, _], W] extends MonadTell[({ type λ[α, β] = OptionT[({ type f[x] = F[α, x] })#f, β] })#λ, W] with OptionTMonad[({ type λ[α] = F[W, α] })#λ] with OptionTHoist {
def MT: MonadTell[F, W]

implicit def F = MT

def writer[A](w: W, v: A): OptionT[({ type λ[+α] = F[W, α] })#λ, A] =
liftM[({ type λ[+α] = F[W, α] })#λ, A](MT.writer(w, v))
def writer[A](w: W, v: A): OptionT[({ type λ[α] = F[W, α] })#λ, A] =
liftM[({ type λ[α] = F[W, α] })#λ, A](MT.writer(w, v))

def some[A](v: => A): OptionT[({ type λ[+α] = F[W, α] })#λ, A] =
OptionT.optionT[({ type λ[+α] = F[W, α] })#λ].apply[A](MT.point(Some(v)))
def some[A](v: => A): OptionT[({ type λ[α] = F[W, α] })#λ, A] =
OptionT.optionT[({ type λ[α] = F[W, α] })#λ].apply[A](MT.point(Some(v)))

def none[A]: OptionT[({ type λ[+α] = F[W, α] })#λ, A] =
OptionT.optionT[({ type λ[+α] = F[W, α] })#λ].apply[A](MT.point(None))
def none[A]: OptionT[({ type λ[α] = F[W, α] })#λ, A] =
OptionT.optionT[({ type λ[α] = F[W, α] })#λ].apply[A](MT.point(None))
}

private[scalaz] trait OptionTMonadListen[F[+_, +_], W] extends MonadListen[({ type λ[α, β] = OptionT[({ type f[+x] = F[α, x] })#f, β] })#λ, W] with OptionTMonadTell[F, W] {
private[scalaz] trait OptionTMonadListen[F[_, _], W] extends MonadListen[({ type λ[α, β] = OptionT[({ type f[x] = F[α, x] })#f, β] })#λ, W] with OptionTMonadTell[F, W] {
def MT: MonadListen[F, W]

def listen[A](ma: OptionT[({ type λ[+α] = F[W, α] })#λ, A]): OptionT[({ type λ[+α] = F[W, α] })#λ, (A, W)] = {
def listen[A](ma: OptionT[({ type λ[α] = F[W, α] })#λ, A]): OptionT[({ type λ[α] = F[W, α] })#λ, (A, W)] = {
val tmp = MT.bind[(Option[A], W), Option[(A, W)]](MT.listen(ma.run)) {
case (None, _) => MT.point(None)
case (Some(a), w) => MT.point(Some(a, w))
}

OptionT.optionT[({ type λ[+α] = F[W, α] })#λ].apply[(A, W)](tmp)
OptionT.optionT[({ type λ[α] = F[W, α] })#λ].apply[(A, W)](tmp)
}
}
@@ -16,7 +16,7 @@ package scalaz
* @tparam B1 The initial type of the optional field
* @tparam B2 The final type of the optional field
*/
sealed trait PLensFamily[-A1, +A2, +B1, -B2] {
sealed trait PLensFamily[A1, A2, B1, B2] {
def run(a: A1): Option[IndexedStore[B1, B2, A2]]

def apply(a: A1): Option[IndexedStore[B1, B2, A2]] =
@@ -182,7 +182,7 @@ sealed trait PLensFamily[-A1, +A2, +B1, -B2] {
def >=>[C1, C2](that: PLensFamily[B1, B2, C1, C2]): PLensFamily[A1, A2, C1, C2] = andThen(that)

/** Two partial lenses that view a value of the same type can be joined */
def sum[C1, C2, B1m >: B1, B2m <: B2](that: => PLensFamily[C1, C2, B1m, B2m]): PLensFamily[A1 \/ C1, A2 \/ C2, B1m, B2m] =
def sum[C1, C2](that: => PLensFamily[C1, C2, B1, B2]): PLensFamily[A1 \/ C1, A2 \/ C2, B1, B2] =
plensFamily {
case -\/(a) =>
run(a) map (_ map (-\/(_)))
@@ -191,7 +191,7 @@ sealed trait PLensFamily[-A1, +A2, +B1, -B2] {
}

/** Alias for `sum` */
def |||[C1, C2, B1m >: B1, B2m <: B2](that: => PLensFamily[C1, C2, B1m, B2m]): PLensFamily[A1 \/ C1, A2 \/ C2, B1m, B2m] = sum(that)
def |||[C1, C2](that: => PLensFamily[C1, C2, B1, B2]): PLensFamily[A1 \/ C1, A2 \/ C2, B1, B2] = sum(that)

/** Two disjoint partial lenses can be paired */
def product[C1, C2, D1, D2](that: PLensFamily[C1, C2, D1, D2]): PLensFamily[(A1, C1), (A2, C2), (B1, D1), (B2, D2)] =
@@ -1,7 +1,7 @@
package scalaz

/** A monad transformer stack yielding `(R, S1) => F[(W, A, S2)]`. */
sealed trait IndexedReaderWriterStateT[F[+_], -R, +W, -S1, +S2, +A] {
sealed trait IndexedReaderWriterStateT[F[_], -R, W, -S1, S2, A] {
self =>
def run(r: R, s: S1): F[(W, A, S2)]

@@ -17,8 +17,8 @@ sealed trait IndexedReaderWriterStateT[F[+_], -R, +W, -S1, +S2, +A] {
}
}

def flatMap[B, RR <: R, WW >: W, S3](f: A => IndexedReaderWriterStateT[F, RR, WW, S2, S3, B])(implicit F: Bind[F], W: Semigroup[WW]): IndexedReaderWriterStateT[F, RR, WW, S1, S3, B] = new IndexedReaderWriterStateT[F, RR, WW, S1, S3, B] {
def run(r: RR, s1: S1): F[(WW, B, S3)] = {
def flatMap[B, RR <: R, S3](f: A => IndexedReaderWriterStateT[F, RR, W, S2, S3, B])(implicit F: Bind[F], W: Semigroup[W]): IndexedReaderWriterStateT[F, RR, W, S1, S3, B] = new IndexedReaderWriterStateT[F, RR, W, S1, S3, B] {
def run(r: RR, s1: S1): F[(W, B, S3)] = {
F.bind(self.run(r, s1)) {
case (w1, a, s2) => {
F.map(f(a).run(r, s2)) {
@@ -31,7 +31,7 @@ sealed trait IndexedReaderWriterStateT[F[+_], -R, +W, -S1, +S2, +A] {
}

object IndexedReaderWriterStateT extends ReaderWriterStateTFunctions with ReaderWriterStateTInstances {
def apply[F[+_], R, W, S1, S2, A](f: (R, S1) => F[(W, A, S2)]): IndexedReaderWriterStateT[F, R, W, S1, S2, A] = new IndexedReaderWriterStateT[F, R, W, S1, S2, A] {
def apply[F[_], R, W, S1, S2, A](f: (R, S1) => F[(W, A, S2)]): IndexedReaderWriterStateT[F, R, W, S1, S2, A] = new IndexedReaderWriterStateT[F, R, W, S1, S2, A] {
def run(r: R, s: S1): F[(W, A, S2)] = f(r, s)
}
}
@@ -41,37 +41,37 @@ trait ReaderWriterStateTFunctions {
}

trait IndexedReaderWriterStateTInstances {
implicit def irwstFunctor[F[+_], R, W, S1, S2](implicit F0: Functor[F]): Functor[({type λ[+α] = IndexedReaderWriterStateT[F, R, W, S1, S2, α]})#λ] =
implicit def irwstFunctor[F[_], R, W, S1, S2](implicit F0: Functor[F]): Functor[({type λ[α] = IndexedReaderWriterStateT[F, R, W, S1, S2, α]})#λ] =
new IndexedReaderWriterStateTFunctor[F, R, W, S1, S2] {
implicit def F = F0
}
}

trait ReaderWriterStateTInstances extends IndexedReaderWriterStateTInstances {
implicit def rwstMonad[F[+_], R, W, S](implicit W0: Monoid[W], F0: Monad[F]):
MonadReader[({type λ[-r, +α]=ReaderWriterStateT[F, r, W, S, α]})#λ, R] with MonadState[({type f[s, +α] = ReaderWriterStateT[F, R, W, s, α]})#f, S] with MonadListen[({type f[+w, +α] = ReaderWriterStateT[F, R, w, S, α]})#f, W] =
implicit def rwstMonad[F[_], R, W, S](implicit W0: Monoid[W], F0: Monad[F]):
MonadReader[({type λ[r, α]=ReaderWriterStateT[F, r, W, S, α]})#λ, R] with MonadState[({type f[s, α] = ReaderWriterStateT[F, R, W, s, α]})#f, S] with MonadListen[({type f[w, α] = ReaderWriterStateT[F, R, w, S, α]})#f, W] =
new ReaderWriterStateTMonad[F, R, W, S] {
implicit def F = F0
implicit def W = W0
}

implicit def rwstHoist[R, W, S](implicit W0: Monoid[W]): Hoist[({type λ[α[+_], β] = ReaderWriterStateT[α, R, W, S, β]})#λ] =
implicit def rwstHoist[R, W, S](implicit W0: Monoid[W]): Hoist[({type λ[α[_], β] = ReaderWriterStateT[α, R, W, S, β]})#λ] =
new ReaderWriterStateTHoist[R, W, S] {
implicit def W = W0
}

}

private[scalaz] trait IndexedReaderWriterStateTFunctor[F[+_], R, W, S1, S2] extends Functor[({type λ[+α]=IndexedReaderWriterStateT[F, R, W, S1, S2, α]})#λ] {
private[scalaz] trait IndexedReaderWriterStateTFunctor[F[_], R, W, S1, S2] extends Functor[({type λ[α]=IndexedReaderWriterStateT[F, R, W, S1, S2, α]})#λ] {
implicit def F: Functor[F]

override def map[A, B](fa: IndexedReaderWriterStateT[F, R, W, S1, S2, A])(f: A => B): IndexedReaderWriterStateT[F, R, W, S1, S2, B] = fa map f
}

private[scalaz] trait ReaderWriterStateTMonad[F[+_], R, W, S]
extends MonadReader[({type λ[-r, +α]=ReaderWriterStateT[F, r, W, S, α]})#λ, R]
with MonadState[({type f[s, +α] = ReaderWriterStateT[F, R, W, s, α]})#f, S]
with MonadListen[({type f[+w, +α] = ReaderWriterStateT[F, R, w, S, α]})#f, W]
private[scalaz] trait ReaderWriterStateTMonad[F[_], R, W, S]
extends MonadReader[({type λ[r, α]=ReaderWriterStateT[F, r, W, S, α]})#λ, R]
with MonadState[({type f[s, α] = ReaderWriterStateT[F, R, W, s, α]})#f, S]
with MonadListen[({type f[w, α] = ReaderWriterStateT[F, R, w, S, α]})#f, W]
with IndexedReaderWriterStateTFunctor[F, R, W, S, S] {
implicit def F: Monad[F]
implicit def W: Monoid[W]
@@ -104,17 +104,17 @@ private[scalaz] trait ReaderWriterStateTMonad[F[+_], R, W, S]
ReaderWriterStateT((r, s) => F.map(ma.run(r, s)) { case (w, a, s1) => (w, (a, w), s1)})
}

private[scalaz] trait ReaderWriterStateTHoist[R, W, S] extends Hoist[({type λ[α[+_], β] = ReaderWriterStateT[α, R, W, S, β]})#λ] {
private[scalaz] trait ReaderWriterStateTHoist[R, W, S] extends Hoist[({type λ[α[_], β] = ReaderWriterStateT[α, R, W, S, β]})#λ] {
implicit def W: Monoid[W]

def hoist[M[+_], N[+_]](f: M ~> N)(implicit M: Monad[M]) = new (({type λ[α] = ReaderWriterStateT[M, R, W, S, α]})#λ ~> ({type λ[α] = ReaderWriterStateT[N, R, W, S, α]})#λ) {
def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]) = new (({type λ[α] = ReaderWriterStateT[M, R, W, S, α]})#λ ~> ({type λ[α] = ReaderWriterStateT[N, R, W, S, α]})#λ) {
def apply[A](ma: ReaderWriterStateT[M, R, W, S, A]): ReaderWriterStateT[N, R, W, S, A] = ReaderWriterStateT {
case (r,s) => f.apply(ma.run(r,s))
}
}

def liftM[M[+_], A](ma: M[A])(implicit M: Monad[M]): ReaderWriterStateT[M, R, W, S, A] = {
def liftM[M[_], A](ma: M[A])(implicit M: Monad[M]): ReaderWriterStateT[M, R, W, S, A] = {
ReaderWriterStateT( (r,s) => M.map(ma)((W.zero, _, s)))
}
implicit def apply[M[+_] : Monad]: Monad[({type λ[α] = ReaderWriterStateT[M, R, W, S, α]})#λ] = IndexedReaderWriterStateT.rwstMonad
implicit def apply[M[_] : Monad]: Monad[({type λ[α] = ReaderWriterStateT[M, R, W, S, α]})#λ] = IndexedReaderWriterStateT.rwstMonad
}
@@ -9,7 +9,7 @@ import Id._
//[06:58am] dabblego: :t \f p -> evalStateT (StateT (\s -> do r <- f s; q <- p s; return (r, if q then succ s else s))) mempty
//[06:58am] lambdabot: forall s (m :: * -> *) a. (Monad m, Enum s, Monoid s) => (s -> m a) -> (s -> m Bool) -> m a
//
trait IndexedStateT[F[+_], -S1, +S2, +A] { self =>
trait IndexedStateT[F[_], -S1, S2, A] { self =>
/** Run and return the final value and state in the context of `F` */
def apply(initial: S1): F[(S2, A)]

@@ -66,32 +66,32 @@ trait IndexedStateT[F[+_], -S1, +S2, +A] { self =>
case (s1, a) => f(a)(s1)
})

def lift[M[+_]: Applicative]: IndexedStateT[({type λ[+α]=M[F[α]]})#λ, S1, S2, A] = new IndexedStateT[({type λ[+α]=M[F[α]]})#λ, S1, S2, A] {
def lift[M[_]: Applicative]: IndexedStateT[({type λ[α]=M[F[α]]})#λ, S1, S2, A] = new IndexedStateT[({type λ[α]=M[F[α]]})#λ, S1, S2, A] {
def apply(initial: S1): M[F[(S2, A)]] = Applicative[M].point(self(initial))
}

import Liskov._
def unlift[M[+_], FF[+_], AA >: A, S1m <: S1, S2m >: S2](implicit M: Comonad[M], ev: this.type <~< IndexedStateT[({type λ[+α] = M[FF[α]]})#λ, S1m, S2m, AA]): IndexedStateT[FF, S1m, S2m, AA] = new IndexedStateT[FF, S1m, S2m, AA] {
def apply(initial: S1m): FF[(S2m, AA)] = Comonad[M].copoint(ev(self)(initial))
def unlift[M[_], FF[_], S <: S1](implicit M: Comonad[M], ev: this.type <~< IndexedStateT[({type λ[α] = M[FF[α]]})#λ, S, S2, A]): IndexedStateT[FF, S, S2, A] = new IndexedStateT[FF, S, S2, A] {
def apply(initial: S): FF[(S2, A)] = Comonad[M].copoint(ev(self)(initial))
}

def unliftId[M[+_], AA >: A, S1m <: S1, S2m >: S2](implicit M: Comonad[M], ev: this.type <~< IndexedStateT[M, S1m, S2m, AA]): IndexedState[S1m, S2m, AA] = unlift[M, Id, AA, S1m, S2m]
def unliftId[M[_], S <: S1](implicit M: Comonad[M], ev: this.type <~< IndexedStateT[M, S, S2, A]): IndexedState[S, S2, A] = unlift[M, Id, S]

def rwst[W, R](implicit F: Functor[F], W: Monoid[W]): IndexedReaderWriterStateT[F, R, W, S1, S2, A] = IndexedReaderWriterStateT(
(r, s) => F.map(self(s)) {
case (s, a) => (W.zero, a, s)
}
)

def zoom[S0, S3](l: LensFamily[S0, S3, S1, S2])(implicit F: Functor[F]): IndexedStateT[F, S0, S3, A] = new IndexedStateT[F, S0, S3, A] {
def zoom[S0, S3, S <: S1](l: LensFamily[S0, S3, S, S2])(implicit F: Functor[F]): IndexedStateT[F, S0, S3, A] = new IndexedStateT[F, S0, S3, A] {
def apply(s0: S0) = F.map(self(l get s0)) {
case (s2, a) => (l.set(s0, s2), a)
}
}
}

object IndexedStateT extends StateTFunctions with StateTInstances {
def apply[F[+_], S1, S2, A](f: S1 => F[(S2, A)]): IndexedStateT[F, S1, S2, A] = new IndexedStateT[F, S1, S2, A] {
def apply[F[_], S1, S2, A](f: S1 => F[(S2, A)]): IndexedStateT[F, S1, S2, A] = new IndexedStateT[F, S1, S2, A] {
def apply(s: S1) = f(s)
}
}
@@ -101,82 +101,82 @@ object IndexedStateT extends StateTFunctions with StateTInstances {
//

trait IndexedStateTInstances2 {
implicit def indexedStateTContravariant[S2, A0, F[+_]]: Contravariant[({type f[-a] = IndexedStateT[F, a, S2, A0]})#f] = new IndexedStateTContravariant[S2, A0, F] {}
implicit def indexedStateTContravariant[S2, A0, F[_]]: Contravariant[({type f[-a] = IndexedStateT[F, a, S2, A0]})#f] = new IndexedStateTContravariant[S2, A0, F] {}
}

trait IndexedStateTInstances1 extends IndexedStateTInstances2 {
implicit def indexedStateTFunctorLeft[S1, A0, F[+_]](implicit F0: Functor[F]): Functor[({type f[+a] = IndexedStateT[F, S1, a, A0]})#f] = new IndexedStateTFunctorLeft[S1, A0, F] {
implicit def indexedStateTFunctorLeft[S1, A0, F[_]](implicit F0: Functor[F]): Functor[({type f[a] = IndexedStateT[F, S1, a, A0]})#f] = new IndexedStateTFunctorLeft[S1, A0, F] {
implicit def F: Functor[F] = F0
}
}

trait IndexedStateTInstances0 extends IndexedStateTInstances1 {
implicit def indexedStateTBifunctor[S1, F[+_]](implicit F0: Functor[F]): Bifunctor[({type f[+a, +b] = IndexedStateT[F, S1, a, b]})#f] = new IndexedStateTBifunctor[S1, F] {
implicit def indexedStateTBifunctor[S1, F[_]](implicit F0: Functor[F]): Bifunctor[({type f[a, b] = IndexedStateT[F, S1, a, b]})#f] = new IndexedStateTBifunctor[S1, F] {
implicit def F: Functor[F] = F0
}
}

trait IndexedStateTInstances extends IndexedStateTInstances0 {
implicit def indexedStateTFunctorRight[S1, S2, F[+_]](implicit F0: Functor[F]): Functor[({type f[+a] = IndexedStateT[F, S1, S2, a]})#f] = new IndexedStateTFunctorRight[S1, S2, F] {
implicit def indexedStateTFunctorRight[S1, S2, F[_]](implicit F0: Functor[F]): Functor[({type f[a] = IndexedStateT[F, S1, S2, a]})#f] = new IndexedStateTFunctorRight[S1, S2, F] {
implicit def F: Functor[F] = F0
}
}

trait StateTInstances1 extends IndexedStateTInstances {
implicit def stateTMonadState[S, F[+_]](implicit F0: Monad[F]): MonadState[({type f[s, +a] = StateT[F, s, a]})#f, S] = new StateTMonadState[S, F] {
implicit def stateTMonadState[S, F[_]](implicit F0: Monad[F]): MonadState[({type f[s, a] = StateT[F, s, a]})#f, S] = new StateTMonadState[S, F] {
implicit def F: Monad[F] = F0
}
}

trait StateTInstances0 extends StateTInstances1 {
implicit def StateMonadTrans[S]: Hoist[({type f[g[+_], +a] = StateT[g, S, a]})#f] = new StateTHoist[S] {}
implicit def StateMonadTrans[S]: Hoist[({type f[g[_], a] = StateT[g, S, a]})#f] = new StateTHoist[S] {}
}

trait StateTInstances extends StateTInstances0 {
implicit def stateMonad[S]: MonadState[({type f[s, +a] = State[s, a]})#f, S] =
implicit def stateMonad[S]: MonadState[({type f[s, a] = State[s, a]})#f, S] =
StateT.stateTMonadState[S, Id](Id.id)
}

trait IndexedStateTFunctions {
def constantIndexedStateT[F[+_], S1, S2, A](a: A)(s: => S2)(implicit F: Applicative[F]): IndexedStateT[F, S1, S2, A] =
def constantIndexedStateT[F[_], S1, S2, A](a: A)(s: => S2)(implicit F: Applicative[F]): IndexedStateT[F, S1, S2, A] =
IndexedStateT((_: S1) => F.point((s, a)))
}

trait StateTFunctions extends IndexedStateTFunctions {
def constantStateT[F[+_], S, A](a: A)(s: => S)(implicit F: Applicative[F]): StateT[F, S, A] =
def constantStateT[F[_], S, A](a: A)(s: => S)(implicit F: Applicative[F]): StateT[F, S, A] =
StateT((_: S) => F.point((s, a)))

def stateT[F[+_], S, A](a: A)(implicit F: Applicative[F]): StateT[F, S, A] =
def stateT[F[_], S, A](a: A)(implicit F: Applicative[F]): StateT[F, S, A] =
StateT(s => F.point((s, a)))
}

//
// Implementation traits for type class instances
//

private[scalaz] trait IndexedStateTContravariant[S2, A0, F[+_]] extends Contravariant[({type f[-a] = IndexedStateT[F, a, S2, A0]})#f] {
private[scalaz] trait IndexedStateTContravariant[S2, A0, F[_]] extends Contravariant[({type f[-a] = IndexedStateT[F, a, S2, A0]})#f] {
override def contramap[A, B](fa: IndexedStateT[F, A, S2, A0])(f: B => A): IndexedStateT[F, B, S2, A0] = fa.contramap(f)
}

private[scalaz] trait IndexedStateTBifunctor[S1, F[+_]] extends Bifunctor[({type f[+a, +b] = IndexedStateT[F, S1, a, b]})#f] {
private[scalaz] trait IndexedStateTBifunctor[S1, F[_]] extends Bifunctor[({type f[a, b] = IndexedStateT[F, S1, a, b]})#f] {
implicit def F: Functor[F]

override def bimap[A, B, C, D](fab: IndexedStateT[F, S1, A, B])(f: A => C, g: B => D): IndexedStateT[F, S1, C, D] = fab.bimap(f)(g)
}

private[scalaz] trait IndexedStateTFunctorLeft[S1, A0, F[+_]] extends Functor[({type f[+a] = IndexedStateT[F, S1, a, A0]})#f] {
private[scalaz] trait IndexedStateTFunctorLeft[S1, A0, F[_]] extends Functor[({type f[a] = IndexedStateT[F, S1, a, A0]})#f] {
implicit def F: Functor[F]

override def map[A, B](fa: IndexedStateT[F, S1, A, A0])(f: A => B): IndexedStateT[F, S1, B, A0] = fa.imap(f)
}

private[scalaz] trait IndexedStateTFunctorRight[S1, S2, F[+_]] extends Functor[({type f[+a] = IndexedStateT[F, S1, S2, a]})#f] {
private[scalaz] trait IndexedStateTFunctorRight[S1, S2, F[_]] extends Functor[({type f[a] = IndexedStateT[F, S1, S2, a]})#f] {
implicit def F: Functor[F]

override def map[A, B](fa: IndexedStateT[F, S1, S2, A])(f: A => B): IndexedStateT[F, S1, S2, B] = fa.map(f)
}

private[scalaz] trait StateTMonadState[S, F[+_]] extends MonadState[({type f[s, +a] = StateT[F, s, a]})#f, S] {
private[scalaz] trait StateTMonadState[S, F[_]] extends MonadState[({type f[s, a] = StateT[F, s, a]})#f, S] {
implicit def F: Monad[F]

def bind[A, B](fa: StateT[F, S, A])(f: A => StateT[F, S, B]): StateT[F, S, B] = fa.flatMap(f)
@@ -197,19 +197,19 @@ private[scalaz] trait StateTMonadState[S, F[+_]] extends MonadState[({type f[s,
override def gets[A](f: S => A): StateT[F, S, A] = StateT(s => F.point((s, f(s))))
}

private[scalaz] trait StateTHoist[S] extends Hoist[({type f[g[+_], +a] = StateT[g, S, a]})#f] {
private[scalaz] trait StateTHoist[S] extends Hoist[({type f[g[_], a] = StateT[g, S, a]})#f] {

type StateTF[G[+_], S] = {
type f[+x] = StateT[G, S, x]
type StateTF[G[_], S] = {
type f[x] = StateT[G, S, x]
}

def liftM[G[+_], A](ga: G[A])(implicit G: Monad[G]): StateT[G, S, A] =
def liftM[G[_], A](ga: G[A])(implicit G: Monad[G]): StateT[G, S, A] =
StateT(s => G.map(ga)(a => (s, a)))

def hoist[M[+_]: Monad, N[+_]](f: M ~> N) = new (StateTF[M, S]#f ~> StateTF[N, S]#f) {
def hoist[M[_]: Monad, N[_]](f: M ~> N) = new (StateTF[M, S]#f ~> StateTF[N, S]#f) {
def apply[A](action: StateT[M, S, A]) =
StateT[N, S, A](s => f(action(s)))
}

implicit def apply[G[+_] : Monad]: Monad[({type λ[+α] = StateT[G, S, α]})#λ] = StateT.stateTMonadState[S, G]
implicit def apply[G[_] : Monad]: Monad[({type λ[α] = StateT[G, S, α]})#λ] = StateT.stateTMonadState[S, G]
}
@@ -5,7 +5,7 @@ import Id._
/**
* @see [[scalaz.Lens]]
*/
sealed trait IndexedStoreT[F[+_], +I, -A, +B] {
sealed trait IndexedStoreT[F[_], +I, A, B] {
def run: (F[A => B], I)

import StoreT._
@@ -53,7 +53,7 @@ sealed trait IndexedStoreT[F[+_], +I, -A, +B] {
def seeks[J](f: I => J): IndexedStoreT[F, J, A, B] =
indexedStoreT((set, f(pos)))

def experiment[G[+_]](f: I => G[A])(implicit F: Comonad[F], G: Functor[G]): G[B] =
def experiment[G[_]](f: I => G[A])(implicit F: Comonad[F], G: Functor[G]): G[B] =
G.map(f(pos))(F.copoint(set))

def copoint(implicit F: Comonad[F], ev: I <:< A): B =
@@ -80,13 +80,13 @@ sealed trait IndexedStoreT[F[+_], +I, -A, +B] {
}

object IndexedStoreT extends StoreTFunctions with StoreTInstances {
def apply[F[+_], I, A, B](r: (F[A => B], I)): IndexedStoreT[F, I, A, B] =
def apply[F[_], I, A, B](r: (F[A => B], I)): IndexedStoreT[F, I, A, B] =
indexedStoreT(r)
}

trait IndexedStoreTFunctions {

def indexedStoreT[F[+_], I, A, B](r: (F[A => B], I)): IndexedStoreT[F, I, A, B] = new IndexedStoreT[F, I, A, B] {
def indexedStoreT[F[_], I, A, B](r: (F[A => B], I)): IndexedStoreT[F, I, A, B] = new IndexedStoreT[F, I, A, B] {
val run = r
}

@@ -96,95 +96,95 @@ trait IndexedStoreTFunctions {

trait StoreTFunctions extends IndexedStoreTFunctions {

def storeT[F[+_], A, B](r: (F[A => B], A)): StoreT[F, A, B] =
def storeT[F[_], A, B](r: (F[A => B], A)): StoreT[F, A, B] =
indexedStoreT[F, A, A, B](r)

def store[A, B](a: A)(f: A => B): Store[A, B] =
storeT[Id, A, B](f -> a)
}
trait IndexedStoreTInstances2 {
implicit def indexedStoreTContravariant[F[+_], I, B](implicit F0: Functor[F]) = new IndexedStoreTContravariant[F, I, B] {
implicit def indexedStoreTContravariant[F[_], I, B](implicit F0: Functor[F]) = new IndexedStoreTContravariant[F, I, B] {
implicit def F: Functor[F] = F0
}
}
trait IndexedStoreTInstances1 extends IndexedStoreTInstances2 {
implicit def indexedStoreTFunctorLeft[F[+_], A, B] = new IndexedStoreTFunctorLeft[F, A, B] {}
implicit def indexedStoreTFunctorLeft[F[_], A, B] = new IndexedStoreTFunctorLeft[F, A, B] {}
}
trait IndexedStoreTInstances0 extends IndexedStoreTInstances1 {
implicit def indexedStoreTBifunctor[F[+_], A](implicit F0: Functor[F]) = new IndexedStoreTBifunctor[F, A] {
implicit def indexedStoreTBifunctor[F[_], A](implicit F0: Functor[F]) = new IndexedStoreTBifunctor[F, A] {
implicit def F: Functor[F] = F0
}
}
trait IndexedStoreTInstances extends IndexedStoreTInstances0 {
implicit def indexedStoreTFunctorRight[F[+_], I, A](implicit F0: Functor[F]) = new IndexedStoreTFunctorRight[F, I, A] {
implicit def indexedStoreTFunctorRight[F[_], I, A](implicit F0: Functor[F]) = new IndexedStoreTFunctorRight[F, I, A] {
implicit def F: Functor[F] = F0
}
}
trait StoreTInstances2 extends IndexedStoreTInstances {
implicit def storeTCobind[F[+_], A](implicit F0: Cobind[F]) = new StoreTCobind[F, A] {
implicit def storeTCobind[F[_], A](implicit F0: Cobind[F]) = new StoreTCobind[F, A] {
implicit def F: Cobind[F] = F0
}
}
trait StoreTInstances1 extends StoreTInstances2 {
implicit def storeTComonad[F[+_], A](implicit F0: Comonad[F]) = new StoreTComonad[F, A] {
implicit def storeTComonad[F[_], A](implicit F0: Comonad[F]) = new StoreTComonad[F, A] {
implicit def F: Comonad[F] = F0
}
}
trait StoreTInstances0 extends StoreTInstances1 {
implicit def storeTComonadStore[F[+_], A](implicit F0: Comonad[F]) = new StoreTComonadStore[F, A] {
implicit def storeTComonadStore[F[_], A](implicit F0: Comonad[F]) = new StoreTComonadStore[F, A] {
implicit def F: Comonad[F] = F0
}
}
trait StoreTInstances extends StoreTInstances0 {
implicit def storeTCohoist[S]: Cohoist[({type f[g[+_], +a] = StoreT[g, S, a]})#f] = new StoreTCohoist[S] {}
implicit def storeTCohoist[S]: Cohoist[({type f[g[_], a] = StoreT[g, S, a]})#f] = new StoreTCohoist[S] {}
}

private[scalaz] trait IndexedStoreTFunctorLeft[F[+_], A0, B0] extends Functor[({type λ[+α]=IndexedStoreT[F, α, A0, B0]})#λ]{
private[scalaz] trait IndexedStoreTFunctorLeft[F[_], A0, B0] extends Functor[({type λ[α]=IndexedStoreT[F, α, A0, B0]})#λ]{
override def map[A, B](fa: IndexedStoreT[F, A, A0, B0])(f: A => B): IndexedStoreT[F, B, A0, B0] = fa imap f
}

private[scalaz] trait IndexedStoreTFunctorRight[F[+_], I0, A0] extends Functor[({type λ[+α]=IndexedStoreT[F, I0, A0, α]})#λ]{
private[scalaz] trait IndexedStoreTFunctorRight[F[_], I0, A0] extends Functor[({type λ[α]=IndexedStoreT[F, I0, A0, α]})#λ]{
implicit def F: Functor[F]
override def map[A, B](fa: IndexedStoreT[F, I0, A0, A])(f: A => B): IndexedStoreT[F, I0, A0, B] = fa map f
}

private[scalaz] trait IndexedStoreTContravariant[F[+_], I0, B0] extends Contravariant[({type λ[-α]=IndexedStoreT[F, I0, α, B0]})#λ] {
private[scalaz] trait IndexedStoreTContravariant[F[_], I0, B0] extends Contravariant[({type λ[α]=IndexedStoreT[F, I0, α, B0]})#λ] {
implicit def F: Functor[F]
override def contramap[A, B](fa: IndexedStoreT[F, I0, A, B0])(f: B => A): IndexedStoreT[F, I0, B, B0] = fa contramap f
}

private[scalaz] trait IndexedStoreTBifunctor[F[+_], A0] extends Bifunctor[({type λ[+α, +β]=IndexedStoreT[F, α, A0, β]})#λ] {
private[scalaz] trait IndexedStoreTBifunctor[F[_], A0] extends Bifunctor[({type λ[α, β]=IndexedStoreT[F, α, A0, β]})#λ] {
implicit def F: Functor[F]
override def bimap[A, B, C, D](fab: IndexedStoreT[F, A, A0, B])(f: A => C, g: B => D): IndexedStoreT[F, C, A0, D] = (fab bimap f)(g)
}

private[scalaz] trait StoreTCobind[F[+_], A0] extends Cobind[({type λ[+α]=StoreT[F, A0, α]})#λ] with IndexedStoreTFunctorRight[F, A0, A0] {
private[scalaz] trait StoreTCobind[F[_], A0] extends Cobind[({type λ[α]=StoreT[F, A0, α]})#λ] with IndexedStoreTFunctorRight[F, A0, A0] {
implicit def F: Cobind[F]
def cobind[A, B](fa: StoreT[F, A0, A])(f: (StoreT[F, A0, A]) => B) = fa cobind f
}

private[scalaz] trait StoreTComonad[F[+_], A0] extends Comonad[({type λ[+α]=StoreT[F, A0, α]})#λ] with StoreTCobind[F, A0] {
private[scalaz] trait StoreTComonad[F[_], A0] extends Comonad[({type λ[α]=StoreT[F, A0, α]})#λ] with StoreTCobind[F, A0] {
implicit def F: Comonad[F]
def cojoin[A](a: StoreT[F, A0, A]) = a.duplicate
def copoint[A](p: StoreT[F, A0, A]) = p.copoint

}

private[scalaz] trait StoreTComonadStore[F[+_], S] extends ComonadStore[({type λ[σ, +α]=StoreT[F, σ, α]})#λ, S] with StoreTComonad[F, S] {
private[scalaz] trait StoreTComonadStore[F[_], S] extends ComonadStore[({type λ[σ, α]=StoreT[F, σ, α]})#λ, S] with StoreTComonad[F, S] {
def pos[A](w: StoreT[F, S, A]): S = w.pos
def peek[A](s: S, w: StoreT[F, S, A]): A = w peek s
override def peeks[A](s: S => S, w: StoreT[F, S, A]): A = w peeks s
override def seek[A](s: S, w: StoreT[F, S, A]): StoreT[F, S, A] = w seek s
override def seeks[A](s: S => S, w: StoreT[F, S, A]): StoreT[F, S, A] = w seeks s
override def experiment[G[+_], A](s: S => G[S], w: StoreT[F, S, A])(implicit FG: Functor[G]): G[A] = w experiment s
override def experiment[G[_], A](s: S => G[S], w: StoreT[F, S, A])(implicit FG: Functor[G]): G[A] = w experiment s
}

private[scalaz] trait StoreTCohoist[S] extends Cohoist[({type f[g[+_], +a] = StoreT[g, S, a]})#f] {
def lower[G[+_] : Cobind, A](a: StoreT[G, S, A]) =
private[scalaz] trait StoreTCohoist[S] extends Cohoist[({type f[g[_], a] = StoreT[g, S, a]})#f] {
def lower[G[_] : Cobind, A](a: StoreT[G, S, A]) =
Cobind[G].map(a.run._1)((z: S => A) => z(a.run._2))

def cohoist[M[+_], N[+_]: Comonad](f: M ~> N) =
new (({type f[+x] = StoreT[M, S, x]})#f ~> ({type f[+x] = StoreT[N, S, x]})#f) {
def cohoist[M[_], N[_]: Comonad](f: M ~> N) =
new (({type f[x] = StoreT[M, S, x]})#f ~> ({type f[x] = StoreT[N, S, x]})#f) {
def apply[A](c: StoreT[M, S, A]) = {
val r = c.run
StoreT((f(r._1), r._2))
@@ -5,7 +5,7 @@ import Id._
/**
* StreamT monad transformer.
*/
sealed class StreamT[M[+_], +A](val step: M[StreamT.Step[A, StreamT[M, A]]]) {
sealed class StreamT[M[_], A](val step: M[StreamT.Step[A, StreamT[M, A]]]) {

import StreamT._

@@ -16,7 +16,7 @@ sealed class StreamT[M[+_], +A](val step: M[StreamT.Step[A, StreamT[M, A]]]) {
, done = M.point(None)
))

def ::[AA >: A](a: => AA)(implicit M: Applicative[M]): StreamT[M, AA] = StreamT[M, AA](M.point(Yield(a, this)))
def ::(a: => A)(implicit M: Applicative[M]): StreamT[M, A] = StreamT[M, A](M.point(Yield(a, this)))

def isEmpty(implicit M: Monad[M]): M[Boolean] = M.map(uncons)(!_.isDefined)

@@ -26,7 +26,7 @@ sealed class StreamT[M[+_], +A](val step: M[StreamT.Step[A, StreamT[M, A]]]) {

def tailM(implicit M: Monad[M]): M[StreamT[M, A]] = M.map(uncons)(_.getOrElse(sys.error("tailM: empty StreamT"))._2)

def trans[N[+_]](t: M ~> N)(implicit M: Monad[M], N: Functor[N]): StreamT[N, A] =
def trans[N[_]](t: M ~> N)(implicit M: Monad[M], N: Functor[N]): StreamT[N, A] =
StreamTHoist.hoist(t).apply(this)

def filter(p: A => Boolean)(implicit m: Functor[M]): StreamT[M, A] = stepMap {
@@ -64,7 +64,7 @@ sealed class StreamT[M[+_], +A](val step: M[StreamT.Step[A, StreamT[M, A]]]) {
)
}

def ++[AA >: A](bs: => StreamT[M, AA])(implicit m: Functor[M]): StreamT[M, AA] = stepMap {
def ++(bs: => StreamT[M, A])(implicit m: Functor[M]): StreamT[M, A] = stepMap {
_( yieldd = (a, as) => Yield(a, as ++ bs)
, skip = as => Skip(as ++ bs)
, done = Skip(bs)
@@ -148,43 +148,43 @@ sealed class StreamT[M[+_], +A](val step: M[StreamT.Step[A, StreamT[M, A]]]) {
//

trait StreamTInstances2 {
implicit def StreamTFunctor[F[+_]](implicit F0: Functor[F]): Functor[({type λ[α] = StreamT[F, α]})#λ] = new StreamTFunctor[F] {
implicit def StreamTFunctor[F[_]](implicit F0: Functor[F]): Functor[({type λ[α] = StreamT[F, α]})#λ] = new StreamTFunctor[F] {
implicit def F: Functor[F] = F0
}

implicit def StreamTSemigroup[F[+_], A](implicit F0: Functor[F]): Semigroup[StreamT[F, A]] = new StreamTSemigroup[F, A] {
implicit def StreamTSemigroup[F[_], A](implicit F0: Functor[F]): Semigroup[StreamT[F, A]] = new StreamTSemigroup[F, A] {
implicit def F: Functor[F] = F0
}
}

trait StreamTInstances1 extends StreamTInstances2 {

implicit def StreamTMonoid[F[+_], A](implicit F0: Applicative[F]): Monoid[StreamT[F, A]] = new StreamTMonoid[F, A] {
implicit def StreamTMonoid[F[_], A](implicit F0: Applicative[F]): Monoid[StreamT[F, A]] = new StreamTMonoid[F, A] {
implicit def F: Applicative[F] = F0
}
}

trait StreamTInstances0 extends StreamTInstances1 {
implicit def StreamTMonad[F[+_]](implicit F0: Monad[F]): Monad[({type λ[α] = StreamT[F, α]})#λ] = new StreamTMonad[F] {
implicit def StreamTMonad[F[_]](implicit F0: Monad[F]): Monad[({type λ[α] = StreamT[F, α]})#λ] = new StreamTMonad[F] {
implicit def F: Monad[F] = F0
}
}

trait StreamTInstances extends StreamTInstances0 {
implicit def StreamTMonadPlus[F[+_]](implicit F0: Monad[F]): MonadPlus[({type λ[α] = StreamT[F, α]})#λ] = new StreamTMonadPlus[F] {
implicit def StreamTMonadPlus[F[_]](implicit F0: Monad[F]): MonadPlus[({type λ[α] = StreamT[F, α]})#λ] = new StreamTMonadPlus[F] {
implicit def F: Monad[F] = F0
}
implicit def StreamTEqual[F[+_], A](implicit E: Equal[F[Stream[A]]], F: Monad[F]): Equal[StreamT[F, A]] = E.contramap((_: StreamT[F, A]).toStream)
implicit def StreamTShow[F[+_], A](implicit E: Show[F[Stream[A]]], F: Monad[F]): Show[StreamT[F, A]] = Contravariant[Show].contramap(E)((_: StreamT[F, A]).toStream)
implicit def StreamTEqual[F[_], A](implicit E: Equal[F[Stream[A]]], F: Monad[F]): Equal[StreamT[F, A]] = E.contramap((_: StreamT[F, A]).toStream)
implicit def StreamTShow[F[_], A](implicit E: Show[F[Stream[A]]], F: Monad[F]): Show[StreamT[F, A]] = Contravariant[Show].contramap(E)((_: StreamT[F, A]).toStream)
implicit def StreamTHoist: Hoist[StreamT] = new StreamTHoist {}
}

object StreamT extends StreamTInstances {
def apply[M[+_], A](step: M[Step[A, StreamT[M, A]]]): StreamT[M, A] = new StreamT[M, A](step)
def apply[M[_], A](step: M[Step[A, StreamT[M, A]]]): StreamT[M, A] = new StreamT[M, A](step)

def empty[M[+_], A](implicit M: Applicative[M]): StreamT[M, A] = new StreamT[M, A](M point Done)
def empty[M[_], A](implicit M: Applicative[M]): StreamT[M, A] = new StreamT[M, A](M point Done)

def fromStream[M[+_], A](mas: M[Stream[A]])(implicit M: Applicative[M]): StreamT[M, A] = {
def fromStream[M[_], A](mas: M[Stream[A]])(implicit M: Applicative[M]): StreamT[M, A] = {
def loop(as: Stream[A]): Step[A, StreamT[M, A]] = as match {
case head #:: tail => Yield(head, apply(M.point(loop(tail))))
case _ => Done
@@ -193,7 +193,7 @@ object StreamT extends StreamTInstances {
apply[M, A](M.map(mas)(loop))
}

def unfoldM[M[+_],A,B](start: B)(f: B => M[Option[(A,B)]])(implicit M: Functor[M]): StreamT[M,A] =
def unfoldM[M[_],A,B](start: B)(f: B => M[Option[(A,B)]])(implicit M: Functor[M]): StreamT[M,A] =
StreamT[M,A](M.map(f(start)) {
case Some((a, b)) => Yield(a, unfoldM(b)(f))
case None => Done
@@ -206,13 +206,13 @@ object StreamT extends StreamTInstances {
unfold(s)(stepper)
}

def wrapEffect[M[+_]:Functor,A](m: M[StreamT[M,A]]): StreamT[M,A] = StreamT(Functor[M].map(m)(Skip(_)))
def wrapEffect[M[_]:Functor,A](m: M[StreamT[M,A]]): StreamT[M,A] = StreamT(Functor[M].map(m)(Skip(_)))

abstract sealed class Step[+A, +S] {
def apply[Z](yieldd: (A, => S) => Z, skip: => S => Z, done: => Z): Z
}

def runStreamT[S,A](stream : StreamT[({type λ[+X] = State[S,X]})#λ,A], s0: S): StreamT[Id,A] =
def runStreamT[S,A](stream : StreamT[({type λ[X] = State[S,X]})#λ,A], s0: S): StreamT[Id,A] =
StreamT[Id,A]({
val (s1, sa) = stream.step(s0)
sa((a, as) => Yield(a, runStreamT(as, s1)),
@@ -246,33 +246,33 @@ object StreamT extends StreamTInstances {
// Implementation traits for type class instances
//

private[scalaz] trait StreamTFunctor[F[+_]] extends Functor[({type λ[α] = StreamT[F, α]})#λ] {
private[scalaz] trait StreamTFunctor[F[_]] extends Functor[({type λ[α] = StreamT[F, α]})#λ] {
implicit def F: Functor[F]

override def map[A, B](fa: StreamT[F, A])(f: A => B): StreamT[F, B] = fa map f
}

private[scalaz] trait StreamTSemigroup[F[+_], A] extends Semigroup[StreamT[F, A]] {
private[scalaz] trait StreamTSemigroup[F[_], A] extends Semigroup[StreamT[F, A]] {
implicit def F: Functor[F]

def append(f1: StreamT[F, A], f2: => StreamT[F, A]): StreamT[F, A] = f1 ++ f2
}

private[scalaz] trait StreamTMonoid[F[+_], A] extends Monoid[StreamT[F, A]] with StreamTSemigroup[F, A] {
private[scalaz] trait StreamTMonoid[F[_], A] extends Monoid[StreamT[F, A]] with StreamTSemigroup[F, A] {
implicit def F: Applicative[F]

def zero: StreamT[F, A] = StreamT.empty[F, A]
}

private[scalaz] trait StreamTMonad[F[+_]] extends Monad[({type λ[α] = StreamT[F, α]})#λ] with StreamTFunctor[F] {
private[scalaz] trait StreamTMonad[F[_]] extends Monad[({type λ[α] = StreamT[F, α]})#λ] with StreamTFunctor[F] {
implicit def F: Monad[F]

def point[A](a: => A): StreamT[F, A] = a :: StreamT.empty[F, A]

def bind[A, B](fa: StreamT[F, A])(f: A => StreamT[F, B]): StreamT[F, B] = fa flatMap f
}

private[scalaz] trait StreamTMonadPlus[F[+_]] extends MonadPlus[({type λ[α] = StreamT[F, α]})#λ] with StreamTMonad[F] {
private[scalaz] trait StreamTMonadPlus[F[_]] extends MonadPlus[({type λ[α] = StreamT[F, α]})#λ] with StreamTMonad[F] {
implicit def F: Monad[F]

def empty[A]: StreamT[F, A] = StreamT.empty
@@ -283,11 +283,11 @@ private[scalaz] trait StreamTMonadPlus[F[+_]] extends MonadPlus[({type λ[α] =
private[scalaz] trait StreamTHoist extends Hoist[StreamT] {
import StreamT._

implicit def apply[G[+_] : Monad]: Monad[({type λ[α] = StreamT[G, α]})#λ] = StreamTMonad[G]
implicit def apply[G[_] : Monad]: Monad[({type λ[α] = StreamT[G, α]})#λ] = StreamTMonad[G]

def liftM[G[+_], A](a: G[A])(implicit G: Monad[G]): StreamT[G, A] = StreamT[G, A](G.map(a)(Yield(_, empty)))
def liftM[G[_], A](a: G[A])(implicit G: Monad[G]): StreamT[G, A] = StreamT[G, A](G.map(a)(Yield(_, empty)))

def hoist[M[+_], N[+_]](f: M ~> N)(implicit M: Monad[M]): ({type f[x] = StreamT[M, x]})#f ~> ({type f[x] = StreamT[N, x]})#f =
def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]): ({type f[x] = StreamT[M, x]})#f ~> ({type f[x] = StreamT[N, x]})#f =
new (({type f[x] = StreamT[M, x]})#f ~> ({type f[x] = StreamT[N, x]})#f) {
def apply[A](a: StreamT[M, A]): StreamT[N, A] = StreamT[N, A](f(M.map(a.step)(
_( yieldd = (a, as) => Yield(a, hoist(f) apply as)