Earlier I asked about translating monadic code to use only the applicative functor instance of Parsec. Unfortunately I got several replies which answered the question I literally asked, but didn't really give me much insight. So let me try this again...
Summarising my knowledge so far, an applicative functor is something which is somewhat more restricted than a monad. In the tradition of "less is more", restricting what the code can do increases the possibilities for crazy code manipulation. Regardless, a lot of people seem to believe that using applicative instead of monad is a superior solution where it's possible.
The Applicative
class is defined in Control.Applicative
, whose Haddock's listing helpfully separates the class methods and utility functions with a vast swathe of class instances between them, to make it hard to quickly see everything on screen at once. But the pertinent type signatures are
pure :: x -> f x
<*> :: f (x -> y) -> f x -> f y
*> :: f x -> f y -> f y
<* :: f x -> f y -> f x
<$> :: (x -> y) -> f x -> f y
<$ :: x -> f y -> f x
Makes perfect sense, right?
Well, Functor
already gives us fmap
, which is basically <$>
. I.e., given a function from x
to y
, we can map an f x
to an f y
. Applicative
adds two essentially new elements. One is pure
, which has roughly the same type as return
(and several other operators in various category theory classes). The other is <*>
, which gives us the ability to take a container of functions and a container of inputs and produce a container of outputs.
Using the operators above, we can very neatly do something such as
foo <$> abc <*> def <*> ghi
This allows us to take an N-ary function and source its arguments from N functors in a way which generalises easily to any N.
This much I already understand. There are two main things which I do not yet understand.
First, the functions *>
, <*
and <$
. From their types, <* = const
, *> = flip const
, and <$
could be something similar. Presumably this does not describe what these functions actually do though. (??!)
Second, when writing a Parsec parser, each parsable entity usually ends up looking something like this:
entity = do
var1 <- parser1
var2 <- parser2
var3 <- parser3
...
return $ foo var1 var2 var3...
Since an applicative functor does not allow us to bind intermediate results to variables in this way, I'm puzzled as to how to gather them up for the final stage. I haven't been able to wrap my mind around the idea fully enough in order to comprehend how to do this.
You can view functors, applicatives and monads like this: They all carry a kind of "effect" and a "value". (Note that the terms "effect" and "value" are only approximations - there doesn't actually need to be any side effects or values - like in
Identity
orConst
.)Functor
you can modify possible values inside usingfmap
, but you cannot do anything with effects inside.With
Applicative
, you can create a value without any effect withpure
, and you can sequence effects and combine their values inside. But the effects and values are separate: When sequencing effects, an effect cannot depend on the value of a previous one. This is reflected in<*
,<*>
and*>
: They sequence effects and combine their values, but you cannot examine the values inside in any way.You could define
Applicative
using this alternative set of functions:(where
pureUnit
doesn't carry any effect) and definepure
and<*>
from them (and vice versa). Herepair
sequences two effects and remembers the values of both of them. This definition expresses the fact thatApplicative
is a monoidal functor.Now consider an arbitrary (finite) expression consisting of
pair
,fmap
,pureUnit
and some primitive applicative values. We have several rules we can use:Using these rules, we can reorder
pair
s, pushfmap
s outwards and eliminatepureUnit
s, so eventually such expression can be converted intoor
So indeed, we can first collect all effects together using
pair
and then modify the resulting value inside using a pure function.With
Monad
, an effect can depend on the value of a previous monadic value. This makes them so powerful.I can make a few remarks here, hopefully helpful. This reflects my understanding which itself might be wrong.
pure
is unusually named. Usually functions are named referring to what they produce, but inpure x
it isx
that is pure.pure x
produces an applicative functor which "carries" the purex
. "Carries" of course is approximate. An example:pure 1 :: ZipList Int
is aZipList
, carrying a pureInt
value,1
.<*>
,*>
, and<*
are not functions, but methods (this answers your first concern).f
in their types is not general (like it would be, for functions) but specific, as specified by a specific instance. That's why they are indeed not just$
,flip const
andconst
. The specialized typef
specifies the semantics of combination. In the usual applicative style programming, combination means application. But with functors, an additional dimension is present, represented by the "carrier" typef
. Inf x
, there is a "contents",x
, but there is also a "context",f
.The "applicative functors" style sought to enable the "applicative style" programming, with effects. Effects being represented by functors, carriers, providers of context; "applicative" referring to the normal applicative style of functional application. Writing just
f x
to denote application was once a revolutionary idea. There was no need for additional syntax anymore, no(funcall f x)
, noCALL
statements, none of this extra stuff - combination was application... Not so, with effects, seemingly - there was again that need for the special syntax, when programming with effects. The slain beast reappeared again.So came the Applicative Programming with Effects to again make the combination mean just application - in the special (perhaps effectful) context, if they were indeed in such context. So for
a :: f (t -> r)
andb :: f t
, the (almost plain) combinationa <*> b
is an application of carried contents (or typest -> r
andt
), in a given context (of typef
).The main distinction from monads is, monads are non-linear. In
the computation
b x
depends onx
, andc x y
depends on bothx
andy
. The functions are nested:If
b
andc
do not depend on the previous results (x
,y
), this can be made flat by making the computation stages return repackaged, compound data (this addresses your second concern):and this is essentially an applicative style (
b
,c
are fully known in advance, independent of the valuex
produced bya
, etc.). So when your combinations create data that encompass all the information they need for further combinations, and there's no need for "outer variables" (i.e. all computations are already fully known, independent of any values produced by any of the previous stages), you can use this style of combination.But if your monadic chain has branches dependent on values of such "outer" variables (i.e. results of previous stages of monadic computation), then you can't make a linear chain out of it. It is essentially monadic then.
As an illustration, the first example from that paper shows how the "monadic" function
can actually be coded in this "flat, linear" style as
There's no use here for the monad's ability to branch on previous results.
a note on the excellent Petr Pudlák's answer: in my "terminology" here, his
pair
is combination without application. It shows that the essence of what the Applictive Functors add to plain Functors, is the ability to combine. Application is then achieved by the good oldfmap
. This suggests combinatory functors as perhaps a better name (update: in fact, "Monoidal Functors" is the name).The answers already given are excellent, but there's one small(ish) point I'd like to spell out explicitly, and it has to do with
<*
,<$
and*>
.One of the examples was
which can also be written as
foo <$> parser1 <*> parser2 <*> parser3
.Suppose that the value of
var2
is irrelevant forfoo
- e.g. it's just some separating whitespace. Then it also doesn't make sense to havefoo
accept this whitespace only to ignore it. In this casefoo
should have two parameters, not three. Usingdo
-notation, you can write this as:If you wanted to write this using only
<$>
and<*>
it should be something like one of these equivalent expressions:But that's kind of tricky to get right with more arguments!
However, you can also write
foo <$> parser1 <* parser2 <*> parser3
. You could callfoo
the semantic function which is fed the result ofparser1
andparser3
while ignoring the result ofparser2
in between. The absence of>
is meant to be indicative of the ignoring.If you wanted to ignore the result of
parser1
but use the other two results, you can similarly writefoo <$ parser1 <*> parser2 <*> parser3
, using<$
instead of<$>
.I've never found much use for
*>
, I would normally writeid <$ p1 <*> p2
for the parser that ignores the result ofp1
and just parses withp2
; you could write this asp1 *> p2
but that increases the cognitive load for readers of the code.I've learnt this way of thinking just for parsers, but it has later been generalised to
Applicative
s; but I think this notation comes from the uuparsing library; at least I used it at Utrecht 10+ years ago.I'd like to add/reword a couple things to the very helpful existing answers:
Applicatives are "static". In
pure f <*> a <*> b
,b
does not depend ona
, and so can be analyzed statically. This is what I was trying to show in my answer to your previous question (but I guess I failed -- sorry) -- that since there was actually no sequential dependence of parsers, there was no need for monads.The key difference that monads bring to the table is
(>>=) :: Monad m => m a -> (a -> m b) -> m a
, or, alternatively,join :: Monad m => m (m a)
. Note that whenever you havex <- y
insidedo
notation, you're using>>=
. These say that monads allow you to use a value "inside" a monad to produce a new monad, "dynamically". This cannot be done with an Applicative. Examples:Lastly, Applicatives enable lifted function application as mentioned by @WillNess. To try to get an idea of what the "intermediate" results look like, you can look at the parallels between normal and lifted function application. Assuming
add2 = (+) :: Int -> Int -> Int
:Unfortunately, you can't meaningfully print the result of
pure add2 <*> pure 3
for the same reason that you can't foradd2
... frustrating. You may also want to look at theIdentity
and its typeclass instances to get a handle on Applicatives.The
<*
and*>
functions are very simple: they work the same way as>>
. The<*
would work the same way as<<
except<<
does not exist. Basically, givena *> b
, you first "do"a
, then you "do"b
and return the result ofb
. Fora <* b
, you still first "do"a
then "do"b
, but you return the result ofa
. (For appropriate meanings of "do", of course.)The
<$
function is justfmap const
. Soa <$ b
is equal tofmap (const a) b
. You just throw away the result of an "action" and return a constant value instead. TheControl.Monad
functionvoid
, which has a typeFunctor f => f a -> f ()
could be written as() <$
.These three functions are not fundamental to the definition of an applicative functor. (
<$
, in fact, works for any functor.) This, again, is just like>>
for monads. I believe they're in the class to make it easier to optimize them for specific instances.When you use applicative functors, you do not "extract" the value from the functor. In a monad, this is what
>>=
does, and whatfoo <- ...
desugars to. Instead, you pass the wrapped values into a function directly using<$>
and<*>
. So you could rewrite your example as:If you want intermediate variables, you could just use a
let
statement:As you correctly surmised,
pure
is just another name forreturn
. So, to make the shared structure more obvious, we can rewrite this as:I hope this clarifies things.
Now just a little note. People do recommend using applicative functor functions for parsing. However, you should only use them if they make more sense! For sufficiently complex things, the monad version (especially with do-notation) can actually be clearer. The reason people recommend this is that
is both shorter and more readable than
Essentially, the
f <$> a <*> b <*> c
is essentially like lifted function application. You can imagine the<*>
being a replacement for a space (e.g. function application) in the same way thatfmap
is a replacement for function application. This should also give you an intuitive notion of why we use<$>
--it's like a lifted version of$
.