Algebra of Types — When Data Structures Start Behaving Like Math
There is a point in programming where types stop feeling like containers and start feeling like equations.
At first, this sounds abstract. But once you see it, it becomes one of the most practical mental models you can have for designing systems.
This idea is called the Algebra of Types.
Why Think About Types as Algebra?
When we write code, we constantly combine data:
These combinations are not random. They follow patterns — and those patterns behave exactly like addition and multiplication in mathematics.
The Building Blocks of Algebraic Data Types
One of the beautiful ideas in functional programming is that types begin to behave like algebra. Not metaphorically — almost literally. Once you see this, many functional programming concepts suddenly become much easier to reason about.
Here’s the basic mapping:
| Math | Types (Kotlin intuition) |
|---|---|
| 0 | Nothing (no value exists) |
| 1 | Unit (exactly one value) |
| a + b | Either<A, B> |
| a × b | Pair<A, B> |
These become the foundation of Algebraic Data Types.
What makes ADTs powerful is not just the mathematics — it’s the practical benefits they bring to real-world software design.
They help us build systems that are:
ADTs are especially useful for modeling:
They also help eliminate many common problems like:
unclear business logic
And perhaps the most interesting part is this:
From just two simple ideas —
AND (×)
OR (+)we can model surprisingly complex systems.
The fascinating part is that these tiny algebraic ideas scale much further than they initially appear.
What starts as simple type composition gradually evolves into a completely different way of thinking about software — one that helps model state, behavior, workflows, transformations, and even entire application architectures more predictably and safely.
In the upcoming posts, we’ll slowly build that intuition step by step and see how these mathematical ideas quietly power many concepts used in modern functional programming and real-world systems.
