On this page
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
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(Semigrouplaw)- 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
Methods
Identity of mappend
>>> "Hello world" <> mempty
"Hello world"
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.
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
| Monoid ByteArray Source | Since: base-4.17.0.0 |
| Monoid All Source | Since: base-2.1 |
| Monoid Any Source | Since: base-2.1 |
| Monoid Event Source | Since: base-4.4.0.0 |
| Monoid Lifetime Source |
Since: base-4.8.0.0 |
| Monoid Ordering Source | Since: base-2.1 |
| Monoid () Source | Since: base-2.1 |
| FiniteBits a => Monoid (And a) Source | This constraint is arguably too strong. However, as some types (such as Since: base-4.16 |
| FiniteBits a => Monoid (Iff a) Source | This constraint is arguably too strong. However, as some types (such as Since: base-4.16 |
| Bits a => Monoid (Ior a) Source | Since: base-4.16 |
| Bits a => Monoid (Xor a) Source | Since: base-4.16 |
| Monoid (Comparison a) Source |
|
|
Defined in Data.Functor.Contravariant Methodsmempty :: Comparison a Source mappend :: Comparison a -> Comparison a -> Comparison a Source mconcat :: [Comparison a] -> Comparison a Source |
|
| Monoid (Equivalence a) Source |
|
|
Defined in Data.Functor.Contravariant Methodsmempty :: Equivalence a Source mappend :: Equivalence a -> Equivalence a -> Equivalence a Source mconcat :: [Equivalence a] -> Equivalence a Source |
|
| Monoid (Predicate a) Source |
|
| Monoid a => Monoid (Identity a) Source | Since: base-4.9.0.0 |
| Monoid (First a) Source | Since: base-2.1 |
| Monoid (Last a) Source | Since: base-2.1 |
| Monoid a => Monoid (Down a) Source | Since: base-4.11.0.0 |
| (Ord a, Bounded a) => Monoid (Max a) Source | Since: base-4.9.0.0 |
| (Ord a, Bounded a) => Monoid (Min a) Source | Since: base-4.9.0.0 |
| Monoid m => Monoid (WrappedMonoid m) Source | Since: base-4.9.0.0 |
|
Defined in Data.Semigroup Methodsmempty :: WrappedMonoid m Source mappend :: WrappedMonoid m -> WrappedMonoid m -> WrappedMonoid m Source mconcat :: [WrappedMonoid m] -> WrappedMonoid m Source |
|
| Monoid a => Monoid (Dual a) Source | Since: base-2.1 |
| Monoid (Endo a) Source | Since: base-2.1 |
| Num a => Monoid (Product a) Source | Since: base-2.1 |
| Num a => Monoid (Sum a) Source | Since: base-2.1 |
| Monoid a => Monoid (STM a) Source | Since: base-4.17.0.0 |
| (Generic a, Monoid (Rep a ())) => Monoid (Generically a) Source | Since: base-4.17.0.0 |
|
Defined in GHC.Generics Methodsmempty :: Generically a Source mappend :: Generically a -> Generically a -> Generically a Source mconcat :: [Generically a] -> Generically a Source |
|
| Monoid p => Monoid (Par1 p) Source | Since: base-4.12.0.0 |
| Monoid a => Monoid (IO a) Source | Since: base-4.9.0.0 |
| Semigroup a => Monoid (Maybe a) Source | Lift a semigroup into Since 4.11.0: constraint on inner Since: base-2.1 |
| Monoid a => Monoid (a) Source | Since: base-4.15 |
| Monoid [a] Source | Since: base-2.1 |
| Monoid a => Monoid (Op a b) Source |
|
| Monoid (Proxy s) Source | Since: base-4.7.0.0 |