Safe Haskell | None |
---|---|
Language | Haskell2010 |
This is the simplest implementation of the effect monad
directly using a Free
over a Union
of functors
representing effects
- data Eff r a
- type Handler e r a b = Comp e r a b -> Res r b
- data Comp e r a b
- effect :: (forall b. (a -> Res r b) -> Union r (Res r b)) -> Eff r a
- runPure :: Eff [] a -> a
- runPureRes :: Res [] a -> a
- finish :: Eff r a -> Res r a
- continue :: Res r a -> Eff r a
- handle :: (Functor e, Typeable e) => Handler e r a b -> Eff (e : r) a -> Eff r b
- inj :: (Typeable f, Functor f, Member f r) => f a -> Union r a
- class Member f r
- class Typeable a
Documentation
data Eff r a
The type of programs using effects. Indexed by a type list of functors representing effects. This implementation is just a wrapper for the result type.
type Handler e r a b = Comp e r a b -> Res r b
Handler is a function that takes a result or an effect and a continuation |and handles it.
e
is the effect functor you are handling
r
represents the type of the type list of the remaining effects.
Usually you want to be polymorphic in this.
a
is the result type of the program you will handle
b
is the result of handled computation.
data Comp e r a b
Comp represents a computation. It is either a pure value or a computation that needs further evaluation and effect handling.
A program without effects is guaranteed to be pure so you can safely convert it into a value.
runPureRes :: Res [] a -> a
Like runPure
but for program results. You only need this for implementing
some handlers.
Finish a program and convert it into a result structure. Free at runtime.
continue :: Res r a -> Eff r a
Convert a result back into a program in order to compose it. Free at runtime.
handle :: (Functor e, Typeable e) => Handler e r a b -> Eff (e : r) a -> Eff r b
Use a Handler
on an Eff
program to stripe away the first layer of effects.
There are some issues if you are using a handler that is somewhat polymorphic in e
As the compiler cannot figure out which effect are you handling. Currently the best
solution seems to be to manually specify type of the handler such that it is monomorphic
in e
. Sorry.
class Typeable a
The class Typeable
allows a concrete representation of a type to
be calculated.
Typeable * Bool | |
Typeable * Char | |
Typeable * Double | |
Typeable * Float | |
Typeable * Int | |
Typeable * Integer | |
Typeable * Ordering | |
Typeable * RealWorld | |
Typeable * Word | |
Typeable * Word8 | |
Typeable * Word16 | |
Typeable * Word32 | |
Typeable * Word64 | |
Typeable * () | |
Typeable * TypeRep | |
Typeable * TyCon | |
(Typeable (k1 -> k) s, Typeable k1 a) => Typeable k (s a) | Kind-polymorphic Typeable instance for type application |
Typeable ((* -> *) -> * -> *) Free | |
Typeable ((* -> *) -> Constraint) Alternative | |
Typeable ((* -> *) -> Constraint) Applicative | |
Typeable (* -> * -> * -> * -> * -> * -> * -> *) (,,,,,,) | |
Typeable (* -> * -> * -> * -> * -> * -> *) (,,,,,) | |
Typeable (* -> * -> * -> * -> * -> *) (,,,,) | |
Typeable (* -> * -> * -> * -> *) (,,,) | |
Typeable (* -> * -> * -> *) (,,) | |
Typeable (* -> * -> * -> *) STArray | |
Typeable (* -> * -> *) (->) | |
Typeable (* -> * -> *) Either | |
Typeable (* -> * -> *) (,) | |
Typeable (* -> * -> *) ST | |
Typeable (* -> * -> *) Array | |
Typeable (* -> * -> *) STRef | |
Typeable (* -> * -> *) Reader | |
Typeable (* -> * -> *) Writer | |
Typeable (* -> * -> *) State | |
Typeable (* -> * -> *) Exception | |
Typeable (* -> * -> *) Search | |
Typeable (* -> * -> *) Reader | |
Typeable (* -> * -> *) Writer | |
Typeable (* -> * -> *) State | |
Typeable (* -> * -> *) Exception | |
Typeable (* -> * -> *) Search | |
Typeable (* -> * -> *) Reader | |
Typeable (* -> * -> *) Writer | |
Typeable (* -> * -> *) State | |
Typeable (* -> * -> *) Exception | |
Typeable (* -> * -> *) Search | |
Typeable (* -> *) [] | |
Typeable (* -> *) Ratio | |
Typeable (* -> *) IO | |
Typeable (* -> *) Ptr | |
Typeable (* -> *) FunPtr | |
Typeable (* -> *) Maybe | |
Typeable (* -> *) LiftIO | |
Typeable (* -> *) LiftIO | |
Typeable (* -> *) LiftIO | |
Typeable (* -> Constraint) Monoid | |
Typeable (k -> *) (Proxy k) | |
Typeable (k -> k -> *) (Coercion k) | |
Typeable (k -> k -> *) ((:~:) k) |