Isomorphism — When Two Different Models Are Actually the Same
Sometimes the hardest bugs come from believing two structures are different when they actually contain the same information.
In real software systems we constantly transform data:
Database → Domain model → API response → UI modelEach layer uses a different structure.
But sometimes those structures are not fundamentally different — they just look different.
Mathematics calls this relationship isomorphism.
Two objects are isomorphic if they can be converted back and forth without losing information.
The Mathematical Idea
In category theory we describe relationships using morphisms (arrows).
If we have two objects:
A
BAn isomorphism exists when there are functions:
f : A → B
g : B → Asuch that:
g ∘ f = id_A
f ∘ g = id_BMeaning:
You get exactly the original value. Nothing was lost.
Mathematically we write:
A ≅ BVisual Intuition
The arrows undo each other.
Real Programming Example
Consider a database row returned from a query.
id | name | email
-----------------------
1 | Arun | arun@email.comIn Kotlin we usually represent this as a domain model.
data class User(
val id: Int,
val name: String,
val email: String
)Now we define conversion functions.
Database row → object
fun Map<String, Any>.toUser(): User =
User(
id = this["id"] as Int,
name = this["name"] as String,
email = this["email"] as String
)Object → database row
fun User.toRow(): Map<String, Any> =
mapOf(
"id" to id,
"name" to name,
"email" to email
)Now observe what happens.
val row = mapOf(
"id" to 1,
"name" to "Arun",
"email" to "arun@email.com"
)
val user = row.toUser()
val back = user.toRow()Result:
back == row
Convert again:
row → User → row
User → row → UserNo information changed.
So mathematically:
Row ≅ UserThe database representation and the domain model contain the same structure. They are isomorphic representations of the same data.
Why This Matters in Real Systems
This pattern appears everywhere in software:
| Representation A | Representation B |
|---|---|
| Database row | Domain model |
| JSON | Data class |
| DTO | Entity |
| API request | Domain object |
They look different, but often they contain exactly the same information.
Recognizing this helps developers:
Because when two structures are isomorphic, we know that nothing is lost during conversion.
The Core Insight
Isomorphism teaches a powerful lesson:
Representation ≠ Meaning
Two structures may look different. But if they can convert back and forth perfectly, they are the same structure in disguise.
And in category theory, that relationship is written as:
A ≅ B
