unlift-0.0.0.0: Typeclass for monads that can be unlifted to arbitrary base monads
Copyright(c) 2021 Kowainik
(c) 2017 FP Complete
LicenseMPL-2.0
MaintainerKowainik <xrom.xkov@gmail.com>
StabilityStable
PortabilityPortable
Safe HaskellNone
LanguageHaskell2010

Unlift

Description

This module provides MonadUnlift typeclass and functions to work with it.

See MonadUnlift documentation for more information about its purpose.

Since: 0.0.0.0

Synopsis

Documentation

class MonadBase b m => MonadUnlift b m where Source #

Typeclass to allow actions in monadic context m to be run in the base monad b.

This typeclass is similar to MonadUnliftIO from the unliftio package. However MonadUnlift works with any base monad, not only IO.

This typeclass is helpful when writing code that is polymorphic over the base monad, so later you can select a different base monad for each specific use-case.

While you can use lift to allow some action to be lifted into another monad, this class captures the opposite concept.

Instances of this typeclass should satisfy the following laws:

Since: 0.0.0.0

Methods

withRunInBase :: ((forall a. m a -> b a) -> b x) -> m x Source #

Convenient function to capture the monadic context m and run the b action with a runner function. The runner function is used to run a monadic action m in the base monad b.

Since: 0.0.0.0

Instances

Instances details
MonadUnlift IO IO Source #

Since: 0.0.0.0

Instance details

Defined in Unlift

Methods

withRunInBase :: ((forall a. IO a -> IO a) -> IO x) -> IO x Source #

MonadUnlift STM STM Source #

Since: 0.0.0.0

Instance details

Defined in Unlift

Methods

withRunInBase :: ((forall a. STM a -> STM a) -> STM x) -> STM x Source #

MonadUnlift b m => MonadUnlift b (ReaderT r m) Source #

Since: 0.0.0.0

Instance details

Defined in Unlift

Methods

withRunInBase :: ((forall a. ReaderT r m a -> b a) -> b x) -> ReaderT r m x Source #

MonadUnlift b m => MonadUnlift b (IdentityT m) Source #

Since: 0.0.0.0

Instance details

Defined in Unlift

Methods

withRunInBase :: ((forall a. IdentityT m a -> b a) -> b x) -> IdentityT m x Source #

MonadUnlift (ST s) (ST s) Source #

Since: 0.0.0.0

Instance details

Defined in Unlift

Methods

withRunInBase :: ((forall a. ST s a -> ST s a) -> ST s x) -> ST s x Source #

defaultWithRunInBase Source #

Arguments

:: MonadUnlift b n 
=> (n x -> m x)

Wrapper

-> (forall a. m a -> n a)

Unwrapper

-> ((forall a. m a -> b a) -> b x)

Action to do in base monad

-> m x

Result in unlifted monad

A helper function for implementing MonadUnlift instances.

Useful for the common case where you want to simply delegate to the underlying transformer in newtypes.

Example:

newtype AppT m a = AppT
    { unAppT :: ReaderT Int m a
    } deriving newtype (Functor, Applicative, Monad)

instance (MonadUnlift b m) => MonadUnlift b (AppT m)
  where
    withRunInBase = defaultWithRunInBase AppT unAppT

Since: 0.0.0.0

askRunInBase :: MonadUnlift b m => m (m a -> b a) Source #

Capture the current monadic context m, providing the ability to run monadic actions in the base monad b.

Useful when you need to apply on one concrete type.

Note: If you run into issues when using this function, most likely that you need askUnlift instead.

Since: 0.0.0.0

newtype Unlift b m Source #

Polymorphic wrapper over the function returned by withRunInBase.

Use askUnlift instead of askRunInBase when you need to use the return unlift with variables of different types.

Since: 0.0.0.0

Constructors

Unlift 

Fields

askUnlift :: MonadUnlift b m => m (Unlift b m) Source #

Similar to askRunInBase, but works with the Unlift wrapper.

Use askUnlift instead of askRunInBase when you need to use the return unlift with variables of different types.

Since: 0.0.0.0