generic-monoid

Derive monoid instances for product types.

Version on this page:0.1.0.0@rev:1
LTS Haskell 22.14:0.1.0.1@rev:3
Stackage Nightly 2024-03-28:0.1.0.1@rev:3
Latest on Hackage:0.1.0.1@rev:3

See all snapshots generic-monoid appears in

BSD-3-Clause licensed by Luke Clifton
Maintained by [email protected]
This version can be pinned in stack with:generic-monoid-0.1.0.0@sha256:372875d12742b97befaacbd08a35c2e73cc3821a3b7a09ec1b17a079d0b24ff7,856

Module documentation for 0.1.0.0

Depends on 1 package(full list with versions):
Used by 1 package in nightly-2019-12-29(full list with versions):

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

Changes

Revision history for generic-monoid

0.1.0.0 – 2018-12-12

  • Initial release.