I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = Notheeng Juust x >>= f = f x fail _ = Notheeng This should be...

Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative? (This kind of equivalence is valid in a popular idiom with plain $, but currently $ is right-associative!) <*> has the same associativity and precedence as <$>, but behaves differently! Example:...

This is a followup to my previous question with an example found on the Internet. Suppose I define a typeclass Applicative as follows: trait Functor[T[_]]{ def map[A,B](f:A=>B, ta:T[A]):T[B] } trait Applicative[T[_]] extends Functor[T] { def unit[A](a:A):T[A] def ap[A,B](tf:T[A=>B], ta:T[A]):T[B] } I can define an instance of Applicative for List object...

Suppose I need to run two concurrent computations, wait for both of them, and then combine their results. More specifically, I need to run f1: X1 => Y1 and f2: X2 => Y2 concurrently and then call f: (Y1, Y2) => Y to finally get a value of Y. I...

Given the following code: object Person { override def makePerson(name: String, age: Int, gender: Gender): Validation[NonEmptyList[InvalidPersonError], Person] = (validateName(name) |@| validateAge(age)) {_ ++ _} private def validateName(name: String): Validation[NonEmptyList[InvalidPersonError], String] = if(name.nonEmpty) name.successNel else InvalidName.failureNel private def validateAge(age: Int): Validation[NonEmptyList[InvalidPersonError], Int] = if(age >= 0) age.successNel else InvalidAge.failureNel sealed trait...

I'll start by introducing a concrete problem (StackOverflow guys like that). Say you define a simple type data T a = T a This type is a Functor, Applicative and a Monad. Ignoring automatic deriving, to get those instances you have to write each one of them, even though Monad...

Suppose I have a list of numbers and list of functions to apply to numbers: val xs: List[Int] = List(1, 2, 3) val fs: List[Int => Int] = List(f1, f2, f3) Now I would like to use an Applicative to apply f1 to 1, f2 to 2, etc. val ys:...

Haskell provides a standard typeclass 'Alternative' that effectively provides the <|> operator for any type that is also an Applicative. As I understand it Alternative is considered a Monoid on Applicative's, however the <|> operator seems to make complete sense in a lot of types that aren't Applicative Functors as...

Given value f with type :: Applicative f => f (a -> b -> c), What's the best way to map arguments to the inner function. So far I've found the following: (\x -> x a b) <$> f (flip ($ a) b) <$> f ($ b) <$> ($ a)...

This is a follow-up to my previous question I would like to generalize the implicit conversion toApplicative, which adds method <*> to any M[A=>B], where M is Applicative (i.e. there is a typeclass instance Applicative[M]) implicit def toApplicative[M[_], A, B](mf: M[A=>B])(implicit apm: Applicative[M]) = new { def<*>(ma: M[A]) = apm.ap(ma)(mf)...

This is a follow-up to my previous question Suppose I have two validating functions that return either the input if it is valid or the error messages if it is not. type Status[A] = ValidationNel[String, A] val isPositive: Int => Status[Int] = x => if (x > 0) x.success else...

For a List, why does right apply (*>) behave as repeating and appending the second argument n times, where n is the length of the first argument? ghci> [1,2,3] *> [4,5] [4,5,4,5,4,5] ...

There is an instance Monoid a => Monoid (Const a b) for the Const functor from Control.Applicative. There is also an instance Monoid m => Applicative (Const m). I would therefore expect that there is also an instance Monoid m => Alternative (Const m) that coincides with the one for...

I've been playing around with Applicative instances in order to figure out how they work. However, I honestly don't understand this behavior. If I define my own datatype, then apply pure to it with no other arguments, nothing prints out, but it errors if I try to apply something to...

I'm playing around with formulating Applicative in terms of pure and liftA2 (so that (<*>) = liftA2 id becomes a derived combinator). I can think of a bunch of candidate laws, but I'm not sure what the minimal set would be. f <$> pure x = pure (f x) f...

Why does the WrappedMonad and WrappedArrow types exist? Is it because Monads were not Applicative? Given that WrappedArrow exists, should the instance Arrow a => Applicative (Arrow a b) simply be built into the Haskell itself the same way that Applicative is now a superclass of Monad?...

I'm wondering if there is an Applicative that can track how many applicative operations have occurred. I tried to implement it as follows: import Control.Applicative main :: IO () main = print $ run 1 $ (,,,) <$> FromInt id <*> FromInt id <*> FromInt id <*> FromInt id data...

My type and correponding FromJSON implementation as listed below. The nonEmpty turns a List into a Maybe NonEmpty and I'm trying to correctly deal with the case where the List is indeed empty and I have to abort the parsing. This parsing is actually done inside of parseJsonBody, which means...

I would like to write something as the following: (+) <$> Just 3 <*> Just 5 <*>' (+) <*> Just 6 However the problem is that I need to somehow flip <*>. What is the idiomatic way in Haskell to do the type of chaining I'm trying?...