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
7
votes
1 answer

Could the `Applicative` instance for `These` preserve more "failure" information?

I'm looking at the These datatype from the these package, in particular at its Applicative instance: instance (Semigroup a) => Applicative (These a) where pure = That This a <*> _ = This a That _ <*> This b = This b …
danidiaz
  • 26,936
  • 4
  • 45
  • 95
7
votes
0 answers

Does a joined Bitraversable require Monad?

Despite the jargon filled title I don't think this question is very complex. Introducing the characters There are two important Functor combinators at play here. Flip equivalent to the haskell functiong flip but operating on types newtype Flip p a…
Wheat Wizard
  • 3,982
  • 14
  • 34
7
votes
3 answers

Why are Monoidal and Applicative laws telling us the same thing?

I have learnt about Monoidal being an alternative way to represent Applicative not so long ago. There is an interesting question on Typeclassopedia: (Tricky) Prove that given your implementations from the first exercise [pure and (<*>) written…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
7
votes
1 answer

How can I generalize the arity of rxjava2 Zip function (from Single/Observable) to n Optional arguments without lose its types?

Related to this question https://stackoverflow.com/questions I want to achieve the same in Java with rxJava2 as in haskell How can I implement generalized "zipn" and "unzipn" in Haskell?: In haskell I can achieve this with applicative functors: f…
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
7
votes
1 answer

Why does mutual yielding make ArrowApply and Monads equivalent, unlike Arrow and Applicative?

Here's the SO post I'm going to refer to. Also, I'm going to use the same snippets as the OP in that question in order not to separate the materials. It is widely known that an ArrowApply instance yields a Monad and vice versa: newtype ArrowMonad a…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
7
votes
1 answer

What does the Naturality law for Traversables mean?

The Naturality law states that: t . traverse f == traverse (t . f) -- for every applicative transformer t Now for the RHS of the law, if f has the type Applicative a => x -> a y, then t has to be of the type (Applicative a, Applicative b) => a y ->…
user4132
  • 397
  • 1
  • 7
7
votes
3 answers

Proving equivalence of sequence definitions from Applicative and Monad

How can I properly prove that sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a) sequenceA [] = pure [] sequenceA (x:xs) = pure (:) <*> x <*> sequenceA xs is essentially the same to monad inputs as sequenceA' :: Monad m => [m a]…
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
7
votes
2 answers

What can the Reader monad do that applicative functions cannot?

Having read http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors , I can provide an example of the use of functions as applicative functors: Let's say res is a function of 4 arguments and fa, fb, fc, fd are all…
user4385532
7
votes
2 answers

What does Traversable is to Applicative contexts mean?

I am trying to understand Traversable with the help of https://namc.in/2018-02-05-foldables-traversals. Somewhere the author mentioned the following sentence: Traversable is to Applicative contexts what Foldable is to Monoid values. What did…
softshipper
  • 32,463
  • 51
  • 192
  • 400
7
votes
1 answer

Why is this applicative instance unlawful?

I was reading about monad transformers and found this apparently well known article - A Gentle Introduction to Monad Transformers. What caught my attention was the part where the author describes an applicative instance for a makeshift ExceptT…
Dmitry
  • 73
  • 3
7
votes
2 answers

Every monad is an applicative functor — generalizing to other categories

I can readily enough define general Functor and Monad classes in Haskell: class (Category s, Category t) => Functor s t f where map :: s a b -> t (f a) (f b) class Functor s s m => Monad s m where pure :: s a (m a) join :: s (m (m a))…
7
votes
4 answers

`(<*>)` definition for the Applicative functor?

Some Haskell source code (see ref): -- | Sequential application. -- -- A few functors support an implementation of '<*>' that is more -- efficient than the default one. (<*>) :: f (a -> b) -> f a -> f b (<*>) = liftA2 id -- | Lift a binary function…
Zazaeil
  • 3,900
  • 2
  • 14
  • 31
7
votes
2 answers

Why doesn't this simple composition work?

I was recently in need of putting head in between two monadic operations. Here's the SSCCE: module Main where f :: IO [Int] f = return [1..5] g :: Int -> IO () g = print main = do putStrLn "g <$> head <$> f" g <$> head <$> f putStrLn…
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
7
votes
1 answer

Applicative instance for non-empty leafy tree in Haskell

Given the following data type: data Tree a = Branch (Tree a) (Tree a) | Leaf a deriving (Eq, Show) And the following Functor instance : instance Functor Tree where fmap f (Leaf a) = Leaf $ f a fmap f (Branch t1 t2) = Branch (fmap f…
MMacphail
  • 541
  • 3
  • 19
7
votes
6 answers

How can I abstract a common Haskell recursive applicative functor pattern

While using applicative functors in Haskell I've often run into situations where I end up with repetitive code like this: instance Arbitrary MyType where arbitrary = MyType <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary In this example…
Trystan Spangler
  • 1,685
  • 11
  • 20