Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Written by: Arun Pandian M•Published on: Nov 24, 2026

Algebra of Types in Functional Programming

In functional programming, types are not just about what values can be stored—they can be understood through an elegant lens of algebra. This perspective reveals deep connections between type systems and mathematical structures, offering a powerful way to reason about code.

At the heart of this idea is the algebra of types, where sum types behave like addition, product types behave like multiplication, and special types such as uninhabited and unit types correspond to zero and one, respectively. This analogy is not just abstract—it has practical implications in how we design and reason about data structures and functions.

Sum Types and Product Types

A sum type represents a choice between two or more alternatives. In Kotlin, sum-type-like structures can be modeled using a sealed interface and its subclasses, such as a custom Either<A, B>, which can be either Left<A> or Right<B>. This mirrors the logical OR operation and is analogous to addition in algebra:

sealed interface Result
data class Success(val value: Int) : Result
data class Failure(val message: String) : Result

Conceptually, Result is a sum of the Success and Failure alternatives. In mathematical terms, this is written as:

Result = Success + Failure

On the other hand, a product type represents a combination of two or more values. A Kotlin data class can model a product type by combining multiple fields into one value. This mirrors the logical AND operation and is analogous to multiplication in algebra:

data class User(
    val name: String,
    val age: Int
)

This can be thought of as:

User = String × Int
https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/fp_algebra_of_types_img.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20261125%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261125T141945Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=4221c48064f04ededd983a8301e6d4f5c32d501bed07685c1ea0048b654e28b96c06eb9e8e68912be92a605fb441f277aa4c6e7b1e3a9277deb774ec5babd436ba8db6731ebb4869fb8bf9b92743ad2ed109935b22321d7190711a41e9b89cad7545c9e33002696c493aace92abb709d96439ccc40c350801711bda29d5d667270fe5d23be0d90dcbeef283beb886a8cfd2550c10309a8e010d19d87d35d7e51f491f4a0589bdea8b162d65b4cff08e0edad4029ffc9040580ab6911004151b234675c889590ed9cbf885494626562e8f5bb7d43761fb7bd41b13dc22909a53e1f1181239dc451f51bda2db7ec0ddc0e1ee8d8cd68951a23b279b12cdad2ebde

Algebraic Constants

Just as algebra has constants like zero and one, type theory has equivalent constructs:

  • In Haskell, Void is an uninhabited type—it has no values. Kotlin’s Nothing plays a similar role: it is a type with no values and is commonly used for computations that never successfully return, such as functions that always throw an exception. In the algebra of types, an uninhabited type plays the role of zero.
  • Haskell’s () is the unit type/value with exactly one inhabitant. Kotlin’s Unit plays a similar practical role: it represents a type with a single possible value and is commonly used when a function produces no meaningful result.
  • These constants give rise to important algebraic laws:

    A × 0 ≅ 0 → A product with an uninhabited type is also uninhabited.

  • A × 1 ≅ A → A product with a unit type contains the same information as A.
  • The symbol ≅ means isomorphic: the two types may have different representations, but they contain equivalent information and can be converted between one another without losing information.

    In Kotlin, this is reflected in the Nothing type and the Unit type:

    fun impossible(): Nothing = error("No value")
    fun doSomething(): Unit {
        println("Done")
    }

    Distributivity and Algebraic Laws

    Just as multiplication distributes over addition in algebra, product types distribute over sum types in type theory. This is expressed as:

    A × (B + C) ≅ (A × B) + (A × C)

    In Kotlin, this means that a product of a type with a sum is equivalent to a sum of products. This law is not just theoretical—it helps in reasoning about complex data structures and transformations.

    sealed interface Payment
    
    data class Cash(val amount: Int) : Payment
    data class Card(val number: String) : Payment
    Conceptually:
    
    Payment = Cash + Card
    
    User × Payment
    ≅
    (User × Cash) + (User × Card)

    In other words, a value containing both a User and a Payment can be understood as either a (User, Cash) pair or a (User, Card) pair.

    Recursive Data Structures

    Recursive data structures, such as lists, can also be described algebraically. A list of type A can be seen as:

    List A = 1 + A × List A
    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/fp_recursive_list_exp_img.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20261125%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261125T141945Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=00eb493e6154ce5c04ebec631414ca385aeb994d55918f80fe4d2fc3d5c420359353b119a9aa7c1299f46ef44edc62381fe75124ca23c69320b3b16d3cb409049b4479041385b23871a974c9af27ab99edbf61962fa8e13a0005d5dc79bd50b0ba4fd18f2db98ffac5c1d4df1ca10d360c0e801310be024a4cf0911131cf1dbc91aebd4e0127e67145bd10c983dbb3aa5438772b4b5703fe747ee4c0da90b1c36b41700ed93e5e2172ecc9e5dabd5af4ea7171534832ddb3ba6a890eeaabbbb06a8d787864ac0e95efd6f904176761a88e82502e388d01afff94bf771267197ee4c886707df67dc7c07b285e4e77ed130fc0620fb708fe4e2b997b394e506e01

    This equation captures the idea that a list is either empty, represented by the single Nil alternative corresponding to 1, or non-empty, consisting of a head of type A and a tail of type List A. Expanding this recursively gives:

    List A = 1 + A + A² + A³ + ...

    Here, A² means A × A, A³ means A × A × A, and so on. These terms represent lists containing exactly two, three, or more elements of type A.

    This infinite series representation is a powerful way to think about the structure of lists and similar recursive types.

    Logic and Type Theory

    The algebraic perspective on types extends into logic. In this view:

  • Logical OR corresponds to sum types.
  • Logical AND corresponds to product types.
  • False corresponds to an uninhabited type.
  • True corresponds to a unit type.

    In Haskell, Void can represent the uninhabited type, while Kotlin’s Nothing plays a similar role. Likewise, Haskell’s () and Kotlin’s Unit play the role of a unit type.

    This mapping is central to the Curry–Howard correspondence, which draws a deep connection between logic and type theory. In this framework:

  • A proposition is a type.
  • A proof of a proposition is a value of that type.
  • Logical implication (A ⇒ B) corresponds to a function type (A → B). Under this correspondence, a function of type A → B can be understood as a proof that A implies B.

    Kotlin Cheat Sheet

    To summarize the key concepts in Kotlin:

    AlgebraType-theory ideaKotlin analogue / mental model
    0No inhabitantsNothing
    1Exactly one inhabitantUnit
    A + BEither A or Bsealed interface + alternatives
    A × BBoth A and BPair<A, B> / data class
    1 + AEmpty or AMaybe<A> / A? as practical analogy
    A × (B + C)Product over sumsealed alternatives containing A
    A → BFunction / proof of implication(A) -> B

    These are algebraic correspondences and mental models, not claims that Kotlin’s types are literally identical to their Haskell or mathematical counterparts.

    Maybe as a Sum Type

    A useful example of the algebra of types is the optional-value type:

    Maybe A = 1 + A

    This equation describes a type with two possibilities: either there is no value, or there is a value of type A.

    In Haskell, this idea is represented by:

    Maybe A = Nothing | Just A

    The Nothing alternative contributes the 1, because there is exactly one way to represent the absence of a value. The Just A alternative contributes A, because it contains a value of type A.

    Kotlin's nullable type A? provides a practical way to model the same general idea:

    val name1: String? = null
    val name2: String? = "Arun"

    From an algebraic perspective, the important idea is that an optional value represents one of two alternatives:

    1 + A
    
    absence + presence of A

    Kotlin's A? is a practical analogue of this idea rather than a literal implementation of Haskell's Maybe.

    Core Mental Model

    The key takeaway is that sum types behave like addition, product types behave like multiplication, an uninhabited type plays the role of zero, and a unit type plays the role of one.

    This algebraic perspective also extends to recursive data structures, which can be described as type equations. The same structure that underlies sum and product types also mirrors logical OR and AND, leading toward the Curry–Howard correspondence.

    Once you see this, types like Either, Pair, Maybe, and recursive lists stop looking like unrelated features. They become different expressions of a common algebra of types.

    Code Examples in Kotlin

    Let’s look at a few concrete examples:

    Sum Type Example: Result

    sealed interface Result
    data class Success(val value: Int) : Result
    data class Failure(val message: String) : Result

    This structure allows us to model a computation that can either succeed or fail. Conceptually, it represents a value from one of two alternatives.

    Product Type Example: User

    data class User(
        val name: String,
        val age: Int
    )

    This structure represents a user with both a name and an age. It reflects the product of two values.

    Recursive List Example: MyList

    sealed interface MyList<out A>
    data object Nil : MyList<Nothing>
    data class Cons<A>(
        val head: A,
        val tail: MyList<A>
    ) : MyList<A>

    This is a recursive type that represents a list. It mirrors the algebraic equation:

    List A = 1 + A × List A

    This structure captures the idea that a list is either empty or has a head and a tail.

    Conclusion

    Understanding the algebra of types gives us a compact way to reason about how data structures are constructed and how their possible values relate to one another. Instead of treating sum types, product types, recursive types, and optional values as unrelated features, we can understand them through a small set of algebraic ideas.

    This perspective not only helps in designing and reasoning about data structures but also reveals deep connections between type theory and logic through the Curry–Howard correspondence. With this insight, types such as Either, Pair, optional values, and recursive lists become expressions of a unified algebra rather than isolated features.

    #FunctionalProgramming#Kotlin#SoftwareEngineering#CategoryTheory#TypeSystems#FP#Programming#TypeTheory#ComputerScience#AlgebraicDataTypes#ProductTypes#SumTypes#Either#Maybe#AlgebraOfTypes#CurryHoward#RecursiveTypes
    LAMBDA BRICKS