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
35
votes
3 answers

Lax monoidal functors with a different monoidal structure

Applicative functors are well-known and well-loved among Haskellers, for their ability to apply functions in an effectful context. In category-theoretic terms, it can be shown that the methods of Applicative: pure :: a -> f a (<*>) :: f (a -> b) ->…
32
votes
4 answers

How to combine Futures of different types into a single Future without using zip()

I want to create a Future of type Future[(Class1,Class2,Class3)] from below code. However the only way I have found to do this is by using zip(). I find the solution ugly and properly not optimal. Can anybody enlightened me. val v = for ( a…
Peter Lerche
  • 469
  • 4
  • 6
31
votes
2 answers

Haskell - What is Control.Applicative.Alternative good for?

I was looking at the Applicative class within Haskell libraries and stumbled across Alternative. What is this class good for? A google search did not reveal anything particularly insightful. And it seems to be completely out of place, bundled as it…
Anupam Jain
  • 7,851
  • 2
  • 39
  • 74
31
votes
2 answers

What is Applicative Functor definition from the category theory POV?

I was able to map Functor's definition from category theory to Haskell's definition in the following way: since objects of Hask are types, the functor F maps every type a of Hask to the new type F a by, roughly saying, prepending "F " to it. maps…
arrowd
  • 33,231
  • 8
  • 79
  • 110
31
votes
3 answers

Examples of a monad whose Applicative part can be better optimized than the Monad part

In one discussion I heard that Applicative interface of some parsers is implemented differently, more efficiently than their Monad interface. The reason is that with Applicative we know all "effects" in advance, before the whole effectful…
Petr
  • 62,528
  • 13
  • 153
  • 317
30
votes
4 answers

What is the 'Const' applicative functor useful for?

I've just found Const in the documentation of Control.Applicative, but I have a hard time working out where this is useful, over just using Monoid directly. What am I missing?
ocharles
  • 6,172
  • 2
  • 35
  • 46
29
votes
2 answers

To what extent are Applicative/Monad instances uniquely determined?

As described this question/answers, Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList. So Applicative isn't unique (see also Can GHC derive Functor and Applicative…
Petr
  • 62,528
  • 13
  • 153
  • 317
29
votes
4 answers

Where to find programming exercises for applicative functors?

I've been reading about applicative functors, notably in the Functional Pearl by McBride and Paterson. But I'd like to solidify my understanding by doing some exercises. I'd prefer programming exercises but proof exercises are OK too. What…
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
27
votes
1 answer

How to parse an optional flag as a Maybe value?

I'm trying to use optparse-applicative to parse a Maybe String but I can't find anywhere how to deal with Maybe. The only thing I found is to add a default value but I really need a Nothing if user didn't supply an option instead of "". Is there any…
mb14
  • 22,276
  • 7
  • 60
  • 102
26
votes
2 answers

What is the etymology of <*> from Applicative in Haskell?

Where did the name <*> first begin to appear in literature or code, and did it come with any explanation for the choice of symbol?
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
26
votes
1 answer

What’s an example of a Monad which is an Alternative but not a MonadPlus?

In his answer to the question “Distinction between typeclasses MonadPlus, Alternative, and Monoid?”, Edward Kmett says that Moreover, even if Applicative was a superclass of Monad, you’d wind up needing the MonadPlus class anyways, because…
25
votes
3 answers

What exactly are the categories that are being mapped by Applicative Functors?

I've been reading up on Applicative Functors and I am having difficulty reconciling a mismatch in the respective terminologies of category theory and functional programming. Although I have looked over various blogs, the most complete resources that…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
23
votes
3 answers

Are free monads also zippily applicative?

I think I've come up with an interesting "zippy" Applicative instance for Free. data FreeMonad f a = Free (f (FreeMonad f a)) | Return a instance Functor f => Functor (FreeMonad f) where fmap f (Return x) = Return (f x) …
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
22
votes
2 answers

Isn't it redundant for Control.Lens.Setter to wrap types in functors?

I'm watching the Control.Lens introduction video. It makes me wonder why is it needed for the Setter type to wrap things in functors. It's (roughly) defined like this: type Control.Lens.Setter s t a b = (Functor f) => (a -> f a) -> s -> f t Let's…
qwe
  • 766
  • 1
  • 5
  • 15
21
votes
1 answer

Functors and Applicatives for types of kind (* -> *) -> *

I ran into a situation where my code would benefit from using Functor and Applicative -like abstractions, but for types of kind (* -> *) -> *. Defining a higher-kinded functor can be done with RankNTypes like this class HFunctor f where hfmap ::…
shang
  • 24,642
  • 3
  • 58
  • 86
1
2
3
38 39