This module provides alternatives to the Functor
, Monad
and MonadPlus
classes,
allowing for constraints on the contained type (a restricted monad).
It makes use of associated datatypes (available in GHC 6.8).
To make your own type instances of these classes, first define
the Suitable
type class for it. For example,
instance Ord a => Suitable Set a where data Constraints Set a = Ord a => SetConstraints constraints _ = SetConstraints
You need to change Set
to your own type, Ord a
to your own
constraints, and SetConstraints
to some distinguished name (this name
will not normally be visible to users of your type)
Next you can make an instance of RMonad
and if appropriate RMonadPlus
by defining the members in the usual way. When you need to make use of the
constraint on the contained type, you will need to get hold of the constraint
wrapped up in the Constraints
datatype. For example here are the instances
for Set
:
instance RMonad Set where return = Set.singleton s >>= f = let res = case constraints res of SetConstraints -> Set.fold (a s' -> Set.union (f a) s') Set.empty s in res fail _ = Set.empty
instance RMonadPlus Set where mzero = Set.empty mplus s1 s2 = let res = case constraints res of SetConstraints -> Set.union s1 s2 in res
Once you have made your type an instance of RMonad
, you can
use it in two ways.
Firstly, import this module directly and use the NoImplicitPrelude
extension
so that do-syntax is rebound.
Secondly, use the wrapper type in Control.RMonad.AsMonad which supports
the normal Monad
operations.
Documentation
class Suitable m a whereSource
constraints :: m a -> Constraints m aSource
Suitable [] a | |
Suitable IO a | |
Suitable Maybe a | |
Ord a => Suitable Set a | |
Suitable ((->) r) a | |
(Suitable m a, Suitable m [a]) => Suitable (ListT m) a | |
(Ord a, Suitable m a, Suitable m (Set a)) => Suitable (SetT m) a | |
Suitable m a => Suitable (ReaderT r m) a | |
(Suitable m a, Suitable m r) => Suitable (ContT r m) a |
class RMonad m => RMonadPlus m whereSource
RMonadPlus [] | |
RMonadPlus Maybe | |
RMonadPlus Set | |
RMonad m => RMonadPlus (ListT m) | |
RMonad m => RMonadPlus (SetT m) |
(<=<) :: (RMonad m, Suitable m a, Suitable m b, Suitable m c) => (b -> m c) -> (a -> m b) -> a -> m cSource
(>=>) :: (RMonad m, Suitable m a, Suitable m b, Suitable m c) => (a -> m b) -> (b -> m c) -> a -> m cSource
liftM2 :: (RMonad m, Suitable m a1, Suitable m a2, Suitable m r) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rSource
liftM3 :: (RMonad m, Suitable m a1, Suitable m a2, Suitable m a3, Suitable m r) => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m rSource
liftM4 :: (RMonad m, Suitable m a1, Suitable m a2, Suitable m a3, Suitable m a4, Suitable m r) => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m rSource
liftM5 :: (RMonad m, Suitable m a1, Suitable m a2, Suitable m a3, Suitable m a4, Suitable m a5, Suitable m r) => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m rSource
mapAndUnzipM :: (RMonad m, Suitable m (b, c), Suitable m [(b, c)], Suitable m ([b], [c])) => (a -> m (b, c)) -> [a] -> m ([b], [c])Source
msum :: (RMonadPlus m, Suitable m a) => [m a] -> m aSource