Monoid

The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following: You can alternatively define mconcat instead of mempty, in which case the laws are: 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.
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:
>>> import Data.Int

>>> 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}
Reexports functions to work with monoids plus adds extra useful functions.
The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following: 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.
TextShow instances for Monoid-related newtypes. Since: 2
Monoid properties You will need TypeApplications to use these.
Abstract concept of a Monoid. Will be used in order to generate type classes for generic algebras. An algebra is a vector space that also is a monoid. Should we use the Monoid class from base library despite its unfortunate method name mappend?
Semigroups and monoids.
The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following laws:
  • mappend mempty x = x
  • mappend x mempty = x
  • mappend x (mappend y z) = mappend (mappend x y) z
  • mconcat = foldr mappend 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.
This module reexports functions to work with monoids plus adds extra useful functions.
TOML-specific combinators for converting between TOML and Haskell Monoid wrapper data types. These codecs are especially handy when you are implementing the Partial Options Monoid pattern. TODO: table
Monoid properties You will need TypeApplications to use these.
More monoids.
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:
>>> import Data.Int (Int)

>>> 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}
The distinction between appending on the left and appending on the right is important for monoids that are sensitive to ordering such as List. It is also of relevance for monoids with set semantics with non-extensional equality such as HashMap.
Bidirectional transforms for Data.Monoid.
This module defines the MonoidApplicative and MonoidAlternative type classes. Their methods are specialized forms of the standard Applicative and Alternative class methods. Instances of these classes should override the default method implementations with more efficient ones.
Eliminator functions for data types in Data.Monoid. All of these are re-exported from Data.Eliminator with the following exceptions: