# Arrows to Arrows, Categories to Queries

> A programming language compiles to SQL using category theory.
- Title: Arrows to Arrows, Categories to Queries | Shortify
- Summary: A programming language compiles to SQL using category theory. Author created catlang, a programming language that compiles to SQL via category theory.
- Keywords: haskell, category theory, programming, catlang, sql, arrow notation, compiler, intermediary language, postgres, Reasonablypolymorphic
- Source: Reasonablypolymorphic — https://reasonablypolymorphic.com/blog/arrows-to-arrows
- Read time: 1 min
- Topics: haskell, category theory, programming, catlang, sql, arrow notation, compiler, intermediary language, postgres, technology, startups
## Catlang Compiles to SQL
Author created catlang, a programming language that compiles to one giant SQL SELECT statement.

> I’ve written a little programming language that compiles to sql
## Count Function Example
Count function returns 100 using arrow notation with loops and conditionals.

> count : Int -> Int
## Category Theory Basics
Category theory uses arrows and types without function application.

> We do not have the notion of function application
## Product and Coproduct
Product types use projections, coproducts use injections and branching.

> prj₁ :: (a, b) ~> a
## Stack-Based Compilation
Compiler eliminates variable names using stack-based representation.

> No variable names anywhere to be found
## Cochoice for Loops
Cochoice operation implements loops by recursively processing either values.

> cochoice :: ( Either a c ~> Either b c) -> (a ~> b)
## SQL Implementation
SQL implements category theory operations with nested queries and unions.

> Composition corresponds to a nested query
## Recursive CTE for Loops
Loops use recursive CTEs with clock timestamps to track iterations.

> clock_timestamp() as step
## Brainfuck in Postgres
Author plans to compile brainfuck to sql for postgres execution.

> Write a little brainfuck interpreter in catlang
## Haskell Inspiration
Catlang is inspired by haskell's arrow syntax and category theory.

> Source language of catlang is heavily inspired by haskell's arrow syntax
## Compiler Backend
Compiler backend uses stack representation for efficient sql generation.

> No compiler backend needs to deal with names
## Future Plans
Author plans to add string-crunching primitives for brainfuck interpreter.

> Compiler still needs a few string-crunching primitives
## GitHub Repository
Code available on github with template-thaskell backend for haskell.

> All of the code is available on github
## Category Theory Background
Category theory underpins programming language design and compilation.

> Lambda calculus corresponds to cartesian-closed categories
## Compiler Limitations
Compiler has limitations in handling complex data structures and loops.

> Cochoice is the only really tricky thing
## Next Steps
Author will focus on improving compiler and adding new features.

> I’ve run out of vacation time to work on this project
## Key takeaway

Author created catlang, a programming language that compiles to SQL via category theory.