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.*) [details]
License BSD-3-Clause
Copyright 2018 Luke Clifton
Author Luke Clifton
Maintainer lukec@themk.net
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 46 total (20 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]

Modules

[Index] [Quick Jump]

Downloads

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