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-compat-batteries-0.14.1Haskell2010

Data.Semigroup.Compat

  • 14 types
  • 1 class
  • 6 values
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 ":)"
Instances79Semigroup, …
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

Instances23Monad, 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

Instances23Monad, 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

Instances21Monad, 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

Instances21Monad, 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

Instances15NFData1, Generic1, Bounded, Enum, Eq, Data, …

Re-exported monoids from Data.Monoid

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

Instances22Monad, 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

Instances11Bounded, 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
  • NFData AllDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • 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

Instances11Bounded, 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
  • NFData AnyDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • 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

Instances23Monad, 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
  • NFData1 SumDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • 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
  • NFData a => NFData (Sum a)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • 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

Instances23Monad, 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

Instances19Bifoldable, Bifoldable1, Bifunctor, Bitraversable, NFData2, Generic1, …
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"