{-# 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 { forall {k} (f :: k -> *) (a :: k). 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
$cfrom :: forall k (f :: k -> *) (a :: k) x.
Backwards f a -> Rep (Backwards f a) x
from :: forall 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
to :: forall x. Rep (Backwards f a) x -> Backwards f a
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
$cfrom1 :: forall k (f :: k -> *) (a :: k).
Backwards f a -> Rep1 (Backwards f) a
from1 :: forall (a :: k). Backwards f a -> Rep1 (Backwards f) a
$cto1 :: forall k (f :: k -> *) (a :: k).
Rep1 (Backwards f) a -> Backwards f a
to1 :: forall (a :: k). Rep1 (Backwards f) a -> 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) = (a -> b -> Bool) -> f a -> f b -> Bool
forall a b. (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 :: 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) = (a -> b -> Ordering) -> f a -> f b -> Ordering
forall a b. (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 :: forall a.
(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 a. (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 :: 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) =
(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 a.
(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 :: forall a b. (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 a b. (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 <$ :: forall a b. 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 a b. 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 :: forall a. 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 a. a -> f a
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 = 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 :: 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) = 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 a b c. (a -> b -> c) -> f a -> f b -> 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 *> :: forall a b. 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 a b. f a -> f b -> f a
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 = 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 a b. f a -> f b -> f b
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 = f a -> Backwards f a
forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards f a
forall a. f a
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 = 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 a. 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 :: forall m a. Monoid m => (a -> m) -> Backwards f a -> m
foldMap a -> m
f (Backwards f a
t) = (a -> m) -> f a -> m
forall m a. Monoid m => (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 :: forall a b. (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 a b. (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 :: forall b a. (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 b a. (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 :: forall a. (a -> a -> a) -> Backwards f a -> a
foldr1 a -> a -> a
f (Backwards f a
t) = (a -> a -> a) -> f a -> a
forall a. (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 :: forall a. (a -> a -> a) -> Backwards f a -> a
foldl1 a -> a -> a
f (Backwards f a
t) = (a -> a -> a) -> f a -> a
forall a. (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 :: forall a. Backwards f a -> Bool
null (Backwards f a
t) = f a -> Bool
forall a. f a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null f a
t
length :: forall a. Backwards f a -> Int
length (Backwards f a
t) = f a -> Int
forall a. f a -> Int
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) = (f b -> Backwards f b) -> f (f b) -> f (Backwards f b)
forall a b. (a -> b) -> f a -> 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)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> f a -> f (f 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) = (f a -> Backwards f a) -> f (f a) -> f (Backwards f a)
forall a b. (a -> b) -> f a -> f b
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)
forall (f :: * -> *) a. Applicative f => f (f a) -> f (f 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 = f a' -> Backwards f a'
forall {k} (f :: k -> *) (a :: k). f a -> Backwards f a
Backwards (f a' -> Backwards f a')
-> (Backwards f a -> f a') -> Backwards f a -> Backwards f a'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a' -> a) -> f a -> f a'
forall a' a. (a' -> a) -> f a -> f a'
forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a' -> a
f (f a -> f a') -> (Backwards f a -> f a) -> Backwards f a -> f a'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Backwards f a -> f a
forall {k} (f :: k -> *) (a :: k). Backwards f a -> f a
forwards
{-# INLINE contramap #-}
#endif