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]

Downloads

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

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1
Change log ChangeLog.md
Dependencies base (>=4.12 && <4.15) [details]
License BSD-3-Clause
Copyright 2018 Luke Clifton
Author Luke Clifton
Maintainer lukec@themk.net
Revised Revision 2 made by lukec at 2020-05-17T03:58:13Z
Category Data
Source repo head: git clone https://github.com/luke-clifton/generic-monoid
Uploaded by lukec at 2018-12-12T04:09:03Z
Distributions LTSHaskell:0.1.0.1, NixOS:0.1.0.1, Stackage:0.1.0.1
Reverse Dependencies 5 direct, 22 indirect [details]
Downloads 14803 total (89 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-12-12 [all 1 reports]

Readme for generic-monoid-0.1.0.0

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