Live data from Hacker News

Applicative WTF?

blog.plover.com

1–10 of 31 posts

Re: Applicative WTF?

#2
> 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 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?

#3
"Digging deeper into why this worked this way was interesting, but it's bed time, so I'm going to cut the scroll here." I can very roughly intuit why this is-- the difference between the two Applicative instances the author noticed was that the shape of the first tree was being fitted into the second tree vs the shape of the second into the first. So if you "bind" the lefthand side of first, its shape will be "fitted" to the righthand side, and if you do the opposite you get the second "fitted" to the first shape.

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. :-)

Re: Applicative WTF?

#4
post #2

> 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 instance for Applicative but not Monad.

Re: Applicative WTF?

#5
post #4
post #2

> 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…

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

Re: Applicative WTF?

#6
post #4
post #2

> 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…

see the addendum on the follow up post: https://blog.plover.com/prog/haskell/getting-bind-from-join....

Re: Applicative WTF?

#7
post #5
post #4

Earlier 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

Indeed, the learning process is important. I think it's particularly instructive to look at how `ap` is implemented, and what is left out in the transition from Monad to Applicative:

    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?

#8
The 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.

Re: Applicative WTF?

#10
post #8

The 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.

That's true, but only one of the instances is 'compatible' with the Monad definition which requires () = ap (the first 'recipe' he showed for implementing Applicative)
Post reply on HN