95 points · by lainon · 8y ago · 29 comments
For example, take a list of number and a function that takes 2 parameters, x and y. Maybe the function adds x to y (it doesn't matter). Pass these to fmap. Your result will be a list of partially applied functions with x being set to the values in the list. Now we want to apply the y parameter. We need something similar to fmap, but rather than taking a function and a functor, it needs to take a functor containing functions and a functor containing data. It will then apply the data to the functions and you will end up with a functor containing the result.
That's all applicative is. It applies the subsequent parameters to the result of having run fmap on functions that have more than one parameter. It shows up in a lot of different places, though and is incredibly handy. When I'm writing FP style code in languages that don't normally curry parameters, I often find myself currying parameters precisely because I want applicative ;-). It's just super convenient.
NB: pure being part of applicative is really interesting. I don't know for sure, but I'm relatively sure that a functor is applicative IFF your can define pure for it, which is really interesting to think about.
Edit: weird wording
I blame this on Haskell's overreliance on currying. If applicative was explained in terms of (pure and) liftA2 aka map2, which is completely equivalent to defining it in terms of ap, the analogy with Functor would be much more obvious.
> NB: pure being part of applicative is really interesting. I don't know for sure, but I'm relatively sure that a functor is applicative IFF your can define pure for it, which is really interesting to think about.
Not true. Const b (i.e. f a = b for all a) forms a valid functor and you can define pure as long as there's at least one value of type b (pure a = 1 where 1 is that value), but if b is some type that does not form a monoid (e.g. nonzero octonions) then Const b does not from an applicative.
In the previous post he learns how to use an Applicative.
In this post, he learns how to define Applicative for a new type he's created.
Yes, Applicative is abstract enough that it takes a few concrete examples to get a feel for it and grok what it's purpose is.
This is the work that's required to learn a new abstraction. All abstractions that are sufficiently different from previous experience require this kind of effort to make it legible to you.
Once you do, it kind of just 'snaps' and you get it.
Then, you get to leverage that knowledge in all the places it is re-used, instead of having to read the library code doing data manipulation in a bespoke way - you can just look at the type can say "yes, this is an Applicative - i know how to manipulate it and it will accord to some laws i can depend on", without having to read the source code.
import Control.Monad (ap)
instance Applicative Tree where
pure = return
(<*>) = ap
Also, given a Traversable instance—which is essentially just "fmap with effects"—one can get Foldable and Functor for free via foldMapDefault and fmapDefault.The really interesting cases are the ones where you can define an instance for Applicative but not Monad.
import Control.Applicative
newtype ZList a = ZList { runZList :: [a] }
instance Functor ZList where
fmap f = ZList . fmap f . runZList
instance Applicative ZList where
pure a = ZList [a]
(<*>) f x = ZList $ zipper (runZList f) (runZList x)
where zipper (f:fs) (x:xs) = f x : zipper fs xs
zipper [] _ = []
zipper _ [] = []
Difference of behaviour: *Main> ZipList [(*3),(*5),(*6)] <*> ZipList [1,2,3,4]
ZipList {getZipList = [3,10,18]}
*Main> [(*3),(*5),(*6)] <*> [1,2,3,4]
[3,6,9,12,5,10,15,20,6,12,18,24]
*Main> pure id <*> ZipList [x, y, z] == ZipList [x]
and thus breaks one of the Applicative laws: pure id <*> v == v
The standard ZipList definition is `pure a = ZipList (repeat a)` (an infinite list).The issue with defining a Monad instance for ZList is that mapping the function on the right of (>>=) over the ZList on the left gets you a ZList of `ZList b`, with no meaningful way to zip these ZLists together.
It is interesting that he knew this fact,and that it would be nice if this code could be automatically derived too, but I think that should not replace the learning process.
It is more of a nit for advanced users
mf `ap` ma = do
f <- mf
a <- ma
return (f a)
With a Monad, the action on the right of the bind operator is a function of the result of the action on the left. The second operand of the `ap` function, however, does not have any access to the result of the first operand. The two operands are evaluated independently, apart from their side-effects. That reflects the fundamental difference between Applicative and Monad: an Applicative instance just sequences effects and combines results, whereas a Monad instance allows effects to depend on previous results.The canonical example of this is lists, with 2 valid instances of `Applicative`.
The author of the post seems to have this realization, but I wanted to call it out, just in case.
x <- f
y <- g
Is equal to: y <- g
x <- f
Provided that “x” is not free in “g”. A good example is an async monad that provides concurrency in (<*>) and only blocks when there’s a data dependency in (>>=)—the result is the same regardless of evaluation order, but the implementations (and performance characteristics) are different. You could handwave it away by arguing that they’re “morally equivalent”, but I think it pays to be precise about properties like commutativity of actions.You have to decide whether they're equivalent or they're not, and if they're not equivalent (for your purposes) you probably shouldn't provide both instances. If you implement <hnmarkupisbad> and ap but they're not equivalent, a future maintainer will get a nasty surprise sooner or later.
I believe it's the same as iterating over the righthand side of <* > in its implementation first instead of the lefthand side like in their first, wrong implementation?
But my reasoning feels loose here. I probably just need to look at their implementation of Monad Tree closer. :-)
pair :: f a -> f b -> f (a, b)
from which you can derive the rest using pure and fmap
(Although for optimization purposes it can help to also have the other stuff it already has too, in case you want to use an optimized implementation instead of the default implementation.)