Module

Control.Monad.Unlift

Package
purescript-unlift
Repository
tweag/purescript-unlift

#MonadUnlift Source

class MonadUnlift :: (Type -> Type) -> (Type -> Type) -> Constraintclass (MonadBase b m) <= MonadUnlift b m | m -> b where

Monads which allow their actions to be run in a base monad.

MonadUnlift captures the opposite notion of MonadBase - while MonadBase allows any base monad b to be lifted into a transformed monad m, MonadUnlift allows m to be run in b, as long as the outer context is in m.

Note that the laws given below require that a monad have no "monadic state", which essentially limits instances to ReaderT and IdentityT stacks.

Instances should satisfy the following laws, which state that unlift is a transformer of monads for any given u returned by askUnlift:

unlift u <<< pure = pure
unlift u (f =<< m) = unlift u <<< f =<< unlift u m

Members

  • withRunInBase :: forall x. ((m ~> b) -> b x) -> m x

    Run a base monad action with access to a runner function that is capable of running a monadic action m in b (strictly speaking, a natural transformation from m to b).

Instances

#Unlift Source

newtype Unlift :: forall k. (k -> Type) -> (k -> Type) -> Typenewtype Unlift b m

A newtype wrapper around a natural transformation from m to b.

Constructors

#unlift Source

unlift :: forall b m. Unlift b m -> m ~> b

Run an action directly in a base monad b. Use askUnlift or withUnlift to obtain an Unlift b m value.

#askUnlift Source

askUnlift :: forall b m. MonadUnlift b m => m (Unlift b m)

Returns a natural transformation from m to b within an m context. This can subsequently be used to run m actions in the base monad b.

#askRunInBase Source

askRunInBase :: forall b m a. MonadUnlift b m => m (m a -> b a)

A monomorphic version of askUnlift which can be more convenient when you only want to use the resulting runner function once with a concrete type.

If you run into type issues using this, try using askUnlit instead.

#withUnlift Source

withUnlift :: forall b m a. MonadUnlift b m => (Unlift b m -> b a) -> m a

A version of withRunInBase that provides an Unlift wrapper instead of a rank-2 polymorphic function.

#toBase Source

toBase :: forall b m a. MonadUnlift b m => m a -> m (b a)

Run the given action inside the base monad b.