QuiverSphere QUIVERSPHERE SUBSCRIBE
QuiverSphere
← Blog

Exploring catlang: a new programming language that compiles to SQL

Discover catlang, a novel programming language that compiles to SQL using category theory principles and offers new programming paradigms.

16 November 2026 · 6 min read
Exploring catlang: a new software-development-amidst-ai-advancements/">programming-language-a-memory-safe-systems-language/">programming language that compiles to SQL

In an era where reflection-capabilities-of-c-zig-and-c3/">programming languages evolve rapidly, a unique entrant has emerged: catlang. This novel programming language expands the boundaries of conventional programming by enabling its compilation directly into SQL, a language predominantly used for querying databases.

Created during a recent break from traditional work, catlang is not merely another query language; it presents a complex synthesis of programming constructs and category theory principles. The ambition behind catlang seeks to provide programmers a humorous yet functional pathway to explore the steep concepts of category theory while achieving practical results in SQL.

What is catlang?

Catlang is designed as a functional programming language that ultimately compiles to a massive SQL query, specifically tailored for PostgreSQL. The language diverges from the traditional approaches and introduces the notion of a higher-level intermediary language. This intermediary language operates heavily on abstract category theoretical principles, which introduces both complexity and versatility.

By building catlang, the creator aimed to experiment with compiling the language's constructs into an SQL representation, emphasizing creativity and the potential for bridging vast programming paradigms.

A closer look at the syntax and constructs

One essential function showcased is the count function, which exemplifies how one can leverage catlang's constructs to achieve a computational result:

count : Int -> Int
count = x -> loop x i -> n <- join id id -< i
                  z <- abs . (-) -< (n, 100)
                  case z of
                    inl _ -> inr . (+) -< (n, 1)
                    inr _ -> inl -< n

At first glance, the syntax of catlang may seem reminiscent of more traditional functional languages, particularly with its use of arrow notation. However, beneath this surface lies a sophisticated manipulation of category theory constructs that deeply informs its design principles.

The intermediary language: a category-themed DSL

Upon deeper examination, after the desugaring of the source syntax within catlang, the resulting intermediary language (IL) presents a series of beautiful yet bewildering syntax primarily consisting of symbols from category theory. The language allows for constructs such as:

id △ id ⨟ cochoice
  (undist ⨟ ((prj₁ ⨟ id ▽ id) △ id ⨟ (prj₁ △ 100 ⨟ (-) ⨟ abs) △ id ⨟ prj₁ △ id ⨟ dist ⨟ ((prj₂ ⨟ prj₂ ⨟ prj₁) △ 1 ⨟ (+) ⨟ inr) ▽ (prj₂ ⨟ prj₂ ⨟ prj₁ ⨟ inl))
  △ prj₂ ⨟ dist)

This representation is rich in mathematical semantics, where symbols like A ⨟ B signify a composition that pipes the output of A into B. This method of design allows the language effectively to create modular pieces that can compile into various output formats, including SQL.

Creating SQL from catlang

Once the program is defined, the next step is converting catlang into SQL. For instance, running a simple computation like counting has a generated SQL query resembling the following:

SELECT f0 AS f0 FROM (
  SELECT f0 AS f0, f1 AS f1 FROM (
    SELECT * FROM (
      WITH t0 AS (SELECT * FROM (
        WITH RECURSIVE recursion AS (
          SELECT clock_timestamp() AS step, *
          FROM (
            WITH t1 AS (SELECT * FROM (
              SELECT f0 AS f0, f1 AS f1, NULL::integer AS f2 FROM (
                WITH t2 AS (SELECT * FROM (SELECT 0 AS f0) AS _)
                SELECT * FROM (
                  SELECT * FROM (SELECT f0 AS f0 FROM t2 AS _) AS _) AS _) AS _
              ) SELECT * FROM (SELECT * FROM (SELECT f0 AS f0 FROM (SELECT f0 AS f0 FROM t10 AS _) AS _) AS _ CROSS JOIN (SELECT f0 AS f1 FROM (SELECT 100 AS f0 FROM t10 AS _) AS _) AS _) AS _) AS _ CROSS JOIN (SELECT f0 AS f1, f1 AS f2, f2 AS f3 FROM t5 AS _)) AS _) SELECT *
             FROM recursion ORDER BY step DESC LIMIT 1) AS _);

This complex SQL statement accurately reflects the inner workings of catlang and represents a compact way to execute a loop and return a computed value.

Insights from category theory

Digging into the core principles, catlang rests on a foundation of category theory. Fundamental elements include types and arrows, with the identity arrow functioning as a basic building block of this system. The language’s design anticipates operations on arrows, enabling bright possibilities for programming and abstraction.

In category theory, the ability to compose arrows heralds a powerful means of constructing programs that are clean, modular, and easily analyzable. This design can be visualized as a series of pathways or processes that link values in intricate ways.

Furthermore, the use of coproducts within catlang allows for unique branching structures, aiding in dynamic programming and functional construction. The interplay between different constructs leads to a powerful expressiveness that straddles diverse domains in programming.

Optimization and Future Directions

The compilation process certainly has room for optimization, particularly in the generated SQL, which can be cumbersome and difficult to read. This complexity belies the relatively straightforward concepts that catlang distills, and refining these SQL outputs will restore clarity while maintaining power.

Additionally, future improvements could see increased capabilities within catlang itself, such as adding more primitives that facilitate string manipulation, which could prove to bolster the language’s expressiveness further.

Moreover, the idea of creating a brainfuck interpreter within the framework of catlang opens myriad opportunities for experimentation and innovation in programming language design, allowing for further exploration of recursion and computational theory.

Conclusion: the journey ahead

While catlang is still a work in progress, the potential inherent in this language is immense. It not only bridges traditional programming with SQL queries but also opens up a realm where category theory can inform language design in novel ways.

As programmers and theorists alike explore catlang, the language could lead to significant advancements in realizing functional programming's full potential in practical environments. Engaging with catlang might inspire a new generation of programmers to blend mathematical theory with hands-on coding in exciting and unforeseen ways.

Frequently asked questions

What is catlang?

Catlang is a programming language that compiles to SQL while employing principles of category theory, creating a unique fusion of programming paradigms.

How does catlang compile to SQL?

The compiler transforms catlang code into a complex SQL query that adheres to PostgreSQL syntax, enabling dynamic computations and logic structures.

What role does category theory play in catlang?

Category theory provides the foundational framework for catlang, allowing it to utilize concepts like arrows and types to achieve modular and expressive programming constructions.