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

How does Haskell perform Beta conversion to derive a type?

I'm learning Haskell by taking fp-course exercise. There is a question block my way. I don't know how Haskell infer lift2 (<$>) (,)'s type, and turn out Functor k => (a1 -> k a2) -> a1 -> k (a1, a2). I have tried out lift2 (<$>)'s type, and verified…
4
votes
1 answer

Why do Static Arrows generalise Arrows?

It is widely known that Applicative generalises Arrows. In the Idioms are oblivious, arrows are meticulous, monads are promiscuous paper by Sam Lindley, Philip Wadler and Jeremy Yallop it is said that Applicative is equivalent to Static Arrows, that…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
4
votes
1 answer

Where to use `ApplicativeError` instead of `Either`?

There is ApplicativeError[F,E] + F[A] and there is Either[E, A]. Both convey the message that the function could fail with an E or succeed with an A but I'm not sure about the different message they convey to the client about the intended way of…
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
4
votes
2 answers

What does "f (a -> b)" type signature mean in Haskell?

I'm trying to understand applicatives in Haskell. Can't figure out what does following type signature mean: f (a -> b) For example: foo :: Num a => Maybe (a -> a) foo = Just (+1) How can I understand the meaning of Maybe (a -> a)? Is it a…
wspbr
  • 137
  • 4
4
votes
0 answers

Inductive lifting

I would like to combine the various lifts into a single class class Lift a b where lift :: a -> b So that lift can be used in place of fmap, liftA2, liftA3, etc. Now it is easy enough to write the instances for these: instance Functor f => Lift…
Wheat Wizard
  • 3,982
  • 14
  • 34
4
votes
1 answer

The definition for (<*) and (*>)

Can I assume that the below is true for all applicatives ? f1 <* f2 = fmap const f1 <*> f2 and f1 *> f2 = fmap (flip const ) f1 <*> f2
Tomer
  • 1,159
  • 7
  • 15
4
votes
2 answers

When should one use applicatives over monads?

I’ve been using Scala at work and to understand Functional Programming more deeply I picked Graham Hutton’s Programming in Haskell (love it :) In the chapter on Monads I got my first look into the concept of Applicative Functors (AFs) In my…
PhD
  • 11,202
  • 14
  • 64
  • 112
4
votes
1 answer

Are the liftM functions deprived of their monadic essence?

The difference between monad and applicative is that the former can choose the next computation depending on a previous result: (\x -> if x == 1 then (\_ -> []) else (\y -> (\z -> \w -> [x,y,z]) =<< sqr) =<< (+1)) =<< (+1) $ 0 -- …
user5536315
4
votes
3 answers

How to define apply in terms of bind?

In Haskell Applicatives are considered stronger than Functor that means we can define Functor using Applicative like -- Functor fmap :: (a -> b) -> f a -> f b fmap f fa = pure f <*> fa and Monads are considered stronger than Applicatives & Functors…
Pawan Kumar
  • 1,443
  • 2
  • 16
  • 30
4
votes
2 answers

What is the best way to realize `(->) ((->) a b)` as an applicative functor?

I'm working on Problem 19 in Ninety-Nine Haskell Problems, and I've encountered the following difficulty. The problem asks to "rotate a list N places to the left." This could easily be achieved in a pointed way, e.g., rotate :: [a] -> Int ->…
jgaeb
  • 197
  • 8
4
votes
3 answers

In the declaration of class Functor, can the type variables be function types?

In Haskell, the class Functor is declared as: class Functor f where fmap :: (a -> b) -> f a -> f b Can type variables a and b be function types, or must they be non-function types? If they can be function types, isn't it that…
Tim
  • 1
  • 141
  • 372
  • 590
4
votes
4 answers

Applicative functor evaluation is not clear to me

I am currently reading Learn You a Haskell for Great Good! and am stumbling on the explanation for the evaluation of a certain code block. I've read the explanations several times and am starting to doubt if even the author understands what this…
Allen Han
  • 1,163
  • 7
  • 16
4
votes
1 answer

Understand the Applicative problem (#7) in Chapter 12 of "Programming In Haskell"

I've been working through the excellent Programming in Haskell (2nd ed). I'm a bit flummoxed by the question on Applicatives, though. Given the following type: data Expr a = Var a | Val Int | Add (Expr a) (Expr a) deriving Show The question is to…
retnuH
  • 1,525
  • 2
  • 11
  • 18
4
votes
2 answers

Trying to implement "the essence of the iterator pattern"

I came across the paper "https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf" which has code examples in quite an abstract pseudo haskell syntax. I'm struggeling to implement the example from section 6.2. in real haskell. This is how…
Thomas Mahler
  • 180
  • 10
4
votes
2 answers

(fmap.fmap) for Applicative

fmap.fmap allows us to go "two layers deep" into a functor: fmap.fmap :: (a -> b) -> f (g a) -> f (g b) Is this also possible for applicative functors? Let's say I wanted to combine Just (+5) and [1,2,3] by using their applicative properties. I can…
hgiesel
  • 5,430
  • 2
  • 29
  • 56