Data.Functor.Classes.Generic

transformers-compat-0.7.2: A small compatibility shim for the transformers library

Copyright | (C) 2015-2016 Edward Kmett Ryan Scott
License | BSD-style (see the file LICENSE)
Maintainer | Ryan Scott
Stability | Provisional
Portability | GHC
Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Functor.Classes.Generic

Contents

- Options
- `Eq1`
- `Ord1`
- `Read1`
- `Show1`
- `GenericFunctorClasses`
- Example

Description

Functions to generically derive `Eq1`, `Ord1`, `Read1`, and `Show1` instances from Data.Functor.Classes.

Synopsis

- newtype Options = Options {
  - ghc8ShowBehavior :: Bool
  }
- defaultOptions :: Options
- latestGHCOptions :: Options
- liftEqDefault :: (GEq1 NonV4 (Rep1 f), Generic1 f) => (a -> b -> Bool) -> f a -> f b -> Bool
- liftEqOptions :: (GEq1 NonV4 (Rep1 f), Generic1 f) => Options -> (a -> b -> Bool) -> f a -> f b -> Bool
- liftCompareDefault :: (GOrd1 NonV4 (Rep1 f), Generic1 f) => (a -> b -> Ordering) -> f a -> f b -> Ordering
- liftCompareOptions :: (GOrd1 NonV4 (Rep1 f), Generic1 f) => Options -> (a -> b -> Ordering) -> f a -> f b -> Ordering
- liftReadsPrecDefault :: (GRead1 NonV4 (Rep1 f), Generic1 f) => (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
- liftReadsPrecOptions :: (GRead1 NonV4 (Rep1 f), Generic1 f) => Options -> (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
- liftShowsPrecDefault :: (GShow1 NonV4 (Rep1 f), Generic1 f) => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
- liftShowsPrecOptions :: (GShow1 NonV4 (Rep1 f), Generic1 f) => Options -> (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
- newtype FunctorClassesDefault f a = FunctorClassesDefault {
  - getFunctorClassesDefault :: f a
  }

# Options

newtype Options #

Options that further configure how the functions in Data.Functor.Classes.Generic should behave.

Constructors

Options |
Fields ghc8ShowBehavior :: Bool If `True`, a default `Show1` implementation will show hash signs (`#`) when showing unlifted types.

defaultOptions :: Options #

Options that match the behavior of the installed version of GHC.

latestGHCOptions :: Options #

Options that match the behavior of the most recent GHC release.

# `Eq1`

liftEqDefault :: (GEq1 NonV4 (Rep1 f), Generic1 f) => (a -> b -> Bool) -> f a -> f b -> Bool #

A sensible default `liftEq` implementation for `Generic1` instances.

liftEqOptions :: (GEq1 NonV4 (Rep1 f), Generic1 f) => Options -> (a -> b -> Bool) -> f a -> f b -> Bool #

Like `liftEqDefault`, but with configurable `Options`. Currently, the `Options` have no effect (but this may change in the future).

# `Ord1`

liftCompareDefault :: (GOrd1 NonV4 (Rep1 f), Generic1 f) => (a -> b -> Ordering) -> f a -> f b -> Ordering #

A sensible default `liftCompare` implementation for `Generic1` instances.

liftCompareOptions :: (GOrd1 NonV4 (Rep1 f), Generic1 f) => Options -> (a -> b -> Ordering) -> f a -> f b -> Ordering #

Like `liftCompareDefault`, but with configurable `Options`. Currently, the `Options` have no effect (but this may change in the future).

# `Read1`

liftReadsPrecDefault :: (GRead1 NonV4 (Rep1 f), Generic1 f) => (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a) #

A sensible default `liftReadsPrec` implementation for `Generic1` instances.

liftReadsPrecOptions :: (GRead1 NonV4 (Rep1 f), Generic1 f) => Options -> (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a) #

Like `liftReadsPrecDefault`, but with configurable `Options`. Currently, the `Options` have no effect (but this may change in the future).

# `Show1`

liftShowsPrecDefault :: (GShow1 NonV4 (Rep1 f), Generic1 f) => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS #

A sensible default `liftShowsPrec` implementation for `Generic1` instances.

liftShowsPrecOptions :: (GShow1 NonV4 (Rep1 f), Generic1 f) => Options -> (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS #

Like `liftShowsPrecDefault`, but with configurable `Options`.

# `GenericFunctorClasses`

newtype FunctorClassesDefault f a #

An adapter newtype, suitable for `DerivingVia`. Its `Eq1`, `Ord1`, `Read1`, and `Show1` instances leverage `Generic1`-based defaults.

Constructors

FunctorClassesDefault |
Fields getFunctorClassesDefault :: f a

#### Instances

Instances details

(GEq1 NonV4 (Rep1 f), Generic1 f) => Eq1 (FunctorClassesDefault f) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods liftEq :: (a -> b -> Bool) -> FunctorClassesDefault f a -> FunctorClassesDefault f b -> Bool #
(GOrd1 NonV4 (Rep1 f), Generic1 f) => Ord1 (FunctorClassesDefault f) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods liftCompare :: (a -> b -> Ordering) -> FunctorClassesDefault f a -> FunctorClassesDefault f b -> Ordering #
(GRead1 NonV4 (Rep1 f), Generic1 f) => Read1 (FunctorClassesDefault f) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (FunctorClassesDefault f a) # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [FunctorClassesDefault f a] # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (FunctorClassesDefault f a) # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [FunctorClassesDefault f a] #
(GShow1 NonV4 (Rep1 f), Generic1 f) => Show1 (FunctorClassesDefault f) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> FunctorClassesDefault f a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [FunctorClassesDefault f a] -> ShowS #
(GRead (Rep1 f a), Generic1 f) => Read (FunctorClassesDefault f a) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods readsPrec :: Int -> ReadS (FunctorClassesDefault f a) # readList :: ReadS [FunctorClassesDefault f a] # readPrec :: ReadPrec (FunctorClassesDefault f a) # readListPrec :: ReadPrec [FunctorClassesDefault f a] #
(GShow (Rep1 f a), Generic1 f) => Show (FunctorClassesDefault f a) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods showsPrec :: Int -> FunctorClassesDefault f a -> ShowS # show :: FunctorClassesDefault f a -> String # showList :: [FunctorClassesDefault f a] -> ShowS #
(GEq (Rep1 f a), Generic1 f) => Eq (FunctorClassesDefault f a) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods (==) :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Bool # (/=) :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Bool #
(GOrd (Rep1 f a), Generic1 f) => Ord (FunctorClassesDefault f a) # |
Instance details Defined in Data.Functor.Classes.Generic.Internal Methods compare :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Ordering # (<) :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Bool # (<=) :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Bool # (>) :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Bool # (>=) :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> Bool # max :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> FunctorClassesDefault f a # min :: FunctorClassesDefault f a -> FunctorClassesDefault f a -> FunctorClassesDefault f a #

# Example

The most straightforward way to use the defaults in this module is to use `DerivingVia` on GHC 8.6 or later. For example:

    {-# LANGUAGE DeriveGeneric, DerivingVia #-}

    import Data.Functor.Classes
    import Data.Functor.Classes.Generic
    import GHC.Generics

    data Pair a = Pair a a
      deriving stock Generic1
      deriving (Eq1, Ord1, Read1, Show1)
               via FunctorClassesDefault Pair

If using an older version of GHC, then one can also define instances manually. This is slightly trickier to accomplish since this module exports different functions depending on which version of `transformers` this library is built against. Here is an example of how to define instances manually:

    {-# LANGUAGE CPP, DeriveGeneric #-}

    import Data.Functor.Classes
    import Data.Functor.Classes.Generic
    import GHC.Generics

    data Pair a = Pair a a deriving Generic1

    instance Eq1 Pair where
    #if MIN_VERSION_transformers(0,4,0) && !(MIN_VERSION_transformers(0,5,0))
        eq1 = eq1Default
    #else
        liftEq = liftEqDefault
    #endif

    instance Ord1 Pair where
    #if MIN_VERSION_transformers(0,4,0) && !(MIN_VERSION_transformers(0,5,0))
        compare1 = compare1Default
    #else
        liftCompare = liftCompareDefault
    #endif

    instance Read1 Pair where
    #if MIN_VERSION_transformers(0,4,0) && !(MIN_VERSION_transformers(0,5,0))
        readsPrec1 = readsPrec1Default
    #else
        liftReadsPrec = liftReadsPrecDefault
    #endif

    instance Show1 Pair where
    #if MIN_VERSION_transformers(0,4,0) && !(MIN_VERSION_transformers(0,5,0))
        showsPrec1 = showsPrec1Default
    #else
        liftShowsPrec = liftShowsPrecDefault
    #endif