Skip to content
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

Closed
ChristopherKing42 opened this issue Nov 27, 2017 · 1 comment
Closed

Idea: Give Async a Monad and MonadPlus instance #70

ChristopherKing42 opened this issue Nov 27, 2017 · 1 comment

Comments

@ChristopherKing42
Copy link

ChristopherKing42 commented Nov 27, 2017

We would need to add a Pure :: a -> Async a data constructor to Async, but other than that, the current Async type supports the Monad and MonadPlus typeclasses. This is because STM is a Monad and MonadPlus.

return would equal Pure, obviously. Pure a >>= f = f a. For x >>= f where x isn't Pure, we give the resulting async x's asyncThreadId, and use the STM monad to generate the resulting _asyncWait.

The MonadPlus instance would be similar, with mzero corresponding to STM's mzero (and some junk ThreadId) and mplus corresponding to STM's mplus (and choosing the left ThreadId (or mzero's ThreadId).

If you don't want to add a new data constructor to Async, you can also define an Applicative and Alternative instance without changing the type at all. pure a would have a junk asyncThreadId and would use STM's pure for asyncThreadId. <*> would use STM's <*> for _asyncWait, and would choose the left-most non-junk-asyncThreadId for the resulting asyncThreadId. Alternative would work similarly. (If you do not like using an invalid ThreadId for asyncThreadId, you could slightly change asyncThreadId's type and use Nothing instead. Or you could make it a [ThreadId] of all the threads involved.)

@ChristopherKing42 ChristopherKing42 changed the title Idea: Give async a Monad and MonadPlus instance Idea: Give Async a Monad and MonadPlus instance Nov 27, 2017
@ChristopherKing42 ChristopherKing42 changed the title Idea: Give Async a Monad and MonadPlus instance Idea: Give Async a Monad and MonadPlus instance Nov 27, 2017
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.
@simonmar
Copy link
Owner

See discussion in #71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants