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
21
votes
1 answer

What is Control.Applicative.Lift useful for?

I wrote about transformers in a recent blog post, and someone asked "what do people use Control.Applicative.Lift for?" I wasn't able to answer this, so I echo the question to StackOverflow - what is Control.Applicative.Lift used for? I see one…
ocharles
  • 6,172
  • 2
  • 35
  • 46
20
votes
0 answers

How can (<*) be implemented optimally for sequences?

The Applicative instance for Data.Sequence generally performs very well. Almost all the methods are incrementally asymptotically optimal in time and space. That is, given fully forced/realized inputs, it's possible to access any portion of the…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
20
votes
3 answers

What is the purpose of `pure` in Applicative Functor

Meet the Applicative typeclass. It lies in the Control.Applicative module and it defines two methods, pure and <*>. It doesn't provide a default implementation for any of them, so we have to define them both if we want something to be an…
Sumanth Kumar Mora
  • 637
  • 1
  • 5
  • 15
20
votes
2 answers

Why does <$> and <*> take input in an order opposite of >>=?

I understand the reasoning behind <$>'s type signature, as it's just an infix version of fmap, but comparing it to >>='s type signature it makes a lot less sense to me. Let's first establish what I mean by that. (>>=) :: Monad m => m a -> (a -> m b)…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
20
votes
1 answer

Correspondence between type classes and grammar levels in the Chomsky hierarchy

My question is about the Applicative and Monad type classes on the one hand, and the context-free and context-sensitive grammar levels of the Chomsky hierarchy on the other. I've heard that there's a correspondence between the type classes and the…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
20
votes
3 answers

Is it possible to use a bracketing syntactic sugar for an applicative functor?

In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w…
AndrewC
  • 32,300
  • 7
  • 79
  • 115
19
votes
2 answers

Meaning of "closed under composition"

I have been reading this paper and it is mentioned there that the Applicative class is closed under composition. What does that actually mean ?
Sibi
  • 47,472
  • 16
  • 95
  • 163
19
votes
5 answers

More fun with applicative functors

Earlier I asked about translating monadic code to use only the applicative functor instance of Parsec. Unfortunately I got several replies which answered the question I literally asked, but didn't really give me much insight. So let me try this…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
19
votes
3 answers

Translate from monad to applicative

OK, so I know what the Applicative type class contains, and why that's useful. But I can't quite wrap my brain around how you'd use it in a non-trivial example. Consider, for example, the following fairly simple Parsec parser: integer :: Parser…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
18
votes
1 answer

What does "lax" mean in "lax monoidal functor"?

I know that the Applicative class is described in category theory as a "lax monoidal functor" but I've never heard the term "lax" before, and the nlab page on lax functor a bunch of stuff I don't recognize at all, re: bicategories and things that I…
luqui
  • 59,485
  • 12
  • 145
  • 204
18
votes
3 answers

Proving equality of streams

I have a data type data N a = N a [N a] of rose trees and Applicative instance instance Applicative N where pure a = N a (repeat (pure a)) (N f xs) <*> (N a ys) = N (f a) (zipWith (<*>) xs ys) and need to prove the Applicative laws for it.…
emi
  • 5,380
  • 1
  • 27
  • 45
18
votes
1 answer

Monads VS Applicative functors for Futures

Suppose I want to aggregate data from 2 remote services, and serve response as fast as I can: def loadUser: Future[User] def loadData: Future[Data] case class Payload(user: User, data: Data) I understand that this one executes async tasks…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
18
votes
1 answer

Generate optparse-applicative parser from free alternative functor

Consider the following type signatures: data Foo x = Foo { name :: String , reader :: String -> x } instance Functor Foo where fmap f (Foo n r) = Foo n $ f . r Now I show a natural transformation from Foo to optparse-applicative's Parser…
18
votes
5 answers

Is it better to define Functor in terms of Applicative in terms of Monad, or vice versa?

This is a general question, not tied to any one piece of code. Say you have a type T a that can be given an instance of Monad. Since every monad is an Applicative by assigning pure = return and (<*>) = ap, and then every applicative is a Functor via…
bstamour
  • 7,746
  • 1
  • 26
  • 39
18
votes
4 answers

Why can applicative functors have side effects, but functors can't?

I'm feeling rather silly asking this question, but it's been on my mind for a while and I can't find any answers. So the question is: why can applicative functors have side effects, but functors can't? Maybe they can and I've just never noticed...?
Brian
  • 692
  • 3
  • 14
1 2
3
38 39