Applicative functorIn functional programming, an applicative functor, or an applicative for short, is an intermediate structure between functors and monads. In Category Theory they are called Closed Monoidal Functors. Applicative functors allow for functorial computations to be sequenced (unlike plain functors), but don't allow using results from prior computations in the definition of subsequent ones (unlike monads). Applicative functors are the programming equivalent of lax monoidal functors with tensorial strength in category theory. Applicative functors were introduced in 2008 by Conor McBride and Ross Paterson in their paper Applicative programming with effects.[1] Applicative functors first appeared as a library feature in Haskell, but have since spread to other languages as well, including Idris, Agda, OCaml, Scala and F#. Glasgow Haskell, Idris, and F# offer language features designed to ease programming with applicative functors.
In Haskell, applicative functors are implemented in the While in languages like Haskell monads are applicative functors, it is not always so in general settings of Category Theory - examples of monads that are not strong can be found on Math Overflow. DefinitionIn Haskell, an applicative is a parameterized type that we think of as being a container for data of the parameter type plus two methods pure :: a -> f a
and can be thought of as bringing values into the applicative. The (<*>) :: f (a -> b) -> f a -> f b
and can be thought of as the equivalent of function application inside the applicative.[2] Alternatively, instead of providing Applicatives are also required to satisfy four equational laws:[3]
Every applicative is a functor. To be explicit, given the methods fmap g x = pure g <*> x
The commonly-used notation ExamplesIn Haskell, the Maybe type can be made an instance of the type class instance Applicative Maybe where
-- pure :: a -> Maybe a
pure a = Just a
-- (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b
Nothing <*> _ = Nothing
_ <*> Nothing = Nothing
(Just g) <*> (Just x) = Just (g x)
As stated in the Definition section, (+) <$> m <*> n
For the non-error case, adding See alsoReferences
External links |