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
9
votes
2 answers

How can I test functions polymorphic over Applicatives?

I've just written a function (for Data.Sequence) traverseWithIndex :: Applicative f => (Int -> a -> f b) -> Seq a -> f (Seq b) which should obey traverseWithIndex f = sequenceA . mapWithIndex f Thankfully, this is a straightforward mechanical…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
9
votes
2 answers

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…
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
9
votes
4 answers

<*> for lists implemented as do notation - isn't this "cheating"?

According to 'Learn you a Haskell', the implementation of <*> for lists is: fs <*> xs = [f x | f <- fs, x <- xs] Am I mistaken, or is this sugared monadic code based on >>= ? As far as I understand, it should be possible to implement <*> only using…
Marco Faustinelli
  • 3,734
  • 5
  • 30
  • 49
9
votes
4 answers

Is there an equivalent in Scala to Python's more general map function?

I know that Scala's Lists have a map implementation with signature (f: (A) => B):List[B] and a foreach implementation with signature (f: (A) => Unit):Unit but I'm looking for something that accepts multiple iterables the same way that the Python map…
wheaties
  • 35,646
  • 15
  • 94
  • 131
9
votes
4 answers

What are applicative effects?

What is the meaning of the concept of effect in effectful applicative programming? For example, which parts of expressions below are the effects? [(+1)] <*> [2,3] Just (+1) <*> Nothing
sevo
  • 4,559
  • 1
  • 15
  • 31
9
votes
3 answers

How to handle side effect with Applicative?

I see everywhere that Applicative can handle side effects, but all the simple examples I've seen are just combining stuff together like: > (,,) <$> [1,2] <*> ["a", "b", "c"] <*> ["foo",…
mb14
  • 22,276
  • 7
  • 60
  • 102
9
votes
3 answers

Proofs of Applicative laws for haskell instances

Have all the Haskell instances of Applicative typeclass that we get with the Haskell platform been proved to satisfy all the Applicative laws? If yes, where do we find those proofs? The source code of Control.Applicative does not seem to contain any…
user1308560
  • 409
  • 2
  • 9
9
votes
1 answer

How do applicative functors tie in with parallelizing algorithms? (Scala and Scalaz)

From Josh Suereth's "Scala in Depth": "Applicative functors provide a way to take two computations and join them together using a function. The Traversable example highlights how two collections can be parallelized into pairs. Applicative functors…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
9
votes
1 answer

Applicative instance for a tuple with monoid and function inside

I was trying to convert a haskell example, I came across earlier, to scalaz. The original example was this: ("Answer to the ", (*)) <*> ("Ultimate Question of ", 6) <*> ("Life, the Universe, and Everything", 7) Which, as far as I am able to…
George
  • 8,368
  • 12
  • 65
  • 106
8
votes
1 answer

Why doesn't `coerce` implicitly apply Compose to these functions?

Consider this type: data Vec3 = Vec3 {_x, _y, _z :: Int} I have some functions that all take the same input, and may fail to compute a field: data Input getX, getY, getZ :: Input -> Maybe Int I can write a function that tries all three of these…
amalloy
  • 89,153
  • 8
  • 140
  • 205
8
votes
1 answer

What is the difference between *> and >> in Haskell?

From the docs: (>>) : Sequentially compose two actions, discarding any value produced by the first (*>) : Sequence actions, discarding the value of the first argument. Both seem to me doing the same job.
user5895938
8
votes
3 answers

Can a Functor / Applicative be tied to one specific type or structure?

I’m trying to understand the applicative typeclass, and in particular the <*> function. Now I see its type signature is f (a -> b) -> f a -> f b and I take it that f is a functor, which I think of as some kind of structure wrapping some data. It…
Addem
  • 3,635
  • 3
  • 35
  • 58
8
votes
3 answers

Are all fixed size containers strong monoidal functors, and/or vice versa?

The Applicative typeclass represents lax monoidal functors that preserve the cartesian monoidal structure on the category of typed functions. In other words, given the canonical isomorphisms witnessing that (,) forms a monoidal structure: --…
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
8
votes
1 answer

Stream to be an instance of traversable

The vector-0.1 package has a quite efficient Stream implementation (Data.Vector.Stream): data Step s a = Yield a s | Skip s | Done -- | The type of fusible streams data Stream a = forall s. Stream (s -> Step s a) s…
mcmayer
  • 1,931
  • 12
  • 22
8
votes
4 answers

Applicative instance for State - order of data flow

I am trying to implement Applicative instance for such a type: newtype State s a = State {runState :: s -> (a, s)} I have some different ideas for (<*>) function. One way to implement it that comes to my mind is (<*>) :: State s (a -> b) -> State…
radrow
  • 6,419
  • 4
  • 26
  • 53