module Control.Monad.Parameterized (
Return (returnM)
, Fail (fail)
, Bind ((>>=),(>>))
, (=<<)
, MPlus (mplus)
, MonadZero (mzeroM)
, MZero
, Monad
, MonadPlus
, Go (go)
, return
, mzero
, module Control.Concurrent.STM
, module Control.Monad.Cont
, module Control.Monad.Cont.Class
, module Control.Monad.Error
, module Control.Monad.Error.Class
, module Control.Monad.Fix
, module Control.Monad.Identity
, module Control.Monad.List
, module Control.Monad.Reader
, module Control.Monad.State
, module Control.Monad.Writer.Class
, mapM
, mapM_
, forM
, forM_
, sequence
, sequence_
, join
, msum
, filterM
, mapAndUnzipM
, zipWithM
, zipWithM_
, foldM
, foldM_
, replicateM
, replicateM_
, guard
, when
, unless
, liftM
, liftM2
, liftM3
, liftM4
, liftM5
, ap
) where
import Data.Monoid
import Prelude hiding (Monad,(>>=),(>>),return,fail,(=<<))
import Data.Maybe (catMaybes)
import Control.Concurrent.STM
import Control.Monad
(forM, forM_, join, msum, filterM, mapAndUnzipM, zipWithM, zipWithM_,
foldM, foldM_, replicateM, replicateM_, guard, when, unless, liftM, liftM2, liftM3, liftM4, liftM5, ap)
import Control.Monad.Cont
(Cont, mapCont, withCont, ContT(..),mapContT,withContT)
import Control.Monad.Cont.Class
(MonadCont, callCC)
import Control.Monad.Error
(ErrorT, runErrorT, mapErrorT)
import Control.Monad.Error.Class
(Error, noMsg, strMsg, MonadError, throwError, catchError)
import Control.Monad.Fix
(MonadFix, mfix, fix)
import Control.Monad.Identity (Identity,runIdentity)
import Control.Monad.State
(State, StateT(..),MonadState
,evalState,execState,mapState,withState
,evalStateT,execStateT,mapStateT,withStateT)
import Control.Monad.ST.Strict as StrictST
(ST)
import Control.Monad.ST.Lazy as LazyST
(ST)
import Control.Monad.Reader
(Reader, ReaderT(..)
,mapReader,withReader,mapReaderT,withReaderT)
import qualified Control.Monad.Writer.Lazy as LazyW
(Writer, WriterT)
import qualified Control.Monad.Writer.Strict as StrictW
(Writer, WriterT)
import Control.Monad.Writer.Class
import Control.Monad.List (ListT(..),runListT)
import qualified Control.Monad as Old
infixl 1 >>, >>=
infixr 1 =<<
return :: a -> Identity a
return = Old.return
class Fail m where
fail :: String -> m a
class (Functor m, Functor m', Functor m'') => Bind m m' m'' | m m' -> m'' where
(>>=) :: m a -> (a -> m' b) -> (m'' b)
(>>) :: m a -> m' b -> m'' b
m >> k = m >>= const k
instance Functor a => Bind Identity a a where m >>= f = f (runIdentity m)
instance Functor a => Bind a Identity a where m >>= f = fmap (runIdentity . f) m
instance Bind Identity Identity Identity where m >>= f = f (runIdentity m)
(=<<) :: Bind m m' m'' => (a -> m' b) -> m a -> m'' b
k =<< m = m >>= k
class (Fail m, Return m, Bind m m m) => Monad m
instance (Fail m, Return m, Bind m m m) => Monad m
data MZero a
mzero :: MZero a
mzero = undefined
instance Functor MZero where fmap _ = undefined
class MPlus m m' m'' | m m' -> m'' where
mplus :: m a -> m' a -> m'' a
instance MPlus MZero a a where mplus _ a = a
instance MPlus a MZero a where mplus a _ = a
instance MPlus MZero MZero MZero where mplus _ _ = undefined
instance Functor a => Bind MZero a MZero where _ >>= _ = undefined
instance Functor a => Bind a MZero MZero where _ >>= _ = undefined
instance Bind MZero MZero MZero where _ >>= _ = undefined
instance Bind Identity MZero MZero where _ >>= _ = undefined
instance Bind MZero Identity MZero where _ >>= _ = undefined
class Return m where
returnM :: a -> m a
class MonadZero m where
mzeroM :: m a
class Go n m where
go :: n a -> m a
instance Return a => Go Identity a where go = returnM . runIdentity
instance MonadZero a => Go MZero a where go _ = mzeroM
instance Go a a where go = id
instance Return Identity where returnM = return
instance Return MZero where returnM _ = undefined
class (MPlus m m m, MonadZero m) => MonadPlus m
instance (MPlus m m m, MonadZero m) => MonadPlus m
instance Return Maybe where returnM = Old.return
instance Fail Maybe where fail = Old.fail
instance Bind Maybe Maybe Maybe where (>>=) = (Old.>>=)
instance MonadZero Maybe where mzeroM = Old.mzero
instance MPlus Maybe Maybe Maybe where mplus = Old.mplus
instance Return [] where returnM = Old.return
instance Fail [] where fail = Old.fail
instance Bind [] [] [] where (>>=) = (Old.>>=)
instance MonadZero [] where mzeroM = Old.mzero
instance MPlus [] [] [] where mplus = Old.mplus
instance Return STM where returnM = Old.return
instance Fail STM where fail = Old.fail
instance Bind STM STM STM where (>>=) = (Old.>>=)
instance Return IO where returnM = Old.return
instance Fail IO where fail = Old.fail
instance Bind IO IO IO where (>>=) = (Old.>>=)
instance Return (State s) where returnM = Old.return
instance Fail (State s) where fail = Old.fail
instance Bind (State s) (State s) (State s) where (>>=) = (Old.>>=)
instance Return (Reader e) where returnM = Old.return
instance Fail (Reader e) where fail = Old.fail
instance Bind (Reader e) (Reader e) (Reader e) where (>>=) = (Old.>>=)
instance Return (Cont r) where returnM = Old.return
instance Fail (Cont r) where fail = Old.fail
instance Bind (Cont r) (Cont r) (Cont r) where (>>=) = (Old.>>=)
instance Return (StrictST.ST s) where returnM = Old.return
instance Fail (StrictST.ST s) where fail = Old.fail
instance Bind (StrictST.ST s) (StrictST.ST s) (StrictST.ST s) where (>>=) = (Old.>>=)
instance Return (LazyST.ST s) where returnM = Old.return
instance Fail (LazyST.ST s) where fail = Old.fail
instance Bind (LazyST.ST s) (LazyST.ST s) (LazyST.ST s) where (>>=) = (Old.>>=)
instance Monoid w => Return (LazyW.Writer w) where returnM = Old.return
instance Monoid w => Fail (LazyW.Writer w) where fail = Old.fail
instance Monoid w => Bind (LazyW.Writer w) (LazyW.Writer w) (LazyW.Writer w) where (>>=) = (Old.>>=)
instance Monoid w => Return (StrictW.Writer w) where returnM = Old.return
instance Monoid w => Fail (StrictW.Writer w) where fail = Old.fail
instance Monoid w => Bind (StrictW.Writer w) (StrictW.Writer w) (StrictW.Writer w) where (>>=) = (Old.>>=)
instance Old.Monad m => Return (ListT m) where returnM = Old.return
instance Old.Monad m => Fail (ListT m) where fail = Old.fail
instance (Functor m, Old.Monad m) => Bind (ListT m) (ListT m) (ListT m) where (>>=) = (Old.>>=)
instance Old.Monad m => MonadZero (ListT m) where mzeroM = Old.mzero
instance Old.Monad m => MPlus (ListT m) (ListT m) (ListT m) where mplus = Old.mplus
instance Old.Monad m => Return (StateT s m) where returnM = Old.return
instance Old.Monad m => Fail (StateT s m) where fail = Old.fail
instance (Functor m, Old.Monad m) => Bind (StateT s m) (StateT s m) (StateT s m) where (>>=) = (Old.>>=)
instance Old.Monad m => Return (ReaderT e m) where returnM = Old.return
instance Old.Monad m => Fail (ReaderT e m) where fail = Old.fail
instance (Functor m, Old.Monad m) => Bind (ReaderT e m) (ReaderT e m) (ReaderT e m) where (>>=) = (Old.>>=)
instance (Old.Monad m, Monoid w) => Return (LazyW.WriterT w m) where returnM = Old.return
instance (Old.Monad m, Monoid w) => Fail (LazyW.WriterT w m) where fail = Old.fail
instance (Functor m, Old.Monad m, Monoid w) => Bind (LazyW.WriterT w m) (LazyW.WriterT w m) (LazyW.WriterT w m) where (>>=) = (Old.>>=)
instance (Old.Monad m, Monoid w) => Return (StrictW.WriterT w m) where returnM = Old.return
instance (Old.Monad m, Monoid w) => Fail (StrictW.WriterT w m) where fail = Old.fail
instance (Functor m, Old.Monad m, Monoid w) => Bind (StrictW.WriterT w m) (StrictW.WriterT w m) (StrictW.WriterT w m) where (>>=) = (Old.>>=)
instance (Old.Monad m, Error e) => Return (ErrorT e m) where returnM = Old.return
instance (Old.Monad m, Error e) => Fail (ErrorT e m) where fail = Old.fail
instance (Functor m, Old.Monad m, Error e) => Bind (ErrorT e m) (ErrorT e m) (ErrorT e m) where (>>=) = (Old.>>=)
instance (Old.Monad m, Error e) => MonadZero (ErrorT e m) where mzeroM = Old.mzero
instance (Old.Monad m, Error e) => MPlus (ErrorT e m) (ErrorT e m) (ErrorT e m) where mplus = Old.mplus
instance Old.Monad m => Return (ContT r m) where returnM = Old.return
instance Old.Monad m => Fail (ContT r m) where fail = Old.fail
instance Old.Monad m => Bind (ContT r m) (ContT r m) (ContT r m) where (>>=) = (Old.>>=)
instance Bind Maybe [] [] where
Just a >>= f = f a
Nothing >>= _ = []
instance Bind [] Maybe [] where
xs >>= f = catMaybes $ map f xs
instance Bind STM IO IO where m >>= k = atomically m >>= k
instance Bind IO STM IO where m >>= k = m >>= (atomically . k)
instance Bind [] IO (ListT IO) where
xs >>= f = ListT (mapM f xs)
testSTMIO = newTVar 2 >>= readTVar >>= print
testMaybeList = Just 2 >>= \x -> [x*1,x*2]
testIdentityMaybe =
return 2 >>= (Just . (*4)) >>= \y ->
[y,y+1] >>= \z ->
if z `mod` 2 == 0 then Just z else Nothing