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
- Given a binary joining operation
•, - and all
a,b, andcof the set, - 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
Specs
append(any, any) :: any
Combine two members of the monoid, and return another member