{-# 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
#if MIN_VERSION_base(4,18,0)
import Data.Foldable1 (Foldable1(foldMap1))
#endif
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 { forall {k} (f :: k -> *) (a :: k). Backwards f a -> f a
forwards :: f a }
#if __GLASGOW_HASKELL__ >= 710
deriving (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 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 :: 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 #-}
a
x <$ :: forall a b. a -> Backwards f b -> Backwards f a
<$ Backwards f b
a = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (a
x forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f b
a)
{-# INLINE (<$) #-}
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 (<*>) #-}
#if MIN_VERSION_base(4,10,0)
liftA2 :: forall a b c.
(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) = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (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 *> :: forall a b. Backwards f a -> Backwards f b -> Backwards f b
*> Backwards f b
ys = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f b
ys forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* f a
xs)
{-# INLINE (*>) #-}
Backwards f a
ys <* :: forall a b. Backwards f a -> Backwards f b -> Backwards f a
<* Backwards f b
xs = forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f b
xs 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 :: 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
#if MIN_VERSION_base(4,18,0)
instance (Foldable1 f) => Foldable1 (Backwards f) where
foldMap1 f (Backwards t) = foldMap1 f t
{-# INLINE foldMap1 #-}
#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