Monoid is a
Semigroup with a value
mempty, which is both a
left and right unit for the associative operation
- Left unit:
(mempty <> x) = x
- Right unit:
(x <> mempty) = x
Monoids are commonly used as the result of fold operations, where
<> is used to combine individual results, and
mempty gives the result
of folding an empty collection of elements.
Newtypes for Monoid
Some types (e.g.
Boolean) can implement multiple law-abiding
Monoid. Let's use
Int as an example
To clarify these ambiguous situations, one should use the newtypes
In the above ambiguous situation, we could use
for the first situation or
Multiplicative for the second one.
mempty :: m
Append a value to itself a certain number of times. For the
Multiplicative type, and for a non-negative power, this is the same as
normal number exponentiation.
If the second argument is negative this function will return
(unlike normal number exponentiation). The
Monoid constraint alone
is not enough to write a
power function with the property that
n cancels with
power x (-n), i.e.
power x n <> power x (-n) = mempty.
For that, we would additionally need the ability to invert elements, i.e.
power [1,2] 3 == [1,2,1,2,1,2] power [1,2] 1 == [1,2] power [1,2] 0 ==  power [1,2] (-3) == 
class (SemigroupRecord rowlist row subrow) <= MonoidRecord rowlist row subrow | rowlist -> row subrow where
Re-exports from Data.
class Semigroup a
Semigroup type class identifies an associative operation on a type.
Instances are required to satisfy the following law:
(x <> y) <> z = x <> (y <> z)
One example of a
(<>) defined as string
concatenation. Another example is
List a, with
(<>) defined as
Newtypes for Semigroup
There are two other ways to implement an instance for this type class regardless of which type is used. These instances can be used by wrapping the values in one of the two newtypes below:
First- Use the first argument every time:
append first _ = first.
Last- Use the last argument every time:
append _ last = last.