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
231
votes
5 answers

Good examples of Not a Functor/Functor/Applicative/Monad?

While explaining to someone what a type class X is I struggle to find good examples of data structures which are exactly X. So, I request examples for: A type constructor which is not a Functor. A type constructor which is a Functor, but not…
Rotsor
  • 13,655
  • 6
  • 43
  • 57
126
votes
7 answers

Applicatives compose, monads don't

Applicatives compose, monads don't. What does the above statement mean? And when is one preferable to other?
91
votes
1 answer

Distinction between typeclasses MonadPlus, Alternative, and Monoid?

The standard-library Haskell typeclasses MonadPlus, Alternative, and Monoid each provide two methods with essentially the same semantics: An empty value: mzero, empty, or mempty. An operator a -> a -> a that joins values in the typeclass together:…
00dani
  • 1,498
  • 15
  • 17
76
votes
4 answers

What does Haskell's <|> operator do?

Going through Haskell's documentation is always a bit of a pain for me, because all the information you get about a function is often nothing more than just: f a -> f [a] which could mean any number of things. As is the case of the <|> function. All…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
74
votes
5 answers

What are the benefits of applicative parsing over monadic parsing?

There seems to be a consensus that you should use Parsec as an applicative rather than a monad. What are the benefits of applicative parsing over monadic parsing? style performance abstraction Is monadic parsing out?
gawi
  • 13,940
  • 7
  • 42
  • 78
74
votes
11 answers

What are practical uses of applicative style?

I am a Scala programmer, learning Haskell now. It's easy to find practical use cases and real world examples for OO concepts, such as decorators, strategy pattern etc. Books and interwebs are filled with it. I came to the realization that this…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
73
votes
2 answers

When and why should one use Applicative Functors in Scala

I know that Monad can be expressed in Scala as follows: trait Monad[F[_]] { def flatMap[A, B](f: A => F[B]): F[A] => F[B] } I see why it is useful. For example, given two functions: getUserById(userId: Int): Option[User] = ... getPhone(user:…
Michael
  • 41,026
  • 70
  • 193
  • 341
68
votes
7 answers

Difference between Monad and Applicative in Haskell

I just read the following from typeclassopedia about the difference between Monad and Applicative. I can understand that there is no join in Applicative. But the following description looks vague to me and I couldn't figure out what exactly is meant…
thor
  • 21,418
  • 31
  • 87
  • 173
53
votes
5 answers

What are Alternative's "some" and "many" useful for?

Alternative, an extension of Applicative, declares empty, <|> and these two functions: One or more: some :: f a -> f [a] Zero or more: many :: f a -> f [a] If defined, some and many should be the least solutions of the equations: some v = (:) <$>…
Petr
  • 62,528
  • 13
  • 153
  • 317
46
votes
5 answers

What exactly does "effectful" mean?

Time and again I read the term effectful, but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations, but I've also seen the term effectful values) I used to think that effectful means…
Martin Drautzburg
  • 5,143
  • 1
  • 27
  • 39
46
votes
4 answers

functions as applicative functors (Haskell / LYAH)

Chapter 11 of Learn You a Haskell introduces the following definition: instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x) Here, the author engages in some uncharacteristic hand-waving ("The instance…
planarian
  • 2,047
  • 18
  • 18
45
votes
4 answers

Why should Applicative be a superclass of Monad?

Given: Applicative m, Monad m => mf :: m (a -> b), ma :: m a it seems to be considered a law that: mf <*> ma === do { f <- mf; a <- ma; return (f a) } or more concisely: (<*>) === ap The documentation for Control.Applicative says that <*> is…
mergeconflict
  • 8,156
  • 34
  • 63
41
votes
2 answers

Traversing lists and streams with a function returning a future

Introduction Scala's Future (new in 2.10 and now 2.9.3) is an applicative functor, which means that if we have a traversable type F, we can take an F[A] and a function A => Future[B] and turn them into a Future[F[B]]. This operation is available in…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
41
votes
8 answers

What advantage does Monad give us over an Applicative?

I've read this article, but didn't understand last section. The author says that Monad gives us context sensitivity, but it's possible to achieve the same result using only an Applicative instance: let maybeAge = (\futureYear birthYear -> if…
arrowd
  • 33,231
  • 8
  • 79
  • 110
38
votes
3 answers

Arrows are exactly equivalent to applicative functors?

According to the famous paper Idioms are oblivious, arrows are meticulous, monads are promiscuous, the expressive power of arrows (without any additional typeclasses) should be somewhere strictly between applicative functors and monads: monads are…
Cactus
  • 27,075
  • 9
  • 69
  • 149
1
2 3
38 39