To describe the applicative style, first we need to understand functors, or instances of the Functor
type class. Typically, they are the structures that can be mapped over. The Functor
type class defined in Functor.hs
.
Thus fmap
defines how to map a function to another function defined on the instances of this type class. It is illustrated with the Maybe
functor in MaybeFunctor.hs
.
Here we defined that if Maybe
has a value, then the function must be applied to that value, otherwise Nothing
should be returned. Thus we can promote a function over primitive types to another function between the two Maybe
values.
Applicative functor is a functor with additional property: you can apply function inside a functor to values that can be either outside or inside the functor. This is defined in Applicative.hs
.
So this definition introduces a class constraint. It says that each instance of the Applicative
type class must also be an instance of Functor
. Thus it has the...