I鈥檝e had a little time off of work as of late, and been spending it in characteristically unwise ways. In particular, I鈥檝e written a little programming language that compiles to SQL. I call it catlang. That鈥檚 not to say that I鈥檝e written a new query language. It鈥檚 a programming language, whose compiler spits out one giant SELECT statement. When you run that query in postgres, you get the output of your program. Why have I done this? Because I needed a funny compilation target to test out the actual features of the language, which is that its intermediary language is a bunch of abstract category theory nonsense. Which I鈥檒l get to. But I鈥檓 sure you first want to see this bad boy in action. Behold, the function that returns 100 regardless of what input you give it. But it does it with the equivalent of a while loop: 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 If you鈥檙e familiar with arrow notation, you鈥檒l notice the above looks kinda like one big proc block. This is not a coincidence (because nothing is a coincidence). I figured if I were to go through all of this work, we might as well get a working arrow desugarer out of the mix. But I digress; that鈥檚 a story for another time. Anyway, what鈥檚 going on here is we have an arrow count, which takes a single argument x. We then loop, starting from the value of x. Inside the loop, we now have a new variable i, which we do some voodoo on to compute n鈥攖he current value of the loop variable. Then we subtract 100 from n, and take the absolute value. The abs function here is a bit odd; it returns Left (abs x) if the input was negative, and Right x otherwise. Then we branch on the output of abs, where Left and Right have been renamed inl and inr respectively. If n - 100 was less than zero, we find ourselves in the inl case, where we add 1 to n and wrap the whole thing in inr鈥攚hich the loop interprets as 鈥渓oop again with this n...
First seen: 2026-01-27 23:07
Last seen: 2026-01-28 00:07