generic-monoid

Derive monoid instances for product types.

Stackage Nightly 2019-12-06:0.1.0.0@rev:1
Latest on Hackage:0.1.0.0@rev:1

See all snapshots generic-monoid appears in

BSD-3-Clause licensed by Luke Clifton
Maintained by lukec@themk.net

Module documentation for 0.1.0.0

This version can be pinned in stack with:generic-monoid-0.1.0.0@sha256: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 ""

Changes

Revision history for generic-monoid

0.1.0.0 – 2018-12-12

  • Initial release.
Depends on 1 package(full list with versions):
comments powered byDisqus