GHC.Generics.Generically

generically-0.1.1: Generically newtype to use with DerivingVia

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.Generics.Generically

Description

This module exports `Generically` and `Generically` newtypes meant to be used with GHC.Generics and `DerivingVia`.

These types are re-exported from GHC.Generics on `base-4.17` and later, and defined here for older `base` versions.

Synopsis

- newtype Generically a = Generically a
- newtype Generically1 (f :: k -> Type) (a :: k) where
  - Generically1 :: forall {k} (f :: k -> Type) (a :: k). f a -> Generically1 f a

# Documentation

newtype Generically a #

A datatype whose instances are defined generically, using the `Generic` representation. `Generically1` is a higher-kinded version of `Generically` that uses `Generic1`.

Generic instances can be derived via ``Generically` A` using `-XDerivingVia`.

    {-# LANGUAGE DeriveGeneric      #-}
    {-# LANGUAGE DerivingStrategies #-}
    {-# LANGUAGE DerivingVia        #-}

    import GHC.Generics (Generic)

    data V4 a = V4 a a a a
      deriving stock Generic

      deriving (Semigroup, Monoid)
      via Generically (V4 a)

This corresponds to `Semigroup` and `Monoid` instances defined by pointwise lifting:

    instance Semigroup a => Semigroup (V4 a) where
      (<>) :: V4 a -> V4 a -> V4 a
      V4 a1 b1 c1 d1 <> V4 a2 b2 c2 d2 =
        V4 (a1 <> a2) (b1 <> b2) (c1 <> c2) (d1 <> d2)

    instance Monoid a => Monoid (V4 a) where
      mempty :: V4 a
      mempty = V4 mempty mempty mempty mempty

Historically this required modifying the type class to include generic method definitions (`-XDefaultSignatures`) and deriving it with the `anyclass` strategy (`-XDeriveAnyClass`). Having a /via type/ like `Generically` decouples the instance from the type class.

@since base-4.17.0.0

Constructors

Generically a |

#### Instances

Instances details

(Generic a, Monoid (Rep a ())) => Monoid (Generically a) | @since base-4.17.0.0
Instance details Defined in GHC.Internal.Generics Methods mempty :: Generically a # mappend :: Generically a -> Generically a -> Generically a # mconcat :: [Generically a] -> Generically a #
(Generic a, Semigroup (Rep a ())) => Semigroup (Generically a) | @since base-4.17.0.0
Instance details Defined in GHC.Internal.Generics Methods (<>) :: Generically a -> Generically a -> Generically a # sconcat :: NonEmpty (Generically a) -> Generically a # stimes :: Integral b => b -> Generically a -> Generically a #

newtype Generically1 (f :: k -> Type) (a :: k) where #

A type whose instances are defined generically, using the `Generic1` representation. `Generically1` is a higher-kinded version of `Generically` that uses `Generic`.

Generic instances can be derived for type constructors via ``Generically1` F` using `-XDerivingVia`.

    {-# LANGUAGE DeriveGeneric      #-}
    {-# LANGUAGE DerivingStrategies #-}
    {-# LANGUAGE DerivingVia        #-}

    import GHC.Generics (Generic)

    data V4 a = V4 a a a a
      deriving stock (Functor, Generic1)

      deriving Applicative
      via Generically1 V4

This corresponds to `Applicative` instances defined by pointwise lifting:

    instance Applicative V4 where
      pure :: a -> V4 a
      pure a = V4 a a a a

      liftA2 :: (a -> b -> c) -> (V4 a -> V4 b -> V4 c)
      liftA2 (·) (V4 a1 b1 c1 d1) (V4 a2 b2 c2 d2) =
        V4 (a1 · a2) (b1 · b2) (c1 · c2) (d1 · d2)

Historically this required modifying the type class to include generic method definitions (`-XDefaultSignatures`) and deriving it with the `anyclass` strategy (`-XDeriveAnyClass`). Having a /via type/ like `Generically1` decouples the instance from the type class.

@since base-4.17.0.0

Constructors

Generically1 :: forall {k} (f :: k -> Type) (a :: k). f a -> Generically1 f a |

#### Instances

Instances details

(Generic1 f, Eq1 (Rep1 f)) => Eq1 (Generically1 f) | Since: base-4.17.0.0
Instance details Defined in Data.Functor.Classes Methods liftEq :: (a -> b -> Bool) -> Generically1 f a -> Generically1 f b -> Bool #
(Generic1 f, Ord1 (Rep1 f)) => Ord1 (Generically1 f) | Since: base-4.17.0.0
Instance details Defined in Data.Functor.Classes Methods liftCompare :: (a -> b -> Ordering) -> Generically1 f a -> Generically1 f b -> Ordering #
(Generic1 f, Alternative (Rep1 f)) => Alternative (Generically1 f) | @since base-4.17.0.0
Instance details Defined in GHC.Internal.Generics Methods empty :: Generically1 f a # (<|>) :: Generically1 f a -> Generically1 f a -> Generically1 f a # some :: Generically1 f a -> Generically1 f [a] # many :: Generically1 f a -> Generically1 f [a] #
(Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f) | @since base-4.17.0.0
Instance details Defined in GHC.Internal.Generics Methods pure :: a -> Generically1 f a # (<*>) :: Generically1 f (a -> b) -> Generically1 f a -> Generically1 f b # liftA2 :: (a -> b -> c) -> Generically1 f a -> Generically1 f b -> Generically1 f c # (*>) :: Generically1 f a -> Generically1 f b -> Generically1 f b # (<*) :: Generically1 f a -> Generically1 f b -> Generically1 f a #
(Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f) | @since base-4.17.0.0
Instance details Defined in GHC.Internal.Generics Methods fmap :: (a -> b) -> Generically1 f a -> Generically1 f b # (<$) :: a -> Generically1 f b -> Generically1 f a #
(Generic1 f, Eq (Rep1 f a)) => Eq (Generically1 f a) | @since base-4.18.0.0
Instance details Defined in GHC.Internal.Generics Methods (==) :: Generically1 f a -> Generically1 f a -> Bool # (/=) :: Generically1 f a -> Generically1 f a -> Bool #
(Generic1 f, Ord (Rep1 f a)) => Ord (Generically1 f a) | @since base-4.18.0.0
Instance details Defined in GHC.Internal.Generics Methods compare :: Generically1 f a -> Generically1 f a -> Ordering # (<) :: Generically1 f a -> Generically1 f a -> Bool # (<=) :: Generically1 f a -> Generically1 f a -> Bool # (>) :: Generically1 f a -> Generically1 f a -> Bool # (>=) :: Generically1 f a -> Generically1 f a -> Bool # max :: Generically1 f a -> Generically1 f a -> Generically1 f a # min :: Generically1 f a -> Generically1 f a -> Generically1 f a #