polysemy-0.1.0.0: Higher-order, low-boilerplate, zero-cost free monads.

Polysemy.Internal

Synopsis

# Documentation

newtype Semantic r a Source #

The Semantic monad handles computations of arbitrary extensible effects. A value of type Semantic r describes a program with the capabilities of r. For best results, r should always be kept polymorphic, but you can add capabilities via the Member constraint.

The value of the Semantic monad is that it allows you to write programs against a set of effects without a predefined meaning, and provide that meaning later. For example, unlike with mtl, you can decide to interpret an Error effect tradtionally as an Either, or instead significantly faster as an IO Exception. These interpretations (and others that you might add) may be used interchangably without needing to write any newtypes or Monad instances. The only change needed to swap interpretations is to change a call from runError to 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 lifted into a Semantic via sendM.

A Semantic can be interpreted as a pure value (via run) or as any traditional Monad (via runM). Each effect E comes equipped with some interpreters of the form:

runE :: Semantic (E ': r) a -> Semantic r a


which is responsible for removing the effect E from the effect stack. It is the order in which you call the interpreters that determines the monomorphic representation of the r parameter.

After all of your effects are handled, you'll be left with either a Semantic '[] a or a Semantic (Lift m) a value, which can be consumed respectively by run and runM.

#### Examples

As an example of keeping r polymorphic, we can consider the type

Member (State String) r => Semantic r ()


get :: Semantic r String
put :: String -> Semantic r ()


methods.

Member (Error Bool) r


constraint on r, we gain access to the

throw :: Bool -> Semantic r a
catch :: Semantic r a -> (Bool -> Semantic r a) -> Semantic r a


functions as well.

In this sense, a Member (State s) r constraint is analogous to mtl's MonadState s m and should be thought of as such. However, unlike mtl, a Semantic monad may have an arbitrary number of the same effect.

For example, we can write a Semantic program which can output either Ints or Bools:

foo :: ( Member (Output Int) r
, Member (Output Bool) r
)
=> Semantic r ()
foo = do
output @Int  5
output True


Notice that we must use -XTypeApplications to specify that we'd like to use the (Output Int) effect.

Constructors

 Semantic FieldsrunSemantic :: forall m. Monad m => (forall x. Union r (Semantic r) x -> m x) -> m a
Instances
 Source # Instance detailsDefined in Polysemy.Internal Methods(>>=) :: Semantic f a -> (a -> Semantic f b) -> Semantic f b #(>>) :: Semantic f a -> Semantic f b -> Semantic f b #return :: a -> Semantic f a #fail :: String -> Semantic f a # Source # Instance detailsDefined in Polysemy.Internal Methodsfmap :: (a -> b) -> Semantic f a -> Semantic f b #(<\$) :: a -> Semantic f b -> Semantic f a # Source # Instance detailsDefined in Polysemy.Internal Methodsmfix :: (a -> Semantic r a) -> Semantic r a # Source # Instance detailsDefined in Polysemy.Internal Methodspure :: a -> Semantic f a #(<*>) :: Semantic f (a -> b) -> Semantic f a -> Semantic f b #liftA2 :: (a -> b -> c) -> Semantic f a -> Semantic f b -> Semantic f c #(*>) :: Semantic f a -> Semantic f b -> Semantic f b #(<*) :: Semantic f a -> Semantic f b -> Semantic f a # Member (Lift IO) r => MonadIO (Semantic r) Source # Instance detailsDefined in Polysemy.Internal MethodsliftIO :: IO a -> Semantic r a # Source # Instance detailsDefined in Polysemy.Internal Methodsempty :: Semantic r a #(<|>) :: Semantic r a -> Semantic r a -> Semantic r a #some :: Semantic r a -> Semantic r [a] #many :: Semantic r a -> Semantic r [a] #

type Member e r = Member' e r Source #

A proof that the effect e is available somewhere inside of the effect stack r.

send :: Member e r => e (Semantic r) a -> Semantic r a Source #

Lift an effect into a Semantic. This is used primarily via makeSemantic to implement smart constructors.

sendM :: Member (Lift m) r => m a -> Semantic r a Source #

Lift a monadic action m into Semantic.

run :: Semantic '[] a -> a Source #

Run a Semantic containing no effects as a pure value.

runM :: Monad m => Semantic '[Lift m] a -> m a Source #

Lower a Semantic containing only a single lifted Monad into that monad.

raise :: forall e r a. Semantic r a -> Semantic (e ': r) a Source #

Introduce an effect into Semantic. Analogous to lift in the mtl ecosystem

data Lift m (z :: * -> *) a Source #

An effect which allows a regular Monad m into the Semantic ecosystem. Monadic actions in m can be lifted into Semantic via sendM.

For example, you can use this effect to lift IO actions directly into Semantic:

sendM (putStrLn "hello") :: Member (Lift IO) r => Semantic r ()


That being said, you lose out on a significant amount of the benefits of Semantic by using 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.

Consider using trace and runTraceIO as a substitute for using putStrLn directly.

usingSemantic :: Monad m => (forall x. Union r (Semantic r) x -> m x) -> Semantic r a -> m a Source #

Like runSemantic but flipped for better ergonomics sometimes.

hoistSemantic :: (forall x. Union r (Semantic r) x -> Union r' (Semantic r') x) -> Semantic r a -> Semantic r' a Source #

(.@) infixl 9 Source #

Arguments

 :: Monad m => (forall x. Semantic r x -> m x) The lowering function, likely runM. -> (forall y. (forall x. Semantic r x -> m x) -> Semantic (e ': r) y -> Semantic r y) -> Semantic (e ': r) z -> m z

Some interpreters need to be able to lower down to the base monad (often IO) in order to function properly --- some good examples of this are runErrorInIO and runResource.

However, these interpreters don't compose particularly nicely; for example, to run runResource, you must write:

runM . runErrorInIO runM


Notice that runM is duplicated in two places here. The situation gets exponentially worse the more intepreters you have that need to run in this pattern.

Instead, .@ performs the composition we'd like. The above can be written as

(runM .@ runErrorInIO)


The parentheses here are important; without them you'll run into operator precedence errors.

(.@@) infixl 9 Source #

Arguments

 :: Monad m => (forall x. Semantic r x -> m x) The lowering function, likely runM. -> (forall y. (forall x. Semantic r x -> m x) -> Semantic (e ': r) y -> Semantic r (f y)) -> Semantic (e ': r) z -> m (f z)

Like .@, but for interpreters which change the resulting type --- eg. runErrorInIO.