| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Generic.Functor
Description
Generic and generalized functors.
Synopsis
- gsolomap :: forall a b x y. (Generic x, Generic y, GSolomap a b x y) => (a -> b) -> x -> y
- solomap :: forall a b x y. Solomap a b x y => (a -> b) -> x -> y
- gmultimap :: forall arr x y. (Generic x, Generic y, GMultimap arr x y) => arr -> x -> y
- multimap :: forall arr x y. Multimap arr x y => arr -> x -> y
- data a :+ b = a :+ b
- 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
- class GMultimap (a -> b) x y => GSolomap a b x y
- class Multimap (a -> b) x y => Solomap a b x y
- class GMap1 (Default Incoherent arr) (Rep x) (Rep y) => GMultimap arr x y
- class MultimapI (Default Incoherent arr) x y => Multimap arr x y
Derive functors
Unary functors
gsolomap :: forall a b x y. (Generic x, Generic y, GSolomap a b x y) => (a -> b) -> x -> y Source #
Generalized generic functor.
gsolomap is a generalization of gfmap (generic fmap),
where the type parameter to be "mapped" does not have to be the last one.
gsolomap is unsafe: misuse will break your programs.
Read the Usage section below for details.
Example
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
import Generic.Functor (gsolomap)
data Result a r = Error a | Ok r -- Another name for Either
deriving Generic
mapError :: (a -> b) -> Result a r -> Result b r
mapError = gsolomap
mapOk :: (r -> s) -> Result a r -> Result a s
mapOk = gsolomap
mapBoth :: (a -> b) -> Result a a -> Result b b
mapBoth = gsolomap
Usage
(This also applies to solomap, gmultimap, and multimap.)
gsolomap should only be used to define polymorphic "fmap-like functions".
It works only in contexts where a and b are two distinct, non-unifiable
type variables. This is usually the case when they are bound by universal
quantification (forall a b. ...), with no equality constraints on a and
b.
The one guarantee of gsolomap is that .
Under the above conditions, that law and the types should uniquely determine
the implementation, which gsolomap id = idgsolomap seeks automatically.
The unsafety is due to the use of incoherent instances as part of the
definition of GSolomap. Functions are safe to specialize after GSolomap
(and Solomap) constraints have been discharged.
Note also that the type parameters of gsolomap must all be determined by
the context. For instance, composing two gsolomap, as in
, is a type error because the type in the middle
cannot be inferred.gsolomap f . gsolomap g
solomap :: forall a b x y. Solomap a b x y => (a -> b) -> x -> y Source #
Generalized implicit functor.
Use this when x and y are applications of existing functors
(Functor, Bifunctor).
This is a different use case from gfmap and gsolomap, which make
functors out of freshly declared data types.
solomap is unsafe: misuse will break your programs.
See the Usage section of gsolomap for details.
Example
map1 :: (a -> b) -> Either e (Maybe [IO a]) -> Either e (Maybe [IO b]) map1 =solomap-- equivalent to: fmap . fmap . fmap . fmap map2 :: (a -> b) -> (e -> Either [a] r) -> (e -> Either [b] r) map2 =solomap-- equivalent to: \f -> fmap (bimap (fmap f) id)
N-ary functors
gmultimap :: forall arr x y. (Generic x, Generic y, GMultimap arr x y) => arr -> x -> y Source #
Generic n-ary functor.
A generalization of gsolomap to map over multiple parameters simultaneously.
gmultimap takes a list of functions separated by ( and terminated by :+)().
gmultimap is unsafe: misuse will break your programs.
The type of every function in the list must be some (a -> b)
where a and b are distinct type variables.
See the Usage section of gsolomap for details.
Example
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
import Generic.Functor (gmultimap)
data Three a b c = One a | Two b | Three c
deriving Generic
mapThree :: (a -> a') -> (b -> b') -> (c -> c') -> Three a b c -> Three a' b' c'
mapThree f g h = gmultimap (f :+ g :+ h :+ ())
multimap :: forall arr x y. Multimap arr x y => arr -> x -> y Source #
Implicit n-ary functor.
A generalization of solomap to map over multiple parameters simultaneously.
multimap takes a list of functions separated by ( and terminated by :+)().
multimap is unsafe: misuse will break your programs.
The type of every function in the list must be some (a -> b)
where a and b are distinct type variables.
See the Usage section of gsolomap for details.
Example
type F a b c = Either a (b, c) map3 :: (a -> a') -> (b -> b') -> (c -> c') -> F a b c -> F a' b' c' map3 f g h =multimap(f:+g:+h:+()) -- equivalent to: \f g h -> bimap f (bimap g h)
Heterogeneous lists of arrows are constructed as lists separated by
( and terminated by :+)().
Example
Given f :: a -> a' and g :: b -> b',
(f is a list with the two elements :+ g :+ ())f and g.
if f :: a -> a' g :: b -> b' then f:+g:+() :: (a -> a'):+(b -> b'):+()
Those lists are used by gmultimap and multimap.
bimap_ :: (a -> a') -> (b -> b') -> (Maybe a, [Either b a]) -> (Maybe a', [Either b' a']) bimap_ f g =multimap(f:+g:+())
Constructors
| a :+ b infixr 1 |
Instances
| CatLike cat => Multimap_ cat (S arr (Rule AnyId Incoherent :+ arr')) x x Source # | |
Defined in Generic.Functor.Internal.Implicit | |
| Multimap_ cat (S arr (cat a b :+ arr')) a b Source # | |
| Multimap_ cat (S arr arr') x y => Multimap_ cat (S arr (() :+ arr')) x y Source # | |
| Multimap_ cat (S arr arr') x y => Multimap_ cat (S arr (NilArr :+ arr')) x y Source # | |
| Multimap_ cat (S arr (arr0 :+ (arr1 :+ arr2))) x y => Multimap_ cat (S arr ((arr0 :+ arr1) :+ arr2)) x y Source # | |
| Multimap_ cat (S arr arr') x y => Multimap_ cat (S arr (arr0 :+ arr')) x y Source # | |
| (FunctorOf cat f, MultimapOf cat arr x y) => Multimap_ cat (S arr (Rule AnyFunctor Incoherent :+ arr')) (f x) (f y) Source # | |
Defined in Generic.Functor.Internal.Implicit Methods multimap_ :: S arr (Rule AnyFunctor Incoherent :+ arr') -> cat (f x) (f y) Source # | |
| (CoercibleKleisli f a b, CoercibleKleisli f arr arr') => CoercibleKleisli f (a :+ arr) (b :+ arr') Source # | |
Defined in Generic.Functor.Internal.Implicit | |
| (BifunctorOf cat f, MultimapOf cat arr x1 y1, MultimapOf cat arr x2 y2) => Multimap_ cat (S arr (Rule AnyBifunctor Incoherent :+ arr')) (f x1 x2) (f y1 y2) Source # | |
Defined in Generic.Functor.Internal.Implicit Methods multimap_ :: S arr (Rule AnyBifunctor Incoherent :+ arr') -> cat (f x1 x2) (f y1 y2) Source # | |
| (MultimapOf ((->) :: Type -> Type -> Type) arr y1 x1, MultimapOf ((->) :: Type -> Type -> Type) arr x2 y2) => Multimap_ ((->) :: Type -> Type -> Type) (S arr (Rule AnyBifunctor Incoherent :+ arr')) (x1 -> x2) (y1 -> y2) Source # | |
Defined in Generic.Functor.Internal.Implicit Methods multimap_ :: S arr (Rule AnyBifunctor Incoherent :+ arr') -> (x1 -> x2) -> (y1 -> y2) Source # | |
| type WrapKleisli f (a :+ arr) Source # | |
Defined in Generic.Functor.Internal.Implicit | |
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)
deriving Generic
deriving (Functor, Foldable) via (GenericFunctor Twice)
Constructors
| GenericFunctor (f a) |
Instances
newtype GenericBifunctor f a b Source #
newtype for DerivingVia of Bifunctor and Bifoldable instances.
Note: deriving Bifunctor for a generic type often requires Functor
instances for types mentioned in the fields.
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
deriving Generic
deriving (Functor, Foldable) via (GenericFunctor (Tree a))
deriving (Bifunctor, Bifoldable) via (GenericBifunctor Tree)
data CofreeF f a b = a :< f b
deriving Generic
deriving (Bifunctor, Bifoldable) via (GenericBifunctor (CofreeF f))
Constructors
| GenericBifunctor (f a b) |
Instances
| GBifoldable f => Bifoldable (GenericBifunctor f) Source # | |
Defined in Generic.Functor.Internal Methods bifold :: Monoid m => GenericBifunctor f m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> GenericBifunctor f a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> GenericBifunctor f a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> GenericBifunctor f a b -> c # | |
| GBifunctor f => Bifunctor (GenericBifunctor f) Source # | |
Defined in Generic.Functor.Internal Methods bimap :: (a -> b) -> (c -> d) -> GenericBifunctor f a c -> GenericBifunctor f b d # first :: (a -> b) -> GenericBifunctor f a c -> GenericBifunctor f b c # second :: (b -> c) -> GenericBifunctor f a b -> GenericBifunctor f a c # | |
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)
deriving Generic
instance Functor Twice where
fmap = 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 | |
Generalized functors
class GMultimap (a -> b) x y => GSolomap a b x y Source #
Constraint for gsolomap.
Instances
| GMultimap (a -> b) x y => GSolomap a b x y Source # | |
Defined in Generic.Functor.Internal | |
class Multimap (a -> b) x y => Solomap a b x y Source #
Constraint for solomap.
Instances
| Multimap (a -> b) x y => Solomap a b x y Source # | |
Defined in Generic.Functor.Internal | |
class GMap1 (Default Incoherent arr) (Rep x) (Rep y) => GMultimap arr x y Source #
Constraint for gmultimap.
Instances
| GMap1 (Default Incoherent arr) (Rep x) (Rep y) => GMultimap arr x y Source # | |
Defined in Generic.Functor.Internal | |
class MultimapI (Default Incoherent arr) x y => Multimap arr x y Source #
Constraint for multimap.
Instances
| MultimapI (Default Incoherent arr) x y => Multimap arr x y Source # | |
Defined in Generic.Functor.Internal | |