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

Monad laws for an applicative-based formulation

Normally in Haskell we define Monads in terms of return and >>=. Sometimes it's convenient to decompose >>= into fmap and join. The Monad laws for these two formulations are well known and fairly intuitive, once you get used to them. There's another…
SEC
  • 799
  • 4
  • 16
7
votes
1 answer

What is Applicative Builder

I am scala and functional newbie and trying to learn applicatives. val o1 = Some(1) val o2 = Some(2) val o3 = Some(3) val result = (o1 |@| o2 |@| o3) {_ + _ + _} There is a very good read about Applicatives and Functors here As per this…
mogli
  • 1,549
  • 4
  • 29
  • 57
7
votes
1 answer

Applicative instance of List runs forever in composition law test with QuickCheck/Checkers

I want to implement the regular applicative instance for lists, using my customly defined list: import Control.Monad import Test.QuickCheck import Test.QuickCheck.Checkers import Test.QuickCheck.Classes data List a = Nil | Cons a (List a) …
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
7
votes
4 answers

How to implement Future as Applicative in Scala?

Suppose I need to run two concurrent computations, wait for both of them, and then combine their results. More specifically, I need to run f1: X1 => Y1 and f2: X2 => Y2 concurrently and then call f: (Y1, Y2) => Y to finally get a value of Y. I can…
Michael
  • 41,026
  • 70
  • 193
  • 341
7
votes
3 answers

Must I implement Applicative and Functor to implement a Monad

I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = Notheeng Juust x >>= f = f x fail _ = Notheeng…
trevore
  • 425
  • 2
  • 10
7
votes
5 answers

How can I combine a tuple of values with a tuple of functions?

I have scalaZ available. I have an (A, B) and a (A => C, B => D), I'd like to get a (C, D) in a simple and readable way. I feel like there's something I can do with applicatives but I can't find the right methods.
Daenyth
  • 35,856
  • 13
  • 85
  • 124
7
votes
1 answer

How to correctly error out in JSON parsing with Data.Aeson

My type and correponding FromJSON implementation as listed below. The nonEmpty turns a List into a Maybe NonEmpty and I'm trying to correctly deal with the case where the List is indeed empty and I have to abort the parsing. This parsing is actually…
Alexandr Kurilin
  • 7,685
  • 6
  • 48
  • 76
7
votes
3 answers

Applicative Functor on Lists

I understand that following will be: [(+2),(+1)]<*>[1,2,3] == [3,4,5,2,3,4] I also understand that fmap is implemented as map. But how could I mentally map this computation in my head? The first time I saw this I assumed the…
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
7
votes
3 answers

Conditional looping in an Applicative Functor

Suppose that Parser x is a parser that parses an x. This parser probably possesses a many combinator, that parses zero or more occurrences of something (stopping when the item parser fails). I can see how one might implement that if Parser forms a…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
7
votes
2 answers

Can two non-functors compose to a functor?

We can have two types f, g :: * -> * such that they're not monads, but their composition is. For example for an arbitrary fixed s: f a := s -> a g a := (s, a) g a isn't a monad (unless we restrict s to a monoid), but f (g a) is the state monad s ->…
Petr
  • 62,528
  • 13
  • 153
  • 317
7
votes
2 answers

Better Applicative instance for Parser (Haskell)

I'm working through the Brent Yorgey Haskell course, and I'm having trouble defining a good instance for Applicative. A parser is defined as follows: newtype Parser a = Parser { runParser :: String -> Maybe (a, String) } The function takes a…
anjruu
  • 1,224
  • 10
  • 24
7
votes
1 answer

F# record constructor function

Is there a way to call the constructor for an F# record type in F#? My motivation is I've been using the applicative validation from FSharpx but find myself writing lots of boilerplate functions that just construct records. The buildAddress function…
Brownie
  • 7,688
  • 5
  • 27
  • 39
7
votes
1 answer

Is there a standard name or implementation of the "purely applicative Either"?

I frequently find use for what I call the "purely applicative Either", i.e. Either with the Applicative instance available so long as we don't implement a Monad instance as well. newtype AEither e a = AEither { unAEither :: Either e a } deriving…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
7
votes
3 answers

Applicative style for infix operators?

Is there a way to make applicative uses of <$> and <*> look nice when dealing with infix operators? I think that ((++) <$> a <*> ((++) <$> b <*> c )) looks much more cluttered then a ++ b ++ c so I wonder if there is a nicer way.
hugomg
  • 68,213
  • 24
  • 160
  • 246
6
votes
2 answers

Applicative instance for State and other MTL monads?

Looking at the docs for Control.Applicative, I notice that they have instance declarations for certain monads (e.g. IO, Maybe and notably ST), but there are no instances for MTL monads such as State and RWS. Instead it looks like there's a…
mergeconflict
  • 8,156
  • 34
  • 63