Safe Haskell | None |
---|---|
Language | Haskell2010 |
Generic combinators to derive type class instances.
Overview
base classes that GHC can not derive instances for, as of version 8.2:
Semigroup
,Monoid
,Applicative
,Alternative
,Eq1
,Ord1
,Show1
.
On base < 4.12 (i.e., GHC < 8.6), import Generic.Data.Orphans to obtain instances needed internally to derive those.
GHC can derive instances for other classes here, although there may be types supported by one method but not the other or vice versa.
Minor discrepancies
Here are documented some corner cases of deriving, both by GHC and generic-data. They are all minor and unlikely to cause problems in practice.
Empty types
- Some of the derived methods are lazy, which might result in errors being silenced, though unlikely.
- The only generic-data implementation which differs from GHC stock
instances is
gfoldMap
.
Class method | GHC stock | generic-data | Comment |
( | lazy | lazy | True |
compare | lazy | lazy | EQ |
fmap | strict | strict | must be bottom anyway |
foldMap | lazy | strict | mempty if lazy |
foldr | lazy | lazy | returns accumulator |
traverse | strict | strict | |
sequenceA | strict | strict |
Single-constructor single-field types
data
types with one constructor and one field are extremely rare.
newtype
is almost always more appropriate (for which there is no issue).
That said, for data
types both strict and lazy, all generic-data
implementations are lazy (they don't even force the constructor),
whereas GHC stock implementations, when they exist, are strict.
Functor composition
Fields of functors involving the composition of two or more functors
f (g (h a))
result in some overhead using GHC.Generics.Generic1.
This is due to a particular encoding choice of GHC.Generics
, where
composition are nested to the right instead of to the left. f (g (h _))
is
represented by the functor f
, so one must use
:.:
(g :.:
Rec1
h)fmap
on f
to convert that back to `f (g (h _))`. A better choice would
have been to encode it as (
, because that is
coercible back to Rec1
f :.:
g) :.:
hf (g (h _))
.
Synopsis
- newtype Generically a = Generically {
- unGenerically :: a
- newtype GenericProduct a = GenericProduct {
- unGenericProduct :: a
- newtype FiniteEnumeration a = FiniteEnumeration {
- unFiniteEnumeration :: a
- newtype Generically1 f a = Generically1 {
- unGenerically1 :: f a
- gmappend :: (Generic a, Semigroup (Rep a ())) => a -> a -> a
- gmempty :: (Generic a, Monoid (Rep a ())) => a
- gmappend' :: (Generic a, Monoid (Rep a ())) => a -> a -> a
- geq :: (Generic a, Eq (Rep a ())) => a -> a -> Bool
- gcompare :: (Generic a, Ord (Rep a ())) => a -> a -> Ordering
- greadPrec :: (Generic a, GRead0 (Rep a)) => ReadPrec a
- type GRead0 = GRead Proxy
- gshowsPrec :: (Generic a, GShow0 (Rep a)) => Int -> a -> ShowS
- type GShow0 = GShow Proxy
- class GEnum opts f
- data StandardEnum
- gtoEnum :: (Generic a, GEnum StandardEnum (Rep a)) => Int -> a
- gfromEnum :: (Generic a, GEnum StandardEnum (Rep a)) => a -> Int
- genumFrom :: (Generic a, GEnum StandardEnum (Rep a)) => a -> [a]
- genumFromThen :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
- genumFromTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
- genumFromThenTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> a -> [a]
- data FiniteEnum
- gtoFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => Int -> a
- gfromFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> Int
- gfiniteEnumFrom :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> [a]
- gfiniteEnumFromThen :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
- gfiniteEnumFromTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
- gfiniteEnumFromThenTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> a -> [a]
- gminBound :: (Generic a, GBounded (Rep a)) => a
- gmaxBound :: (Generic a, GBounded (Rep a)) => a
- class GBounded f
- grange :: (Generic a, GIx (Rep a)) => (a, a) -> [a]
- gindex :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Int
- ginRange :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Bool
- class GIx f
- gunsafeIndex :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Int
- gfmap :: (Generic1 f, Functor (Rep1 f)) => (a -> b) -> f a -> f b
- gconstmap :: (Generic1 f, Functor (Rep1 f)) => a -> f b -> f a
- gfoldMap :: (Generic1 f, GFoldable (Rep1 f), Monoid m) => (a -> m) -> f a -> m
- gfoldr :: (Generic1 f, Foldable (Rep1 f)) => (a -> b -> b) -> b -> f a -> b
- class GFoldable_ t => GFoldable t
- gtraverse :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => (a -> m b) -> f a -> m (f b)
- gsequenceA :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => f (m a) -> m (f a)
- class GTraversable_ t => GTraversable t
- gpure :: (Generic1 f, Applicative (Rep1 f)) => a -> f a
- gap :: (Generic1 f, Applicative (Rep1 f)) => f (a -> b) -> f a -> f b
- gliftA2 :: (Generic1 f, Applicative (Rep1 f)) => (a -> b -> c) -> f a -> f b -> f c
- gempty :: (Generic1 f, Alternative (Rep1 f)) => f a
- galt :: (Generic1 f, Alternative (Rep1 f)) => f a -> f a -> f a
- gliftEq :: (Generic1 f, Eq1 (Rep1 f)) => (a -> b -> Bool) -> f a -> f b -> Bool
- gliftCompare :: (Generic1 f, Ord1 (Rep1 f)) => (a -> b -> Ordering) -> f a -> f b -> Ordering
- gliftReadPrec :: (Generic1 f, GRead1 (Rep1 f)) => ReadPrec a -> ReadPrec [a] -> ReadPrec (f a)
- type GRead1 = GRead Identity
- gliftShowsPrec :: (Generic1 f, GShow1 (Rep1 f)) => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
- type GShow1 = GShow Identity
- newtype Id1 f a = Id1 {
- unId1 :: f a
- newtype Opaque a = Opaque {
- unOpaque :: a
- newtype Opaque1 f a = Opaque1 {
- unOpaque1 :: f a
- class (Generic a, Coercible a (Old a), Newtype' a) => Newtype a
- type Old a = GOld (Rep a)
- pack :: Newtype a => Old a -> a
- unpack :: Newtype a => a -> Old a
- gcoerce :: (Generic a, Generic b, Coercible (Rep a) (Rep b)) => a -> b
- gcoerceBinop :: (Generic a, Generic b, Coercible (Rep a) (Rep b)) => (a -> a -> a) -> b -> b -> b
- gdatatypeName :: forall a. (Generic a, GDatatype (Rep a)) => String
- gmoduleName :: forall a. (Generic a, GDatatype (Rep a)) => String
- gpackageName :: forall a. (Generic a, GDatatype (Rep a)) => String
- gisNewtype :: forall a. (Generic a, GDatatype (Rep a)) => Bool
- class GDatatype f
- gconName :: forall a. Constructors a => a -> String
- gconFixity :: forall a. Constructors a => a -> Fixity
- gconIsRecord :: forall a. Constructors a => a -> Bool
- gconNum :: forall a. Constructors a => Int
- gconIndex :: forall a. Constructors a => a -> Int
- class (Generic a, GConstructors (Rep a)) => Constructors a
- class GConstructors r
- data ConId a
- conId :: forall a. Constructors a => a -> ConId a
- conIdToInt :: forall a. ConId a -> Int
- conIdToString :: forall a. Constructors a => ConId a -> String
- conIdEnum :: forall a. Constructors a => [ConId a]
- conIdNamed :: forall s a. ConIdNamed s a => ConId a
- class (Generic a, KnownNat (ConIdNamed' n a)) => ConIdNamed n a
- conIdMin :: forall a. (Constructors a, NonEmptyType "conIdMin" a) => ConId a
- conIdMax :: forall a. (Constructors a, NonEmptyType "conIdMax" a) => ConId a
- class NonEmptyType_ fname a => NonEmptyType fname a
- type IsEmptyType a = IsEmptyType_ a
- type family MetaOf (f :: * -> *) :: Meta where ...
- type family MetaDataName (m :: Meta) :: Symbol where ...
- type family MetaDataModule (m :: Meta) :: Symbol where ...
- type family MetaDataPackage (m :: Meta) :: Symbol where ...
- type family MetaDataNewtype (m :: Meta) :: Bool where ...
- type family MetaConsName (m :: Meta) :: Symbol where ...
- type family MetaConsFixity (m :: Meta) :: FixityI where ...
- type family MetaConsRecord (m :: Meta) :: Bool where ...
- type family MetaSelNameM (m :: Meta) :: Maybe Symbol where ...
- type family MetaSelName (m :: Meta) :: Symbol where ...
- type family MetaSelUnpack (m :: Meta) :: SourceUnpackedness where ...
- type family MetaSelSourceStrictness (m :: Meta) :: SourceStrictness where ...
- type family MetaSelStrictness (m :: Meta) :: DecidedStrictness where ...
- class Generic a
- class Generic1 (f :: k -> Type)
Newtypes for Deriving Via
newtype Generically a Source #
Type with instances derived via Generic
.
Examples
Deriving Eq
, Ord
, Show
, Read
>>>
:{
data T = C Int Bool deriving Generic deriving (Eq, Ord, Show, Read) via (Generically T) :}
Deriving Semigroup
, Monoid
The type must have only one constructor.
>>>
import Data.Monoid (Sum)
>>>
:{
data U = D [Int] (Sum Int) deriving Generic deriving (Semigroup, Monoid) via (Generically U) :}
Deriving Enum
, Bounded
The type must have only nullary constructors.
To lift that restriction, see FiniteEnumeration
.
>>>
:{
data V = X | Y | Z deriving Generic deriving (Eq, Ord, Enum, Bounded) via (Generically V) :}
Generically | |
|
Instances
newtype GenericProduct a Source #
Product type with generic instances of Semigroup
and Monoid
.
This is similar to Generically
in most cases, but
GenericProduct
also works for types T
with deriving
via
, where GenericProduct
UU
is a generic product type coercible to,
but distinct from T
. In particular, U
may not have an instance of
Semigroup
, which Generically
requires.
Example
>>>
import Data.Monoid (Sum(..))
>>>
data Point a = Point a a deriving Generic
>>>
:{
newtype Vector a = Vector (Point a) deriving (Semigroup, Monoid) via GenericProduct (Point (Sum a)) :}
If it were via
instead, then
Generically
(Point (Sum a))Vector
's mappend
(the Monoid
method) would be defined as Point
's
(
(the <>
)Semigroup
method), which might not exist, or might not be
equivalent to Vector
's generic Semigroup
instance, which would be
unlawful.
Instances
Generic a => Generic (GenericProduct a) Source # | |
Defined in Generic.Data.Internal.Generically type Rep (GenericProduct a) :: Type -> Type # from :: GenericProduct a -> Rep (GenericProduct a) x # to :: Rep (GenericProduct a) x -> GenericProduct a # | |
(AssertNoSum Semigroup a, Generic a, Semigroup (Rep a ())) => Semigroup (GenericProduct a) Source # | |
Defined in Generic.Data.Internal.Generically (<>) :: GenericProduct a -> GenericProduct a -> GenericProduct a # sconcat :: NonEmpty (GenericProduct a) -> GenericProduct a # stimes :: Integral b => b -> GenericProduct a -> GenericProduct a # | |
(AssertNoSum Semigroup a, Generic a, Monoid (Rep a ())) => Monoid (GenericProduct a) Source # | |
Defined in Generic.Data.Internal.Generically mempty :: GenericProduct a # mappend :: GenericProduct a -> GenericProduct a -> GenericProduct a # mconcat :: [GenericProduct a] -> GenericProduct a # | |
type Rep (GenericProduct a) Source # | |
Defined in Generic.Data.Internal.Generically |
newtype FiniteEnumeration a Source #
Type with Enum
instance derived via Generic
with FiniteEnum
option.
This allows deriving Enum
for types whose constructors have fields.
Some caution is advised; see details in FiniteEnum
.
Example
>>>
:{
data Booool = Booool Bool Bool deriving Generic deriving (Enum, Bounded) via (FiniteEnumeration Booool) :}
Instances
newtype Generically1 f a Source #
Type with instances derived via Generic1
.
Examples
Deriving Functor
, Applicative
, Alternative
Applicative
can be derived for types with only one
constructor, aka. products.
>>>
:{
data F a = F1 a | F2 (Maybe a) | F3 [Either Bool a] (Int, a) deriving Generic1 deriving Functor via (Generically1 F) :}
>>>
:{
data G a = G a (Maybe a) [a] (IO a) deriving Generic1 deriving (Functor, Applicative) via (Generically1 G) :}
>>>
import Control.Applicative (Alternative)
>>>
:{
data G' a = G' (Maybe a) [a] deriving Generic1 deriving (Functor, Applicative, Alternative) via (Generically1 G') :}
Deriving Foldable
>>>
import Generic.Data.Orphans ()
>>>
:{
data H a = H1 a | H2 (Maybe a) deriving Generic1 deriving (Functor, Foldable) via (Generically1 H) :}
Note: we can't use DerivingVia
for Traversable
.
One may implement Traversable
explicitly using gtraverse
.
Deriving Eq1
, Ord1
>>>
import Data.Functor.Classes (Eq1, Ord1)
>>>
:{
data I a = I [a] (Maybe a) deriving Generic1 deriving (Eq1, Ord1) via (Generically1 I) :}
Generically1 | |
|
Instances
Regular classes
Default implementations for classes indexed by types
(kind Type
).
Semigroup
Monoid
Eq
Can also be derived by GHC as part of the standard.
Ord
Can also be derived by GHC as part of the standard.
Read
Can also be derived by GHC as part of the standard.
greadPrec :: (Generic a, GRead0 (Rep a)) => ReadPrec a Source #
Generic readPrec
.
instanceRead
MyType wherereadPrec
=greadPrec
readListPrec
=readListPrecDefault
Show
Can also be derived by GHC as part of the standard.
gshowsPrec :: (Generic a, GShow0 (Rep a)) => Int -> a -> ShowS Source #
Generic showsPrec
.
instanceShow
MyType whereshowsPrec
=gshowsPrec
Enum
Generic representation of Enum
types.
The opts
parameter is a type-level option to select different
implementations.
Instances
GEnum opts (U1 :: Type -> Type) Source # | |
(GEnum opts f, GEnum opts g) => GEnum opts (f :+: g) Source # | |
(Bounded c, Enum c) => GEnum FiniteEnum (K1 i c :: Type -> Type) Source # | |
(GEnum FiniteEnum f, GEnum FiniteEnum g) => GEnum FiniteEnum (f :*: g) Source # | |
GEnum opts f => GEnum opts (M1 i c f) Source # | |
StandardEnum option
Can also be derived by GHC as part of the standard.
data StandardEnum Source #
Standard option for GEnum
: derive Enum
for types with only nullary
constructors (the same restrictions as in the Haskell 2010
report).
gtoEnum :: (Generic a, GEnum StandardEnum (Rep a)) => Int -> a Source #
Generic toEnum
generated with the StandardEnum
option.
instanceEnum
MyType wheretoEnum
=gtoEnum
fromEnum
=gfromEnum
enumFrom
=genumFrom
enumFromThen
=genumFromThen
enumFromTo
=genumFromTo
enumFromThenTo
=genumFromThenTo
gfromEnum :: (Generic a, GEnum StandardEnum (Rep a)) => a -> Int Source #
Generic fromEnum
generated with the StandardEnum
option.
See also gtoEnum
.
genumFrom :: (Generic a, GEnum StandardEnum (Rep a)) => a -> [a] Source #
Generic enumFrom
generated with the StandardEnum
option.
See also gtoEnum
.
genumFromThen :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a] Source #
Generic enumFromThen
generated with the StandardEnum
option.
See also gtoEnum
.
genumFromTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a] Source #
Generic enumFromTo
generated with the StandardEnum
option.
See also gtoEnum
.
genumFromThenTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> a -> [a] Source #
Generic enumFromThenTo
generated with the StandardEnum
option.
See also gtoEnum
.
FiniteEnum option
data FiniteEnum Source #
Extends the StandardEnum
option for GEnum
to allow all constructors to
have arbitrary many fields. Each field type must be an instance of
both Enum
and Bounded
. Avoid fields of types Int
and Word
.
Details
Two restrictions require the user's attention:
- The
Enum
instances of the field types need to start enumerating from 0. In particular,Int
is an unfit field type, because the enumeration of the negative values starts before 0. - There can only be up to
values (because the implementation represents the cardinality explicitly as anmaxBound
::Int
Int
). This restriction makesWord
an invalid field type as well. Notably, it is insufficient for each individual field types to stay below this limit. Instead it applies to the generic type as a whole.
Elements are numbered by toEnum
, from 0
up to (cardinality - 1)
.
The resulting ordering matches the generic Ord
instance defined by
gcompare
.
The values from different constructors are enumerated sequentially.
data Example = C0 Bool Bool | C1 Bool deriving (Eq
,Ord
,Show
,Generic
) cardinality = 6 -- 2 * 2 + 2 -- Bool * Bool | Bool enumeration = [ C0 False False , C0 False True , C0 True False , C0 True True , C1 False , C1 True ] enumeration == mapgtoFiniteEnum
[0 .. 5] [0 .. 5] == mapgfromFiniteEnum
enumeration
Instances
(Bounded c, Enum c) => GEnum FiniteEnum (K1 i c :: Type -> Type) Source # | |
(GEnum FiniteEnum f, GEnum FiniteEnum g) => GEnum FiniteEnum (f :*: g) Source # | |
gtoFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => Int -> a Source #
Generic toEnum
generated with the FiniteEnum
option.
instanceEnum
MyType wheretoEnum
=gtoFiniteEnum
fromEnum
=gfromFiniteEnum
enumFrom
=gfiniteEnumFrom
enumFromThen
=gfiniteEnumFromThen
enumFromTo
=gfiniteEnumFromTo
enumFromThenTo
=gfiniteEnumFromThenTo
gfromFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> Int Source #
Generic fromEnum
generated with the FiniteEnum
option.
See also gtoFiniteEnum
.
gfiniteEnumFrom :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> [a] Source #
Generic enumFrom
generated with the FiniteEnum
option.
See also gtoFiniteEnum
.
gfiniteEnumFromThen :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a] Source #
Generic enumFromThen
generated with the FiniteEnum
option.
See also gtoFiniteEnum
.
gfiniteEnumFromTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a] Source #
Generic enumFromTo
generated with the FiniteEnum
option.
See also gtoFiniteEnum
.
gfiniteEnumFromThenTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> a -> [a] Source #
Generic enumFromThenTo
generated with the FiniteEnum
option.
See also gtoFiniteEnum
.
Bounded
Can also be derived by GHC as part of the standard.
Generic representation of Bounded
types.
Ix
Can also be derived by GHC as part of the standard.
Generic representation of Ix
types.
gunsafeIndex :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Int Source #
Generic unsafeIndex
.
Details
The functions unsafeIndex
and unsafeRangeSize
belong to Ix
but are
internal to GHC and hence not exported from the module Data.Ix. However they
are exported from the module GHC.Arr
.
See grange
for how to define an instance of Ix
such that it does not
depend on the stability of GHCs internal API. Unfortunately this results in
additional (unnecessary) bound checks.
With the danger of having no stability guarantees for GHC's internal API one
can alternatively define an instance of Ix
as
import GHC.Arr instanceIx
MyType whererange
=grange
unsafeIndex =gunsafeIndex
inRange
=ginRange
Higher-kinded classes
Default implementations for classes indexed by type constructors
(kind Type -> Type
).
Functor
Can also be derived by GHC (DeriveFunctor
extension).
Foldable
Can also be derived by GHC (DeriveFoldable
extension).
class GFoldable_ t => GFoldable t Source #
Class of generic representations for which Foldable
can be derived.
Instances
GFoldable_ t => GFoldable t Source # | |
Defined in Generic.Data.Internal.Traversable |
Traversable
Can also be derived by GHC (DeriveTraversable
extension).
gtraverse :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => (a -> m b) -> f a -> m (f b) Source #
Generic traverse
.
instanceTraversable
MyTypeF wheretraverse
=gtraverse
gsequenceA :: (Generic1 f, GTraversable (Rep1 f), Applicative m) => f (m a) -> m (f a) Source #
class GTraversable_ t => GTraversable t Source #
Class of generic representations for which Traversable
can be derived.
Instances
GTraversable_ t => GTraversable t Source # | |
Defined in Generic.Data.Internal.Traversable |
Applicative
Alternative
Eq1
gliftEq :: (Generic1 f, Eq1 (Rep1 f)) => (a -> b -> Bool) -> f a -> f b -> Bool Source #
Generic liftEq
.
Ord1
gliftCompare :: (Generic1 f, Ord1 (Rep1 f)) => (a -> b -> Ordering) -> f a -> f b -> Ordering Source #
Generic liftCompare
.
Read1
gliftReadPrec :: (Generic1 f, GRead1 (Rep1 f)) => ReadPrec a -> ReadPrec [a] -> ReadPrec (f a) Source #
Generic liftReadPrec
.
Show1
gliftShowsPrec :: (Generic1 f, GShow1 (Rep1 f)) => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS Source #
Generic liftShowsPrec
.
Fields wrappers for deriving
A newtype whose instances for simple classes (Eq
, Ord
, Read
, Show
)
use higher-kinded class instances for f
(Eq1
, Ord1
, Read1
, Show1
).
Instances
Eq1 f => Eq1 (Id1 f) Source # | |
Ord1 f => Ord1 (Id1 f) Source # | |
Defined in Generic.Data.Internal.Resolvers | |
Read1 f => Read1 (Id1 f) Source # | |
Defined in Generic.Data.Internal.Resolvers | |
Show1 f => Show1 (Id1 f) Source # | |
(Eq1 f, Eq a) => Eq (Id1 f a) Source # | |
(Ord1 f, Ord a) => Ord (Id1 f a) Source # | |
Defined in Generic.Data.Internal.Resolvers | |
(Read1 f, Read a) => Read (Id1 f a) Source # | |
(Show1 f, Show a) => Show (Id1 f a) Source # | |
A newtype with trivial instances, that considers
every value equivalent to every other one,
and shows as just "_"
.
A higher-kinded version of Opaque
.
Instances
Eq1 (Opaque1 f) Source # | All equal. |
Ord1 (Opaque1 f) Source # | All equal. |
Defined in Generic.Data.Internal.Resolvers | |
Show1 (Opaque1 f) Source # | Shown as |
Eq (Opaque1 f a) Source # | All equal. |
Ord (Opaque1 f a) Source # | All equal. |
Defined in Generic.Data.Internal.Resolvers | |
Show (Opaque1 f a) Source # | Shown as |
Newtype
Generic pack/unpack.
class (Generic a, Coercible a (Old a), Newtype' a) => Newtype a Source #
type Old a = GOld (Rep a) Source #
The type wrapped by a newtype.
newtype Foo = Foo { bar :: Bar } deriving Generic
-- Old Foo ~ Bar
Generic coercions
gcoerce :: (Generic a, Generic b, Coercible (Rep a) (Rep b)) => a -> b Source #
Convert between types with representationally equivalent generic representations.
gcoerceBinop :: (Generic a, Generic b, Coercible (Rep a) (Rep b)) => (a -> a -> a) -> b -> b -> b Source #
Compose gcoerce
with a binary operation.
Accessing metadata
Using TypeApplications
.
Datatype
gdatatypeName :: forall a. (Generic a, GDatatype (Rep a)) => String Source #
Name of the first data constructor in a type as a string.
>>>
gdatatypeName @(Maybe Int)
"Maybe"
gmoduleName :: forall a. (Generic a, GDatatype (Rep a)) => String Source #
Name of the module where the first type constructor is defined.
>>>
gmoduleName @(ZipList Int)
"Control.Applicative"
gpackageName :: forall a. (Generic a, GDatatype (Rep a)) => String Source #
Name of the package where the first type constructor is defined.
>>>
gpackageName @(Maybe Int)
"base"
gisNewtype :: forall a. (Generic a, GDatatype (Rep a)) => Bool Source #
True
if the first type constructor is a newtype.
>>>
gisNewtype @[Int]
False>>>
gisNewtype @(ZipList Int)
True
Generic representations that contain datatype metadata.
Instances
Datatype d => GDatatype (M1 D d f :: k -> Type) Source # | |
Defined in Generic.Data.Internal.Meta gDatatypeName :: String Source # gModuleName :: String Source # gPackageName :: String Source # gIsNewtype :: Bool Source # |
Constructor
gconName :: forall a. Constructors a => a -> String Source #
Name of the first constructor in a value.
>>>
gconName (Just 0)
"Just"
gconFixity :: forall a. Constructors a => a -> Fixity Source #
The fixity of the first constructor.
>>>
import GHC.Generics ((:*:)(..))
>>>
gconFixity (Just 0)
Prefix>>>
gconFixity ([] :*: id)
Infix RightAssociative 6
gconIsRecord :: forall a. Constructors a => a -> Bool Source #
True
if the constructor is a record.
>>>
gconIsRecord (Just 0)
False>>>
gconIsRecord (Sum 0) -- Note: newtype Sum a = Sum { getSum :: a }
True
gconNum :: forall a. Constructors a => Int Source #
Number of constructors.
>>>
gconNum @(Maybe Int)
2
gconIndex :: forall a. Constructors a => a -> Int Source #
Index of a constructor.
>>>
gconIndex Nothing
0>>>
gconIndex (Just "test")
1
class (Generic a, GConstructors (Rep a)) => Constructors a Source #
Constraint synonym for Generic
and GConstructors
.
Instances
(Generic a, GConstructors (Rep a)) => Constructors a Source # | |
Defined in Generic.Data.Internal.Meta |
class GConstructors r Source #
Generic representations that contain constructor metadata.
Instances
GConstructors (V1 :: k -> Type) Source # | |
(GConstructors f, GConstructors g) => GConstructors (f :+: g :: k -> Type) Source # | |
Defined in Generic.Data.Internal.Meta | |
Constructor c => GConstructors (M1 C c f :: k -> Type) Source # | |
Defined in Generic.Data.Internal.Meta | |
GConstructors f => GConstructors (M1 D c f :: k -> Type) Source # | |
Defined in Generic.Data.Internal.Meta |
Constructor tags
An opaque identifier for a constructor.
conId :: forall a. Constructors a => a -> ConId a Source #
Identifier of a constructor.
conIdToInt :: forall a. ConId a -> Int Source #
Index of a constructor, given its identifier.
See also gconIndex
.
conIdToString :: forall a. Constructors a => ConId a -> String Source #
Name of a constructor. See also gconName
.
conIdEnum :: forall a. Constructors a => [ConId a] Source #
conIdNamed :: forall s a. ConIdNamed s a => ConId a Source #
Get a ConId
by name.
>>>
conIdNamed @"Nothing" :: ConId (Maybe Int)
ConId 0>>>
conIdNamed @"Just" :: ConId (Maybe Int)
ConId 1
class (Generic a, KnownNat (ConIdNamed' n a)) => ConIdNamed n a Source #
Constraint synonym for generic types a
with a constructor named n
.
Instances
(Generic a, KnownNat (ConIdNamed' n a)) => ConIdNamed n a Source # | |
Defined in Generic.Data.Internal.Meta |
conIdMin :: forall a. (Constructors a, NonEmptyType "conIdMin" a) => ConId a Source #
The first constructor. This must not be called on an empty type.
conIdMax :: forall a. (Constructors a, NonEmptyType "conIdMax" a) => ConId a Source #
The last constructor. This must not be called on an empty type.
class NonEmptyType_ fname a => NonEmptyType fname a Source #
Constraint that a generic type a
is not empty.
Producing an error message otherwise.
The Symbol
parameter fname
is used only for error messages.
It is implied by the simpler constraint IsEmptyType
a ~ 'False
Instances
NonEmptyType_ fname a => NonEmptyType fname a Source # | |
Defined in Generic.Data.Internal.Meta |
type IsEmptyType a = IsEmptyType_ a Source #
True
if the generic type a
is empty.
Using type families
type family MetaDataName (m :: Meta) :: Symbol where ... Source #
Name of the data type (MetaData
).
MetaDataName ('MetaData n _m _p _nt) = n |
type family MetaDataModule (m :: Meta) :: Symbol where ... Source #
Name of the module where the data type is defined (MetaData
)
MetaDataModule ('MetaData _n m _p _nt) = m |
type family MetaDataPackage (m :: Meta) :: Symbol where ... Source #
Name of the package where the data type is defined (MetaData
)
MetaDataPackage ('MetaData _n _m p _nt) = p |
type family MetaDataNewtype (m :: Meta) :: Bool where ... Source #
True
if the data type is a newtype (MetaData
).
MetaDataNewtype ('MetaData _n _m _p nt) = nt |
type family MetaConsName (m :: Meta) :: Symbol where ... Source #
Name of the constructor (MetaCons
).
MetaConsName ('MetaCons n _f _s) = n |
type family MetaConsFixity (m :: Meta) :: FixityI where ... Source #
Fixity of the constructor (MetaCons
).
MetaConsFixity ('MetaCons _n f s) = f |
type family MetaConsRecord (m :: Meta) :: Bool where ... Source #
True
for a record constructor (MetaCons
).
MetaConsRecord ('MetaCons _n _f s) = s |
type family MetaSelNameM (m :: Meta) :: Maybe Symbol where ... Source #
Just
the name of the record field, if it is one (MetaSel
).
MetaSelNameM ('MetaSel mn _su _ss _ds) = mn |
type family MetaSelName (m :: Meta) :: Symbol where ... Source #
Name of the record field; undefined for non-record fields (MetaSel
).
MetaSelName ('MetaSel ('Just n) _su _ss _ds) = n |
type family MetaSelUnpack (m :: Meta) :: SourceUnpackedness where ... Source #
Unpackedness annotation of a field (MetaSel
).
MetaSelUnpack ('MetaSel _mn su _ss _ds) = su |
type family MetaSelSourceStrictness (m :: Meta) :: SourceStrictness where ... Source #
Strictness annotation of a field (MetaSel
).
MetaSelSourceStrictness ('MetaSel _mn _su ss _ds) = ss |
type family MetaSelStrictness (m :: Meta) :: DecidedStrictness where ... Source #
Inferred strictness of a field (MetaSel
).
MetaSelStrictness ('MetaSel _mn _su _ss ds) = ds |
The Generic
class
Reexported from GHC.Generics.
Representable types of kind *
.
This class is derivable in GHC with the DeriveGeneric
flag on.
A Generic
instance must satisfy the following laws:
from
.to
≡id
to
.from
≡id
Instances
Generic Bool | Since: base-4.6.0.0 |
Generic Ordering | Since: base-4.6.0.0 |
Generic () | Since: base-4.6.0.0 |
Generic Version | Since: base-4.9.0.0 |
Generic ExitCode | |
Generic All | Since: base-4.7.0.0 |
Generic Any | Since: base-4.7.0.0 |
Generic Fixity | Since: base-4.7.0.0 |
Generic Associativity | Since: base-4.7.0.0 |
Defined in GHC.Generics type Rep Associativity :: Type -> Type # from :: Associativity -> Rep Associativity x # to :: Rep Associativity x -> Associativity # | |
Generic SourceUnpackedness | Since: base-4.9.0.0 |
Defined in GHC.Generics type Rep SourceUnpackedness :: Type -> Type # from :: SourceUnpackedness -> Rep SourceUnpackedness x # to :: Rep SourceUnpackedness x -> SourceUnpackedness # | |
Generic SourceStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics type Rep SourceStrictness :: Type -> Type # from :: SourceStrictness -> Rep SourceStrictness x # to :: Rep SourceStrictness x -> SourceStrictness # | |
Generic DecidedStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics type Rep DecidedStrictness :: Type -> Type # from :: DecidedStrictness -> Rep DecidedStrictness x # to :: Rep DecidedStrictness x -> DecidedStrictness # | |
Generic [a] | Since: base-4.6.0.0 |
Generic (Maybe a) | Since: base-4.6.0.0 |
Generic (Par1 p) | Since: base-4.7.0.0 |
Generic (Complex a) | Since: base-4.9.0.0 |
Generic (Min a) | Since: base-4.9.0.0 |
Generic (Max a) | Since: base-4.9.0.0 |
Generic (First a) | Since: base-4.9.0.0 |
Generic (Last a) | Since: base-4.9.0.0 |
Generic (WrappedMonoid m) | Since: base-4.9.0.0 |
Defined in Data.Semigroup type Rep (WrappedMonoid m) :: Type -> Type # from :: WrappedMonoid m -> Rep (WrappedMonoid m) x # to :: Rep (WrappedMonoid m) x -> WrappedMonoid m # | |
Generic (Option a) | Since: base-4.9.0.0 |
Generic (ZipList a) | Since: base-4.7.0.0 |
Generic (Identity a) | Since: base-4.8.0.0 |
Generic (First a) | Since: base-4.7.0.0 |
Generic (Last a) | Since: base-4.7.0.0 |
Generic (Dual a) | Since: base-4.7.0.0 |
Generic (Endo a) | Since: base-4.7.0.0 |
Generic (Sum a) | Since: base-4.7.0.0 |
Generic (Product a) | Since: base-4.7.0.0 |
Generic (Down a) | Since: base-4.12.0.0 |
Generic (NonEmpty a) | Since: base-4.6.0.0 |
Generic a => Generic (GenericProduct a) Source # | |
Defined in Generic.Data.Internal.Generically type Rep (GenericProduct a) :: Type -> Type # from :: GenericProduct a -> Rep (GenericProduct a) x # to :: Rep (GenericProduct a) x -> GenericProduct a # | |
Generic a => Generic (FiniteEnumeration a) Source # | |
Defined in Generic.Data.Internal.Generically type Rep (FiniteEnumeration a) :: Type -> Type # from :: FiniteEnumeration a -> Rep (FiniteEnumeration a) x # to :: Rep (FiniteEnumeration a) x -> FiniteEnumeration a # | |
Generic a => Generic (Generically a) Source # | |
Defined in Generic.Data.Internal.Generically type Rep (Generically a) :: Type -> Type # from :: Generically a -> Rep (Generically a) x # to :: Rep (Generically a) x -> Generically a # | |
Generic (Either a b) | Since: base-4.6.0.0 |
Generic (V1 p) | Since: base-4.9.0.0 |
Generic (U1 p) | Since: base-4.7.0.0 |
Generic (a, b) | Since: base-4.6.0.0 |
Generic (Arg a b) | Since: base-4.9.0.0 |
Generic (WrappedMonad m a) | Since: base-4.7.0.0 |
Defined in Control.Applicative type Rep (WrappedMonad m a) :: Type -> Type # from :: WrappedMonad m a -> Rep (WrappedMonad m a) x # to :: Rep (WrappedMonad m a) x -> WrappedMonad m a # | |
Generic (Proxy t) | Since: base-4.6.0.0 |
(Functor r, Contravariant r) => Generic (Data r p) Source # | |
Generic (f a) => Generic (Generically1 f a) Source # | |
Defined in Generic.Data.Internal.Generically type Rep (Generically1 f a) :: Type -> Type # from :: Generically1 f a -> Rep (Generically1 f a) x # to :: Rep (Generically1 f a) x -> Generically1 f a # | |
(Generic a, Coercible (GSurgery s (Rep a)) (Rep a)) => Generic (Surgery' s a) Source # | |
Generic (Rec1 f p) | Since: base-4.7.0.0 |
Generic (URec (Ptr ()) p) | Since: base-4.9.0.0 |
Generic (URec Char p) | Since: base-4.9.0.0 |
Generic (URec Double p) | Since: base-4.9.0.0 |
Generic (URec Float p) | |
Generic (URec Int p) | Since: base-4.9.0.0 |
Generic (URec Word p) | Since: base-4.9.0.0 |
Generic (a, b, c) | Since: base-4.6.0.0 |
Generic (WrappedArrow a b c) | Since: base-4.7.0.0 |
Defined in Control.Applicative type Rep (WrappedArrow a b c) :: Type -> Type # from :: WrappedArrow a b c -> Rep (WrappedArrow a b c) x # to :: Rep (WrappedArrow a b c) x -> WrappedArrow a b c # | |
Generic (Kleisli m a b) | Since: base-4.14.0.0 |
Generic (Const a b) | Since: base-4.9.0.0 |
Generic (Ap f a) | Since: base-4.12.0.0 |
Generic (Alt f a) | Since: base-4.8.0.0 |
Generic (K1 i c p) | Since: base-4.7.0.0 |
Generic ((f :+: g) p) | Since: base-4.7.0.0 |
Generic ((f :*: g) p) | Since: base-4.7.0.0 |
Generic (a, b, c, d) | Since: base-4.6.0.0 |
Generic (Product f g a) | Since: base-4.9.0.0 |
Generic (Sum f g a) | Since: base-4.9.0.0 |
Generic (M1 i c f p) | Since: base-4.7.0.0 |
Generic ((f :.: g) p) | Since: base-4.7.0.0 |
Generic (a, b, c, d, e) | Since: base-4.6.0.0 |
Generic (Compose f g a) | Since: base-4.9.0.0 |
Generic (a, b, c, d, e, f) | Since: base-4.6.0.0 |
Generic (a, b, c, d, e, f, g) | Since: base-4.6.0.0 |
class Generic1 (f :: k -> Type) #
Representable types of kind * -> *
(or kind k -> *
, when PolyKinds
is enabled).
This class is derivable in GHC with the DeriveGeneric
flag on.
A Generic1
instance must satisfy the following laws:
from1
.to1
≡id
to1
.from1
≡id
Instances
Generic1 (V1 :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (U1 :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (Proxy :: k -> Type) | Since: base-4.6.0.0 |
Generic1 (Alt f :: k -> Type) | Since: base-4.8.0.0 |
Generic1 (Ap f :: k -> Type) | Since: base-4.12.0.0 |
Generic1 (Const a :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (URec (Ptr ()) :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (URec Char :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (URec Double :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (URec Float :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (URec Int :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (URec Word :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (Rec1 f :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (K1 i c :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (f :+: g :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (f :*: g :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (Sum f g :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (Product f g :: k -> Type) | Since: base-4.9.0.0 |
Generic1 (M1 i c f :: k -> Type) | Since: base-4.9.0.0 |
Functor f => Generic1 (f :.: g :: k -> Type) | Since: base-4.9.0.0 |
Functor f => Generic1 (Compose f g :: k -> Type) | Since: base-4.9.0.0 |
Generic1 [] | Since: base-4.6.0.0 |
Generic1 Maybe | Since: base-4.6.0.0 |
Generic1 Par1 | Since: base-4.9.0.0 |
Generic1 Complex | Since: base-4.9.0.0 |
Generic1 Min | Since: base-4.9.0.0 |
Generic1 Max | Since: base-4.9.0.0 |
Generic1 First | Since: base-4.9.0.0 |
Generic1 Last | Since: base-4.9.0.0 |
Generic1 WrappedMonoid | Since: base-4.9.0.0 |
Defined in Data.Semigroup type Rep1 WrappedMonoid :: k -> Type # from1 :: forall (a :: k). WrappedMonoid a -> Rep1 WrappedMonoid a # to1 :: forall (a :: k). Rep1 WrappedMonoid a -> WrappedMonoid a # | |
Generic1 Option | Since: base-4.9.0.0 |
Generic1 ZipList | Since: base-4.7.0.0 |
Generic1 Identity | Since: base-4.8.0.0 |
Generic1 First | Since: base-4.7.0.0 |
Generic1 Last | Since: base-4.7.0.0 |
Generic1 Dual | Since: base-4.7.0.0 |
Generic1 Sum | Since: base-4.7.0.0 |
Generic1 Product | Since: base-4.7.0.0 |
Generic1 Down | Since: base-4.12.0.0 |
Generic1 NonEmpty | Since: base-4.6.0.0 |
Generic1 (Either a :: Type -> Type) | Since: base-4.6.0.0 |
Generic1 ((,) a :: Type -> Type) | Since: base-4.6.0.0 |
Generic1 (Arg a :: Type -> Type) | Since: base-4.9.0.0 |
Generic1 (WrappedMonad m :: Type -> Type) | Since: base-4.7.0.0 |
Defined in Control.Applicative type Rep1 (WrappedMonad m) :: k -> Type # from1 :: forall (a :: k). WrappedMonad m a -> Rep1 (WrappedMonad m) a # to1 :: forall (a :: k). Rep1 (WrappedMonad m) a -> WrappedMonad m a # | |
Generic1 (Data r :: Type -> Type) Source # | |
Generic1 f => Generic1 (Generically1 f :: Type -> Type) Source # | |
Defined in Generic.Data.Internal.Generically type Rep1 (Generically1 f) :: k -> Type # from1 :: forall (a :: k). Generically1 f a -> Rep1 (Generically1 f) a # to1 :: forall (a :: k). Rep1 (Generically1 f) a -> Generically1 f a # | |
Generic1 ((,,) a b :: Type -> Type) | Since: base-4.6.0.0 |
Generic1 (WrappedArrow a b :: Type -> Type) | Since: base-4.7.0.0 |
Defined in Control.Applicative type Rep1 (WrappedArrow a b) :: k -> Type # from1 :: forall (a0 :: k). WrappedArrow a b a0 -> Rep1 (WrappedArrow a b) a0 # to1 :: forall (a0 :: k). Rep1 (WrappedArrow a b) a0 -> WrappedArrow a b a0 # | |
Generic1 (Kleisli m a :: Type -> Type) | Since: base-4.14.0.0 |
Generic1 ((,,,) a b c :: Type -> Type) | Since: base-4.6.0.0 |
Generic1 ((,,,,) a b c d :: Type -> Type) | Since: base-4.6.0.0 |
Generic1 ((,,,,,) a b c d e :: Type -> Type) | Since: base-4.6.0.0 |
Generic1 ((,,,,,,) a b c d e f :: Type -> Type) | Since: base-4.6.0.0 |