Questions tagged [applicative]

In Haskell, Applicative functors are functors such that two functorial values can be combined into one, whilst the two values inside are combined via a functional application. An applicative functor has more structure than a functor but less than a monad.

In Haskell, applicative functors are functors such that two functorial values (of type Applicative f => f a) can be combined into one, and the two values inside will be combined via a functional application (so the types "on the inside" must be compatible).

Definition

 class (Functor f) => Applicative f where
   pure  :: a -> f a                          -- injection
   (<*>) :: f (a -> b) -> f a -> f b          -- combination

The pure function lifts any value into the functor. (<*>) changes a functorial function into a function over functorial values. Applicative functor should satisfy some laws:

 pure id <*> v = v                            -- Identity
 pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- Composition
 pure f <*> pure x = pure (f x)               -- Homomorphism
 u <*> pure y = pure ($ y) <*> u              -- Interchange

And the Functor instance should satisfy the following law:

 fmap f x = pure f <*> x                      -- Fmap
572 questions
1
vote
2 answers

Why does not sequence work with List of Validations

I think I understand what sequence is. I am wondering why it does not work with List[ValidationNel]. For instance: The sequence works fine with List[Option]] scala> val os = List(1.some, 2.some) os: List[Option[Int]] = List(Some(1), Some(2)) scala>…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

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

Given the following code: object Person { override def makePerson(name: String, age: Int, gender: Gender): Validation[NonEmptyList[InvalidPersonError], Person] = (validateName(name) |@| validateAge(age)) {_ ++ _} …
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
1
vote
1 answer

Applicative style understanding

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?
user3139545
  • 6,882
  • 13
  • 44
  • 87
1
vote
0 answers

How to define <*> for applicative parser?

Suppose we define parser as a function type Parser[A] = String => List(A, String) The parser takes an input string and yields a sequence of pairs. Each pair consists of the parsing result and unconsumed part of the input As I understand, we can…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

Example of a matrix as Applicative functor

I already asked a similar question but it was not clear enough, so I decided to rephrase it. I know that a matrix is an applicative functor but not a monad. I am wondering if there is a simple and practical example of <*> for matrices.
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
2 answers

What are advantages of ApplicativeBuilder?

Applicative provides "operator" <*>, which I can use as follows: val f: (Int, Int) => Int = {(x, y) => x + y} 1.some <*> (2.some <*> f.curried.some) In addition to that scalaz provides ApplicativeBuilder: (1.some |@| 2.some)(f) What are advantages…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

Understanding Applicative Functor in Scala

Suppose I need to write some functions to invoke a few REST APIs: api1, api2, api3. def api1(url: Url) = ??? def api2(url: Url) = ??? def api3(url: Url) = ??? Suppose for simplicity that I use my own simplified class Url: case class Url(host:…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
2 answers

Interaction between functor, applicative and Monad

I am totally new to Haskell and I am trying to understand better how functor, applicative and monad work together. Below in my example: import Control.Monad import Control.Applicative data FooBar a = Foo a | Bar a deriving (Show) myf :: FooBar Int…
Randomize
  • 8,651
  • 18
  • 78
  • 133
1
vote
1 answer

How to apply function to list of options in Scala?

Suppose I have a function f: (Int, Int, Int) => String. I can easily apply it to three parameters of type Option[Int]: def foo(f: (Int, Int, Int) => String, ox: Option[Int], oy: Option[Int], oz: Option[Int]): Option[String] =…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

Get indices of Applicative Traversable without dummy

Let's say I have some v, which is both Applicative and also Traversable. How can I get a v with the indices of v? For a concrete example, consider V3 from Linear. I want V3 0 1 2. One way is to use mapAccumL with a dummy, for example: snd $…
yong
  • 3,583
  • 16
  • 32
1
vote
2 answers

Understanding the signature of <$>

I have the applicative <$> operator more or less figured out, but I can't understand the signature I'm getting with the following example: ghci> let f x y z = x + y + z -- f::Num a => a -> a -> a -> a ghci> f <$> Just 2 <*> Just 3 <*> Just 4…
Chirlo
  • 5,989
  • 1
  • 29
  • 45
1
vote
2 answers

How to define Applicative in this case?

This is a follow-up to my previous quesion. I am reading this post again to understand the design described there. They introduce a Job monad similar to Haxl. Job[T] is a data fetch operation that fetches data of type T (and may consist of other…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
3 answers

Extracting nested monadic result: m (m a) -> m a

I have a function parseArgs :: [String] -> StdGen -> IO () which selects the function to run. The main looks like main = parseArgs <$> getArgs <*> getStdGen >>= id The problem I have, parseArgs <$> getArgs <*> getStdGen is of type IO (IO ()),…
seequ
  • 456
  • 2
  • 13
1
vote
1 answer

How to test the homomorphism law of an Applicative instance?

I'm doing the exercises from Typeclassopedia; in the Applicative section, I write ZipList's pure function, and check whether it follows the Applicative Laws. I've checked: identity law interchange law composition law But when I try to check the…
Julian.zhang
  • 709
  • 1
  • 7
  • 11
1
vote
2 answers

Control.Applicative precedence

Regarding Control.Applicative, If I have the following expression: f = (expr1 <|> expr2) <* expr3 Are the brackets necessary? That is, will expr3 get evaluated (and thrown away) regardless of which branch is taken?
Mike Menzel
  • 583
  • 2
  • 12