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] 0.1.0.0
Change log ChangeLog.md
Dependencies base (>=4.12 && <4.14) [details]
License BSD-3-Clause
Copyright 2018 Luke Clifton
Author Luke Clifton
Maintainer lukec@themk.net
Revised Revision 1 made by lukec at Fri Sep 27 02:16:34 UTC 2019
Category Data
Source repo head: git clone https://github.com/luke-clifton/generic-monoid
Uploaded by lukec at Wed Dec 12 04:09:03 UTC 2018
Distributions NixOS:0.1.0.0
Downloads 941 total (115 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]

Modules

[Index] [Quick Jump]

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

For package maintainers and hackage trustees


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