nLab applicative functor

Redirected from "applicative functors".
Contents

Contents

Idea

In computer science, applicative functors (also known as idioms) are the programming equivalent of lax monoidal functors with a tensorial strength in category theory.

A strong monad gives rise to an applicative functor, but not all applicative functors result from monads. Unlike monads, applicative functors are closed under composition.

Equivalence

An applicative functorΒ FF in a closed (symmetric) monoidal category (π’ž,βŠ—,I)(\mathcal{C}, {\otimes}, I) is given by natural transformations Ξ· x:xβ†’F(x)\eta_x : x \to F(x) and Ο• x,y:F([x,y])βŠ—F(x)β†’F(y)\phi_{x,y} : F([x, y]) \otimes F(x) \to F(y), satisfying unit and associativity coherence.

class (Functor f) => Applicative f where
  pure  :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b

It defines a strong lax monoidal endofunctor as follows:

  • Ο΅=Ξ· I:Iβ†’F(I)\epsilon = \eta_I : I \to F(I),
  • ΞΌ x,y:F(x)βŠ—F(y)β†’F(Ξ›(id xβŠ—y))βŠ—id F(y)F([y,xβŠ—y])βŠ—F(y)β†’Ο• y,xβŠ—yF(xβŠ—y)\mu_{x,y} : F(x) \otimes F(y) \xrightarrow{F(\Lambda(\mathrm{id}_{x \otimes y})) \otimes \mathrm{id}_{F(y)}} F([y, x \otimes y]) \otimes F(y) \xrightarrow{\phi_{y,x \otimes y}} F(x \otimes y) where Ξ›(id xβŠ—y):xβ†’[y,xβŠ—y]\Lambda(\mathrm{id}_{x \otimes y}) : x \to [y, x \otimes y] is the currying of the identity morphism on xβŠ—yx \otimes y,
  • st x,y=ΞΌ x,y∘(Ξ· xβŠ—id F(y))\mathrm{st}_{x,y} = \mu_{x,y} \circ (\eta_x \otimes \mathrm{id}_{F(y)}).

Conversely, given such an endofunctor (F,Ο΅,ΞΌ,st)(F, \epsilon, \mu, \mathrm{st}), one can consider the following applicative:

  • Ξ· x:x→ρ x βˆ’1xβŠ—Iβ†’id xβŠ—Ο΅xβŠ—F(I)β†’st x,IF(xβŠ—I)β†’F(ρ x)F(x)\eta_x : x \xrightarrow{\rho_x^{-1}} x \otimes I \xrightarrow{\mathrm{id}_x \otimes \epsilon} x \otimes F(I) \xrightarrow{\mathrm{st}_{x,I}} F(x \otimes I) \xrightarrow{F(\rho_x)} F(x), where ρ x:xβŠ—Iβ†’x\rho_x : x \otimes I \to x is the right unitorΒ for βŠ—\otimes,
  • Ο• x,y=F(ev x,y)∘μ [x,y],x\phi_{x,y} = F(\mathrm{ev}_{x,y}) \circ \mu_{[x,y], x}.

Examples

There are two ways to define List as an applicative functor within the category of data types. In Haskell, the first is the default implementation, while the second is accessible via the ZipList newtype. They can be presented as an instance of the Applicative type class, or, equivalently, as an instance of the Monoidal type class.

instance Applicative List where
  pure x = [x]
  fs <*> xs = [ f x | f <- fs, x <- xs ]

instance Monoidal List where
  unit = [()]
  as βŠ— bs = [ (a, b) | a <- as, b <- bs ]

and

instance Applicative List where
  pure x = repeat x
  fs <*> xs = zipWith (\f x -> f x) fs xs

instance Monoidal List where
  unit = repeat ()
  as βŠ— bs = zip as bs

with the canonical strength being defined as follows:

strength :: Functor f => (a, f b) -> f (a, b)
strength (a, fb) = fmap (a,) fb

References

Review:

Last revised on November 4, 2025 at 04:46:29. See the history of this page for a list of all contributions to it.