module Agda.Utils.Functor
( (<.>)
, for
, Decoration(traverseF, distributeF)
, dmap
, dget
, (<$>)
, ($>)
, (<&>)
)
where
import Control.Applicative ( Const(Const), getConst )
import Data.Functor (($>))
import Data.Functor.Identity
import Data.Functor.Compose
infixr 9 <.>
(<.>) :: Functor m => (b -> c) -> (a -> m b) -> a -> m c
(b -> c
f <.> :: forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> a -> m b
g) a
a = b -> c
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
g a
a
for :: Functor m => m a -> (a -> b) -> m b
for :: forall (m :: * -> *) a b. Functor m => m a -> (a -> b) -> m b
for m a
a a -> b
b = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
b m a
a
{-# INLINE for #-}
infixl 1 <&>
(<&>) :: Functor m => m a -> (a -> b) -> m b
<&> :: forall (m :: * -> *) a b. Functor m => m a -> (a -> b) -> m b
(<&>) m a
a a -> b
b = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
b m a
a
{-# INLINE (<&>) #-}
class Functor t => Decoration t where
traverseF :: Functor m => (a -> m b) -> t a -> m (t b)
traverseF a -> m b
f = forall (t :: * -> *) (m :: * -> *) a.
(Decoration t, Functor m) =>
t (m a) -> m (t a)
distributeF forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> m b
f
distributeF :: (Functor m) => t (m a) -> m (t a)
distributeF = forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF forall a. a -> a
id
dmap :: Decoration t => (a -> b) -> t a -> t b
dmap :: forall (t :: * -> *) a b. Decoration t => (a -> b) -> t a -> t b
dmap a -> b
f = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
dget :: Decoration t => t a -> a
dget :: forall (t :: * -> *) a. Decoration t => t a -> a
dget = forall {k} a (b :: k). Const a b -> a
getConst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF forall {k} a (b :: k). a -> Const a b
Const
instance Decoration Identity where
traverseF :: forall (m :: * -> *) a b.
Functor m =>
(a -> m b) -> Identity a -> m (Identity b)
traverseF a -> m b
f (Identity a
x) = forall a. a -> Identity a
Identity forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
f a
x
instance (Decoration d, Decoration t) => Decoration (Compose d t) where
traverseF :: forall (m :: * -> *) a b.
Functor m =>
(a -> m b) -> Compose d t a -> m (Compose d t b)
traverseF a -> m b
f (Compose d (t a)
x) = forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF (forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF a -> m b
f) d (t a)
x
instance Decoration ((,) a) where
traverseF :: forall (m :: * -> *) a b.
Functor m =>
(a -> m b) -> (a, a) -> m (a, b)
traverseF a -> m b
f (a
a, a
x) = (a
a,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
f a
x