generic-monoid: Derive monoid instances for product types.

[ bsd3, data, library ] [ Propose Tags ]

Using GHC's generics, allow for deriving Monoid and Semigroup instances for your product types.

[Skip to Readme]
Versions [faq],
Change log
Dependencies base (>=4.12 && <4.15) [details]
License BSD-3-Clause
Copyright 2018 Luke Clifton
Author Luke Clifton
Revised Revision 2 made by lukec at 2020-05-17T03:58:13Z
Category Data
Source repo head: git clone
Uploaded by lukec at 2018-12-12T04:09:03Z
Distributions NixOS:
Downloads 5579 total (486 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2018-12-12 [all 1 reports]


[Index] [Quick Jump]


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees

Readme for generic-monoid-

[back to package description]

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 ""