Skip to content

Commit

Permalink
Rename Lift -> Embed
Browse files Browse the repository at this point in the history
  • Loading branch information
googleson78 committed Jul 8, 2019
1 parent 82b1708 commit aee0326
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 89 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ data Teletype m a where

makeSem ''Teletype

runTeletypeIO :: Member (Lift IO) r => Sem (Teletype ': r) a -> Sem r a
runTeletypeIO :: Member (Embed IO) r => Sem (Teletype ': r) a -> Sem r a
runTeletypeIO = interpret $ \case
ReadTTY -> sendM getLine
WriteTTY msg -> sendM $ putStrLn msg
Expand Down Expand Up @@ -121,7 +121,7 @@ pureOutput :: [String] -> [String]
pureOutput = fst . run . echoPure

-- Now let's do things
echoIO :: Sem '[Lift IO] ()
echoIO :: Sem '[Embed IO] ()
echoIO = runTeletypeIO echo

-- echo forever
Expand Down
2 changes: 1 addition & 1 deletion bench/Poly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ prog
:: Sem '[ State Bool
, Error Bool
, Resource
, Lift IO
, Embed IO
] Bool
prog = catch @Bool (throw True) (pure . not)

Expand Down
2 changes: 1 addition & 1 deletion polysemy-plugin/test/ExampleSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data Teletype m a where

makeSem ''Teletype

runTeletypeIO :: Member (Lift IO) r => Sem (Teletype ': r) a -> Sem r a
runTeletypeIO :: Member (Embed IO) r => Sem (Teletype ': r) a -> Sem r a
runTeletypeIO = interpret $ \case
ReadTTY -> sendM getLine
WriteTTY msg -> sendM $ putStrLn msg
Expand Down
6 changes: 3 additions & 3 deletions polysemy-plugin/test/LegitimateTypeErrorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Polysemy
import Test.Hspec
import Test.ShouldNotTypecheck

wrongLift :: Member (Lift IO) r => Sem r ()
wrongLift = sendM putStrLn
wrongEmbed :: Member (Embed IO) r => Sem r ()
wrongEmbed = sendM putStrLn

wrongReturn :: Sem (e ': r) () -> Sem r ()
wrongReturn = reinterpret undefined
Expand All @@ -18,7 +18,7 @@ spec :: Spec
spec = do
describe "Legitimate type errors" $ do
it "should be caused by `sendM`ing an unsaturated function" $
shouldNotTypecheck wrongLift
shouldNotTypecheck wrongEmbed

it "should be caused by giving a bad type to reinterpret" $
shouldNotTypecheck wrongReturn
Expand Down
4 changes: 2 additions & 2 deletions polysemy-plugin/test/PluginSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ errState = do
err


lifted :: Monad m => Member (Lift m) r => Sem r ()
lifted :: Monad m => Member (Embed m) r => Sem r ()
lifted = sendM $ pure ()


Expand Down Expand Up @@ -136,7 +136,7 @@ spec = do
output $ replicate 2 5


describe "Lift effect" $ do
describe "Embed effect" $ do
it "should interpret against IO" $ do
res <- runM lifted
res `shouldBe` ()
Expand Down
2 changes: 2 additions & 0 deletions polysemy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ library
exposed-modules:
Polysemy
Polysemy.Async
Polysemy.Embed
Polysemy.Embed.Type
Polysemy.Error
Polysemy.Fixpoint
Polysemy.Input
Expand Down
2 changes: 1 addition & 1 deletion src/Polysemy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Polysemy
, runM

-- * Interoperating With Other Monads
, Lift (..)
, Embed (..)
, sendM

-- * Lifting
Expand Down
4 changes: 2 additions & 2 deletions src/Polysemy/Async.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ makeSem ''Async
--
-- @since 0.5.0.0
runAsync
:: LastMember (Lift IO) r
:: LastMember (Embed IO) r
=> Sem (Async ': r) a
-> Sem r a
runAsync m = withLowerToIO $ \lower _ -> lower $
Expand All @@ -64,7 +64,7 @@ runAsync m = withLowerToIO $ \lower _ -> lower $
--
-- @since 0.5.0.0
runAsyncInIO
:: Member (Lift IO) r
:: Member (Embed IO) r
=> (forall x. Sem r x -> IO x)
-- ^ Strategy for lowering a 'Sem' action down to 'IO'. This is likely
-- some combination of 'runM' and other interpreters composed via '.@'.
Expand Down
34 changes: 34 additions & 0 deletions src/Polysemy/Embed.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{-# LANGUAGE TemplateHaskell #-}

module Polysemy.Embed
( -- * Effect
Embed (..)

-- * Actions
, embed

-- * Interpretations
, runEmbed

-- * Embeding monadic actions into effects
, sendM
) where

import Polysemy
import Polysemy.Embed.Type (Embed (..))

makeSem ''Embed

------------------------------------------------------------------------------
-- | Given a natural transform from @m1@ to @m2@
-- run a @Embed m1@ effect by transforming it into a @Embed m2@ effect.
--
-- TODO(sandy): @since
runEmbed
:: forall m1 m2 r a
. Member (Embed m2) r
=> (forall x. m1 x -> m2 x)
-> Sem (Embed m1 ': r) a
-> Sem r a
runEmbed f = interpret $ sendM . f . unEmbed
{-# INLINE runEmbed #-}
12 changes: 6 additions & 6 deletions src/Polysemy/Lift/Type.hs → src/Polysemy/Embed/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

{-# OPTIONS_HADDOCK not-home #-}

module Polysemy.Lift.Type
module Polysemy.Embed.Type
( -- * Effect
Lift(..)
Embed (..)
) where

import Data.Kind
Expand All @@ -19,16 +19,16 @@ import Data.Kind
-- 'Polysemy.Sem':
--
-- @
-- 'Polysemy.sendM' (putStrLn "hello") :: 'Polysemy.Member' ('Polysemy.Lift' IO) r => 'Polysemy.Sem' r ()
-- 'Polysemy.sendM' (putStrLn "hello") :: 'Polysemy.Member' ('Polysemy.Embed' IO) r => 'Polysemy.Sem' r ()
-- @
--
-- That being said, you lose out on a significant amount of the benefits of
-- 'Polysemy.Sem' by using 'Polysemy.sendM' directly in application code; doing
-- so will tie your application code directly to the underlying monad, and
-- prevent you from interpreting it differently. For best results, only use
-- 'Lift' in your effect interpreters.
-- 'Embed' in your effect interpreters.
--
-- Consider using 'Polysemy.Trace.trace' and 'Polysemy.Trace.runTraceIO' as
-- a substitute for using 'putStrLn' directly.
newtype Lift m (z :: Type -> Type) a where
Lift :: { unLift :: m a } -> Lift m z a
newtype Embed m (z :: Type -> Type) a where
Embed :: { unEmbed :: m a } -> Embed m z a
6 changes: 3 additions & 3 deletions src/Polysemy/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fromEither (Right a) = pure a
fromEitherM
:: forall e m r a
. ( Member (Error e) r
, Member (Lift m) r
, Member (Embed m) r
)
=> m (Either e a)
-> Sem r a
Expand Down Expand Up @@ -135,7 +135,7 @@ instance (Typeable e) => X.Exception (WrappedExc e)
-- significantly faster than 'runError', at the cost of being less flexible.
runErrorInIO
:: ( Typeable e
, Member (Lift IO) r
, Member (Embed IO) r
)
=> ( x. Sem r x -> IO x)
-- ^ Strategy for lowering a 'Sem' action down to 'IO'. This is
Expand All @@ -154,7 +154,7 @@ runErrorInIO lower
-- TODO(sandy): Can we use the new withLowerToIO machinery for this?
runErrorAsExc
:: forall e r a. ( Typeable e
, Member (Lift IO) r
, Member (Embed IO) r
)
=> ( x. Sem r x -> IO x)
-> Sem (Error e ': r) a
Expand Down
2 changes: 1 addition & 1 deletion src/Polysemy/Fixpoint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ runFixpoint lower = interpretH $ \case
-- | Run a 'Fixpoint' effect in terms of an underlying 'MonadFix' instance.
runFixpointM
:: ( MonadFix m
, Member (Lift m) r
, Member (Embed m) r
)
=> ( x. Sem r x -> m x)
-> Sem (Fixpoint ': r) a
Expand Down
16 changes: 8 additions & 8 deletions src/Polysemy/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Polysemy.IO

import Control.Monad.IO.Class
import Polysemy
import Polysemy.Lift
import Polysemy.Embed
import Polysemy.Internal
import Polysemy.Internal.Union

Expand Down Expand Up @@ -38,16 +38,16 @@ import Polysemy.Internal.Union
runIO
:: forall m r a
. ( MonadIO m
, Member (Lift m) r
, Member (Embed m) r
)
=> Sem (Lift IO ': r) a
=> Sem (Embed IO ': r) a
-> Sem r a
runIO = runLift $ liftIO @m
runIO = runEmbed $ liftIO @m
{-# INLINE runIO #-}


------------------------------------------------------------------------------
-- | Given some @'MonadIO' m@, interpret all @'Lift' m@ actions in that monad
-- | Given some @'MonadIO' m@, interpret all @'Embed' m@ actions in that monad
-- at once. This is useful for interpreting effects like databases, which use
-- their own monad for describing actions.
--
Expand All @@ -56,10 +56,10 @@ runIO = runLift $ liftIO @m
-- @since 0.6.0.0
runEmbedded
:: ( MonadIO m
, LastMember (Lift IO) r
, LastMember (Embed IO) r
)
=> (forall x. m x -> IO x) -- ^ The means of running this monad.
-> Sem (Lift m ': r) a
-> Sem (Embed m ': r) a
-> Sem r a
runEmbedded run_m (Sem m) = withLowerToIO $ \lower _ ->
run_m $ m $ \u ->
Expand All @@ -69,6 +69,6 @@ runEmbedded run_m (Sem m) = withLowerToIO $ \lower _ ->
. liftSem
$ hoist (runEmbedded run_m) x

Right (Weaving (Lift wd) s _ y _) ->
Right (Weaving (Embed wd) s _ y _) ->
fmap y $ fmap (<$ s) wd

22 changes: 11 additions & 11 deletions src/Polysemy/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Polysemy.Internal
, raiseUnder
, raiseUnder2
, raiseUnder3
, Lift (..)
, Embed (..)
, usingSem
, liftSem
, hoistSem
Expand All @@ -34,7 +34,7 @@ import Control.Monad.IO.Class
import Data.Functor.Identity
import Data.Kind
import Polysemy.Internal.Fixpoint
import Polysemy.Lift.Type
import Polysemy.Embed.Type
import Polysemy.Internal.NonDet
import Polysemy.Internal.PluginLookup
import Polysemy.Internal.Union
Expand All @@ -57,7 +57,7 @@ import Polysemy.Internal.Union
-- 'Polysemy.Error.runError' to 'Polysemy.Error.runErrorInIO'.
--
-- The effect stack @r@ can contain arbitrary other monads inside of it. These
-- monads are lifted into effects via the 'Lift' effect. Monadic values can be
-- monads are lifted into effects via the 'Embed' effect. Monadic values can be
-- lifted into a 'Sem' via 'sendM'.
--
-- A 'Sem' can be interpreted as a pure value (via 'run') or as any
Expand All @@ -73,7 +73,7 @@ import Polysemy.Internal.Union
-- monomorphic representation of the @r@ parameter.
--
-- After all of your effects are handled, you'll be left with either
-- a @'Sem' '[] a@ or a @'Sem' '[ 'Lift' m ] a@ value, which can be
-- a @'Sem' '[] a@ or a @'Sem' '[ 'Embed' m ] a@ value, which can be
-- consumed respectively by 'run' and 'runM'.
--
-- ==== Examples
Expand Down Expand Up @@ -231,7 +231,7 @@ instance (Member NonDet r) => MonadFail (Sem r) where
-- | This instance will only lift 'IO' actions. If you want to lift into some
-- other 'MonadIO' type, use this instance, and handle it via the
-- 'Polysemy.IO.runIO' interpretation.
instance (Member (Lift IO) r) => MonadIO (Sem r) where
instance (Member (Embed IO) r) => MonadIO (Sem r) where
liftIO = sendM
{-# INLINE liftIO #-}

Expand Down Expand Up @@ -300,17 +300,17 @@ raiseUnder3 = hoistSem $ hoist raiseUnder3 . weakenUnder3


------------------------------------------------------------------------------
-- | Lift an effect into a 'Sem'. This is used primarily via
-- | Embed an effect into a 'Sem'. This is used primarily via
-- 'Polysemy.makeSem' to implement smart constructors.
send :: Member e r => e (Sem r) a -> Sem r a
send = liftSem . inj
{-# INLINE[3] send #-}


------------------------------------------------------------------------------
-- | Lift a monadic action @m@ into 'Sem'.
sendM :: Member (Lift m) r => m a -> Sem r a
sendM = send . Lift
-- | Embed a monadic action @m@ in 'Sem'.
sendM :: Member (Embed m) r => m a -> Sem r a
sendM = send . Embed
{-# INLINE sendM #-}


Expand All @@ -324,11 +324,11 @@ run (Sem m) = runIdentity $ m absurdU
------------------------------------------------------------------------------
-- | Lower a 'Sem' containing only a single lifted 'Monad' into that
-- monad.
runM :: Monad m => Sem '[Lift m] a -> m a
runM :: Monad m => Sem '[Embed m] a -> m a
runM (Sem m) = m $ \z ->
case extract z of
Weaving e s _ f _ -> do
a <- unLift e
a <- unEmbed e
pure $ f $ a <$ s
{-# INLINE runM #-}

Expand Down
8 changes: 4 additions & 4 deletions src/Polysemy/Internal/Forklift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Polysemy.Internal.Union
--
-- @since 0.5.0.0
data Forklift r = forall a. Forklift
{ responseMVar :: MVar (Sem '[Lift IO] a)
{ responseMVar :: MVar (Sem '[Embed IO] a)
, request :: Union r (Sem r) a
}

Expand All @@ -30,10 +30,10 @@ data Forklift r = forall a. Forklift
--
-- @since 0.5.0.0
runViaForklift
:: LastMember (Lift IO) r
:: LastMember (Embed IO) r
=> InChan (Forklift r)
-> Sem r a
-> Sem '[Lift IO] a
-> Sem '[Embed IO] a
runViaForklift chan (Sem m) = Sem $ \k -> m $ \u -> do
case decompLast u of
Left x -> usingSem k $ join $ sendM $ do
Expand All @@ -53,7 +53,7 @@ runViaForklift chan (Sem m) = Sem $ \k -> m $ \u -> do
--
-- @since 0.5.0.0
withLowerToIO
:: LastMember (Lift IO) r
:: LastMember (Embed IO) r
=> ((forall x. Sem r x -> IO x) -> IO () -> IO a)
-- ^ A lambda that takes the lowering function, and a finalizing 'IO'
-- action to mark a the forked thread as being complete. The finalizing
Expand Down
Loading

0 comments on commit aee0326

Please sign in to comment.