2

Is there any way to make forkIO classy?

import Control.Monad.IO.Class (MonadIO)
import Control.Concurrent (ThreadId)

-- | Is this possible?
forkIO :: MonadIO m => m () -> m ThreadId
forkIO = undefined

I'm trying to call it from within a function using a few mtl monad class constraints. I'm aware of lifted-base but I'd rather not have an extra MonadBaseControl IO constraint bubble up through my program. Solutions and/or suggestions would be appreciated.

Jordan Mackie
  • 1,193
  • 10
  • 17
  • 3
    This would appear to require a function `MonadIO m => m () -> IO ()` which does not exist. – AJF May 29 '18 at 10:15
  • 2
    This can not be implemented for, e.g., `StateT s IO a` since that would require the state to be shared among multiple threads, which can not be achieved using the underlying type `s -> IO (s, a)`. – chi May 29 '18 at 10:38

1 Answers1

4

It can't be done with MonadIO, but it can be done with MonadUnliftIO, see here.

Not everything that has a MonadIO instance also has a MonadUnliftIO instance, but depending on what you need this for this might be sufficient.

Cubic
  • 14,902
  • 5
  • 47
  • 92