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%2F20260404%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260404T162214Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=71dbe6ace2a570fa3dcec337aef839242ba9473ed8610e3c4523eb6cb4571c9283d5341b4b62d7f1ca8964898c4172320b368b2a69fa65377314c62f62e76ba5b765143e2e34c9447fe7c7edafdf7c7e2b80144da3166b5b385c541ac47405f46330ec6dc016b569905900e67fcea8be37927deae8d9fc57c10f62a08ac6e230833e9e363dd81dbeaa37a3e6816e11fda4e16ee1e6c8c5bb38689a3700136d97981dcf991c53ae47235b85687a0d0a4f2ae6ed5c070bab99eba8c9b3964ee7784fae51735c8de44cfb277d02443a581b86c402f68b302abf8210066373e6d242331fac03fd2a928c5ea0da81609fd25e8bffdf6fc0230b0dab542e0c3c5cbd70

    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%2F20260404%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260404T162214Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=5eba75167f930fd2310accb2978fbe040b0ed60b49773883458a496d4714b656999b154eb8ebe71a41de6693cacceadfaadf274940f9e3f36c3f94ae11cabe92678387148d225f5f1b2f271564c32aeaa2d789181f7b5cc7c8e25aad297603313426a5a028a5a785523eb5ab456ec04ffe18d5cf4eaf4e15bfb8b2bb02c8922fb03202f005ed73b90348d8d6553b896676c993f7f2652d8781191ec7fb645764a30c9a9de8a0cd84d38c7366179836e64bd79c44cd285325fb6792c9107655e7508d60ec1d9fb2649c2ae900850b3a5cc4d1d69aff64ea29a4c37eb9a629cb716d2ad4b953f927dee302c93d3b9ba626134862a10fc2baebcbb0830185320b93

    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