Applicative functor

From English Wikipedia @ Freddythechick

This is the current revision of this page, as edited by imported>Egidio24 at 20:11, 29 May 2024 (v2.05 - Fix errors for CW project (Link equal to linktext)). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In 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 Applicative type class.

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.

Definition

In 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 and <syntaxhighlight lang="text" class="" style="" inline="1"><*></syntaxhighlight>. The pure method for an applicative of parameterized type f has type <syntaxhighlight lang="haskell"> pure :: a -> f a </syntaxhighlight> and can be thought of as bringing values into the applicative. The <syntaxhighlight lang="text" class="" style="" inline="1"><*></syntaxhighlight> method for an applicative of type f 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.[2]

Alternatively, instead of providing <syntaxhighlight lang="text" class="" style="" inline="1"><*></syntaxhighlight>, 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.[3]

Applicatives are also required to satisfy four equational laws:[3]

  • Identity: <syntaxhighlight lang="haskell" class="" style="" inline="1">pure id <*> v = v</syntaxhighlight>
  • Composition: <syntaxhighlight lang="haskell" class="" style="" inline="1">pure (.) <*> u <*> v <*> w = u <*> (v <*> w)</syntaxhighlight>
  • Homomorphism: <syntaxhighlight lang="haskell" class="" style="" inline="1">pure f <*> pure x = pure (f x)</syntaxhighlight>
  • Interchange: <syntaxhighlight lang="haskell" class="" style="" inline="1">u <*> pure y = pure ($ y) <*> u</syntaxhighlight>

Every applicative is a functor. To be explicit, given the methods pure and <syntaxhighlight lang="text" class="" style="" inline="1"><*></syntaxhighlight>, fmap can be implemented as[3] <syntaxhighlight lang="haskell"> fmap g x = pure g <*> x </syntaxhighlight>

The commonly-used notation <syntaxhighlight lang="haskell" class="" style="" inline="1">g <$> x</syntaxhighlight> is equivalent to <syntaxhighlight lang="haskell" class="" style="" inline="1">pure g <*> x</syntaxhighlight>.

Examples

In Haskell, the Maybe type can be made an instance of the type class Applicative using the following definition:[2] <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, pure turns an a into a <syntaxhighlight lang="haskell" class="" style="" inline="1">Maybe a</syntaxhighlight>, and <syntaxhighlight lang="text" class="" style="" inline="1"><*></syntaxhighlight> 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 <syntaxhighlight lang="haskell" class="" style="" inline="1">m :: Maybe Int</syntaxhighlight> and <syntaxhighlight lang="haskell" class="" style="" inline="1">n :: Maybe Int</syntaxhighlight>, one needs only write <syntaxhighlight lang="haskell"> (+) <$> m <*> n </syntaxhighlight> For the non-error case, adding <syntaxhighlight lang="haskell" class="" style="" inline="1">m=Just i</syntaxhighlight> and <syntaxhighlight lang="haskell" class="" style="" inline="1">n=Just j</syntaxhighlight> gives <syntaxhighlight lang="haskell" class="" style="" inline="1">Just(i+j)</syntaxhighlight>. If either of <syntaxhighlight lang="haskell" class="" style="" inline="1">m</syntaxhighlight> or <syntaxhighlight lang="haskell" class="" style="" inline="1">n</syntaxhighlight> is <syntaxhighlight lang="haskell" class="" style="" inline="1">Nothing</syntaxhighlight>, then the result will be <syntaxhighlight lang="haskell" class="" style="" inline="1">Nothing</syntaxhighlight> also. This example also demonstrates how applicatives allow a sort of generalized function application.

See also

References

  1. ^ McBride, Conor; Paterson, Ross (2008-01-01). "Applicative programming with effects". Journal of Functional Programming. 18 (1): 1–13. CiteSeerX 10.1.1.114.1555. doi:10.1017/S0956796807006326. ISSN 1469-7653.
  2. ^ 2.0 2.1 Hutton, Graham (2016). Programming in Haskell (2 ed.). pp. 157–163.
  3. ^ 3.0 3.1 3.2 "Control.Applicative".

External links