Witchcraft v0.3.0 Witchcraft.Monoid protocol

Monoids are a set of elements, and a binary combining operation (op) that returns another member of the set.

Properties

Associativity

  1. Given a binary joining operation ,
  2. and all a, b, and c of the set,
  3. then: a • (b • c) == (a • b) • c

Identity element

  • Unique element (id, sometimes called the ‘zero’ of the set)
  • Behaves as an identity with op

identity = 0
op = &(&1 + &2) # Integer addition
append(34, identity) == 34

identity = 1
append = &(&1 * &2) # Integer multiplication
append(42, identity) == 42

Counter-Example

Integer division is not a monoid. Because you cannot divide by zero, the property does not hold for all values in the set.

Notes

You can of course abuse this protocol to define a fake ‘monoid’ that behaves differently. For the protocol to operate as intended, you need to respect the above properties.

Summary

Functions

Combine two members of the monoid, and return another member

Get the identity (‘zero’) element of the monoid by passing in any element of the set

Types

t :: term

Functions

append(a, b)

Specs

append(any, any) :: any

Combine two members of the monoid, and return another member

identity(a)

Specs

identity(any) :: any

Get the identity (‘zero’) element of the monoid by passing in any element of the set