Copyright | (c) Evgeny Poberezkin |
---|---|
License | BSD3 |
Maintainer | evgeny@poberezkin.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Safe |
Language | Haskell2010 |
This module defines a "freer indexed monad" XFree
.
It generalizes freer monad to have type indices/parameters:
- Freer Monads, More Extensible Effects by Oleg Kiselyov and Hiromi Ishii
- Parameterized monads
- Parameterized extensible effects and session types
It defines Functor
instance for
, XFree
f p qXApplicative
and XMonad
instances for
,
as well as XFree
fApplicative
and Monad
instances for
, where f is an effect of kind XFree
f p pk -> k -> Type -> Type
XFree
simplifies defining indexed monadic computations as GADTs without making
them into ad-hoc indexed monads and defining all needed applicative and monadic functions on them.
Example
Given an indexed (non-composable) effect XState that allows changing data type of the stored data and tracks these changes on the type level:
data IxdState s s' x where XGet :: IxdState s s s XPut :: s' -> IxdState s s' ()
you can make it into an indexed monad and use it with do notation
(with RebindableSyntax
and Control.XMonad.
)
with a few lines of boilerplate:Do
type XState = XFree IxdState xGet :: XState s s s xGet = xfree XGet xPut :: s' -> XState s s' () xPut = xfree . XPut
To execute this effect you need an interpreter:
runXState :: XState s s' x -> s -> (x, s') runXState (Pure x) s = (x, s) runXState (Bind m j) s = let (x, s') = unIxdState m s in runXState (j x) s' unIxdState :: IxdState s s' x -> (s -> (x, s')) unIxdState XGet s = (s, s) unIxdState (XPut s) _ = ((), s)
Documentation
data XFree f p q a where Source #
XFree
is the freer indexed monad that wraps an (algebraic, non-composable) effect
to provide Functor
, XApplicative
and XMonad
(indexed applicative and monad) for free.
Instances
XApplicative (XFree f :: k -> k -> Type -> Type) Source # | |
Defined in Control.XFreer xpure :: a -> XFree f p p a Source # (<*>:) :: XFree f p q (a -> b) -> XFree f q r a -> XFree f p r b Source # xliftA2 :: (a -> b -> c) -> XFree f p q a -> XFree f q r b -> XFree f p r c Source # (*>:) :: XFree f p q a -> XFree f q r b -> XFree f p r b Source # (<*:) :: XFree f p q a -> XFree f q r b -> XFree f p r a Source # | |
XMonad (XFree f :: k -> k -> Type -> Type) Source # | |
Monad (XFree f p p) Source # |
|
Functor (XFree f p q) Source # | |
Applicative (XFree f p p) Source # |
|
Defined in Control.XFreer |