[Haskell-cafe] ReaderT and concurrency
tomasz.zielonka at gmail.com
Wed Nov 16 13:20:48 EST 2005
On Wed, Nov 16, 2005 at 09:45:17AM -0800, Andrew Pimlott wrote:
> On Wed, Nov 16, 2005 at 11:51:19AM -0500, Kurt Hutchinson wrote:
> > I have to perform another runReaderT when forking? Or is there a way
> > to get the ReaderT environment automatically carried over to the newly
> > created Set B thread?
> This is an unavoidable pain as far as I know. It would be nice if
> forkIO were defined in terms of MonadIO:
> forkIO :: MonadIO m => m () -> m ThreadId
> (Same with forkProcess.) I haven't thought too hard about it, but it
> seems that it should be possible.
I think it wouldn't be possible using only methods in MonadIO. Besides,
what should be the semantics of forkIO for (StateT s IO)? I can't think
of anything reasonable.
I played with this idea a bit, and below is the result. I'm not sure
there are any meaningful instances of UnliftIO (bad name) other than
the two below.
class Monad m => UnliftIO m where
unliftIO :: m a -> m (IO a)
instance UnliftIO IO where
unliftIO io = return io
instance UnliftIO m => UnliftIO (ReaderT r m) where
unliftIO m = do
r <- ask
lift (unliftIO (runReaderT m r))
forkIO' :: (UnliftIO m, MonadIO m) => m () -> m ThreadId
forkIO' m = do
io <- unliftIO m
liftIO (forkIO io)
More information about the Haskell-Cafe