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 <.> :: (b -> c) -> (a -> m b) -> a -> m c
<.> a -> m b
g) a
a = b -> c
f (b -> c) -> m b -> m c
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 :: m a -> (a -> b) -> m b
for m a
a a -> b
b = (a -> b) -> m a -> m 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
<&> :: m a -> (a -> b) -> m b
(<&>) m a
a a -> b
b = (a -> b) -> m a -> m 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 = t (m b) -> m (t b)
forall (t :: * -> *) (m :: * -> *) a.
(Decoration t, Functor m) =>
t (m a) -> m (t a)
distributeF (t (m b) -> m (t b)) -> (t a -> t (m b)) -> t a -> m (t b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m b) -> t a -> t (m b)
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 = (m a -> m a) -> t (m a) -> m (t a)
forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF m a -> m a
forall a. a -> a
id
dmap :: Decoration t => (a -> b) -> t a -> t b
dmap :: (a -> b) -> t a -> t b
dmap a -> b
f = Identity (t b) -> t b
forall a. Identity a -> a
runIdentity (Identity (t b) -> t b) -> (t a -> Identity (t b)) -> t a -> t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Identity b) -> t a -> Identity (t b)
forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF (b -> Identity b
forall a. a -> Identity a
Identity (b -> Identity b) -> (a -> b) -> a -> Identity b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
dget :: Decoration t => t a -> a
dget :: t a -> a
dget = Const a (t Any) -> a
forall a k (b :: k). Const a b -> a
getConst (Const a (t Any) -> a) -> (t a -> Const a (t Any)) -> t a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Const a Any) -> t a -> Const a (t Any)
forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF a -> Const a Any
forall k a (b :: k). a -> Const a b
Const
instance Decoration Identity where
traverseF :: (a -> m b) -> Identity a -> m (Identity b)
traverseF a -> m b
f (Identity a
x) = b -> Identity b
forall a. a -> Identity a
Identity (b -> Identity b) -> m b -> m (Identity b)
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 :: (a -> m b) -> Compose d t a -> m (Compose d t b)
traverseF a -> m b
f (Compose d (t a)
x) = d (t b) -> Compose d t b
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (d (t b) -> Compose d t b) -> m (d (t b)) -> m (Compose d t b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (t a -> m (t b)) -> d (t a) -> m (d (t b))
forall (t :: * -> *) (m :: * -> *) a b.
(Decoration t, Functor m) =>
(a -> m b) -> t a -> m (t b)
traverseF ((a -> m b) -> t a -> m (t b)
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 :: (a -> m b) -> (a, a) -> m (a, b)
traverseF a -> m b
f (a
a, a
x) = (a
a,) (b -> (a, b)) -> m b -> m (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
f a
x