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

Can I write a higher order type for a -> b -> *?

I understand that (->) a is a higher order type of kind * -> *, that when applied to a type argument b gives the type a -> b Can I write a type of kind * -> * that when applied to c would give a -> b -> c? If not, why not? Maybe using some language…
Tobia
  • 17,856
  • 6
  • 74
  • 93
10
votes
3 answers

How `sequenceA` works

I'm new to Haskell and trying to understand how does this work? sequenceA [(+3),(+2),(+1)] 3 I have started from the definition sequenceA :: (Applicative f) => [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = (:) <$> x <*> sequenceA…
Yola
  • 18,496
  • 11
  • 65
  • 106
10
votes
2 answers

Haskell: Flaw in the description of applicative functor laws in the hackage Control.Applicative article?: it says Applicative determines Functor

I think I found a flaw in the hackage article for Control.Applicative. As a description of the applicative functor laws, it says: class Functor f => Applicative f where A functor with application, providing operations to embed pure expressions…
gksato
  • 368
  • 1
  • 10
10
votes
1 answer

Is there an "ApplicativeIO" class?

Is there somewhere in Hackage a typeclass analogous to MonadIO but for Applicatives, that allows one to easily lift IO actions to "applicative composition stacks" based on IO? If such a typeclass existed, would it be made obsolete by the…
danidiaz
  • 26,936
  • 4
  • 45
  • 95
10
votes
2 answers

Applicative transformer classes

Where are the Applicative transformer classes? I wanted to use transformer classes for the applicative transformer stack in a previous answer, but they don't seem to exist. The transformers package and many others are full of transformers that…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
10
votes
1 answer

Simple Applicative Functor Example

I'm reading the Learn You a Haskell book. I'm struggling to understand this applicative functor code: (*) <$> (+3) <*> (*2) $ 2 This boils down to: (3+2) * (2*2) = 20 I don't follow how. I can expand the above into the less elegant but more…
clay
  • 18,138
  • 28
  • 107
  • 192
10
votes
6 answers

Applicative functors: why can fmap take a function with more than one argument?

I am getting into Haskell and found the book "learn you a Haskell" most helpful. I am up to the section on applicative functors. I am puzzled by the following as it appears in the book: (\x y z -> [x, y, z]) <$> (+3) <*> (*2) <*> (/2) $ 5 which…
Felix Schlitter
  • 501
  • 4
  • 12
10
votes
2 answers

How do I implement an Applicative instance for a parser without assuming Monad?

I can't figure out how to implement an Applicative instance for this parser: newtype Parser m s a = Parser { getParser :: [s] -> m ([s], a) } without assuming Monad m. I expected to only have to assume Applicative m, since the Functor instance…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
10
votes
2 answers

Parsec and Applicative style

can someone help me to understand how to use Applicative style for writing Parsec parsers? This is the code i have: module Main where import Control.Applicative hiding (many) import Text.Parsec import Data.Functor.Identity data Cmd = A | B deriving…
Arg
  • 1,926
  • 21
  • 32
9
votes
4 answers

Merging/Appending Justs in Haskell

I'm trying to do what must be blindingly obvious in Haskell, which is go from Just [1] and Just [2] to Just [1, 2]. However I can't find anything online as I keep finding related but unhelpful pages. So, how do you achieve this?
Dean Barnes
  • 2,252
  • 4
  • 29
  • 53
9
votes
1 answer

ghci special case for Applicative?

In ghci: λ> :t (pure 1) (pure 1) :: (Applicative f, Num a) => f a λ> show (pure 1) :1:1: No instance for (Show (f0 a0)) arising from a use of `show' Possible fix: add an instance declaration for (Show (f0 a0)) In the…
gawi
  • 13,940
  • 7
  • 42
  • 78
9
votes
1 answer

Is there a way to show stepwise how Clojure evaluates a function?

I'm just starting to teach myself Clojure. As part of supplementing my studies I've watched a few UC Berkley lectures by Brian Harvey on the topic of functional programming. In his second lecture on functional programming, at about minute 34, he…
9
votes
2 answers

Haskell - How does this average function work?

I found this implementation of the average function: avg :: [Int] -> Int avg = div . sum <*> length How does this work? I looked at the function that was produced as a result of div . sum: (div . sum) :: (Integral a, Foldable t) => t a -> a -> a I…
xilpex
  • 3,097
  • 2
  • 14
  • 45
9
votes
1 answer

How to transform Either[Future[A], Future[B]] to Future[Either[A, B]]

I have an instance of Either[Future[A], Future[B]] and I would like to transform it to Future[Either[A, B]]. Since my previous question, cats 0.8.1 has been released, changing the structure of the library and dropping Xor in favor of Either, which…
kostja
  • 60,521
  • 48
  • 179
  • 224
9
votes
1 answer

Why ZipList is not the default Applicative Instance for List

I am currently learning Applicatives in Haskell. If I am not wrong, there are two different Applicative instances for Lists, (List and ZipList - the second being defined as a newtype wrapping a List value). The ZipList applicative instances seems…
zeronone
  • 2,912
  • 25
  • 28