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/core/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable ( requires mulit-parameter type classes )
12 -- $Id: List.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
16 -----------------------------------------------------------------------------
18 module Control.Monad.List (
23 module Control.Monad.Trans,
29 import Control.Monad.Trans
30 import Control.Monad.Reader
31 import Control.Monad.State
32 import Control.Monad.Cont
33 import Control.Monad.Error
35 -- ---------------------------------------------------------------------------
36 -- Our parameterizable list monad, with an inner monad
38 newtype ListT m a = ListT { runListT :: m [a] }
40 instance (Monad m) => Functor (ListT m) where
45 instance (Monad m) => Monad (ListT m) where
46 return a = ListT $ return [a]
49 b <- mapM (runListT . k) a
51 fail _ = ListT $ return []
53 instance (Monad m) => MonadPlus (ListT m) where
54 mzero = ListT $ return []
55 m `mplus` n = ListT $ do
60 instance MonadTrans ListT where
65 instance (MonadIO m) => MonadIO (ListT m) where
66 liftIO = lift . liftIO
68 instance (MonadReader s m) => MonadReader s (ListT m) where
70 local f m = ListT $ local f (runListT m)
72 instance (MonadState s m) => MonadState s (ListT m) where
76 instance (MonadCont m) => MonadCont (ListT m) where
79 runListT (f (\a -> ListT $ c [a]))
81 instance (MonadError e m) => MonadError e (ListT m) where
82 throwError = lift . throwError
83 m `catchError` h = ListT $ runListT m
84 `catchError` \e -> runListT (h e)
86 mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
87 mapListT f m = ListT $ f (runListT m)