haskell / 9 / libraries / base-4.17.0.0 / data-monoid.html

Data.Monoid

Copyright (c) Andy Gill 2001
(c) Oregon Graduate Institute of Science and Technology 2001
License BSD-style (see the file libraries/base/LICENSE)
Maintainer libraries@haskell.org
Stability stable
Portability portable
Safe Haskell Trustworthy
Language Haskell2010

Description

A type a is a Monoid if it provides an associative function (<>) that lets you combine any two values of type a into one, and a neutral element (mempty) such that

a <> mempty == mempty <> a == a

A Monoid is a Semigroup with the added requirement of a neutral element. Thus any Monoid is a Semigroup, but not the other way around.

Examples
Expand

The Sum monoid is defined by the numerical addition operator and `0` as neutral element:

>>> mempty :: Sum Int
Sum {getSum = 0}
>>> Sum 1 <> Sum 2 <> Sum 3 <> Sum 4 :: Sum Int
Sum {getSum = 10}

We can combine multiple values in a list into a single value using the mconcat function. Note that we have to specify the type here since Int is a monoid under several different operations:

>>> mconcat [1,2,3,4] :: Sum Int
Sum {getSum = 10}
>>> mconcat [] :: Sum Int
Sum {getSum = 0}

Another valid monoid instance of Int is Product It is defined by multiplication and `1` as neutral element:

>>> Product 1 <> Product 2 <> Product 3 <> Product 4 :: Product Int
Product {getProduct = 24}
>>> mconcat [1,2,3,4] :: Product Int
Product {getProduct = 24}
>>> mconcat [] :: Product Int
Product {getProduct = 1}

Monoid typeclass

class Semigroup a => Monoid a where Source

The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following:

Right identity
x <> mempty = x
Left identity
mempty <> x = x
Associativity
x <> (y <> z) = (x <> y) <> z (Semigroup law)
Concatenation
mconcat = foldr (<>) mempty

The method names refer to the monoid of lists under concatenation, but there are many other instances.

Some types can be viewed as a monoid in more than one way, e.g. both addition and multiplication on numbers. In such cases we often define newtypes and make those instances of Monoid, e.g. Sum and Product.

NOTE: Semigroup is a superclass of Monoid since base-4.11.0.0.

Minimal complete definition

mempty

Methods

mempty :: a Source

Identity of mappend

>>> "Hello world" <> mempty
"Hello world"

mappend :: a -> a -> a Source

An associative operation

NOTE: This method is redundant and has the default implementation mappend = (<>) since base-4.11.0.0. Should it be implemented manually, since mappend is a synonym for (<>), it is expected that the two functions are defined the same way. In a future GHC release mappend will be removed from Monoid.

mconcat :: [a] -> a Source

Fold a list using the monoid.

For most types, the default definition for mconcat will be used, but the function is included in the class definition so that an optimized version can be provided for specific types.

>>> mconcat ["Hello", " ", "Haskell", "!"]
"Hello Haskell!"
Instances
Instances details
Monoid ByteArray Source

Since: base-4.17.0.0

Instance details

Defined in Data.Array.Byte

Monoid All Source

Since: base-2.1

Instance details

Defined in Data.Semigroup.Internal

Methods

mempty :: All Source

mappend :: All -> All -> All Source

mconcat :: [All] -> All Source

Monoid Any Source

Since: base-2.1

Instance details

Defined in Data.Semigroup.Internal

Methods

mempty :: Any Source

mappend :: Any -> Any -> Any Source

mconcat :: [Any] -> Any Source

Monoid Event Source

Since: base-4.4.0.0

Instance details

Defined in GHC.Event.Internal.Types

Monoid Lifetime Source

mappend takes the longer of two lifetimes.

Since: base-4.8.0.0

Instance details

Defined in GHC.Event.Internal.Types

Monoid Ordering Source

Since: base-2.1

Instance details

Defined in GHC.Base

Monoid () Source

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

mempty :: () Source

mappend :: () -> () -> () Source

mconcat :: [()] -> () Source

FiniteBits a => Monoid (And a) Source

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

Since: base-4.16

Instance details

Defined in Data.Bits

Methods

mempty :: And a Source

mappend :: And a -> And a -> And a Source

mconcat :: [And a] -> And a Source

FiniteBits a => Monoid (Iff a) Source

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

Since: base-4.16

Instance details

Defined in Data.Bits

Methods

mempty :: Iff a Source

mappend :: Iff a -> Iff a -> Iff a Source

mconcat :: [Iff a] -> Iff a Source

Bits a => Monoid (Ior a) Source

Since: base-4.16

Instance details

Defined in Data.Bits

Methods

mempty :: Ior a Source

mappend :: Ior a -> Ior a -> Ior a Source

mconcat :: [Ior a] -> Ior a Source

Bits a => Monoid (Xor a) Source

Since: base-4.16

Instance details

Defined in Data.Bits

Methods

mempty :: Xor a Source

mappend :: Xor a -> Xor a -> Xor a Source

mconcat :: [Xor a] -> Xor a Source

Monoid (Comparison a) Source

mempty on comparisons always returns EQ. Without newtypes this equals pure (pure EQ).

mempty :: Comparison a
mempty = Comparison _ _ -> EQ
Instance details

Defined in Data.Functor.Contravariant

Monoid (Equivalence a) Source

mempty on equivalences always returns True. Without newtypes this equals pure (pure True).

mempty :: Equivalence a
mempty = Equivalence _ _ -> True
Instance details

Defined in Data.Functor.Contravariant

Monoid (Predicate a) Source

mempty on predicates always returns True. Without newtypes this equals pure True.

mempty :: Predicate a
mempty = _ -> True
Instance details

Defined in Data.Functor.Contravariant

Monoid a => Monoid (Identity a) Source

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Identity

Monoid (First a) Source

Since: base-2.1

Instance details

Defined in Data.Monoid

Methods

mempty :: First a Source

mappend :: First a -> First a -> First a Source

mconcat :: [First a] -> First a Source

Monoid (Last a) Source

Since: base-2.1

Instance details

Defined in Data.Monoid

Methods

mempty :: Last a Source

mappend :: Last a -> Last a -> Last a Source

mconcat :: [Last a] -> Last a Source

Monoid a => Monoid (Down a) Source

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

mempty :: Down a Source

mappend :: Down a -> Down a -> Down a Source

mconcat :: [Down a] -> Down a Source

(Ord a, Bounded a) => Monoid (Max a) Source

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Methods

mempty :: Max a Source

mappend :: Max a -> Max a -> Max a Source

mconcat :: [Max a] -> Max a Source

(Ord a, Bounded a) => Monoid (Min a) Source

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Methods

mempty :: Min a Source

mappend :: Min a -> Min a -> Min a Source

mconcat :: [Min a] -> Min a Source

Monoid m => Monoid (WrappedMonoid m) Source

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Monoid a => Monoid (Dual a) Source

Since: base-2.1

Instance details

Defined in Data.Semigroup.Internal

Methods

mempty :: Dual a Source

mappend :: Dual a -> Dual a -> Dual a Source

mconcat :: [Dual a] -> Dual a Source

Monoid (Endo a) Source

Since: base-2.1

Instance details

Defined in Data.Semigroup.Internal

Methods

mempty :: Endo a Source

mappend :: Endo a -> Endo a -> Endo a Source

mconcat :: [Endo a] -> Endo a Source

Num a => Monoid (Product a) Source

Since: base-2.1

Instance details

Defined in Data.Semigroup.Internal

Num a => Monoid (Sum a) Source

Since: base-2.1

Instance details

Defined in Data.Semigroup.Internal

Methods

mempty :: Sum a Source

mappend :: Sum a -> Sum a -> Sum a Source

mconcat :: [Sum a] -> Sum a Source

Monoid a => Monoid (STM a) Source

Since: base-4.17.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

mempty :: STM a Source

mappend :: STM a -> STM a -> STM a Source

mconcat :: [STM a] -> STM a Source

(Generic a, Monoid (Rep a ())) => Monoid (Generically a) Source

Since: base-4.17.0.0

Instance details

Defined in GHC.Generics

Monoid p => Monoid (Par1 p) Source

Since: base-4.12.0.0

Instance details

Defined in GHC.Generics

Methods

mempty :: Par1 p Source

mappend :: Par1 p -> Par1 p -> Par1 p Source

mconcat :: [Par1 p] -> Par1 p Source

Monoid a => Monoid (IO a) Source

Since: base-4.9.0.0

Instance details

Defined in GHC.Base

Methods

mempty :: IO a Source

mappend :: IO a -> IO a -> IO a Source

mconcat :: [IO a] -> IO a Source

Semigroup a => Monoid (Maybe a) Source

Lift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup S may be turned into a monoid simply by adjoining an element e not in S and defining e*e = e and e*s = s = s*e for all s ∈ S."

Since 4.11.0: constraint on inner a value generalised from Monoid to Semigroup.

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

mempty :: Maybe a Source

mappend :: Maybe a -> Maybe a -> Maybe a Source

mconcat :: [Maybe a] -> Maybe a Source

Monoid a => Monoid (a) Source

Since: base-4.15

Instance details

Defined in GHC.Base

Methods

mempty :: (a) Source

mappend :: (a) -> (a) -> (a) Source

mconcat :: [(a)] -> (a) Source

Monoid [a] Source

Since: base-2.1

Instance details

Defined in GHC.Base

Methods

mempty :: [a] Source

mappend :: [a] -> [a] -> [a] Source

mconcat :: [[a]] -> [a] Source

Monoid a => Monoid (Op a b) Source

mempty @(Op a b) without newtypes is mempty @(b->a) = _ -> mempty.

mempty :: Op a b
mempty = Op _ -> mempty
Instance details

Defined in Data.Functor.Contravariant

Methods

mempty :: Op a b Source

mappend :: Op a b -> Op a b -> Op a b Source

mconcat :: [Op a b] -> Op a b Source

Monoid (Proxy s) Source

Since: base-4.7.0.0

Instance details

Defined in Data.Proxy

Methods

mempty :: Proxy s Source

mappend :: Proxy s -> Proxy s -> Proxy s Source

mconcat :: [Proxy s] -> Prox