FAQ Database Discussion Community

What are the applicative functor laws in terms of pure and liftA2?

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...

Returning NonEmptyList `sealed trait`'s w/ scalaz.Validation

scala,scalaz,applicative
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...

Why is f <\$> g <\$> x equivalent to (f . g) <\$> x although <\$> is not right-associative?

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:...

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?...

Understanding Right Apply

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] ...

How to correctly error out in JSON parsing with Data.Aeson

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...

Why does the Alternative typeclass need to be a sub-class of Control.Applicative

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...

Applicative that increments the environment in haskell

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...

Why are instances matched only by their heads?

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...

ZipList with Scalaz

list,scala,scalaz,applicative
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:...

Easier way to apply multiple arguments in Haskell

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)...

How to fix implicit conversion to Applicative?

scala,scalaz,applicative
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)...

How to implement Future as Applicative in Scala?

scala,concurrency,functional-programming,future,applicative
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...

Why is there not 'Alternative' instance for 'Control.Applicative.Const'

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...

Must I implement Applicative and Functor to implement a Monad

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...

How to define <*> for Option[List[_]] n Scala

scala,applicative
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...