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) : ResultConceptually, Result is a sum of the Success and Failure alternatives. In mathematical terms, this is written as:
Result = Success + FailureOn 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 × IntAlgebraic Constants
Just as algebra has constants like zero and one, type theory has equivalent constructs:
These constants give rise to important algebraic laws:
A × 0 ≅ 0 → A product with an uninhabited type is also uninhabited.
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) : PaymentConceptually:
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 AThis 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:
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:
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:
| Algebra | Type-theory idea | Kotlin analogue / mental model |
|---|---|---|
0 | No inhabitants | Nothing |
1 | Exactly one inhabitant | Unit |
A + B | Either A or B | sealed interface + alternatives |
A × B | Both A and B | Pair<A, B> / data class |
1 + A | Empty or A | Maybe<A> / A? as practical analogy |
A × (B + C) | Product over sum | sealed alternatives containing A |
A → B | Function / 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 + AThis 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 AKotlin'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) : ResultThis 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 AThis 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.
