{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Bitraversable
( Bitraversable(..)
, bisequenceA
, bisequence
, bimapM
, bifor
, biforM
, bimapAccumL
, bimapAccumR
, bimapDefault
, bifoldMapDefault
) where
import Control.Applicative
import Data.Bifunctor
import Data.Bifoldable
import Data.Coerce
import Data.Functor.Identity (Identity(..))
import Data.Functor.Utils (StateL(..), StateR(..))
import GHC.Generics (K1(..))
class (Bifunctor t, Bifoldable t) => Bitraversable t where
bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g = t (f c) (f d) -> f (t c d)
forall (t :: * -> * -> *) (f :: * -> *) a b.
(Bitraversable t, Applicative f) =>
t (f a) (f b) -> f (t a b)
bisequenceA (t (f c) (f d) -> f (t c d))
-> (t a b -> t (f c) (f d)) -> t a b -> f (t c d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f c) -> (b -> f d) -> t a b -> t (f c) (f d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> f c
f b -> f d
g
bisequenceA :: (Bitraversable t, Applicative f) => t (f a) (f b) -> f (t a b)
bisequenceA :: forall (t :: * -> * -> *) (f :: * -> *) a b.
(Bitraversable t, Applicative f) =>
t (f a) (f b) -> f (t a b)
bisequenceA = t (f a) (f b) -> f (t a b)
forall (t :: * -> * -> *) (f :: * -> *) a b.
(Bitraversable t, Applicative f) =>
t (f a) (f b) -> f (t a b)
bisequence
bimapM :: (Bitraversable t, Applicative f)
=> (a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bimapM :: forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bimapM = (a -> f c) -> (b -> f d) -> t a b -> f (t c d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse
bisequence :: (Bitraversable t, Applicative f) => t (f a) (f b) -> f (t a b)
bisequence :: forall (t :: * -> * -> *) (f :: * -> *) a b.
(Bitraversable t, Applicative f) =>
t (f a) (f b) -> f (t a b)
bisequence = (f a -> f a) -> (f b -> f b) -> t (f a) (f b) -> f (t a b)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse f a -> f a
forall a. a -> a
id f b -> f b
forall a. a -> a
id
instance Bitraversable (,) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> (a, b) -> f (c, d)
bitraverse a -> f c
f b -> f d
g ~(a
a, b
b) = (c -> d -> (c, d)) -> f c -> f d -> f (c, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (a -> f c
f a
a) (b -> f d
g b
b)
instance Bitraversable ((,,) x) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> (x, a, b) -> f (x, c, d)
bitraverse a -> f c
f b -> f d
g ~(x
x, a
a, b
b) = (c -> d -> (x, c, d)) -> f c -> f d -> f (x, c, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((,,) x
x) (a -> f c
f a
a) (b -> f d
g b
b)
instance Bitraversable ((,,,) x y) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> (x, y, a, b) -> f (x, y, c, d)
bitraverse a -> f c
f b -> f d
g ~(x
x, y
y, a
a, b
b) = (c -> d -> (x, y, c, d)) -> f c -> f d -> f (x, y, c, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((,,,) x
x y
y) (a -> f c
f a
a) (b -> f d
g b
b)
instance Bitraversable ((,,,,) x y z) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> (x, y, z, a, b) -> f (x, y, z, c, d)
bitraverse a -> f c
f b -> f d
g ~(x
x, y
y, z
z, a
a, b
b) = (c -> d -> (x, y, z, c, d)) -> f c -> f d -> f (x, y, z, c, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((,,,,) x
x y
y z
z) (a -> f c
f a
a) (b -> f d
g b
b)
instance Bitraversable ((,,,,,) x y z w) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c)
-> (b -> f d) -> (x, y, z, w, a, b) -> f (x, y, z, w, c, d)
bitraverse a -> f c
f b -> f d
g ~(x
x, y
y, z
z, w
w, a
a, b
b) = (c -> d -> (x, y, z, w, c, d))
-> f c -> f d -> f (x, y, z, w, c, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((,,,,,) x
x y
y z
z w
w) (a -> f c
f a
a) (b -> f d
g b
b)
instance Bitraversable ((,,,,,,) x y z w v) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c)
-> (b -> f d) -> (x, y, z, w, v, a, b) -> f (x, y, z, w, v, c, d)
bitraverse a -> f c
f b -> f d
g ~(x
x, y
y, z
z, w
w, v
v, a
a, b
b) =
(c -> d -> (x, y, z, w, v, c, d))
-> f c -> f d -> f (x, y, z, w, v, c, d)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((,,,,,,) x
x y
y z
z w
w v
v) (a -> f c
f a
a) (b -> f d
g b
b)
instance Bitraversable Either where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> Either a b -> f (Either c d)
bitraverse a -> f c
f b -> f d
_ (Left a
a) = c -> Either c d
forall a b. a -> Either a b
Left (c -> Either c d) -> f c -> f (Either c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
a
bitraverse a -> f c
_ b -> f d
g (Right b
b) = d -> Either c d
forall a b. b -> Either a b
Right (d -> Either c d) -> f d -> f (Either c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f d
g b
b
instance Bitraversable Const where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> Const a b -> f (Const c d)
bitraverse a -> f c
f b -> f d
_ (Const a
a) = c -> Const c d
forall {k} a (b :: k). a -> Const a b
Const (c -> Const c d) -> f c -> f (Const c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
a
instance Bitraversable (K1 i) where
bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> K1 i a b -> f (K1 i c d)
bitraverse a -> f c
f b -> f d
_ (K1 a
c) = c -> K1 i c d
forall k i c (p :: k). c -> K1 i c p
K1 (c -> K1 i c d) -> f c -> f (K1 i c d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
c
bifor :: (Bitraversable t, Applicative f)
=> t a b -> (a -> f c) -> (b -> f d) -> f (t c d)
bifor :: forall (t :: * -> * -> *) (f :: * -> *) a b c d.
(Bitraversable t, Applicative f) =>
t a b -> (a -> f c) -> (b -> f d) -> f (t c d)
bifor t a b
t a -> f c
f b -> f d
g = (a -> f c) -> (b -> f d) -> t a b -> f (t c d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g t a b
t
biforM :: (Bitraversable t, Applicative f)
=> t a b -> (a -> f c) -> (b -> f d) -> f (t c d)
biforM :: forall (t :: * -> * -> *) (f :: * -> *) a b c d.
(Bitraversable t, Applicative f) =>
t a b -> (a -> f c) -> (b -> f d) -> f (t c d)
biforM = t a b -> (a -> f c) -> (b -> f d) -> f (t c d)
forall (t :: * -> * -> *) (f :: * -> *) a b c d.
(Bitraversable t, Applicative f) =>
t a b -> (a -> f c) -> (b -> f d) -> f (t c d)
bifor
bimapAccumL :: Bitraversable t => (a -> b -> (a, c)) -> (a -> d -> (a, e))
-> a -> t b d -> (a, t c e)
bimapAccumL :: forall (t :: * -> * -> *) a b c d e.
Bitraversable t =>
(a -> b -> (a, c))
-> (a -> d -> (a, e)) -> a -> t b d -> (a, t c e)
bimapAccumL a -> b -> (a, c)
f a -> d -> (a, e)
g a
s t b d
t
= StateL a (t c e) -> a -> (a, t c e)
forall s a. StateL s a -> s -> (s, a)
runStateL ((b -> StateL a c) -> (d -> StateL a e) -> t b d -> StateL a (t c e)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse ((a -> (a, c)) -> StateL a c
forall s a. (s -> (s, a)) -> StateL s a
StateL ((a -> (a, c)) -> StateL a c)
-> (b -> a -> (a, c)) -> b -> StateL a c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> (a, c)) -> b -> a -> (a, c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> (a, c)
f) ((a -> (a, e)) -> StateL a e
forall s a. (s -> (s, a)) -> StateL s a
StateL ((a -> (a, e)) -> StateL a e)
-> (d -> a -> (a, e)) -> d -> StateL a e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> d -> (a, e)) -> d -> a -> (a, e)
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> d -> (a, e)
g) t b d
t) a
s
bimapAccumR :: Bitraversable t => (a -> b -> (a, c)) -> (a -> d -> (a, e))
-> a -> t b d -> (a, t c e)
bimapAccumR :: forall (t :: * -> * -> *) a b c d e.
Bitraversable t =>
(a -> b -> (a, c))
-> (a -> d -> (a, e)) -> a -> t b d -> (a, t c e)
bimapAccumR a -> b -> (a, c)
f a -> d -> (a, e)
g a
s t b d
t
= StateR a (t c e) -> a -> (a, t c e)
forall s a. StateR s a -> s -> (s, a)
runStateR ((b -> StateR a c) -> (d -> StateR a e) -> t b d -> StateR a (t c e)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse ((a -> (a, c)) -> StateR a c
forall s a. (s -> (s, a)) -> StateR s a
StateR ((a -> (a, c)) -> StateR a c)
-> (b -> a -> (a, c)) -> b -> StateR a c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> (a, c)) -> b -> a -> (a, c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> (a, c)
f) ((a -> (a, e)) -> StateR a e
forall s a. (s -> (s, a)) -> StateR s a
StateR ((a -> (a, e)) -> StateR a e)
-> (d -> a -> (a, e)) -> d -> StateR a e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> d -> (a, e)) -> d -> a -> (a, e)
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> d -> (a, e)
g) t b d
t) a
s
bimapDefault :: forall t a b c d . Bitraversable t
=> (a -> b) -> (c -> d) -> t a c -> t b d
bimapDefault :: forall (t :: * -> * -> *) a b c d.
Bitraversable t =>
(a -> b) -> (c -> d) -> t a c -> t b d
bimapDefault = ((a -> Identity b)
-> (c -> Identity d) -> t a c -> Identity (t b d))
-> (a -> b) -> (c -> d) -> t a c -> t b d
coerce
((a -> Identity b) -> (c -> Identity d) -> t a c -> Identity (t b d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse :: (a -> Identity b)
-> (c -> Identity d) -> t a c -> Identity (t b d))
{-# INLINE bimapDefault #-}
bifoldMapDefault :: forall t m a b . (Bitraversable t, Monoid m)
=> (a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault :: forall (t :: * -> * -> *) m a b.
(Bitraversable t, Monoid m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault = ((a -> Const m ())
-> (b -> Const m ()) -> t a b -> Const m (t () ()))
-> (a -> m) -> (b -> m) -> t a b -> m
coerce
((a -> Const m ())
-> (b -> Const m ()) -> t a b -> Const m (t () ())
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse :: (a -> Const m ())
-> (b -> Const m ()) -> t a b -> Const m (t () ()))
{-# INLINE bifoldMapDefault #-}