HOME

TheInfoList



OR:

In
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
, an applicative functor, or an applicative for short, is an intermediate structure between
functors In mathematics, specifically category theory, a functor is a mapping between categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) are associated to topological spaces, and m ...
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). Applicative functors are the programming equivalent of lax monoidal functors with tensorial strength in
category theory Category theory is a general theory of mathematical structures and their relations that was introduced by Samuel Eilenberg and Saunders Mac Lane in the middle of the 20th century in their foundational work on algebraic topology. Nowadays, cate ...
. 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 Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
, but have since spread to other languages as well, including Idris,
Agda Agda may refer to: * Agda (programming language), the programming language and theorem prover * Agda (Golgafrinchan), the character in ''The Hitchhiker's Guide to the Galaxy'' by Douglas Adams * Liten Agda, the heroine of a Swedish legend * Agda M ...
,
OCaml OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
, 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.


Definition

In Haskell, an applicative is a parametrized type that we think of as being a container for data of that type plus two methods pure and . Consider a parametrized type f a. The pure method for an applicative of type f has type pure :: 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 (a -> b) -> 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: Every applicative is a functor. To be explicit, given the methods pure and , fmap can be implemented as fmap 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 class Applicative using the following definition: 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, 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.


See also

*
Functor In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
*
Monad Monad may refer to: Philosophy * Monad (philosophy), a term meaning "unit" **Monism, the concept of "one essence" in the metaphysical and theological theory ** Monad (Gnosticism), the most primal aspect of God in Gnosticism * ''Great Monad'', an ...


References


External links


Description of the Applicative typeclass in the Haskell docs


{{Design Patterns Patterns Programming idioms Functional programming Software design patterns