HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulegeneric-data-1.1.0.2Haskell2010

Generic.Data.Internal.Enum

Generic deriving for Enum, Bounded and Ix.

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.

  • 2 types
  • 3 classes
  • 27 values
valuegunsafeIndex :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Int
#

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
instance Ix MyType where
  range = grange
  unsafeIndex = gunsafeIndex
  inRange = ginRange
classclass GEnum opts (f :: Type -> Type) where
#

Generic representation of Enum types.

The opts parameter is a type-level option to select different implementations.

Methods

Instances5GEnum
datadata FiniteEnum
#

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 maxBound :: Int values (because the implementation represents the cardinality explicitly as an Int). This restriction makes Word 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 Generic.Data.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 == map gtoFiniteEnum [0 .. 5]
[0 .. 5] == map gfromFiniteEnum enumeration
Instances2GEnum
classclass GBounded (f :: Type -> Type) where
#

Generic representation of Bounded types.

Methods

Instances5GBounded
classclass GIx (f :: Type -> Type) where
#

Generic representation of Ix types.

Methods

Instances5GIx
  • GIx U1Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Enum
  • Ix c => GIx (K1 i c)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Enum
  • (GEnum StandardEnum f, GEnum StandardEnum g) => GIx (f :+: g)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Enum
  • (GIx f, GIx g) => GIx (f :*: g)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Enum
  • GIx f => GIx (M1 i c f)Defined in generic-data-1.1.0.2 · Generic.Data.Internal.Enum