Applicative functor
In functional programming, an applicative functor, or an applicative for short, is an intermediate structure between functors and monads. Applicative functors allow for functorial computations to be sequenced, but don't allow using results from prior computations in the definition of subsequent ones.
In terms of category theory, applicative functors may be defined as lax monoidal functors with tensorial strength. This may not be the most obvious categorification of the standard definition of applicative functor given below, but they are equivalent in the category of Haskell types. Note that the observation that all monads are applicative functors is specific to the category of Haskell types, and is not true in general with this categorical definition of applicative functor; monads in an arbitrary category need not preserve any monoidal product.
Applicative functors were introduced in 2008 by Conor McBride and Ross Paterson in their paper Applicative programming with effects.
Applicative functors first appeared as a library feature in Haskell, but have since spread to other languages such as 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
Applicative type class.While in languages like Haskell monads are applicative functors, this is not always the case in general settings of category theoryexamples of monads which are not strong can be found on .
Definition
In Haskell, an applicative is a parameterized type that can be thought of as being a container for data of the parameter type with two additional methods:pure and. The pure method for an applicative of parameterized type f has typepure :: a -> f a
and can be thought of as bringing values into the applicative. The method for an applicative of type
f has type:: f -> f a -> f b
and can be thought of as the equivalent of function application inside the applicative.
Alternatively, instead of providing, one may provide a function called
liftA2. These two functions may be defined in terms of each other; therefore only one is needed for a minimally complete definition.Applicatives are also required to satisfy four equational laws:
- Identity:
- Composition:
- Homomorphism:
- Interchange:
pure and, fmap can be implemented asfmap g x = pure g <*> x
The commonly used notation is equivalent to.
Examples
In Haskell, the Maybe type can be made an instance of the type classApplicative using the following definition:instance Applicative Maybe where
-- pure :: a -> Maybe a
pure a = Just a
-- :: Maybe -> Maybe a -> Maybe b
Nothing <*> _ = Nothing
_ <*> Nothing = Nothing
<*> = Just
As stated in the Definition section,
pure turns an a into a, and applies a Maybe function to a Maybe value. Using the Maybe applicative for type a allows one to operate on values of type a with the error being handled automatically by the applicative machinery. For example, to add and, one needs only write<$> m <*> n
For the non-error case, adding and gives.
If either of or is, then the result will be also. This example also demonstrates how applicatives allow a sort of generalized function application.