Data.Semigroup.Generic

semigroups-0.20: Anything that associates

Copyright | (C) 2014-2015 Edward Kmett Eric Mertens
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Data.Semigroup.Generic

Contents

- Generic method implementations
- Adapter newtype
- Internal classes

Description

This module provides generic deriving tools for monoids and semigroups for product-like structures.

Synopsis

- gmappend :: (Generic a, GSemigroup (Rep a)) => a -> a -> a
- gmempty :: (Generic a, GMonoid (Rep a)) => a
- newtype GenericSemigroupMonoid a = GenericSemigroupMonoid {
  - getGenericSemigroupMonoid :: a
  }
- class GSemigroup f
- class GSemigroup f => GMonoid f

# Generic method implementations

gmappend :: (Generic a, GSemigroup (Rep a)) => a -> a -> a #

Generically generate a `Semigroup` (`<>`) operation for any type implementing `Generic`. This operation will append two values by point-wise appending their component fields. It is only defined for product types.

    gmappend a (gmappend b c) = gmappend (gmappend a b) c

gmempty :: (Generic a, GMonoid (Rep a)) => a #

Generically generate a `Monoid` `mempty` for any product-like type implementing `Generic`.

It is only defined for product types.

    gmappend gmempty a = a = gmappend a gmempty

# Adapter newtype

newtype GenericSemigroupMonoid a #

An adapter newtype, suitable for `DerivingVia`. Its `Semigroup` and `Monoid` instances leverage the `Generic`-based defaults defined by `gmappend` and `gmempty`. Here is an example of how to use it:

    {-# LANGUAGE DerivingVia #-}
    import Data.Semigroup.Generic

    data Pair a = MkPair a a
      deriving (Semigroup, Monoid) via (GenericSemigroupMonoid (Pair a))

Constructors

GenericSemigroupMonoid |
Fields getGenericSemigroupMonoid :: a

#### Instances

Instances details

(Generic a, GMonoid (Rep a)) => Monoid (GenericSemigroupMonoid a) # |
Instance details Defined in Data.Semigroup.Generic Methods mempty :: GenericSemigroupMonoid a # mappend :: GenericSemigroupMonoid a -> GenericSemigroupMonoid a -> GenericSemigroupMonoid a # mconcat :: [GenericSemigroupMonoid a] -> GenericSemigroupMonoid a #
(Generic a, GSemigroup (Rep a)) => Semigroup (GenericSemigroupMonoid a) # |
Instance details Defined in Data.Semigroup.Generic Methods (<>) :: GenericSemigroupMonoid a -> GenericSemigroupMonoid a -> GenericSemigroupMonoid a # sconcat :: NonEmpty (GenericSemigroupMonoid a) -> GenericSemigroupMonoid a # stimes :: Integral b => b -> GenericSemigroupMonoid a -> GenericSemigroupMonoid a #

# Internal classes

class GSemigroup f #

Minimal complete definition

gmappend'

#### Instances

Instances details

GSemigroup (U1 :: Type -> Type) # |
Instance details Defined in Data.Semigroup.Generic Methods gmappend' :: U1 p -> U1 p -> U1 p
GSemigroup (V1 :: Type -> Type) # |
Instance details Defined in Data.Semigroup.Generic Methods gmappend' :: V1 p -> V1 p -> V1 p
(GSemigroup f, GSemigroup g) => GSemigroup (f :*: g) # |
Instance details Defined in Data.Semigroup.Generic Methods gmappend' :: (f :*: g) p -> (f :*: g) p -> (f :*: g) p
Semigroup a => GSemigroup (K1 i a :: Type -> Type) # |
Instance details Defined in Data.Semigroup.Generic Methods gmappend' :: K1 i a p -> K1 i a p -> K1 i a p
GSemigroup f => GSemigroup (M1 i c f) # |
Instance details Defined in Data.Semigroup.Generic Methods gmappend' :: M1 i c f p -> M1 i c f p -> M1 i c f p

class GSemigroup f => GMonoid f #

Minimal complete definition

gmempty'

#### Instances

Instances details

GMonoid (U1 :: Type -> Type) # |
Instance details Defined in Data.Semigroup.Generic Methods gmempty' :: U1 p
(GMonoid f, GMonoid g) => GMonoid (f :*: g) # |
Instance details Defined in Data.Semigroup.Generic Methods gmempty' :: (f :*: g) p
(Semigroup a, Monoid a) => GMonoid (K1 i a :: Type -> Type) # |
Instance details Defined in Data.Semigroup.Generic Methods gmempty' :: K1 i a p
GMonoid f => GMonoid (M1 i c f) # |
Instance details Defined in Data.Semigroup.Generic Methods gmempty' :: M1 i c f p