{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Description: Compatibility with 'MonadIO'
module Polysemy.IO
  ( -- * Interpretations
    embedToMonadIO
  ) where

import Control.Monad.IO.Class
import Polysemy
import Polysemy.Embed


------------------------------------------------------------------------------
-- | The 'MonadIO' class is conceptually an interpretation of 'IO' to some
-- other monad. This function reifies that intuition, by transforming an 'IO'
-- effect into some other 'MonadIO'.
--
-- This function is especially useful when using the 'MonadIO' instance for
-- 'Sem' instance.
--
-- Make sure to type-apply the desired 'MonadIO' instance when using
-- 'embedToMonadIO'.
--
-- @since 1.0.0.0
--
-- ==== Example
--
-- @
-- foo :: PandocIO ()
-- foo = 'runM' . 'embedToMonadIO' @PandocIO $ do
--   'liftIO' $ putStrLn "hello from polysemy"
-- @
--
embedToMonadIO
    :: forall m r a
     . ( MonadIO m
       , Member (Embed m) r
       )
    => Sem (Embed IO ': r) a
    -> Sem r a
embedToMonadIO :: forall (m :: * -> *) (r :: EffectRow) a.
(MonadIO m, Member (Embed m) r) =>
Sem (Embed IO : r) a -> Sem r a
embedToMonadIO = (forall x. IO x -> m x) -> Sem (Embed IO : r) a -> Sem r a
forall (m1 :: * -> *) (m2 :: * -> *) (r :: EffectRow) a.
Member (Embed m2) r =>
(forall x. m1 x -> m2 x) -> Sem (Embed m1 : r) a -> Sem r a
runEmbedded ((forall x. IO x -> m x) -> Sem (Embed IO : r) a -> Sem r a)
-> (forall x. IO x -> m x) -> Sem (Embed IO : r) a -> Sem r a
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO @m
{-# INLINE embedToMonadIO #-}