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

Why is `((,) r)` a Functor that is NOT an Applicative?

From functors that are not applicatives: A type constructor which is a Functor but not an Applicative. A simple example is a pair: instance Functor ((,) r) where fmap f (x,y) = (x, f y) But there is no way how to define its Applicative…
George
  • 6,927
  • 4
  • 34
  • 67
8
votes
2 answers

Why can AccValidation not have a Monad instance?

From the documentation of the validation package: The AccValidation data type is isomorphic to Either, but has an instance of Applicative that accumulates on the error side. That is to say, if two (or more) errors are encountered, they are appended…
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
8
votes
2 answers

How to interpret bind/>>= of the function instance?

I'm trying to improve my understanding of Applicatives and Monads by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all. Here are my implementations of fmap, <*> and…
user6445533
8
votes
1 answer

How to write the <*> when writing the Applicative instance for Reader r

I'm stuck with an exercise in Haskell Book, "Chapter 22. Reader". The exercise says "Implement the Applicative for Reader" and it gives the following: {-# LANGUAGE InstanceSigs #-} newtype Reader r a = Reader { runReader :: r -> a } instance…
Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
8
votes
1 answer

Why do we need to separate Apply and Applicative type classes?

I read in cats documentation about typeclasses Apply and Applicative. I wonder why the library provides two separate type classes instead of just one type class Applicative, which would extend Functor and add ap ? Does anybody use Apply that is not…
Michael
  • 41,026
  • 70
  • 193
  • 341
8
votes
1 answer

Applicative functor that reverses order of effects

Given an applicative functor f, I had an idea of making a new applicative functor Rev f like f but with the order of effects reversed. Here it is: import Control.Applicative newtype Rev f a = Rev {unRev :: f a} instance Functor f => Functor (Rev…
PyRulez
  • 10,513
  • 10
  • 42
  • 87
8
votes
1 answer

Product and Sum Type Parallels in Haskell Type Classes

It appears that type classes such as Applicative, Monad and Arrow have some sort of sum type equivalent in type classes such as Alternative, MonadPlus and ArrowPlus respectively. For example, Applicative and Alternative can be used to define the…
8
votes
1 answer

how to get "leftover arguments" in optparse-applicative?

I am trying to use optparse-applicative. How can I access arguments that are not options? (From prog --foo --bar=42 baz, I want to get ["baz"]) All the the "high level" functions…
d8d0d65b3f7cf42
  • 2,597
  • 15
  • 28
8
votes
2 answers

Why WrappedMonad and WrappedArrow?

Why does the WrappedMonad and WrappedArrow types exist? Is it because Monads were not Applicative? Given that WrappedArrow exists, should the instance Arrow a => Applicative (Arrow a b) simply be built into the Haskell itself the same way that…
yong
  • 3,583
  • 16
  • 32
8
votes
2 answers

Howto create a nested/conditional option with optparse-applicative?

Is possible to create a haskell expression, using the methods in optparse-applicative, that parses program options like this? program [-a [-b]] ... -a and -b are optionals flags (implemented using switch), with the constraint that the -b option…
8
votes
1 answer

JsResult - Monad or Applicative?

My understanding of one of the distinctions between Monad and Applicative is that flatMap is available on Monad, but not Applicative. If that's true, I'm confused by these Scala Play JSON docs: So what’s interesting there is that JsResult[A] is a…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
8
votes
3 answers

optparse-applicative: displaying help for programs invoked with no arguments

I'm using optparse-applicative version 0.7.0.2. I want to write a parser that takes some mandatory options, but that when invoked with no options it shows both usage and help, as opposed to just usage (that is, I want the invocation with no options…
danidiaz
  • 26,936
  • 4
  • 45
  • 95
8
votes
2 answers

How is getZipList defined?

I am curious about where getZipList is defined in ghc. Control.Applicative has this definition for ZipList. newtype ZipList a = ZipList { getZipList :: [a] } One way to use ZipLists is (from LYAH): ghci> getZipList $ (+) <$> ZipList [1,2,3] <*>…
rsinha
  • 2,167
  • 3
  • 18
  • 17
8
votes
2 answers

Applicative Functors and Left from Either

I have been working through the great good book, but I am struggling slightly with Applicative Functors. In the following example max is applied to the contents of the two Maybe functors and returns Just 6. max <$> Just 3 <*> Just 6 Why in the…
Jim Jeffries
  • 9,841
  • 15
  • 62
  • 103
7
votes
3 answers

Explain Traverse[List] implementation in scalaz-seven

I'm trying to understand the traverseImpl implementation in scalaz-seven: def traverseImpl[F[_], A, B](l: List[A])(f: A => F[B])(implicit F: Applicative[F]) = { DList.fromList(l).foldr(F.point(List[B]())) { (a, fbs) => F.map2(f(a), fbs)(_ ::…
betehess
  • 839
  • 4
  • 19