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 (multi-parameter type classes)
14 -----------------------------------------------------------------------------
16 module Control.Monad.List (
20 module Control.Monad.Trans,
26 import Control.Monad.Trans
27 import Control.Monad.Reader
28 import Control.Monad.State
29 import Control.Monad.Cont
30 import Control.Monad.Error
32 -- ---------------------------------------------------------------------------
33 -- Our parameterizable list monad, with an inner monad
35 newtype ListT m a = ListT { runListT :: m [a] }
37 instance (Monad m) => Functor (ListT m) where
42 instance (Monad m) => Monad (ListT m) where
43 return a = ListT $ return [a]
46 b <- mapM (runListT . k) a
48 fail _ = ListT $ return []
50 instance (Monad m) => MonadPlus (ListT m) where
51 mzero = ListT $ return []
52 m `mplus` n = ListT $ do
57 instance MonadTrans ListT where
62 instance (MonadIO m) => MonadIO (ListT m) where
63 liftIO = lift . liftIO
65 instance (MonadReader s m) => MonadReader s (ListT m) where
67 local f m = ListT $ local f (runListT m)
69 instance (MonadState s m) => MonadState s (ListT m) where
73 instance (MonadCont m) => MonadCont (ListT m) where
76 runListT (f (\a -> ListT $ c [a]))
78 instance (MonadError e m) => MonadError e (ListT m) where
79 throwError = lift . throwError
80 m `catchError` h = ListT $ runListT m
81 `catchError` \e -> runListT (h e)
83 mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
84 mapListT f m = ListT $ f (runListT m)