Monoids

It's a things.

Axiomatic Satisfaction

Associativity

(x \cdot y) \cdot z = x \cdot (y \cdot z)
(xy)z=x(yz)
a \space b \space c \in S
a b cS
(a \cdot b) \cdot c = a \cdot (b \cdot c)
(ab)c=a(bc)
\cdot : S \times S \rightarrow S
:S×SS

Identity

a \cdot e = a = e \cdot a
ae=a=ea
a \in S
aS
a \cdot e = a = e \cdot a
ae=a=ea
\cdot : S \times S \rightarrow S
:S×SS

e \in S
eS

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)
(ab)c=a(bc)
a * 1 = a = 1 * a
a1=a=1a

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)
(ab)c=a(bc)
a \wedge true = a = true \wedge a
atrue=a=truea

Booleans are a Monoid

with     (OR) and False

(a \vee b) \vee c = a \vee (b \vee c)
(ab)c=a(bc)
a \vee false = a = false \vee a
afalse=a=falsea

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)
(ab)c=a(bc)
a \cup \{\} = a = \{\} \cup a
a{}=a={}a

Function Composition

add5 : Number \rightarrow Number
add5:NumberNumber
add5 \space x = x + 5
add5 x=x+5
div2 : Number \rightarrow Number
div2:NumberNumber
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:ab
bar : b \rightarrow c
bar:bc
baz = bar \space \space \space \space \space \space \space foo
baz=bar       foo
baz : a \rightarrow c
baz:ac

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:aa
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