Applicative
is the class of types f :: * -> *
which allows lifted function application over a structure where the function is also embedded in that structure.
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
Note the Functor
constraint on f
. The pure
function returns its argument embedded in the Applicative
structure. The infix function <*>
(pronounced "apply") is very similar to fmap
except with the function embedded in the Applicative
structure.
A correct instance of Applicative
should satisfy the applicative laws, though these are not enforced by the compiler:
pure id <*> a = a -- identity
pure (.) <*> a <*> b <*> c = a <*> (b <*> c) -- composition
pure f <*> pure a = pure (f a) -- homomorphism
a <*> pure b = pure ($ b) <*> a -- interchange