News for hackers. Stuff that matters.
Re:Applicative WTF? by nybble41 on Tuesday October 23, 2018 @06:56PM (#18288216)
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 <- 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.
Re:Applicative WTF? by taejo on Wednesday October 24, 2018 @08:06AM (#18291664)
Does this mean that many Monads have two possible Applicative instances? For the two orders of "running" mf and ma?
Re:Applicative WTF? by nybble41 on Wednesday October 24, 2018 @11:55AM (#18293509)
All Applicatives have those two variants, not just Applicatives which are also Monads. There is a Backwards newtype in Control.Applicative.Backwards which reverses the order of effects for any Applicative:

    GHCi> import Control.Applicative.Backwards
    GHCi> :i Backwards
    newtype Backwards (f :: k -> *) (a :: k)
      = Backwards {forwards :: f a}
    GHCi> forwards $ (++) <$> Backwards ("foo" <$ print 1) <*> Backwards ("bar" <$ print 2)
    2
    1
    "foobar"
As you can see, the result is the same but the effects are reversed. The definition is simple:

    instance Applicative f => Applicative (Backwards f) where
       pure = Backwards . pure
       Backwards f <*> Backwards a = Backwards $ (flip ($)) <$> a <*> f
Re:Applicative WTF? by lmm on Wednesday October 24, 2018 @09:37AM (#18292260)
I don't know about "many", but certainly some - e.g. you could form a "reversed MonadWriter" applicative that would write the messages from ma before the messages from mf. For a lot of monads the effects are commutative though, in which case the "two possible" Applicatives are actually the same, e.g. Maybe obviously behaves the same whichever order you "run" mf and ma in.
Re:Applicative WTF? by taejo on Friday October 26, 2018 @03:44AM (#18307470)
My "many" exactly meant to exclude the commutative case :)