Applicative WTF?
blog.plover.com
Applicative WTF?
1–10 of 31 posts
Re: Applicative WTF?
#2In 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.
Re: Applicative WTF?
#3I 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. :-)
Re: Applicative WTF?
#4> Last time I used Haskell, Applicative wasn't even a thing. I had read the McBride and Paterson paper that introduced applicative functors, but that was years ago, and I didn't remember any of the details. 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…
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.
Re: Applicative WTF?
#5> Last time I used Haskell, Applicative wasn't even a thing. I had read the McBride and Paterson paper that introduced applicative functors, but that was years ago, and I didn't remember any of the details. 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…
Given that he already knew how to define a Monad instance for his type, the Applicative instance could simply be: 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…
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
Re: Applicative WTF?
#6> Last time I used Haskell, Applicative wasn't even a thing. I had read the McBride and Paterson paper that introduced applicative functors, but that was years ago, and I didn't remember any of the details. 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…
Given that he already knew how to define a Monad instance for his type, the Applicative instance could simply be: 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…
Re: Applicative WTF?
#7Earlier quoted context omitted.
Given that he already knew how to define a Monad instance for his type, the Applicative instance could simply be: 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…
Right 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
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.Re: Applicative WTF?
#8The 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.
Re: Applicative WTF?
#9Re: Applicative WTF?
#10The reason GHC won't infer the definition of Applicative is because there can be multiple valid `Applicative` instances for a type (unlike `Functor` where there is a unique (non-trivial) instance). 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.