category-theory-illustrated/_chapters/03_monoid.md

322 lines
19 KiB
Markdown
Raw Normal View History

2020-12-28 21:52:31 +03:00
---
layout: default
2021-01-22 15:40:10 +03:00
title: Monoids
2020-12-28 21:52:31 +03:00
---
2020-10-07 22:13:09 +03:00
2020-12-28 21:52:31 +03:00
Monoids etc
2020-10-07 22:13:09 +03:00
===
2021-02-23 14:33:24 +03:00
Since we are done with categories, let's look at some other structures that are also interesting - monoids. Like categories, monoids/groups are also abstract systems consisting of set of elements and rules for manipulating these elements.
2020-10-07 22:13:09 +03:00
What are monoids
===
2020-12-28 07:47:53 +03:00
Monoids are simpler than categories. A monoid is defined by a collection (set) of elements and an operation that allows us to combine two element and produce a third one of the same kind.
2020-10-07 22:13:09 +03:00
2021-04-01 10:29:59 +03:00
Let's take our familiar colorful balls.
2020-10-07 22:13:09 +03:00
2020-12-28 21:52:31 +03:00
![Balls](balls.svg)
2020-10-07 22:13:09 +03:00
2020-12-28 07:47:53 +03:00
In this case a monoid would be a rule (operation) for "combining" two balls into one.
2020-10-07 22:13:09 +03:00
An example of such rule would be blending the colors of the balls, as if we are mixing paint.
2020-12-28 21:52:31 +03:00
![A rule for combining balls](balls_rule.svg)
2020-10-07 22:13:09 +03:00
2020-12-28 07:47:53 +03:00
You can probably think of other ways to define such a rule. This will help you realize that there can be many ways to create a monoid from a given set of items. The monoid is not the set itself, it is the set *together with the rule*.
Associativity
---
The monoid rule should, like functional composition, be "associative" i.e. applying it on the same number of elements in a different order should make no difference.
2020-12-28 21:52:31 +03:00
![Associativity in the color mixing operation](balls_associativity.svg)
2020-12-28 07:47:53 +03:00
2021-01-23 11:56:16 +03:00
When a rule is associative, this means we can use all kinds of algebraic operations to any sequence of terms (or in other words to apply equation reasoning), like for example we can replace any element with a set of elements from which it is composed of, or add a term that is present at both sides of an equation and retaining the equality of the existing terms.
2020-12-28 07:47:53 +03:00
2020-12-28 21:52:31 +03:00
![Associativity in the color mixing operation](balls_arithmetic.svg)
2020-10-07 22:13:09 +03:00
The identity element
---
2021-01-23 11:56:16 +03:00
Actually, not any (associative) rule for combining elements makes the balls form a monoid (it makes them form a "semigroup", which is also a thing, but that's a separate topic). In order to be a monoid, a set must feature what is called an "identity element" of a given rule (or a *zero* element, if you prefer) - one that, when combined with any other element gives back that same element not the identity but the other one. Or simply **x • i = x and i • x = x for any x**. In the case of our color-mixing monoid the identity element is the white ball (or perhaps a transparent one, if we have one).
2020-10-07 22:13:09 +03:00
2020-12-28 21:52:31 +03:00
![The identity element of the color-mixing monoid](balls_identity.svg)
2020-10-07 22:13:09 +03:00
2020-12-28 07:47:53 +03:00
As you probably remember from the last chapter, functional composition is also associative and it also contains an identity element, so you might start suspecting that it forms a monoid in some way. And it is really the case with one little caveat.
2020-10-07 22:13:09 +03:00
2021-04-01 10:29:59 +03:00
To keep the suspense alive, let's see some simpler monoids before we delve into that:
2020-12-28 07:47:53 +03:00
Basic monoids
2020-10-07 22:13:09 +03:00
===
Monoids from numbers
---
2020-12-28 07:47:53 +03:00
Mathematics is not all about numbers, however numbers do tend to pop up in most of its areas and monoids are no exception. The set of natural numbers *N* form a monoid when combined with the all too familiar operation of addition (or to use the official terminology *N* *form* a monoid *under* addition).
2020-10-07 22:13:09 +03:00
2020-12-28 21:52:31 +03:00
![The monoid of numbers under addition](numbers_addition.svg)
2020-10-07 22:13:09 +03:00
2021-01-25 11:21:14 +03:00
(if you see a **1 + 1 = 2** in your textbook you know you are working on math foundations (or you are in kindergarten)).
2020-10-07 22:13:09 +03:00
2020-12-28 07:47:53 +03:00
The natural numbers also form a monoid under multiplication as well:
2020-10-07 22:13:09 +03:00
2020-12-28 21:52:31 +03:00
![The monoid of numbers under multiplication](numbers_multiplication.svg)
2020-10-07 22:13:09 +03:00
**Task:** Which are the identity elements of those monoids?
2021-01-27 12:41:58 +03:00
**Task:** Go through other mathematical operations and figure out why they are not monoidal.
2020-10-07 22:13:09 +03:00
2021-01-25 11:21:14 +03:00
Monoids from boolean algebra
2020-10-07 22:13:09 +03:00
---
2021-01-25 11:21:14 +03:00
Thinking about other operations that we covered (operation being a function which takes a pair of element of a given type and returns one element of the same type), we may remember the boolean operations **AND** and **OR**. which operate on the set, consisting of just two values **{ True, False }**. Those operations form monoids too. Proving that they do is easy enough by just enumerating all cases.
2020-10-07 22:13:09 +03:00
We can prove that **AND** is associative by expanding the formula **(A AND B) AND C = A AND (B AND C)** in all possible ways:
**(TRUE AND FALSE) AND TRUE = TRUE AND (FALSE AND TRUE)**
2020-12-28 21:52:31 +03:00
2020-10-07 22:13:09 +03:00
**(TRUE AND FALSE) AND FALSE = TRUE AND (FALSE AND FALSE)**
2020-12-28 21:52:31 +03:00
2020-10-07 22:13:09 +03:00
**(FALSE AND FALSE) AND TRUE = FALSE AND (FALSE AND TRUE)**
2020-12-28 21:52:31 +03:00
2020-10-07 22:13:09 +03:00
...
And we can prove that **TRUE** is the identity element by expanding the other formulas that state that for all elements **A** **I AND A = A**
**FALSE AND TRUE = FALSE**
2020-12-28 21:52:31 +03:00
2020-10-07 22:13:09 +03:00
**TRUE AND TRUE = TRUE**
...and then do the same for **A AND I = A**.
2020-12-28 07:47:53 +03:00
Intuitions
---
2021-01-25 11:21:14 +03:00
In order to form the correct intuition about monoids, try to avoid thinking of the elements in the set as objects, but instead think of them as *actions*. For example, when thinking about numbers don't think of them as *quantities* (as in two apples, two oranges etc.), but as *operations*, (e.g. as the action of adding one to a given quantity). In other words, don't think of the element by itself, but only think of it together with the operation (in this case addition).
2020-10-07 22:13:09 +03:00
2020-12-28 07:47:53 +03:00
This touches a programming concept which is very popular in category-theory inspired languages - currying - that is based on the idea that a function that accepts two arguments together with one of those arguments already supplied can be viewed as a function which takes one argument. e.g. the function `add(number, number)` together with the element `2` is equivalent to the `addTwo(number)` function.
2020-10-07 22:13:09 +03:00
2021-02-23 14:33:24 +03:00
In general, we use monoids and related structures as a way to model how a set of (associative) actions that are performed on a given object (or objects) alter it's state. We can do that, provided that the object's state is determined solely by the actions that are performed on it, this allows us to leave the object out of the equation and concentrate on how the actions are combined. And as per usual, the actions (and elements) can be anything, from mixing colors in paint, or adding a quantities to a given set of things etc.
2020-12-28 07:47:53 +03:00
2021-02-24 19:39:55 +03:00
<!--
Free Monoids
===
-->
2020-12-28 07:47:53 +03:00
Other monoid-like objects
2020-10-07 22:13:09 +03:00
===
2021-04-01 10:29:59 +03:00
Monoid operations obey two laws - they are associative and there is an identity element. In some cases we come across operations that also obey other laws that are also interesting. Imposing more (or less) rules to the way in which (elements) actions are combined results in the definition of other monoid-like structures.
2020-10-07 22:13:09 +03:00
2021-01-22 12:50:21 +03:00
Commutative (abelian) monoids
2020-10-07 22:13:09 +03:00
---
2021-01-22 12:50:21 +03:00
Looking at the monoid laws and the examples we gave so far, we observe that all of them obey one more rule (law) which we didn't specify - the order in which the operations are applied is irrelevant to the end result.
2020-10-07 22:13:09 +03:00
2020-12-28 21:52:31 +03:00
![Commutative monoid operation](monoid_commutative.svg)
2020-10-07 22:13:09 +03:00
2021-02-23 14:33:24 +03:00
Such operations (ones for which combining a given set of elements yields the same result no matter which one is first and which one is second) are called *commutative* operations. Monoids with operations that are commutative are called *commutative monoids*.
2020-12-28 07:47:53 +03:00
2021-01-22 12:50:21 +03:00
As we said, addition is commutative as well - it does not matter whether if I have given you 1 apple and then 2 more, or if I have given you 2 first and then 1 more.
2020-12-28 07:47:53 +03:00
2020-12-28 21:52:31 +03:00
![Commutative monoid operation](addition_commutative.svg)
2020-12-28 07:47:53 +03:00
2021-01-22 12:50:21 +03:00
All monoids that we examined so far are also *commutative*, but we will see some non-commutative ones later.
2020-10-07 22:13:09 +03:00
Groups
---
2021-01-22 12:50:21 +03:00
A group is a monoid in which each element has what is called an "inverse" element where the element and its inverse cancel each other out when applied one after the other, in other words , **forall x, there must exist x' such that x • x' = i** ( where **i** is the identity element).
2021-04-01 10:29:59 +03:00
If we view *monoids* as a means of modeling the effect of applying a set of (associative) actions, we use *groups* to model the effects of actions are also *reversible*.
2020-10-07 22:13:09 +03:00
2021-04-01 10:29:59 +03:00
A nice example of a group can be found in the realm of numbers (really, numbers are a nice example of almost all laws) - it is the set of integers under addition, where the inverse of each number is its opposite number (positive numbers' inverse are negatives and vice versa). The above formula, then, becomes **x + (-x) = 0**
2020-12-28 07:47:53 +03:00
2021-01-22 12:50:21 +03:00
The study of groups is a field that is much bigger than the theory of monoids (and perhaps bigger than category theory itself).
2020-10-07 22:13:09 +03:00
2021-01-22 12:50:21 +03:00
And it all started with the what are now called the "symmetry groups" which we will look into in more detail.
2020-10-07 22:13:09 +03:00
2020-12-28 07:47:53 +03:00
Summary
---
2021-01-22 12:50:21 +03:00
The algebraic structures that we saw can be summarized based on the laws that define them in this table.
2020-12-28 07:47:53 +03:00
| | Semigroups | Monoids | Groups |
|---| --- | --- |
|Associativity| X | X | X |
|Identity| | X | X |
|Invertability | | | X |
2021-01-22 12:50:21 +03:00
Symmetry groups
===
An interesting kinds of groups/monoids are the groups of *symmetries* of geometric figures. Given some geometric figure, a symmetry is an action after which the figure is not displaced (e.g. it can fit into the same mold that it fit before the action was applied).
2021-02-01 18:55:32 +03:00
We won't use the balls this time, because in terms of symmetries they have just one position and hence just one action - the identity action (which is it's own reverse by the way). So let's take this triangle, which is the same as any other triangle for our purposes, (as we are not interested in the triangle itself, but in its rotations).
2021-01-22 12:50:21 +03:00
![A triangle](symmetry_group.svg)
Groups of rotations
---
Let's first review the group of ways in which we can rotate our triangle i.e. its *rotation group*. A geometric figure can be rotated without displacement in positions equal to the number of its sides, so for our triangle there are 3 positions.
![The group of rotations in a triangle](symmetry_rotation.svg)
Connecting the dots (or the triangles in this case) shows us that there are just two possible actions that get us from any state to any other one - a *120-degree rotation* (i.e. flipping the triangle one time) and a *240-degree rotation* (i.e. flipping it two times (or equivalently, flipping it once, but in the opposite direction)). Adding the identity action of 0-degree rotation makes up for 3 actions in total.
2021-02-01 18:55:32 +03:00
2021-01-22 12:50:21 +03:00
![The group of rotations in a triangle](symmetry_rotation_actions.svg)
2021-02-01 18:55:32 +03:00
The rotations of a triangle form a monoid - the rotations are actions (of which the zero-degree rotation is the identity) and we can combine two actions by performing the first one and then performing the second one.
2021-04-01 10:29:59 +03:00
Note once again that the elements in the group are the *rotations*, not the triangles themselves, actually the group has nothing to do with the triangles themselves, as we shall see later.
2021-02-01 18:55:32 +03:00
2021-01-22 12:50:21 +03:00
Cyclic groups/monoids
---
2021-04-01 10:29:59 +03:00
Enumerating all the rotations of a more complex geometrical figure looks quite messy at first.
2021-01-22 12:50:21 +03:00
![The group of rotations in a more complex figure](symmetry_rotation_square.svg)
2021-01-27 12:41:58 +03:00
But it's much simpler to grasp if we notice the following: although our group has many actions, and there are more still for figures with more sides (if I am not mistaken, the number of actions is equal to the number of the sides), all of those actions can be reduced to the repetitive application of just the simplest action, (the 120-degree rotation for triangles and the 45-degree rotation for octagons). Let's make up a symbol for this rotation.
2021-01-22 12:50:21 +03:00
![The group of rotations in a triangle](symmetry_rotation_cyclic.svg)
2021-01-27 12:41:58 +03:00
Groups and monoids that have this "main" action (called a "generator") that, when applied enough times, can get you to any state are called *cyclic groups*. All rotation groups are cyclic groups. Another example of a cyclic groups is, yes, the integers under addition. Here we can use **+1** or **-1** as generators.
2021-01-22 12:50:21 +03:00
![The group of numbers under addition](numbers_cyclic.svg)
Wait, how can this be a cyclic group when there are no cycles? This is because the integers are an *infinite* cyclic group.
2021-01-27 12:41:58 +03:00
An example of a finite number-based cycle group are the integers in *modular arithmetic* (sometimes called "clock arithmetic"). Modular arithmetic's operation is based on a number called the modulus of an arithmetic (let's take **12** for example). In it, each number is mapped to the *remainder of the integer addition of that number and the modulus*.
2021-01-22 12:50:21 +03:00
For example: **1 modulo 12 = 1** (because 1/12 = 0 with 1 remainder) **2 modulo 12 = 2** etc.
but **13 modulo 12 = 1** (13/12 = 1 with 1 remainder) **14 modulo 12 = 2**, **15 modulo 12 = 3** etc.
In effect numbers "wrap around" upon reaching the modulus.
Here is a group representation of modular arithmetic with modulus 3.
![The group of numbers under addition](numbers_modular.svg)
Here are a couple of interesting facts about cyclic groups.
2021-01-27 12:41:58 +03:00
1 All cyclic groups that have the same number of elements (or of the *same order*) are isomorphic to each other i.e. they are the same group. For example, the group of rotations of the triangle is isomorphic to the integers under the addition with modulo 3. This group is called **Z3**.
2021-01-22 12:50:21 +03:00
![The group of numbers under addition](symmetry_modular.svg)
2 All cyclic groups are *commutative* (or "abelian" as they are also called). There are, however abelian groups that are not cyclic, but, as we shall see below, the concepts of cyclic groups and of abelian groups are deeply related.
**Task:** Prove that there are no other groups with 3 objects, other than **Z3**.
The smallest groups
---
Like with sets, the concept of an isomorphism in group theory allows us to identify common groups, like the smallest groups.
2021-02-23 14:33:24 +03:00
The smallest group is just the trivial group **Z1** that has just one element.
2021-01-22 12:50:21 +03:00
![The smallest group](trivial_group.svg)
2021-02-23 14:33:24 +03:00
The smallest non-trivial group is the group **Z2** that has two elements.
2021-01-22 12:50:21 +03:00
![The smallest non-trivial group](smallest_group.svg)
**Z2** is also known as the *boolean group*, due to the fact that it is isomorphic to the **{ True, False }** set.
2021-02-23 21:13:30 +03:00
Like **Z3**, **Z1** and **Z2** are cyclic.
2021-01-22 12:50:21 +03:00
Group products
---
Given any two groups we can create a third group, called their *product group*, comprised of all possible pairs of elements from the two groups and of the sum of all their actions.
To demonstrate how, let's take the following two groups (which, having just two elements and one operation, are both isomorphic to **Z2**) - the first one is based on the vertical reflection of a figure and the second, just the horizontal reflection.
![Two trivial groups](groups_product.svg)
2021-04-01 10:29:59 +03:00
We get set of elements of the new group by making *the Cartesian product* of the set of the elements of the first group and the set of the element of the second one.
2021-01-22 12:50:21 +03:00
![Two trivial groups](groups_product_four.svg)
2021-01-27 12:41:58 +03:00
The *actions* of the product group are comprised of the actions of the first group, combined with the actions of the second one, where each action is applied only on the element that is a member of its corresponding group leaving the other element unchanged.
2021-01-22 12:50:21 +03:00
![Klein four](klein_four_as_product.svg)
The result of our particular operation is called the **Klein four-group** and is the simplest *abelian non-cyclic* group.
Another way to present the Klein-four group is the *group of symmetries of a non-square rectangle*.
![Klein four](klein_four.svg)
Task: prove that the two representations are isomorphic.
The Klein-four groups is *non-cyclic* because there are not one, but two main actions - vertical and horizontal spin. It is *abelian*, because the ordering of the actions still does not matter for the end results - this is because the actions do not interfere with one another.
Fundamental theory of Finite Abelian groups
---
We already saw one way to create non-cyclic abelian groups - by creating a product of two or more cyclic groups. All product groups (except the ones that feature the trivial group) would necessarily be non-cyclic, and they would also be abelian. The fundamental theory of finite abelian groups is a result that tells us that this is the only way to produce non-cyclic abelian groups.
> All abelian groups are either cyclic or products of cyclic groups.
And this is how the two concepts are related.
Groups of rotations and reflections
---
Now let's see a non-commutative group - the group of rotations *and reflections* of a given geometrical figure. It is the same as the last one, but here besides the rotation action that we already saw (and its composite actions), we have the action of flipping the figure vertically, an operation which results in its mirror image:
![Reflection of a triangle](reflection.svg)
Those two operations and their composite results in a group called **Dih3** that is not abelian (and is furthermore the *smallest* non-abelian group).
![The group of rotations and reflections in a triangle](symmetry_reflection.svg)
**Task:** Prove that this group is indeed not abelian.
**Question:** Besides having two main actions, what is the defining factor that makes this and any other group non-abelian?
Monoids as Categories
===
2021-02-23 14:33:24 +03:00
We began by defining a monoid as a set of composable elements, but, as we saw, those elements can be also seen as actions e.g. the **red ball** in our color-blending monoid can be seen as the action of **adding the color red** to the mix. By the same token, the number **2** in the monoid of addition can be seen as the function **+2**. Because actions are basically functions from a set of the monoid's elements to itself, there is a way to model monoids using set theory.
2021-01-22 12:50:21 +03:00
So let's consider the monoid/group **Z3** again.
![The group of rotations in a triangle - group notation](symmetry_rotation_actions.svg)
2021-02-23 14:33:24 +03:00
We can see **Z3** as composed of **3** functions between that one set and itself, where we should have a function to go from any element of the set to any other one (due to which the set would have **3** elements as well).
2021-01-22 12:50:21 +03:00
![The group of rotations in a triangle - set notation](symmetry_rotation_functions.svg)
2021-02-23 14:33:24 +03:00
Those functions obviously compose, as all functions do, and we do have the *identity function* too, so monoids are totally categories.
2021-01-22 12:50:21 +03:00
![The group of rotations in a triangle - set notation](symmetry_rotation_set.svg)
2021-01-22 15:40:10 +03:00
The diagram above is a good representation of the category **Z3** in terms of set theory, having all of it's objects and morphisms (except the identity one, but it goes without saying).
So let's try to draw an external (categorical) diagram of the same category.
2021-01-22 12:50:21 +03:00
![The group of rotations in a triangle - categorical notation](symmetry_rotation_external.svg)
2021-01-22 15:40:10 +03:00
But wait, if *sets* in set theory correspond to *objects* in category theory and if a monoid is just one set, then the corresponding category would have just one object and therefore it is better represented as just one point from which all arrows come and to which they go (this representation would also show (correctly) that all morphisms in the category compose with one another).
2021-01-22 12:50:21 +03:00
![The group of rotations in a triangle - categorical notation](symmetry_rotation_category.svg)
2021-01-22 15:40:10 +03:00
All monoids' external diagrams would have just one object - the only difference would be the number of morphisms that the object has.
2021-01-27 12:41:58 +03:00
The intuition behind this representation is encompassed by the requirement of **closure** that monoid and group operations have - it is the law that applying the operation on any two elements of the set of elements that form the monoid always results in an element that is also a member of the set e.g. no matter how do you flip a triangle, you'd still get a triangle.
2021-01-22 12:50:21 +03:00
| | Categories | Monoids | Groups
|---| --- | --- |
|Associativity| X | X | X |
|Identity| X | X | X |
|Invertability | | | X |
|Closure | | X | X |
2021-01-22 15:40:10 +03:00
When we view a monoid as a category, this law says that all morphisms in the category should be from one object to itself - a monoid, any monoid, can be seen as a category with one object.