if Type is Monad than `ap` from Applicative should be sequential?
See original GitHub issueIn 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:
- @robotlolita/data.task invalid
- @rpominov/fun-task valid
- @Avaq/Fluture invalid
- ramda/ramda-fantasy#Future invalid
- @safareli/Free invalid? (it delegates .ap to target monad so not quite invalid but sort of invalid)
- fantasyland/fantasy-promises valid
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:16 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Curious what if we just turn derivations into laws? E.g. add a second law for Chain:
Here’s an example:
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 toap
, but which does not promise consistency with monad.