Module

Data.Monoid

#Monoid

class (Semigroup m) <= Monoid m  where

A 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. Int, Boolean) can implement multiple law-abiding instances for Monoid. Let's use Int as an example

  1. <> could be + and mempty could be 0
  2. <> could be * and mempty could be 1.

To clarify these ambiguous situations, one should use the newtypes defined in Data.Monoid.<NewtypeName> modules.

In the above ambiguous situation, we could use Additive for the first situation or Multiplicative for the second one.

Members

Instances

#power

power :: forall m. Monoid m => m -> Int -> 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 mempty (unlike normal number exponentiation). The Monoid constraint alone is not enough to write a power function with the property that power x 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. a Group.

power [1,2] 3    == [1,2,1,2,1,2]
power [1,2] 1    == [1,2]
power [1,2] 0    == []
power [1,2] (-3) == []

#guard

guard :: forall m. Monoid m => Boolean -> m -> m

Allow or "truncate" a Monoid to its mempty value based on a condition.

#MonoidRecord

class MonoidRecord :: RowList Type -> Row Type -> Row Type -> Constraintclass (SemigroupRecord rowlist row subrow) <= MonoidRecord rowlist row subrow | rowlist -> row subrow where

A class for records where all fields have Monoid instances, used to implement the Monoid instance for records.

Members

Instances

Re-exports from Data.Semigroup

#Semigroup

class Semigroup a 

The Semigroup type class identifies an associative operation on a type.

Instances are required to satisfy the following law:

  • Associativity: (x <> y) <> z = x <> (y <> z)

One example of a Semigroup is String, with (<>) defined as string concatenation. Another example is List a, with (<>) defined as list concatenation.

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:

  1. First - Use the first argument every time: append first _ = first.
  2. Last - Use the last argument every time: append _ last = last.

Instances

#SemigroupRecord

class SemigroupRecord :: RowList Type -> Row Type -> Row Type -> Constraintclass SemigroupRecord rowlist row subrow | rowlist -> subrow

A class for records where all fields have Semigroup instances, used to implement the Semigroup instance for records.

Instances

#(<>)

Operator alias for Data.Semigroup.append (right-associative / precedence 5)

Modules