extensible-effects-5.0.0.0: An Alternative to Monad Transformers

Safe HaskellSafe
LanguageHaskell2010

Control.Eff

Contents

Description

A monadic library for implementing effectful computation in a modular way.

This module provides the Eff monad - the base type for all effectful computation. The Member typeclass is the main interface for describing which effects are necessary for a given function.

Consult the Control.Eff.QuickStart module and the readme for gentle introductions.

To use extensible effects effectively some language extensions are necessary/recommended.

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
Synopsis

Effect type

run :: Eff '[] w -> w Source #

Get the result from a pure computation

A pure computation has type Eff '[] a. The empty effect-list indicates that no further effects need to be handled.

data Eff r a Source #

The monad that all effects in this library are based on.

An effectful computation is a value of type `Eff r a`. In this signature, r is a type-level list of effects that are being requested and need to be handled inside an effectful computation. a is the computation's result similar to other monads.

A computation's result can be retrieved via the run function. However, all effects used in the computation need to be handled by the use of the effects' run* functions before unwrapping the final result. For additional details, see the documentation of the effects you are using.

Instances
Alternative f => Handle NDet r a ([Eff r a] -> Eff r' (f w)) Source #

More performant handler; uses reified job queue

Instance details

Defined in Control.Eff.Logic.NDet

Methods

handle :: (Eff r a -> [Eff r a] -> Eff r' (f w)) -> Arrs r v a -> NDet v -> [Eff r a] -> Eff r' (f w) Source #

handle_relay :: (r ~ (NDet ': r'0), Relay ([Eff r a] -> Eff r' (f w)) r'0) => (a -> [Eff r a] -> Eff r' (f w)) -> (Eff r a -> [Eff r a] -> Eff r' (f w)) -> Eff r a -> [Eff r a] -> Eff r' (f w) Source #

respond_relay :: (a -> [Eff r a] -> Eff r' (f w)) -> (Eff r a -> [Eff r a] -> Eff r' (f w)) -> Eff r a -> [Eff r a] -> Eff r' (f w) Source #

Alternative f => Handle NDet r a (Eff r' (f w)) Source #

Given a callback and NDet requests respond to them. Note that this makes explicit that we rely on f to have enough room to store all possibilities.

Instance details

Defined in Control.Eff.Logic.NDet

Methods

handle :: (Eff r a -> Eff r' (f w)) -> Arrs r v a -> NDet v -> Eff r' (f w) Source #

handle_relay :: (r ~ (NDet ': r'0), Relay (Eff r' (f w)) r'0) => (a -> Eff r' (f w)) -> (Eff r a -> Eff r' (f w)) -> Eff r a -> Eff r' (f w) Source #

respond_relay :: (a -> Eff r' (f w)) -> (Eff r a -> Eff r' (f w)) -> Eff r a -> Eff r' (f w) Source #

(MonadBase b m, Lifted m r) => MonadBase b (Eff r) Source # 
Instance details

Defined in Control.Eff.Internal

Methods

liftBase :: b α -> Eff r α #

MonadBase m m => MonadBaseControl m (Eff (Lift m ': ([] :: [Type -> Type]))) Source # 
Instance details

Defined in Control.Eff.Internal

Associated Types

type StM (Eff (Lift m ': [])) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Lift m ': [])) m -> m a) -> Eff (Lift m ': []) a #

restoreM :: StM (Eff (Lift m ': [])) a -> Eff (Lift m ': []) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (Writer w ': r)) Source # 
Instance details

Defined in Control.Eff.Writer.Strict

Associated Types

type StM (Eff (Writer w ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Writer w ': r)) m -> m a) -> Eff (Writer w ': r) a #

restoreM :: StM (Eff (Writer w ': r)) a -> Eff (Writer w ': r) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (Writer w ': r)) Source # 
Instance details

Defined in Control.Eff.Writer.Lazy

Associated Types

type StM (Eff (Writer w ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Writer w ': r)) m -> m a) -> Eff (Writer w ': r) a #

restoreM :: StM (Eff (Writer w ': r)) a -> Eff (Writer w ': r) a #

(MonadBase m m, LiftedBase m s) => MonadBaseControl m (Eff (Reader e ': s)) Source # 
Instance details

Defined in Control.Eff.Reader.Strict

Associated Types

type StM (Eff (Reader e ': s)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Reader e ': s)) m -> m a) -> Eff (Reader e ': s) a #

restoreM :: StM (Eff (Reader e ': s)) a -> Eff (Reader e ': s) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (State s ': r)) Source # 
Instance details

Defined in Control.Eff.State.Strict

Associated Types

type StM (Eff (State s ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (State s ': r)) m -> m a) -> Eff (State s ': r) a #

restoreM :: StM (Eff (State s ': r)) a -> Eff (State s ': r) a #

(MonadBase m m, LiftedBase m s) => MonadBaseControl m (Eff (Reader e ': s)) Source # 
Instance details

Defined in Control.Eff.Reader.Lazy

Associated Types

type StM (Eff (Reader e ': s)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Reader e ': s)) m -> m a) -> Eff (Reader e ': s) a #

restoreM :: StM (Eff (Reader e ': s)) a -> Eff (Reader e ': s) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (State s ': r)) Source # 
Instance details

Defined in Control.Eff.State.Lazy

Associated Types

type StM (Eff (State s ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (State s ': r)) m -> m a) -> Eff (State s ': r) a #

restoreM :: StM (Eff (State s ': r)) a -> Eff (State s ': r) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (OnDemandState s ': r)) Source # 
Instance details

Defined in Control.Eff.State.OnDemand

Associated Types

type StM (Eff (OnDemandState s ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (OnDemandState s ': r)) m -> m a) -> Eff (OnDemandState s ': r) a #

restoreM :: StM (Eff (OnDemandState s ': r)) a -> Eff (OnDemandState s ': r) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (Fresh ': r)) Source # 
Instance details

Defined in Control.Eff.Fresh

Associated Types

type StM (Eff (Fresh ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Fresh ': r)) m -> m a) -> Eff (Fresh ': r) a #

restoreM :: StM (Eff (Fresh ': r)) a -> Eff (Fresh ': r) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff ((Exc e :: Type -> Type) ': r)) Source # 
Instance details

Defined in Control.Eff.Exception

Associated Types

type StM (Eff (Exc e ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Exc e ': r)) m -> m a) -> Eff (Exc e ': r) a #

restoreM :: StM (Eff (Exc e ': r)) a -> Eff (Exc e ': r) a #

(MonadBase m m, LiftedBase m r) => MonadBaseControl m (Eff (NDet ': r)) Source # 
Instance details

Defined in Control.Eff.Logic.NDet

Associated Types

type StM (Eff (NDet ': r)) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (NDet ': r)) m -> m a) -> Eff (NDet ': r) a #

restoreM :: StM (Eff (NDet ': r)) a -> Eff (NDet ': r) a #

Monad (Eff r) Source # 
Instance details

Defined in Control.Eff.Internal

Methods

(>>=) :: Eff r a -> (a -> Eff r b) -> Eff r b #

(>>) :: Eff r a -> Eff r b -> Eff r b #

return :: a -> Eff r a #

fail :: String -> Eff r a #

Functor (Eff r) Source # 
Instance details

Defined in Control.Eff.Internal

Methods

fmap :: (a -> b) -> Eff r a -> Eff r b #

(<$) :: a -> Eff r b -> Eff r a #

Applicative (Eff r) Source # 
Instance details

Defined in Control.Eff.Internal

Methods

pure :: a -> Eff r a #

(<*>) :: Eff r (a -> b) -> Eff r a -> Eff r b #

liftA2 :: (a -> b -> c) -> Eff r a -> Eff r b -> Eff r c #

(*>) :: Eff r a -> Eff r b -> Eff r b #

(<*) :: Eff r a -> Eff r b -> Eff r a #

(MonadIO m, Lifted m r) => MonadIO (Eff r) Source # 
Instance details

Defined in Control.Eff.Internal

Methods

liftIO :: IO a -> Eff r a #

Member NDet r => Alternative (Eff r) Source # 
Instance details

Defined in Control.Eff.Logic.NDet

Methods

empty :: Eff r a #

(<|>) :: Eff r a -> Eff r a -> Eff r a #

some :: Eff r a -> Eff r [a] #

many :: Eff r a -> Eff r [a] #

Member NDet r => MonadPlus (Eff r) Source #

Mapping of NDet requests to MonadPlus. We obey the following laws (taken from the Backtr and @LogicT papers):

mzero >>= f = mzero                               -- (L1)
mzero `mplus` m = m                               -- (L2)
m `mplus` mzero = m                               -- (L3)
m `mplus` (n `mplus` o) = (m `mplus` n) `mplus` o -- (L4)
(m `mplus` n) >>= k = (m >>= k) `mplus` (n >>= k) -- (L5)
  • L1 is the left-zero law for mzero
  • L2, L3, L4 are the Monoid laws

NOTE that we do not obey the right-zero law for mzero. Specifically, we do not obey:

m >> mzero  = mzero
Instance details

Defined in Control.Eff.Logic.NDet

Methods

mzero :: Eff r a #

mplus :: Eff r a -> Eff r a -> Eff r a #

Member NDet r => MSplit (Eff r) Source #

We implement LogicT, the non-determinism reflection, of which soft-cut is one instance. See the LogicT paper for an explanation.

Instance details

Defined in Control.Eff.Logic.NDet

Methods

msplit :: Eff r a -> Eff r (Maybe (a, Eff r a)) Source #

Relay (Eff r w) r Source # 
Instance details

Defined in Control.Eff.Internal

Methods

relay :: (v -> Eff r w) -> Union r v -> Eff r w Source #

Handle (Program f) r a (Intrprtr f r' -> Eff r' a) Source #

Given a continuation and a program, interpret it Usually, we have r ~ [Program f : r']

Instance details

Defined in Control.Eff.Operational

Methods

handle :: (Eff r a -> Intrprtr f r' -> Eff r' a) -> Arrs r v a -> Program f v -> Intrprtr f r' -> Eff r' a Source #

handle_relay :: (r ~ (Program f ': r'0), Relay (Intrprtr f r' -> Eff r' a) r'0) => (a -> Intrprtr f r' -> Eff r' a) -> (Eff r a -> Intrprtr f r' -> Eff r' a) -> Eff r a -> Intrprtr f r' -> Eff r' a Source #

respond_relay :: (a -> Intrprtr f r' -> Eff r' a) -> (Eff r a -> Intrprtr f r' -> Eff r' a) -> Eff r a -> Intrprtr f r' -> Eff r' a Source #

Handle (Yield a b) (Yield a b ': r) w (Eff r (Y r b a)) Source #

Given a continuation and a request, respond to it

Instance details

Defined in Control.Eff.Coroutine

Methods

handle :: (Eff (Yield a b ': r) w -> Eff r (Y r b a)) -> Arrs (Yield a b ': r) v w -> Yield a b v -> Eff r (Y r b a) Source #

handle_relay :: ((Yield a b ': r) ~ (Yield a b ': r'), Relay (Eff r (Y r b a)) r') => (w -> Eff r (Y r b a)) -> (Eff (Yield a b ': r) w -> Eff r (Y r b a)) -> Eff (Yield a b ': r) w -> Eff r (Y r b a) Source #

respond_relay :: (w -> Eff r (Y r b a)) -> (Eff (Yield a b ': r) w -> Eff r (Y r b a)) -> Eff (Yield a b ': r) w -> Eff r (Y r b a) Source #

type StM (Eff (Lift m ': ([] :: [Type -> Type]))) a Source # 
Instance details

Defined in Control.Eff.Internal

type StM (Eff (Lift m ': ([] :: [Type -> Type]))) a = a
type StM (Eff (Writer w ': r)) a Source # 
Instance details

Defined in Control.Eff.Writer.Strict

type StM (Eff (Writer w ': r)) a = StM (Eff r) (a, [w])
type StM (Eff (Writer w ': r)) a Source # 
Instance details

Defined in Control.Eff.Writer.Lazy

type StM (Eff (Writer w ': r)) a = StM (Eff r) (a, [w])
type StM (Eff (Reader e ': s)) a Source # 
Instance details

Defined in Control.Eff.Reader.Strict

type StM (Eff (Reader e ': s)) a = StM (Eff s) a
type StM (Eff (State s ': r)) a Source # 
Instance details

Defined in Control.Eff.State.Strict

type StM (Eff (State s ': r)) a = StM (Eff r) (a, s)
type StM (Eff (Reader e ': s)) a Source # 
Instance details

Defined in Control.Eff.Reader.Lazy

type StM (Eff (Reader e ': s)) a = StM (Eff s) a
type StM (Eff (State s ': r)) a Source # 
Instance details

Defined in Control.Eff.State.Lazy

type StM (Eff (State s ': r)) a = StM (Eff r) (a, s)
type StM (Eff (OnDemandState s ': r)) a Source # 
Instance details

Defined in Control.Eff.State.OnDemand

type StM (Eff (OnDemandState s ': r)) a = StM (Eff r) (a, s)
type StM (Eff (Fresh ': r)) a Source # 
Instance details

Defined in Control.Eff.Fresh

type StM (Eff (Fresh ': r)) a = StM (Eff r) (a, Int)
type StM (Eff ((Exc e :: Type -> Type) ': r)) a Source # 
Instance details

Defined in Control.Eff.Exception

type StM (Eff ((Exc e :: Type -> Type) ': r)) a = StM (Eff r) (Either e a)
type StM (Eff (NDet ': r)) a Source # 
Instance details

Defined in Control.Eff.Logic.NDet

type StM (Eff (NDet ': r)) a = StM (Eff r) [a]

Lift IO computations

lift :: Lifted m r => m a -> Eff r a Source #

embed an operation of type `m a` into the Eff monad when Lift m is in a part of the effect-list.

runLift :: Monad m => Eff '[Lift m] w -> m w Source #

The handler of Lift requests. It is meant to be terminal: we only allow a single Lifted Monad. Note, too, how this is different from other handlers.

catchDynE :: forall e a r. (Lifted IO r, Exception e) => Eff r a -> (e -> Eff r a) -> Eff r a Source #

Catching of dynamic exceptions See the problem in http://okmij.org/ftp/Haskell/misc.html#catch-MonadIO

data HandlerDynE r a Source #

You need this when using catchesDynE.

Constructors

(Exception e, Lifted IO r) => HandlerDynE (e -> Eff r a) 

catchesDynE :: Lifted IO r => Eff r a -> [HandlerDynE r a] -> Eff r a Source #

Catch multiple dynamic exceptions. The implementation follows that in Control.Exception almost exactly. Not yet tested. Could this be useful for control with cut?

newtype Lift m a Source #

Lifting: emulating monad transformers

Constructors

Lift 

Fields

Instances
MonadBase m m => MonadBaseControl m (Eff (Lift m ': ([] :: [Type -> Type]))) Source # 
Instance details

Defined in Control.Eff.Internal

Associated Types

type StM (Eff (Lift m ': [])) a :: Type #

Methods

liftBaseWith :: (RunInBase (Eff (Lift m ': [])) m -> m a) -> Eff (Lift m ': []) a #

restoreM :: StM (Eff (Lift m ': [])) a -> Eff (Lift m ': []) a #

Monad m => Handle (Lift m) r a (m k) Source #

Handle lifted requests by running them sequentially

Instance details

Defined in Control.Eff.Internal

Methods

handle :: (Eff r a -> m k) -> Arrs r v a -> Lift m v -> m k Source #

handle_relay :: (r ~ (Lift m ': r'), Relay (m k) r') => (a -> m k) -> (Eff r a -> m k) -> Eff r a -> m k Source #

respond_relay :: (a -> m k) -> (Eff r a -> m k) -> Eff r a -> m k Source #

type StM (Eff (Lift m ': ([] :: [Type -> Type]))) a Source # 
Instance details

Defined in Control.Eff.Internal

type StM (Eff (Lift m ': ([] :: [Type -> Type]))) a = a

type Lifted m r = SetMember Lift (Lift m) r Source #

A convenient alias to SetMember Lift (Lift m) r, which allows us to assert that the lifted type occurs ony once in the effect list.

type LiftedBase m r = (SetMember Lift (Lift m) r, MonadBaseControl m (Eff r)) Source #

Same as Lifted but with additional MonadBaseControl constraint

Effect list

class FindElem t r => Member (t :: * -> *) r Source #

Typeclass that asserts that effect t is contained inside the effect-list r.

The FindElem typeclass is an implementation detail and not required for using the effect list or implementing custom effects.

Minimal complete definition

inj, prj

Instances
FindElem t r => Member t r Source # 
Instance details

Defined in Data.OpenUnion

Methods

inj :: t v -> Union r v Source #

prj :: Union r v -> Maybe (t v) Source #

t ~ s => Member t (s ': ([] :: [Type -> Type])) Source #

Explicit type-level equality condition is a dirty hack to eliminate the type annotation in the trivial case, such as run (runReader () get).

There is no ambiguity when finding instances for Member t (a ': b ': r), which the second instance is selected.

The only case we have to concerned about is Member t '[s]. But, in this case, values of definition is the same (if present), and the first one is chosen according to GHC User Manual, since the latter one is incoherent. This is the optimal choice.

Instance details

Defined in Data.OpenUnion

Methods

inj :: t v -> Union (s ': []) v Source #

prj :: Union (s ': []) v -> Maybe (t v) Source #

class Member t r => SetMember (tag :: k -> * -> *) (t :: * -> *) r | tag r -> t Source #

This class is used for emulating monad transformers

Instances
(EQU t1 t2 p, MemberU' p tag t1 (t2 ': r)) => SetMember (tag :: k -> Type -> Type) t1 (t2 ': r) Source # 
Instance details

Defined in Data.OpenUnion

type family (ms :: [* -> *]) <:: r where ... Source #

A useful operator for reducing boilerplate in signatures.

The following lines are equivalent.

(Member (Exc e) r, Member (State s) r) => ...
[ Exc e, State s ] r = ...

Equations

'[] <:: r = (() :: Constraint) 
(m ': ms) <:: r = (Member m r, (<::) ms r)