Skip to content

Commit

Permalink
Move withRunInIO into typeclass #13
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Jan 2, 2018
1 parent 7b9a583 commit a99e302
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions unliftio-core/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Doc improvements.
* Inline functions in `Control.Monad.IO.Unlift` module [#4](https://github.com/fpco/unliftio/pull/4).
* Fully polymorphic `withRunInIO` [#12](https://github.com/fpco/unliftio/pull/12).
* Move `withRunInIO` into the `MonadUnliftIO` typeclass itself[#13](https://github.com/fpco/unliftio/issues/13)

## 0.1.0.0

Expand Down
21 changes: 11 additions & 10 deletions unliftio-core/src/Control/Monad/IO/Unlift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Control.Monad.IO.Unlift
, UnliftIO (..)
, askRunInIO
, withUnliftIO
, withRunInIO
, toIO
, MonadIO (..)
) where
Expand Down Expand Up @@ -52,15 +51,26 @@ newtype UnliftIO m = UnliftIO { unliftIO :: forall a. m a -> IO a }
--
-- @since 0.1.0.0
class MonadIO m => MonadUnliftIO m where
{-# MINIMAL askUnliftIO | withRunInIO #-}
-- | Capture the current monadic context, providing the ability to
-- run monadic actions in 'IO'.
--
-- See 'UnliftIO' for an explanation of why we need a helper
-- datatype here.
-- @since 0.1.0.0
askUnliftIO :: m (UnliftIO m)
askUnliftIO = withRunInIO (\run -> return (UnliftIO run))
-- Would be better, but GHC hates us
-- askUnliftIO :: m (forall a. m a -> IO a)

-- | Convenience function for capturing the monadic context and running an 'IO'
-- action with a runner function. The runner function is used to run a monadic
-- action @m@ in @IO@.
--
-- @since 0.1.0.0
{-# INLINE withRunInIO #-}
withRunInIO :: ((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO inner = withUnliftIO $ \u -> inner (unliftIO u)
instance MonadUnliftIO IO where
{-# INLINE askUnliftIO #-}
askUnliftIO = return (UnliftIO id)
Expand Down Expand Up @@ -94,15 +104,6 @@ askRunInIO = liftM unliftIO askUnliftIO
withUnliftIO :: MonadUnliftIO m => (UnliftIO m -> IO a) -> m a
withUnliftIO inner = askUnliftIO >>= liftIO . inner

-- | Convenience function for capturing the monadic context and running an 'IO'
-- action with a runner function. The runner function is used to run a monadic
-- action @m@ in @IO@.
--
-- @since 0.1.0.0
{-# INLINE withRunInIO #-}
withRunInIO :: MonadUnliftIO m => ((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO inner = withUnliftIO $ \u -> inner (unliftIO u)

-- | Convert an action in @m@ to an action in @IO@.
--
-- @since 0.1.0.0
Expand Down

0 comments on commit a99e302

Please sign in to comment.