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

Can someone explain where Applicative instances arise in this code?

isAlphaNum :: Char -> Bool isAlphaNum = (||) <$> isAlpha <*> isNum I can see that it works, but I don't understand where the instances of Applicative (or Functor) come from.
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
17
votes
2 answers

Uniqueness of pure

For any Applicative instance, once <*> is written, pure is uniquely determined. Suppose you have pure1 and pure2, both of which obey the laws. Then pure2 f <*> pure1 y = pure1 ($ y) <*> pure2 f -- interchange for pure1 pure2 id <*> pure1 y = pure1…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
17
votes
2 answers

The need for pure in Applicatives

I'm learning Haskell's Applicatives. It seems to me (I'm probably wrong) that the pure function is not really needed, for example: pure (+) <*> [1,2,3] <*> [3,4,5] can be written as (+) <$> [1,2,3] <*> [3,4,5] Can someone explain the benefit…
Tomer
  • 1,159
  • 7
  • 15
17
votes
2 answers

How arbitrary is the "ap" implementation for monads?

I am currently studying the bonds between monad and applicative functors. I see two implementation for ap: ap m1 m2 = do { f <- m1 ; x <- m2 ; return (f x) } and ap m1 m2 = do { x <- m2 ; f <- m1 ; return (f x) } The second one is different, yet,…
Simon A.
  • 173
  • 6
17
votes
2 answers

Why there is no way to derive Applicative Functors in Haskell?

In Haskell, you can derive Functor, Foldable and Traversable automatically using deriving. There is no way to derive Applicative, though. Considering there is one obvious way to define an Applicative instance (which would amount to a zipped…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
16
votes
3 answers

Monoidal Functor is Applicative but where is the Monoid typeclass in the definition of Applicative?

Applicative is a Monoidal Functor : mappend :: f -> f -> f $ :: (a -> b) -> a -> b <*> :: f(a -> b) -> f a -> f b But I don't see any reference about Monoid in the definition of the Applicative typeclass, could you tell me…
Nicolas Henin
  • 3,244
  • 2
  • 21
  • 42
16
votes
1 answer

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

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…
meiersi
  • 308
  • 1
  • 5
16
votes
7 answers

What are some better ways to write [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] in Haskell?

I've run in to a few situations where I need the list: [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] -- no (0,0) Note that there is no (0,0) in the list. I use the (dx,dy) tuples to search up, down, left, right and diagonally from a…
Jake Brownson
  • 469
  • 2
  • 9
15
votes
2 answers

How to show that a monad is a functor and an applicative functor?

Monads are known to be theoretically a subset of functors and specifically applicative functors, even though it's not indicated in Haskell's type system. Knowing that, given a monad and basing on return and bind, how to: derive fmap, derive <*> ?
Kos
  • 70,399
  • 25
  • 169
  • 233
15
votes
1 answer

Which applicative functor is used for passing shared parameters?

I think I kind of understand how applicative functors work in Haskell and I'm using them for basic datatypes (Maybe, Either...). However, I found this question with the following example: withPool pool = bracket (takeConn pool) (putConn pool) can…
ondra
  • 9,122
  • 1
  • 25
  • 34
15
votes
1 answer

What is the 'minimum' needed to make an Applicative a Monad?

The Monad typeclass can be defined in terms of return and (>>=). However, if we already have a Functor instance for some type constructor f, then this definition is sort of 'more than we need' in that (>>=) and return could be used to implement fmap…
15
votes
4 answers

Removing duplication (with Applicative ((->) t), perhaps?)

I was playing around with a simple function for someone else's Stack Overflow question, and wrote the expression: f a x ++ f a y Obviously this is the best way to write that expression in real life, given I have all those variables in scope anyway,…
amalloy
  • 89,153
  • 8
  • 140
  • 205
15
votes
1 answer

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

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…
James Davies
  • 9,602
  • 5
  • 38
  • 42
15
votes
1 answer

Understanding Data.Functor.Constant constructor and applicative laws

I'm confused about Data.Functor.Constant's type constructor and also how it works with applicative. First the constructor: When I examine the type of Constant :: a -> Constant a b I see it takes an a, but returns a Constant a b Where does the b…
Brian
  • 692
  • 3
  • 14
15
votes
2 answers

Applicative functors analysis

I've been trying to learn about static analysis of applicative functors. Many sources say that an advantage of using them over monads is the susceptibility to static analysis. However, the only example I can find of actually performing static…
aurickQ
  • 153
  • 6