{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE PolyKinds #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# 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
import Prelude hiding (foldr, foldr1, foldl, foldl1, null, length)
import Control.Applicative
import Data.Foldable
import Data.Traversable
newtype Backwards f a = Backwards { forall {k} (f :: k -> *) (a :: k). Backwards f a -> f a
forwards :: f a }
instance (Eq1 f) => Eq1 (Backwards f) where
liftEq :: forall a b.
(a -> b -> Bool) -> Backwards f a -> Backwards f b -> Bool
liftEq a -> b -> Bool
eq (Backwards f a
x) (Backwards f b
y) = 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 :: forall a b.
(a -> b -> Ordering) -> Backwards f a -> Backwards f b -> Ordering
liftCompare a -> b -> Ordering
comp (Backwards f a
x) (Backwards f b
y) = 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 :: forall a.
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Backwards f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$
forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl) String
"Backwards" forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards
instance (Show1 f) => Show1 (Backwards f) where
liftShowsPrec :: forall a.
(Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Backwards f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (Backwards f a
x) =
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (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
(==) = 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 = 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 = 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 = forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1
instance (Functor f) => Functor (Backwards f) where
fmap :: forall a b. (a -> b) -> Backwards f a -> Backwards f b
fmap a -> b
f (Backwards f a
a) = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f f a
a)
{-# INLINE fmap #-}
instance (Applicative f) => Applicative (Backwards f) where
pure :: forall a. a -> Backwards f a
pure a
a = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a)
{-# INLINE pure #-}
Backwards f (a -> b)
f <*> :: forall a b. Backwards f (a -> b) -> Backwards f a -> Backwards f b
<*> Backwards f a
a = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a
a forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> f (a -> b)
f)
{-# INLINE (<*>) #-}
instance (Alternative f) => Alternative (Backwards f) where
empty :: forall a. Backwards f a
empty = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE empty #-}
Backwards f a
x <|> :: forall a. Backwards f a -> Backwards f a -> Backwards f a
<|> Backwards f a
y = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a
x forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f a
y)
{-# INLINE (<|>) #-}
instance (Foldable f) => Foldable (Backwards f) where
foldMap :: forall m a. Monoid m => (a -> m) -> Backwards f a -> m
foldMap a -> m
f (Backwards f a
t) = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f f a
t
{-# INLINE foldMap #-}
foldr :: forall a b. (a -> b -> b) -> b -> Backwards f a -> b
foldr a -> b -> b
f b
z (Backwards f a
t) = 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 :: forall b a. (b -> a -> b) -> b -> Backwards f a -> b
foldl b -> a -> b
f b
z (Backwards f a
t) = 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 :: forall a. (a -> a -> a) -> Backwards f a -> a
foldr1 a -> a -> a
f (Backwards f a
t) = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 a -> a -> a
f f a
t
{-# INLINE foldr1 #-}
foldl1 :: forall a. (a -> a -> a) -> Backwards f a -> a
foldl1 a -> a -> a
f (Backwards f a
t) = 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 :: forall a. Backwards f a -> Bool
null (Backwards f a
t) = forall (t :: * -> *) a. Foldable t => t a -> Bool
null f a
t
length :: forall a. Backwards f a -> Int
length (Backwards f a
t) = forall (t :: * -> *) a. Foldable t => t a -> Int
length f a
t
#endif
instance (Traversable f) => Traversable (Backwards f) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Backwards f a -> f (Backwards f b)
traverse a -> f b
f (Backwards f a
t) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (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 :: forall (f :: * -> *) a.
Applicative f =>
Backwards f (f a) -> f (Backwards f a)
sequenceA (Backwards f (f a)
t) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (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 :: forall a' a. (a' -> a) -> Backwards f a -> Backwards f a'
contramap a' -> a
f = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). Backwards f a -> f a
forwards
{-# INLINE contramap #-}
#endif