{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Monad.Trans.List
-- Copyright   :  (c) Andy Gill 2001,
--                (c) Oregon Graduate Institute of Science and Technology, 2001
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  R.Paterson@city.ac.uk
-- Stability   :  experimental
-- Portability :  portable
--
-- The ListT monad transformer, adding backtracking to a given monad,
-- which must be commutative.
-----------------------------------------------------------------------------

module Control.Monad.Trans.List
  {-# DEPRECATED "This transformer is invalid on most monads" #-} (
    -- * The ListT monad transformer
    ListT(..),
    mapListT,
    -- * Lifting other operations
    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))

-- | Parameterizable list monad, with an inner monad.
--
-- /Note:/ this does not yield a monad unless the argument monad is commutative.
newtype ListT m a = ListT { forall (m :: * -> *) a. ListT m a -> m [a]
runListT :: m [a] }

instance (Eq1 m) => Eq1 (ListT m) where
    liftEq :: forall a b. (a -> b -> Bool) -> ListT m a -> ListT m b -> Bool
liftEq a -> b -> Bool
eq (ListT m [a]
x) (ListT m [b]
y) = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq (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 :: forall a b.
(a -> b -> Ordering) -> ListT m a -> ListT m b -> Ordering
liftCompare a -> b -> Ordering
comp (ListT m [a]
x) (ListT m [b]
y) = forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare (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 :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (ListT m a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
        forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS [a]
rp' ReadS [[a]]
rl') String
"ListT" forall (m :: * -> *) a. m [a] -> ListT m a
ListT
      where
        rp' :: Int -> ReadS [a]
rp' = 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' = 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 :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> ListT m a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (ListT m [a]
m) =
        forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (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' = 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' = 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
(==) = 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 = 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 = 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 = forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1

-- | Map between 'ListT' computations.
--
-- * @'runListT' ('mapListT' f m) = f ('runListT' m)@
mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT :: forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT m [a] -> n [b]
f ListT m a
m = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ m [a] -> n [b]
f (forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m)
{-# INLINE mapListT #-}

instance (Functor m) => Functor (ListT m) where
    fmap :: forall a b. (a -> b) -> ListT m a -> ListT m b
fmap a -> b
f = forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall 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 :: forall m a. Monoid m => (a -> m) -> ListT f a -> m
foldMap a -> m
f (ListT f [a]
a) = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (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 :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListT f a -> f (ListT f b)
traverse a -> f b
f (ListT f [a]
a) = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (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 :: forall a. a -> ListT m a
pure a
a  = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure [a
a]
    {-# INLINE pure #-}
    ListT m (a -> b)
f <*> :: forall a b. ListT m (a -> b) -> ListT m a -> ListT m b
<*> ListT m a
v = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
v
    {-# INLINE (<*>) #-}

instance (Applicative m) => Alternative (ListT m) where
    empty :: forall a. ListT m a
empty   = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure []
    {-# INLINE empty #-}
    ListT m a
m <|> :: forall a. ListT m a -> ListT m a -> ListT m a
<|> ListT m a
n = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a] -> [a]
(++) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> 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 >>= :: forall a b. ListT m a -> (a -> ListT m b) -> ListT m b
>>= a -> ListT m b
k  = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ do
        [a]
a <- forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m
        [[b]]
b <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *) a. ListT m a -> m [a]
runListT forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ListT m b
k) [a]
a
        forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall a. String -> ListT m a
fail String
_ = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return []
    {-# INLINE fail #-}
#endif

instance (Monad m) => MonadPlus (ListT m) where
    mzero :: forall a. ListT m a
mzero       = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return []
    {-# INLINE mzero #-}
    ListT m a
m mplus :: forall a. ListT m a -> ListT m a -> ListT m a
`mplus` ListT m a
n = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ do
        [a]
a <- forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m
        [a]
b <- forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
n
        forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
a forall a. [a] -> [a] -> [a]
++ [a]
b)
    {-# INLINE mplus #-}

instance (MonadFix m) => MonadFix (ListT m) where
    mfix :: forall a. (a -> ListT m a) -> ListT m a
mfix a -> ListT m a
f = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall (m :: * -> *) a. ListT m a -> m [a]
runListT forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ListT m a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
head) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ [a]
xs -> case [a]
xs of
        [] -> forall (m :: * -> *) a. Monad m => a -> m a
return []
        a
x:[a]
_ -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (a
xforall a. a -> [a] -> [a]
:) (forall (m :: * -> *) a. ListT m a -> m [a]
runListT (forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. [a] -> [a]
tail) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ListT m a
f)))
    {-# INLINE mfix #-}

instance MonadTrans ListT where
    lift :: forall (m :: * -> *) a. Monad m => m a -> ListT m a
lift m a
m = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ do
        a
a <- m a
m
        forall (m :: * -> *) a. Monad m => a -> m a
return [a
a]
    {-# INLINE lift #-}

instance (MonadIO m) => MonadIO (ListT m) where
    liftIO :: forall a. IO a -> ListT m a
liftIO = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a b c. (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) = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> m a -> m b -> m c
mzipWith (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 :: forall a' a. (a' -> a) -> ListT m a -> ListT m a'
contramap a' -> a
f = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a' -> a
f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. ListT m a -> m [a]
runListT
    {-# INLINE contramap #-}
#endif

-- | Lift a @callCC@ operation to the new monad.
liftCallCC :: CallCC m [a] [b] -> CallCC (ListT m) a b
liftCallCC :: forall (m :: * -> *) a b. CallCC m [a] [b] -> CallCC (ListT m) a b
liftCallCC CallCC m [a] [b]
callCC (a -> ListT m b) -> ListT m a
f = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$
    CallCC m [a] [b]
callCC forall a b. (a -> b) -> a -> b
$ \ [a] -> m [b]
c ->
    forall (m :: * -> *) a. ListT m a -> m [a]
runListT ((a -> ListT m b) -> ListT m a
f (\ a
a -> forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ [a] -> m [b]
c [a
a]))
{-# INLINE liftCallCC #-}

-- | Lift a @catchE@ operation to the new monad.
liftCatch :: Catch e m [a] -> Catch e (ListT m) a
liftCatch :: forall e (m :: * -> *) a. Catch e m [a] -> Catch e (ListT m) a
liftCatch Catch e m [a]
catchE ListT m a
m e -> ListT m a
h = forall (m :: * -> *) a. m [a] -> ListT m a
ListT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. ListT m a -> m [a]
runListT ListT m a
m
    Catch e m [a]
`catchE` \ e
e -> forall (m :: * -> *) a. ListT m a -> m [a]
runListT (e -> ListT m a
h e
e)
{-# INLINE liftCatch #-}