Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

UNSTABLE: causing StackOverflowError at compilation

  • Loading branch information...
commit 95338d00c0e83e5afa7d72cdbe8e45d67e18e733 1 parent 6f42712
Yorick authored

Showing 1 changed file with 166 additions and 14 deletions. Show diff stats Hide diff stats

  1. +166 14 core/src/main/scala/scalaz/ValidationT.scala
180 core/src/main/scala/scalaz/ValidationT.scala
@@ -65,7 +65,7 @@ sealed trait ValidationT[F[_], E, A] {
65 65 def foldRight[B](z: => B)(f: (A, => B) => B)(implicit F: Foldable[F]): B =
66 66 F.foldRight(run, z)(_.foldRight(_)(f))
67 67
68   - def ap[B, EE >: E](f: => ValidationT[F, EE, A => B])(implicit F: Monad[F], E: Semigroup[EE]): ValidationT[F, EE, B] =
  68 + def ap[B, EE >: E](f: => ValidationT[F, EE, A => B])(implicit F: Applicative[F], E: Semigroup[EE]): ValidationT[F, EE, B] =
69 69 ValidationT(F.lift2[Validation[E, A], Validation[EE, A => B], Validation[EE, B]](_.ap(_))(run, f.run))
70 70
71 71 def bimap[C, D](f: E => C, g: A => D)(implicit F: Functor[F]): ValidationT[F, C, D] =
@@ -75,37 +75,189 @@ sealed trait ValidationT[F[_], E, A] {
75 75 G.map(F.traverse(run)(_.bitraverse(f, g)))(ValidationT(_))
76 76 }
77 77
78   -object ValidationT {
79   - def apply[M[_], E, A](m: M[Validation[E, A]]) = new ValidationT[M, E, A] {
  78 +object ValidationT extends ValidationTFunctions with ValidationTInstances {
  79 + def apply[F[_], E, A](m: F[Validation[E, A]]) = new ValidationT[F, E, A] {
80 80 def run = m
81 81 }
82   -
83   - sealed trait FailProjectionT[M[_], E, A]{
  82 +
  83 + sealed trait FailProjectionT[F[_], E, A]{
84 84 self =>
85 85
86   - val validationT: ValidationT[M, E, A]
  86 + val validationT: ValidationT[F, E, A]
87 87
88   - def toOption(implicit M: Functor[M]): OptionT[M, E] =
  88 + def toOption(implicit M: Functor[F]): OptionT[F, E] =
89 89 OptionT(M.map(validationT.run)(_.fail.toOption))
90 90
91   - def |||[EE >: E](f: A => EE)(implicit M: Functor[M]): M[EE] =
  91 + def |||[EE >: E](f: A => EE)(implicit M: Functor[F]): F[EE] =
92 92 M.map(validationT.run)(_.fail.|||(f))
93 93
94   - def getOrElse[EE >: E](f: => EE)(implicit M: Functor[M]): M[EE] = |||(_ => f)
  94 + def getOrElse[EE >: E](f: => EE)(implicit M: Functor[F]): F[EE] = |||(_ => f)
95 95
96   - def |[EE >: E](f: => EE)(implicit M: Functor[M]): M[EE] = getOrElse(f)
  96 + def |[EE >: E](f: => EE)(implicit M: Functor[F]): F[EE] = getOrElse(f)
97 97
98   - def pointT[F[_], EE >: E, AA >: A](implicit M: Functor[M], P: Pointed[F]): ValidationT[M, F[EE], AA] =
  98 + def pointT[G[_], EE >: E, AA >: A](implicit M: Functor[F], G: Pointed[G]): ValidationT[F, G[EE], AA] =
99 99 ValidationT(M.map(validationT.run)(_.fail.point))
100 100
101   - def exists(f: E => Boolean)(implicit M: Functor[M]): M[Boolean] =
  101 + def exists(f: E => Boolean)(implicit M: Functor[F]): F[Boolean] =
102 102 M.map(validationT.run)(_.fail.exists(f))
103 103
104   - def forall(f: E => Boolean)(implicit M: Functor[M]): M[Boolean] =
  104 + def forall(f: E => Boolean)(implicit M: Functor[F]): F[Boolean] =
105 105 M.map(validationT.run)(_.fail.forall(f))
106 106
107   - def map[B](f: E => B)(implicit M: Functor[M]): FailProjectionT[M, B, A] = new FailProjectionT[M, B, A]{
  107 + def map[B](f: E => B)(implicit M: Functor[F]): FailProjectionT[F, B, A] = new FailProjectionT[F, B, A]{
108 108 val validationT = ValidationT(M.map(self.validationT.run)(_.fail.map(f).validation))
109 109 }
110 110 }
  111 +}
  112 +
  113 +trait ValidationTFunctions {
  114 + def successT[F[_], E, A](a: => A)(implicit F: Pointed[F]): ValidationT[F, E, A] =
  115 + ValidationT(F.point(Validation.success(a)))
  116 +
  117 + def failureT[F[_], E, A](e: => E)(implicit F: Pointed[F]): ValidationT[F, E, A] =
  118 + ValidationT(F.point(Validation.failure(e)))
  119 +
  120 + import Isomorphism.{IsoFunctorTemplate, IsoBiFunctorTemplate}
  121 +
  122 + implicit def validationTFailProjectionTEIso2[F[_], E]: IsoFunctorTemplate[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] = new IsoFunctorTemplate[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] {
  123 + def to[A](fa: ValidationT.FailProjectionT[F, E, A]): ValidationT[F, E, A] = fa.validationT
  124 + def from[A](ga: ValidationT[F, E, A]): ValidationT.FailProjectionT[F, E, A] = ga.fail
  125 + }
  126 + implicit def validationTFailProjectionTIso2[F[_]]: IsoBiFunctorTemplate[({type λ[α, β]=ValidationT.FailProjectionT[F, α, β]})#λ, ({type λ[α, β]=ValidationT[F, α, β]})#λ] = new IsoBiFunctorTemplate[({type λ[α, β]=ValidationT.FailProjectionT[F, α, β]})#λ, ({type λ[α, β]=ValidationT[F, α, β]})#λ] {
  127 + def to[E, A](fa: ValidationT.FailProjectionT[F, E, A]): ValidationT[F, E, A] = fa.validationT
  128 + def from[E, A](ga: ValidationT[F, E, A]): ValidationT.FailProjectionT[F, E, A] = ga.fail
  129 + }
  130 +}
  131 +
  132 +trait ValidationTInstances3 {
  133 + implicit def validationTFunctor[F[_], E](implicit F0: Functor[F]): ValidationTFunctor[F, E] = new ValidationTFunctor[F, E] {
  134 + implicit def F = F0
  135 + }
  136 + implicit def FailProjectionTFunctor[F[_], E](implicit F0: Functor[F]): IsomorphismFunctor[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] = new IsomorphismFunctor[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] {
  137 + implicit def G = validationTFunctor[F, E]
  138 + def iso = ValidationT.validationTFailProjectionTIso2[F]
  139 + }
  140 +}
  141 +
  142 +trait ValidationTInstances2 extends ValidationTInstances3 {
  143 + implicit def validationTPointed[F[_], E](implicit F0: Pointed[F]): ValidationTPointed[F, E] = new ValidationTPointed[F, E] {
  144 + implicit def F = F0
  145 + }
  146 + implicit def validationTFailProjectionTPointed[F[_], E](implicit F0: Pointed[F]): IsomorphismPointed[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] = new IsomorphismPointed[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] {
  147 + implicit def G = validationTPointed[F, E]
  148 + def iso = ValidationT.validationTFailProjectionTIso2[F]
  149 + }
  150 +}
  151 +
  152 +trait ValidationTInstances1 extends ValidationTInstances2 {
  153 + implicit def validationTApplicative[F[_], E](implicit F0: Applicative[F]): ValidationTApplicative[F, E] = new ValidationTApplicative[F, E] {
  154 + implicit def F = F0
  155 + }
  156 + implicit def validationTFailProjectionTApplicative[F[_], E](implicit F0: Applicative[F]): IsomorphismApplicative[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] = new IsomorphismApplicative[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] {
  157 + implicit def G = validationTApplicative[F, E]
  158 + def iso = ValidationT.validationTFailProjectionTIso2[F]
  159 + }
  160 +}
  161 +
  162 +trait ValidationTInstances0 extends ValidationTInstances1 {
  163 + implicit def validationTBiFunctor[F[_]](implicit F0: Functor[F]): ValidationTBiFunctor[F] = new ValidationTBiFunctor[F] {
  164 + implicit def F = F0
  165 + }
  166 + implicit def validationTFailProjectionTBiFunctor[F[_]](implicit F0: Functor[F]): IsomorphismBiFunctor[({type λ[α, β]=ValidationT.FailProjectionT[F, α, β]})#λ, ({type λ[α, β]=ValidationT[F, α, β]})#λ] = new IsomorphismBiFunctor[({type λ[α, β]=ValidationT.FailProjectionT[F, α, β]})#λ, ({type λ[α, β]=ValidationT[F, α, β]})#λ] {
  167 + implicit def G = validationTBiFunctor[F]
  168 + def iso = ValidationT.validationTFailProjectionTEIso2[F]
  169 + }
  170 +
  171 + implicit def validationTMonad[F[_], E](implicit F0: Monad[F]): ValidationTMonad[F, E] = new ValidationTMonad[F, E] {
  172 + implicit def F = F0
  173 + }
  174 + implicit def validationTFailProjectionTMonad[F[_], E](implicit F0: Monad[F]): IsomorphismMonad[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] = new IsomorphismMonad[({type λ[α] = ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α] = ValidationT[F, E, α]})#λ] {
  175 + implicit def G = validationTMonad[F, E]
  176 + def iso = ValidationT.validationTFailProjectionTIso2[F]
  177 + }
  178 + implicit def validationTFoldable[F[_], E](implicit F0: Foldable[F]): ValidationTFoldable[F, E] = new ValidationTFoldable[F, E] {
  179 + implicit def F = F0
  180 + }
  181 +}
  182 +
  183 +trait ValidationTInstances extends ValidationTInstances0 {
  184 + implicit def validationTBiTraverse[F[_]](implicit F0: Traverse[F]): ValidationTBiTraverse[F] = new ValidationTBiTraverse[F] {
  185 + implicit def F = F0
  186 + }
  187 + implicit def validationTFailProjectionTBiTraverse[F[_]](implicit F0: Traverse[F]): IsomorphismBiTraverse[({type λ[α, β] = ValidationT.FailProjectionT[F, α, β]})#λ, ({type λ[α, β] = ValidationT[F, α, β]})#λ] = new IsomorphismBiTraverse[({type λ[α, β] = ValidationT.FailProjectionT[F, α, β]})#λ, ({type λ[α, β] = ValidationT[F, α, β]})#λ] {
  188 + implicit def G = validationTBiTraverse[F]
  189 + def iso = ValidationT.validationTFailProjectionTIso2[F]
  190 + }
  191 +
  192 + implicit def validationTTraverse[F[_], E](implicit F0: Traverse[F]): ValidationTTraverse[F, E] = new ValidationTTraverse[F, E] {
  193 + implicit def F = F0
  194 + }
  195 +
  196 + implicit def validationTFailProjectionTTraverse[F[_], E](implicit F0: Traverse[F]): IsomorphismTraverse[({type λ[α]=ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α]=ValidationT[F, E, α]})#λ] = new IsomorphismTraverse[({type λ[α]=ValidationT.FailProjectionT[F, E, α]})#λ, ({type λ[α]=ValidationT[F, E, α]})#λ] {
  197 + implicit def G = validationTTraverse[F, E]
  198 + def iso = ValidationT.validationTFailProjectionTEIso2[F, E]
  199 + }
  200 +
  201 + implicit def validationTMonadTrans[E]: MonadTrans[({type λ[α[_], β] = ValidationT[α, E, β]})#λ] = new ValidationTMonadTrans[E] {}
  202 +}
  203 +
  204 +trait ValidationTFunctor[F[_], E] extends Functor[({type λ[α]=ValidationT[F, E, α]})#λ] {
  205 + implicit def F: Functor[F]
  206 +
  207 + override def map[A, B](fa: ValidationT[F, E, A])(f: (A) => B): ValidationT[F, E, B] = fa map f
  208 +}
  209 +
  210 +trait ValidationTPointed[F[_], E] extends Pointed[({type λ[α]=ValidationT[F, E, α]})#λ] with ValidationTFunctor[F, E] {
  211 + implicit def F: Pointed[F]
  212 +
  213 + def point[A](a: => A): ValidationT[F, E, A] = ValidationT.successT(a)
  214 +}
  215 +
  216 +trait ValidationTApplicative[F[_], E] extends Applicative[({type λ[α]=ValidationT[F, E, α]})#λ] with ValidationTPointed[F, E] {
  217 + implicit def F: Applicative[F]
  218 + implicit def E: Semigroup[E]
  219 +
  220 + override def ap[A, B](fa: => ValidationT[F, E, A])(f: => ValidationT[F, E, (A) => B]): ValidationT[F, E, B] = fa ap f
  221 +}
  222 +
  223 +trait ValidationTMonad[F[_], E] extends Monad[({type λ[α]=ValidationT[F, E, α]})#λ] with ValidationTApplicative[F, E] {
  224 + implicit def F: Monad[F]
  225 +
  226 + def bind[A, B](fa: ValidationT[F, E, A])(f: (A) => ValidationT[F, E, B]): ValidationT[F, E, B] = fa flatMap f
  227 +}
  228 +
  229 +trait ValidationTFoldable[F[_], E] extends Foldable.FromFoldr[({type λ[α]=ValidationT[F, E, α]})#λ] {
  230 + implicit def F: Foldable[F]
  231 +
  232 + override def foldRight[A, B](fa: ValidationT[F, E, A], z: => B)(f: (A, => B) => B): B = fa.foldRight(z)(f)
  233 +}
  234 +
  235 +trait ValidationTTraverse[F[_], E] extends Traverse[({type λ[α]=ValidationT[F, E, α]})#λ] with ValidationTFoldable[F, E] {
  236 + implicit def F: Traverse[F]
  237 +
  238 + def traverseImpl[G[_]: Applicative, A, B](fa: ValidationT[F, E, A])(f: (A) => G[B]): G[ValidationT[F, E, B]] = fa traverse f
  239 +}
  240 +
  241 +trait ValidationTBiFunctor[F[_]] extends BiFunctor[({type λ[α, β]=ValidationT[F, α, β]})#λ] {
  242 + implicit def F: Functor[F]
  243 +
  244 + override def bimap[A, B, C, D](fab: ValidationT[F, A, B])(f: (A) => C, g: (B) => D): ValidationT[F, C, D] = fab.bimap(f, g)
  245 +}
  246 +
  247 +trait ValidationTBiTraverse[F[_]] extends BiTraverse[({type λ[α, β] = ValidationT[F, α, β]})#λ] with ValidationTBiFunctor[F] {
  248 + implicit def F: Traverse[F]
  249 +
  250 + def bitraverse[G[_] : Applicative, A, B, C, D](fab: ValidationT[F, A, B])
  251 + (f: (A) => G[C], g: (B) => G[D]): G[ValidationT[F, C, D]] =
  252 + fab.bitraverse(f, g)
  253 +}
  254 +
  255 +trait ValidationTMonadTrans[A] extends MonadTrans[({type λ[α[_], β] = ValidationT[α, A, β]})#λ] {
  256 + def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]) = new (({type λ[α] = ValidationT[M, A, α]})#λ ~> ({type λ[α] = ValidationT[N, A, α]})#λ) {
  257 + def apply[B](mb: ValidationT[M, A, B]): ValidationT[N, A, B] = ValidationT(f.apply(mb.run))
  258 + }
  259 +
  260 + def liftM[M[_], B](mb: M[B])(implicit M: Monad[M]): ValidationT[M, A, B] = ValidationT(M.map(mb)(Success[A, B](_)))
  261 +
  262 + implicit def apply[M[_] : Monad]: Monad[({type λ[α] = ValidationT[M, A, α]})#λ] = ValidationT.validationTMonad
111 263 }

0 comments on commit 95338d0

Please sign in to comment.
Something went wrong with that request. Please try again.