Monoids
It's a things.
Axiomatic Satisfaction
Associativity
(x \cdot y) \cdot z = x \cdot (y \cdot z)
(x⋅y)⋅z=x⋅(y⋅z)
a \space b \space c \in S
a b c∈S
(a \cdot b) \cdot c = a \cdot (b \cdot c)
(a⋅b)⋅c=a⋅(b⋅c)
\cdot : S \times S \rightarrow S
⋅:S×S→S
∀
Identity
a \cdot e = a = e \cdot a
a⋅e=a=e⋅a
a \in S
a∈S
a \cdot e = a = e \cdot a
a⋅e=a=e⋅a
\cdot : S \times S \rightarrow S
⋅:S×S→S
∀
e \in S
e∈S
Monoidal Objects
Integers are a Monoid
with + and 0
(a + b) + c = a + (b + c)
(a+b)+c=a+(b+c)
a + 0 = a = 0 + a
a+0=a=0+a
Integers are a Monoid
with * and 1
(a * b) * c = a * (b * c)
(a∗b)∗c=a∗(b∗c)
a * 1 = a = 1 * a
a∗1=a=1∗a
Lists are a Monoid
with concat and []
(a \space \space \space \space b) \space \space \space \space c = a \space \space \space \space (b \space \space \space \space c)
(a b) c=a (b c)
a \space \space \space \space [] = a = [] \space \space \space \space a
a []=a=[] a
⧺
⧺
⧺
⧺
⧺
⧺
Booleans are a Monoid
with (AND) and True
(a \wedge b) \wedge c = a \wedge (b \wedge c)
(a∧b)∧c=a∧(b∧c)
a \wedge true = a = true \wedge a
a∧true=a=true∧a
∧
Booleans are a Monoid
with (OR) and False
(a \vee b) \vee c = a \vee (b \vee c)
(a∨b)∨c=a∨(b∨c)
a \vee false = a = false \vee a
a∨false=a=false∨a
∨
Booleans are a Monoid
with (XOR) and False
(a \space \oplus \space b) \space \oplus \space c = a \space \oplus \space (b \space \oplus \space c)
(a ⊕ b) ⊕ c=a ⊕ (b ⊕ c)
a \space \oplus \space false = a = false \space \oplus \space a
a ⊕ false=a=false ⊕ a
⊕
Sets are a Monoid
with (union) and {}
(a \cup b) \cup c = a \cup (b \cup c)
(a∪b)∪c=a∪(b∪c)
a \cup \{\} = a = \{\} \cup a
a∪{}=a={}∪a
∪
Function Composition
add5 : Number \rightarrow Number
add5:Number→Number
add5 \space x = x + 5
add5 x=x+5
div2 : Number \rightarrow Number
div2:Number→Number
div2 \space x = x / 2
div2 x=x/2
a : Number
a:Number
a = 3
a=3
add5 \space 6 = 6 + 5 = 11
add5 6=6+5=11
div2 \space 12 = 12 / 2 = 6
div2 12=12/2=6
div2 \space (add5 \space 8) = 4
div2 (add5 8)=4
ad5dv2 \space x = div2 \space (add5 \space x)
ad5dv2 x=div2 (add5 x)
ad5dv2 \space 7 = 6
ad5dv2 7=6
ad5dv2 = div2 \space \space \space \space \space \space \space add5
ad5dv2=div2 add5
⋘
foo : a \rightarrow b
foo:a→b
bar : b \rightarrow c
bar:b→c
baz = bar \space \space \space \space \space \space \space foo
baz=bar foo
baz : a \rightarrow c
baz:a→c
⋘
Functions are a Monoid
with and id
(a \space \space \space \space \space b) \space \space \space \space \space c = a \space \space \space \space \space (b \space \space \space \space \space c)
(a b) c=a (b c)
a \space \space \space \space \space id = a = id \space \space \space \space \space a
a id=a=id a
⋘
⋘
⋘
⋘
⋘
⋘
⋘
id : a \rightarrow a
id:a→a
id \space x = x
id x=x
Other Monoids
- Observables (Subscriptions)
- Publishing
- Promises
- Any Functor containing a Monoid is a Monoid
Example Functors
- Lists
- Trees
- Sets
mconcat
mconcat [[0,1,2], [3], [], [4,5]] = [0, 1, 2, 3, 4, 5]
mconcat ["hi", " ", "there", " ", "folks"] = "hi there folks"
mconcat [0, 1, 2, 4, 5] = 12
mconcat [{a, b}, {}, {c, d}] = {a, b, c, d}mconcat : Monoid \space a \space \space \space \space \space \space \space [a] \rightarrow a
mconcat:Monoid a [a]→a
⇒
Monoids
By fresheyeball
Monoids
- 574