module Data.Functor.Reverse where
import Control.Applicative.Backwards
import Prelude hiding (foldr, foldr1, foldl, foldl1)
import Control.Applicative
import Data.Foldable
import Data.Traversable
import Data.Monoid
newtype Reverse f a = Reverse { getReverse :: f a }
instance (Functor f) => Functor (Reverse f) where
fmap f (Reverse a) = Reverse (fmap f a)
instance (Applicative f) => Applicative (Reverse f) where
pure a = Reverse (pure a)
Reverse f <*> Reverse a = Reverse (f <*> a)
instance (Alternative f) => Alternative (Reverse f) where
empty = Reverse empty
Reverse x <|> Reverse y = Reverse (x <|> y)
instance (Foldable f) => Foldable (Reverse f) where
foldMap f (Reverse t) = getDual (foldMap (Dual . f) t)
foldr f z (Reverse t) = foldl (flip f) z t
foldl f z (Reverse t) = foldr (flip f) z t
foldr1 f (Reverse t) = foldl1 (flip f) t
foldl1 f (Reverse t) = foldr1 (flip f) t
instance (Traversable f) => Traversable (Reverse f) where
traverse f (Reverse t) =
fmap Reverse . forwards $ traverse (Backwards . f) t
sequenceA (Reverse t) =
fmap Reverse . forwards $ sequenceA (fmap Backwards t)