# Catlang: Compiling Category Theory to SQL Queries

A novel programming language that transforms abstract categorical semantics into complex SQL execution plans.

- **Source:** [Hacker News: Front Page](https://brifing.si/source/b07aa772-b1a0-4a76-aa18-719c6f9e2dde)
- **Published:** 2026-11-04
- **Topic:** [Software & Engineering](https://brifing.si/c/engineering)
- **Original article:** <https://reasonablypolymorphic.com/blog/arrows-to-arrows/>
- **On Brifing:** https://brifing.si/s/5afac744-7fc7-4774-a8a0-a92add07e2fb

## The Catlang Experiment

Catlang is a custom programming language designed to compile directly into a single, massive SQL SELECT statement. Developed in October 2025, the project serves as a practical testbed for implementing abstract category theory concepts. By targeting PostgreSQL, the language demonstrates how high-level functional abstractions can be mapped to relational database execution.

## Arrow-Based Logic

The language utilizes arrow notation to handle control flow, resembling standard procedural blocks while maintaining categorical rigor. A core function, such as a constant return loop, is desugared from source syntax into an intermediate representation. This process allows the compiler to handle complex logic through algebraic transformations rather than traditional imperative structures.

## Intermediate Representation

After desugaring, the code is represented as a series of categorical symbols like composition, cochoice, and distribution. Each symbol possesses specific algebraic semantics, such as the pipe operator for sequential execution. This intermediate layer acts as a universal bridge, enabling the compiler to translate source code into various target domains.

## SQL Compilation Target

The generated SQL output is intentionally verbose and complex, utilizing recursive common table expressions to simulate loop behavior. By leveraging WITH RECURSIVE clauses, the compiler maps the functional loop structure into a relational format. This approach proves that even highly abstract categorical logic can be executed within a standard relational database.

## The Loop Mechanism

The loop implementation uses a recursive CTE to maintain state across iterations. It processes inputs by branching on the absolute value of a calculation, determining whether to continue the loop or return a result. This mechanism effectively mimics a while loop, demonstrating the flexibility of SQL when pushed beyond simple data retrieval.

## Handling Branching Logic

Branching is managed through union operations that handle different execution paths based on input conditions. By renaming Left and Right branches to inl and inr, the compiler maintains consistency with categorical definitions. This structural mapping ensures that the logic flow remains intact even when translated into the rigid syntax of SQL.

## Algebraic Semantics

The core strength of the compiler lies in its reliance on simple, specific algebraic semantics. By defining how each categorical operator behaves, the system can systematically transform code into different targets. This modularity allows for the creation of new compilation targets simply by defining the transformation rules for these base symbols.

## Complexity of Output

The resulting SQL query is significantly more complex than the original source code, reflecting the overhead of mapping functional abstractions to relational operations. The output includes multiple nested subqueries and cross joins to manage state and data flow. This complexity is a direct consequence of the chosen compilation strategy for catlang.

## Functional Programming Roots

Catlang draws heavily from Haskell and category theory, treating programming as a series of mathematical compositions. By focusing on arrows, the language simplifies the desugaring process into a predictable set of transformations. This approach highlights the potential for using functional paradigms to bridge the gap between high-level logic and database engines.

## Practical Implementation Details

The compiler handles variable state by passing tuples through the SQL query structure. Each step of the computation is isolated within its own CTE, ensuring that the state remains consistent throughout the execution. This rigorous approach to data flow management is essential for maintaining the integrity of the categorical logic during translation.

## Testing Abstract Features

The primary motivation for building catlang was to test the feasibility of using category theory as a compilation target. By using SQL as a funny but functional target, the project validates that the intermediate representation is robust enough to handle complex logic. It serves as a proof of concept for categorical compilation.

## Future of Categorical Compilers

This project illustrates that the boundary between programming languages and database queries is more fluid than typically assumed. By treating SQL as a compilation target, developers can explore new ways to express complex logic. The experiment highlights how abstract mathematical frameworks can provide powerful tools for building unconventional and highly expressive compilers.

## Sources covering this story

- https://reasonablypolymorphic.com/blog/arrows-to-arrows/ — 

---

_Summary written by Brifing from the original reporting. Copyright in the original article belongs to its publisher; read the full piece at the link above._
