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

How to compose function to applicatives with scalaz

While learning Scalaz 6, I'm trying to write type-safe readers returning validations. Here are my new types: type ValidReader[S,X] = (S) => Validation[NonEmptyList[String],X] type MapReader[X] = ValidReader[Map[String,String],X] and I have two…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
11
votes
3 answers

Are there human-friendly names for applicative (and friends) methods?

I've been using applicative (and alternative) a fair bit lately, and one thing that has been frustrating me is my lack of knowledge of the nomenclature. As an example, I'd like to be able to say function name instead of star thing for <*>. So, in…
lukerandall
  • 2,201
  • 17
  • 29
11
votes
1 answer

Does * in (<*>) have a special meaning?

Trying to expand my understanding about symbols in Haskell : ($) : Function Application operator (Allow you to apply arguments over a function) (&) : flipped version of Function Application Operator? (&) = flip ($) (<>) : associative operator…
11
votes
2 answers

Meaning and usage of complex number functor and monad?

I was kinda surprised when I read the source code of instances of Applicative Complex and Monad Complex from GHC Data.Complex module: -- | @since 4.9.0.0 instance Applicative Complex where pure a = a :+ a f :+ g <*> a :+ b = f a :+ g b liftA2…
Dannyu NDos
  • 2,458
  • 13
  • 32
11
votes
6 answers

applicative functor: <*> and partial application, how it works

I am reading the book Programming in Haskell by Graham Hutton and I have some problem to understand how <*> and partial application can be used to parse a string. I know that pure (+1) <*> Just 2 produces Just 3 because pure (+1) produces Just (+1)…
yaa
  • 129
  • 1
  • 4
11
votes
1 answer

ApplicativeDo language extension with `Parsing` applicative still looking for Monad instance

I'm trying to write a parser using the parsers package using do syntax. Here is an example: {-# LANGUAGE ApplicativeDo #-} import Text.Parser.Char (string, spaces) import Text.Parser.Token (TokenParsing, natural) issueParser :: TokenParsing p => p…
Asa
  • 1,466
  • 9
  • 27
11
votes
0 answers

A categorical analogue for some applicative infrastructure I used?

I've been reading a bit about the category theoric interpretation of Haskell Applicative functors: apparently, they can be interpreted in category theoretic terms as either lax closed functors or lax monoidal functors (depending on who you ask, it…
Dominique Devriese
  • 2,998
  • 1
  • 15
  • 21
11
votes
1 answer

Haskell - Evaluation of (+) <$> (+3) <*> (*100) $ 5

From the chapter on Functors in Learn You a Haskell for Great Good, Lipovača states: "When we do (+) <$> (+3) <*> (*100), we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example,…
rjs
  • 838
  • 2
  • 10
  • 21
11
votes
4 answers

Examples of Haskell Applicative Transformers

The wiki on www.haskell.org tells us the following about Applicative Transformers: So where are applicative transformers? The answer is, that we do not need special transformers for applicative functors since they can be combined in a generic way.…
user1308560
  • 409
  • 2
  • 9
10
votes
7 answers

Functional composition of Optionals

I have 2 Optionals (or Maybe objects) that I would like to combine so that I get the following results: || first operand second ++-------------+------------- operand || empty | optional(x) …
raner
  • 1,175
  • 1
  • 11
  • 21
10
votes
3 answers

Applicative without a functor

I have a type Image which is basically an c-array of floats. It is easy to create functions such as map :: (Float -> Float) -> Image -> Image, or zipWith :: (Float -> Float -> Float) -> Image -> Image -> Image. However, I have a feeling that it…
aleator
  • 4,436
  • 20
  • 31
10
votes
5 answers

Is Applicative IO implemented based on functions from Monad IO?

In "Learn You a Haskell for Great Good!" author claims that Applicative IO instance is implemented like this: instance Applicative IO where pure = return a <*> b = do f <- a x <- b return (f x) I might be wrong, but…
gorsky
  • 2,282
  • 1
  • 16
  • 22
10
votes
2 answers

Resolving the type of `f = f (<*>) pure`

Recently I noticed that humourously liftA can be written as liftA (<*>) pure I thought this was neat and so as a bit of a joke I thought I would make a new "definition" of liftA based on this property: f = f (<*>) pure Now I had expected that this…
Wheat Wizard
  • 3,982
  • 14
  • 34
10
votes
2 answers

Computational cost of applicative style

I am using a small database pool in my web app. And this particular function: withPool pool = bracket (takeConn pool) (putConn pool) can be rewritten in applicative style: withPool = bracket <$> takeConn <*> putConn Arguably it is just as readable…
Vagif Verdi
  • 4,816
  • 1
  • 26
  • 31
10
votes
2 answers

Is there a zipWith analogue for tuples?

Preliminary note: this is a respin of a deleted question by SeanD. Just like there is zipWith for lists... GHCi> zipWith (+) [1,2] [3,4] [4,6] ... it feels like there should be something analogous for tuples, in the spirit of... tupleZipWith (+)…
duplode
  • 33,731
  • 7
  • 79
  • 150