BSD-3-Clause licensed by Luke Clifton
Maintained by [email protected]

Module documentation for

This version can be pinned in stack with:[email protected]:372875d12742b97befaacbd08a35c2e73cc3821a3b7a09ec1b17a079d0b24ff7,856

Generic Monoid (and Semigroup)

This library provides a method of deriving Semigroup and Monoid instances for your large product types. It does this using GHC generics, and can provides a mechanism for using the DerivingVia extension to reduce boilerplate.

It only works if each field of your product type is itself a Semigroup/Monoid.

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

import GHC.Generics
import Data.Monoid.Generic

data BigProduct = BigProduct
    { theList   :: [Int]
    , theSum    :: Sum Double
    , theString :: String
    } deriving (Generic, Eq)
    deriving Semigroup via GenericSemigroup BigProduct
    deriving Monoid    via GenericMonoid BigProduct

useIt :: Bool
useIt = (mempty <> mempty) == BigProduct [] 0 ""


Revision history for generic-monoid – 2018-12-12

  • Initial release.
Depends on 1 package(full list with versions):
Used by 2 packages in nightly-2020-03-25(full list with versions):