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
13
votes
2 answers

Can I model a list of successes with short circuiting failure via the composition of applicative functors?

The user 'singpolyma' asked on reddit if there was some general structure underlying: data FailList a e = Done | Next a (FailList a e) | Fail e A free monad was suggested, but I wondered if this could be modeled more generally via applicative…
ocharles
  • 6,172
  • 2
  • 35
  • 46
13
votes
1 answer

How much is applicative really about applying, rather than "combining"?

For an uncertainty-propagating Approximate type, I'd like to have instances for Functor through Monad. This however doesn't work because I need a vector space structure on the contained types, so it must actually be restricted versions of the…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
12
votes
1 answer

Haskell: some and many

What are some and many in Control.Applicative.Alternative good for? If I write something like some $ Just 42, it seems to cause infinite recursion, which seems not very useful...
Landei
  • 54,104
  • 13
  • 100
  • 195
12
votes
3 answers

How is "a monoid on applicative functors" different than "a monoid in the category of endofunctors"?

Perhaps neither of these statements are categorically precise, but a monad is often defined as "a monoid in the category of endofunctors"; a Haskell Alternative is defined as "a monoid on applicative functors", where an applicative functor is a…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
12
votes
1 answer

Applicative laws for alternative class formulations

A well-known alternative formulation of Applicative (see, e.g., Typeclassopedia) is class Functor f => Monoidal f where unit :: f () pair :: f a -> f b -> f (a, b) This leads to laws that look more like typical identity and associativity laws…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
12
votes
2 answers

I can't understand Wikipedia's definition of "applicative functor"

Studying functors, applicative functors and monads in Haskell, I found this definition on Wikipedia: In functional programming, specifically Haskell, an applicative functor is a structure that is like a monad (return, fmap, join) without join,…
mmlab
  • 125
  • 1
  • 7
12
votes
1 answer

Foldable vs Traversable

While studying Applicative deeper, I came to Traversable. Although I already knew Foldable from LYHGG, I haven't seen the former yet, so I started reading the Haskell wiki about Traversable. While reading it, I understood why Foldable.fold is…
FtheBuilder
  • 1,410
  • 12
  • 19
12
votes
2 answers

Functor / Applicative instances for State in Haskell

After reading (and skimming some sections of) Wadler's paper on monads, I decided to work through the paper more closely, defining functor and applicative instances for each of the monads he describes. Using the type synonym type M a = State -> (a,…
emi
  • 5,380
  • 1
  • 27
  • 45
12
votes
2 answers

Applicative is to monad what X is to comonad

Can we solve this equation for X ? Applicative is to monad what X is to comonad
nicolas
  • 9,549
  • 3
  • 39
  • 83
12
votes
2 answers

Naming of `pure` function in Control.Applicative

Why is the function for lifting a value into a functor named pure in Control.Applicative?
Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
12
votes
1 answer

Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative?

Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative? (This kind of equivalence is valid in a popular idiom with plain $, but currently $ is right-associative!) <*> has the same associativity and precedence as <$>,…
12
votes
2 answers

Is it possible to encode a generic "lift" function in Haskell?

I'm not the biggest fan of varargs, but I always thought both the applicative (f <$> x <*> y) and idiom ([i| f x y |]) styles have too many symbols. I usually prefer going the liftA2 f x y way, but I, too, think that A2 is a little ugly. From this…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
12
votes
3 answers

How to construct an Applicative instance with constraints (similarly to constructing Monad instances using ContT)

This question deals with constructing a proper Monad instance from something that is a monad, but only under certain constraints - for example Set. The trick is to wrap it into ContT, which defers the constraints to wrapping/unwrapping its…
Petr
  • 62,528
  • 13
  • 153
  • 317
12
votes
4 answers

instance Alternative ZipList in Haskell?

ZipList comes with a Functor and an Applicative instance (Control.Applicative) but why not Alternative? Is there no good instance? What about the one proposed below? Is it flawed? is it useless? Are there other reasonable possibilities (like…
sgm
  • 143
  • 6
11
votes
2 answers

Why does importing Control.Applicative allow this bad code to type check?

I'm helping a friend learn Haskell and he recently created code like this, which type checks and produces a CPU-burning loop at runtime. I'm completely baffled by this. import Control.Monad import Control.Applicative main = forever putStrLn "Hello,…
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77