Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Products — When Two Independent Things Become One Structure

In programming, we combine values all the time.

But rarely do we ask a deeper question:

Is there a correct way to combine things?

Not just a convenient way. Not just a common pattern. But a way that is universally right.

Category theory answers this using something called a universal construction. Instead of defining a structure directly, it defines it by how it relates to everything else.

And when we ask:

“What is the best way to combine two things?”

This perspective leads us to a fundamental idea:

The Product.

A product is not just a pair of values. It is the *most universal* way of combining them.

To make this concrete, consider something simple:

Latitude and longitude. Individually, they are just numbers. But when combined correctly, they represent a precise location on Earth. And when combined incorrectly… everything breaks.

So what does it mean to combine them correctly?

That’s exactly what the idea of a Product captures.

The Mathematical Idea

In category theory, a product of A and B is:

An object (A, B) with two projections:
π₁ : (A, B) → A  
π₂ : (A, B) → B

These just mean:

  • take first value
  • take second value
  • The Pattern (What really matters)

    We don’t define product by structure.

    We define it by behavior:

          c
         / \
        p   q
       /     \
      A       B
    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/product.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260326%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260326T075115Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=29ed1babfeec34e222b7f587a870289e03c339a72a98f76cf5d7f7242f00ee2d0efaaa7a41d8ad1e3014962cb18cee935411030bdf60c1e6a45bc80100679d5f7188a2a4fc6b2bff4243b4ee20481e1a1b2e836ff68aec54cdc01666f1bdd353a7538837b3649f6ad0ca900efb44f807477f0a6fcf329c8c2147c05d1f7f0fb690fd93efe576715ed30ef0f3085f9309978c12b76b535344b8bede956e02c7e711779ef800813c19a2a6e84914669e47ec79dd17b7f38c6ffb7d9528c7535a097505f9124400808d4086ed95fb801107cd38b5b003da37b5565e5aa533c40c9ea0bf205bd23085e006c08e1b06e3c50788437e091a1c3091851b6832224cb50f

    Key Idea

    If you have:

    p : C → A  
    q : C → B

    Then there must exist a unique function:

    m : C → (A, B)

    Factorizer (The Heart of Product)

    This function is called the factorizer:

    m(x) = (p(x), q(x))

    Kotlin Implementation

    fun <C, A, B> factorizer(
        p: (C) -> A,
        q: (C) -> B
    ): (C) -> Pair<A, B> = { x ->
        Pair(p(x), q(x))
    }

    Real Example — Latitude & Longitude

    Domain

    data class Location(
        val latitude: Double,
        val longitude: Double
    )

    Projections

    val getLatitude: (Location) -> Double = { it.latitude }
    val getLongitude: (Location) -> Double = { it.longitude }

    Type matches, meaning is wrong(Compiles!)

    val wrong = { loc: Location ->
        Pair(getLatitude(loc), getLatitude(loc)) // bug
    }

    Output:

    (12.97, 12.97)

    Here Longitude lost, Type system didn’t help

    Correct (Factorizer)

    val correct = factorizer(getLatitude, getLongitude)
    
    println(correct(Location(12.97, 77.59)))
    // (12.97, 77.59)

    Why Factorizer Matters

    It enforces laws:

    fst(m(x)) = p(x)  
    snd(m(x)) = q(x)

    Any function that breaks this is not a product

    Without ProductWith Product
    Many ways to combineOnly ONE valid way
    Easy bugsGuaranteed correctness
    No structureMathematical guarantee

    Real Use Case — Parallel Data Fetch

    A screen needs to show user info along with their posts.

    Define Combine

    fun <C, A, B> combine(
        f: (C) -> A,
        g: (C) -> B
    ): (C) -> Pair<A, B> = { x ->
        Pair(f(x), g(x))
    }

    Use combine

    val fetchAll = combine(
        ::fetchUser,
        ::fetchPosts
    )
    
    val result = fetchAll(userId)

    We are combining two independent computations into one.

    Parallel Version

    fun <C, A, B> combineAsync(
        f: suspend (C) -> A,
        g: suspend (C) -> B
    ): suspend (C) -> Pair<A, B> = { x ->
        coroutineScope {
            val a = async { f(x) }
            val b = async { g(x) }
            Pair(a.await(), b.await())
        }
    }
    val fetchAll = combineAsync(
        ::fetchUser,
        ::fetchPosts
    )
    
    val result = fetchAll(userId)

    When values are independent but needed together, they form a Product.

    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/product_factorization.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260326%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260326T075115Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=3070fc8bc39667ceff7a59238d0097d557f15eb5aced47740d2c2822e4d5a0d78a7e12572c3de353bc0a92f11b8d91b98a7adc7b81232a36cdd4f331ff1c79eec9be25ade5913a61ee3b0f0738d702fda34d7fcde4c6a826a7d2bb84de976d316561fcd970deb62eb12c37593caa4a4a51c50e7ef3256bfe1709f1c12c114ab057654048523a7d2c33aa40745135e219025ebaef3977933bec4ef72129ae1b7d973703bd3e52c0c1db35fcec3704ac24b40116016d2c0db56978c420a03ebaffc3f7ca4556284a1f81a8e1c45c3cbacd6874c5a946a6f65ebf88b4174da38900dd402dd6ad0412a16754a9ab14e2243c01cb8eed0175fc5e608ae9b8bdae259a

    Final Takeaway

    A product is not just a pair.

    It is:

    A structure where **every valid combination must pass through one unique path**

    One-line Conclusion

    Product is not about combining values — it’s about guaranteeing the **only correct way to combine them**
    #TypeTheory#BuildInPublic#EngineeringMindset#ComputerScience#SoftwareDesign#CategoryTheory#DataModeling#FunctionalProgramming#KotlinFP#ProductType#MathForDevelopers#FPFoundations#ProgrammingConcepts#TheoreticalCS#LearnInPublic