Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
This module exports functions, types, and typeclasses necessary for implementing a custom effect and/or effect handler.
Synopsis
- data Eff r a
- run :: Eff '[] w -> w
- data Union (r :: [* -> *]) v
- class FindElem t r => Member (t :: * -> *) r
- inj :: Member t r => t v -> Union r v
- prj :: Member t r => Union r v -> Maybe (t v)
- decomp :: Union (t ': r) v -> Either (Union r v) (t v)
- class Member t r => SetMember (tag :: k -> * -> *) (t :: * -> *) r | tag r -> t
- weaken :: Union r w -> Union (any ': r) w
- handle_relay :: (a -> Eff r w) -> (forall v. t v -> Arr r v w -> Eff r w) -> Eff (t ': r) a -> Eff r w
- handle_relay_s :: s -> (s -> a -> Eff r w) -> (forall v. s -> t v -> (s -> Arr r v w) -> Eff r w) -> Eff (t ': r) a -> Eff r w
- interpose :: Member t r => (a -> Eff r w) -> (forall v. t v -> Arr r v w -> Eff r w) -> Eff r a -> Eff r w
- raise :: Eff r a -> Eff (e ': r) a
- send :: Member t r => t v -> Eff r v
- type Arr r a b = a -> Eff r b
- data Arrs r a b
- first :: Arr r a b -> Arr r (a, c) (b, c)
- singleK :: Arr r a b -> Arrs r a b
- qApp :: forall r b w. Arrs r b w -> Arr r b w
- (^$) :: forall r b w. Arrs r b w -> Arr r b w
- arr :: (a -> b) -> Arrs r a b
- ident :: Arrs r a a
- comp :: Arrs r a b -> Arrs r b c -> Arrs r a c
- (^|>) :: Arrs r a b -> Arr r b c -> Arrs r a c
- qComp :: Arrs r a b -> (Eff r b -> Eff r' c) -> Arr r' a c
- qComps :: Arrs r a b -> (Eff r b -> Eff r' c) -> Arrs r' a c
The effect monad
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
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.
Open Unions
data Union (r :: [* -> *]) v Source #
The data constructors of Union are not exported
Strong Sum (Existential with the evidence) is an open union t is can be a GADT and hence not necessarily a Functor. Int is the index of t in the list r; that is, the index of t in the universe r
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.
Instances
FindElem t r => Member t r 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 There is no ambiguity when finding instances for
The only case we have to concerned about is |
class Member t r => SetMember (tag :: k -> * -> *) (t :: * -> *) r | tag r -> t Source #
This class is used for emulating monad transformers
Helper functions that are used for implementing effect-handlers
handle_relay :: (a -> Eff r w) -> (forall v. t v -> Arr r v w -> Eff r w) -> Eff (t ': r) a -> Eff r w Source #
A convenient pattern: given a request (open union), either handle it or relay it.
handle_relay_s :: s -> (s -> a -> Eff r w) -> (forall v. s -> t v -> (s -> Arr r v w) -> Eff r w) -> Eff (t ': r) a -> Eff r w Source #
Parameterized handle_relay
interpose :: Member t r => (a -> Eff r w) -> (forall v. t v -> Arr r v w -> Eff r w) -> Eff r a -> Eff r w Source #
Intercept the request and possibly reply to it, but leave it unhandled (that's why the same r is used all throuout)
send :: Member t r => t v -> Eff r v Source #
Send a request and wait for a reply (resulting in an effectful computation).
Arrow types and compositions
type Arr r a b = a -> Eff r b Source #
Effectful arrow type: a function from a to b that also does effects denoted by r
An effectful function from a
to b
that is a composition of one or more
effectful functions. The paremeter r describes the overall effect.
The composition members are accumulated in a type-aligned queue.
Using a newtype here enables us to define Category
and Arrow
instances.