Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Monoid as a Category — When “Combining” Becomes “Moving”

Most developers first learn a monoid like this:

A set + a binary operation + an identity element

Examples:

  • numbers with addition and zero
  • strings with concatenation and empty string
  • lists with append and empty list
  • It feels like a rule about values being added together.But category theory asks us to look at it differently. Instead of thinking about elements being combined,we think about actions being applied.

    Stop Thinking About Values — Think About Transformations

    Take a very simple monoid: natural numbers with addition.

    Normally we say:

    5 + 7 = 12
    

    But we can reinterpret this in a completely different way.

    Instead of treating 5 as a number, treat it as an action:

    “Add 5 to something.”

    That means 5 is no longer just a value — it becomes a function:

    add5(x) = x + 5
    add7(x) = x + 7

    Now something interesting happens.

    Combining Numbers Becomes Composing Functions

    If we apply add5 and then add7:

    add7(add5(x)) = x + 12

    That is the same as:

    add12(x)

    So:

    Combining numbers is the same as composing transformations.
    5 + 7     ≡     add7 ∘ add5

    We didn’t lose information. We just changed perspective.

    The Identity Appears Naturally

    What is the identity element of addition? 0

    What does add0 do?

    add0(x) = x

    It does nothing. That is exactly the identity function.

    So:

  • zero → identity morphism
  • addition → function composition
  • And composition is always associative:

    (add2 ∘ add3) ∘ add4 = add2 ∘ (add3 ∘ add4)
    

    Not because addition is associative — but because function composition is associative.

    The Big Realization

    We no longer need numbers.

    We only need:

  • one object (the number line)
  • many transformations (adders)
  • This structure is a category with one object.

    And that is precisely what a monoid is.

    A monoid is a single-object category.

    The name even hints at it:

    mono = one

    Visual Intuition

    Instead of imagining a set of values:

    0,1,2,3,4,5...
    

    Imagine a blob — a single space — and arrows that transform it:

    (add 1)
    (add 2)
    (add 3)
    (add 4)
    ...

    All arrows start and end at the same place. They differ only by how they move things inside. That’s a monoid as a category.

    Kotlin Example — Seeing the Same Idea

    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/monoid_int.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260225%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260225T031132Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=2480eb065310a728d75bbf5d14b383e158763688378b5749cdc105acef061c6e8ec698f8a2fb039305300db627d911f2b7689134397a0565666d561669e1a1b3530ced76820a9ec6f3db2bfa05b434d5ccf3bfef3e478af93113398baa2dd7d581d5aba56caad04f70ac0ef6ef1287b050708bedb53482db995a3fc0025a00d6a8d378f88f954278ee890a8c64ff5cc2e081946335ebdbfc4698ad39899f5f1f10e0377bf2a042e18b42b38b6a120300a9a7fa31b7868da8513b91bd7c075622ee231323dcffb85397b7030b387f3f3dea9cb1e1c5783810331571e2f3a3c2e47bf8220ed9c84853b5e42bda463fa6fafae76e268cf2e7859436067c08eadc7a

    Let’s model this idea directly.

    Instead of storing numbers, store operations:

    typealias Endo<A> = (A) -> A
    

    Now define adders:

    fun add(n: Int): Endo<Int> = { x -> x + n }

    Compose them:

    val add5 = add(5)
    val add7 = add(7)
    
    val add12 = { x: Int -> add7(add5(x)) }
    
    println(add12(10)) // 22

    We never added numbers. We composed actions. That’s the categorical monoid.

    Recovering the Usual Monoid

    Here’s the beautiful part. If you take all these arrows:

    add0, add1, add2, add3...

    They form a set.

    And composition between them behaves exactly like addition:

    add5 ∘ add7  →  add12

    So from the category we get the original monoid back.

    This means:

    The algebraic monoid and the categorical monoid are the same idea seen from two angles.

    One sees values. The other sees transformations.

    Why This Matters

    This perspective explains why monoids appear everywhere in programming:

  • reducers
  • middleware chains
  • UI modifiers
  • state transformations
  • function pipelines

    Nothing is being “added”. We are composing behaviors that share a common space.

    #MathForDevelopers#FunctionalProgramming#BuildInPublic#CategoryTheory#FPFoundations#ProgrammingConcepts#KotlinFP#Composability#SoftwareDesign#CleanArchitecture#DeclarativeProgramming#MonoidAsCategory#ComposingActions#Endomorphism#FunctionComposition