Semigroup & Monoid

Elementry
Last updated: Tags: Group Theory, Abstract Algebra

Prerequisites

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 SS is a rule that takes two elements of SS and produces one element, also in SS.

More precisely, a binary operation is a function:

:S×SS\star : S \times S \to S

The symbol \star is just a placeholder — the actual operation might be ++, ×\times, string concatenation, or anything else you can think of.

The crucial constraint is closure: the result of combining two elements of SS must land back inside SS. 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 N\mathbb{N}: 3+5=83 + 5 = 8, still in N\mathbb{N}. ✓
  • ×\times on N\mathbb{N}: 3×5=153 \times 5 = 15, still in N\mathbb{N}. ✓
  • Concatenation on the set of all strings: "hello" ++\mathbin{++} " world" == "hello world", still a string. ✓
  • max\max on N\mathbb{N}: max(3,5)=5\max(3, 5) = 5, still in N\mathbb{N}. ✓

The key property: associativity

Not all binary operations are created equal. The most important property a binary operation can have is associativity.

An operation \star on SS is associative if, for every a,b,cSa, b, c \in S:

(ab)c=a(bc)(1)(a \star b) \star c = a \star (b \star c) \tag{1}

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 N\mathbb{N}:

(2+3)+4=5+4=9(2 + 3) + 4 = 5 + 4 = 9 2+(3+4)=2+7=92 + (3 + 4) = 2 + 7 = 9

Same result either way. Addition is associative.

String concatenation is also associative. Let’s verify:

("ab"++"cd")++"ef"="abcd"++"ef"="abcdef"(\texttt{"ab"} \mathbin{++} \texttt{"cd"}) \mathbin{++} \texttt{"ef"} = \texttt{"abcd"} \mathbin{++} \texttt{"ef"} = \texttt{"abcdef"} "ab"++("cd"++"ef")="ab"++"cdef"="abcdef"\texttt{"ab"} \mathbin{++} (\texttt{"cd"} \mathbin{++} \texttt{"ef"}) = \texttt{"ab"} \mathbin{++} \texttt{"cdef"} = \texttt{"abcdef"}

Same result. The grouping truly does not matter.

Now consider subtraction on the integers Z\mathbb{Z}. Is that associative?

(103)2=72=5(10 - 3) - 2 = 7 - 2 = 5 10(32)=101=910 - (3 - 2) = 10 - 1 = 9

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 (ab)c(a \star b) \star c or a(bc)a \star (b \star c), you write abca \star b \star c with no ambiguity whatsoever.

Semigroups

You now have everything you need to understand the first structure.

Definition. A semigroup is a pair (S,)(S, \star) where SS is a non-empty set and \star is a binary operation on SS 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 Z+={1,2,3,}\mathbb{Z}^+ = \{1, 2, 3, \ldots\}. Addition is a binary operation (the sum of two positive integers is a positive integer) and is associative. So (Z+,+)(\mathbb{Z}^+, +) is a semigroup.

Natural numbers under multiplication. The product of two natural numbers is a natural number, and multiplication is associative, so (N,×)(\mathbb{N}, \times) is a semigroup.

Non-empty strings under concatenation. Let S+S^+ 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 (S+,++)(S^+, \mathbin{++}) is a semigroup.

Natural numbers under maximum. max(a,b)\max(a, b) is always a natural number when aa and bb are, and:

max(max(a,b),  c)=max(a,  max(b,c))\max(\max(a, b),\; c) = \max(a,\; \max(b, c))

Both sides simply equal the largest value among aa, bb, and cc. So (N,max)(\mathbb{N}, \max) is a semigroup.

A non-example

(Z,)(\mathbb{Z}, -), 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 00 to any natural number:

n+0=nand0+n=nn + 0 = n \qquad \text{and} \qquad 0 + n = n

Or when you concatenate the empty string "" with any string:

s++""=sand""++s=ss \mathbin{++} \texttt{""} = s \qquad \text{and} \qquad \texttt{""} \mathbin{++} s = s

The number 00 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, eSe \in S is an identity element for (S,)(S, \star) if, for every aSa \in S:

ea=aandae=a(2)e \star a = a \qquad \text{and} \qquad a \star e = a \tag{2}

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 (Z+,+)(\mathbb{Z}^+, +) — positive integers under addition. For an identity element ee to exist, you would need some positive integer ee such that n+e=nn + e = n for every nZ+n \in \mathbb{Z}^+. That means e=0e = 0, but 00 is not a positive integer. So (Z+,+)(\mathbb{Z}^+, +) has no identity element.

Likewise, (S+,++)(S^+, \mathbin{++}) — non-empty strings under concatenation — has no identity: the empty string "" would work perfectly, but it isn’t in the set S+S^+ 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 (S,,e)(S, \star, e) where (S,)(S, \star) is a semigroup and eSe \in S is an identity element for \star.

Every monoid is a semigroup, but not every semigroup is a monoid. The extra ingredient is exactly the identity element.

Examples of monoids

Set SSOperation \starIdentity ee
N\mathbb{N}++00
N\mathbb{N}×\times11
All stringsconcatenation""
All listsconcatenation[]
N\mathbb{N}max\max00

The last row might surprise you. Is 00 really an identity for max\max on N\mathbb{N}?

max(0,n)=nandmax(n,0)=n\max(0, n) = n \qquad \text{and} \qquad \max(n, 0) = n

Yes — because 00 is the smallest natural number, taking the max of 00 with any nn always returns nn. So (N,max,0)(\mathbb{N}, \max, 0) 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 ee and ff are identity elements for the same operation. Then:

e=ef=fe = e \star f = f

The first equality holds because ff is an identity (it changes nothing on the right). The second equality holds because ee is an identity (it changes nothing on the left). So ee and ff 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 N\mathbb{N} is defined recursively using just two rules:

m+0m(A1)m + 0 \coloneqq m \tag{A1} m+S(n)S(m+n)(A2)m + S(n) \coloneqq S(m + n) \tag{A2}

where S(n)S(n) means “the successor of nn”, i.e. the number right after nn.

From these two rules alone, you can prove that (N,+,0)(\mathbb{N}, +, 0) is a monoid — you don’t need to take it on faith.

Identity (right side). Rule (A1) directly states m+0=mm + 0 = m, so 00 is a right identity. ✓

Identity (left side). The claim 0+m=m0 + m = m requires a short induction on mm:

  • Base case (m=0m = 0): 0+0=00 + 0 = 0 by (A1). ✓
  • Inductive step: assume 0+m=m0 + m = m. Then 0+S(m)=S(0+m)=S(m)0 + S(m) = S(0 + m) = S(m) by (A2) and the assumption. ✓

So 00 is also a left identity, confirming it is the identity element. ✓

Associativity. The proof that (a+b)+c=a+(b+c)(a + b) + c = a + (b + c) is a similar induction on cc, using (A1) and (A2) at each step. The details are a good exercise in proof by induction.

The upshot: the monoid (N,+,0)(\mathbb{N}, +, 0) 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 \star on a set SS maps any two elements of SS to another element of SS — this is called closure.
  • An operation is associative if (ab)c=a(bc)(a \star b) \star c = a \star (b \star c) always holds. Associativity lets you remove parentheses from long chains without changing the result.
  • A semigroup (S,)(S, \star) is a set equipped with an associative binary operation. Examples: (Z+,+)(\mathbb{Z}^+, +), (N,×)(\mathbb{N}, \times), and non-empty strings under concatenation.
  • An identity element ee satisfies ea=ae=ae \star a = a \star e = a for every aSa \in S. When an identity exists, it is unique.
  • A monoid (S,,e)(S, \star, e) is a semigroup that also has an identity element. Examples: (N,+,0)(\mathbb{N}, +, 0), (N,×,1)(\mathbb{N}, \times, 1), all strings under concatenation with identity "".
  • Not every semigroup is a monoid: (Z+,+)(\mathbb{Z}^+, +) and non-empty strings under concatenation are semigroups without an identity.
  • From the Peano axioms, you can prove that (N,+,0)(\mathbb{N}, +, 0) is a monoid — the structure is a logical consequence of the definitions of 00, SS, and ++.