    {-# OPTIONS_GHC -Wunused-imports #-}

    module Agda.Utils.Tuple
      ( (-*-)
      , mapFst
      , mapSnd
      , (/\)
      , sortPair
      , fst3
      , snd3
      , thd3
      , swap
      , uncurry3
      , uncurry4
      , mapPairM
      , mapFstM
      , mapSndM
      , Pair(..)
      ) where

    import Control.Arrow   ( (&&&) )
    import Control.DeepSeq ( NFData )

    import Data.Bifunctor  ( bimap, first, second )
    import Data.Tuple      ( swap )

    import GHC.Generics    ( Generic )

    infix 2 -*-
    infix 3 /\ -- backslashes at EOL interact badly with CPP...

    -- | Bifunctoriality for pairs.
    (-*-) :: (a -> c) -> (b -> d) -> (a,b) -> (c,d)
    -*- :: forall a c b d. (a -> c) -> (b -> d) -> (a, b) -> (c, d)
    (-*-) = (a -> c) -> (b -> d) -> (a, b) -> (c, d)
    forall a c b d. (a -> c) -> (b -> d) -> (a, b) -> (c, d)
    forall (p :: * -> * -> *) a b c d.
    Bifunctor p =>
    (a -> b) -> (c -> d) -> p a c -> p b d
    bimap

    -- | @mapFst f = f -*- id@
    mapFst :: (a -> c) -> (a,b) -> (c,b)
    mapFst :: forall a c b. (a -> c) -> (a, b) -> (c, b)
    mapFst = (a -> c) -> (a, b) -> (c, b)
    forall a c b. (a -> c) -> (a, b) -> (c, b)
    forall (p :: * -> * -> *) a b c.
    Bifunctor p =>
    (a -> b) -> p a c -> p b c
    first

    -- | @mapSnd g = id -*- g@
    mapSnd :: (b -> d) -> (a,b) -> (a,d)
    mapSnd :: forall b d a. (b -> d) -> (a, b) -> (a, d)
    mapSnd = (b -> d) -> (a, b) -> (a, d)
    forall b d a. (b -> d) -> (a, b) -> (a, d)
    forall (p :: * -> * -> *) b c a.
    Bifunctor p =>
    (b -> c) -> p a b -> p a c
    second

    -- | Lifted pairing.
    (/\) :: (a -> b) -> (a -> c) -> a -> (b,c)
    /\ :: forall a b c. (a -> b) -> (a -> c) -> a -> (b, c)
    (/\) = (a -> b) -> (a -> c) -> a -> (b, c)
    forall a b c. (a -> b) -> (a -> c) -> a -> (b, c)
    forall (a :: * -> * -> *) b c c'.
    Arrow a =>
    a b c -> a b c' -> a b (c, c')
    (&&&)

    -- | Order a pair.
    sortPair :: Ord a => (a, a) -> (a, a)
    sortPair :: forall a. Ord a => (a, a) -> (a, a)
    sortPair p :: (a, a)
    p@(a
    x, a
    y)
      | a
    x a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= a
    y    = (a, a)
    p
      | Bool
    otherwise = (a
    y, a
    x)

    -- * Triple (stolen from Data.Tuple.HT)

    {-# INLINE fst3 #-}
    fst3 :: (a,b,c) -> a
    fst3 :: forall a b c. (a, b, c) -> a
    fst3 ~(a
    x,b
    _,c
    _) = a
    x

    {-# INLINE snd3 #-}
    snd3 :: (a,b,c) -> b
    snd3 :: forall a b c. (a, b, c) -> b
    snd3 ~(a
    _,b
    x,c
    _) = b
    x

    {-# INLINE thd3 #-}
    thd3 :: (a,b,c) -> c
    thd3 :: forall a b c. (a, b, c) -> c
    thd3 ~(a
    _,b
    _,c
    x) = c
    x

    {-# INLINE uncurry3 #-}
    uncurry3 :: (a -> b -> c -> d) -> (a,b,c) -> d
    uncurry3 :: forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
    uncurry3 a -> b -> c -> d
    f ~(a
    x,b
    y,c
    z) = a -> b -> c -> d
    f a
    x b
    y c
    z

    {-# INLINE uncurry4 #-}
    uncurry4 :: (a -> b -> c -> d -> e) -> (a,b,c,d) -> e
    uncurry4 :: forall a b c d e. (a -> b -> c -> d -> e) -> (a, b, c, d) -> e
    uncurry4 a -> b -> c -> d -> e
    f ~(a
    w,b
    x,c
    y,d
    z) = a -> b -> c -> d -> e
    f a
    w b
    x c
    y d
    z

    -- | Monadic version of '-*-'.
    mapPairM :: (Applicative m) => (a -> m c) -> (b -> m d) -> (a,b) -> m (c,d)
    mapPairM :: forall (m :: * -> *) a c b d.
    Applicative m =>
    (a -> m c) -> (b -> m d) -> (a, b) -> m (c, d)
    mapPairM a -> m c
    f b -> m d
    g ~(a
    a,b
    b) = (,) (c -> d -> (c, d)) -> m c -> m (d -> (c, d))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> a -> m c
    f a
    a m (d -> (c, d)) -> m d -> m (c, d)
    forall a b. m (a -> b) -> m a -> m b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> b -> m d
    g b
    b

    -- | Monadic 'mapFst'.
    mapFstM :: Functor m => (a -> m c) -> (a,b) -> m (c,b)
    mapFstM :: forall (m :: * -> *) a c b.
    Functor m =>
    (a -> m c) -> (a, b) -> m (c, b)
    mapFstM a -> m c
    f ~(a
    a,b
    b) = (,b
    b) (c -> (c, b)) -> m c -> m (c, b)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> a -> m c
    f a
    a

    -- | Monadic 'mapSnd'.
    mapSndM :: Functor m => (b -> m d) -> (a,b) -> m (a,d)
    mapSndM :: forall (m :: * -> *) b d a.
    Functor m =>
    (b -> m d) -> (a, b) -> m (a, d)
    mapSndM b -> m d
    f ~(a
    a,b
    b) = (a
    a,) (d -> (a, d)) -> m d -> m (a, d)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> b -> m d
    f b
    b

    data Pair a = Pair a a
      deriving (Int -> Pair a -> ShowS
    [Pair a] -> ShowS
    Pair a -> String
    (Int -> Pair a -> ShowS)
    -> (Pair a -> String) -> ([Pair a] -> ShowS) -> Show (Pair a)
    forall a. Show a => Int -> Pair a -> ShowS
    forall a. Show a => [Pair a] -> ShowS
    forall a. Show a => Pair a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Show a => Int -> Pair a -> ShowS
    showsPrec :: Int -> Pair a -> ShowS
    $cshow :: forall a. Show a => Pair a -> String
    show :: Pair a -> String
    $cshowList :: forall a. Show a => [Pair a] -> ShowS
    showList :: [Pair a] -> ShowS
    Show, Pair a -> Pair a -> Bool
    (Pair a -> Pair a -> Bool)
    -> (Pair a -> Pair a -> Bool) -> Eq (Pair a)
    forall a. Eq a => Pair a -> Pair a -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall a. Eq a => Pair a -> Pair a -> Bool
    == :: Pair a -> Pair a -> Bool
    $c/= :: forall a. Eq a => Pair a -> Pair a -> Bool
    /= :: Pair a -> Pair a -> Bool
    Eq, (forall x. Pair a -> Rep (Pair a) x)
    -> (forall x. Rep (Pair a) x -> Pair a) -> Generic (Pair a)
    forall x. Rep (Pair a) x -> Pair a
    forall x. Pair a -> Rep (Pair a) x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    forall a x. Rep (Pair a) x -> Pair a
    forall a x. Pair a -> Rep (Pair a) x
    $cfrom :: forall a x. Pair a -> Rep (Pair a) x
    from :: forall x. Pair a -> Rep (Pair a) x
    $cto :: forall a x. Rep (Pair a) x -> Pair a
    to :: forall x. Rep (Pair a) x -> Pair a
    Generic, (forall a b. (a -> b) -> Pair a -> Pair b)
    -> (forall a b. a -> Pair b -> Pair a) -> Functor Pair
    forall a b. a -> Pair b -> Pair a
    forall a b. (a -> b) -> Pair a -> Pair b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    $cfmap :: forall a b. (a -> b) -> Pair a -> Pair b
    fmap :: forall a b. (a -> b) -> Pair a -> Pair b
    $c<$ :: forall a b. a -> Pair b -> Pair a
    <$ :: forall a b. a -> Pair b -> Pair a
    Functor, (forall m. Monoid m => Pair m -> m)
    -> (forall m a. Monoid m => (a -> m) -> Pair a -> m)
    -> (forall m a. Monoid m => (a -> m) -> Pair a -> m)
    -> (forall a b. (a -> b -> b) -> b -> Pair a -> b)
    -> (forall a b. (a -> b -> b) -> b -> Pair a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Pair a -> b)
    -> (forall b a. (b -> a -> b) -> b -> Pair a -> b)
    -> (forall a. (a -> a -> a) -> Pair a -> a)
    -> (forall a. (a -> a -> a) -> Pair a -> a)
    -> (forall a. Pair a -> [a])
    -> (forall a. Pair a -> Bool)
    -> (forall a. Pair a -> Int)
    -> (forall a. Eq a => a -> Pair a -> Bool)
    -> (forall a. Ord a => Pair a -> a)
    -> (forall a. Ord a => Pair a -> a)
    -> (forall a. Num a => Pair a -> a)
    -> (forall a. Num a => Pair a -> a)
    -> Foldable Pair
    forall a. Eq a => a -> Pair a -> Bool
    forall a. Num a => Pair a -> a
    forall a. Ord a => Pair a -> a
    forall m. Monoid m => Pair m -> m
    forall a. Pair a -> Bool
    forall a. Pair a -> Int
    forall a. Pair a -> [a]
    forall a. (a -> a -> a) -> Pair a -> a
    forall m a. Monoid m => (a -> m) -> Pair a -> m
    forall b a. (b -> a -> b) -> b -> Pair a -> b
    forall a b. (a -> b -> b) -> b -> Pair a -> b
    forall (t :: * -> *).
    (forall m. Monoid m => t m -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. t a -> [a])
    -> (forall a. t a -> Bool)
    -> (forall a. t a -> Int)
    -> (forall a. Eq a => a -> t a -> Bool)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> Foldable t
    $cfold :: forall m. Monoid m => Pair m -> m
    fold :: forall m. Monoid m => Pair m -> m
    $cfoldMap :: forall m a. Monoid m => (a -> m) -> Pair a -> m
    foldMap :: forall m a. Monoid m => (a -> m) -> Pair a -> m
    $cfoldMap' :: forall m a. Monoid m => (a -> m) -> Pair a -> m
    foldMap' :: forall m a. Monoid m => (a -> m) -> Pair a -> m
    $cfoldr :: forall a b. (a -> b -> b) -> b -> Pair a -> b
    foldr :: forall a b. (a -> b -> b) -> b -> Pair a -> b
    $cfoldr' :: forall a b. (a -> b -> b) -> b -> Pair a -> b
    foldr' :: forall a b. (a -> b -> b) -> b -> Pair a -> b
    $cfoldl :: forall b a. (b -> a -> b) -> b -> Pair a -> b
    foldl :: forall b a. (b -> a -> b) -> b -> Pair a -> b
    $cfoldl' :: forall b a. (b -> a -> b) -> b -> Pair a -> b
    foldl' :: forall b a. (b -> a -> b) -> b -> Pair a -> b
    $cfoldr1 :: forall a. (a -> a -> a) -> Pair a -> a
    foldr1 :: forall a. (a -> a -> a) -> Pair a -> a
    $cfoldl1 :: forall a. (a -> a -> a) -> Pair a -> a
    foldl1 :: forall a. (a -> a -> a) -> Pair a -> a
    $ctoList :: forall a. Pair a -> [a]
    toList :: forall a. Pair a -> [a]
    $cnull :: forall a. Pair a -> Bool
    null :: forall a. Pair a -> Bool
    $clength :: forall a. Pair a -> Int
    length :: forall a. Pair a -> Int
    $celem :: forall a. Eq a => a -> Pair a -> Bool
    elem :: forall a. Eq a => a -> Pair a -> Bool
    $cmaximum :: forall a. Ord a => Pair a -> a
    maximum :: forall a. Ord a => Pair a -> a
    $cminimum :: forall a. Ord a => Pair a -> a
    minimum :: forall a. Ord a => Pair a -> a
    $csum :: forall a. Num a => Pair a -> a
    sum :: forall a. Num a => Pair a -> a
    $cproduct :: forall a. Num a => Pair a -> a
    product :: forall a. Num a => Pair a -> a
    Foldable, Functor Pair
    Foldable Pair
    (Functor Pair, Foldable Pair) =>
    (forall (f :: * -> *) a b.
     Applicative f =>
     (a -> f b) -> Pair a -> f (Pair b))
    -> (forall (f :: * -> *) a.
        Applicative f =>
        Pair (f a) -> f (Pair a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> Pair a -> m (Pair b))
    -> (forall (m :: * -> *) a. Monad m => Pair (m a) -> m (Pair a))
    -> Traversable Pair
    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 => Pair (m a) -> m (Pair a)
    forall (f :: * -> *) a. Applicative f => Pair (f a) -> f (Pair a)
    forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Pair a -> m (Pair b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Pair a -> f (Pair b)
    $ctraverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Pair a -> f (Pair b)
    traverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Pair a -> f (Pair b)
    $csequenceA :: forall (f :: * -> *) a. Applicative f => Pair (f a) -> f (Pair a)
    sequenceA :: forall (f :: * -> *) a. Applicative f => Pair (f a) -> f (Pair a)
    $cmapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Pair a -> m (Pair b)
    mapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Pair a -> m (Pair b)
    $csequence :: forall (m :: * -> *) a. Monad m => Pair (m a) -> m (Pair a)
    sequence :: forall (m :: * -> *) a. Monad m => Pair (m a) -> m (Pair a)
    Traversable)

    instance Applicative Pair where
      pure :: forall a. a -> Pair a
    pure a
    a                  = a -> a -> Pair a
    forall a. a -> a -> Pair a
    Pair a
    a a
    a
      Pair a -> b
    f a -> b
    f' <*> :: forall a b. Pair (a -> b) -> Pair a -> Pair b
    <*> Pair a
    a a
    a' = b -> b -> Pair b
    forall a. a -> a -> Pair a
    Pair (a -> b
    f a
    a) (a -> b
    f' a
    a')

    instance NFData a => NFData (Pair a)