1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.List
4 -- Copyright : (c) Andy Gill 2001,
5 -- (c) Oregon Graduate Institute of Science and Technology, 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable ( requires mulit-parameter type classes )
14 -----------------------------------------------------------------------------
16 module Control.Monad.List (
21 module Control.Monad.Trans,
27 import Control.Monad.Trans
28 import Control.Monad.Reader
29 import Control.Monad.State
30 import Control.Monad.Cont
31 import Control.Monad.Error
33 -- ---------------------------------------------------------------------------
34 -- Our parameterizable list monad, with an inner monad
36 newtype ListT m a = ListT { runListT :: m [a] }
38 instance (Monad m) => Functor (ListT m) where
43 instance (Monad m) => Monad (ListT m) where
44 return a = ListT $ return [a]
47 b <- mapM (runListT . k) a
49 fail _ = ListT $ return []
51 instance (Monad m) => MonadPlus (ListT m) where
52 mzero = ListT $ return []
53 m `mplus` n = ListT $ do
58 instance MonadTrans ListT where
63 instance (MonadIO m) => MonadIO (ListT m) where
64 liftIO = lift . liftIO
66 instance (MonadReader s m) => MonadReader s (ListT m) where
68 local f m = ListT $ local f (runListT m)
70 instance (MonadState s m) => MonadState s (ListT m) where
74 instance (MonadCont m) => MonadCont (ListT m) where
77 runListT (f (\a -> ListT $ c [a]))
79 instance (MonadError e m) => MonadError e (ListT m) where
80 throwError = lift . throwError
81 m `catchError` h = ListT $ runListT m
82 `catchError` \e -> runListT (h e)
84 mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
85 mapListT f m = ListT $ f (runListT m)