{- |
Copyright   :  (c) Henning Thielemann 2007-2010

Maintainer  :  haskell@henning-thielemann.de
Stability   :  stable
Portability :  Haskell 98

Lists of elements of alternating type.
This module is based on the standard list type
and may benefit from list optimizations.
-}
module Data.AlternatingList.List.Uniform
   (T(Cons),
    map, mapFirst, mapSecond,
    zipWithFirst, zipWithSecond,
    concatMonoid, concatMapMonoid,
    sequence, sequence_,
    traverse, traverse_, traverseFirst, traverseSecond,
    getFirsts, getSeconds, length, genericLength,
    fromFirstList, fromSecondList, fromEitherList,
    singleton, isSingleton,
    cons, snoc, reverse,
    mapSecondHead, forceSecondHead,
    foldr, foldl,
    format,
    filterFirst, partitionFirst, partitionMaybeFirst,
    partitionEitherFirst, unzipEitherFirst,
    filterSecond, partitionSecond, partitionMaybeSecond,
    partitionEitherSecond, unzipEitherSecond,

    catMaybesFirst, catMaybesSecond,
   ) where

import qualified Data.AlternatingList.List.Disparate as Disp

import qualified Control.Monad as Monad
import qualified Control.Applicative as Applicative
import qualified Data.List as List

import Control.Applicative (Applicative, pure, )
import Data.Monoid (Monoid, mempty, mappend, )

import Test.QuickCheck (Arbitrary(arbitrary, shrink))

import Text.Show (Show, ShowS, showsPrec, showParen, showString, )
import Data.Function (id, flip, (.), ($), )
import Data.Tuple.HT (mapFst, mapSnd, mapPair, )
import Data.Maybe.HT (toMaybe, )
import Data.Functor (fmap, )
import Data.Either (Either(Left, Right), either, )
import Data.Maybe (Maybe, maybe, )
import Data.Tuple (uncurry, )
import Data.Ord (Ord, (>=), )
{- this way we cannot access (:) in Hugs -}
import Prelude (Integral, Int, String, Bool, error, Eq, )


{- |
The constructor is only exported for use in "Data.AlternatingList.List.Mixed".
-}
data T a b = Cons {
   forall a b. T a b -> b
_lead :: b,
   forall a b. T a b -> T a b
disp  :: Disp.T a b
   }
   deriving (T a b -> T a b -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq b, Eq a) => T a b -> T a b -> Bool
/= :: T a b -> T a b -> Bool
$c/= :: forall a b. (Eq b, Eq a) => T a b -> T a b -> Bool
== :: T a b -> T a b -> Bool
$c== :: forall a b. (Eq b, Eq a) => T a b -> T a b -> Bool
Eq, T a b -> T a b -> Bool
T a b -> T a b -> Ordering
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 b, Ord a) => Eq (T a b)
forall a b. (Ord b, Ord a) => T a b -> T a b -> Bool
forall a b. (Ord b, Ord a) => T a b -> T a b -> Ordering
forall a b. (Ord b, Ord a) => T a b -> T a b -> T a b
min :: T a b -> T a b -> T a b
$cmin :: forall a b. (Ord b, Ord a) => T a b -> T a b -> T a b
max :: T a b -> T a b -> T a b
$cmax :: forall a b. (Ord b, Ord a) => T a b -> T a b -> T a b
>= :: T a b -> T a b -> Bool
$c>= :: forall a b. (Ord b, Ord a) => T a b -> T a b -> Bool
> :: T a b -> T a b -> Bool
$c> :: forall a b. (Ord b, Ord a) => T a b -> T a b -> Bool
<= :: T a b -> T a b -> Bool
$c<= :: forall a b. (Ord b, Ord a) => T a b -> T a b -> Bool
< :: T a b -> T a b -> Bool
$c< :: forall a b. (Ord b, Ord a) => T a b -> T a b -> Bool
compare :: T a b -> T a b -> Ordering
$ccompare :: forall a b. (Ord b, Ord a) => T a b -> T a b -> Ordering
Ord)


format :: (Show a, Show b) =>
   String -> String -> Int -> T a b -> ShowS
format :: forall a b.
(Show a, Show b) =>
String -> String -> Int -> T a b -> ShowS
format String
first String
second Int
p T a b
xs =
   Bool -> ShowS -> ShowS
showParen (Int
pforall a. Ord a => a -> a -> Bool
>=Int
5) forall a b. (a -> b) -> a -> b
$
   forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr
      (\a
a -> forall a. Show a => Int -> a -> ShowS
showsPrec Int
5 a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
first)
      (\b
b -> forall a. Show a => Int -> a -> ShowS
showsPrec Int
5 b
b forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
second))
      T a b
xs forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      String -> ShowS
showString String
"empty"

instance (Show a, Show b) => Show (T a b) where
   showsPrec :: Int -> T a b -> ShowS
showsPrec = forall a b.
(Show a, Show b) =>
String -> String -> Int -> T a b -> ShowS
format String
" /. " String
" ./ "


instance (Arbitrary a, Arbitrary b) =>
             Arbitrary (T a b) where
   arbitrary :: Gen (T a b)
arbitrary = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
Monad.liftM2 forall a b. b -> T a b -> T a b
Cons forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary
   shrink :: T a b -> [T a b]
shrink (Cons b
x T a b
xs) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. b -> T a b -> T a b
Cons) forall a b. (a -> b) -> a -> b
$ forall a. Arbitrary a => a -> [a]
shrink (b
x,T a b
xs)



map :: (a0 -> a1) -> (b0 -> b1) -> T a0 b0 -> T a1 b1
map :: forall a0 a1 b0 b1. (a0 -> a1) -> (b0 -> b1) -> T a0 b0 -> T a1 b1
map a0 -> a1
f b0 -> b1
g (Cons b0
b T a0 b0
xs) = forall a b. b -> T a b -> T a b
Cons (b0 -> b1
g b0
b) (forall a0 a1 b0 b1. (a0 -> a1) -> (b0 -> b1) -> T a0 b0 -> T a1 b1
Disp.map a0 -> a1
f b0 -> b1
g T a0 b0
xs)

mapFirst :: (a0 -> a1) -> T a0 b -> T a1 b
mapFirst :: forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
mapFirst a0 -> a1
f (Cons b
b T a0 b
xs) = forall a b. b -> T a b -> T a b
Cons b
b (forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
Disp.mapFirst a0 -> a1
f T a0 b
xs)

mapSecond :: (b0 -> b1) -> T a b0 -> T a b1
mapSecond :: forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
mapSecond b0 -> b1
g (Cons b0
b T a b0
xs) = forall a b. b -> T a b -> T a b
Cons (b0 -> b1
g b0
b) (forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
Disp.mapSecond b0 -> b1
g T a b0
xs)


zipWithFirst :: (a0 -> a1 -> a2) -> [a0] -> T a1 b -> T a2 b
zipWithFirst :: forall a0 a1 a2 b. (a0 -> a1 -> a2) -> [a0] -> T a1 b -> T a2 b
zipWithFirst a0 -> a1 -> a2
f [a0]
xs (Cons b
a T a1 b
bs) =
   forall a b. b -> T a b -> T a b
Cons b
a forall a b. (a -> b) -> a -> b
$ forall a0 a1 a2 b. (a0 -> a1 -> a2) -> [a0] -> T a1 b -> T a2 b
Disp.zipWithFirst a0 -> a1 -> a2
f [a0]
xs T a1 b
bs

zipWithSecond :: (b0 -> b1 -> b2) -> (b0,[b0]) -> T a b1 -> T a b2
zipWithSecond :: forall b0 b1 b2 a.
(b0 -> b1 -> b2) -> (b0, [b0]) -> T a b1 -> T a b2
zipWithSecond b0 -> b1 -> b2
f (b0
x,[b0]
xs) (Cons b1
a T a b1
bs) =
   forall a b. b -> T a b -> T a b
Cons (b0 -> b1 -> b2
f b0
x b1
a) forall a b. (a -> b) -> a -> b
$ forall b0 b1 b2 a. (b0 -> b1 -> b2) -> [b0] -> T a b1 -> T a b2
Disp.zipWithSecond b0 -> b1 -> b2
f [b0]
xs T a b1
bs



concatMonoid :: Monoid m =>
   T m m -> m
concatMonoid :: forall m. Monoid m => T m m -> m
concatMonoid =
   forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr forall a. Monoid a => a -> a -> a
mappend forall a. Monoid a => a -> a -> a
mappend forall a. Monoid a => a
mempty

concatMapMonoid :: Monoid m =>
   (time -> m) ->
   (body -> m) ->
   T time body -> m
concatMapMonoid :: forall m time body.
Monoid m =>
(time -> m) -> (body -> m) -> T time body -> m
concatMapMonoid time -> m
f body -> m
g =
   forall m. Monoid m => T m m -> m
concatMonoid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a0 a1 b0 b1. (a0 -> a1) -> (b0 -> b1) -> T a0 b0 -> T a1 b1
map time -> m
f body -> m
g


sequence :: Applicative m =>
   T (m a) (m b) -> m (T a b)
sequence :: forall (m :: * -> *) a b.
Applicative m =>
T (m a) (m b) -> m (T a b)
sequence (Cons m b
b T (m a) (m b)
xs) =
   forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
Applicative.liftA2 forall a b. b -> T a b -> T a b
Cons m b
b (forall (m :: * -> *) a b.
Applicative m =>
T (m a) (m b) -> m (T a b)
Disp.sequence T (m a) (m b)
xs)

sequence_ :: (Applicative m, Monoid d) =>
   T (m d) (m d) -> m d
sequence_ :: forall (m :: * -> *) d.
(Applicative m, Monoid d) =>
T (m d) (m d) -> m d
sequence_ (Cons m d
b T (m d) (m d)
xs) =
   forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
Applicative.liftA2 forall a. Monoid a => a -> a -> a
mappend m d
b forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) d.
(Applicative m, Monoid d) =>
T (m d) (m d) -> m d
Disp.sequence_ T (m d) (m d)
xs


traverse :: Applicative m =>
   (a0 -> m a1) -> (b0 -> m b1) ->
   T a0 b0 -> m (T a1 b1)
traverse :: forall (m :: * -> *) a0 a1 b0 b1.
Applicative m =>
(a0 -> m a1) -> (b0 -> m b1) -> T a0 b0 -> m (T a1 b1)
traverse a0 -> m a1
aAction b0 -> m b1
bAction =
   forall (m :: * -> *) a b.
Applicative m =>
T (m a) (m b) -> m (T a b)
sequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a0 a1 b0 b1. (a0 -> a1) -> (b0 -> b1) -> T a0 b0 -> T a1 b1
map a0 -> m a1
aAction b0 -> m b1
bAction

traverse_ :: (Applicative m, Monoid d) =>
   (a -> m d) -> (b -> m d) -> T a b -> m d
traverse_ :: forall (m :: * -> *) d a b.
(Applicative m, Monoid d) =>
(a -> m d) -> (b -> m d) -> T a b -> m d
traverse_ a -> m d
aAction b -> m d
bAction =
   forall (m :: * -> *) d.
(Applicative m, Monoid d) =>
T (m d) (m d) -> m d
sequence_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a0 a1 b0 b1. (a0 -> a1) -> (b0 -> b1) -> T a0 b0 -> T a1 b1
map a -> m d
aAction b -> m d
bAction


traverseFirst :: Applicative m =>
   (a0 -> m a1) -> T a0 b -> m (T a1 b)
traverseFirst :: forall (m :: * -> *) a0 a1 b.
Applicative m =>
(a0 -> m a1) -> T a0 b -> m (T a1 b)
traverseFirst a0 -> m a1
aAction =
   forall (m :: * -> *) a0 a1 b0 b1.
Applicative m =>
(a0 -> m a1) -> (b0 -> m b1) -> T a0 b0 -> m (T a1 b1)
traverse a0 -> m a1
aAction forall (f :: * -> *) a. Applicative f => a -> f a
pure

traverseSecond :: Applicative m =>
   (b0 -> m b1) -> T a b0 -> m (T a b1)
traverseSecond :: forall (m :: * -> *) b0 b1 a.
Applicative m =>
(b0 -> m b1) -> T a b0 -> m (T a b1)
traverseSecond b0 -> m b1
bAction =
   forall (m :: * -> *) a0 a1 b0 b1.
Applicative m =>
(a0 -> m a1) -> (b0 -> m b1) -> T a0 b0 -> m (T a1 b1)
traverse forall (f :: * -> *) a. Applicative f => a -> f a
pure b0 -> m b1
bAction


getFirsts :: T a b -> [a]
getFirsts :: forall a b. T a b -> [a]
getFirsts = forall a b. T a b -> [a]
Disp.getFirsts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a b -> T a b
disp

getSeconds :: T a b -> [b]
getSeconds :: forall a b. T a b -> [b]
getSeconds (Cons b
b T a b
xs) = b
b forall a. a -> [a] -> [a]
: forall a b. T a b -> [b]
Disp.getSeconds T a b
xs

length :: T a b -> Int
length :: forall a b. T a b -> Int
length = forall (t :: * -> *) a. Foldable t => t a -> Int
List.length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a b -> [a]
getFirsts

genericLength :: Integral i => T a b -> i
genericLength :: forall i a b. Integral i => T a b -> i
genericLength = forall i a. Num i => [a] -> i
List.genericLength forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a b -> [a]
getFirsts


fromFirstList :: b -> [a] -> T a b
fromFirstList :: forall b a. b -> [a] -> T a b
fromFirstList b
b [a]
as =
   forall a b. b -> T a b -> T a b
Cons b
b (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> T a b -> T a b
Disp.cons b
b) forall a b. T a b
Disp.empty [a]
as)

fromSecondList :: a -> [b] -> T a b
fromSecondList :: forall a b. a -> [b] -> T a b
fromSecondList a
a (b
b:[b]
bs) =
   forall a b. b -> T a b -> T a b
Cons b
b (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr (forall a b. a -> b -> T a b -> T a b
Disp.cons a
a) forall a b. T a b
Disp.empty [b]
bs)
fromSecondList a
_ [] = forall a. HasCallStack => String -> a
error String
"fromSecondList: empty list"

fromEitherList :: [Either a b] -> T a [b]
fromEitherList :: forall a b. [Either a b] -> T a [b]
fromEitherList =
   forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr
      (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
         (forall b a. b -> a -> T a b -> T a b
cons [])
         (forall b a. (b -> b) -> T a b -> T a b
mapSecondHead forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:)))
      (forall b a. b -> T a b
singleton [])


singleton :: b -> T a b
singleton :: forall b a. b -> T a b
singleton b
b = forall a b. b -> T a b -> T a b
Cons b
b forall a b. T a b
Disp.empty

isSingleton :: T a b -> Bool
isSingleton :: forall a b. T a b -> Bool
isSingleton = forall a b. T a b -> Bool
Disp.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a b -> T a b
disp


cons :: b -> a -> T a b -> T a b
cons :: forall b a. b -> a -> T a b -> T a b
cons b
b0 a
a ~(Cons b
b1 T a b
xs) = forall a b. b -> T a b -> T a b
Cons b
b0 (forall a b. a -> b -> T a b -> T a b
Disp.cons a
a b
b1 T a b
xs)


snoc :: T a b -> a -> b -> T a b
snoc :: forall a b. T a b -> a -> b -> T a b
snoc (Cons b
b0 T a b
xs) a
a b
b1 = forall a b. b -> T a b -> T a b
Cons b
b0 (forall a b. T a b -> a -> b -> T a b
Disp.snoc T a b
xs a
a b
b1)


mapSecondHead :: (b -> b) -> T a b -> T a b
mapSecondHead :: forall b a. (b -> b) -> T a b -> T a b
mapSecondHead b -> b
f ~(Cons b
b T a b
xs) = forall a b. b -> T a b -> T a b
Cons (b -> b
f b
b) T a b
xs

forceSecondHead :: T a b -> T a b
forceSecondHead :: forall a b. T a b -> T a b
forceSecondHead = forall b a. (b -> b) -> T a b -> T a b
mapSecondHead forall a. a -> a
id



foldr :: (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr :: forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr a -> c -> d
f b -> d -> c
g d
d (Cons b
b T a b
xs) = b -> d -> c
g b
b forall a b. (a -> b) -> a -> b
$ forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> d
Disp.foldr a -> c -> d
f b -> d -> c
g d
d T a b
xs
{-
The lazy pattern match leads to a space leak in synthesizer-alsa:testArrangeSpaceLeak
I would like to reproduce this in a small test,
but I did not achieve this so far.
-}
-- foldr f g d ~(Cons b xs) = g b $ Disp.foldr f g d xs

foldl :: (c -> a -> d) -> (d -> b -> c) -> d -> T a b -> c
foldl :: forall c a d b. (c -> a -> d) -> (d -> b -> c) -> d -> T a b -> c
foldl c -> a -> d
f d -> b -> c
g d
d0 T a b
xs =
   forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr (\a
a d -> c
go c
c -> d -> c
go (c -> a -> d
f c
c a
a)) (\b
b c -> c
go d
d -> c -> c
go (d -> b -> c
g d
d b
b)) forall a. a -> a
id T a b
xs d
d0

-- for a nicer implementation see Mixed
reverse :: T a b -> T a b
reverse :: forall a b. T a b -> T a b
reverse =
   forall c a d b. (c -> a -> d) -> (d -> b -> c) -> d -> T a b -> c
foldl (\ ~(Cons b
a T a b
xs) a
b -> forall a b. a -> b -> T a b -> T a b
Disp.cons a
b b
a T a b
xs) (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. b -> T a b -> T a b
Cons) forall a b. T a b
Disp.empty


filterFirst :: (a -> Bool) -> T a b -> T a [b]
filterFirst :: forall a b. (a -> Bool) -> T a b -> T a [b]
filterFirst a -> Bool
p =
   forall a b. T (Maybe a) b -> T a [b]
catMaybesFirst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
mapFirst (\a
a -> forall a. Bool -> a -> Maybe a
toMaybe (a -> Bool
p a
a) a
a)

filterSecond :: (b -> Bool) -> T a b -> T b [a]
filterSecond :: forall b a. (b -> Bool) -> T a b -> T b [a]
filterSecond b -> Bool
p =
   forall a b. T a (Maybe b) -> T b [a]
catMaybesSecond forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
mapSecond (\b
a -> forall a. Bool -> a -> Maybe a
toMaybe (b -> Bool
p b
a) b
a)

partitionFirst :: (a -> Bool) -> T a b -> (T a [b], T a [b])
partitionFirst :: forall a b. (a -> Bool) -> T a b -> (T a [b], T a [b])
partitionFirst a -> Bool
p =
   forall a0 a1 b. T (Either a0 a1) b -> (T a0 [b], T a1 [b])
unzipEitherFirst forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
mapFirst (\a
a -> if a -> Bool
p a
a then forall a b. a -> Either a b
Left a
a else forall a b. b -> Either a b
Right a
a)

partitionSecond :: (b -> Bool) -> T a b -> (T b [a], T b [a])
partitionSecond :: forall b a. (b -> Bool) -> T a b -> (T b [a], T b [a])
partitionSecond b -> Bool
p =
   forall a b0 b1. T a (Either b0 b1) -> (T b0 [a], T b1 [a])
unzipEitherSecond forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
mapSecond (\b
b -> if b -> Bool
p b
b then forall a b. a -> Either a b
Left b
b else forall a b. b -> Either a b
Right b
b)

partitionMaybeFirst :: (a0 -> Maybe a1) -> T a0 b -> (T a1 [b], T a0 [b])
partitionMaybeFirst :: forall a0 a1 b. (a0 -> Maybe a1) -> T a0 b -> (T a1 [b], T a0 [b])
partitionMaybeFirst a0 -> Maybe a1
f =
   forall a0 a1 b. T (Either a0 a1) b -> (T a0 [b], T a1 [b])
unzipEitherFirst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
mapFirst (\a0
a0 -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. b -> Either a b
Right a0
a0) forall a b. a -> Either a b
Left (a0 -> Maybe a1
f a0
a0))

partitionMaybeSecond :: (b0 -> Maybe b1) -> T a b0 -> (T b1 [a], T b0 [a])
partitionMaybeSecond :: forall b0 b1 a. (b0 -> Maybe b1) -> T a b0 -> (T b1 [a], T b0 [a])
partitionMaybeSecond b0 -> Maybe b1
f =
   forall a b0 b1. T a (Either b0 b1) -> (T b0 [a], T b1 [a])
unzipEitherSecond forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
mapSecond (\b0
b0 -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. b -> Either a b
Right b0
b0) forall a b. a -> Either a b
Left (b0 -> Maybe b1
f b0
b0))

partitionEitherFirst :: (a -> Either a0 a1) -> T a b -> (T a0 [b], T a1 [b])
partitionEitherFirst :: forall a a0 a1 b.
(a -> Either a0 a1) -> T a b -> (T a0 [b], T a1 [b])
partitionEitherFirst a -> Either a0 a1
f =
   forall a0 a1 b. T (Either a0 a1) b -> (T a0 [b], T a1 [b])
unzipEitherFirst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
mapFirst a -> Either a0 a1
f

partitionEitherSecond :: (b -> Either b0 b1) -> T a b -> (T b0 [a], T b1 [a])
partitionEitherSecond :: forall b b0 b1 a.
(b -> Either b0 b1) -> T a b -> (T b0 [a], T b1 [a])
partitionEitherSecond b -> Either b0 b1
f =
   forall a b0 b1. T a (Either b0 b1) -> (T b0 [a], T b1 [a])
unzipEitherSecond forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
mapSecond b -> Either b0 b1
f

unzipEitherFirst :: T (Either a0 a1) b -> (T a0 [b], T a1 [b])
unzipEitherFirst :: forall a0 a1 b. T (Either a0 a1) b -> (T a0 [b], T a1 [b])
unzipEitherFirst =
   forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr
      (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
          (forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> a -> T a b -> T a b
cons [])
          (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> a -> T a b -> T a b
cons []))
      (\b
b -> forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall b a. (b -> b) -> T a b -> T a b
mapSecondHead (b
bforall a. a -> [a] -> [a]
:), forall b a. (b -> b) -> T a b -> T a b
mapSecondHead (b
bforall a. a -> [a] -> [a]
:)))
      (forall b a. b -> T a b
singleton [], forall b a. b -> T a b
singleton [])

unzipEitherSecond :: T a (Either b0 b1) -> (T b0 [a], T b1 [a])
unzipEitherSecond :: forall a b0 b1. T a (Either b0 b1) -> (T b0 [a], T b1 [a])
unzipEitherSecond =
   forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr
      (\a
a -> forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall b a. (b -> b) -> T a b -> T a b
mapSecondHead (a
aforall a. a -> [a] -> [a]
:), forall b a. (b -> b) -> T a b -> T a b
mapSecondHead (a
aforall a. a -> [a] -> [a]
:)))
      (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
          (forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> a -> T a b -> T a b
cons [])
          (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> a -> T a b -> T a b
cons []))
      (forall b a. b -> T a b
singleton [], forall b a. b -> T a b
singleton [])

catMaybesFirst :: T (Maybe a) b -> T a [b]
catMaybesFirst :: forall a b. T (Maybe a) b -> T a [b]
catMaybesFirst =
   forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr
      (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall b a. b -> a -> T a b -> T a b
cons []))
      (forall b a. (b -> b) -> T a b -> T a b
mapSecondHead forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:))
      (forall b a. b -> T a b
singleton [])

catMaybesSecond :: T a (Maybe b) -> T b [a]
catMaybesSecond :: forall a b. T a (Maybe b) -> T b [a]
catMaybesSecond =
   forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> c
foldr
      (forall b a. (b -> b) -> T a b -> T a b
mapSecondHead forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:))
      (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall b a. b -> a -> T a b -> T a b
cons []))
      (forall b a. b -> T a b
singleton [])