Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data.Monoid

Semigroups and monoids: associative combination, with and without an identity.

Semigroup is the associative operation alone; Monoid adds the identity as a superclass extension, so every Monoid carrier is a Semigroup and a constrained function asks for exactly the structure it uses: Min/Max combine associatively but have no identity without a greatest or least element, so they stop at Semigroup.

Instance resolution keys on the head type constructor, so a carrier admits exactly one instance. Int combines under both + and *, so Int itself gets no instance; the choice is spelled with a wrapper (Sum, Product), and the same convention picks between && and || at Bool (All, Any). List and String each have one canonical combination (concatenation), so they carry instances directly. Opt-in: not in Base.

Types

Sum

newtype Sum(a) = MkSum(a) deriving (Eq, Show)

Addition, chosen by wrapper. get_sum(mconcat(map(mk_sum, xs))) is sum(xs).

Product

newtype Product(a) = MkProduct(a) deriving (Eq, Show)

Multiplication, chosen by wrapper.

All

newtype All = MkAll(Bool) deriving (Eq, Show)

Conjunction: mempty is true, and a fold answers “did every element hold”.

Any

newtype Any = MkAny(Bool) deriving (Eq, Show)

Disjunction: mempty is false, and a fold answers “did any element hold”.

Min

newtype Min(a) = MkMin(a) deriving (Eq, Show)

The lesser of two values under Ord. A Semigroup only: with no greatest element there is no identity, which is exactly the Bounded requirement this library declines to invent.

Max

newtype Max(a) = MkMax(a) deriving (Eq, Show)

The greater of two values under Ord. A Semigroup only, for the dual reason.

Type Classes

Semigroup

class Semigroup(a)
  mappend : (a, a) -> a

An associative combination: mappend(x, mappend(y, z)) and mappend(mappend(x, y), z) agree for all x, y, z.

Monoid

class Monoid(a) given Semigroup(a)
  mempty : () -> a

A Semigroup with an identity: mappend(mempty(), x) and mappend(x, mempty()) are both x.

Instances

semigroupList

instance semigroupList : Semigroup(List(a))

monoidList

instance monoidList : Monoid(List(a))

semigroupStr

instance semigroupStr : Semigroup(String)

monoidStr

instance monoidStr : Monoid(String)

semigroupUnit

instance semigroupUnit : Semigroup(Unit)

monoidUnit

instance monoidUnit : Monoid(Unit)

semigroupOption

instance semigroupOption : Semigroup(Option(a))

None is the identity; two Somes combine under the payload’s own Semigroup. This is the classic lift of a semigroup to a monoid by adjoining a fresh identity.

monoidOption

instance monoidOption : Monoid(Option(a))

semigroupSum

instance semigroupSum : Semigroup(Sum(a))

monoidSum

instance monoidSum : Monoid(Sum(a))

semigroupProduct

instance semigroupProduct : Semigroup(Product(a))

monoidProduct

instance monoidProduct : Monoid(Product(a))

semigroupAll

instance semigroupAll : Semigroup(All)

monoidAll

instance monoidAll : Monoid(All)

semigroupAny

instance semigroupAny : Semigroup(Any)

monoidAny

instance monoidAny : Monoid(Any)

semigroupMin

instance semigroupMin : Semigroup(Min(a))

semigroupMax

instance semigroupMax : Semigroup(Max(a))

Functions and Values

mk_sum

mk_sum : forall a. (a) -> Data.Monoid.Sum(a)

Wrap for a Sum fold.

get_sum

get_sum : forall a. (Data.Monoid.Sum(a)) -> a

The payload of a Sum.

mk_product

mk_product : forall a. (a) -> Data.Monoid.Product(a)

Wrap for a Product fold.

get_product

get_product : forall a. (Data.Monoid.Product(a)) -> a

The payload of a Product.

mk_all

mk_all : (Bool) -> Data.Monoid.All

Wrap for an All fold.

get_all

get_all : (Data.Monoid.All) -> Bool

The payload of an All.

mk_any

mk_any : (Bool) -> Data.Monoid.Any

Wrap for an Any fold.

get_any

get_any : (Data.Monoid.Any) -> Bool

The payload of an Any.

mk_min

mk_min : forall a. (a) -> Data.Monoid.Min(a)

Wrap for a Min fold.

get_min

get_min : forall a. (Data.Monoid.Min(a)) -> a

The payload of a Min.

mk_max

mk_max : forall a. (a) -> Data.Monoid.Max(a)

Wrap for a Max fold.

get_max

get_max : forall a. (Data.Monoid.Max(a)) -> a

The payload of a Max.

mconcat

mconcat : forall a. (List(a)) -> a given Data.Monoid.Monoid(a)

The combination of a list, identity-first.

mconcat([[1, 2], [3], [4, 5]])
[1, 2, 3, 4, 5]

fold_map

fold_map : forall e0 a b c. ((a) -> b ! {e0}, c(a)) -> b ! {e0} given Foldable(c), Data.Monoid.Monoid(b)

Map each element into a monoid and combine the results, in fold order. The effect row of g rides through, like every Foldable aggregation.

(
  get_all(fold_map(\(x) -> mk_all(x > 0), [1, 2, 3])),
  fold_map(\(x) -> show(x), [1, 2, 3]),
)
(true, 123)