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

Debugging infinite Sum in Haskell

Say I have a function (it doesn't have any practical application, just an academic interest, thus weird way to write it, with monoids, applicative functors and fixpoint combinators) f :: Num a => a -> Sum a f = fix ((<>) <$> Sum <*>) It typechecks,…
dmedvinsky
  • 8,046
  • 3
  • 31
  • 25
6
votes
4 answers

Is it possible to map tuple of functions over a list in Haskell?

I'm trying to do find a way to do something like this: (head, last) `someFunction` [1, 2, 3] to produce the tuple (1, 3) as output. It seems similar in theory to an applicative functor, but a little backwards. I'm guessing there's a similar…
Benjamin Kovach
  • 3,190
  • 1
  • 24
  • 38
5
votes
1 answer

Is this "Coapplicative" class a superclass for Comonad?

Recall the Applicative class: class Functor f => Applicative f where pure :: a -> f a liftA2 :: (a -> b -> c) -> f a -> f b -> f c (<*>) :: f (a -> b) -> f a -> f b liftA2 h x y = fmap h x <*> y (<*>) = liftA2 id Though it's…
Dannyu NDos
  • 2,458
  • 13
  • 32
5
votes
2 answers

What is the point of the Functor -> Applicative -> Monad hierarchy

What is the point of making Functor a super class of Applicative and Monad. Both Applicative and Monad immediately imply the only implementation of Functor that abides by the laws as far as I can tell. But rather I have to type out the same…
HashBr0wn
  • 387
  • 1
  • 11
5
votes
1 answer

Applicative operators <* and *>, type signature implication

I recently saw a simple example that brought <* and *> to light. validate :: String -> Maybe String validate s = if s=="" then Nothing else Just s >validate "a" *> validate "b" Just "b" >validate "" *> validate "b" Nothing >validate "a" <*…
user49011
  • 523
  • 1
  • 3
  • 10
5
votes
1 answer

Making QualifiedDo and ApplicativeDo work together when nesting applicative functors

I want to define deeply nested compositions of applicative functors. For example something like this: {-# LANGUAGE TypeOperators #-} import Control.Monad.Trans.Cont import Control.Arrow (Kleisli (..)) import Data.Aeson import Data.Aeson.Types import…
danidiaz
  • 26,936
  • 4
  • 45
  • 95
5
votes
1 answer

Can ZipList be Distributive?

Base provides ZipList, which is just a wrapper for [] where <*> is based on zip instead of cartesian-product. This isn't the default because it's not consistent with the Monad [] instance, but some people find it more intuitive, and both behaviors…
ShapeOfMatter
  • 991
  • 6
  • 25
5
votes
2 answers

How does sequenceA work on lists of pairs?

Spin off of this question. Intuitively I have understood what sequenceA does in that usecase, but not how/why it works like that. So it all boils down to this question: how does sequenceA work in the following case? > sequenceA…
Enlico
  • 23,259
  • 6
  • 48
  • 102
5
votes
2 answers

Why would I want to use the upcoming let! ... and! syntax?

The language suggestion states that the advantages are detailed in the linked paper. I had a quick skim through and I can't see it spelled out explicitly. Is the advantage just that each statement gets executed in parallel so I might get a speed…
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28
5
votes
3 answers

<**> is a variant of <*> with the arguments reversed. What does "reversed" mean?

In GHC.Base the description of <**> runs: A variant of <*> with the arguments reversed. It is widely known that "reversed" in that case does not mean "flipped" as: GHCi> [1, 2, 3] <**> [(^2), (+1)] [1,2,4,3,9,4] GHCi> [(^2), (+1)] <*> [1, 2,…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
5
votes
2 answers

Best way to apply arguments of mixed, possibly Applicative, types to a function

I'm fairly new to Haskell and functional programming and I have recently been learning about Functors, Applicatives and Monads. While I seem to understand the basics, I have trouble figuring out the best/most idiomatic way apply function arguments…
Aiueiia
  • 162
  • 9
5
votes
3 answers

How to map over Applicative form?

I want to map over Applicative form. The type of map-like function would be like below: mapX :: (Applicative f) => (f a -> f b) -> f [a] -> f [b] used as: result :: (Applicative f) => f [b] result = mapX f xs where f :: f a -> f b f =…
masayuki takagi
  • 442
  • 3
  • 12
5
votes
2 answers

Applicatives: <$> vs. pure and <*>

After trying out examples for a while, to me it looks like myFunction <$> and pure myFunction <*> are equivalent when working on the Control.Applicative type class. Example: (++) <$> Just "foo" <*> Just "bar" pure (++) <*> Just "foo" <*> Just…
schmittlauch
  • 127
  • 6
5
votes
4 answers

How to make instance of Applicative a certain data type

I'm reading Graham Hutton book on Haskell, and don't no how to proceed in one part of an excercise. The excercise says as follows: Given the following type expressions data Expr a = Var a | Val Int | Add (Expr a) (Expr a) deriving Show that…
Jordi
  • 253
  • 2
  • 8
5
votes
2 answers

Why does a "let" statement force an "applicative do" block into requiring a monad constraint?

Consider this example: {-# language ApplicativeDo #-} module X where data Tuple a b = Tuple a b deriving Show instance Functor (Tuple a) where fmap f (Tuple x y) = Tuple x (f y) instance Foldable (Tuple a) where foldr f z (Tuple _ y) = f…
Ignat Insarov
  • 4,660
  • 18
  • 37