question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

if Type is Monad than `ap` from Applicative should be sequential?

See original GitHub issue

In documentation of Control.Monad at section class Applicative m => Monad m where is written: (<*>) = ap and ap is defined like this:

{- | In many situations, the 'liftM' operations can be replaced by uses of
'ap', which promotes function application.
>       return f `ap` x1 `ap` ... `ap` xn
is equivalent to
>       liftMn f x1 x2 ... xn
-}
ap       :: (Monad m) => m (a -> b) -> m a -> m b
ap m1 m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) }
-- Since many Applicative instances define (<*>) = ap, we
-- cannot define ap = (<*>)

In Control.Applicative we read about <*>:

(<*>) :: f (a -> b) -> f a -> f b Sequential application.

To sum up if Monad is also Applicative then <*> should preserve sequentiality. Currently there is no word about this nor in Monad nor in Chain nor in Applicative laws.

So is it intentional?

If this should be stated in laws then here is current state of Future/Task/LazyPromise-es:


I found it out just yesterday on tweet of @jdegoes

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
rpominovcommented, Oct 1, 2016

Curious what if we just turn derivations into laws? E.g. add a second law for Chain:

u.chain(f => v.map(f)) is equivalent to v.ap(u)

4reactions
jdegoescommented, Oct 19, 2016

Here’s an example:

foo :: forall m. (Applicative m) => m Unit
foo = void $ doA *> doB
foo :: forall m. (Monad m) => m Unit
foo = void $ doA *> doB

One intuitively expects the observable behavior of these two functions to be the same. But that’s not what happens if monad is inconsistent with applicative.

By the way, there are two ways to solve your problem: simply add a new type wrapper which is ONLY applicative, and not monadic; OR, add a new function to an applicative called zip (or par), which is similar to ap, but which does not promise consistency with monad.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why sequence requires monad if applicative would suffice?
When Applicative was introduced, monads didn't immediately become applicatives. Now that Applicative is a superclass of Monad , sequence could ...
Read more >
Chapter 10: Applicative Functors - mostly-adequate-guide
The issue here is that we are stuck in the sequential world of monads wherein nothing may be evaluated until the previous monad...
Read more >
Haskell/Applicative functors - Wikibooks, open books for an ...
Like monads, applicative functors are functors with extra laws and operations; in fact, Applicative is an intermediate class between Functor and Monad ....
Read more >
Control.Monad - Hackage - Haskell.org
The Monad class defines the basic operations over a monad, a concept from a branch of mathematics known as category theory. From the...
Read more >
What's Ap? - Functional[Justin]
Finally, we'll look at an efficient implementation of blending two images using Applicative programming. Functor and Monad. As a Scala ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found