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

Haskell - Is effect order deterministic in case of Applicative?

When executing the IO action defined by someFun <$> (a :: IO ()) <$> (b :: IO ()), is the execution of the a and b actions ordered? That is, can I count on that a is executed before b is? For GHC, I can see the IO is implemented using State, and…
ron
  • 9,262
  • 4
  • 40
  • 73
14
votes
4 answers

Why does the Applicative instance for Maybe give Nothing when function is Nothing in <*>

I am a beginner with haskell and am reading the Learn you a haskell book. I have been trying to digest functors and applicative functors for a while now. In the applicative functors topic, the instance implementation for Maybe is given as instance…
sharat87
  • 7,330
  • 12
  • 55
  • 80
14
votes
2 answers

Does the function monad really offer something more than the function applicative functor? If so, what?

For the function monad I find that (<*>) and (>>=)/(=<<) have two strikingly similar types. In particular, (=<<) makes the similarity more apparent: (<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) (=<<) :: (a -> r -> b) -> (r -> a) -> (r -> b) So…
Enlico
  • 23,259
  • 6
  • 48
  • 102
14
votes
3 answers

Is `x >> pure y` equivalent to `liftM (const y) x`

The two expressions y >> pure x liftM (const x) y have the same type signature in Haskell. I was curious whether they were equivalent, but I could neither produce a proof of the fact nor a counter example against it. If we rewrite the two…
1000000000
  • 639
  • 4
  • 17
14
votes
2 answers

How and why is ap defined as liftM2 id in Haskell

Whilst trying to better understand Applicative, I looked at the definition of <*>, which tends to be defined as ap, which in turn is defined as: ap :: (Monad m) => m (a -> b) -> m a -> m b ap = liftM2 id Looking at…
lukerandall
  • 2,201
  • 17
  • 29
14
votes
1 answer

Can the continuation monad transformer be given an Alternative instance with some and many?

We can define the continuation monad transformer as data Cont r m a = Cont {run :: (a -> m r) -> m r} We can give Cont r m an Alternative instance if m is a member of Alternative via empty = Cont $ \f -> empty ca <|> cb = Cont $ \f -> run ca f <|>…
14
votes
1 answer

Applicative instance for free monad

While trying to find a haskell monad that can be executed stepwise / allows threading, I discovered the free monad data Free f a = Return a | Roll (f (Free f a)) with its monad instance instance (Functor f) => Monad (Free f) where return =…
Franky
  • 2,339
  • 2
  • 18
  • 27
13
votes
2 answers

Examples of "undoable" applicative functors?

I declared a group on applicative functors. Judging by what we usually call "actions", it seems that such group would enable the action to be undone: import Control.Applicative class Alternative f => Undoable f where undo :: f a -> f a Being a…
Dannyu NDos
  • 2,458
  • 13
  • 32
13
votes
2 answers

Does liftA2 preserve associativity?

Given an operation (??) such that (a ?? b) ?? c = a ?? (b ?? c) (that is to say (??) is associative) must it be the case that liftA2 (??) (liftA2 (??) a b) c = liftA2 (??) a (liftA2 (??) b c) (that is to say that liftA2 (??) is associative) If we…
Wheat Wizard
  • 3,982
  • 14
  • 34
13
votes
1 answer

Relation between `<*>` and `<$>`

According to the Haskell Wikibook, the following relation between <$> and <*> hold: f <$> x = pure f <*> x They claim that one can prove this theorem as a consequence of the functor and applicative laws. I do not see how to prove this. Any help is…
13
votes
3 answers

What are the applicative functor laws in terms of pure and liftA2?

I'm playing around with formulating Applicative in terms of pure and liftA2 (so that (<*>) = liftA2 id becomes a derived combinator). I can think of a bunch of candidate laws, but I'm not sure what the minimal set would be. f <$> pure x = pure (f…
Alan O'Donnell
  • 1,276
  • 9
  • 17
13
votes
1 answer

Applicative instance for MaybeT m assumes Monad m

I've been using the Haxl monad (described here: http://www.reddit.com/r/haskell/comments/1le4y5/the_haxl_project_at_facebook_slides_from_my_talk), which has the interesting feature that <*> for its Applicative instance isn't the same as ap from…
davidsd
  • 771
  • 4
  • 18
13
votes
3 answers

Any advantages to Haskell desugaring?

When I am using Functors, Monads, and other Hakell constructs, if my code is more than just a couple of lines, I prefer using some syntactic sugar like do-notation. This makes it easier for me to follow the flow of the code. Outside of stylistic…
Mike Menzel
  • 583
  • 2
  • 12
13
votes
2 answers

Examples of Functors without Applicatives

Are there any good examples of Functors which are not Applicatives? By good, I'm seeking non-trivial (not Const Void) examples which don't need appeals to undefined. If there are none is there any method of proving that the space there is…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
13
votes
1 answer

What is this special functor structure called?

Suppose that F is an applicative functor with the additional laws (with Haskell syntax): pure (const ()) <*> m === pure () pure (\a b -> (a, b)) <*> m <*> n === pure (\a b -> (b, a)) <*> n <*> m pure (\a b -> (a, b)) <*> m <*> m === pure (\a…
user1972140