{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveGeneric #-}
#endif
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
#if __GLASGOW_HASKELL__ >= 710 && __GLASGOW_HASKELL__ < 802
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Applicative.Backwards (
Backwards(..),
) where
import Data.Functor.Classes
#if MIN_VERSION_base(4,12,0)
import Data.Functor.Contravariant
#endif
#if __GLASGOW_HASKELL__ >= 704
import GHC.Generics
#endif
import Prelude hiding (foldr, foldr1, foldl, foldl1, null, length)
import Control.Applicative
import Data.Foldable
#if !(MIN_VERSION_base(4,8,0))
import Data.Traversable (Traversable(traverse, sequenceA))
#endif
newtype Backwards f a = Backwards { Backwards f a -> f a
forwards :: f a }
#if __GLASGOW_HASKELL__ >= 710
deriving ((forall x. Backwards f a -> Rep (Backwards f a) x)
-> (forall x. Rep (Backwards f a) x -> Backwards f a)
-> Generic (Backwards f a)
forall x. Rep (Backwards f a) x -> Backwards f a
forall x. Backwards f a -> Rep (Backwards f a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (f :: k -> *) (a :: k) x.
Rep (Backwards f a) x -> Backwards f a
forall k (f :: k -> *) (a :: k) x.
Backwards f a -> Rep (Backwards f a) x
$cto :: forall k (f :: k -> *) (a :: k) x.
Rep (Backwards f a) x -> Backwards f a
$cfrom :: forall k (f :: k -> *) (a :: k) x.
Backwards f a -> Rep (Backwards f a) x
Generic, (forall (a :: k). Backwards f a -> Rep1 (Backwards f) a)
-> (forall (a :: k). Rep1 (Backwards f) a -> Backwards f a)
-> Generic1 (Backwards f)
forall (a :: k). Rep1 (Backwards f) a -> Backwards f a
forall (a :: k). Backwards f a -> Rep1 (Backwards f) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall k (f :: k -> *) (a :: k).
Rep1 (Backwards f) a -> Backwards f a
forall k (f :: k -> *) (a :: k).
Backwards f a -> Rep1 (Backwards f) a
$cto1 :: forall k (f :: k -> *) (a :: k).
Rep1 (Backwards f) a -> Backwards f a
$cfrom1 :: forall k (f :: k -> *) (a :: k).
Backwards f a -> Rep1 (Backwards f) a
Generic1)
#elif __GLASGOW_HASKELL__ >= 704
deriving (Generic)
#endif
instance (Eq1 f) => Eq1 (Backwards f) where
liftEq :: (a -> b -> Bool) -> Backwards f a -> Backwards f b -> Bool
liftEq a -> b -> Bool
eq (Backwards f a
x) (Backwards f b
y) = (a -> b -> Bool) -> f a -> f b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq f a
x f b
y
{-# INLINE liftEq #-}
instance (Ord1 f) => Ord1 (Backwards f) where
liftCompare :: (a -> b -> Ordering) -> Backwards f a -> Backwards f b -> Ordering
liftCompare a -> b -> Ordering
comp (Backwards f a
x) (Backwards f b
y) = (a -> b -> Ordering) -> f a -> f b -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
comp f a
x f b
y
{-# INLINE liftCompare #-}
instance (Read1 f) => Read1 (Backwards f) where
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Backwards f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = (String -> ReadS (Backwards f a)) -> Int -> ReadS (Backwards f a)
forall a. (String -> ReadS a) -> Int -> ReadS a
readsData ((String -> ReadS (Backwards f a)) -> Int -> ReadS (Backwards f a))
-> (String -> ReadS (Backwards f a))
-> Int
-> ReadS (Backwards f a)
forall a b. (a -> b) -> a -> b
$
(Int -> ReadS (f a))
-> String
-> (f a -> Backwards f a)
-> String
-> ReadS (Backwards f a)
forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith ((Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"Backwards" f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards
instance (Show1 f) => Show1 (Backwards f) where
liftShowsPrec :: (Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Backwards f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (Backwards f a
x) =
(Int -> f a -> ShowS) -> String -> Int -> f a -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith ((Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"Backwards" Int
d f a
x
instance (Eq1 f, Eq a) => Eq (Backwards f a) where == :: Backwards f a -> Backwards f a -> Bool
(==) = Backwards f a -> Backwards f a -> Bool
forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
eq1
instance (Ord1 f, Ord a) => Ord (Backwards f a) where compare :: Backwards f a -> Backwards f a -> Ordering
compare = Backwards f a -> Backwards f a -> Ordering
forall (f :: * -> *) a. (Ord1 f, Ord a) => f a -> f a -> Ordering
compare1
instance (Read1 f, Read a) => Read (Backwards f a) where readsPrec :: Int -> ReadS (Backwards f a)
readsPrec = Int -> ReadS (Backwards f a)
forall (f :: * -> *) a. (Read1 f, Read a) => Int -> ReadS (f a)
readsPrec1
instance (Show1 f, Show a) => Show (Backwards f a) where showsPrec :: Int -> Backwards f a -> ShowS
showsPrec = Int -> Backwards f a -> ShowS
forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1
instance (Functor f) => Functor (Backwards f) where
fmap :: (a -> b) -> Backwards f a -> Backwards f b
fmap a -> b
f (Backwards f a
a) = f b -> Backwards f b
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards ((a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f f a
a)
{-# INLINE fmap #-}
a
x <$ :: a -> Backwards f b -> Backwards f a
<$ Backwards f b
a = f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (a
x a -> f b -> f a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f b
a)
{-# INLINE (<$) #-}
instance (Applicative f) => Applicative (Backwards f) where
pure :: a -> Backwards f a
pure a
a = f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a)
{-# INLINE pure #-}
Backwards f (a -> b)
f <*> :: Backwards f (a -> b) -> Backwards f a -> Backwards f b
<*> Backwards f a
a = f b -> Backwards f b
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a
a f a -> f (a -> b) -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> f (a -> b)
f)
{-# INLINE (<*>) #-}
#if MIN_VERSION_base(4,10,0)
liftA2 :: (a -> b -> c) -> Backwards f a -> Backwards f b -> Backwards f c
liftA2 a -> b -> c
f (Backwards f a
m) (Backwards f b
n) = f c -> Backwards f c
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f c -> Backwards f c) -> f c -> Backwards f c
forall a b. (a -> b) -> a -> b
$ (b -> a -> c) -> f b -> f a -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((a -> b -> c) -> b -> a -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> c
f) f b
n f a
m
{-# INLINE liftA2 #-}
#endif
#if MIN_VERSION_base(4,2,0)
Backwards f a
xs *> :: Backwards f a -> Backwards f b -> Backwards f b
*> Backwards f b
ys = f b -> Backwards f b
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f b
ys f b -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* f a
xs)
{-# INLINE (*>) #-}
Backwards f a
ys <* :: Backwards f a -> Backwards f b -> Backwards f a
<* Backwards f b
xs = f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f b
xs f b -> f a -> f a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f a
ys)
{-# INLINE (<*) #-}
#endif
instance (Alternative f) => Alternative (Backwards f) where
empty :: Backwards f a
empty = f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards f a
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE empty #-}
Backwards f a
x <|> :: Backwards f a -> Backwards f a -> Backwards f a
<|> Backwards f a
y = f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a
x f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f a
y)
{-# INLINE (<|>) #-}
instance (Foldable f) => Foldable (Backwards f) where
foldMap :: (a -> m) -> Backwards f a -> m
foldMap a -> m
f (Backwards f a
t) = (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f f a
t
{-# INLINE foldMap #-}
foldr :: (a -> b -> b) -> b -> Backwards f a -> b
foldr a -> b -> b
f b
z (Backwards f a
t) = (a -> b -> b) -> b -> f a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
z f a
t
{-# INLINE foldr #-}
foldl :: (b -> a -> b) -> b -> Backwards f a -> b
foldl b -> a -> b
f b
z (Backwards f a
t) = (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl b -> a -> b
f b
z f a
t
{-# INLINE foldl #-}
foldr1 :: (a -> a -> a) -> Backwards f a -> a
foldr1 a -> a -> a
f (Backwards f a
t) = (a -> a -> a) -> f a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 a -> a -> a
f f a
t
{-# INLINE foldr1 #-}
foldl1 :: (a -> a -> a) -> Backwards f a -> a
foldl1 a -> a -> a
f (Backwards f a
t) = (a -> a -> a) -> f a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 a -> a -> a
f f a
t
{-# INLINE foldl1 #-}
#if MIN_VERSION_base(4,8,0)
null :: Backwards f a -> Bool
null (Backwards f a
t) = f a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null f a
t
length :: Backwards f a -> Int
length (Backwards f a
t) = f a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length f a
t
#endif
instance (Traversable f) => Traversable (Backwards f) where
traverse :: (a -> f b) -> Backwards f a -> f (Backwards f b)
traverse a -> f b
f (Backwards f a
t) = (f b -> Backwards f b) -> f (f b) -> f (Backwards f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f b -> Backwards f b
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards ((a -> f b) -> f a -> f (f b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f f a
t)
{-# INLINE traverse #-}
sequenceA :: Backwards f (f a) -> f (Backwards f a)
sequenceA (Backwards f (f a)
t) = (f a -> Backwards f a) -> f (f a) -> f (Backwards f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f (f a) -> f (f a)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA f (f a)
t)
{-# INLINE sequenceA #-}
#if MIN_VERSION_base(4,12,0)
instance Contravariant f => Contravariant (Backwards f) where
contramap :: (a -> b) -> Backwards f b -> Backwards f a
contramap a -> b
f = f a -> Backwards f a
forall k (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a -> Backwards f a)
-> (Backwards f b -> f a) -> Backwards f b -> Backwards f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> f b -> f a
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap a -> b
f (f b -> f a) -> (Backwards f b -> f b) -> Backwards f b -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Backwards f b -> f b
forall k (f :: k -> *) (a :: k). Backwards f a -> f a
forwards
{-# INLINE contramap #-}
#endif