HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebase-4.20.2.0Haskell2010

Data.Semigroup

A type a is a Semigroup if it provides an associative function (<>) that lets you combine any two values of type a into one. Where being associative means that the following must always hold:

Property
(a <> b) <> c == a <> (b <> c)
Examples

The Min Semigroup instance for Int is defined to always pick the smaller number:

Example1 expression
Min 1 <> Min 2 <> Min 3 <> Min 4 :: Min IntMin {getMin = 1}

If we need to combine multiple values we can use the sconcat function to do so. We need to ensure however that we have at least one value to operate on, since otherwise our result would be undefined. It is for this reason that sconcat uses Data.List.NonEmpty.NonEmpty - a list that can never be empty:

Example1 expression
(1 :| [])1 :| []               -- equivalent to [1] but guaranteed to be non-empty.
Example1 expression
(1 :| [2, 3, 4])1 :| [2,3,4]          -- equivalent to [1,2,3,4] but guaranteed to be non-empty.

Equipped with this guaranteed to be non-empty data structure, we can combine values using sconcat and a Semigroup of our choosing. We can try the Min and Max instances of Int which pick the smallest, or largest number respectively:

Example1 expression
sconcat (1 :| [2, 3, 4]) :: Min IntMin {getMin = 1}
Example1 expression
sconcat (1 :| [2, 3, 4]) :: Max IntMax {getMax = 4}

String concatenation is another example of a Semigroup instance:

Example1 expression
"foo" <> "bar""foobar"

A Semigroup is a generalization of a Monoid. Yet unlike the Semigroup, the Monoid requires the presence of a neutral element (mempty) in addition to the associative operator. The requirement for a neutral element prevents many types from being a full Monoid, like Data.List.NonEmpty.NonEmpty.

Note that the use of (<>) in this module conflicts with an operator with the same name that is being exported by Data.Monoid. However, this package re-exports (most of) the contents of Data.Monoid, so to use semigroups and monoids in the same package just

import Data.Semigroup
  • 14 types
  • 1 class
  • 6 values
  • Packagebase-4.20.2.0
  • Exports21
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceSemigroup.hs
classclass Semigroup a where
#

The class of semigroups (types with an associative binary operation).

Instances should satisfy the following:

Associativity

x <> (y <> z) = (x <> y) <> z

You can alternatively define sconcat instead of (<>), in which case the laws are:

Unit

sconcat (pure x) = x

Multiplication

sconcat (join xss) = sconcat (fmap sconcat xss)

Methods

  • (<>) :: a -> a -> ainfixr 6

    An associative operation.

    Examples
    Example1 expression
    [1,2,3] <> [4,5,6][1,2,3,4,5,6]
    Example1 expression
    Just [1, 2, 3] <> Just [4, 5, 6]Just [1,2,3,4,5,6]
    Example1 expression
    putStr "Hello, " <> putStrLn "World!"Hello, World!
  • sconcat :: NonEmpty a -> a

    Reduce a non-empty list with <>

    The default definition should be sufficient, but this can be overridden for efficiency.

    Examples

    For the following examples, we will assume that we have:

    Example1 expression
    import Data.List.NonEmpty (NonEmpty (..))
    Example1 expression
    sconcat $ "Hello" :| [" ", "Haskell", "!"]"Hello Haskell!"
    Example1 expression
    sconcat $ Just [1, 2, 3] :| [Nothing, Just [4, 5, 6]]Just [1,2,3,4,5,6]
    Example1 expression
    sconcat $ Left 1 :| [Right 2, Left 3, Right 4]Right 2
  • stimes :: Integral b => b -> a -> a

    Repeat a value n times.

    The default definition will raise an exception for a multiplier that is <= 0. This may be overridden with an implementation that is total. For monoids it is preferred to use stimesMonoid.

    By making this a member of the class, idempotent semigroups and monoids can upgrade this to execute in \mathcal{O}(1) by picking stimes = stimesIdempotent or stimes = stimesIdempotentMonoid respectively.

    Examples
    Example1 expression
    stimes 4 [1][1,1,1,1]
    Example1 expression
    stimes 5 (putStr "hi!")hi!hi!hi!hi!hi!
    Example1 expression
    stimes 3 (Right ":)")Right ":)"
Instances64Semigroup, …
  • Semigroup ByteArrayDefined in base-4.20.2.0 · Data.Array.Byte
  • Semigroup VoidDefined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Internal.Types
  • Semigroup EventLifetimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Internal.Types
  • Semigroup LifetimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Internal.Types
  • Semigroup ExceptionContextDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.Context
  • Semigroup OrderingDefined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup ()Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Monoid m => Semigroup (WrappedMonoid m)Defined in base-4.20.2.0 · Data.Semigroup
  • Semigroup (FromMaybe b)Defined in base-4.20.2.0 · Data.Foldable1
  • Semigroup (NonEmptyDList a)Defined in base-4.20.2.0 · Data.Foldable1
  • Semigroup (Comparison a)Defined in base-4.20.2.0 · Data.Functor.Contravariant

    (<>) on comparisons combines results with (<>) @Ordering. Without newtypes this equals liftA2 (liftA2 (<>)).

    (<>) :: Comparison a -> Comparison a -> Comparison a
    Comparison cmp <> Comparison cmp' = Comparison a a' ->
      cmp a a' <> cmp a a'
    
  • Semigroup (Equivalence a)Defined in base-4.20.2.0 · Data.Functor.Contravariant

    (<>) on equivalences uses logical conjunction (&&) on the results. Without newtypes this equals liftA2 (liftA2 (&&)).

    (<>) :: Equivalence a -> Equivalence a -> Equivalence a
    Equivalence equiv <> Equivalence equiv' = Equivalence a b ->
      equiv a b && equiv' a b
    
  • Semigroup (Predicate a)Defined in base-4.20.2.0 · Data.Functor.Contravariant

    (<>) on predicates uses logical conjunction (&&) on the results. Without newtypes this equals liftA2 (&&).

    (<>) :: Predicate a -> Predicate a -> Predicate a
    Predicate pred <> Predicate pred' = Predicate a ->
      pred a && pred' a
    
  • Semigroup (First a)Defined in base-4.20.2.0 · Data.Semigroup
  • Semigroup (Last a)Defined in base-4.20.2.0 · Data.Semigroup
  • Semigroup (NonEmpty a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
  • Semigroup (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
  • Semigroup (Endo a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup a => Semigroup (JoinWith a)Defined in base-4.20.2.0 · Data.Foldable1
  • Semigroup a => Semigroup (STM a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Conc.Sync
  • Semigroup a => Semigroup (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Identity
  • Semigroup a => Semigroup (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Ord
  • Semigroup a => Semigroup (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup a => Semigroup (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup a => Semigroup (IO a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup a => Semigroup (a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup p => Semigroup (Par1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Bits a => Semigroup (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => Semigroup (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • Num a => Semigroup (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Semigroup (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Ord a => Semigroup (Max a)Defined in base-4.20.2.0 · Data.Semigroup
  • Ord a => Semigroup (Min a)Defined in base-4.20.2.0 · Data.Semigroup
  • Ord a => Semigroup (Max a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Utils
  • Ord a => Semigroup (Min a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Utils
  • (Generic a, Semigroup (Rep a ())) => Semigroup (Generically a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup (Either a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Either
  • Semigroup (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Semigroup (U1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup (V1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup a => Semigroup (Op a b)Defined in base-4.20.2.0 · Data.Functor.Contravariant

    (<>) @(Op a b) without newtypes is (<>) @(b->a) = liftA2 (<>). This lifts the Semigroup operation (<>) over the output of a.

    (<>) :: Op a b -> Op a b -> Op a b
    Op f <> Op g = Op a -> f a <> g a
    
  • Semigroup a => Semigroup (ST s a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
  • Semigroup b => Semigroup (a -> b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • (Semigroup a, Semigroup b) => Semigroup (a, b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Alternative f => Semigroup (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup (f p) => Semigroup (Rec1 f p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup a => Semigroup (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const
  • (Applicative f, Semigroup a) => Semigroup (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
  • (Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup c => Semigroup (K1 i c p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • (Semigroup (f a), Semigroup (g a)) => Semigroup (Product f g a)Defined in base-4.20.2.0 · Data.Functor.Product
  • (Semigroup (f p), Semigroup (g p)) => Semigroup ((:*:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • (Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
  • Semigroup (f (g a)) => Semigroup (Compose f g a)Defined in base-4.20.2.0 · Data.Functor.Compose
  • Semigroup (f (g p)) => Semigroup ((:.:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup (f p) => Semigroup (M1 i c f p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • (Semigroup a, Semigroup b, Semigroup c, Semigroup d, Semigroup e) => Semigroup (a, b, c, d, e)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
valuestimesIdempotent :: Integral b => b -> a -> a
#

This is a valid definition of stimes for an idempotent Semigroup.

When x <> x = x, this definition should be preferred, because it works in \mathcal{O}(1) rather than \mathcal{O}(\log n).

valuestimesIdempotentMonoid :: (Integral b, Monoid a) => b -> a -> a
#

This is a valid definition of stimes for an idempotent Monoid.

When x <> x = x, this definition should be preferred, because it works in \mathcal{O}(1) rather than \mathcal{O}(\log n)

valuemtimesDefault :: (Integral b, Monoid a) => b -> a -> a
#

Repeat a value n times.

mtimesDefault n a = a <> a <> ... <> a  -- using <> (n-1) times

In many cases, stimes 0 a for a Monoid will produce mempty. However, there are situations when it cannot do so. In particular, the following situation is fairly common:

data T a = ...

class Constraint1 a
class Constraint1 a => Constraint2 a
instance Constraint1 a => Semigroup (T a)
instance Constraint2 a => Monoid (T a)

Since Constraint1 is insufficient to implement mempty, stimes for T a cannot do so.

When working with such a type, or when working polymorphically with Semigroup instances, mtimesDefault should be used when the multiplier might be zero. It is implemented using stimes when the multiplier is nonzero and mempty when it is zero.

Examples
Example1 expression
mtimesDefault 0 "bark"[]
Example1 expression
mtimesDefault 3 "meow""meowmeowmeow"

Semigroups

5 declarations
newtypenewtype Min a
#

The Min Monoid and Semigroup always choose the smaller element as by the Ord instance and min of the contained type.

Examples
Example1 expression
Min 42 <> Min 3Min 3
Example1 expression
sconcat $ Min 1 :| [ Min n | n <- [2 .. 100]]Min {getMin = 1}

Constructors

Instances21Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype Max a
#

The Max Monoid and Semigroup always choose the bigger element as by the Ord instance and max of the contained type.

Examples
Example1 expression
Max 42 <> Max 3Max 42
Example1 expression
sconcat $ Max 1 :| [ Max n | n <- [2 .. 100]]Max {getMax = 100}

Constructors

Instances21Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype First a
#

Beware that Data.Semigroup.First is different from Data.Monoid.First. The former simply returns the first value, so Data.Semigroup.First Nothing <> x = Data.Semigroup.First Nothing. The latter returns the first non-Nothing, thus Data.Monoid.First Nothing <> x = x.

Examples
Example1 expression
First 0 <> First 10First 0
Example1 expression
sconcat $ First 1 :| [ First n | n <- [2 ..] ]First 1

Constructors

Instances19Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype Last a
#

Beware that Data.Semigroup.Last is different from Data.Monoid.Last. The former simply returns the last value, so x <> Data.Semigroup.Last Nothing = Data.Semigroup.Last Nothing. The latter returns the last non-Nothing, thus x <> Data.Monoid.Last Nothing = x.

Examples
Example1 expression
Last 0 <> Last 10Last {getLast = 10}
Example1 expression
sconcat $ Last 1 :| [ Last n | n <- [2..]]Last {getLast = * hangs forever *

Constructors

Instances19Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype WrappedMonoid m
#

Provide a Semigroup for an arbitrary Monoid.

NOTE: This is not needed anymore since Semigroup became a superclass of Monoid in base-4.11 and this newtype be deprecated at some point in the future.

Constructors

Instances13Generic1, Bounded, Enum, Eq, Data, Ord, …

Re-exported monoids

6 declarations
newtypenewtype Dual a
#

The dual of a Monoid, obtained by swapping the arguments of (<>).

Dual a <> Dual b == Dual (b <> a)
Examples
Example1 expression
Dual "Hello" <> Dual "World"Dual {getDual = "WorldHello"}
Example1 expression
Dual (Dual "Hello") <> Dual (Dual "World")Dual {getDual = Dual {getDual = "HelloWorld"}}

Constructors

Instances20Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype Endo a
#

The monoid of endomorphisms under composition.

Endo f <> Endo g == Endo (f . g)
Examples
Example2 expressions
let computation = Endo ("Hello, " ++) <> Endo (++ "!")appEndo computation "Haskell""Hello, Haskell!"
Example2 expressions
let computation = Endo (*3) <> Endo (+1)appEndo computation 16

Constructors

Instances4Generic, Semigroup, Monoid, Rep
newtypenewtype All
#

Boolean monoid under conjunction (&&).

All x <> All y = All (x && y)
Examples
Example1 expression
All True <> mempty <> All False)All {getAll = False}
Example1 expression
mconcat (map (\x -> All (even x)) [2,4,6,7,8])All {getAll = False}
Example1 expression
All True <> memptyAll {getAll = True}

Constructors

Instances10Bounded, Eq, Data, Ord, Read, Show, …
  • Bounded AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Eq AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Data AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Read AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Show AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Generic AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Monoid AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • type Rep All = D1 ('MetaData "All" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "All" 'PrefixI 'True) (S1 ('MetaSel ('Just "getAll") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
newtypenewtype Any
#

Boolean monoid under disjunction (||).

Any x <> Any y = Any (x || y)
Examples
Example1 expression
Any True <> mempty <> Any FalseAny {getAny = True}
Example1 expression
mconcat (map (\x -> Any (even x)) [2,4,6,7,8])Any {getAny = True}
Example1 expression
Any False <> memptyAny {getAny = False}

Constructors

Instances10Bounded, Eq, Data, Ord, Read, Show, …
  • Bounded AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Eq AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Data AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Read AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Show AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Generic AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Monoid AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • type Rep Any = D1 ('MetaData "Any" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "Any" 'PrefixI 'True) (S1 ('MetaSel ('Just "getAny") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
newtypenewtype Sum a
#

Monoid under addition.

Sum a <> Sum b = Sum (a + b)
Examples
Example1 expression
Sum 1 <> Sum 2 <> memptySum {getSum = 3}
Example1 expression
mconcat [ Sum n | n <- [3 .. 9]]Sum {getSum = 42}

Constructors

Instances21Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
  • Monad SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Functor SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • MonadFix SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.Fix
  • Applicative SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Foldable SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Foldable
  • Traversable SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Traversable
  • MonadZip SumDefined in base-4.20.2.0 · Control.Monad.Zip
  • Foldable1 SumDefined in base-4.20.2.0 · Data.Foldable1
  • Generic1 SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Bounded a => Bounded (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Eq a => Eq (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Data a => Data (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num a => Num (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Ord a => Ord (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Read a => Read (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Show a => Show (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Generic (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Semigroup (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Monoid (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • type Rep (Sum a) = D1 ('MetaData "Sum" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "Sum" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSum") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • type Rep1 Sum = D1 ('MetaData "Sum" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "Sum" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSum") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
newtypenewtype Product a
#

Monoid under multiplication.

Product x <> Product y == Product (x * y)
Examples
Example1 expression
Product 3 <> Product 4 <> memptyProduct {getProduct = 12}
Example1 expression
mconcat [ Product n | n <- [2 .. 10]]Product {getProduct = 3628800}

Constructors

Instances21Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …

Difference lists of a semigroup

2 declarations
valuediff :: Semigroup m => m -> Endo m
#

This lets you use a difference list of a Semigroup as a Monoid.

Examples
let hello = diff "Hello, "
Example1 expression
appEndo hello "World!""Hello, World!"
Example1 expression
appEndo (hello <> mempty) "World!""Hello, World!"
Example1 expression
appEndo (mempty <> hello) "World!""Hello, World!"
let world = diff "World"
let excl = diff "!"
Example1 expression
appEndo (hello <> (world <> excl)) mempty"Hello, World!"
Example1 expression
appEndo ((hello <> world) <> excl) mempty"Hello, World!"
valuecycle1 :: Semigroup m => m -> m
#

A generalization of cycle to an arbitrary Semigroup. May fail to terminate for some values in some semigroups.

Examples
Example1 expression
take 10 $ cycle1 [1, 2, 3][1,2,3,1,2,3,1,2,3,1]
Example1 expression
cycle1 (Right 1)Right 1
Example1 expression
cycle1 (Left 1)* hangs forever *

ArgMin, ArgMax

3 declarations
datadata Arg a b
#

Arg isn't itself a Semigroup in its own right, but it can be placed inside Min and Max to compute an arg min or arg max.

Examples
Example1 expression
minimum [ Arg (x * x) x | x <- [-10 .. 10] ]Arg 0 0
Example1 expression
maximum [ Arg (-0.2*x^2 + 1.5*x + 1) x | x <- [-10 .. 10] ]Arg 3.8 4.0
Example1 expression
minimum [ Arg (-0.2*x^2 + 1.5*x + 1) x | x <- [-10 .. 10] ]Arg (-34.0) (-10.0)

Constructors

Instances16Bifoldable, Bifoldable1, Bifunctor, Bitraversable, Generic1, Functor, …
typetype ArgMin a b = Min (Arg a b)
#
Examples
Example1 expression
Min (Arg 0 ()) <> Min (Arg 1 ())Min {getMin = Arg 0 ()}
Example1 expression
minimum [ Arg (length name) name | name <- ["violencia", "lea", "pixie"]]Arg 3 "lea"
typetype ArgMax a b = Max (Arg a b)
#
Examples
Example1 expression
Max (Arg 0 ()) <> Max (Arg 1 ())Max {getMax = Arg 1 ()}
Example1 expression
maximum [ Arg (length name) name | name <- ["violencia", "lea", "pixie"]]Arg 9 "violencia"