I was able to map Functor's definition from category theory to Haskell's definition in the following way: since objects of Hask
are types, the functor F
a
of Hask
to the new type F a
by, roughly saying, prepending "F " to it.a -> b
of Hask
to the new morphism F a -> F b
using fmap :: (a -> b) -> (f a -> f b)
.So far, so good. Now I get to the Applicative
, and can't find any mention of such a concept in textbooks. By looking at what it adds to Functor
, ap :: f (a -> b) -> f a -> f b
, I tried to come up with my own definition.
First, I noticed that since (->)
is also a type, morphisms of Hask
are objects of it too. In light of this, I made a suggestion that applicative functor is a functor that also can map "arrow"-objects of source category into morphisms of the destination one.
Is this a right intuition? Can you provide a more formal and rigorous definition?
A Functor is kind of mapping of objects and morphisms that preserves composition and identity. We have two Categories: A and B . In Category A we have two objects a and b with morphism f . Our Functor is a mapping of objects a and b to Fa and Fb and mapping of morphisms, in this case single morphism: f to Ff .
Morphisms in this category are natural transformations between functors. Functors are often defined by universal properties; examples are the tensor product, the direct sum and direct product of groups or vector spaces, construction of free groups and modules, direct and inverse limits.
Functor categories serve as the hom-categories in the strict 2-category Cat. In the context of enriched category theory the functor category is generalized to the enriched functor category.
Functors apply a function to a wrapped value: Applicatives apply a wrapped function to a wrapped value: Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function >>= (pronounced "bind") to do this.
The key to understanding applicative functors is to figure out what structure they preserve.
Regular functors preserve the basic categorical structure: they map objects and morphisms between categories, and they preserve the laws of the category (associativity and identity).
But a category may have more structure. For instance, it may allow the definition of mappings that are like morphisms but take multiple arguments. Such mappings are defined by currying: e.g., a function of two arguments is defined as a function of one argument returning another function. This is possible if you can define an object that represents a function type. In general, this object is called an exponential (in Haskell, it's just the type b->c
). We can then have morphisms from one object to an exponential and call it a two-argument morphism.
The traditional definition of an applicative functor in Haskell is based on the idea of mapping functions of multiple arguments. But there is an equivalent definition that splits the multi-argument function along a different boundary. You can look at such a function as a mapping of a product (a pair, in Haskell) to another type (here, c
).
a -> (b -> c) ~ (a, b) -> c
That allows us to look at applicative functors as functors that preserve the product. But a product is just one example of what is called a monoidal structure.
In general, a monoidal category is a category equipped with a tensor product and a unit object. In Haskell, this could be, for instance, the cartesian product (a pair) and the unit type ()
. Notice, however that monoidal laws (associativity and unit laws) are valid only up to an isomorphism. For instance:
(a, ()) ~ a
An applicative functor could then be defined as a functor that preserves monoidal structure. In particular, it should preserve the unit and the product. It shouldn't matter whether we do the "multiplication" before or after applying the functor. The results should be isomorphic.
However, we don't really need a full-blown monoidal functor. All we need is two morphisms (as opposed to isomorphisms) -- one for multiplication and one for unit. Such a functor that half-preserves the monoidal structure is called a lax monoidal functor. Hence the alternative definition:
class Functor f => Monoidal f where unit :: f () (**) :: f a -> f b -> f (a, b)
It's easy to show that Monoidal
is equivalent to Applicative
. For instance, we can get pure
from unit
and vice versa:
pure x = fmap (const x) unit unit = pure ()
The applicative laws follow simply from the preservation of monoid laws (associativity and unit laws).
In category theory, preservation of monoidal structure is related to tensorial strength, so an applicative functor is also known as a strong lax monoidal functor. However, in Hask, every functor has canonical strength with respect to the product, so this property doesn't add anything to the definition.
Now, if you're familiar with the definition of a monad as a monoid in the category of endofunctors, you might be interested to know that applicatives are, similarly, monoids in the category of endofunctors where the tensor product is the Day convolution. But that's much harder to explain.
You're right, Applicative
translates less straightforwardly than Functor
or Monad
. But in essence, it is the class of monoidal functors:
class Functor f => Monoidal f where pureUnit :: f () fzip :: f a -> f b -> f (a,b)
From that you can define – within Hask –
pure x = fmap (const x) pureUnit
and
fs <*> xs = fmap (uncurry ($)) $ fzip fs xs
See this answer for a full proof that Applicative
and Monoidal
are really equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With