Arrows to arrows, categories to queries (2025) reasonablypolymorphic.com · 19 po...
HN Best 200 @best
Arrows to arrows, categories to queries (2025) reasonablypolymorphic.com · 19 points · 0 comments · HN thread
TL;DR The author describes catlang, a small categorical programming language whose compiler emits one large PostgreSQL SELECT statement, so the program runs inside postgres. The post walks through an example counting loop, the intermediate category-theory representation (arrows, products, coproducts, cochoice), and how each operation maps to SQL, including loops as recursive CTEs.
- catlang is a programming language whose compiler outputs a single SQL query; running it in postgres 17 returns the program's output (the example returns 100 by actually looping up to 100).
- The source syntax is modeled on Haskell's arrow notation, and desugars into an intermediate language built from eight primitives: id, composition, fork, projections, injections, join and cochoice.
- Products are flattened into columns and coproducts into nullable columns with exactly one non-null per row; composition becomes nested queries, forks become CROSS JOINs, and joins become UNIONs.
- Loops are compiled from cochoice into a recursive CTE; to pick the final row, the author orders by clock_timestamp() as a step column, which he calls a hack that could break with multi-row primitives.
- Future ideas include string primitives, a brainfuck interpreter in catlang, and running postgres inside postgres; the author notes the repo is not up to his usual standards and is on GitHub.
Discussion No comments have been posted yet, so there is no discussion thread to summarize. The story has 19 points, which indicates some readers found it interesting but none has yet written a response.