FAQ Database Discussion Community


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

haskell,applicative,monoids
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...

Applicative that increments the environment in haskell

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

How to compose functions that return Validation?

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

Understanding Right Apply

haskell,applicative
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 does GHCi print partially-applied values created from “pure”?

haskell,typeclass,applicative
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...

Must I implement Applicative and Functor to implement a Monad

haskell,monads,functor,applicative,fam-proposal
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 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)...

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

haskell,applicative,monoids
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...

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 f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative?

haskell,syntax,infix-notation,applicative,infix-operator
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:...

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

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

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

Easier way to apply multiple arguments in Haskell

haskell,functional-programming,composition,functor,applicative
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 correctly error out in JSON parsing with Data.Aeson

parsing,haskell,monads,applicative,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 are instances matched only by their heads?

haskell,monads,typeclass,functor,applicative
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...

Applicative style understanding

haskell,applicative
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?...

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

Why WrappedMonad and WrappedArrow?

haskell,applicative
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?...