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.*) [details]
License BSD-3-Clause
Copyright 2018 Luke Clifton
Author Luke Clifton
Category Data
Source repo head: git clone
Uploaded by lukec at Wed Dec 12 04:09:03 UTC 2018
Distributions NixOS:
Downloads 352 total (65 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2018-12-12 [all 1 reports]


[Index] [Quick Jump]


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