{-# LANGUAGE BangPatterns               #-}
{-# LANGUAGE CPP                        #-}
{-# LANGUAGE DeriveFoldable             #-}
{-# LANGUAGE DeriveTraversable          #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE UndecidableInstances       #-}

-- | The `ListT` type is like a list that lets you interleave effects between
--   each element of the list.
module List.Transformer
    (
      -- * Introduction
      -- $intro

      -- ** Example: stdin, stdout
      -- $standardStreams

      -- ** Core operations
      -- $core

      -- ** Monadic combination
      -- $monad

      -- ** Exercise: Interaction
      -- $interaction

      -- * ListT
      ListT(..)

      -- ** Consuming
      -- $pleaseStream
    , runListT
    , fold
    , foldM

      -- ** Constructing
      -- $constructing
    , select
    , unfold

      -- ** Removing elements
    , take
    , drop
    , dropWhile
    , takeWhile
      -- $filter

      -- ** Concatenation
      -- $concatenation

      -- ** Pairwise combination
      -- $pairwise
    , zip

      -- ** Repetition
      -- $repetition

      -- * Step
    , Step(..)

      -- * Alternative instances
    , ZipListT(..)

      -- * Re-exports
    , MonadTrans(..)
    , MonadIO(..)
    , Alternative(..)
    , MFunctor (..)
    ) where

#if MIN_VERSION_base(4,8,0)
import Control.Applicative (Alternative(..), liftA2)
#else
import Control.Applicative (Applicative(..), Alternative(..), liftA2)
import Data.Foldable (Foldable)
import Data.Functor ((<$))
import Data.Monoid (Monoid(..))
import Data.Traversable (Traversable)
#endif
import Control.Monad (MonadPlus(..))
import Control.Monad.Error.Class (MonadError(..))
#if MIN_VERSION_base(4,9,0) && !(MIN_VERSION_base(4,13,0))
import Control.Monad.Fail (MonadFail(..))
#endif
import Control.Monad.Morph (MFunctor (..))
import Control.Monad.State.Class (MonadState(..))
import Control.Monad.Reader.Class (MonadReader(..))
import Control.Monad.Trans (MonadTrans(..), MonadIO(..))
import Data.Semigroup (Semigroup(..))
import Prelude hiding (drop, dropWhile, pred, take, takeWhile, zip)

import qualified Data.Foldable

-- $setup
-- >>> :set -XNoMonomorphismRestriction

{- $intro

The type's definition is very short:

@newtype 'ListT' m a = ListT { next :: m ('Step' m a) }@

    Every `ListT` begins with an outermost effect (the @\'m\'@, commonly 'IO'). The return value of that effect is either:

@data 'Step' m a = Cons a ('ListT' m a) | Nil@

    * Cons: a new list element followed by the rest of the list
    * Nil : an empty list

-}

{- $standardStreams

    You most commonly use the ListT when you wish to generate each element of
    the list using `IO`.  For example, you can read lines from standard input:

> import List.Transformer
>
> import qualified System.IO
>
> stdin :: ListT IO String
> stdin = ListT (do
>     eof <- System.IO.isEOF
>     if eof
>         then return Nil
>         else do
>             string <- getLine
>             return (Cons string stdin) )

    You can also loop over a `ListT` to consume elements one-at-a-time.  You
    \"pay as you go\" for effects, only running what you actually need:

> stdout :: ListT IO String -> IO ()
> stdout strings = do
>     s <- next strings
>     case s of
>         Nil                  -> return ()
>         Cons string strings' -> do
>             putStrLn string
>             stdout strings'

    Combining @stdin@ and @stdout@ forwards lines one-by-one from standard input
    to standard output:

> main :: IO ()
> main = stdout stdin

    These lines stream in constant space, never retaining more than one line in
    memory:

> $ runghc aboveExample.hs
> Test<Enter>
> Test
> 123<Enter>
> 123
> ABC<Enter>
> ABC
> <Ctrl-D>
> $

-}

{- $core

    The most important operations that you should familiarize yourself with are:

    * `empty`, which gives you an empty `ListT` with 0 elements

> empty :: ListT IO a

    * `pure` / `return`, which both convert a value into a one-element `ListT`

> pure, return :: a -> ListT IO a

    * `liftIO`, which converts an `IO` action into a one-element `ListT`

> liftIO :: IO a -> ListT IO a

    * (`<|>`), which concatenates two `ListT`s

> (<|>) :: ListT IO a -> ListT IO a -> ListT IO a

    * (`>>=`), which powers @do@ notation and @MonadComprehensions@

> (>>=) :: ListT IO a -> (a -> ListT IO b) -> ListT IO b

    * `select`, which converts a plain list into a `ListT`

> select :: [a] -> ListT IO a

-}

{- $monad

    Sometimes we can simplify the code by taking advantage of the fact that the
    `Monad` instance for `ListT` behaves like a list comprehension:

> stdout :: ListT IO String -> IO ()
> stdout strings = runListT (do
>     string <- strings
>     liftIO (putStrLn string) )

    You can read the above code as saying: \"for each @string@ in @strings@,
    call `putStrLn` on @string@."

    You can even use list comprehension syntax if you enable the
    @MonadComprehensions@ language extension:

> stdout strings = runListT [ r | str <- strings, r <- liftIO (putStrLn str) ]

    There are a few ways we could consider defining a `ListT` analogue to the `mapM`
    function from `Prelude`, but none are given in this library because they need
    require only (`>>=`) and some trivial lifting.

> mapM                                :: (a -> IO b)       -> [a]        -> IO [b]
> ( \f xs -> xs        >>=        f ) :: (a -> ListT IO b) -> ListT IO a -> ListT IO b
> ( \f xs -> select xs >>= lift . f ) :: (a -> IO b)       -> [a]        -> ListT IO b
> ( \f xs -> xs        >>= lift . f ) :: (a -> IO b)       -> ListT IO a -> ListT IO b

    A critical difference between `mapM` and `ListT`'s monad is that `ListT` will
    stream in constant space, whereas `mapM` buffers the entire output list before
    returning a single element.

-}

{- $interaction

    To test your understanding, guess what this code does and then test your
    guess by running the code:

@
import List.Transformer ('ListT', 'runListT', 'liftIO', ('<|>'), 'select')
import Data.Foldable ('Data.Foldable.asum')
import Data.List ('Data.List.repeat')

strings :: 'ListT' IO String
strings = do
    'select' ('Data.List.repeat' ())
    'Data.Foldable.asum'
        [ pure ""
        , pure "Say something:"
        , do
            x <- 'liftIO' getLine
            return ("You said: " '<|>' x)
        ]

main :: IO ()
main = 'runListT' (do
    string \<- pure "Hello, there!" '<|>' strings
    'liftIO' (putStrLn string) )
@

-}

{-| This is like a list except that you can interleave effects between each list
    element.  For example:

> stdin :: ListT IO String
> stdin = ListT (do
>     eof <- System.IO.isEOF
>     if eof
>         then return Nil
>         else do
>             line <- getLine
>             return (Cons line stdin) )

    The mnemonic is \"List Transformer\" because this type takes a base `Monad`,
    @\'m\'@, and returns a new transformed `Monad` that adds support for
    list comprehensions
-}
newtype ListT m a = ListT { forall (m :: * -> *) a. ListT m a -> m (Step m a)
next :: m (Step m a) }
    deriving ((forall m. Monoid m => ListT m m -> m)
-> (forall m a. Monoid m => (a -> m) -> ListT m a -> m)
-> (forall m a. Monoid m => (a -> m) -> ListT m a -> m)
-> (forall a b. (a -> b -> b) -> b -> ListT m a -> b)
-> (forall a b. (a -> b -> b) -> b -> ListT m a -> b)
-> (forall b a. (b -> a -> b) -> b -> ListT m a -> b)
-> (forall b a. (b -> a -> b) -> b -> ListT m a -> b)
-> (forall a. (a -> a -> a) -> ListT m a -> a)
-> (forall a. (a -> a -> a) -> ListT m a -> a)
-> (forall a. ListT m a -> [a])
-> (forall a. ListT m a -> Bool)
-> (forall a. ListT m a -> Int)
-> (forall a. Eq a => a -> ListT m a -> Bool)
-> (forall a. Ord a => ListT m a -> a)
-> (forall a. Ord a => ListT m a -> a)
-> (forall a. Num a => ListT m a -> a)
-> (forall a. Num a => ListT m a -> a)
-> Foldable (ListT m)
forall a. Eq a => a -> ListT m a -> Bool
forall a. Num a => ListT m a -> a
forall a. Ord a => ListT m a -> a
forall m. Monoid m => ListT m m -> m
forall a. ListT m a -> Bool
forall a. ListT m a -> Int
forall a. ListT m a -> [a]
forall a. (a -> a -> a) -> ListT m a -> a
forall m a. Monoid m => (a -> m) -> ListT m a -> m
forall b a. (b -> a -> b) -> b -> ListT m a -> b
forall a b. (a -> b -> b) -> b -> ListT m a -> b
forall (m :: * -> *) a.
(Foldable m, Eq a) =>
a -> ListT m a -> Bool
forall (m :: * -> *) a. (Foldable m, Num a) => ListT m a -> a
forall (m :: * -> *) a. (Foldable m, Ord a) => ListT m a -> a
forall (m :: * -> *) m. (Foldable m, Monoid m) => ListT m m -> m
forall (m :: * -> *) a. Foldable m => ListT m a -> Bool
forall (m :: * -> *) a. Foldable m => ListT m a -> Int
forall (m :: * -> *) a. Foldable m => ListT m a -> [a]
forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> ListT m a -> a
forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ListT m a -> m
forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ListT m a -> b
forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ListT m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall (m :: * -> *) m. (Foldable m, Monoid m) => ListT m m -> m
fold :: forall m. Monoid m => ListT m m -> m
$cfoldMap :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ListT m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ListT m a -> m
$cfoldMap' :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ListT m a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ListT m a -> m
$cfoldr :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ListT m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ListT m a -> b
$cfoldr' :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ListT m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ListT m a -> b
$cfoldl :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ListT m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ListT m a -> b
$cfoldl' :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ListT m a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ListT m a -> b
$cfoldr1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> ListT m a -> a
foldr1 :: forall a. (a -> a -> a) -> ListT m a -> a
$cfoldl1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> ListT m a -> a
foldl1 :: forall a. (a -> a -> a) -> ListT m a -> a
$ctoList :: forall (m :: * -> *) a. Foldable m => ListT m a -> [a]
toList :: forall a. ListT m a -> [a]
$cnull :: forall (m :: * -> *) a. Foldable m => ListT m a -> Bool
null :: forall a. ListT m a -> Bool
$clength :: forall (m :: * -> *) a. Foldable m => ListT m a -> Int
length :: forall a. ListT m a -> Int
$celem :: forall (m :: * -> *) a.
(Foldable m, Eq a) =>
a -> ListT m a -> Bool
elem :: forall a. Eq a => a -> ListT m a -> Bool
$cmaximum :: forall (m :: * -> *) a. (Foldable m, Ord a) => ListT m a -> a
maximum :: forall a. Ord a => ListT m a -> a
$cminimum :: forall (m :: * -> *) a. (Foldable m, Ord a) => ListT m a -> a
minimum :: forall a. Ord a => ListT m a -> a
$csum :: forall (m :: * -> *) a. (Foldable m, Num a) => ListT m a -> a
sum :: forall a. Num a => ListT m a -> a
$cproduct :: forall (m :: * -> *) a. (Foldable m, Num a) => ListT m a -> a
product :: forall a. Num a => ListT m a -> a
Foldable, Functor (ListT m)
Foldable (ListT m)
(Functor (ListT m), Foldable (ListT m)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> ListT m a -> f (ListT m b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    ListT m (f a) -> f (ListT m a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> ListT m a -> m (ListT m b))
-> (forall (m :: * -> *) a.
    Monad m =>
    ListT m (m a) -> m (ListT m a))
-> Traversable (ListT m)
forall (m :: * -> *). (Monad m, Traversable m) => Functor (ListT m)
forall (m :: * -> *).
(Monad m, Traversable m) =>
Foldable (ListT m)
forall (m :: * -> *) (m :: * -> *) a.
(Monad m, Traversable m, Monad m) =>
ListT m (m a) -> m (ListT m a)
forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Traversable m, Applicative f) =>
ListT m (f a) -> f (ListT m a)
forall (m :: * -> *) (m :: * -> *) a b.
(Monad m, Traversable m, Monad m) =>
(a -> m b) -> ListT m a -> m (ListT m b)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable m, Applicative f) =>
(a -> f b) -> ListT m a -> f (ListT m b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => ListT m (m a) -> m (ListT m a)
forall (f :: * -> *) a.
Applicative f =>
ListT m (f a) -> f (ListT m a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ListT m a -> m (ListT m b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListT m a -> f (ListT m b)
$ctraverse :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable m, Applicative f) =>
(a -> f b) -> ListT m a -> f (ListT m b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListT m a -> f (ListT m b)
$csequenceA :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Traversable m, Applicative f) =>
ListT m (f a) -> f (ListT m a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ListT m (f a) -> f (ListT m a)
$cmapM :: forall (m :: * -> *) (m :: * -> *) a b.
(Monad m, Traversable m, Monad m) =>
(a -> m b) -> ListT m a -> m (ListT m b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ListT m a -> m (ListT m b)
$csequence :: forall (m :: * -> *) (m :: * -> *) a.
(Monad m, Traversable m, Monad m) =>
ListT m (m a) -> m (ListT m a)
sequence :: forall (m :: * -> *) a. Monad m => ListT m (m a) -> m (ListT m a)
Traversable)

instance MonadTrans ListT where
    lift :: forall (m :: * -> *) a. Monad m => m a -> ListT m a
lift m a
m = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        a
x <- m a
m
        Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x ListT m a
forall a. ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty) )

instance Monad m => Functor (ListT m) where
    fmap :: forall a b. (a -> b) -> ListT m a -> ListT m b
fmap a -> b
k (ListT m (Step m a)
m) = m (Step m b) -> ListT m b
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- m (Step m a)
m
        Step m b -> m (Step m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((a -> b) -> Step m a -> Step m b
forall a b. (a -> b) -> Step m a -> Step m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
k Step m a
s) )

instance Monad m => Applicative (ListT m) where
    pure :: forall a. a -> ListT m a
pure a
x = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x ListT m a
forall a. ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty))

    ListT m (Step m (a -> b))
m <*> :: forall a b. ListT m (a -> b) -> ListT m a -> ListT m b
<*> ListT m a
l = m (Step m b) -> ListT m b
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m (a -> b)
s <- m (Step m (a -> b))
m
        case Step m (a -> b)
s of
            Step m (a -> b)
Nil       -> Step m b -> m (Step m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m b
forall (m :: * -> *) a. Step m a
Nil
            Cons a -> b
f ListT m (a -> b)
l' -> ListT m b -> m (Step m b)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ((a -> b) -> ListT m a -> ListT m b
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ListT m a
l ListT m b -> ListT m b -> ListT m b
forall a. ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ListT m (a -> b)
l' ListT m (a -> b) -> ListT m a -> ListT m b
forall a b. ListT m (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ListT m a
l)) )

    ListT m (Step m a)
m *> :: forall a b. ListT m a -> ListT m b -> ListT m b
*> ListT m b
l = m (Step m b) -> ListT m b
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- m (Step m a)
m
        case Step m a
s of
            Step m a
Nil       -> Step m b -> m (Step m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m b
forall (m :: * -> *) a. Step m a
Nil
            Cons a
_ ListT m a
l' -> ListT m b -> m (Step m b)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next (ListT m b
l ListT m b -> ListT m b -> ListT m b
forall a. ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ListT m a
l' ListT m a -> ListT m b -> ListT m b
forall a b. ListT m a -> ListT m b -> ListT m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ListT m b
l)) )

    ListT m (Step m a)
m <* :: forall a b. ListT m a -> ListT m b -> ListT m a
<* ListT m b
l = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- m (Step m a)
m
        case Step m a
s of
            Step m a
Nil       -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil
            Cons a
x ListT m a
l' -> ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ((a
x a -> ListT m b -> ListT m a
forall a b. a -> ListT m b -> ListT m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ListT m b
l) ListT m a -> ListT m a -> ListT m a
forall a. ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ListT m a
l' ListT m a -> ListT m b -> ListT m a
forall a b. ListT m a -> ListT m b -> ListT m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ListT m b
l)) )

instance Monad m => Monad (ListT m) where
    return :: forall a. a -> ListT m a
return = a -> ListT m a
forall a. a -> ListT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

    ListT m (Step m a)
m >>= :: forall a b. ListT m a -> (a -> ListT m b) -> ListT m b
>>= a -> ListT m b
k = m (Step m b) -> ListT m b
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- m (Step m a)
m
        case Step m a
s of
            Step m a
Nil       -> Step m b -> m (Step m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m b
forall (m :: * -> *) a. Step m a
Nil
            Cons a
x ListT m a
l' -> ListT m b -> m (Step m b)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next (a -> ListT m b
k a
x ListT m b -> ListT m b -> ListT m b
forall a. ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ListT m a
l' ListT m a -> (a -> ListT m b) -> ListT m b
forall a b. ListT m a -> (a -> ListT m b) -> ListT m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ListT m b
k)) )

#if !(MIN_VERSION_base(4,13,0))
    fail _ = mzero
#endif

instance Monad m => Alternative (ListT m) where
    empty :: forall a. ListT m a
empty = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil)

    ListT m (Step m a)
m <|> :: forall a. ListT m a -> ListT m a -> ListT m a
<|> ListT m a
l = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- m (Step m a)
m
        case Step m a
s of
            Step m a
Nil       -> ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m a
l
            Cons a
x ListT m a
l' -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x (ListT m a
l' ListT m a -> ListT m a -> ListT m a
forall a. ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ListT m a
l)) )

instance Monad m => MonadPlus (ListT m) where
    mzero :: forall a. ListT m a
mzero = ListT m a
forall a. ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty

    mplus :: forall a. ListT m a -> ListT m a -> ListT m a
mplus = ListT m a -> ListT m a -> ListT m a
forall a. ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)

#if MIN_VERSION_base(4,9,0)
instance Monad m => MonadFail (ListT m) where
    fail :: forall a. String -> ListT m a
fail String
_ = ListT m a
forall a. ListT m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
#endif

instance (Monad m, Data.Semigroup.Semigroup a) => Data.Semigroup.Semigroup (ListT m a) where
    <> :: ListT m a -> ListT m a -> ListT m a
(<>) = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)

instance (Monad m, Data.Semigroup.Semigroup a, Monoid a) => Monoid (ListT m a) where
    mempty :: ListT m a
mempty  = a -> ListT m a
forall a. a -> ListT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty

#if !(MIN_VERSION_base(4,11,0))
    mappend = (<>)
#endif

instance MonadIO m => MonadIO (ListT m) where
    liftIO :: forall a. IO a -> ListT m a
liftIO IO a
m = m a -> ListT m a
forall (m :: * -> *) a. Monad m => m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
m)

instance MonadError e m => MonadError e (ListT m) where
    throwError :: forall a. e -> ListT m a
throwError e
e = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (e -> m (Step m a)
forall a. e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError e
e)

    catchError :: forall a. ListT m a -> (e -> ListT m a) -> ListT m a
catchError (ListT m (Step m a)
m) e -> ListT m a
k = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (m (Step m a) -> (e -> m (Step m a)) -> m (Step m a)
forall a. m a -> (e -> m a) -> m a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError m (Step m a)
m (ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next (ListT m a -> m (Step m a))
-> (e -> ListT m a) -> e -> m (Step m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ListT m a
k))

instance MonadReader i m => MonadReader i (ListT m) where
    ask :: ListT m i
ask = m i -> ListT m i
forall (m :: * -> *) a. Monad m => m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m i
forall r (m :: * -> *). MonadReader r m => m r
ask

    local :: forall a. (i -> i) -> ListT m a -> ListT m a
local i -> i
k (ListT m (Step m a)
m) = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- (i -> i) -> m (Step m a) -> m (Step m a)
forall a. (i -> i) -> m a -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local i -> i
k m (Step m a)
m
        case Step m a
s of
            Step m a
Nil      -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil
            Cons a
x ListT m a
l -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x ((i -> i) -> ListT m a -> ListT m a
forall a. (i -> i) -> ListT m a -> ListT m a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local i -> i
k ListT m a
l)) )

    reader :: forall a. (i -> a) -> ListT m a
reader i -> a
k = m a -> ListT m a
forall (m :: * -> *) a. Monad m => m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((i -> a) -> m a
forall a. (i -> a) -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader i -> a
k)

instance MonadState s m => MonadState s (ListT m) where
    get :: ListT m s
get = m s -> ListT m s
forall (m :: * -> *) a. Monad m => m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m s
forall s (m :: * -> *). MonadState s m => m s
get

    put :: s -> ListT m ()
put s
x = m () -> ListT m ()
forall (m :: * -> *) a. Monad m => m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put s
x)

    state :: forall a. (s -> (a, s)) -> ListT m a
state s -> (a, s)
k = m a -> ListT m a
forall (m :: * -> *) a. Monad m => m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((s -> (a, s)) -> m a
forall a. (s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state s -> (a, s)
k)

instance MFunctor ListT where
#if MIN_VERSION_base(4,8,0)
    hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> ListT m b -> ListT n b
hoist forall a. m a -> n a
f ListT m b
xs = n (Step n b) -> ListT n b
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (m (Step n b) -> n (Step n b)
forall a. m a -> n a
f ((Step m b -> Step n b) -> m (Step m b) -> m (Step n b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((forall a. m a -> n a) -> Step m b -> Step n b
forall {k} (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
       (b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> Step m b -> Step n b
hoist m a -> n a
forall a. m a -> n a
f) (ListT m b -> m (Step m b)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m b
xs)))
#else
    hoist f xs = ListT (f (next xs >>= \x -> return (hoist f x)))
#endif

instance (Monad m, Num a) => Num (ListT m a) where
    fromInteger :: Integer -> ListT m a
fromInteger Integer
n = a -> ListT m a
forall a. a -> ListT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
n)

    negate :: ListT m a -> ListT m a
negate = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
negate
    abs :: ListT m a -> ListT m a
abs    = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
    signum :: ListT m a -> ListT m a
signum = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum

    + :: ListT m a -> ListT m a -> ListT m a
(+) = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
    * :: ListT m a -> ListT m a -> ListT m a
(*) = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(*)
    (-) = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)

instance (Monad m, Fractional a) => Fractional (ListT m a) where
    fromRational :: Rational -> ListT m a
fromRational Rational
n = a -> ListT m a
forall a. a -> ListT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Rational -> a
forall a. Fractional a => Rational -> a
fromRational Rational
n)

    recip :: ListT m a -> ListT m a
recip = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Fractional a => a -> a
recip

    / :: ListT m a -> ListT m a -> ListT m a
(/) = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)

instance (Monad m, Floating a) => Floating (ListT m a) where
    pi :: ListT m a
pi = a -> ListT m a
forall a. a -> ListT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Floating a => a
pi

    exp :: ListT m a -> ListT m a
exp  = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
exp
    sqrt :: ListT m a -> ListT m a
sqrt = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sqrt
    log :: ListT m a -> ListT m a
log  = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
log
    sin :: ListT m a -> ListT m a
sin  = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sin
    tan :: ListT m a -> ListT m a
tan  = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
tan
    cos :: ListT m a -> ListT m a
cos  = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cos
    asin :: ListT m a -> ListT m a
asin = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asin
    atan :: ListT m a -> ListT m a
atan = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atan
    acos :: ListT m a -> ListT m a
acos = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acos
    sinh :: ListT m a -> ListT m a
sinh = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sinh
    tanh :: ListT m a -> ListT m a
tanh = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
tanh
    cosh :: ListT m a -> ListT m a
cosh = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cosh
    asinh :: ListT m a -> ListT m a
asinh = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asinh
    atanh :: ListT m a -> ListT m a
atanh = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atanh
    acosh :: ListT m a -> ListT m a
acosh = (a -> a) -> ListT m a -> ListT m a
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acosh

    ** :: ListT m a -> ListT m a -> ListT m a
(**)    = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Floating a => a -> a -> a
(**)
    logBase :: ListT m a -> ListT m a -> ListT m a
logBase = (a -> a -> a) -> ListT m a -> ListT m a -> ListT m a
forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Floating a => a -> a -> a
logBase

{-| Use this to drain a `ListT`, running it to completion and discarding all
    values.  For example:

> stdout :: ListT IO String -> IO ()
> stdout l = runListT (do
>     str <- l
>     liftIO (putStrLn str) )

    The most common specialized type for `runListT` will be:

> runListT :: ListT IO a -> IO ()
-}
runListT :: Monad m => ListT m a -> m ()
runListT :: forall (m :: * -> *) a. Monad m => ListT m a -> m ()
runListT (ListT m (Step m a)
m) = do
    Step m a
s <- m (Step m a)
m
    case Step m a
s of
        Step m a
Nil       -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Cons a
_ ListT m a
l' -> ListT m a -> m ()
forall (m :: * -> *) a. Monad m => ListT m a -> m ()
runListT ListT m a
l'

{-| Use this to fold a `ListT` into a single value.  This is designed to be
    used with the @foldl@ library:

> import Control.Foldl (purely)
> import List.Transformer (fold)
>
> purely fold :: Monad m => Fold a b -> ListT m a -> m b

    ... but you can also use the `fold` function directly:

> fold (+) 0 id :: Num a => ListT m a -> m a

>>> fold (<>) "" id (select ["a", "b", "c", "d", "e"])
"abcde"
-}
fold :: Monad m => (x -> a -> x) -> x -> (x -> b) -> ListT m a -> m b
fold :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> ListT m a -> m b
fold x -> a -> x
step x
begin x -> b
done ListT m a
l = x -> ListT m a -> m b
forall {m :: * -> *}. Monad m => x -> ListT m a -> m b
go x
begin ListT m a
l
  where
    go :: x -> ListT m a -> m b
go !x
x (ListT m (Step m a)
m) = do
        Step m a
s <- m (Step m a)
m
        case Step m a
s of
            Cons a
a ListT m a
l' -> x -> ListT m a -> m b
go (x -> a -> x
step x
x a
a) ListT m a
l'
            Step m a
Nil       -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> b
done x
x)

{-| Use this to fold a `ListT` into a single value.  This is designed to be
    used with the @foldl@ library:

> import Control.Foldl (impurely)
> import List.Transformer (fold)
>
> impurely fold :: Monad m => FoldM m a b -> ListT m a -> m b

    ... but you can also use the `foldM` function directly.
-}
foldM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> ListT m a -> m b
foldM :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> ListT m a -> m b
foldM x -> a -> m x
step m x
begin x -> m b
done ListT m a
l0 = do
    x
x0 <- m x
begin
    x -> ListT m a -> m b
go x
x0 ListT m a
l0
  where
    go :: x -> ListT m a -> m b
go !x
x (ListT m (Step m a)
m) = do
        Step m a
s <- m (Step m a)
m
        case Step m a
s of
            Cons a
a ListT m a
l' -> do
                x
x' <- x -> a -> m x
step x
x a
a
                x -> ListT m a -> m b
go x
x' ListT m a
l'
            Step m a
Nil       -> x -> m b
done x
x

{- $pleaseStream

    This library is designed to stream results in constant space and does not
    expose an obvious way to collect all the results into memory.  As a rule of
    thumb if you think you need to collect all the results in memory try to
    instead see if you can consume the results as they are being generated (such
    as in all the above examples).  If you can stream the data from start to
    finish then your code will use significantly less memory and your program
    will become more responsive.

-}

{- $constructing

    `empty` is the empty list with no effects.

    Use `pure`/`return` to construct a singleton list with no effects. Use `liftIO`
    to turn an effect into a singleton list whose sole element is the effect's result.

    Suppose you want to build a `ListT` with three elements and no effects.
    You could write:

> pure 1 <|> pure 2 <|> pure 3 :: ListT IO Int

    ... although you would probably prefer to use `select` instead:

> select [1, 2, 3] :: ListT IO Int

-}

{-| Convert any collection that implements `Foldable` to another collection that
    implements `Alternative`

    For this library, the most common specialized type for `select` will be:

> select :: [a] -> ListT IO a
-}
select :: (Foldable f, Alternative m) => f a -> m a
select :: forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f a -> m a
select = (a -> m a -> m a) -> m a -> f a -> m a
forall a b. (a -> b -> b) -> b -> f a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Data.Foldable.foldr a -> m a -> m a
forall {f :: * -> *} {a}. Alternative f => a -> f a -> f a
cons m a
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty
  where
    cons :: a -> f a -> f a
cons a
x f a
xs = a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x f a -> f a -> f a
forall a. f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f a
xs


-- | @take n xs@ takes @n@ elements from the head of @xs@.
--
-- >>> let list xs = do x <- select xs; liftIO (print (show x)); return x
-- >>> let sum = fold (+) 0 id
-- >>> sum (take 2 (list [5,4,3,2,1]))
-- "5"
-- "4"
-- 9
take :: Monad m => Int -> ListT m a -> ListT m a
take :: forall (m :: * -> *) a. Monad m => Int -> ListT m a -> ListT m a
take Int
n ListT m a
l
    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = ListT m a
forall a. ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty
    | Bool
otherwise = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m a
l
        case Step m a
s of
            Cons a
a ListT m a
l' -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
a (Int -> ListT m a -> ListT m a
forall (m :: * -> *) a. Monad m => Int -> ListT m a -> ListT m a
take (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ListT m a
l'))
            Step m a
Nil       -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil)

-- | @drop n xs@ drops @n@ elements from the head of @xs@, but still runs their
-- effects.
--
-- >>> let list xs = do x <- select xs; liftIO (print (show x)); return x
-- >>> let sum = fold (+) 0 id
-- >>> sum (drop 2 (list [5,4,3,2,1]))
-- "5"
-- "4"
-- "3"
-- "2"
-- "1"
-- 6
drop :: Monad m => Int -> ListT m a -> ListT m a
drop :: forall (m :: * -> *) a. Monad m => Int -> ListT m a -> ListT m a
drop Int
n ListT m a
l
    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = ListT m a
l
    | Bool
otherwise = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Step m a
s <- ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m a
l
        case Step m a
s of
            Cons a
_ ListT m a
l' -> ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next (Int -> ListT m a -> ListT m a
forall (m :: * -> *) a. Monad m => Int -> ListT m a -> ListT m a
drop (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ListT m a
l')
            Step m a
Nil       -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil)

-- | @dropWhile pred xs@ drops elements from the head of @xs@ if they
-- satisfy the predicate, but still runs their effects.
--
-- >>> let list xs = do x <- select xs; liftIO (print (show x)); return x
-- >>> let sum = fold (+) 0 id
-- >>> sum (dropWhile even (list [2,4,5,7,8]))
-- "2"
-- "4"
-- "5"
-- "7"
-- "8"
-- 20
dropWhile :: Monad m => (a -> Bool) -> ListT m a -> ListT m a
dropWhile :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> ListT m a -> ListT m a
dropWhile a -> Bool
pred ListT m a
l = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
    Step m a
n <- ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m a
l
    case Step m a
n of
        Cons a
x ListT m a
l'
            | a -> Bool
pred a
x    -> ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ((a -> Bool) -> ListT m a -> ListT m a
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> ListT m a -> ListT m a
dropWhile a -> Bool
pred ListT m a
l')
            | Bool
otherwise -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x ListT m a
l')
        Step m a
Nil             -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil )

-- | @takeWhile pred xs@ takes elements from @xs@ until the predicate @pred@ fails
--
-- >>> let list xs = do x <- select xs; liftIO (print (show x)); return x
-- >>> let sum = fold (+) 0 id
-- >>> sum (takeWhile even (list [2,4,5,7,8]))
-- "2"
-- "4"
-- "5"
-- 6
takeWhile :: Monad m => (a -> Bool) -> ListT m a -> ListT m a
takeWhile :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> ListT m a -> ListT m a
takeWhile a -> Bool
pred ListT m a
l = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
    Step m a
n <- ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m a
l
    case Step m a
n of
        Cons a
x ListT m a
l' | a -> Bool
pred a
x -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x ((a -> Bool) -> ListT m a -> ListT m a
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> ListT m a -> ListT m a
takeWhile a -> Bool
pred ListT m a
l'))
        Step m a
_                  -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil )

{- $filter

To filter elements from a list based on a predicate, use `Control.Monad.guard`.
For example, the following function is analogous to `Data.List.filter`:

> filter :: Monad m => (a -> m Bool) -> ListT m a -> ListT m a
> filter pred as = do
>     a <- as
>     b <- lift (pred a)
>     guard b
>     return a

-}

{- $concatenation

    Use (`<|>`) to concatenate two lists.

    > (<|>) :: ListT IO a -> ListT IO a -> ListT IO a

    Use `Data.Foldable.asum` to flatten a list of lists.

    > asum :: [ListT IO a] -> ListT IO a

    Use `Control.Monad.join` to flatten a `ListT` of `ListT`s.

    > join :: ListT IO (ListT IO a) -> ListT IO a

-}

{- $pairwise

    The (`<>`) operation joins every combination of an element from one list with
    an element from the other.

>>> runListT ( (select ["a", "b"] <> select ["1", "2", "3"]) >>= (liftIO . print) )
"a1"
"a2"
"a3"
"b1"
"b2"
"b3"

    This is the same combinatorial effect that (`>>=`) produces.

>>> runListT (do x <- select ["a", "b"]; y <- select ["1", "2", "3"]; liftIO (print (x <> y)))
"a1"
"a2"
"a3"
"b1"
"b2"
"b3"

-}

{- $repetition

Unbounded repetition can be induced using @'select' ('Data.List.repeat' ())@.
For example, here are several functions analogous to 'Data.List.cycle':

> cycle1 :: Monad m => a -> ListT m a
> cycle1 a = do
>     select (Data.List.repeat ())
>     return a

> cycle2 :: Monad m => [a] -> ListT m a
> cycle2 as = do
>     select (Data.List.repeat ())
>     select as

> cycle3 :: Monad m => m a -> ListT m a
> cycle3 m = do
>     select (Data.List.repeat ())
>     lift m

> cycle4 :: Monad m => [m a] -> ListT m a
> cycle4 ms = do
>     select (Data.List.repeat ())
>     m <- select ms
>     lift m

> cycle5 :: Monad m => ListT m a -> ListT m a
> cycle5 x = do
>     select (Data.List.repeat ())
>     x

> cycle6 :: Monad m => [ListT m a] -> ListT m a
> cycle6 lists = do
>     select (Data.List.repeat ())
>     x <- select lists
>     x

In a similar manner, we can use 'Data.List.replicate' as the initial selection
to achieve bounded repetition:

> replicate1 :: Monad m => Int -> a -> ListT m a
> replicate1 n a = do
>     select (Data.List.replicate n ())
>     return a

> replicate2 :: Monad m => Int -> [a] -> ListT m a
> replicate2 n as = do
>     select (Data.List.replicate n ())
>     select as

> replicate3 :: Monad m => Int -> m a -> ListT m a
> replicate3 n m = do
>     select (Data.List.replicate n ())
>     lift m

> replicate4 :: Monad m => Int -> [m a] -> ListT m a
> replicate4 n ms = do
>     select (Data.List.replicate n ())
>     m <- select ms
>     lift m

> replicate5 :: Monad m => Int -> ListT m a -> ListT m a
> replicate5 n x = do
>     select (Data.List.replicate n ())
>     x

> replicate6 :: Monad m => Int -> [ListT m a] -> ListT m a
> replicate6 n lists = do
>     select (Data.List.replicate n ())
>     x <- select lists
>     x

-}

-- | @unfold step seed@ generates a 'ListT' from a @step@ function and an
-- initial @seed@.
unfold :: Monad m => (b -> m (Maybe (a, b))) -> b -> ListT m a
unfold :: forall (m :: * -> *) b a.
Monad m =>
(b -> m (Maybe (a, b))) -> b -> ListT m a
unfold b -> m (Maybe (a, b))
step = b -> ListT m a
loop
  where
    loop :: b -> ListT m a
loop b
seed = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
        Maybe (a, b)
mx <- b -> m (Maybe (a, b))
step b
seed
        case Maybe (a, b)
mx of
            Just (a
x, b
seed') -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x (b -> ListT m a
loop b
seed'))
            Maybe (a, b)
Nothing         -> Step m a -> m (Step m a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m a
forall (m :: * -> *) a. Step m a
Nil)

-- | @zip xs ys@ zips two 'ListT' together, running the effects of each before
-- possibly recursing. Notice in the example below, @4@ is output even though
-- it has no corresponding element in the second list.
--
-- >>> let list xs = do x <- select xs; liftIO (print (show x)); return x
-- >>> runListT (zip (list [1,2,3,4,5]) (list [6,7,8]))
-- "1"
-- "6"
-- "2"
-- "7"
-- "3"
-- "8"
-- "4"
zip :: Monad m => ListT m a -> ListT m b -> ListT m (a, b)
zip :: forall (m :: * -> *) a b.
Monad m =>
ListT m a -> ListT m b -> ListT m (a, b)
zip ListT m a
xs ListT m b
ys = m (Step m (a, b)) -> ListT m (a, b)
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (do
    Step m a
sx <- ListT m a -> m (Step m a)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m a
xs
    Step m b
sy <- ListT m b -> m (Step m b)
forall (m :: * -> *) a. ListT m a -> m (Step m a)
next ListT m b
ys
    case (Step m a
sx, Step m b
sy) of
        (Cons a
x ListT m a
xs', Cons b
y ListT m b
ys') -> Step m (a, b) -> m (Step m (a, b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, b) -> ListT m (a, b) -> Step m (a, b)
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons (a
x, b
y) (ListT m a -> ListT m b -> ListT m (a, b)
forall (m :: * -> *) a b.
Monad m =>
ListT m a -> ListT m b -> ListT m (a, b)
zip ListT m a
xs' ListT m b
ys'))
        (Step m a, Step m b)
_                        -> Step m (a, b) -> m (Step m (a, b))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step m (a, b)
forall (m :: * -> *) a. Step m a
Nil)


{-| Pattern match on this type when you loop explicitly over a `ListT` using
    `next`.  For example:

> stdout :: ListT IO String -> IO ()
> stdout l = do
>     s <- next l
>     case s of
>         Nil       -> return ()
>         Cons x l' -> do
>             putStrLn x
>             stdout l'
-}
data Step m a = Cons a (ListT m a) | Nil
    deriving ((forall m. Monoid m => Step m m -> m)
-> (forall m a. Monoid m => (a -> m) -> Step m a -> m)
-> (forall m a. Monoid m => (a -> m) -> Step m a -> m)
-> (forall a b. (a -> b -> b) -> b -> Step m a -> b)
-> (forall a b. (a -> b -> b) -> b -> Step m a -> b)
-> (forall b a. (b -> a -> b) -> b -> Step m a -> b)
-> (forall b a. (b -> a -> b) -> b -> Step m a -> b)
-> (forall a. (a -> a -> a) -> Step m a -> a)
-> (forall a. (a -> a -> a) -> Step m a -> a)
-> (forall a. Step m a -> [a])
-> (forall a. Step m a -> Bool)
-> (forall a. Step m a -> Int)
-> (forall a. Eq a => a -> Step m a -> Bool)
-> (forall a. Ord a => Step m a -> a)
-> (forall a. Ord a => Step m a -> a)
-> (forall a. Num a => Step m a -> a)
-> (forall a. Num a => Step m a -> a)
-> Foldable (Step m)
forall a. Eq a => a -> Step m a -> Bool
forall a. Num a => Step m a -> a
forall a. Ord a => Step m a -> a
forall m. Monoid m => Step m m -> m
forall a. Step m a -> Bool
forall a. Step m a -> Int
forall a. Step m a -> [a]
forall a. (a -> a -> a) -> Step m a -> a
forall m a. Monoid m => (a -> m) -> Step m a -> m
forall b a. (b -> a -> b) -> b -> Step m a -> b
forall a b. (a -> b -> b) -> b -> Step m a -> b
forall (m :: * -> *) a. (Foldable m, Eq a) => a -> Step m a -> Bool
forall (m :: * -> *) a. (Foldable m, Num a) => Step m a -> a
forall (m :: * -> *) a. (Foldable m, Ord a) => Step m a -> a
forall (m :: * -> *) m. (Foldable m, Monoid m) => Step m m -> m
forall (m :: * -> *) a. Foldable m => Step m a -> Bool
forall (m :: * -> *) a. Foldable m => Step m a -> Int
forall (m :: * -> *) a. Foldable m => Step m a -> [a]
forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> Step m a -> a
forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> Step m a -> m
forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> Step m a -> b
forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> Step m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall (m :: * -> *) m. (Foldable m, Monoid m) => Step m m -> m
fold :: forall m. Monoid m => Step m m -> m
$cfoldMap :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> Step m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Step m a -> m
$cfoldMap' :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> Step m a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Step m a -> m
$cfoldr :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> Step m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Step m a -> b
$cfoldr' :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> Step m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Step m a -> b
$cfoldl :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> Step m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Step m a -> b
$cfoldl' :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> Step m a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Step m a -> b
$cfoldr1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> Step m a -> a
foldr1 :: forall a. (a -> a -> a) -> Step m a -> a
$cfoldl1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> Step m a -> a
foldl1 :: forall a. (a -> a -> a) -> Step m a -> a
$ctoList :: forall (m :: * -> *) a. Foldable m => Step m a -> [a]
toList :: forall a. Step m a -> [a]
$cnull :: forall (m :: * -> *) a. Foldable m => Step m a -> Bool
null :: forall a. Step m a -> Bool
$clength :: forall (m :: * -> *) a. Foldable m => Step m a -> Int
length :: forall a. Step m a -> Int
$celem :: forall (m :: * -> *) a. (Foldable m, Eq a) => a -> Step m a -> Bool
elem :: forall a. Eq a => a -> Step m a -> Bool
$cmaximum :: forall (m :: * -> *) a. (Foldable m, Ord a) => Step m a -> a
maximum :: forall a. Ord a => Step m a -> a
$cminimum :: forall (m :: * -> *) a. (Foldable m, Ord a) => Step m a -> a
minimum :: forall a. Ord a => Step m a -> a
$csum :: forall (m :: * -> *) a. (Foldable m, Num a) => Step m a -> a
sum :: forall a. Num a => Step m a -> a
$cproduct :: forall (m :: * -> *) a. (Foldable m, Num a) => Step m a -> a
product :: forall a. Num a => Step m a -> a
Foldable, Functor (Step m)
Foldable (Step m)
(Functor (Step m), Foldable (Step m)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Step m a -> f (Step m b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Step m (f a) -> f (Step m a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Step m a -> m (Step m b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Step m (m a) -> m (Step m a))
-> Traversable (Step m)
forall (m :: * -> *). (Monad m, Traversable m) => Functor (Step m)
forall (m :: * -> *). (Monad m, Traversable m) => Foldable (Step m)
forall (m :: * -> *) (m :: * -> *) a.
(Monad m, Traversable m, Monad m) =>
Step m (m a) -> m (Step m a)
forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Traversable m, Applicative f) =>
Step m (f a) -> f (Step m a)
forall (m :: * -> *) (m :: * -> *) a b.
(Monad m, Traversable m, Monad m) =>
(a -> m b) -> Step m a -> m (Step m b)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable m, Applicative f) =>
(a -> f b) -> Step m a -> f (Step m b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Step m (m a) -> m (Step m a)
forall (f :: * -> *) a.
Applicative f =>
Step m (f a) -> f (Step m a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Step m a -> m (Step m b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Step m a -> f (Step m b)
$ctraverse :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable m, Applicative f) =>
(a -> f b) -> Step m a -> f (Step m b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Step m a -> f (Step m b)
$csequenceA :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Traversable m, Applicative f) =>
Step m (f a) -> f (Step m a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Step m (f a) -> f (Step m a)
$cmapM :: forall (m :: * -> *) (m :: * -> *) a b.
(Monad m, Traversable m, Monad m) =>
(a -> m b) -> Step m a -> m (Step m b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Step m a -> m (Step m b)
$csequence :: forall (m :: * -> *) (m :: * -> *) a.
(Monad m, Traversable m, Monad m) =>
Step m (m a) -> m (Step m a)
sequence :: forall (m :: * -> *) a. Monad m => Step m (m a) -> m (Step m a)
Traversable)

instance Monad m => Functor (Step m) where
    fmap :: forall a b. (a -> b) -> Step m a -> Step m b
fmap a -> b
_  Step m a
Nil       = Step m b
forall (m :: * -> *) a. Step m a
Nil
    fmap a -> b
k (Cons a
x ListT m a
l) = b -> ListT m b -> Step m b
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons (a -> b
k a
x) ((a -> b) -> ListT m a -> ListT m b
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
k ListT m a
l)

instance MFunctor Step where
    hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> Step m b -> Step n b
hoist forall a. m a -> n a
_ Step m b
Nil         = Step n b
forall (m :: * -> *) a. Step m a
Nil
    hoist forall a. m a -> n a
f (Cons b
x ListT m b
xs) = b -> ListT n b -> Step n b
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons b
x ((forall a. m a -> n a) -> ListT m b -> ListT n b
forall {k} (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
       (b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> ListT m b -> ListT n b
hoist m a -> n a
forall a. m a -> n a
f ListT m b
xs)

-- | Similar to 'ZipList' in /base/: a newtype wrapper over 'ListT' that
-- overrides its normal 'Applicative' instance (combine every combination)
-- with one that "zips" outputs together one at a time.
--
-- >>> let xs = do x <- select [1,2,3,4]; liftIO (print x)
-- >>> let ys = do y <- select [5,6]; liftIO (print y)
-- >>> runListT (xs *> ys)
-- 1
-- 5
-- 6
-- 2
-- 5
-- 6
-- 3
-- 5
-- 6
-- 4
-- 5
-- 6
-- >>> runListT (getZipListT (ZipListT xs *> ZipListT ys))
-- 1
-- 5
-- 2
-- 6
-- 3
--
-- Note that the final "3" is printed even though it isn't paired with
-- anything.
--
-- While this can be used to do zipping, it is usually more convenient to
-- just use 'zip'.  This is more useful if you are working with a function
-- that expects "an Applicative instance", written to be polymorphic over
-- all Applicatives.
newtype ZipListT m a = ZipListT { forall (m :: * -> *) a. ZipListT m a -> ListT m a
getZipListT :: ListT m a }
  deriving ((forall a b. (a -> b) -> ZipListT m a -> ZipListT m b)
-> (forall a b. a -> ZipListT m b -> ZipListT m a)
-> Functor (ZipListT m)
forall a b. a -> ZipListT m b -> ZipListT m a
forall a b. (a -> b) -> ZipListT m a -> ZipListT m b
forall (m :: * -> *) a b.
Monad m =>
a -> ZipListT m b -> ZipListT m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> ZipListT m a -> ZipListT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> ZipListT m a -> ZipListT m b
fmap :: forall a b. (a -> b) -> ZipListT m a -> ZipListT m b
$c<$ :: forall (m :: * -> *) a b.
Monad m =>
a -> ZipListT m b -> ZipListT m a
<$ :: forall a b. a -> ZipListT m b -> ZipListT m a
Functor, Applicative (ZipListT m)
Applicative (ZipListT m) =>
(forall a. ZipListT m a)
-> (forall a. ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (forall a. ZipListT m a -> ZipListT m [a])
-> (forall a. ZipListT m a -> ZipListT m [a])
-> Alternative (ZipListT m)
forall a. ZipListT m a
forall a. ZipListT m a -> ZipListT m [a]
forall a. ZipListT m a -> ZipListT m a -> ZipListT m a
forall (m :: * -> *). Monad m => Applicative (ZipListT m)
forall (m :: * -> *) a. Monad m => ZipListT m a
forall (m :: * -> *) a. Monad m => ZipListT m a -> ZipListT m [a]
forall (m :: * -> *) a.
Monad m =>
ZipListT m a -> ZipListT m a -> ZipListT m a
forall (f :: * -> *).
Applicative f =>
(forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
$cempty :: forall (m :: * -> *) a. Monad m => ZipListT m a
empty :: forall a. ZipListT m a
$c<|> :: forall (m :: * -> *) a.
Monad m =>
ZipListT m a -> ZipListT m a -> ZipListT m a
<|> :: forall a. ZipListT m a -> ZipListT m a -> ZipListT m a
$csome :: forall (m :: * -> *) a. Monad m => ZipListT m a -> ZipListT m [a]
some :: forall a. ZipListT m a -> ZipListT m [a]
$cmany :: forall (m :: * -> *) a. Monad m => ZipListT m a -> ZipListT m [a]
many :: forall a. ZipListT m a -> ZipListT m [a]
Alternative, (forall m. Monoid m => ZipListT m m -> m)
-> (forall m a. Monoid m => (a -> m) -> ZipListT m a -> m)
-> (forall m a. Monoid m => (a -> m) -> ZipListT m a -> m)
-> (forall a b. (a -> b -> b) -> b -> ZipListT m a -> b)
-> (forall a b. (a -> b -> b) -> b -> ZipListT m a -> b)
-> (forall b a. (b -> a -> b) -> b -> ZipListT m a -> b)
-> (forall b a. (b -> a -> b) -> b -> ZipListT m a -> b)
-> (forall a. (a -> a -> a) -> ZipListT m a -> a)
-> (forall a. (a -> a -> a) -> ZipListT m a -> a)
-> (forall a. ZipListT m a -> [a])
-> (forall a. ZipListT m a -> Bool)
-> (forall a. ZipListT m a -> Int)
-> (forall a. Eq a => a -> ZipListT m a -> Bool)
-> (forall a. Ord a => ZipListT m a -> a)
-> (forall a. Ord a => ZipListT m a -> a)
-> (forall a. Num a => ZipListT m a -> a)
-> (forall a. Num a => ZipListT m a -> a)
-> Foldable (ZipListT m)
forall a. Eq a => a -> ZipListT m a -> Bool
forall a. Num a => ZipListT m a -> a
forall a. Ord a => ZipListT m a -> a
forall m. Monoid m => ZipListT m m -> m
forall a. ZipListT m a -> Bool
forall a. ZipListT m a -> Int
forall a. ZipListT m a -> [a]
forall a. (a -> a -> a) -> ZipListT m a -> a
forall m a. Monoid m => (a -> m) -> ZipListT m a -> m
forall b a. (b -> a -> b) -> b -> ZipListT m a -> b
forall a b. (a -> b -> b) -> b -> ZipListT m a -> b
forall (m :: * -> *) a.
(Foldable m, Eq a) =>
a -> ZipListT m a -> Bool
forall (m :: * -> *) a. (Foldable m, Num a) => ZipListT m a -> a
forall (m :: * -> *) a. (Foldable m, Ord a) => ZipListT m a -> a
forall (m :: * -> *) m. (Foldable m, Monoid m) => ZipListT m m -> m
forall (m :: * -> *) a. Foldable m => ZipListT m a -> Bool
forall (m :: * -> *) a. Foldable m => ZipListT m a -> Int
forall (m :: * -> *) a. Foldable m => ZipListT m a -> [a]
forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> ZipListT m a -> a
forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ZipListT m a -> m
forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ZipListT m a -> b
forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ZipListT m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall (m :: * -> *) m. (Foldable m, Monoid m) => ZipListT m m -> m
fold :: forall m. Monoid m => ZipListT m m -> m
$cfoldMap :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ZipListT m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ZipListT m a -> m
$cfoldMap' :: forall (m :: * -> *) m a.
(Foldable m, Monoid m) =>
(a -> m) -> ZipListT m a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ZipListT m a -> m
$cfoldr :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ZipListT m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ZipListT m a -> b
$cfoldr' :: forall (m :: * -> *) a b.
Foldable m =>
(a -> b -> b) -> b -> ZipListT m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ZipListT m a -> b
$cfoldl :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ZipListT m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ZipListT m a -> b
$cfoldl' :: forall (m :: * -> *) b a.
Foldable m =>
(b -> a -> b) -> b -> ZipListT m a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ZipListT m a -> b
$cfoldr1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> ZipListT m a -> a
foldr1 :: forall a. (a -> a -> a) -> ZipListT m a -> a
$cfoldl1 :: forall (m :: * -> *) a.
Foldable m =>
(a -> a -> a) -> ZipListT m a -> a
foldl1 :: forall a. (a -> a -> a) -> ZipListT m a -> a
$ctoList :: forall (m :: * -> *) a. Foldable m => ZipListT m a -> [a]
toList :: forall a. ZipListT m a -> [a]
$cnull :: forall (m :: * -> *) a. Foldable m => ZipListT m a -> Bool
null :: forall a. ZipListT m a -> Bool
$clength :: forall (m :: * -> *) a. Foldable m => ZipListT m a -> Int
length :: forall a. ZipListT m a -> Int
$celem :: forall (m :: * -> *) a.
(Foldable m, Eq a) =>
a -> ZipListT m a -> Bool
elem :: forall a. Eq a => a -> ZipListT m a -> Bool
$cmaximum :: forall (m :: * -> *) a. (Foldable m, Ord a) => ZipListT m a -> a
maximum :: forall a. Ord a => ZipListT m a -> a
$cminimum :: forall (m :: * -> *) a. (Foldable m, Ord a) => ZipListT m a -> a
minimum :: forall a. Ord a => ZipListT m a -> a
$csum :: forall (m :: * -> *) a. (Foldable m, Num a) => ZipListT m a -> a
sum :: forall a. Num a => ZipListT m a -> a
$cproduct :: forall (m :: * -> *) a. (Foldable m, Num a) => ZipListT m a -> a
product :: forall a. Num a => ZipListT m a -> a
Foldable, Functor (ZipListT m)
Foldable (ZipListT m)
(Functor (ZipListT m), Foldable (ZipListT m)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> ZipListT m a -> f (ZipListT m b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    ZipListT m (f a) -> f (ZipListT m a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> ZipListT m a -> m (ZipListT m b))
-> (forall (m :: * -> *) a.
    Monad m =>
    ZipListT m (m a) -> m (ZipListT m a))
-> Traversable (ZipListT m)
forall (m :: * -> *).
(Monad m, Traversable m) =>
Functor (ZipListT m)
forall (m :: * -> *).
(Monad m, Traversable m) =>
Foldable (ZipListT m)
forall (m :: * -> *) (m :: * -> *) a.
(Monad m, Traversable m, Monad m) =>
ZipListT m (m a) -> m (ZipListT m a)
forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Traversable m, Applicative f) =>
ZipListT m (f a) -> f (ZipListT m a)
forall (m :: * -> *) (m :: * -> *) a b.
(Monad m, Traversable m, Monad m) =>
(a -> m b) -> ZipListT m a -> m (ZipListT m b)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable m, Applicative f) =>
(a -> f b) -> ZipListT m a -> f (ZipListT m b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
ZipListT m (m a) -> m (ZipListT m a)
forall (f :: * -> *) a.
Applicative f =>
ZipListT m (f a) -> f (ZipListT m a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ZipListT m a -> m (ZipListT m b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ZipListT m a -> f (ZipListT m b)
$ctraverse :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable m, Applicative f) =>
(a -> f b) -> ZipListT m a -> f (ZipListT m b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ZipListT m a -> f (ZipListT m b)
$csequenceA :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Traversable m, Applicative f) =>
ZipListT m (f a) -> f (ZipListT m a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ZipListT m (f a) -> f (ZipListT m a)
$cmapM :: forall (m :: * -> *) (m :: * -> *) a b.
(Monad m, Traversable m, Monad m) =>
(a -> m b) -> ZipListT m a -> m (ZipListT m b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ZipListT m a -> m (ZipListT m b)
$csequence :: forall (m :: * -> *) (m :: * -> *) a.
(Monad m, Traversable m, Monad m) =>
ZipListT m (m a) -> m (ZipListT m a)
sequence :: forall (m :: * -> *) a.
Monad m =>
ZipListT m (m a) -> m (ZipListT m a)
Traversable, Fractional (ZipListT m a)
ZipListT m a
Fractional (ZipListT m a) =>
ZipListT m a
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> Floating (ZipListT m a)
ZipListT m a -> ZipListT m a
ZipListT m a -> ZipListT m a -> ZipListT m a
forall a.
Fractional a =>
a
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> Floating a
forall (m :: * -> *) a.
(Monad m, Floating a) =>
Fractional (ZipListT m a)
forall (m :: * -> *) a. (Monad m, Floating a) => ZipListT m a
forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
$cpi :: forall (m :: * -> *) a. (Monad m, Floating a) => ZipListT m a
pi :: ZipListT m a
$cexp :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
exp :: ZipListT m a -> ZipListT m a
$clog :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
log :: ZipListT m a -> ZipListT m a
$csqrt :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
sqrt :: ZipListT m a -> ZipListT m a
$c** :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
** :: ZipListT m a -> ZipListT m a -> ZipListT m a
$clogBase :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
logBase :: ZipListT m a -> ZipListT m a -> ZipListT m a
$csin :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
sin :: ZipListT m a -> ZipListT m a
$ccos :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
cos :: ZipListT m a -> ZipListT m a
$ctan :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
tan :: ZipListT m a -> ZipListT m a
$casin :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
asin :: ZipListT m a -> ZipListT m a
$cacos :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
acos :: ZipListT m a -> ZipListT m a
$catan :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
atan :: ZipListT m a -> ZipListT m a
$csinh :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
sinh :: ZipListT m a -> ZipListT m a
$ccosh :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
cosh :: ZipListT m a -> ZipListT m a
$ctanh :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
tanh :: ZipListT m a -> ZipListT m a
$casinh :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
asinh :: ZipListT m a -> ZipListT m a
$cacosh :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
acosh :: ZipListT m a -> ZipListT m a
$catanh :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
atanh :: ZipListT m a -> ZipListT m a
$clog1p :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
log1p :: ZipListT m a -> ZipListT m a
$cexpm1 :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
expm1 :: ZipListT m a -> ZipListT m a
$clog1pexp :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
log1pexp :: ZipListT m a -> ZipListT m a
$clog1mexp :: forall (m :: * -> *) a.
(Monad m, Floating a) =>
ZipListT m a -> ZipListT m a
log1mexp :: ZipListT m a -> ZipListT m a
Floating, Num (ZipListT m a)
Num (ZipListT m a) =>
(ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (Rational -> ZipListT m a)
-> Fractional (ZipListT m a)
Rational -> ZipListT m a
ZipListT m a -> ZipListT m a
ZipListT m a -> ZipListT m a -> ZipListT m a
forall a.
Num a =>
(a -> a -> a) -> (a -> a) -> (Rational -> a) -> Fractional a
forall (m :: * -> *) a.
(Monad m, Fractional a) =>
Num (ZipListT m a)
forall (m :: * -> *) a.
(Monad m, Fractional a) =>
Rational -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Fractional a) =>
ZipListT m a -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Fractional a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
$c/ :: forall (m :: * -> *) a.
(Monad m, Fractional a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
/ :: ZipListT m a -> ZipListT m a -> ZipListT m a
$crecip :: forall (m :: * -> *) a.
(Monad m, Fractional a) =>
ZipListT m a -> ZipListT m a
recip :: ZipListT m a -> ZipListT m a
$cfromRational :: forall (m :: * -> *) a.
(Monad m, Fractional a) =>
Rational -> ZipListT m a
fromRational :: Rational -> ZipListT m a
Fractional, Integer -> ZipListT m a
ZipListT m a -> ZipListT m a
ZipListT m a -> ZipListT m a -> ZipListT m a
(ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (ZipListT m a -> ZipListT m a)
-> (Integer -> ZipListT m a)
-> Num (ZipListT m a)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
forall (m :: * -> *) a. (Monad m, Num a) => Integer -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
$c+ :: forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
+ :: ZipListT m a -> ZipListT m a -> ZipListT m a
$c- :: forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
- :: ZipListT m a -> ZipListT m a -> ZipListT m a
$c* :: forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
* :: ZipListT m a -> ZipListT m a -> ZipListT m a
$cnegate :: forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a
negate :: ZipListT m a -> ZipListT m a
$cabs :: forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a
abs :: ZipListT m a -> ZipListT m a
$csignum :: forall (m :: * -> *) a.
(Monad m, Num a) =>
ZipListT m a -> ZipListT m a
signum :: ZipListT m a -> ZipListT m a
$cfromInteger :: forall (m :: * -> *) a. (Monad m, Num a) => Integer -> ZipListT m a
fromInteger :: Integer -> ZipListT m a
Num, NonEmpty (ZipListT m a) -> ZipListT m a
ZipListT m a -> ZipListT m a -> ZipListT m a
(ZipListT m a -> ZipListT m a -> ZipListT m a)
-> (NonEmpty (ZipListT m a) -> ZipListT m a)
-> (forall b. Integral b => b -> ZipListT m a -> ZipListT m a)
-> Semigroup (ZipListT m a)
forall b. Integral b => b -> ZipListT m a -> ZipListT m a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall (m :: * -> *) a.
(Monad m, Semigroup a) =>
NonEmpty (ZipListT m a) -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Semigroup a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
forall (m :: * -> *) a b.
(Monad m, Semigroup a, Integral b) =>
b -> ZipListT m a -> ZipListT m a
$c<> :: forall (m :: * -> *) a.
(Monad m, Semigroup a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
<> :: ZipListT m a -> ZipListT m a -> ZipListT m a
$csconcat :: forall (m :: * -> *) a.
(Monad m, Semigroup a) =>
NonEmpty (ZipListT m a) -> ZipListT m a
sconcat :: NonEmpty (ZipListT m a) -> ZipListT m a
$cstimes :: forall (m :: * -> *) a b.
(Monad m, Semigroup a, Integral b) =>
b -> ZipListT m a -> ZipListT m a
stimes :: forall b. Integral b => b -> ZipListT m a -> ZipListT m a
Semigroup, Semigroup (ZipListT m a)
ZipListT m a
Semigroup (ZipListT m a) =>
ZipListT m a
-> (ZipListT m a -> ZipListT m a -> ZipListT m a)
-> ([ZipListT m a] -> ZipListT m a)
-> Monoid (ZipListT m a)
[ZipListT m a] -> ZipListT m a
ZipListT m a -> ZipListT m a -> ZipListT m a
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall (m :: * -> *) a.
(Monad m, Monoid a) =>
Semigroup (ZipListT m a)
forall (m :: * -> *) a. (Monad m, Monoid a) => ZipListT m a
forall (m :: * -> *) a.
(Monad m, Monoid a) =>
[ZipListT m a] -> ZipListT m a
forall (m :: * -> *) a.
(Monad m, Monoid a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
$cmempty :: forall (m :: * -> *) a. (Monad m, Monoid a) => ZipListT m a
mempty :: ZipListT m a
$cmappend :: forall (m :: * -> *) a.
(Monad m, Monoid a) =>
ZipListT m a -> ZipListT m a -> ZipListT m a
mappend :: ZipListT m a -> ZipListT m a -> ZipListT m a
$cmconcat :: forall (m :: * -> *) a.
(Monad m, Monoid a) =>
[ZipListT m a] -> ZipListT m a
mconcat :: [ZipListT m a] -> ZipListT m a
Monoid)

instance Monad m => Applicative (ZipListT m) where
    pure :: forall a. a -> ZipListT m a
pure a
x = ListT m a -> ZipListT m a
forall (m :: * -> *) a. ListT m a -> ZipListT m a
ZipListT ListT m a
go
      where
#if MIN_VERSION_base(4,8,0)
        go :: ListT m a
go = m (Step m a) -> ListT m a
forall (m :: * -> *) a. m (Step m a) -> ListT m a
ListT (Step m a -> m (Step m a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> ListT m a -> Step m a
forall (m :: * -> *) a. a -> ListT m a -> Step m a
Cons a
x ListT m a
go))
#else
        go = ListT (return (Cons x go))
#endif
    ZipListT ListT m (a -> b)
fs <*> :: forall a b. ZipListT m (a -> b) -> ZipListT m a -> ZipListT m b
<*> ZipListT ListT m a
xs = ListT m b -> ZipListT m b
forall (m :: * -> *) a. ListT m a -> ZipListT m a
ZipListT (((a -> b, a) -> b) -> ListT m (a -> b, a) -> ListT m b
forall a b. (a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((a -> b) -> a -> b) -> (a -> b, a) -> b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
($)) (ListT m (a -> b) -> ListT m a -> ListT m (a -> b, a)
forall (m :: * -> *) a b.
Monad m =>
ListT m a -> ListT m b -> ListT m (a, b)
zip ListT m (a -> b)
fs ListT m a
xs))