-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
if Type is Monad than ap
from Applicative should be sequential?
#179
Comments
This actually stated in Fantasy Land but a bit indirectly see rpominov/fun-task#28 |
Curious what if we just turn derivations into laws? E.g. add a second law for Chain:
|
I think that's a good Idea! and maybe also add them to |
Good spot, there was a PR open in fantasy-promises that did talk about this, but that was probably the wrong place to do so. |
I have checked PRs and Issues of fl-promises but could not find anything related to it. One thing to note is that for example Haxl is not fully lawful. I don't know if it's common practice in haskell to partially obey this law. Some libs might ignore this law but I think we should make it clear that it's also a law and should be obeyed as others. Currently this are derivations separately from algebras but i think we can move them to algebras like this:
I would try to create PR to address this issue. |
If we convert derivations to laws, I wonder should we do this for all derivations? Maybe some of them shouldn't actually be laws, maybe in Haskell they doesn't correspond to a law etc.? Btw, was very interesting to know that in Haskell it also not always obeyed, thank you for the link! |
@rpominov good point! We should check if all derivations are actually laws in haskell. |
I was thinking recently that having law that do
action1;
action2; is translated to But we don't have do
_ <- action1;
action2; With that in mind I don't see any reason to make applicative sequential when it can be parallel and more practical. So i think this derivation should not be a law, or should be ignored form in Task/Future. What others think on this? |
Without the law, it basically means that a program's behavior may change if you change a constraint from Note that a notion of sequentiality is often present for purely Applicative structures, such as Applicative parsing combinators, in which the "left" is parsed before the "right". Perhaps someone has done work on determining the implications of changing these laws? |
actually I was not correct in sequentiality
Applicative should still be sequential, from left to right or right to left. but in case of Task/Future, it should not wait for first task to be finished in order to fork second (Applicative instance derived from chain forks first one and waits until it's finished and then forks second and so on). In case of maybe, either and other non lazy monadic structures, if we implement
I might misunderstood your question but currently only
Did not quite understand how program's behavior could change, can you provide example or something. |
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 |
I have just read this redit thread on Applicative Effects in Free Monads and there was interesting discussion about the law. So as this question is already discussed, I will close this issue. |
@safareli Could you please explain what the conclusion is in general? Is it the case that for any lawful applicative,
Let's say I have What I'm trying to understand is whether the "contextual" effects must also be sequenced. Is it required of a lawful implementation of |
If a structure is a Monad then it's Applicative instance should behave like
This rule makes it possible to refactor code using do notation into code using In Purescript and in Haskell there is sequential Io/Aff and parallel version Async/ParAff which is not a Monad. (same for with fluture.js too). But for example Haxl is a lib used for fetching data and has caching built in, so for it all actions could be parallelized as they would have no observable difference (except the speed). tho doing this for IO will definitely have observable difference. |
@safareli Thanks. I'm assuming the point of this rule is that the user is forced to convert between different data types and explicitly choose between the two possible (legal) applicative instances, i.e. parallel vs "early exit". |
yes, so you will convert your sequential values to parallel one, work with it and then covert back. |
In documentation of Control.Monad at section class Applicative m => Monad m where is written:
(<*>) = ap
and ap is defined like this:In Control.Applicative we read about
<*>
: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
The text was updated successfully, but these errors were encountered: