generic-data-1.1.0.2: Deriving instances with GHC.Generics and related utilities

Safe Haskell | Safe-Inferred
Language | Haskell2010

Generic.Data.Internal.Meta

Contents

- Constructor information on generic representations
- Find a constructor tag by name
- Check that a type is not empty
- Type families

Description

Type metadata accessors

Type names, constructor names...

### Warning

This is an internal module: it is not subject to any versioning policy, breaking changes can happen at any time.

If something here seems useful, please report it or create a pull request to export it from an external module.

Synopsis

- gdatatypeName :: (Generic a, GDatatype (Rep a)) => String
- gmoduleName :: (Generic a, GDatatype (Rep a)) => String
- gpackageName :: (Generic a, GDatatype (Rep a)) => String
- gisNewtype :: (Generic a, GDatatype (Rep a)) => Bool
- fromDatatype :: forall (d :: Meta) r. Datatype d => (M1 D d (Proxy :: Type -> Type) () -> r) -> r
- class GDatatype (f :: k) where
  - gDatatypeName :: String
  - gModuleName :: String
  - gPackageName :: String
  - gIsNewtype :: Bool
- gconName :: Constructors a => a -> String
- gconFixity :: Constructors a => a -> Fixity
- gconIsRecord :: Constructors a => a -> Bool
- gconNum :: Constructors a => Int
- gconIndex :: Constructors a => a -> Int
- newtype ConId (a :: k) = ConId Int
- conId :: Constructors a => a -> ConId a
- conIdToInt :: forall {k} (a :: k). ConId a -> Int
- conIdToString :: Constructors a => ConId a -> String
- conIdEnum :: Constructors a => [ConId a]
- conIdMin :: (Constructors a, NonEmptyType "conIdMin" a) => ConId a
- conIdMax :: (Constructors a, NonEmptyType "conIdMax" a) => ConId a
- conIdNamed :: forall (s :: Symbol) a. ConIdNamed s a => ConId a
- class (Generic a, GConstructors (Rep a)) => Constructors a
- class (Generic a, KnownNat (ConIdNamed' n a)) => ConIdNamed (n :: Symbol) a
- newtype GConId (r :: k) = GConId Int
- gConIdToInt :: forall {k} (r :: k). GConId r -> Int
- toConId :: Generic a => GConId (Rep a) -> ConId a
- fromConId :: Generic a => ConId a -> GConId (Rep a)
- reGConId :: forall {k1} {k2} (r :: k1) (s :: k2). GConId r -> GConId s
- gConIdMin :: forall {k} (r :: k -> Type). GConstructors r => GConId r
- gConIdMax :: forall {k} (r :: k -> Type). GConstructors r => GConId r
- class GConstructors (r :: k -> Type) where
  - gConIdToString :: GConId r -> String
  - gConId :: forall (p :: k). r p -> GConId r
  - gConNum :: Int
  - gConFixity :: forall (p :: k). r p -> Fixity
  - gConIsRecord :: forall (p :: k). r p -> Bool
- type ConIdNamed' (n :: Symbol) t = GConIdNamedIf n t (GConIdNamed n (Rep t))
- type GConIdNamed (n :: Symbol) (f :: k -> Type) = GConIdNamed' n f 0 ('Nothing :: Maybe Nat)
- type family GConIdNamed' (n :: Symbol) (f :: k -> Type) (i :: Nat) (o :: Maybe Nat) :: Maybe Nat where ...
- type family GConIdNamedIf (n :: Symbol) t (o :: Maybe Nat) :: Nat where ...
- class NonEmptyType_ fname a => NonEmptyType (fname :: Symbol) a
- type NonEmptyType_ (fname :: Symbol) a = ErrorIfEmpty fname a (IsEmptyType a) ~ '()
- type family GIsEmptyType (r :: k -> Type) :: Bool where ...
- type IsEmptyType a = IsEmptyType_ a
- type IsEmptyType_ a = GIsEmptyType (Rep a)
- type family ErrorIfEmpty (fname :: Symbol) a (b :: Bool) :: () where ...
- type family MetaOf (f :: Type -> Type) :: 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 ...
- type DummyMeta = 'MetaData "" "" "" 'False
- type family UnM1 (f :: k -> Type) :: k -> Type

# Documentation

    >>> :set -XDataKinds -XTypeApplications
    >>> import Control.Applicative (ZipList)
    >>> import Data.Monoid (Sum(..))

gdatatypeName :: (Generic a, GDatatype (Rep a)) => String Source #

Name of the first data constructor in a type as a string.

    >>> gdatatypeName @(Maybe Int)
    "Maybe"

gmoduleName :: (Generic a, GDatatype (Rep a)) => String Source #

Name of the module where the first type constructor is defined.

    >>> gmoduleName @(ZipList Int)
    "Control.Applicative"

gpackageName :: (Generic a, GDatatype (Rep a)) => String Source #

Name of the package where the first type constructor is defined.

    >>> gpackageName @(Maybe Int)
    "base"

gisNewtype :: (Generic a, GDatatype (Rep a)) => Bool Source #

`True` if the first type constructor is a newtype.

    >>> gisNewtype @[Int]
    False
    >>> gisNewtype @(ZipList Int)
    True

fromDatatype :: forall (d :: Meta) r. Datatype d => (M1 D d (Proxy :: Type -> Type) () -> r) -> r Source #

class GDatatype (f :: k) where Source #

Generic representations that contain datatype metadata.

Methods

gDatatypeName :: String Source #

gModuleName :: String Source #

gPackageName :: String Source #

gIsNewtype :: Bool Source #

#### Instances

Instances details

Datatype d => GDatatype (M1 D d f :: k -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods gDatatypeName :: String Source # gModuleName :: String Source # gPackageName :: String Source # gIsNewtype :: Bool Source #

gconName :: Constructors a => a -> String Source #

Name of the first constructor in a value.

    >>> gconName (Just 0)
    "Just"

gconFixity :: Constructors a => a -> Fixity Source #

The fixity of the first constructor.

    >>> import GHC.Generics ((:*:)(..))
    >>> gconFixity (Just 0)
    Prefix
    >>> gconFixity ([] :*: id)
    Infix RightAssociative 6

gconIsRecord :: 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 :: Constructors a => Int Source #

Number of constructors.

    >>> gconNum @(Maybe Int)
    2

gconIndex :: Constructors a => a -> Int Source #

Index of a constructor.

    >>> gconIndex Nothing
    0
    >>> gconIndex (Just "test")
    1

newtype ConId (a :: k) Source #

An opaque identifier for a constructor.

Constructors

ConId Int |

#### Instances

Instances details

Show (ConId a) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods showsPrec :: Int -> ConId a -> ShowS Source # show :: ConId a -> String Source # showList :: [ConId a] -> ShowS Source #
Eq (ConId a) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods (==) :: ConId a -> ConId a -> Bool Source # (/=) :: ConId a -> ConId a -> Bool Source #
Ord (ConId a) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods compare :: ConId a -> ConId a -> Ordering Source # (<) :: ConId a -> ConId a -> Bool Source # (<=) :: ConId a -> ConId a -> Bool Source # (>) :: ConId a -> ConId a -> Bool Source # (>=) :: ConId a -> ConId a -> Bool Source # max :: ConId a -> ConId a -> ConId a Source # min :: ConId a -> ConId a -> ConId a Source #

conId :: Constructors a => a -> ConId a Source #

Identifier of a constructor.

conIdToInt :: forall {k} (a :: k). ConId a -> Int Source #

Index of a constructor, given its identifier. See also `gconIndex`.

conIdToString :: Constructors a => ConId a -> String Source #

Name of a constructor. See also `gconName`.

conIdEnum :: Constructors a => [ConId a] Source #

All constructor identifiers.

    gconNum @a = length (conIdEnum @a)

conIdMin :: (Constructors a, NonEmptyType "conIdMin" a) => ConId a Source #

The first constructor. This must not be called on an empty type.

conIdMax :: (Constructors a, NonEmptyType "conIdMax" a) => ConId a Source #

The last constructor. This must not be called on an empty type.

conIdNamed :: forall (s :: Symbol) 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, GConstructors (Rep a)) => Constructors a Source #

Constraint synonym for `Generic` and `GConstructors`.

#### Instances

Instances details

(Generic a, GConstructors (Rep a)) => Constructors a Source # |
Instance details Defined in Generic.Data.Internal.Meta

class (Generic a, KnownNat (ConIdNamed' n a)) => ConIdNamed (n :: Symbol) a Source #

Constraint synonym for generic types `a` with a constructor named `n`.

#### Instances

Instances details

(Generic a, KnownNat (ConIdNamed' n a)) => ConIdNamed n a Source # |
Instance details Defined in Generic.Data.Internal.Meta

### Constructor information on generic representations

newtype GConId (r :: k) Source #

Constructors

GConId Int |

#### Instances

Instances details

Eq (GConId r) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods (==) :: GConId r -> GConId r -> Bool Source # (/=) :: GConId r -> GConId r -> Bool Source #
Ord (GConId r) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods compare :: GConId r -> GConId r -> Ordering Source # (<) :: GConId r -> GConId r -> Bool Source # (<=) :: GConId r -> GConId r -> Bool Source # (>) :: GConId r -> GConId r -> Bool Source # (>=) :: GConId r -> GConId r -> Bool Source # max :: GConId r -> GConId r -> GConId r Source # min :: GConId r -> GConId r -> GConId r Source #

gConIdToInt :: forall {k} (r :: k). GConId r -> Int Source #

toConId :: Generic a => GConId (Rep a) -> ConId a Source #

fromConId :: Generic a => ConId a -> GConId (Rep a) Source #

reGConId :: forall {k1} {k2} (r :: k1) (s :: k2). GConId r -> GConId s Source #

gConIdMin :: forall {k} (r :: k -> Type). GConstructors r => GConId r Source #

gConIdMax :: forall {k} (r :: k -> Type). GConstructors r => GConId r Source #

class GConstructors (r :: k -> Type) where Source #

Generic representations that contain constructor metadata.

Methods

gConIdToString :: GConId r -> String Source #

gConId :: forall (p :: k). r p -> GConId r Source #

gConNum :: Int Source #

gConFixity :: forall (p :: k). r p -> Fixity Source #

gConIsRecord :: forall (p :: k). r p -> Bool Source #

#### Instances

Instances details

GConstructors (V1 :: k -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods gConIdToString :: GConId (V1 :: k -> Type) -> String Source # gConId :: forall (p :: k). V1 p -> GConId (V1 :: k -> Type) Source # gConNum :: Int Source # gConFixity :: forall (p :: k). V1 p -> Fixity Source # gConIsRecord :: forall (p :: k). V1 p -> Bool Source #
(GConstructors f, GConstructors g) => GConstructors (f :+: g :: k -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods gConIdToString :: GConId (f :+: g) -> String Source # gConId :: forall (p :: k). (f :+: g) p -> GConId (f :+: g) Source # gConNum :: Int Source # gConFixity :: forall (p :: k). (f :+: g) p -> Fixity Source # gConIsRecord :: forall (p :: k). (f :+: g) p -> Bool Source #
Constructor c => GConstructors (M1 C c f :: k -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods gConIdToString :: GConId (M1 C c f) -> String Source # gConId :: forall (p :: k). M1 C c f p -> GConId (M1 C c f) Source # gConNum :: Int Source # gConFixity :: forall (p :: k). M1 C c f p -> Fixity Source # gConIsRecord :: forall (p :: k). M1 C c f p -> Bool Source #
GConstructors f => GConstructors (M1 D c f :: k -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Meta Methods gConIdToString :: GConId (M1 D c f) -> String Source # gConId :: forall (p :: k). M1 D c f p -> GConId (M1 D c f) Source # gConNum :: Int Source # gConFixity :: forall (p :: k). M1 D c f p -> Fixity Source # gConIsRecord :: forall (p :: k). M1 D c f p -> Bool Source #

### Find a constructor tag by name

type ConIdNamed' (n :: Symbol) t = GConIdNamedIf n t (GConIdNamed n (Rep t)) Source #

type GConIdNamed (n :: Symbol) (f :: k -> Type) = GConIdNamed' n f 0 ('Nothing :: Maybe Nat) Source #

type family GConIdNamed' (n :: Symbol) (f :: k -> Type) (i :: Nat) (o :: Maybe Nat) :: Maybe Nat where ... Source #

Equations

GConIdNamed' n (M1 D _c f :: k -> Type) i r = GConIdNamed' n f i r |
GConIdNamed' n (f :+: g :: k -> Type) i r = GConIdNamed' n f i (GConIdNamed' n g (i + NConstructors f) r) |
GConIdNamed' n (M1 C ('MetaCons n _f _s) _g :: k -> Type) i _r = 'Just i |
GConIdNamed' n (M1 C ('MetaCons _n _f _s) _g :: k -> Type) _i r = r |
GConIdNamed' _n (V1 :: k -> Type) _i r = r |

type family GConIdNamedIf (n :: Symbol) t (o :: Maybe Nat) :: Nat where ... Source #

Equations

GConIdNamedIf _n _t ('Just i) = i |
GConIdNamedIf n t ('Nothing :: Maybe Nat) = TypeError ((('Text "No constructor named " ':<>: 'ShowType n) ':<>: 'Text " in generic type ") ':<>: 'ShowType t) :: Nat |

### Check that a type is not empty

class NonEmptyType_ fname a => NonEmptyType (fname :: Symbol) 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

Instances details

NonEmptyType_ fname a => NonEmptyType fname a Source # |
Instance details Defined in Generic.Data.Internal.Meta

type NonEmptyType_ (fname :: Symbol) a = ErrorIfEmpty fname a (IsEmptyType a) ~ '() Source #

Internal definition of `NonEmptyType`. It is implied by the simpler constraint ``IsEmptyType` a ~ 'False`.

    >>> :set -XTypeFamilies
    >>> :{
    conIdMin' :: (Constructors a, IsEmptyType a ~ 'False) => ConId a
    conIdMin' = conIdMin
    :}

    >>> :{
    conIdMax' :: (Constructors a, IsEmptyType a ~ 'False) => ConId a
    conIdMax' = conIdMax
    :}

type family GIsEmptyType (r :: k -> Type) :: Bool where ... Source #

Equations

GIsEmptyType (M1 D _d (V1 :: k -> Type) :: k -> Type) = 'True |
GIsEmptyType (M1 D _d (M1 C _c _f) :: k -> Type) = 'False |
GIsEmptyType (M1 D _d (_f :+: _g) :: k -> Type) = 'False |

type IsEmptyType a = IsEmptyType_ a Source #

`True` if the generic type `a` is empty.

type IsEmptyType_ a = GIsEmptyType (Rep a) Source #

Internal definition of `IsEmptyType`.

type family ErrorIfEmpty (fname :: Symbol) a (b :: Bool) :: () where ... Source #

Throw an error if the boolean `b` is true, meaning that the type `a` is empty.

Example:

    ghci> data E deriving Generic
    ghci> conIdMin :: ConId E

Error message:

    The function 'conIdMin' cannot be used with the empty type E

Equations

ErrorIfEmpty fname a 'True = TypeError ((('Text "The function '" ':<>: 'Text fname) ':<>: 'Text "' cannot be used with the empty type ") ':<>: 'ShowType a) :: () |
ErrorIfEmpty fname a 'False = '() |

# Type families

type family MetaOf (f :: Type -> Type) :: Meta where ... Source #

`Meta` field of the `M1` type constructor.

Equations

MetaOf (M1 i d f) = d |

type family MetaDataName (m :: Meta) :: Symbol where ... Source #

Name of the data type (`MetaData`).

Equations

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`)

Equations

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`)

Equations

MetaDataPackage ('MetaData _n _m p _nt) = p |

type family MetaDataNewtype (m :: Meta) :: Bool where ... Source #

`True` if the data type is a newtype (`MetaData`).

Equations

MetaDataNewtype ('MetaData _n _m _p nt) = nt |

type family MetaConsName (m :: Meta) :: Symbol where ... Source #

Name of the constructor (`MetaCons`).

Equations

MetaConsName ('MetaCons n _f _s) = n |

type family MetaConsFixity (m :: Meta) :: FixityI where ... Source #

Fixity of the constructor (`MetaCons`).

Equations

MetaConsFixity ('MetaCons _n f s) = f |

type family MetaConsRecord (m :: Meta) :: Bool where ... Source #

`True` for a record constructor (`MetaCons`).

Equations

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`).

Equations

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`).

Equations

MetaSelName ('MetaSel ('Just n) _su _ss _ds) = n |

type family MetaSelUnpack (m :: Meta) :: SourceUnpackedness where ... Source #

Unpackedness annotation of a field (`MetaSel`).

Equations

MetaSelUnpack ('MetaSel _mn su _ss _ds) = su |

type family MetaSelSourceStrictness (m :: Meta) :: SourceStrictness where ... Source #

Strictness annotation of a field (`MetaSel`).

Equations

MetaSelSourceStrictness ('MetaSel _mn _su ss _ds) = ss |

type family MetaSelStrictness (m :: Meta) :: DecidedStrictness where ... Source #

Inferred strictness of a field (`MetaSel`).

Equations

MetaSelStrictness ('MetaSel _mn _su _ss ds) = ds |

type DummyMeta = 'MetaData "" "" "" 'False Source #

A placeholder for `Meta` values.

type family UnM1 (f :: k -> Type) :: k -> Type Source #

Remove an `M1` type constructor.

#### Instances

Instances details

type UnM1 (M1 i c f :: k -> Type) Source # |
Instance details Defined in Generic.Data.Internal.Meta type UnM1 (M1 i c f :: k -> Type) = f