Data.Monoid
- Package
- purescript-prelude
- Repository
- purescript/purescript-prelude
#Monoid Source
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
Monoid
s 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
<>
could be+
andmempty
could be0
<>
could be*
andmempty
could be1
.
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
mempty :: m
Instances
#power Source
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) == []
#MonoidRecord Source
class (SemigroupRecord rowlist row subrow) <= MonoidRecord rowlist row subrow | rowlist -> row subrow where
Members
memptyRecord :: forall rlproxy. rlproxy rowlist -> Record subrow
Instances
MonoidRecord Nil row ()
(IsSymbol key, Monoid focus, Cons key focus subrowTail subrow, MonoidRecord rowlistTail row subrowTail) => MonoidRecord (Cons key focus rowlistTail) row subrow
Re-exports from Data.Semigroup
#Semigroup Source
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:
First
- Use the first argument every time:append first _ = first
.Last
- Use the last argument every time:append _ last = last
.
Instances
#SemigroupRecord Source
class SemigroupRecord rowlist row subrow | rowlist -> subrow
Instances
SemigroupRecord Nil row ()
(IsSymbol key, Cons key focus subrowTail subrow, SemigroupRecord rowlistTail row subrowTail, Semigroup focus) => SemigroupRecord (Cons key focus rowlistTail) row subrow
- Modules
- Control.
Applicative - Control.
Apply - Control.
Bind - Control.
Category - Control.
Monad - Control.
Semigroupoid - Data.
Boolean - Data.
BooleanAlgebra - Data.
Bounded - Data.
Bounded. Generic - Data.
CommutativeRing - Data.
DivisionRing - Data.
Eq - Data.
Eq. Generic - Data.
EuclideanRing - Data.
Field - Data.
Function - Data.
Functor - Data.
Generic. Rep - Data.
HeytingAlgebra - Data.
HeytingAlgebra. Generic - Data.
Monoid - Data.
Monoid. Additive - Data.
Monoid. Conj - Data.
Monoid. Disj - Data.
Monoid. Dual - Data.
Monoid. Endo - Data.
Monoid. Generic - Data.
Monoid. Multiplicative - Data.
NaturalTransformation - Data.
Ord - Data.
Ord. Generic - Data.
Ordering - Data.
Ring - Data.
Ring. Generic - Data.
Semigroup - Data.
Semigroup. First - Data.
Semigroup. Generic - Data.
Semigroup. Last - Data.
Semiring - Data.
Semiring. Generic - Data.
Show - Data.
Show. Generic - Data.
Symbol - Data.
Unit - Data.
Void - Prelude
- Record.
Unsafe - Type.
Data. Row - Type.
Data. RowList - Type.
Proxy