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%2F20260510%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260510T091355Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=5a1c24f7e89af08fcb92adcc11d1dea68dbe40d5e92cbc59b1b47ce0be2f1da4ad5ecb12ce3ff0470b65c61dae0926b09f48995c963cb88c3b778ea28b6e8161700af7336672ff08d8367f49837e132450f148bd6a29b857dc4d1936f1fc27a08b6508af4838ab7c412b125d5849e881f42dad2fd913ac3742d99ea6d480408017dfa17288e03e821c367dfa5fcf6ead791b699fa4b7613278f97ced92ba4460e480b5fe86275b5b36dd4b11c4294496829d8e6ceddc0770f76da1b8f55bf37a8d8fdbecb0a23b382d8119a2d6e27060d60b9cbc627e924348ae34e4e2b667ce63046ef7353b8191ef834b4d698858569ee65d1c3c90823cfe00ca22c9cae5a1

    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%2F20260510%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260510T091355Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=0d8b38940186a0be8171981125743c16cd5899f2c63737adc86a0f587e54365213f733c2a16c1067eace0bd66ccf09e8d59ee3df62dd3319fb4b69590dfa7f06a35746eb016f5be5e9fa1f2f4a911dc53a52ef582df19491f87c67d093fadbf9bcbd4a049585b4dd165500053b1c65e21ad03b9dcd71bcd33485ce1de6895820ba97dc9e0fa6019ea294c0d0ba8d8be8ea97658fa8486480daeba665e41f9baa9564333b9e2cec03ae650761add54c62142052e2c616b04c3009a18153bd7d744969ace92720442131f8167572bcabf1b719d757a162a57b3ee5442a4eb1f4b8e7795bf2613ec2cccabc869ef610c458d22e58bb1871b601503e765ecf9d2a87

    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