on develop help sign in
patch/src/Data/Semigroup/Additive.hs
54 lines 1.7 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE TypeOperators #-}
3{-# LANGUAGE TypeFamilies #-}
4-- |
5-- Module:
6-- Data.Semigroup.Additive
7-- Description:
8-- This module defines a class for commutative semigroups, until it is moved
9-- to another library.
10module Data.Semigroup.Additive
11 ( Additive
12 ) where
14import Data.Functor.Const (Const (..))
15import Data.Functor.Identity
16import Data.Proxy
17#if !MIN_VERSION_base(4,12,0)
18-- for :*: and :.: semigroup instances
19import Data.Orphans ()
20#endif
21#if !MIN_VERSION_base(4,11,0)
22import Data.Semigroup (Semigroup (..))
23#endif
24import GHC.Generics
26-- | An 'Additive' 'Semigroup' is one where (<>) is commutative
27class Semigroup q => Additive q where
29-- | Trivial additive semigroup.
30instance Additive ()
32-- | Product additive semigroup.
33-- A Pair of additive semigroups gives rise to a additive semigroup
34instance (Additive a, Additive b) => Additive (a, b)
36-- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided.
37-- Base does not define Monoid (Compose f g a) so this is the best we can
38-- really do for functor composition.
39instance Additive (f (g a)) => Additive ((f :.: g) a)
41-- | Product of additive semigroups, Functor style.
42instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)
44-- | Trivial additive semigroup, Functor style
45instance Additive (Proxy x)
47-- | Const lifts additive semigroups into a functor.
48instance Additive a => Additive (Const a x)
50-- | Identity lifts additive semigroups pointwise (at only one point)
51instance Additive a => Additive (Identity a)
53-- | Functions lift additive semigroups pointwise.
54instance Additive b => Additive (a -> b)