{{Short description|Intermediate structure between functors and monads}} 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 (unlike plain functors), but don't allow using results from prior computations in the definition of subsequent ones (unlike monads).
In terms of category theory, applicative functors may be defined as lax monoidal functors with tensorial strength<ref name="mcbridepaterson2008">{{Cite journal|last1=McBride|first1=Conor|last2=Paterson|first2=Ross|date=2008-01-01|title=Applicative programming with effects|journal=Journal of Functional Programming|volume=18|issue=1|pages=1–13|doi=10.1017/S0956796807006326|issn=1469-7653|citeseerx=10.1.1.114.1555}}</ref>. 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''.<ref name="mcbridepaterson2008" />
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 <code>Applicative</code> type class.
While in languages like Haskell monads are applicative functors, this is not always the case in general settings of category theory{{nowrap|{{emdash}}}}examples of monads which are ''not'' strong can be found on [https://mathoverflow.net/questions/85391/any-example-of-a-non-strong-monad Math Overflow].
== 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: <code>pure</code> and {{code|code=<*>}}. The <code>pure</code> method for an applicative of parameterized type <code>f</code> has type <syntaxhighlight lang="haskell"> pure :: a -> f a </syntaxhighlight> and can be thought of as bringing values into the applicative. The {{code|code=<*>}} method for an applicative of type <code>f</code> has type <syntaxhighlight lang="haskell"> (<*>) :: f (a -> b) -> f a -> f b </syntaxhighlight> and can be thought of as the equivalent of function application inside the applicative.<ref name="Hutton">{{Cite book|last=Hutton|first=Graham|date=2016|title=Programming in Haskell|edition=2|pages=157–163}}</ref>
Alternatively, instead of providing {{code|code=<*>}}, one may provide a function called <code>liftA2</code>. These two functions may be defined in terms of each other; therefore only one is needed for a minimally complete definition.<ref name="Hackage">{{Cite web|title=Control.Applicative|url=https://hackage.haskell.org/package/base-4.14.1.0/docs/Control-Applicative.html}}</ref>
Applicatives are also required to satisfy four equational laws:<ref name="Hackage" /> * Identity: {{code|lang=haskell|code=pure id <*> v = v}} * Composition: {{code|lang=haskell|code=pure (.) <*> u <*> v <*> w = u <*> (v <*> w)}} * Homomorphism: {{code|lang=haskell|code=pure f <*> pure x = pure (f x)}} * Interchange: {{code|lang=haskell|code=u <*> pure y = pure ($ y) <*> u}}
Every applicative is a functor. To be explicit, given the methods <code>pure</code> and {{code|code=<*>}}, <code>fmap</code> can be implemented as<ref name="Hackage" /> <syntaxhighlight lang="haskell"> fmap g x = pure g <*> x </syntaxhighlight>
The commonly used notation {{code|lang=haskell|code=g <$> x}} is equivalent to {{code|lang=haskell|code=pure g <*> x}}.
== Examples ==
In Haskell, the Maybe type can be made an instance of the type class <code>Applicative</code> using the following definition:<ref name="Hutton" /> <syntaxhighlight lang="haskell"> 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) </syntaxhighlight> As stated in the Definition section, <code>pure</code> turns an <code>a</code> into a {{code|lang=haskell|code=Maybe a}}, and {{code|code=<*>}} applies a Maybe function to a Maybe value. Using the Maybe applicative for type <code>a</code> allows one to operate on values of type <code>a</code> with the error being handled automatically by the applicative machinery. For example, to add {{code|lang=haskell|code=m :: Maybe Int}} and {{code|lang=haskell|code=n :: Maybe Int}}, one needs only write <syntaxhighlight lang="haskell"> (+) <$> m <*> n </syntaxhighlight> For the non-error case, adding {{code|lang=haskell|code=m=Just i}} and {{code|lang=haskell|code=n=Just j}} gives {{code|lang=haskell|code=Just(i+j)}}. If either of {{code|lang=haskell|code=m}} or {{code|lang=haskell|code=n}} is {{code|lang=haskell|code=Nothing}}, then the result will be {{code|lang=haskell|code=Nothing}} also. This example also demonstrates how applicatives allow a sort of generalized function application.
== See also == * Functor * Monad
==References== {{Reflist}}
==External links== * [https://hackage.haskell.org/package/base-4.15.0.0/docs/Control-Applicative.html Description of the Applicative typeclass in the Haskell docs] * [https://hackage.haskell.org/package/base-4.15.0.0/docs/src/GHC-Base.html#Applicative Definition of the Applicative typeclass in the Glasgow Haskell Prelude]
{{Design Patterns Patterns}}
Category:Programming idioms Category:Functional programming Category:Software design patterns