Safe Haskell | None |
---|---|
Language | Haskell2010 |
This is an internal module. Look, don't touch.
Generic.Functor is the public API.
Synopsis
- gfmap :: forall f a b. GFunctor f => (a -> b) -> f a -> f b
- 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
- class (forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f
- class GMap1 a b (Rep (f a)) (Rep (f b)) => GFunctorRep a b f
- class GMap1 a b (Rep x) (Rep y) => GSolomap a b x y
- class Solomap_ a b x y => Solomap a b x y
- newtype DeriveFunctor f a = DeriveFunctor (f a)
- class GMap1 a b f g where
- gmap1 :: (a -> b) -> f () -> g ()
- class Solomap_ a b x y where
- solomap_ :: (a -> b) -> x -> y
Documentation
gfmap :: forall f a b. GFunctor f => (a -> b) -> f a -> f b Source #
Generic implementation of fmap
. See also DeriveFunctor
for deriving-via,
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
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 derivingGeneric
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
.)
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
= id
gsolomap
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.
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)
Constraints for gfmap
class (forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f Source #
Constraint for gfmap
.
Instances
(forall a. Generic (f a), forall a b. GFunctorRep a b f) => GFunctor f Source # | |
Defined in Generic.Functor.Internal |
class GMap1 a b (Rep (f a)) (Rep (f b)) => GFunctorRep a b f Source #
Internal component of GFunctor
.
This is an example of the "quantified constraints trick" to encode
forall a b. GMap1 a b (Rep (f a)) (Rep (f b))
which doesn't actually
work as-is.
Instances
GMap1 a b (Rep (f a)) (Rep (f b)) => GFunctorRep a b f Source # | |
Defined in Generic.Functor.Internal |
Constraint for gsolomap
Constraint for solomap
class Solomap_ a b x y => Solomap a b x y Source #
Constraint for solomap
.
Instances
Solomap_ a b x y => Solomap a b x y Source # | |
Defined in Generic.Functor.Internal |
Deriving Via
newtype DeriveFunctor f a Source #
newtype
for DerivingVia
of Functor
instances.
Note: the GHC extension DeriveFunctor
already works out-of-the-box in most
cases. There are exceptions, such as the following example:
{-# LANGUAGE DeriveGeneric, DerivingVia #-} import GHC.Generics (Generic
) import Generic.Functor (DeriveFunctor
(..)) data Twice a = Twice (Either a a) derivingGeneric
derivingFunctor
via (DeriveFunctor
Twice)
DeriveFunctor (f a) |
Instances
GFunctor f => Functor (DeriveFunctor f) Source # | |
Defined in Generic.Functor.Internal fmap :: (a -> b) -> DeriveFunctor f a -> DeriveFunctor f b # (<$) :: a -> DeriveFunctor f b -> DeriveFunctor f a # |
class GMap1 a b f g where Source #
Instances
GMap1 a b (V1 :: Type -> Type) (V1 :: Type -> Type) Source # | |
GMap1 a b (U1 :: Type -> Type) (U1 :: Type -> Type) Source # | |
Solomap_ a b x y => GMap1 a b (K1 i x :: Type -> Type) (K1 i' y :: Type -> Type) Source # | |
(GMap1 a b f1 g1, GMap1 a b f2 g2) => GMap1 a b (f1 :*: f2) (g1 :*: g2) Source # | |
(GMap1 a b f1 g1, GMap1 a b f2 g2) => GMap1 a b (f1 :+: f2) (g1 :+: g2) Source # | |
GMap1 a b f g => GMap1 a b (M1 i c f) (M1 i' c'' g) Source # | |
class Solomap_ a b x y where Source #
Internal implementation of Solomap
.
Instances
Solomap_ a b x x Source # | |
Defined in Generic.Functor.Internal | |
Solomap_ a b a b Source # | |
Defined in Generic.Functor.Internal | |
Solomap_ a b (f x) (f x) Source # | |
Defined in Generic.Functor.Internal | |
(Functor f, Solomap_ a b x y) => Solomap_ a b (f x) (f y) Source # | |
Defined in Generic.Functor.Internal | |
Solomap_ a b (f x y) (f x y) Source # | |
Defined in Generic.Functor.Internal | |
(Functor (f x), Solomap_ a b x2 y2) => Solomap_ a b (f x x2) (f x y2) Source # | |
Defined in Generic.Functor.Internal | |
(Bifunctor f, Solomap_ a b x1 y1, Solomap_ a b x2 y2) => Solomap_ a b (f x1 x2) (f y1 y2) Source # | |
Defined in Generic.Functor.Internal | |
(Solomap_ a b y1 x1, Solomap_ a b x2 y2) => Solomap_ a b (x1 -> x2) (y1 -> y2) Source # | |
Defined in Generic.Functor.Internal |