-
Notifications
You must be signed in to change notification settings - Fork 65
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
Idea: Give Async a Monad and MonadPlus instance #70
Comments
ChristopherKing42
changed the title
Idea: Give async a Monad and MonadPlus instance
Idea: Give Nov 27, 2017
Async
a Monad
and MonadPlus
instance
ChristopherKing42
changed the title
Idea: Give
Idea: Give Async a Monad and MonadPlus instance
Nov 27, 2017
Async
a Monad
and MonadPlus
instance
ChristopherKing42
added a commit
to ChristopherKing42/async
that referenced
this issue
Dec 11, 2017
The Monad instance is in fact compatible with the Applicative instance. In as >>= asb, it will run concurrently if asb is lazy (such as used by `ap`) and sequential when strict Close simonmar#70
ChristopherKing42
added a commit
to ChristopherKing42/async
that referenced
this issue
Dec 11, 2017
The Monad instance is in fact compatible with the Applicative instance. In as >>= asb, it will run concurrently if asb is lazy (such as used by `ap`) and sequential when strict Close simonmar#70
ChristopherKing42
added a commit
to ChristopherKing42/async
that referenced
this issue
Dec 11, 2017
The Monad instance is in fact compatible with the Applicative instance. In as >>= asb, it will run concurrently if asb is lazy (such as used by `ap`) and sequential when strict Close simonmar#70
ChristopherKing42
added a commit
to ChristopherKing42/async
that referenced
this issue
Dec 12, 2017
The Monad instance is compatible with the Applicative instance. In as >>= asb, the actions will run concurrently if asb is lazy (in `ap` for instance), or sequentially if asb is strict. Close simonmar#70.
See discussion in #71 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We would need to add a
Pure :: a -> Async a
data constructor toAsync
, but other than that, the currentAsync
type supports theMonad
andMonadPlus
typeclasses. This is becauseSTM
is aMonad
andMonadPlus
.return
would equalPure
, obviously.Pure a >>= f = f a
. Forx >>= f
wherex
isn'tPure
, we give the resulting asyncx
'sasyncThreadId
, and use theSTM
monad to generate the resulting_asyncWait
.The
MonadPlus
instance would be similar, withmzero
corresponding toSTM
'smzero
(and some junkThreadId
) andmplus
corresponding toSTM
'smplus
(and choosing the leftThreadId
(ormzero
'sThreadId
).If you don't want to add a new data constructor to
Async
, you can also define anApplicative
and Alternative instance without changing the type at all.pure a
would have a junkasyncThreadId
and would useSTM
's pure forasyncThreadId
.<*>
would useSTM
's<*>
for_asyncWait
, and would choose the left-most non-junk-asyncThreadId
for the resultingasyncThreadId
.Alternative
would work similarly. (If you do not like using an invalidThreadId
forasyncThreadId
, you could slightly changeasyncThreadId
's type and useNothing
instead. Or you could make it a[ThreadId]
of all the threads involved.)The text was updated successfully, but these errors were encountered: