Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Generic functors, foldables, and traversables.
Synopsis
- newtype GenericFunctor f a = GenericFunctor (f a)
- newtype GenericBifunctor f a b = GenericBifunctor (f a b)
- gfmap :: forall f a b. GFunctor f => (a -> b) -> f a -> f b
- gfoldMap :: forall t m a. (GFoldMap m t, Monoid m) => (a -> m) -> t a -> m
- gtraverse :: forall t f a b. (GTraverse f t, Applicative f) => (a -> f b) -> t a -> f (t b)
- gbimap :: forall f a b c d. GBimap f => (a -> b) -> (c -> d) -> f a c -> f b d
- gfirst :: forall f a b c. GFirst f => (a -> b) -> f a c -> f b c
- gsecond :: forall f a c d. GSecond f => (c -> d) -> f a c -> f a d
- gbifoldMap :: forall t m a b. (GBifoldMap m t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> m
- gbitraverse :: forall t f a b c d. (GBitraverse f t, Applicative f) => (a -> f b) -> (c -> f d) -> t a c -> f (t b d)
- class (forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f
- class (forall m. Monoid m => GFoldMap m t) => GFoldable t
- class (forall a. Generic (t a), forall a b. GFoldMapRep a b m t) => GFoldMap m t
- class (forall f. Applicative f => GBitraverse f t) => GTraversable t
- class (forall a. Generic (t a), forall a b. GTraverseRep a b f t) => GTraverse f t
- class (GBimap f, GFirst f, GSecond f) => GBifunctor f
- class (forall a c. Generic (f a c), forall a b c d. GBimapRep a b c d f) => GBimap f
- class (forall a c. Generic (f a c), forall a b c. GFirstRep a b c f) => GFirst f
- class (forall a c. Generic (f a c), forall a c d. GFunctorRep c d (f a)) => GSecond f
- class (forall m. Monoid m => GBifoldMap m t) => GBifoldable t
- class (forall a b. Generic (t a b), forall a b c d. GBifoldMapRep a b c d m t) => GBifoldMap m t
- class (forall f. Applicative f => GBitraverse f t) => GBitraversable t
- class (forall a b. Generic (t a b), forall a b c d. GBitraverseRep a b c d f t) => GBitraverse f t
Derive functors
Derive Functor, Bifunctor, Foldable, Traversable
DerivingVia
The type synonyms GenericFunctor
and GenericBifunctor
can be
used with the DerivingVia
extension to derive Functor
, Bifunctor
, and Foldable
.
Sadly, Traversable
cannot be derived-via.
newtype GenericFunctor f a Source #
newtype
for DerivingVia
of Functor
and Foldable
instances.
Note: the GHC extensions DeriveFunctor
, DeriveFoldable
, and DeriveTraversable
(which implies all three) already works out-of-the-box in most cases.
There are exceptions, such as the following example.
Example
{-# LANGUAGE DeriveGeneric, DerivingVia #-} import GHC.Generics (Generic
) import Generic.Functor (GenericFunctor
(..)) data Twice a = Twice (Either a a) derivingGeneric
deriving (Functor
,Foldable
) via (GenericFunctor
Twice)
GenericFunctor (f a) |
Instances
newtype GenericBifunctor f a b Source #
newtype
for DerivingVia
of Bifunctor
and Bifoldable
instances.
Note: although GenericBifunctor
has Functor
and Foldable
instances,
it is recommended to use GenericFunctor
instead for those two classes.
They have to use a separate deriving clause from Bifunctor
and Bifoldable
anyway.
Those instances exist because they are to become superclasses of Bifunctor
and Bifoldable
. The Foldable
instance of GenericBifunctor
is also less
efficient than GenericFunctor
unless the extra const mempty
gets optimized away.
Example
{-# LANGUAGE DeriveGeneric, DerivingVia #-} import Data.Bifoldable (Bifoldable
) import Data.Bifunctor (Bifunctor
) import GHC.Generics (Generic
) import Generic.Functor (GenericFunctor
(..),GenericBifunctor
(..)) data Tree a b = Node a (Tree a b) (Tree a b) | Leaf b derivingGeneric
deriving (Functor
,Foldable
) via (GenericFunctor
(Tree a)) deriving (Bifunctor
,Bifoldable
) via (GenericBifunctor
Tree) data CofreeF f a b = a :< f b derivingGeneric
deriving (Bifunctor
,Bifoldable
) via (GenericBifunctor
(CofreeF f))
GenericBifunctor (f a b) |
Instances
Generic method definitions
gfmap :: forall f a b. GFunctor f => (a -> b) -> f a -> f b Source #
Generic implementation of fmap
. See also GenericFunctor
for DerivingVia
,
using gfmap
under the hood.
Example
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic
) import Generic.Functor (gfmap
) data Twice a = Twice (Either a a) derivingGeneric
instanceFunctor
Twice wherefmap
=gfmap
gtraverse :: forall t f a b. (GTraverse f t, Applicative f) => (a -> f b) -> t a -> f (t b) Source #
Generic implementation of traverse
from Traversable
.
Bifunctors
gbimap :: forall f a b c d. GBimap f => (a -> b) -> (c -> d) -> f a c -> f b d Source #
Generic implementation of bimap
from Bifunctor
. See also GenericBifunctor
.
gfirst :: forall f a b c. GFirst f => (a -> b) -> f a c -> f b c Source #
Generic implementation of first
from Bifunctor
. See also GenericBifunctor
.
gsecond :: forall f a c d. GSecond f => (c -> d) -> f a c -> f a d Source #
Generic implementation of second
from Bifunctor
. See also GenericBifunctor
.
gbifoldMap :: forall t m a b. (GBifoldMap m t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> m Source #
Generic implementation of bifoldMap
from Bifoldable
.
gbitraverse :: forall t f a b c d. (GBitraverse f t, Applicative f) => (a -> f b) -> (c -> f d) -> t a c -> f (t b d) Source #
Generic implementation of bitraverse
from Bitraversable
.
Auxiliary classes
Related to standard classes
class (forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f Source #
Instances
(forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f Source # | |
Defined in Generic.Functor.Internal |
class (forall m. Monoid m => GFoldMap m t) => GFoldable t Source #
Generic Foldable
. Constraint for GenericFunctor
(deriving-via Foldable
).
class (forall a. Generic (t a), forall a b. GFoldMapRep a b m t) => GFoldMap m t Source #
Constraint for gfoldMap
.
Instances
(forall a. Generic (t a), forall a b. GFoldMapRep a b m t) => GFoldMap m t Source # | |
Defined in Generic.Functor.Internal |
class (forall f. Applicative f => GBitraverse f t) => GTraversable t Source #
Generic Traversable
.
Instances
(forall (f :: Type -> Type). Applicative f => GBitraverse f t) => GTraversable t Source # | |
Defined in Generic.Functor.Internal |
class (forall a. Generic (t a), forall a b. GTraverseRep a b f t) => GTraverse f t Source #
Constraint for gtraverse
.
Instances
(forall a. Generic (t a), forall a b. GTraverseRep a b f t) => GTraverse f t Source # | |
Defined in Generic.Functor.Internal |
Bifunctors
class (GBimap f, GFirst f, GSecond f) => GBifunctor f Source #
Generic Bifunctor
.
Instances
(GBimap f, GFirst f, GSecond f) => GBifunctor f Source # | |
Defined in Generic.Functor.Internal |
class (forall a c. Generic (f a c), forall a b c d. GBimapRep a b c d f) => GBimap f Source #
Constraint for gbimap
.
class (forall a c. Generic (f a c), forall a b c. GFirstRep a b c f) => GFirst f Source #
Constraint for gfirst
.
class (forall a c. Generic (f a c), forall a c d. GFunctorRep c d (f a)) => GSecond f Source #
Constraint for gsecond
.
Instances
(forall a c. Generic (f a c), forall a c d. GFunctorRep c d (f a)) => GSecond f Source # | |
Defined in Generic.Functor.Internal |
class (forall m. Monoid m => GBifoldMap m t) => GBifoldable t Source #
Generic Foldable
. Constraint for GenericFunctor
(deriving-via Foldable
).
Instances
(forall m. Monoid m => GBifoldMap m t) => GBifoldable t Source # | |
Defined in Generic.Functor.Internal |
class (forall a b. Generic (t a b), forall a b c d. GBifoldMapRep a b c d m t) => GBifoldMap m t Source #
Constraint for gbifoldMap
.
Instances
(forall a b. Generic (t a b), forall a b c d. GBifoldMapRep a b c d m t) => GBifoldMap m t Source # | |
Defined in Generic.Functor.Internal |
class (forall f. Applicative f => GBitraverse f t) => GBitraversable t Source #
Generic Bitraversable
.
Instances
(forall (f :: Type -> Type). Applicative f => GBitraverse f t) => GBitraversable t Source # | |
Defined in Generic.Functor.Internal |
class (forall a b. Generic (t a b), forall a b c d. GBitraverseRep a b c d f t) => GBitraverse f t Source #
Constraint for gtraverse
.
Instances
(forall a b. Generic (t a b), forall a b c d. GBitraverseRep a b c d f t) => GBitraverse f t Source # | |
Defined in Generic.Functor.Internal |