-
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
Add spec for Pearing of Monad and Applicative #186
Comments
Just came across purescript-parallel#10 in which -- | The `Parallel` class abstracts over monads which support
-- | parallel composition via some related `Applicative`.
class (Monad m, Applicative f) <= Parallel f m | m -> f, f -> m where
parallel :: m ~> f
sequential :: f ~> m
With that in mind we need a way to convert a Monadic value to corresponding Applicative and Applicative to corresponding Monad. M.parallel :: M a -> Ap a
M.sequential :: Ap a -> M a
M.sequential(
M.parallel(M.of(1)).ap(M.parallel(M.of(id)))
) But I don't quite see how should we express that relation between Monad and Applicative in FL. We would need to also define Alternative (#187) so we still have |
Example for Task would look like this: // Http.get :: URL -> Task HttpErr Response
Task.sequential(
lift2(
(p1 =>p2=> [p1, p2]),
Task.parallel(Http.get('/page1')),
Task.parallel(Http.get('/page2'))
)
).chain([p1, p2] => Http.get(getPageFromTwoPage(p1, p2))) it's not that nice, but we can actually define them as instance methods which look better: lift2(
(p1 =>p2=> [p1, p2]),
Http.get('/page1').parallel(),
Http.get('/page2').parallel()
).sequential().chain([p1, p2] => Http.get(getPageFromTwoPage(p1, p2))) The law would be: |
@rpominov @robotlolita @Avaq @SimonRichardson what do you think on this? It will solve issue related to |
After reading this reddit thread i'm thinking that name Parallel or Concurent is not that precise as this spec shuold just pearing/isomorphism between some Monad and some Applicative. This are example are not strictly related to concurrency/parallelism:
for now i don't have a better name but will think on this. |
Not sure this worth adding to the spec. I can't imagine what kind of generic code we could write against this. I mean say we have some value of a Monadic type, and we know that we can convert it to a value of some other type that is Applicative, not sure what does this give us. The technic itself looks good though, and can be used in Task to provide lawful parallel Applicative, but I'm not sure what we get by adding this to the spec in this particular way. Edit: we could also still use |
This will be useful in Free monad like structures. for example one might have Free&FreeAp conforming to this spec user could switch from one to another when it's appropriate, and then it could be folded down to some other Monad&Applicative pear Task/Future ... Or one might for example in one computation use Either then convert it to Validation and then back to Either (when error is monoid), but I see most use with Task/Future. |
in purescript-parallel there are this classes:
I think we should add them to fantasy land spec so that different implementations of
Future/Task
could implement them consistently such that libs like @safareli/Free and @DrBoolean/freeky could depend on them.If we had this algebras we can also port Parallel using which we can transform any
(Monad m, ChainRace m)
into justApplicative
which is concurant (i.e. we would be able to uselift
and general functions overApplicative
while still be concurant) which is awasome!I think we should name use this names as they dont need structure to be a monad:
realted issues:
The text was updated successfully, but these errors were encountered: