{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}

#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE DeriveGeneric #-}
#endif

#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif

#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
#include "bifunctors-common.h"

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Bifunctor.Flip
-- Copyright   :  (C) 2008-2016 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  portable
--
----------------------------------------------------------------------------
module Data.Bifunctor.Flip
  ( Flip(..)
  ) where

#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif

import Data.Biapplicative
import Data.Bifoldable
import Data.Bifunctor.Functor
import Data.Bitraversable

#if __GLASGOW_HASKELL__ < 710
import Data.Foldable
import Data.Monoid
import Data.Traversable
#endif

#if __GLASGOW_HASKELL__ >= 708
import Data.Typeable
#endif

#if __GLASGOW_HASKELL__ >= 702
import GHC.Generics
#endif

#if LIFTED_FUNCTOR_CLASSES
import Data.Functor.Classes
#endif

-- | Make a 'Bifunctor' flipping the arguments of a 'Bifunctor'.
newtype Flip p a b = Flip { forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip :: p b a }
  deriving ( Flip p a b -> Flip p a b -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Eq (p b a) =>
Flip p a b -> Flip p a b -> Bool
/= :: Flip p a b -> Flip p a b -> Bool
$c/= :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Eq (p b a) =>
Flip p a b -> Flip p a b -> Bool
== :: Flip p a b -> Flip p a b -> Bool
$c== :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Eq (p b a) =>
Flip p a b -> Flip p a b -> Bool
Eq, Flip p a b -> Flip p a b -> Bool
Flip p a b -> Flip p a b -> Ordering
Flip p a b -> Flip p a b -> Flip p 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 {k} {k} {p :: k -> k -> *} {a :: k} {b :: k}.
Ord (p b a) =>
Eq (Flip p a b)
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Bool
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Ordering
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Flip p a b
min :: Flip p a b -> Flip p a b -> Flip p a b
$cmin :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Flip p a b
max :: Flip p a b -> Flip p a b -> Flip p a b
$cmax :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Flip p a b
>= :: Flip p a b -> Flip p a b -> Bool
$c>= :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Bool
> :: Flip p a b -> Flip p a b -> Bool
$c> :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Bool
<= :: Flip p a b -> Flip p a b -> Bool
$c<= :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Bool
< :: Flip p a b -> Flip p a b -> Bool
$c< :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Bool
compare :: Flip p a b -> Flip p a b -> Ordering
$ccompare :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Ord (p b a) =>
Flip p a b -> Flip p a b -> Ordering
Ord, Int -> Flip p a b -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Show (p b a) =>
Int -> Flip p a b -> ShowS
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Show (p b a) =>
[Flip p a b] -> ShowS
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Show (p b a) =>
Flip p a b -> String
showList :: [Flip p a b] -> ShowS
$cshowList :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Show (p b a) =>
[Flip p a b] -> ShowS
show :: Flip p a b -> String
$cshow :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Show (p b a) =>
Flip p a b -> String
showsPrec :: Int -> Flip p a b -> ShowS
$cshowsPrec :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Show (p b a) =>
Int -> Flip p a b -> ShowS
Show, ReadPrec [Flip p a b]
ReadPrec (Flip p a b)
ReadS [Flip p a b]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
ReadPrec [Flip p a b]
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
ReadPrec (Flip p a b)
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
Int -> ReadS (Flip p a b)
forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
ReadS [Flip p a b]
readListPrec :: ReadPrec [Flip p a b]
$creadListPrec :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
ReadPrec [Flip p a b]
readPrec :: ReadPrec (Flip p a b)
$creadPrec :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
ReadPrec (Flip p a b)
readList :: ReadS [Flip p a b]
$creadList :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
ReadS [Flip p a b]
readsPrec :: Int -> ReadS (Flip p a b)
$creadsPrec :: forall k k (p :: k -> k -> *) (a :: k) (b :: k).
Read (p b a) =>
Int -> ReadS (Flip p a b)
Read
#if __GLASGOW_HASKELL__ >= 702
           , forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k k (p :: k -> k -> *) (a :: k) (b :: k) x.
Rep (Flip p a b) x -> Flip p a b
forall k k (p :: k -> k -> *) (a :: k) (b :: k) x.
Flip p a b -> Rep (Flip p a b) x
$cto :: forall k k (p :: k -> k -> *) (a :: k) (b :: k) x.
Rep (Flip p a b) x -> Flip p a b
$cfrom :: forall k k (p :: k -> k -> *) (a :: k) (b :: k) x.
Flip p a b -> Rep (Flip p a b) x
Generic
#endif
#if __GLASGOW_HASKELL__ >= 708
           , Typeable
#endif
           )

#if LIFTED_FUNCTOR_CLASSES
instance (Eq2 p, Eq a) => Eq1 (Flip p a) where
  liftEq :: forall a b. (a -> b -> Bool) -> Flip p a a -> Flip p a b -> Bool
liftEq = forall (f :: * -> * -> *) a b c d.
Eq2 f =>
(a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool
liftEq2 forall a. Eq a => a -> a -> Bool
(==)
instance Eq2 p => Eq2 (Flip p) where
  liftEq2 :: forall a b c d.
(a -> b -> Bool)
-> (c -> d -> Bool) -> Flip p a c -> Flip p b d -> Bool
liftEq2 a -> b -> Bool
f c -> d -> Bool
g (Flip p c a
x) (Flip p d b
y) = forall (f :: * -> * -> *) a b c d.
Eq2 f =>
(a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool
liftEq2 c -> d -> Bool
g a -> b -> Bool
f p c a
x p d b
y

instance (Ord2 p, Ord a) => Ord1 (Flip p a) where
  liftCompare :: forall a b.
(a -> b -> Ordering) -> Flip p a a -> Flip p a b -> Ordering
liftCompare = forall (f :: * -> * -> *) a b c d.
Ord2 f =>
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
liftCompare2 forall a. Ord a => a -> a -> Ordering
compare
instance Ord2 p => Ord2 (Flip p) where
  liftCompare2 :: forall a b c d.
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> Flip p a c -> Flip p b d -> Ordering
liftCompare2 a -> b -> Ordering
f c -> d -> Ordering
g (Flip p c a
x) (Flip p d b
y) = forall (f :: * -> * -> *) a b c d.
Ord2 f =>
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
liftCompare2 c -> d -> Ordering
g a -> b -> Ordering
f p c a
x p d b
y

instance (Read2 p, Read a) => Read1 (Flip p a) where
  liftReadsPrec :: forall a.
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Flip p a a)
liftReadsPrec = forall (f :: * -> * -> *) a b.
Read2 f =>
(Int -> ReadS a)
-> ReadS [a]
-> (Int -> ReadS b)
-> ReadS [b]
-> Int
-> ReadS (f a b)
liftReadsPrec2 forall a. Read a => Int -> ReadS a
readsPrec forall a. Read a => ReadS [a]
readList
instance Read2 p => Read2 (Flip p) where
  liftReadsPrec2 :: forall a b.
(Int -> ReadS a)
-> ReadS [a]
-> (Int -> ReadS b)
-> ReadS [b]
-> Int
-> ReadS (Flip p a b)
liftReadsPrec2 Int -> ReadS a
rp1 ReadS [a]
rl1 Int -> ReadS b
rp2 ReadS [b]
rl2 Int
p = forall a. Bool -> ReadS a -> ReadS a
readParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ \String
s0 -> do
    (String
"Flip",    String
s1) <- ReadS String
lex String
s0
    (String
"{",       String
s2) <- ReadS String
lex String
s1
    (String
"runFlip", String
s3) <- ReadS String
lex String
s2
    (p b a
x,         String
s4) <- forall (f :: * -> * -> *) a b.
Read2 f =>
(Int -> ReadS a)
-> ReadS [a]
-> (Int -> ReadS b)
-> ReadS [b]
-> Int
-> ReadS (f a b)
liftReadsPrec2 Int -> ReadS b
rp2 ReadS [b]
rl2 Int -> ReadS a
rp1 ReadS [a]
rl1 Int
0 String
s3
    (String
"}",       String
s5) <- ReadS String
lex String
s4
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip p b a
x, String
s5)

instance (Show2 p, Show a) => Show1 (Flip p a) where
  liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Flip p a a -> ShowS
liftShowsPrec = forall (f :: * -> * -> *) a b.
Show2 f =>
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> f a b
-> ShowS
liftShowsPrec2 forall a. Show a => Int -> a -> ShowS
showsPrec forall a. Show a => [a] -> ShowS
showList
instance Show2 p => Show2 (Flip p) where
  liftShowsPrec2 :: forall a b.
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> Flip p a b
-> ShowS
liftShowsPrec2 Int -> a -> ShowS
sp1 [a] -> ShowS
sl1 Int -> b -> ShowS
sp2 [b] -> ShowS
sl2 Int
p (Flip p b a
x) = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
      String -> ShowS
showString String
"Flip {runFlip = "
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> * -> *) a b.
Show2 f =>
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> f a b
-> ShowS
liftShowsPrec2 Int -> b -> ShowS
sp2 [b] -> ShowS
sl2 Int -> a -> ShowS
sp1 [a] -> ShowS
sl1 Int
0 p b a
x
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'}'
#endif

instance Bifunctor p => Bifunctor (Flip p) where
  first :: forall a b c. (a -> b) -> Flip p a c -> Flip p b c
first a -> b
f = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE first #-}
  second :: forall b c a. (b -> c) -> Flip p a b -> Flip p a c
second b -> c
f = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first b -> c
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE second #-}
  bimap :: forall a b c d. (a -> b) -> (c -> d) -> Flip p a c -> Flip p b d
bimap a -> b
f c -> d
g = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap c -> d
g a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE bimap #-}

instance Bifunctor p => Functor (Flip p a) where
  fmap :: forall a b. (a -> b) -> Flip p a a -> Flip p a b
fmap a -> b
f = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE fmap #-}

instance Biapplicative p => Biapplicative (Flip p) where
  bipure :: forall a b. a -> b -> Flip p a b
bipure a
a b
b = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip (forall (p :: * -> * -> *) a b. Biapplicative p => a -> b -> p a b
bipure b
b a
a)
  {-# INLINE bipure #-}

  Flip p (c -> d) (a -> b)
fg <<*>> :: forall a b c d.
Flip p (a -> b) (c -> d) -> Flip p a c -> Flip p b d
<<*>> Flip p c a
xy = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip (p (c -> d) (a -> b)
fg forall (p :: * -> * -> *) a b c d.
Biapplicative p =>
p (a -> b) (c -> d) -> p a c -> p b d
<<*>> p c a
xy)
  {-# INLINE (<<*>>) #-}

instance Bifoldable p => Bifoldable (Flip p) where
  bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> Flip p a b -> m
bifoldMap a -> m
f b -> m
g = forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap b -> m
g a -> m
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE bifoldMap #-}

instance Bifoldable p => Foldable (Flip p a) where
  foldMap :: forall m a. Monoid m => (a -> m) -> Flip p a a -> m
foldMap a -> m
f = forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> m
f (forall a b. a -> b -> a
const forall a. Monoid a => a
mempty) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE foldMap #-}

instance Bitraversable p => Bitraversable (Flip p) where
  bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> Flip p a b -> f (Flip p c d)
bitraverse a -> f c
f b -> f d
g = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 b -> f d
g a -> f c
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE bitraverse #-}

instance Bitraversable p => Traversable (Flip p a) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Flip p a a -> f (Flip p a b)
traverse a -> f b
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 b
f forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
Flip p a b -> p b a
runFlip
  {-# INLINE traverse #-}

instance BifunctorFunctor Flip where
  bifmap :: forall (p :: k -> k -> *) (q :: k -> k -> *).
(p :-> q) -> Flip p :-> Flip q
bifmap p :-> q
f (Flip p b a
p) = forall {k} {k} (p :: k -> k -> *) (a :: k) (b :: k).
p b a -> Flip p a b
Flip (p :-> q
f p b a
p)