{-# LANGUAGE AllowAmbiguousTypes #-}
module Polysemy.IO
(
embedToMonadIO
, lowerEmbedded
) where
import Control.Monad.IO.Class
import Polysemy
import Polysemy.Embed
import Polysemy.Internal
import Polysemy.Internal.Union
embedToMonadIO
:: forall m r a
. ( MonadIO m
, Member (Embed m) r
)
=> Sem (Embed IO ': r) a
-> Sem r a
embedToMonadIO :: 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 :: [(* -> *) -> * -> *]) 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 x. MonadIO m => IO x -> m x
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO @m
{-# INLINE embedToMonadIO #-}
lowerEmbedded
:: ( MonadIO m
, Member (Embed IO) r
)
=> (forall x. m x -> IO x)
-> Sem (Embed m ': r) a
-> Sem r a
lowerEmbedded :: (forall x. m x -> IO x) -> Sem (Embed m : r) a -> Sem r a
lowerEmbedded forall x. m x -> IO x
run_m (Sem forall (m :: * -> *).
Monad m =>
(forall x. Union (Embed m : r) (Sem (Embed m : r)) x -> m x) -> m a
m) = ((forall x. Sem r x -> IO x) -> IO () -> IO a) -> Sem r a
forall (r :: [(* -> *) -> * -> *]) a.
Member (Embed IO) r =>
((forall x. Sem r x -> IO x) -> IO () -> IO a) -> Sem r a
withLowerToIO (((forall x. Sem r x -> IO x) -> IO () -> IO a) -> Sem r a)
-> ((forall x. Sem r x -> IO x) -> IO () -> IO a) -> Sem r a
forall a b. (a -> b) -> a -> b
$ \forall x. Sem r x -> IO x
lower IO ()
_ ->
m a -> IO a
forall x. m x -> IO x
run_m (m a -> IO a) -> m a -> IO a
forall a b. (a -> b) -> a -> b
$ (forall x. Union (Embed m : r) (Sem (Embed m : r)) x -> m x) -> m a
forall (m :: * -> *).
Monad m =>
(forall x. Union (Embed m : r) (Sem (Embed m : r)) x -> m x) -> m a
m ((forall x. Union (Embed m : r) (Sem (Embed m : r)) x -> m x)
-> m a)
-> (forall x. Union (Embed m : r) (Sem (Embed m : r)) x -> m x)
-> m a
forall a b. (a -> b) -> a -> b
$ \Union (Embed m : r) (Sem (Embed m : r)) x
u ->
case Union (Embed m : r) (Sem (Embed m : r)) x
-> Either
(Union r (Sem (Embed m : r)) x)
(Weaving (Embed m) (Sem (Embed m : r)) x)
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *])
(m :: * -> *) a.
Union (e : r) m a -> Either (Union r m a) (Weaving e m a)
decomp Union (Embed m : r) (Sem (Embed m : r)) x
u of
Left Union r (Sem (Embed m : r)) x
x -> IO x -> m x
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
(IO x -> m x)
-> (Union r (Sem r) x -> IO x) -> Union r (Sem r) x -> m x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem r x -> IO x
forall x. Sem r x -> IO x
lower
(Sem r x -> IO x)
-> (Union r (Sem r) x -> Sem r x) -> Union r (Sem r) x -> IO x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Union r (Sem r) x -> Sem r x
forall (r :: [(* -> *) -> * -> *]) a. Union r (Sem r) a -> Sem r a
liftSem
(Union r (Sem r) x -> m x) -> Union r (Sem r) x -> m x
forall a b. (a -> b) -> a -> b
$ (forall x. Sem (Embed m : r) x -> Sem r x)
-> Union r (Sem (Embed m : r)) x -> Union r (Sem r) x
forall (m :: * -> *) (n :: * -> *) (r :: [(* -> *) -> * -> *]) a.
(forall x. m x -> n x) -> Union r m a -> Union r n a
hoist ((forall x. m x -> IO x) -> Sem (Embed m : r) x -> Sem r x
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
(MonadIO m, Member (Embed IO) r) =>
(forall x. m x -> IO x) -> Sem (Embed m : r) a -> Sem r a
lowerEmbedded forall x. m x -> IO x
run_m) Union r (Sem (Embed m : r)) x
x
Right (Weaving (Embed m a
wd) f ()
s forall x. f (Sem rInitial x) -> Sem (Embed m : r) (f x)
_ f a -> x
y forall x. f x -> Maybe x
_) ->
f a -> x
y (f a -> x) -> m (f a) -> m x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((a -> f () -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
s) (a -> f a) -> m a -> m (f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
wd)