The document discusses monoids and their properties. It provides examples showing that integers, strings, lists and sets can form monoids when combined with operations like addition, concatenation and union. A monoid is an algebraic structure with an associative binary operation and an identity element. The document outlines the key properties of monoids including associativity, identity, and how mappings between monoids must preserve the monoid structure. It also discusses commutative monoids, free monoids and using monoids with reduce operations.
4. Monoid Rules
1)
a Op b = c
2) Zero Op a = a Op Zero = a
signatures:
Op: (T, T) => T
Zero: () => T // or rather just T
but wait, there's more!
5. Monoid Rules - associativity
(a Op b) Op c == a Op (b Op c)
Not everything is associative
5-(3-2) != (5-3)-2
avg(10, avg(30, 50)) != avg(avg(10, 30), 50)
Magma - like monoid, but no associativity and no unit
●
Tuples: (a, (b, c)) is not the same as ((a,b), c)
●
Binary trees:
6. Monoid Rules - associativity
(a1 Op (a2 Op (a3 Op (a4...))))
aka fold
(Zero /: a) (x Op y)
Can regroup and run in parallel: thanks to associativity!
This is what empowers Map/Reduce
7. Mappings between Monoids
f: A → B
f(ZeroA) = ZeroB
f(x OpA y) = f(x) OpB f(y)
e.g.
●
●
●
●
●
●
log(1)=0; log(a*b)=log(a)+log(b)
twice(n)=2*n; twice(0)=0, twice(n+m)=twice(n)+twice(m)
length("")=0; length(s1+s2)=length(s1)+length(s2)
sum(Nil)=0; sum(list1+list2)=sum(list1)+sum(list2)
prod(Nil)=1; prod(list1+list2)=prod(list1)*prod(list2)
maxOf(Nil)=Int.MinValue; maxOf(list1+list2)=max(maxOf
(list1)),maxOf(list2))
8. Free Monoid
any type
a monoid
take any function, f: A → B
this is equivalent to specifying...
f': List[A] → B
where
f'(Nil) = ZeroB
f'(list1 + list2) = f'(list1) OpB f'(list2)
List[A] is a Free Monoid on A
9. Free Monoid - example
Suppose we have...
object WeekDay extends Enumeration {
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
and a function
WeekDay=>Int = (Mon->1, Tue->1, Wed->1, Thu->1, Fri->1,
Sat->0, Sun->0)
If we have a monoid (Int, 0, +), we can automatically extend this
mapping to List[WeekDay]->Int
which counts the number of working days.
10. Reduce
reduce: List[A] → A
with properties:
●
●
reduce(Nil) = ZeroA
reduce(list1+list2) = reduce(list1) OpA reduce(list2)
(This defines an algebra over the functor List[_].)
This is exactly what we did on the previous page, reduce.
12. Commutative Monoids
a Op b == b Op a
e.g.
●
●
2+3==3+2
max(2.72, 3.14) == max(3.14, 2.72)
but not these:
●
●
"hello" + "world" != "world" + "hello"
List(1,2,3)+List(4,5,6) != List(4,5,6)+List(1,2,3)
13. Commutative Monoids
How can we create one?
e.g.
"abracadabra" -> "aaaaabbcdrr"
Order does not matter; numbers matter.
Free commutative monoid on A: a collection of possibly duplicates, order
does not matter.
It is called Bag[A]
17. Commutative Monoids...
How about sets?!
x Op x == x : x is idempotent
Set('a','b','c') + Set('b','c','d') = Set('a','b','c','d')
another example
●
●
max(x, x) = x
min(x, x) = x