Semigroup & Monoid
ElementryPrerequisites
You add numbers, join strings, and merge lists every day as a programmer. These feel like completely different operations — but they all secretly share the same mathematical shape. Once you know that shape by name, you’ll start spotting it everywhere, and recognising it will help you reason about code more clearly.
What is a binary operation?
A binary operation on a set is a rule that takes two elements of and produces one element, also in .
More precisely, a binary operation is a function:
The symbol is just a placeholder — the actual operation might be , , string concatenation, or anything else you can think of.
The crucial constraint is closure: the result of combining two elements of must land back inside . If you add two natural numbers, you get another natural number — not a string, not a fraction, another natural number. The operation never escapes the set.
A few concrete examples:
- on : , still in . ✓
- on : , still in . ✓
- Concatenation on the set of all strings:
"hello"" world""hello world", still a string. ✓ - on : , still in . ✓
The key property: associativity
Not all binary operations are created equal. The most important property a binary operation can have is associativity.
An operation on is associative if, for every :
In plain English: when combining three things in a row, where you place the parentheses doesn’t matter. You can group the first two elements first, or the last two first — the answer is always the same.
Let’s check this for addition on :
Same result either way. Addition is associative.
String concatenation is also associative. Let’s verify:
Same result. The grouping truly does not matter.
Now consider subtraction on the integers . Is that associative?
Different results — subtraction is not associative. Moving the parentheses changes the answer.
Associativity is powerful because it means you can safely drop all the parentheses in a long chain of operations. Instead of worrying about whether to write or , you write with no ambiguity whatsoever.
Semigroups
You now have everything you need to understand the first structure.
Definition. A semigroup is a pair where is a non-empty set and is a binary operation on that is associative.
That is the entire definition — a set and an associative operation. Nothing else is required.
Examples of semigroups
Positive integers under addition. Let . Addition is a binary operation (the sum of two positive integers is a positive integer) and is associative. So is a semigroup.
Natural numbers under multiplication. The product of two natural numbers is a natural number, and multiplication is associative, so is a semigroup.
Non-empty strings under concatenation. Let be the set of all strings with at least one character. Concatenating two non-empty strings always gives a non-empty string, and concatenation is associative. So is a semigroup.
Natural numbers under maximum. is always a natural number when and are, and:
Both sides simply equal the largest value among , , and . So is a semigroup.
A non-example
, the integers under subtraction, is not a semigroup: you just saw above that subtraction fails associativity.
What’s missing: the identity element
Semigroups are already useful, but there is something extra that some of them have and others don’t.
Think about what happens when you add to any natural number:
Or when you concatenate the empty string "" with any string:
The number and the string "" each play a special role: they don’t do anything. Combining them with any element just gives that element back unchanged. This special element is called an identity element.
Formally, is an identity element for if, for every :
Both conditions must hold. The identity must be neutral whether it appears on the left or on the right.
When does a semigroup have an identity?
Not every semigroup does. Take — positive integers under addition. For an identity element to exist, you would need some positive integer such that for every . That means , but is not a positive integer. So has no identity element.
Likewise, — non-empty strings under concatenation — has no identity: the empty string "" would work perfectly, but it isn’t in the set of non-empty strings.
Whether or not an identity exists often comes down to whether you’ve been careful to include the “empty” or “zero” case in your set.
Monoids
A semigroup that has an identity element earns a new name.
Definition. A monoid is a triple where is a semigroup and is an identity element for .
Every monoid is a semigroup, but not every semigroup is a monoid. The extra ingredient is exactly the identity element.
Examples of monoids
| Set | Operation | Identity |
|---|---|---|
| All strings | concatenation | "" |
| All lists | concatenation | [] |
The last row might surprise you. Is really an identity for on ?
Yes — because is the smallest natural number, taking the max of with any always returns . So is indeed a monoid.
The identity is unique
A monoid has exactly one identity element — you can never have two different ones. Here’s why: suppose both and are identity elements for the same operation. Then:
The first equality holds because is an identity (it changes nothing on the right). The second equality holds because is an identity (it changes nothing on the left). So and must be the same element.
This is reassuring: once you find an identity, you know it’s the identity.
A case study: natural number addition from Peano axioms
If you’ve read the Peano Axioms checkpoint, you’ll remember that addition on is defined recursively using just two rules:
where means “the successor of ”, i.e. the number right after .
From these two rules alone, you can prove that is a monoid — you don’t need to take it on faith.
Identity (right side). Rule (A1) directly states , so is a right identity. ✓
Identity (left side). The claim requires a short induction on :
- Base case (): by (A1). ✓
- Inductive step: assume . Then by (A2) and the assumption. ✓
So is also a left identity, confirming it is the identity element. ✓
Associativity. The proof that is a similar induction on , using (A1) and (A2) at each step. The details are a good exercise in proof by induction.
The upshot: the monoid is not just a pattern you observe in examples — it is a theorem that follows from Peano’s definitions. The structure is baked into the axioms themselves.
Summary
- A binary operation on a set maps any two elements of to another element of — this is called closure.
- An operation is associative if always holds. Associativity lets you remove parentheses from long chains without changing the result.
- A semigroup is a set equipped with an associative binary operation. Examples: , , and non-empty strings under concatenation.
- An identity element satisfies for every . When an identity exists, it is unique.
- A monoid is a semigroup that also has an identity element. Examples: , , all strings under concatenation with identity
"". - Not every semigroup is a monoid: and non-empty strings under concatenation are semigroups without an identity.
- From the Peano axioms, you can prove that is a monoid — the structure is a logical consequence of the definitions of , , and .