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

In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,...`?

Class Applicative is declared as: class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b We can represent fmapi, i=0,1,2,... in terms of pure and (<*>): fmap0 :: a -> f a fmap0 = …
Tim
  • 1
  • 141
  • 372
  • 590
5
votes
1 answer

Why isn't this this applicative statement being lazily evaluated, and how can I understand why?

abc :: IO (Int) abc = do print "abc" pure $ 10 xyz :: IO (Int) xyz = undefined main :: IO () main = do x <- (((+) <$> abc <*> abc) <* xyz) print x Why in the above is xyz being evaluated? I would assume due to Haskell's lazy nature it…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
5
votes
1 answer

Cats: mapping over tuples with the same applicative

Let's say I have: val x1: Either[String, Int] = Right(1) val x2: Either[String, Float] = Left("Nope") val x3: Either[String, Double] = Left("Not Today") I want to combine these together and get a Either[NonEmptyList[String], (Int, Float, Double)].…
Maths noob
  • 1,684
  • 20
  • 42
5
votes
2 answers

Making instance of Applicative

Still not a hundred percent shure how to make instances of the more complex types. Have this: data CouldBe a = Is a | Lost deriving (Show, Ord) Made an instance of Functor, using Maybe as an example: instance Functor CouldBe where fmap f (Is…
Madderote
  • 1,107
  • 10
  • 19
5
votes
1 answer

Haskell example where pure and return not interchangeable

Is Haskell's pure function the same as return? I just can make a type an instance of Monad if it is already an instance of Applicative, right? So I wonder that Applicative's pure is everytime interchangeable with Monad's return? Is there an example…
5
votes
1 answer

Scala cats and traverse syntax for Either - doesn't compile

I am trying to use traverse (or sequence which is pretty much the same for my task) from cats library https://typelevel.org/cats/typeclasses/traverse.html . I want to traverse a List[A] with function A => Either[L,R] to get Either[L,List[R]] as a…
pkozlov
  • 746
  • 5
  • 17
5
votes
2 answers

I think there is a type mismatch in default definition in instance Applicative Maybe in Haskell

I am currently studying Haskell with Prof. Hutton's "Programming in Haskell", and I found something strange regarding the definition of Maybe as an instance of the class Applicative. In GHC.Base, the instance Applicative Maybe is defined as…
Namudon'tdie
  • 306
  • 1
  • 10
5
votes
2 answers

How exactly does the `(<*>) = ap` Applicative/Monad law relate the two classes?

ap doesn't have a documented spec, and reads with a comment pointing out it could be <*>, but isn't for practical reasons: ap :: (Monad m) => m (a -> b) -> m a -> m b ap m1 m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) } --…
jberryman
  • 16,334
  • 5
  • 42
  • 83
5
votes
2 answers

x <*> y <$> z in Haskell

I'm trying to understand some Haskell source code, and I encountered this structure some times: x <*> y <$> z e.g. (+) <*> (+1) <$> a Can somebody explain this structure to me? I get that it translates to fmap a (+ a + 1), but I can't make the…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
5
votes
1 answer

Are applicative functors composed with the applicative style really independent?

I've come to understand functors, applicative functors, and monads as follows: Functors: computations that can be mapped over. Applicative functors: independent computations whose results can be combined together. Monad: (possibly, but not…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
5
votes
1 answer

ApplicativeDo in Haskell

AFAIK one of the new additions to GHC8 is the ApplicativeDo language extension, which desugars the do-notation to the corresponding Applicative methods (<$>, <*>) if possible. I have the following questions. How does it decide whether the desugaring…
zeronone
  • 2,912
  • 25
  • 28
5
votes
1 answer

Can I rewrite this unionWith-like function with Applicative instead Monad?

I tried to write a function similar to Data.Map.unionWith, but may fail. The original one uses Maybe, which indeed is a Monad, so the monadic one just works fine for me. but I wonder if it can be rewritten with Applicative, since I fmapped it with…
Cosmia Luna
  • 883
  • 1
  • 8
  • 8
5
votes
2 answers

Applicative implementation of Const Monoid

instance Monoid m => Applicative (Const m) where pure _ = Const mempty Const f <*> Const v = Const (f `mappend` v) I do not understand how can the definition of <*> type-check. On the left side f is constrained by the signature of <*> as in…
libeako
  • 2,324
  • 1
  • 16
  • 20
5
votes
1 answer

Are there flipped operators for Applicative?

The operators $ . or <<< <$> <*> =<< <=< Go very well together, each in some sense applying something on the left to something on the right. When thinking the other way, we have & >>> >>= >=> But are there flipped versions of <$> and <*>?
dfeuer
  • 48,079
  • 5
  • 63
  • 167
5
votes
1 answer

optparse-applicative: How to handle no-arguments situation in Arrow syntax

There is example: https://github.com/pcapriotti/optparse-applicative/blob/master/tests/Examples/Cabal.hs#L46-L62 parser :: Parser Args parser = runA $ proc () -> do opts <- asA commonOpts -< () cmds <- (asA . hsubparser) ( command…
cnd
  • 32,616
  • 62
  • 183
  • 313