{-# LANGUAGE UndecidableInstances #-}

--------------------------------------------------------------------------------
-- |
-- Module      : Control.Monad.Heap.List
-- Copyright   : (c) Donnacha Oisín Kidney 2021
-- Maintainer  : mail@doisinkidney.com
-- Stability   : experimental
-- Portability : non-portable
--
-- A simple re-implementation of the list monad transformer, for use by the heap
-- monad.
--------------------------------------------------------------------------------
module Control.Monad.Heap.List
  ( -- * Type Definition
    ListT(..)
  , ListCons(..)
    -- * Building the list
  , unfoldrM
    -- * Running the list
  , toListT
    -- * Transforming lists
  , catMaybesT
  , listMmap
  ) where

import Data.Bifunctor ( Bifunctor(first, bimap) )
import Data.Bifoldable ( Bifoldable(..) )
import Data.Bitraversable ( Bitraversable(..) )
import Control.Monad ( MonadPlus )
import Control.Applicative
    ( Applicative(liftA2), Alternative((<|>), empty) )
import Control.Monad.Trans ( MonadTrans(..) )
import Control.Monad.State ( MonadState(..) )
import Control.Monad.Except ( MonadError(..) )
import Control.Monad.Reader ( MonadReader(..) )
import Control.Monad.Writer ( Alt(Alt), MonadWriter(..) )
import Control.Monad.Cont ( MonadCont(..) )
import Test.QuickCheck
    ( Arbitrary(..),
      arbitrary1,
      arbitrary2,
      shrink1,
      shrink2,
      frequency,
      sized,
      Arbitrary1(..),
      Arbitrary2(..) )
import MonusWeightedSearch.Internal.CoerceOperators
    ( (#.), (.#), (<#$>) )
import Control.DeepSeq ( NFData(..) )
import GHC.Generics ( Generic, Generic1 )
import Data.Data ( Data, Typeable )
import Data.Coerce ( coerce )

infixr 5 :-
-- | The list constructor.
data ListCons a b = Nil | a :- !b
  deriving ((forall a b. (a -> b) -> ListCons a a -> ListCons a b)
-> (forall a b. a -> ListCons a b -> ListCons a a)
-> Functor (ListCons a)
forall a b. a -> ListCons a b -> ListCons a a
forall a b. (a -> b) -> ListCons a a -> ListCons a b
forall a a b. a -> ListCons a b -> ListCons a a
forall a a b. (a -> b) -> ListCons a a -> ListCons a b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ListCons a b -> ListCons a a
$c<$ :: forall a a b. a -> ListCons a b -> ListCons a a
fmap :: forall a b. (a -> b) -> ListCons a a -> ListCons a b
$cfmap :: forall a a b. (a -> b) -> ListCons a a -> ListCons a b
Functor, (forall m. Monoid m => ListCons a m -> m)
-> (forall m a. Monoid m => (a -> m) -> ListCons a a -> m)
-> (forall m a. Monoid m => (a -> m) -> ListCons a a -> m)
-> (forall a b. (a -> b -> b) -> b -> ListCons a a -> b)
-> (forall a b. (a -> b -> b) -> b -> ListCons a a -> b)
-> (forall b a. (b -> a -> b) -> b -> ListCons a a -> b)
-> (forall b a. (b -> a -> b) -> b -> ListCons a a -> b)
-> (forall a. (a -> a -> a) -> ListCons a a -> a)
-> (forall a. (a -> a -> a) -> ListCons a a -> a)
-> (forall a. ListCons a a -> [a])
-> (forall a. ListCons a a -> Bool)
-> (forall a. ListCons a a -> Int)
-> (forall a. Eq a => a -> ListCons a a -> Bool)
-> (forall a. Ord a => ListCons a a -> a)
-> (forall a. Ord a => ListCons a a -> a)
-> (forall a. Num a => ListCons a a -> a)
-> (forall a. Num a => ListCons a a -> a)
-> Foldable (ListCons a)
forall a. Eq a => a -> ListCons a a -> Bool
forall a. Num a => ListCons a a -> a
forall a. Ord a => ListCons a a -> a
forall m. Monoid m => ListCons a m -> m
forall a. ListCons a a -> Bool
forall a. ListCons a a -> Int
forall a. ListCons a a -> [a]
forall a. (a -> a -> a) -> ListCons a a -> a
forall a a. Eq a => a -> ListCons a a -> Bool
forall a a. Num a => ListCons a a -> a
forall a a. Ord a => ListCons a a -> a
forall m a. Monoid m => (a -> m) -> ListCons a a -> m
forall a m. Monoid m => ListCons a m -> m
forall a a. ListCons a a -> Bool
forall a a. ListCons a a -> Int
forall a a. ListCons a a -> [a]
forall b a. (b -> a -> b) -> b -> ListCons a a -> b
forall a b. (a -> b -> b) -> b -> ListCons a a -> b
forall a a. (a -> a -> a) -> ListCons a a -> a
forall a m a. Monoid m => (a -> m) -> ListCons a a -> m
forall a b a. (b -> a -> b) -> b -> ListCons a a -> b
forall a a b. (a -> b -> b) -> b -> ListCons a 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
product :: forall a. Num a => ListCons a a -> a
$cproduct :: forall a a. Num a => ListCons a a -> a
sum :: forall a. Num a => ListCons a a -> a
$csum :: forall a a. Num a => ListCons a a -> a
minimum :: forall a. Ord a => ListCons a a -> a
$cminimum :: forall a a. Ord a => ListCons a a -> a
maximum :: forall a. Ord a => ListCons a a -> a
$cmaximum :: forall a a. Ord a => ListCons a a -> a
elem :: forall a. Eq a => a -> ListCons a a -> Bool
$celem :: forall a a. Eq a => a -> ListCons a a -> Bool
length :: forall a. ListCons a a -> Int
$clength :: forall a a. ListCons a a -> Int
null :: forall a. ListCons a a -> Bool
$cnull :: forall a a. ListCons a a -> Bool
toList :: forall a. ListCons a a -> [a]
$ctoList :: forall a a. ListCons a a -> [a]
foldl1 :: forall a. (a -> a -> a) -> ListCons a a -> a
$cfoldl1 :: forall a a. (a -> a -> a) -> ListCons a a -> a
foldr1 :: forall a. (a -> a -> a) -> ListCons a a -> a
$cfoldr1 :: forall a a. (a -> a -> a) -> ListCons a a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> ListCons a a -> b
$cfoldl' :: forall a b a. (b -> a -> b) -> b -> ListCons a a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ListCons a a -> b
$cfoldl :: forall a b a. (b -> a -> b) -> b -> ListCons a a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ListCons a a -> b
$cfoldr' :: forall a a b. (a -> b -> b) -> b -> ListCons a a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ListCons a a -> b
$cfoldr :: forall a a b. (a -> b -> b) -> b -> ListCons a a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> ListCons a a -> m
$cfoldMap' :: forall a m a. Monoid m => (a -> m) -> ListCons a a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ListCons a a -> m
$cfoldMap :: forall a m a. Monoid m => (a -> m) -> ListCons a a -> m
fold :: forall m. Monoid m => ListCons a m -> m
$cfold :: forall a m. Monoid m => ListCons a m -> m
Foldable, Functor (ListCons a)
Foldable (ListCons a)
Functor (ListCons a)
-> Foldable (ListCons a)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> ListCons a a -> f (ListCons a b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    ListCons a (f a) -> f (ListCons a a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> ListCons a a -> m (ListCons a b))
-> (forall (m :: * -> *) a.
    Monad m =>
    ListCons a (m a) -> m (ListCons a a))
-> Traversable (ListCons a)
forall a. Functor (ListCons a)
forall a. Foldable (ListCons a)
forall a (m :: * -> *) a.
Monad m =>
ListCons a (m a) -> m (ListCons a a)
forall a (f :: * -> *) a.
Applicative f =>
ListCons a (f a) -> f (ListCons a a)
forall a (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ListCons a a -> m (ListCons a b)
forall a (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListCons a a -> f (ListCons a 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 =>
ListCons a (m a) -> m (ListCons a a)
forall (f :: * -> *) a.
Applicative f =>
ListCons a (f a) -> f (ListCons a a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ListCons a a -> m (ListCons a b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListCons a a -> f (ListCons a b)
sequence :: forall (m :: * -> *) a.
Monad m =>
ListCons a (m a) -> m (ListCons a a)
$csequence :: forall a (m :: * -> *) a.
Monad m =>
ListCons a (m a) -> m (ListCons a a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ListCons a a -> m (ListCons a b)
$cmapM :: forall a (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ListCons a a -> m (ListCons a b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ListCons a (f a) -> f (ListCons a a)
$csequenceA :: forall a (f :: * -> *) a.
Applicative f =>
ListCons a (f a) -> f (ListCons a a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListCons a a -> f (ListCons a b)
$ctraverse :: forall a (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListCons a a -> f (ListCons a b)
Traversable, ListCons a b -> ListCons a b -> Bool
(ListCons a b -> ListCons a b -> Bool)
-> (ListCons a b -> ListCons a b -> Bool) -> Eq (ListCons a b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq a, Eq b) => ListCons a b -> ListCons a b -> Bool
/= :: ListCons a b -> ListCons a b -> Bool
$c/= :: forall a b. (Eq a, Eq b) => ListCons a b -> ListCons a b -> Bool
== :: ListCons a b -> ListCons a b -> Bool
$c== :: forall a b. (Eq a, Eq b) => ListCons a b -> ListCons a b -> Bool
Eq, Eq (ListCons a b)
Eq (ListCons a b)
-> (ListCons a b -> ListCons a b -> Ordering)
-> (ListCons a b -> ListCons a b -> Bool)
-> (ListCons a b -> ListCons a b -> Bool)
-> (ListCons a b -> ListCons a b -> Bool)
-> (ListCons a b -> ListCons a b -> Bool)
-> (ListCons a b -> ListCons a b -> ListCons a b)
-> (ListCons a b -> ListCons a b -> ListCons a b)
-> Ord (ListCons a b)
ListCons a b -> ListCons a b -> Bool
ListCons a b -> ListCons a b -> Ordering
ListCons a b -> ListCons a b -> ListCons a b
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a} {b}. (Ord a, Ord b) => Eq (ListCons a b)
forall a b. (Ord a, Ord b) => ListCons a b -> ListCons a b -> Bool
forall a b.
(Ord a, Ord b) =>
ListCons a b -> ListCons a b -> Ordering
forall a b.
(Ord a, Ord b) =>
ListCons a b -> ListCons a b -> ListCons a b
min :: ListCons a b -> ListCons a b -> ListCons a b
$cmin :: forall a b.
(Ord a, Ord b) =>
ListCons a b -> ListCons a b -> ListCons a b
max :: ListCons a b -> ListCons a b -> ListCons a b
$cmax :: forall a b.
(Ord a, Ord b) =>
ListCons a b -> ListCons a b -> ListCons a b
>= :: ListCons a b -> ListCons a b -> Bool
$c>= :: forall a b. (Ord a, Ord b) => ListCons a b -> ListCons a b -> Bool
> :: ListCons a b -> ListCons a b -> Bool
$c> :: forall a b. (Ord a, Ord b) => ListCons a b -> ListCons a b -> Bool
<= :: ListCons a b -> ListCons a b -> Bool
$c<= :: forall a b. (Ord a, Ord b) => ListCons a b -> ListCons a b -> Bool
< :: ListCons a b -> ListCons a b -> Bool
$c< :: forall a b. (Ord a, Ord b) => ListCons a b -> ListCons a b -> Bool
compare :: ListCons a b -> ListCons a b -> Ordering
$ccompare :: forall a b.
(Ord a, Ord b) =>
ListCons a b -> ListCons a b -> Ordering
Ord, Int -> ListCons a b -> ShowS
[ListCons a b] -> ShowS
ListCons a b -> String
(Int -> ListCons a b -> ShowS)
-> (ListCons a b -> String)
-> ([ListCons a b] -> ShowS)
-> Show (ListCons a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> ListCons a b -> ShowS
forall a b. (Show a, Show b) => [ListCons a b] -> ShowS
forall a b. (Show a, Show b) => ListCons a b -> String
showList :: [ListCons a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [ListCons a b] -> ShowS
show :: ListCons a b -> String
$cshow :: forall a b. (Show a, Show b) => ListCons a b -> String
showsPrec :: Int -> ListCons a b -> ShowS
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> ListCons a b -> ShowS
Show, ReadPrec [ListCons a b]
ReadPrec (ListCons a b)
Int -> ReadS (ListCons a b)
ReadS [ListCons a b]
(Int -> ReadS (ListCons a b))
-> ReadS [ListCons a b]
-> ReadPrec (ListCons a b)
-> ReadPrec [ListCons a b]
-> Read (ListCons a b)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall a b. (Read a, Read b) => ReadPrec [ListCons a b]
forall a b. (Read a, Read b) => ReadPrec (ListCons a b)
forall a b. (Read a, Read b) => Int -> ReadS (ListCons a b)
forall a b. (Read a, Read b) => ReadS [ListCons a b]
readListPrec :: ReadPrec [ListCons a b]
$creadListPrec :: forall a b. (Read a, Read b) => ReadPrec [ListCons a b]
readPrec :: ReadPrec (ListCons a b)
$creadPrec :: forall a b. (Read a, Read b) => ReadPrec (ListCons a b)
readList :: ReadS [ListCons a b]
$creadList :: forall a b. (Read a, Read b) => ReadS [ListCons a b]
readsPrec :: Int -> ReadS (ListCons a b)
$creadsPrec :: forall a b. (Read a, Read b) => Int -> ReadS (ListCons a b)
Read, Typeable (ListCons a b)
Typeable (ListCons a b)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ListCons a b -> c (ListCons a b))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (ListCons a b))
-> (ListCons a b -> Constr)
-> (ListCons a b -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (ListCons a b)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (ListCons a b)))
-> ((forall b. Data b => b -> b) -> ListCons a b -> ListCons a b)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ListCons a b -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ListCons a b -> r)
-> (forall u. (forall d. Data d => d -> u) -> ListCons a b -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> ListCons a b -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b))
-> Data (ListCons a b)
ListCons a b -> DataType
ListCons a b -> Constr
(forall b. Data b => b -> b) -> ListCons a b -> ListCons a b
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ListCons a b -> u
forall u. (forall d. Data d => d -> u) -> ListCons a b -> [u]
forall {a} {b}. (Data a, Data b) => Typeable (ListCons a b)
forall a b. (Data a, Data b) => ListCons a b -> DataType
forall a b. (Data a, Data b) => ListCons a b -> Constr
forall a b.
(Data a, Data b) =>
(forall b. Data b => b -> b) -> ListCons a b -> ListCons a b
forall a b u.
(Data a, Data b) =>
Int -> (forall d. Data d => d -> u) -> ListCons a b -> u
forall a b u.
(Data a, Data b) =>
(forall d. Data d => d -> u) -> ListCons a b -> [u]
forall a b r r'.
(Data a, Data b) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
forall a b r r'.
(Data a, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
forall a b (m :: * -> *).
(Data a, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
forall a b (c :: * -> *).
(Data a, Data b) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (ListCons a b)
forall a b (c :: * -> *).
(Data a, Data b) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ListCons a b -> c (ListCons a b)
forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (ListCons a b))
forall a b (t :: * -> * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ListCons a b))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (ListCons a b)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ListCons a b -> c (ListCons a b)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (ListCons a b))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ListCons a b))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
$cgmapMo :: forall a b (m :: * -> *).
(Data a, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
$cgmapMp :: forall a b (m :: * -> *).
(Data a, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
$cgmapM :: forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(forall d. Data d => d -> m d) -> ListCons a b -> m (ListCons a b)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ListCons a b -> u
$cgmapQi :: forall a b u.
(Data a, Data b) =>
Int -> (forall d. Data d => d -> u) -> ListCons a b -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> ListCons a b -> [u]
$cgmapQ :: forall a b u.
(Data a, Data b) =>
(forall d. Data d => d -> u) -> ListCons a b -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
$cgmapQr :: forall a b r r'.
(Data a, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
$cgmapQl :: forall a b r r'.
(Data a, Data b) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ListCons a b -> r
gmapT :: (forall b. Data b => b -> b) -> ListCons a b -> ListCons a b
$cgmapT :: forall a b.
(Data a, Data b) =>
(forall b. Data b => b -> b) -> ListCons a b -> ListCons a b
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ListCons a b))
$cdataCast2 :: forall a b (t :: * -> * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ListCons a b))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (ListCons a b))
$cdataCast1 :: forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (ListCons a b))
dataTypeOf :: ListCons a b -> DataType
$cdataTypeOf :: forall a b. (Data a, Data b) => ListCons a b -> DataType
toConstr :: ListCons a b -> Constr
$ctoConstr :: forall a b. (Data a, Data b) => ListCons a b -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (ListCons a b)
$cgunfold :: forall a b (c :: * -> *).
(Data a, Data b) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (ListCons a b)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ListCons a b -> c (ListCons a b)
$cgfoldl :: forall a b (c :: * -> *).
(Data a, Data b) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ListCons a b -> c (ListCons a b)
Data, Typeable, (forall x. ListCons a b -> Rep (ListCons a b) x)
-> (forall x. Rep (ListCons a b) x -> ListCons a b)
-> Generic (ListCons a b)
forall x. Rep (ListCons a b) x -> ListCons a b
forall x. ListCons a b -> Rep (ListCons a b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a b x. Rep (ListCons a b) x -> ListCons a b
forall a b x. ListCons a b -> Rep (ListCons a b) x
$cto :: forall a b x. Rep (ListCons a b) x -> ListCons a b
$cfrom :: forall a b x. ListCons a b -> Rep (ListCons a b) x
Generic, (forall a. ListCons a a -> Rep1 (ListCons a) a)
-> (forall a. Rep1 (ListCons a) a -> ListCons a a)
-> Generic1 (ListCons a)
forall a. Rep1 (ListCons a) a -> ListCons a a
forall a. ListCons a a -> Rep1 (ListCons a) a
forall a a. Rep1 (ListCons a) a -> ListCons a a
forall a a. ListCons a a -> Rep1 (ListCons a) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a a. Rep1 (ListCons a) a -> ListCons a a
$cfrom1 :: forall a a. ListCons a a -> Rep1 (ListCons a) a
Generic1)

instance (NFData a, NFData b) => NFData (ListCons a b) where
  rnf :: ListCons a b -> ()
rnf ListCons a b
Nil = ()
  rnf (a
x :- b
xs) = a -> ()
forall a. NFData a => a -> ()
rnf a
x () -> () -> ()
`seq` b -> ()
forall a. NFData a => a -> ()
rnf b
xs
  {-# INLINE rnf #-}

instance Bifunctor ListCons where
  bimap :: forall a b c d.
(a -> b) -> (c -> d) -> ListCons a c -> ListCons b d
bimap a -> b
f c -> d
g ListCons a c
Nil = ListCons b d
forall a b. ListCons a b
Nil
  bimap a -> b
f c -> d
g (a
x :- c
xs) = a -> b
f a
x b -> d -> ListCons b d
forall a b. a -> b -> ListCons a b
:- c -> d
g c
xs
  {-# INLINE bimap #-}

instance Bifoldable ListCons where
  bifold :: forall m. Monoid m => ListCons m m -> m
bifold ListCons m m
Nil = m
forall a. Monoid a => a
mempty
  bifold (m
x :- m
xs) = m
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
xs
  {-# INLINE bifold #-}

  bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> ListCons a b -> m
bifoldMap a -> m
f b -> m
g ListCons a b
Nil = m
forall a. Monoid a => a
mempty
  bifoldMap a -> m
f b -> m
g (a
x :- b
xs) = a -> m
f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> b -> m
g b
xs
  {-# INLINE bifoldMap #-}

  bifoldr :: forall a c b.
(a -> c -> c) -> (b -> c -> c) -> c -> ListCons a b -> c
bifoldr a -> c -> c
f b -> c -> c
g c
b ListCons a b
Nil = c
b
  bifoldr a -> c -> c
f b -> c -> c
g c
b (a
x :- b
xs) = a -> c -> c
f a
x (b -> c -> c
g b
xs c
b)
  {-# INLINE bifoldr #-}

  bifoldl :: forall c a b.
(c -> a -> c) -> (c -> b -> c) -> c -> ListCons a b -> c
bifoldl c -> a -> c
f c -> b -> c
g c
b ListCons a b
Nil = c
b
  bifoldl c -> a -> c
f c -> b -> c
g c
b (a
x :- b
xs) = c -> b -> c
g (c -> a -> c
f c
b a
x) b
xs
  {-# INLINE bifoldl #-}
  
instance Bitraversable ListCons where
  bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> ListCons a b -> f (ListCons c d)
bitraverse a -> f c
f b -> f d
g ListCons a b
Nil = ListCons c d -> f (ListCons c d)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons c d
forall a b. ListCons a b
Nil
  bitraverse a -> f c
f b -> f d
g (a
a :- b
b) = (c -> d -> ListCons c d) -> f c -> f d -> f (ListCons c d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 c -> d -> ListCons c d
forall a b. a -> b -> ListCons a b
(:-) (a -> f c
f a
a) (b -> f d
g b
b)
  {-# INLINE bitraverse #-}

instance Arbitrary2 ListCons where
  liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (ListCons a b)
liftArbitrary2 Gen a
xs Gen b
ys = (Int -> Gen (ListCons a b)) -> Gen (ListCons a b)
forall a. (Int -> Gen a) -> Gen a
sized (\Int
n -> [(Int, Gen (ListCons a b))] -> Gen (ListCons a b)
forall a. [(Int, Gen a)] -> Gen a
frequency ((Int
1, ListCons a b -> Gen (ListCons a b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons a b
forall a b. ListCons a b
Nil) (Int, Gen (ListCons a b))
-> [(Int, Gen (ListCons a b))] -> [(Int, Gen (ListCons a b))]
forall a. a -> [a] -> [a]
: [(Int
n, (a -> b -> ListCons a b) -> Gen a -> Gen b -> Gen (ListCons a b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> ListCons a b
forall a b. a -> b -> ListCons a b
(:-) Gen a
xs Gen b
ys) | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1]))
  liftShrink2 :: forall a b.
(a -> [a]) -> (b -> [b]) -> ListCons a b -> [ListCons a b]
liftShrink2 a -> [a]
xs b -> [b]
ys ListCons a b
Nil = []
  liftShrink2 a -> [a]
xs b -> [b]
ys (a
x :- b
y) = ListCons a b
forall a b. ListCons a b
Nil ListCons a b -> [ListCons a b] -> [ListCons a b]
forall a. a -> [a] -> [a]
: ((a, b) -> ListCons a b) -> [(a, b)] -> [ListCons a b]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> b -> ListCons a b) -> (a, b) -> ListCons a b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> ListCons a b
forall a b. a -> b -> ListCons a b
(:-)) ((a -> [a]) -> (b -> [b]) -> (a, b) -> [(a, b)]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
xs b -> [b]
ys (a
x,b
y))
  
instance Arbitrary a => Arbitrary1 (ListCons a) where
  liftArbitrary :: forall a. Gen a -> Gen (ListCons a a)
liftArbitrary = Gen a -> Gen a -> Gen (ListCons a a)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen a
forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> ListCons a a -> [ListCons a a]
liftShrink = (a -> [a]) -> (a -> [a]) -> ListCons a a -> [ListCons a a]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
forall a. Arbitrary a => a -> [a]
shrink
  
instance (Arbitrary a, Arbitrary b) => Arbitrary (ListCons a b) where
  arbitrary :: Gen (ListCons a b)
arbitrary = Gen (ListCons a b)
forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
Gen (f a b)
arbitrary2
  shrink :: ListCons a b -> [ListCons a b]
shrink = ListCons a b -> [ListCons a b]
forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
f a b -> [f a b]
shrink2

-- | A list monad transformer, "done right".
newtype ListT m a
  = ListT { forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT :: m (ListCons a (ListT m a)) }
  deriving (Typeable, (forall x. ListT m a -> Rep (ListT m a) x)
-> (forall x. Rep (ListT m a) x -> ListT m a)
-> Generic (ListT m a)
forall x. Rep (ListT m a) x -> ListT m a
forall x. ListT m a -> Rep (ListT m a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (m :: * -> *) a x. Rep (ListT m a) x -> ListT m a
forall (m :: * -> *) a x. ListT m a -> Rep (ListT m a) x
$cto :: forall (m :: * -> *) a x. Rep (ListT m a) x -> ListT m a
$cfrom :: forall (m :: * -> *) a x. ListT m a -> Rep (ListT m a) x
Generic)
  deriving (NonEmpty (ListT m a) -> ListT m a
ListT m a -> ListT m a -> ListT m a
(ListT m a -> ListT m a -> ListT m a)
-> (NonEmpty (ListT m a) -> ListT m a)
-> (forall b. Integral b => b -> ListT m a -> ListT m a)
-> Semigroup (ListT m a)
forall b. Integral b => b -> ListT m a -> ListT m a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall (m :: * -> *) a.
Monad m =>
NonEmpty (ListT m a) -> ListT m a
forall (m :: * -> *) a.
Monad m =>
ListT m a -> ListT m a -> ListT m a
forall (m :: * -> *) a b.
(Monad m, Integral b) =>
b -> ListT m a -> ListT m a
stimes :: forall b. Integral b => b -> ListT m a -> ListT m a
$cstimes :: forall (m :: * -> *) a b.
(Monad m, Integral b) =>
b -> ListT m a -> ListT m a
sconcat :: NonEmpty (ListT m a) -> ListT m a
$csconcat :: forall (m :: * -> *) a.
Monad m =>
NonEmpty (ListT m a) -> ListT m a
<> :: ListT m a -> ListT m a -> ListT m a
$c<> :: forall (m :: * -> *) a.
Monad m =>
ListT m a -> ListT m a -> ListT m a
Semigroup, Semigroup (ListT m a)
ListT m a
Semigroup (ListT m a)
-> ListT m a
-> (ListT m a -> ListT m a -> ListT m a)
-> ([ListT m a] -> ListT m a)
-> Monoid (ListT m a)
[ListT m a] -> ListT m a
ListT m a -> ListT m a -> ListT m a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall (m :: * -> *) a. Monad m => Semigroup (ListT m a)
forall (m :: * -> *) a. Monad m => ListT m a
forall (m :: * -> *) a. Monad m => [ListT m a] -> ListT m a
forall (m :: * -> *) a.
Monad m =>
ListT m a -> ListT m a -> ListT m a
mconcat :: [ListT m a] -> ListT m a
$cmconcat :: forall (m :: * -> *) a. Monad m => [ListT m a] -> ListT m a
mappend :: ListT m a -> ListT m a -> ListT m a
$cmappend :: forall (m :: * -> *) a.
Monad m =>
ListT m a -> ListT m a -> ListT m a
mempty :: ListT m a
$cmempty :: forall (m :: * -> *) a. Monad m => ListT m a
Monoid) via Alt (ListT m) a

deriving instance (forall x. Data x => Data (m x), Typeable m, Data a) => Data (ListT m a)

deriving newtype instance (forall x. NFData x => NFData (m x), NFData a) => NFData (ListT m a)

-- | Unfold, monadically, a list from a seed.
unfoldrM :: Functor m => (b -> m (Maybe (a, b))) -> b -> ListT m a
unfoldrM :: forall (m :: * -> *) b a.
Functor m =>
(b -> m (Maybe (a, b))) -> b -> ListT m a
unfoldrM b -> m (Maybe (a, b))
f = b -> ListT m a
go
  where
    go :: b -> ListT m a
go = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons a (ListT m a)) -> ListT m a)
-> (b -> m (ListCons a (ListT m a))) -> b -> ListT m a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (Maybe (a, b) -> ListCons a (ListT m a))
-> m (Maybe (a, b)) -> m (ListCons a (ListT m a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe (a, b) -> ListCons a (ListT m a)
h (m (Maybe (a, b)) -> m (ListCons a (ListT m a)))
-> (b -> m (Maybe (a, b))) -> b -> m (ListCons a (ListT m a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> m (Maybe (a, b))
f

    h :: Maybe (a, b) -> ListCons a (ListT m a)
h Maybe (a, b)
Nothing = ListCons a (ListT m a)
forall a b. ListCons a b
Nil
    h (Just (a
x, b
xs)) = a
x a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
:- b -> ListT m a
go b
xs
    {-# INLINE h #-}
{-# INLINE unfoldrM #-}

instance (forall x. Show x => Show (m x), Show a) => Show (ListT m a) where
  showsPrec :: Int -> ListT m a -> ShowS
showsPrec Int
n (ListT m (ListCons a (ListT m a))
xs) = Bool -> ShowS -> ShowS
showParen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (String -> ShowS
showString String
"ListT " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> m (ListCons a (ListT m a)) -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 m (ListCons a (ListT m a))
xs)
  
deriving instance (forall x. Eq x => Eq (m x), Eq a) => Eq (ListT m a)
deriving instance (forall x. Ord x => Ord (m x), Eq (ListT m a), Ord a) => Ord (ListT m a)

instance (Arbitrary1 m, Arbitrary a) => Arbitrary (ListT m a) where
  arbitrary :: Gen (ListT m a)
arbitrary = Gen (ListT m a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: ListT m a -> [ListT m a]
shrink = ListT m a -> [ListT m a]
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

instance Arbitrary1 m => Arbitrary1 (ListT m) where
  liftArbitrary :: forall a. Gen a -> Gen (ListT m a)
liftArbitrary Gen a
arb = (Int -> Gen (ListT m a)) -> Gen (ListT m a)
forall a. (Int -> Gen a) -> Gen a
sized Int -> Gen (ListT m a)
go
    where
      go :: Int -> Gen (ListT m a)
go Int
n
        | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons a (ListT m a)) -> ListT m a)
-> Gen (m (ListCons a (ListT m a))) -> Gen (ListT m a)
forall (f :: * -> *) a b.
Coercible (f a) (f b) =>
(a -> b) -> f a -> f b
<#$> Gen (ListCons a (ListT m a)) -> Gen (m (ListCons a (ListT m a)))
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary (ListCons a (ListT m a) -> Gen (ListCons a (ListT m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons a (ListT m a)
forall a b. ListCons a b
Nil)
        | Bool
otherwise = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons a (ListT m a)) -> ListT m a)
-> Gen (m (ListCons a (ListT m a))) -> Gen (ListT m a)
forall (f :: * -> *) a b.
Coercible (f a) (f b) =>
(a -> b) -> f a -> f b
<#$> Gen (ListCons a (ListT m a)) -> Gen (m (ListCons a (ListT m a)))
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary ((a -> ListT m a -> ListCons a (ListT m a))
-> Gen a -> Gen (ListT m a) -> Gen (ListCons a (ListT m a))
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
(:-) Gen a
arb (Int -> Gen (ListT m a)
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)))
  liftShrink :: forall a. (a -> [a]) -> ListT m a -> [ListT m a]
liftShrink a -> [a]
shr = (m (ListCons a (ListT m a)) -> ListT m a)
-> [m (ListCons a (ListT m a))] -> [ListT m a]
forall a b. (a -> b) -> [a] -> [b]
map m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT ([m (ListCons a (ListT m a))] -> [ListT m a])
-> (ListT m a -> [m (ListCons a (ListT m a))])
-> ListT m a
-> [ListT m a]
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. ((ListCons a (ListT m a) -> [ListCons a (ListT m a)])
-> m (ListCons a (ListT m a)) -> [m (ListCons a (ListT m a))]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink ListCons a (ListT m a) -> [ListCons a (ListT m a)]
f (m (ListCons a (ListT m a)) -> [m (ListCons a (ListT m a))])
-> (ListT m a -> m (ListCons a (ListT m a)))
-> ListT m a
-> [m (ListCons a (ListT m a))]
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT)
    where
      f :: ListCons a (ListT m a) -> [ListCons a (ListT m a)]
f ListCons a (ListT m a)
Nil       = []
      f (a
x :- ListT m a
xs) = ListCons a (ListT m a)
forall a b. ListCons a b
Nil ListCons a (ListT m a)
-> [ListCons a (ListT m a)] -> [ListCons a (ListT m a)]
forall a. a -> [a] -> [a]
: ((a, ListT m a) -> ListCons a (ListT m a))
-> [(a, ListT m a)] -> [ListCons a (ListT m a)]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> ListT m a -> ListCons a (ListT m a))
-> (a, ListT m a) -> ListCons a (ListT m a)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
(:-)) ((a -> [a])
-> (ListT m a -> [ListT m a]) -> (a, ListT m a) -> [(a, ListT m a)]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
shr ((a -> [a]) -> ListT m a -> [ListT m a]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) (a
x,ListT m a
xs))
  
instance Functor m => Functor (ListT m) where
  fmap :: forall a b. (a -> b) -> ListT m a -> ListT m b
fmap a -> b
f = m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons b (ListT m b)) -> ListT m b)
-> (ListT m a -> m (ListCons b (ListT m b)))
-> ListT m a
-> ListT m b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. ((ListCons a (ListT m a) -> ListCons b (ListT m b))
-> m (ListCons a (ListT m a)) -> m (ListCons b (ListT m b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b)
-> (ListT m a -> ListT m b)
-> ListCons a (ListT m a)
-> ListCons b (ListT m b)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f ((a -> b) -> ListT m a -> ListT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)) (m (ListCons a (ListT m a)) -> m (ListCons b (ListT m b)))
-> (ListT m a -> m (ListCons a (ListT m a)))
-> ListT m a
-> m (ListCons b (ListT m b))
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT)
  {-# INLINE fmap #-}
  
foldrListT :: Monad m => (a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT a -> m b -> m b
f m b
b = (m (ListCons a (ListT m a))
-> (ListCons a (ListT m a) -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ListCons a (ListT m a) -> m b
h) (m (ListCons a (ListT m a)) -> m b)
-> (ListT m a -> m (ListCons a (ListT m a))) -> ListT m a -> m b
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT
  where
    h :: ListCons a (ListT m a) -> m b
h ListCons a (ListT m a)
Nil = m b
b
    h (a
x :- ListT m (ListCons a (ListT m a))
xs) = a -> m b -> m b
f a
x (m (ListCons a (ListT m a))
xs m (ListCons a (ListT m a))
-> (ListCons a (ListT m a) -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ListCons a (ListT m a) -> m b
h)
{-# INLINE foldrListT #-}

instance Monad m => Applicative (ListT m) where
  pure :: forall a. a -> ListT m a
pure a
x = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (ListCons a (ListT m a) -> m (ListCons a (ListT m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
x a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
:- m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (ListCons a (ListT m a) -> m (ListCons a (ListT m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons a (ListT m a)
forall a b. ListCons a b
Nil)))
  {-# INLINE pure #-}
  liftA2 :: forall a b c. (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
liftA2 a -> b -> c
f ListT m a
xs ListT m b
ys = m (ListCons c (ListT m c)) -> ListT m c
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT ((a -> m (ListCons c (ListT m c)) -> m (ListCons c (ListT m c)))
-> m (ListCons c (ListT m c))
-> ListT m a
-> m (ListCons c (ListT m c))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT a -> m (ListCons c (ListT m c)) -> m (ListCons c (ListT m c))
g (ListCons c (ListT m c) -> m (ListCons c (ListT m c))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons c (ListT m c)
forall a b. ListCons a b
Nil) ListT m a
xs)
    where
      g :: a -> m (ListCons c (ListT m c)) -> m (ListCons c (ListT m c))
g a
x m (ListCons c (ListT m c))
xs = (b -> m (ListCons c (ListT m c)) -> m (ListCons c (ListT m c)))
-> m (ListCons c (ListT m c))
-> ListT m b
-> m (ListCons c (ListT m c))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT (a -> b -> m (ListCons c (ListT m c)) -> m (ListCons c (ListT m c))
h a
x) m (ListCons c (ListT m c))
xs ListT m b
ys
      h :: a -> b -> m (ListCons c (ListT m c)) -> m (ListCons c (ListT m c))
h a
x b
y m (ListCons c (ListT m c))
xs = ListCons c (ListT m c) -> m (ListCons c (ListT m c))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> b -> c
f a
x b
y c -> ListT m c -> ListCons c (ListT m c)
forall a b. a -> b -> ListCons a b
:- m (ListCons c (ListT m c)) -> ListT m c
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT m (ListCons c (ListT m c))
xs)
  {-# INLINE liftA2 #-}
  *> :: forall a b. ListT m a -> ListT m b -> ListT m b
(*>) = (a -> b -> b) -> ListT m a -> ListT m b -> ListT m b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((b -> b) -> a -> b -> b
forall a b. a -> b -> a
const b -> b
forall a. a -> a
id)
  {-# INLINE (*>) #-}
  <* :: forall a b. ListT m a -> ListT m b -> ListT m a
(<*) = (a -> b -> a) -> ListT m a -> ListT m b -> ListT m a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> a
forall a b. a -> b -> a
const
  {-# INLINE (<*) #-}
  
instance Monad m => Monad (ListT m) where
  ListT m a
xs >>= :: forall a b. ListT m a -> (a -> ListT m b) -> ListT m b
>>= a -> ListT m b
f = m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT ((a -> m (ListCons b (ListT m b)) -> m (ListCons b (ListT m b)))
-> m (ListCons b (ListT m b))
-> ListT m a
-> m (ListCons b (ListT m b))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT a -> m (ListCons b (ListT m b)) -> m (ListCons b (ListT m b))
g (ListCons b (ListT m b) -> m (ListCons b (ListT m b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons b (ListT m b)
forall a b. ListCons a b
Nil) ListT m a
xs)
    where
      g :: a -> m (ListCons b (ListT m b)) -> m (ListCons b (ListT m b))
g a
x m (ListCons b (ListT m b))
xs = ListT m b -> m (ListCons b (ListT m b))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT (a -> ListT m b
f a
x ListT m b -> ListT m b -> ListT m b
forall a. Semigroup a => a -> a -> a
<> m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT m (ListCons b (ListT m b))
xs)
  {-# INLINE (>>=) #-}
  >> :: forall a b. ListT m a -> ListT m b -> ListT m b
(>>) = ListT m a -> ListT m b -> ListT m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
  
instance Foldable m => Foldable (ListT m) where
  foldr :: forall a b. (a -> b -> b) -> b -> ListT m a -> b
foldr a -> b -> b
f = (ListT m a -> b -> b) -> b -> ListT m a -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip ListT m a -> b -> b
go
    where
      go :: ListT m a -> b -> b
go = (b -> m (ListCons a (ListT m a)) -> b)
-> m (ListCons a (ListT m a)) -> b -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((ListCons a (ListT m a) -> b -> b)
-> b -> m (ListCons a (ListT m a)) -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((b -> ListCons a (ListT m a) -> b)
-> ListCons a (ListT m a) -> b -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((a -> b -> b)
-> (ListT m a -> b -> b) -> b -> ListCons a (ListT m a) -> b
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr a -> b -> b
f ListT m a -> b -> b
go))) (m (ListCons a (ListT m a)) -> b -> b)
-> (ListT m a -> m (ListCons a (ListT m a))) -> ListT m a -> b -> b
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT
  {-# INLINE foldr #-}
  foldMap :: forall m a. Monoid m => (a -> m) -> ListT m a -> m
foldMap a -> m
f = ListT m a -> m
go
    where
      go :: ListT m a -> m
go = (ListCons a (ListT m a) -> m) -> m (ListCons a (ListT m a)) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> m) -> (ListT m a -> m) -> ListCons a (ListT m a) -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> m
f ListT m a -> m
go) (m (ListCons a (ListT m a)) -> m)
-> (ListT m a -> m (ListCons a (ListT m a))) -> ListT m a -> m
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT
  {-# INLINE foldMap #-}

instance Traversable m => Traversable (ListT m) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ListT m a -> f (ListT m b)
traverse a -> f b
f = (m (ListCons b (ListT m b)) -> ListT m b)
-> f (m (ListCons b (ListT m b))) -> f (ListT m b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (f (m (ListCons b (ListT m b))) -> f (ListT m b))
-> (ListT m a -> f (m (ListCons b (ListT m b))))
-> ListT m a
-> f (ListT m b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ListCons a (ListT m a) -> f (ListCons b (ListT m b)))
-> m (ListCons a (ListT m a)) -> f (m (ListCons b (ListT m b)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ListCons a (ListT m a) -> f (ListCons b (ListT m b))
h (m (ListCons a (ListT m a)) -> f (m (ListCons b (ListT m b))))
-> (ListT m a -> m (ListCons a (ListT m a)))
-> ListT m a
-> f (m (ListCons b (ListT m b)))
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT)
    where
      h :: ListCons a (ListT m a) -> f (ListCons b (ListT m b))
h ListCons a (ListT m a)
Nil = ListCons b (ListT m b) -> f (ListCons b (ListT m b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons b (ListT m b)
forall a b. ListCons a b
Nil
      h (a
x :- ListT m (ListCons a (ListT m a))
xs) = (b -> m (ListCons b (ListT m b)) -> ListCons b (ListT m b))
-> f b
-> f (m (ListCons b (ListT m b)))
-> f (ListCons b (ListT m b))
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (((ListT m b -> ListCons b (ListT m b))
-> (m (ListCons b (ListT m b)) -> ListT m b)
-> m (ListCons b (ListT m b))
-> ListCons b (ListT m b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT) ((ListT m b -> ListCons b (ListT m b))
 -> m (ListCons b (ListT m b)) -> ListCons b (ListT m b))
-> (b -> ListT m b -> ListCons b (ListT m b))
-> b
-> m (ListCons b (ListT m b))
-> ListCons b (ListT m b)
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. b -> ListT m b -> ListCons b (ListT m b)
forall a b. a -> b -> ListCons a b
(:-)) (a -> f b
f a
x) ((ListCons a (ListT m a) -> f (ListCons b (ListT m b)))
-> m (ListCons a (ListT m a)) -> f (m (ListCons b (ListT m b)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ListCons a (ListT m a) -> f (ListCons b (ListT m b))
h m (ListCons a (ListT m a))
xs)
  {-# INLINE traverse #-}

-- | Flatten all of the effects in the list and collect the results.
toListT :: Monad m => ListT m a -> m [a]
toListT :: forall (m :: * -> *) a. Monad m => ListT m a -> m [a]
toListT = (a -> m [a] -> m [a]) -> m [a] -> ListT m a -> m [a]
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT (([a] -> [a]) -> m [a] -> m [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([a] -> [a]) -> m [a] -> m [a])
-> (a -> [a] -> [a]) -> a -> m [a] -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:)) ([a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
{-# INLINE toListT #-}

instance Monad m => Alternative (ListT m) where
  <|> :: forall a. ListT m a -> ListT m a -> ListT m a
(<|>) = (forall {a}.
(ListT m a
 -> m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
-> ListT m a -> ListT m a -> ListT m a
coerce :: (ListT m a -> m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
                  -> (ListT m a -> ListT m a -> ListT m a))
          ((m (ListCons a (ListT m a))
 -> ListT m a -> m (ListCons a (ListT m a)))
-> ListT m a
-> m (ListCons a (ListT m a))
-> m (ListCons a (ListT m a))
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((a -> m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
-> m (ListCons a (ListT m a))
-> ListT m a
-> m (ListCons a (ListT m a))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT (\a
z m (ListCons a (ListT m a))
zs -> ListCons a (ListT m a) -> m (ListCons a (ListT m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
z a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
:- m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT m (ListCons a (ListT m a))
zs))))
  {-# INLINE (<|>) #-}
  empty :: forall a. ListT m a
empty = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (ListCons a (ListT m a) -> m (ListCons a (ListT m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons a (ListT m a)
forall a b. ListCons a b
Nil)
  {-# INLINE empty #-}
  
instance Monad m => MonadPlus (ListT m)

instance MonadTrans ListT where
  lift :: forall (m :: * -> *) a. Monad m => m a -> ListT m a
lift = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons a (ListT m a)) -> ListT m a)
-> (m a -> m (ListCons a (ListT m a))) -> m a -> ListT m a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> ListCons a (ListT m a)) -> m a -> m (ListCons a (ListT m a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
:- m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (ListCons a (ListT m a) -> m (ListCons a (ListT m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons a (ListT m a)
forall a b. ListCons a b
Nil))
  {-# INLINE lift #-}
  
-- | Apply a function to every effect layered in the list transformer.
listMmap :: Functor m
         => (m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
         -> ListT m a -> ListT n b
listMmap :: forall (m :: * -> *) a (n :: * -> *) b.
Functor m =>
(m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
-> ListT m a -> ListT n b
listMmap m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b))
f = ListT m a -> ListT n b
go
  where
    go :: ListT m a -> ListT n b
go = n (ListCons b (ListT n b)) -> ListT n b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (n (ListCons b (ListT n b)) -> ListT n b)
-> (ListT m a -> n (ListCons b (ListT n b)))
-> ListT m a
-> ListT n b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b))
f (m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
-> (ListT m a -> m (ListCons a (ListT n b)))
-> ListT m a
-> n (ListCons b (ListT n b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ListCons a (ListT m a) -> ListCons a (ListT n b))
-> m (ListCons a (ListT m a)) -> m (ListCons a (ListT n b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ListT m a -> ListT n b)
-> ListCons a (ListT m a) -> ListCons a (ListT n b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ListT m a -> ListT n b
go) (m (ListCons a (ListT m a)) -> m (ListCons a (ListT n b)))
-> (ListT m a -> m (ListCons a (ListT m a)))
-> ListT m a
-> m (ListCons a (ListT n b))
forall a b c. Coercible a b => (b -> c) -> (a -> b) -> a -> c
.# ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT)
{-# INLINE listMmap #-}

instance MonadState s m => MonadState s (ListT m) where
  get :: ListT m s
get = m s -> ListT m s
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 = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ListT m ()) -> (s -> m ()) -> s -> ListT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put
  state :: forall a. (s -> (a, s)) -> ListT m a
state = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a)
-> ((s -> (a, s)) -> m a) -> (s -> (a, s)) -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
  {-# INLINE get #-}
  {-# INLINE put #-}
  {-# INLINE state #-}
  
instance MonadError e m => MonadError e (ListT m) where
  throwError :: forall a. e -> ListT m a
throwError = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> (e -> m a) -> e -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
  {-# INLINE throwError #-}
  catchError :: forall a. ListT m a -> (e -> ListT m a) -> ListT m a
catchError ListT m a
xs e -> ListT m a
h = (m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
-> ListT m a -> ListT m a
forall (m :: * -> *) a (n :: * -> *) b.
Functor m =>
(m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
-> ListT m a -> ListT n b
listMmap (m (ListCons a (ListT m a))
-> (e -> m (ListCons a (ListT m a))) -> m (ListCons a (ListT m a))
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` (ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT (ListT m a -> m (ListCons a (ListT m a)))
-> (e -> ListT m a) -> e -> m (ListCons a (ListT m a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ListT m a
h)) ListT m a
xs
  {-# INLINE catchError #-}
  
instance MonadReader r m => MonadReader r (ListT m) where
  ask :: ListT m r
ask = m r -> ListT m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m r
forall r (m :: * -> *). MonadReader r m => m r
ask
  {-# INLINE ask #-}
  reader :: forall a. (r -> a) -> ListT m a
reader = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> ((r -> a) -> m a) -> (r -> a) -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> a) -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
  {-# INLINE reader #-}
  local :: forall a. (r -> r) -> ListT m a -> ListT m a
local = (m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
-> ListT m a -> ListT m a
forall (m :: * -> *) a (n :: * -> *) b.
Functor m =>
(m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
-> ListT m a -> ListT n b
listMmap ((m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
 -> ListT m a -> ListT m a)
-> ((r -> r)
    -> m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a)))
-> (r -> r)
-> ListT m a
-> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> r)
-> m (ListCons a (ListT m a)) -> m (ListCons a (ListT m a))
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
  {-# INLINE local #-}
  
instance MonadWriter w m => MonadWriter w (ListT m) where
  writer :: forall a. (a, w) -> ListT m a
writer = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> ((a, w) -> m a) -> (a, w) -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, w) -> m a
forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
  {-# INLINE writer #-}
  tell :: w -> ListT m ()
tell = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ListT m ()) -> (w -> m ()) -> w -> ListT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
  {-# INLINE tell #-}
  listen :: forall a. ListT m a -> ListT m (a, w)
listen = (m (ListCons a (ListT m (a, w)))
 -> m (ListCons (a, w) (ListT m (a, w))))
-> ListT m a -> ListT m (a, w)
forall (m :: * -> *) a (n :: * -> *) b.
Functor m =>
(m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
-> ListT m a -> ListT n b
listMmap (((ListCons a (ListT m (a, w)), w)
 -> ListCons (a, w) (ListT m (a, w)))
-> m (ListCons a (ListT m (a, w)), w)
-> m (ListCons (a, w) (ListT m (a, w)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ListCons a (ListT m (a, w))
 -> w -> ListCons (a, w) (ListT m (a, w)))
-> (ListCons a (ListT m (a, w)), w)
-> ListCons (a, w) (ListT m (a, w))
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((w
 -> ListCons a (ListT m (a, w)) -> ListCons (a, w) (ListT m (a, w)))
-> ListCons a (ListT m (a, w))
-> w
-> ListCons (a, w) (ListT m (a, w))
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((a -> (a, w))
-> ListCons a (ListT m (a, w)) -> ListCons (a, w) (ListT m (a, w))
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((a -> (a, w))
 -> ListCons a (ListT m (a, w)) -> ListCons (a, w) (ListT m (a, w)))
-> (w -> a -> (a, w))
-> w
-> ListCons a (ListT m (a, w))
-> ListCons (a, w) (ListT m (a, w))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> w -> (a, w)) -> w -> a -> (a, w)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)))) (m (ListCons a (ListT m (a, w)), w)
 -> m (ListCons (a, w) (ListT m (a, w))))
-> (m (ListCons a (ListT m (a, w)))
    -> m (ListCons a (ListT m (a, w)), w))
-> m (ListCons a (ListT m (a, w)))
-> m (ListCons (a, w) (ListT m (a, w)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (ListCons a (ListT m (a, w)))
-> m (ListCons a (ListT m (a, w)), w)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen)
  {-# INLINE listen #-}
  pass :: forall a. ListT m (a, w -> w) -> ListT m a
pass = (m (ListCons (a, w -> w) (ListT m a))
 -> m (ListCons a (ListT m a)))
-> ListT m (a, w -> w) -> ListT m a
forall (m :: * -> *) a (n :: * -> *) b.
Functor m =>
(m (ListCons a (ListT n b)) -> n (ListCons b (ListT n b)))
-> ListT m a -> ListT n b
listMmap (m (ListCons a (ListT m a), w -> w) -> m (ListCons a (ListT m a))
forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass (m (ListCons a (ListT m a), w -> w) -> m (ListCons a (ListT m a)))
-> (m (ListCons (a, w -> w) (ListT m a))
    -> m (ListCons a (ListT m a), w -> w))
-> m (ListCons (a, w -> w) (ListT m a))
-> m (ListCons a (ListT m a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ListCons (a, w -> w) (ListT m a)
 -> (ListCons a (ListT m a), w -> w))
-> m (ListCons (a, w -> w) (ListT m a))
-> m (ListCons a (ListT m a), w -> w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ListCons (a, w -> w) (ListT m a)
-> (ListCons a (ListT m a), w -> w)
forall {a} {a} {b}.
ListCons (a, a -> a) b -> (ListCons a b, a -> a)
h)
    where
      h :: ListCons (a, a -> a) b -> (ListCons a b, a -> a)
h ListCons (a, a -> a) b
Nil = (ListCons a b
forall a b. ListCons a b
Nil, a -> a
forall a. a -> a
id)
      h ((a
x,a -> a
f):-b
xs) = (a
x a -> b -> ListCons a b
forall a b. a -> b -> ListCons a b
:- b
xs , a -> a
f)
  {-# INLINE pass #-}

instance MonadCont m => MonadCont (ListT m) where
  callCC :: forall a b. ((a -> ListT m b) -> ListT m a) -> ListT m a
callCC (a -> ListT m b) -> ListT m a
f = m (ListCons a (ListT m a)) -> ListT m a
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (((ListCons a (ListT m a) -> m (ListCons b (ListT m b)))
 -> m (ListCons a (ListT m a)))
-> m (ListCons a (ListT m a))
forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
callCC (\ListCons a (ListT m a) -> m (ListCons b (ListT m b))
c -> ListT m a -> m (ListCons a (ListT m a))
forall (m :: * -> *) a. ListT m a -> m (ListCons a (ListT m a))
runListT ((a -> ListT m b) -> ListT m a
f (m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons b (ListT m b)) -> ListT m b)
-> (a -> m (ListCons b (ListT m b))) -> a -> ListT m b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. ListCons a (ListT m a) -> m (ListCons b (ListT m b))
c (ListCons a (ListT m a) -> m (ListCons b (ListT m b)))
-> (a -> ListCons a (ListT m a)) -> a -> m (ListCons b (ListT m b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> ListT m a -> ListCons a (ListT m a)
forall a b. a -> b -> ListCons a b
:- ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty)))))
  {-# INLINE callCC #-}

-- | Filter the list. An analogue of mapMaybe on lists.
catMaybesT :: Monad m => (a -> Maybe b) -> ListT m a -> ListT m b
catMaybesT :: forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> ListT m a -> ListT m b
catMaybesT a -> Maybe b
f = m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT (m (ListCons b (ListT m b)) -> ListT m b)
-> (ListT m a -> m (ListCons b (ListT m b)))
-> ListT m a
-> ListT m b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> m (ListCons b (ListT m b)) -> m (ListCons b (ListT m b)))
-> m (ListCons b (ListT m b))
-> ListT m a
-> m (ListCons b (ListT m b))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> ListT m a -> m b
foldrListT (\a
x m (ListCons b (ListT m b))
xs -> m (ListCons b (ListT m b))
-> (b -> m (ListCons b (ListT m b)))
-> Maybe b
-> m (ListCons b (ListT m b))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m (ListCons b (ListT m b))
xs (ListCons b (ListT m b) -> m (ListCons b (ListT m b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ListCons b (ListT m b) -> m (ListCons b (ListT m b)))
-> (b -> ListCons b (ListT m b)) -> b -> m (ListCons b (ListT m b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> ListT m b -> ListCons b (ListT m b)
forall a b. a -> b -> ListCons a b
:- m (ListCons b (ListT m b)) -> ListT m b
forall (m :: * -> *) a. m (ListCons a (ListT m a)) -> ListT m a
ListT m (ListCons b (ListT m b))
xs)) (a -> Maybe b
f a
x)) (ListCons b (ListT m b) -> m (ListCons b (ListT m b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListCons b (ListT m b)
forall a b. ListCons a b
Nil)
{-# INLINE catMaybesT #-}