{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Monad.Trans.List
{-# DEPRECATED "This transformer is invalid on most monads" #-} (
ListT(..),
mapListT,
liftCallCC,
liftCatch,
) where
import Control.Monad.IO.Class
import Control.Monad.Signatures
import Control.Monad.Trans.Class
import Data.Functor.Classes
#if MIN_VERSION_base(4,12,0)
import Data.Functor.Contravariant
#endif
import Control.Applicative
import Control.Monad
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
import Control.Monad.Fix
#if MIN_VERSION_base(4,4,0)
import Control.Monad.Zip (MonadZip(mzipWith))
#endif
import Data.Foldable (Foldable(foldMap))
import Data.Traversable (Traversable(traverse))
newtype ListT m a = ListT { ListT m a -> m [a]
runListT :: m [a] }
instance (Eq1 m) => Eq1 (ListT m) where
liftEq :: (a -> b -> Bool) -> ListT m a -> ListT m b -> Bool
liftEq a -> b -> Bool
eq (ListT m [a]
x) (ListT m [b]
y) = ([a] -> [b] -> Bool) -> m [a] -> m [b] -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq ((a -> b -> Bool) -> [a] -> [b] -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq) m [a]
x m [b]
y
{-# INLINE liftEq #-}
instance (Ord1 m) => Ord1 (ListT m) where
liftCompare :: (a -> b -> Ordering) -> ListT m a -> ListT m b -> Ordering
liftCompare a -> b -> Ordering
comp (ListT m [a]
x) (ListT m [b]
y) = ([a] -> [b] -> Ordering) -> m [a] -> m [b] -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering) -> [a] -> [b] -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
comp) m [a]
x m [b]
y
{-# INLINE liftCompare #-}
instance (Read1 m) => Read1 (ListT m) where
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (ListT m a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = (String -> ReadS (ListT m a)) -> Int -> ReadS (ListT m a)
forall a. (String -> ReadS a) -> Int -> ReadS a
readsData ((String -> ReadS (ListT m a)) -> Int -> ReadS (ListT m a))
-> (String -> ReadS (ListT m a)) -> Int -> ReadS (ListT m a)
forall a b. (a -> b) -> a -> b
$
(Int -> ReadS (m [a]))
-> String -> (m [a] -> ListT m a) -> String -> ReadS (ListT m a)
forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith ((Int -> ReadS [a]) -> ReadS [[a]] -> Int -> ReadS (m [a])
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS [a]
rp' ReadS [[a]]
rl') String
"ListT" m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT
where
rp' :: Int -> ReadS [a]
rp' = (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS [a]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl
rl' :: ReadS [[a]]
rl' = (Int -> ReadS a) -> ReadS [a] -> ReadS [[a]]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Int -> ReadS a
rp ReadS [a]
rl
instance (Show1 m) => Show1 (ListT m) where
liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> ListT m a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (ListT m [a]
m) =
(Int -> m [a] -> ShowS) -> String -> Int -> m [a] -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith ((Int -> [a] -> ShowS) -> ([[a]] -> ShowS) -> Int -> m [a] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> [a] -> ShowS
sp' [[a]] -> ShowS
sl') String
"ListT" Int
d m [a]
m
where
sp' :: Int -> [a] -> ShowS
sp' = (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> [a] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl
sl' :: [[a]] -> ShowS
sl' = (Int -> a -> ShowS) -> ([a] -> ShowS) -> [[a]] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp [a] -> ShowS
sl
instance (Eq1 m, Eq a) => Eq (ListT m a) where == :: ListT m a -> ListT m a -> Bool
(==) = ListT m a -> ListT m a -> Bool
forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
eq1
instance (Ord1 m, Ord a) => Ord (ListT m a) where compare :: ListT m a -> ListT m a -> Ordering
compare = ListT m a -> ListT m a -> Ordering
forall (f :: * -> *) a. (Ord1 f, Ord a) => f a -> f a -> Ordering
compare1
instance (Read1 m, Read a) => Read (ListT m a) where readsPrec :: Int -> ReadS (ListT m a)
readsPrec = Int -> ReadS (ListT m a)
forall (f :: * -> *) a. (Read1 f, Read a) => Int -> ReadS (f a)
readsPrec1
instance (Show1 m, Show a) => Show (ListT m a) where showsPrec :: Int -> ListT m a -> ShowS
showsPrec = Int -> ListT m a -> ShowS
forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1
mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT m [a] -> n [b]
f ListT m a
m = n [b] -> ListT n b
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (n [b] -> ListT n b) -> n [b] -> ListT n b
forall a b. (a -> b) -> a -> b
$ m [a] -> n [b]
f (ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m)
{-# INLINE mapListT #-}
instance (Functor m) => Functor (ListT m) where
fmap :: (a -> b) -> ListT m a -> ListT m b
fmap a -> b
f = (m [a] -> m [b]) -> ListT m a -> ListT m b
forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT ((m [a] -> m [b]) -> ListT m a -> ListT m b)
-> (m [a] -> m [b]) -> ListT m a -> ListT m b
forall a b. (a -> b) -> a -> b
$ ([a] -> [b]) -> m [a] -> m [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([a] -> [b]) -> m [a] -> m [b]) -> ([a] -> [b]) -> m [a] -> m [b]
forall a b. (a -> b) -> a -> b
$ (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f
{-# INLINE fmap #-}
instance (Foldable f) => Foldable (ListT f) where
foldMap :: (a -> m) -> ListT f a -> m
foldMap a -> m
f (ListT f [a]
a) = ([a] -> m) -> f [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) f [a]
a
{-# INLINE foldMap #-}
instance (Traversable f) => Traversable (ListT f) where
traverse :: (a -> f b) -> ListT f a -> f (ListT f b)
traverse a -> f b
f (ListT f [a]
a) = f [b] -> ListT f b
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (f [b] -> ListT f b) -> f (f [b]) -> f (ListT f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([a] -> f [b]) -> f [a] -> f (f [b])
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((a -> f b) -> [a] -> f [b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) f [a]
a
{-# INLINE traverse #-}
instance (Applicative m) => Applicative (ListT m) where
pure :: a -> ListT m a
pure a
a = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [a
a]
{-# INLINE pure #-}
ListT m (a -> b)
f <*> :: ListT m (a -> b) -> ListT m a -> ListT m b
<*> ListT m a
v = m [b] -> ListT m b
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [b] -> ListT m b) -> m [b] -> ListT m b
forall a b. (a -> b) -> a -> b
$ [a -> b] -> [a] -> [b]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) ([a -> b] -> [a] -> [b]) -> m [a -> b] -> m ([a] -> [b])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ListT m (a -> b) -> m [a -> b]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m (a -> b)
f m ([a] -> [b]) -> m [a] -> m [b]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
v
{-# INLINE (<*>) #-}
instance (Applicative m) => Alternative (ListT m) where
empty :: ListT m a
empty = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
{-# INLINE empty #-}
ListT m a
m <|> :: ListT m a -> ListT m a -> ListT m a
<|> ListT m a
n = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
(++) ([a] -> [a] -> [a]) -> m [a] -> m ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m m ([a] -> [a]) -> m [a] -> m [a]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
n
{-# INLINE (<|>) #-}
instance (Monad m) => Monad (ListT m) where
#if !(MIN_VERSION_base(4,8,0))
return a = ListT $ return [a]
{-# INLINE return #-}
#endif
ListT m a
m >>= :: ListT m a -> (a -> ListT m b) -> ListT m b
>>= a -> ListT m b
k = m [b] -> ListT m b
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [b] -> ListT m b) -> m [b] -> ListT m b
forall a b. (a -> b) -> a -> b
$ do
[a]
a <- ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m
[[b]]
b <- (a -> m [b]) -> [a] -> m [[b]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (ListT m b -> m [b]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT (ListT m b -> m [b]) -> (a -> ListT m b) -> a -> m [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ListT m b
k) [a]
a
[b] -> m [b]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[b]] -> [b]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[b]]
b)
{-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
fail _ = ListT $ return []
{-# INLINE fail #-}
#endif
#if MIN_VERSION_base(4,9,0)
instance (Monad m) => Fail.MonadFail (ListT m) where
fail :: String -> ListT m a
fail String
_ = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
{-# INLINE fail #-}
#endif
instance (Monad m) => MonadPlus (ListT m) where
mzero :: ListT m a
mzero = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
{-# INLINE mzero #-}
ListT m a
m mplus :: ListT m a -> ListT m a -> ListT m a
`mplus` ListT m a
n = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ do
[a]
a <- ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m
[a]
b <- ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
n
[a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
b)
{-# INLINE mplus #-}
instance (MonadFix m) => MonadFix (ListT m) where
mfix :: (a -> ListT m a) -> ListT m a
mfix a -> ListT m a
f = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ ([a] -> m [a]) -> m [a]
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT (ListT m a -> m [a]) -> ([a] -> ListT m a) -> [a] -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ListT m a
f (a -> ListT m a) -> ([a] -> a) -> [a] -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> a
forall a. [a] -> a
head) m [a] -> ([a] -> m [a]) -> m [a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ [a]
xs -> case [a]
xs of
[] -> [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
a
x:[a]
_ -> ([a] -> [a]) -> m [a] -> m [a]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ((a -> ListT m a) -> ListT m a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix ((m [a] -> m [a]) -> ListT m a -> ListT m a
forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT (([a] -> [a]) -> m [a] -> m [a]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [a] -> [a]
forall a. [a] -> [a]
tail) (ListT m a -> ListT m a) -> (a -> ListT m a) -> a -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ListT m a
f)))
{-# INLINE mfix #-}
instance MonadTrans ListT where
lift :: m a -> ListT m a
lift m a
m = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ do
a
a <- m a
m
[a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [a
a]
{-# INLINE lift #-}
instance (MonadIO m) => MonadIO (ListT m) where
liftIO :: IO a -> ListT m a
liftIO = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> (IO a -> m a) -> IO a -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE liftIO #-}
#if MIN_VERSION_base(4,4,0)
instance (MonadZip m) => MonadZip (ListT m) where
mzipWith :: (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
mzipWith a -> b -> c
f (ListT m [a]
a) (ListT m [b]
b) = m [c] -> ListT m c
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [c] -> ListT m c) -> m [c] -> ListT m c
forall a b. (a -> b) -> a -> b
$ ([a] -> [b] -> [c]) -> m [a] -> m [b] -> m [c]
forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> m a -> m b -> m c
mzipWith ((a -> b -> c) -> [a] -> [b] -> [c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> b -> c
f) m [a]
a m [b]
b
{-# INLINE mzipWith #-}
#endif
#if MIN_VERSION_base(4,12,0)
instance Contravariant m => Contravariant (ListT m) where
contramap :: (a -> b) -> ListT m b -> ListT m a
contramap a -> b
f = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a)
-> (ListT m b -> m [a]) -> ListT m b -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> [b]) -> m [b] -> m [a]
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap ((a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m [b] -> m [a]) -> (ListT m b -> m [b]) -> ListT m b -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListT m b -> m [b]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT
{-# INLINE contramap #-}
#endif
liftCallCC :: CallCC m [a] [b] -> CallCC (ListT m) a b
liftCallCC :: CallCC m [a] [b] -> CallCC (ListT m) a b
liftCallCC CallCC m [a] [b]
callCC (a -> ListT m b) -> ListT m a
f = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$
CallCC m [a] [b]
callCC CallCC m [a] [b] -> CallCC m [a] [b]
forall a b. (a -> b) -> a -> b
$ \ [a] -> m [b]
c ->
ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ((a -> ListT m b) -> ListT m a
f (\ a
a -> m [b] -> ListT m b
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [b] -> ListT m b) -> m [b] -> ListT m b
forall a b. (a -> b) -> a -> b
$ [a] -> m [b]
c [a
a]))
{-# INLINE liftCallCC #-}
liftCatch :: Catch e m [a] -> Catch e (ListT m) a
liftCatch :: Catch e m [a] -> Catch e (ListT m) a
liftCatch Catch e m [a]
catchE ListT m a
m e -> ListT m a
h = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m
Catch e m [a]
`catchE` \ e
e -> ListT m a -> m [a]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT (e -> ListT m a
h e
e)
{-# INLINE liftCatch #-}